|
| 1 | +require_relative '../test_case' |
| 2 | + |
| 3 | +class TestAncestorsPrecompute < LinkedData::TestCase |
| 4 | + |
| 5 | + def setup |
| 6 | + @indexer = LinkedData::Services::OntologySubmissionIndexer.new(nil) |
| 7 | + end |
| 8 | + |
| 9 | + # A -> B -> C (linear chain) |
| 10 | + def test_linear_chain |
| 11 | + direct_parents = { |
| 12 | + "http://example.org/C" => ["http://example.org/B"], |
| 13 | + "http://example.org/B" => ["http://example.org/A"] |
| 14 | + } |
| 15 | + ancestors_map = {} |
| 16 | + |
| 17 | + compute_all(direct_parents, ancestors_map) |
| 18 | + |
| 19 | + assert_equal Set.new(["http://example.org/B", "http://example.org/A"]), |
| 20 | + ancestors_map["http://example.org/C"] |
| 21 | + assert_equal Set.new(["http://example.org/A"]), |
| 22 | + ancestors_map["http://example.org/B"] |
| 23 | + end |
| 24 | + |
| 25 | + # Root node with no parents |
| 26 | + def test_root_node |
| 27 | + direct_parents = { |
| 28 | + "http://example.org/A" => [] |
| 29 | + } |
| 30 | + ancestors_map = {} |
| 31 | + |
| 32 | + compute_all(direct_parents, ancestors_map) |
| 33 | + |
| 34 | + assert_equal Set.new, ancestors_map["http://example.org/A"] |
| 35 | + end |
| 36 | + |
| 37 | + # A |
| 38 | + # / \ |
| 39 | + # B C |
| 40 | + # \ / |
| 41 | + # D |
| 42 | + def test_diamond_inheritance |
| 43 | + direct_parents = { |
| 44 | + "http://example.org/D" => ["http://example.org/B", "http://example.org/C"], |
| 45 | + "http://example.org/B" => ["http://example.org/A"], |
| 46 | + "http://example.org/C" => ["http://example.org/A"] |
| 47 | + } |
| 48 | + ancestors_map = {} |
| 49 | + |
| 50 | + compute_all(direct_parents, ancestors_map) |
| 51 | + |
| 52 | + assert_equal Set.new(["http://example.org/B", "http://example.org/C", "http://example.org/A"]), |
| 53 | + ancestors_map["http://example.org/D"] |
| 54 | + assert_equal Set.new(["http://example.org/A"]), |
| 55 | + ancestors_map["http://example.org/B"] |
| 56 | + assert_equal Set.new(["http://example.org/A"]), |
| 57 | + ancestors_map["http://example.org/C"] |
| 58 | + end |
| 59 | + |
| 60 | + # A B |
| 61 | + # | | |
| 62 | + # C D |
| 63 | + def test_multiple_roots |
| 64 | + direct_parents = { |
| 65 | + "http://example.org/C" => ["http://example.org/A"], |
| 66 | + "http://example.org/D" => ["http://example.org/B"] |
| 67 | + } |
| 68 | + ancestors_map = {} |
| 69 | + |
| 70 | + compute_all(direct_parents, ancestors_map) |
| 71 | + |
| 72 | + assert_equal Set.new(["http://example.org/A"]), |
| 73 | + ancestors_map["http://example.org/C"] |
| 74 | + assert_equal Set.new(["http://example.org/B"]), |
| 75 | + ancestors_map["http://example.org/D"] |
| 76 | + end |
| 77 | + |
| 78 | + # A -> B -> A (cycle) |
| 79 | + def test_cycle |
| 80 | + direct_parents = { |
| 81 | + "http://example.org/A" => ["http://example.org/B"], |
| 82 | + "http://example.org/B" => ["http://example.org/A"] |
| 83 | + } |
| 84 | + ancestors_map = {} |
| 85 | + |
| 86 | + compute_all(direct_parents, ancestors_map) |
| 87 | + |
| 88 | + assert_includes ancestors_map["http://example.org/A"], "http://example.org/B" |
| 89 | + assert_includes ancestors_map["http://example.org/B"], "http://example.org/A" |
| 90 | + end |
| 91 | + |
| 92 | + # Class not in direct_parents (leaf with no edges) |
| 93 | + def test_class_not_in_map |
| 94 | + direct_parents = {} |
| 95 | + ancestors_map = {} |
| 96 | + |
| 97 | + @indexer.send(:compute_ancestors_for, "http://example.org/X", direct_parents, ancestors_map) |
| 98 | + |
| 99 | + assert_equal Set.new, ancestors_map["http://example.org/X"] |
| 100 | + end |
| 101 | + |
| 102 | + # Memoization: computing ancestors for a child reuses already-computed parent ancestors |
| 103 | + def test_memoization |
| 104 | + direct_parents = { |
| 105 | + "http://example.org/C" => ["http://example.org/B"], |
| 106 | + "http://example.org/B" => ["http://example.org/A"] |
| 107 | + } |
| 108 | + ancestors_map = {} |
| 109 | + |
| 110 | + # Compute B first |
| 111 | + @indexer.send(:compute_ancestors_for, "http://example.org/B", direct_parents, ancestors_map) |
| 112 | + assert ancestors_map.key?("http://example.org/B") |
| 113 | + refute ancestors_map.key?("http://example.org/C") |
| 114 | + |
| 115 | + # Now compute C — should reuse B's cached result |
| 116 | + @indexer.send(:compute_ancestors_for, "http://example.org/C", direct_parents, ancestors_map) |
| 117 | + assert_equal Set.new(["http://example.org/B", "http://example.org/A"]), |
| 118 | + ancestors_map["http://example.org/C"] |
| 119 | + end |
| 120 | + |
| 121 | + # A |
| 122 | + # / \ |
| 123 | + # B C |
| 124 | + # / \ \ |
| 125 | + # D E F |
| 126 | + # \ / |
| 127 | + # G |
| 128 | + def test_complex_dag |
| 129 | + direct_parents = { |
| 130 | + "http://example.org/D" => ["http://example.org/B"], |
| 131 | + "http://example.org/E" => ["http://example.org/B"], |
| 132 | + "http://example.org/F" => ["http://example.org/C"], |
| 133 | + "http://example.org/G" => ["http://example.org/E", "http://example.org/F"], |
| 134 | + "http://example.org/B" => ["http://example.org/A"], |
| 135 | + "http://example.org/C" => ["http://example.org/A"] |
| 136 | + } |
| 137 | + ancestors_map = {} |
| 138 | + |
| 139 | + compute_all(direct_parents, ancestors_map) |
| 140 | + |
| 141 | + assert_equal Set.new(["http://example.org/E", "http://example.org/F", |
| 142 | + "http://example.org/B", "http://example.org/C", |
| 143 | + "http://example.org/A"]), |
| 144 | + ancestors_map["http://example.org/G"] |
| 145 | + |
| 146 | + assert_equal Set.new(["http://example.org/B", "http://example.org/A"]), |
| 147 | + ancestors_map["http://example.org/D"] |
| 148 | + end |
| 149 | + |
| 150 | + def test_empty_ontology |
| 151 | + direct_parents = {} |
| 152 | + ancestors_map = {} |
| 153 | + |
| 154 | + compute_all(direct_parents, ancestors_map) |
| 155 | + |
| 156 | + assert_equal({}, ancestors_map) |
| 157 | + end |
| 158 | + |
| 159 | + private |
| 160 | + |
| 161 | + def compute_all(direct_parents, ancestors_map) |
| 162 | + direct_parents.each_key do |cls| |
| 163 | + @indexer.send(:compute_ancestors_for, cls, direct_parents, ancestors_map) |
| 164 | + end |
| 165 | + end |
| 166 | +end |
0 commit comments