Example #1
0
    def setUp(self):
        super().setUp()
        self.premise = Graph()
        for premise_document in self.test.premise_documents:
            if premise_document.type != TEST['False-Document']:
                self.premise.update(premise_document.read(self.opener))
            else:
                self.premise = False

        self.entailments = []
        for rules_uri in self.test.entailment_rules:
            entailment = self.ENTAILMENT_RULES[rules_uri]
            self.entailments.append(entailment)
        if (SIMPLE_ENTAILMENT not in self.entailments
                and SIMPLE_ENTAILMENT_LG not in self.entailments):
            self.entailments.append(SIMPLE_ENTAILMENT_LG)

        self.merged_entailment = Entailment()
        for entailment in self.entailments:
            self.merged_entailment.conditions += entailment.conditions
            self.merged_entailment.axioms |= entailment.axioms
            self.merged_entailment.rules += entailment.rules

        self.merged_entailment.axioms |= {
            (datatype, RDF.type, RDFS.Datatype)
            for datatype in self.test.datatype_support
        }

        if self.test.conclusion_document.type != TEST['False-Document']:
            self.conclusion = Graph(
                self.test.conclusion_document.read(self.opener))
        else:
            self.conclusion = False
Example #2
0
 def setUp(self):
     self.rule = Rule({}, {(RDF.type, RDF.type, RDF.Property)})
     self.rule_se1 = Rule({(uuu, aaa, xxx)}, {(uuu, aaa, xxx.nnn)},
                          name='se1')
     self.empty_graph = Graph()
     self.graph = Graph({(EX.a, EX.property, EX.b),
                         (EX.b, EX.property, EX.c)})
     self.context = Context()
Example #3
0
 def setUp(self):
     self.triples = {(URI('http://www.example.org/index.html'),
                      URI('http://purl.org/dc/elements/1.1/creator'),
                      URI('http://www.example.org/staffid/85740')),
                     (URI('http://www.example.org/staffid/85740'),
                      URI('http://xmlns.com/foaf/0.1/name'),
                      Literal("John Lennon", XSD.string))}
     self.graph = Graph(self.triples)
Example #4
0
class PositiveParserTestCase(RDFTestCase):
    def setUp(self):
        super().setUp()
        self.input_graph = Graph()
        for document in self.test.input_documents:
            self.input_graph.update(document.read(self.opener))
        self.output_graph = Graph(self.test.output_document.read(self.opener))

    def runTest(self):
        self.assertEqual(self.input_graph, self.output_graph)
Example #5
0
class PositiveParserTestCase(RDFTestCase):
    def setUp(self):
        super().setUp()
        self.input_graph = Graph()
        for document in self.test.input_documents:
            self.input_graph.update(document.read(self.opener))
        self.output_graph = Graph(self.test.output_document.read(self.opener))

    def runTest(self):
        self.assertEqual(self.input_graph, self.output_graph)
Example #6
0
    def setUp(self):
        super().setUp()
        self.premise = Graph()
        for premise_document in self.test.premise_documents:
            if premise_document.type != TEST['False-Document']:
                self.premise.update(premise_document.read(self.opener))
            else:
                self.premise = False

        self.entailments = []
        for rules_uri in self.test.entailment_rules:
            entailment = self.ENTAILMENT_RULES[rules_uri]
            self.entailments.append(entailment)
        if (SIMPLE_ENTAILMENT not in self.entailments and
            SIMPLE_ENTAILMENT_LG not in self.entailments):
            self.entailments.append(SIMPLE_ENTAILMENT_LG)

        self.merged_entailment = Entailment()
        for entailment in self.entailments:
            self.merged_entailment.conditions += entailment.conditions
            self.merged_entailment.axioms |= entailment.axioms
            self.merged_entailment.rules += entailment.rules

        self.merged_entailment.axioms |= {
            (datatype, RDF.type, RDFS.Datatype) for datatype in
            self.test.datatype_support}

        if self.test.conclusion_document.type != TEST['False-Document']:
            self.conclusion = Graph(
                self.test.conclusion_document.read(self.opener))
        else:
            self.conclusion = False
Example #7
0
 def test_not_equal_to_graph_with_different_number_of_unground_triples(self):
     graph = Graph({(URI('http://www.example.org/index.html'),
                     URI('http://purl.org/dc/elements/1.1/creator'),
                     URI('john')),
                    (URI('john'),
                     URI('http://xmlns.com/foaf/0.1/name'),
                     Literal("John Lennon", XSD.string))})
     self.assertNotEqual(self.graph, graph)        
     self.assertNotEqual(graph, self.graph)        
Example #8
0
 def setUp(self):
     self.triples = {(URI('http://www.example.org/index.html'),
                      URI('http://purl.org/dc/elements/1.1/creator'),
                      BlankNode('john')),
                     (BlankNode('john'),
                      URI('http://xmlns.com/foaf/0.1/name'),
                      Literal("John Lennon", XSD.string)),
                     (BlankNode('john'),
                      URI('http://xmlns.com/foaf/0.1/knows'),
                      BlankNode('paul'))}
     self.bijection = {(URI('http://www.example.org/index.html'),
                        URI('http://purl.org/dc/elements/1.1/creator'),
                        BlankNode('lennon')),
                       (BlankNode('lennon'),
                        URI('http://xmlns.com/foaf/0.1/name'),
                        Literal("John Lennon", XSD.string)),
                       (BlankNode('lennon'),
                        URI('http://xmlns.com/foaf/0.1/knows'),
                        BlankNode('starr'))}
     self.graph = Graph(self.triples)
Example #9
0
 def test_not_equal_to_graph_without_valid_bijection(self):
     graph = Graph({(URI('http://www.example.org/index.html'),
                     URI('http://purl.org/dc/elements/1.1/creator'),
                     BlankNode('john')),
                    (BlankNode('john'),
                     URI('http://xmlns.com/foaf/0.1/name'),
                     Literal("John Lennon", XSD.string)),
                    (BlankNode('paul'),
                     URI('http://xmlns.com/foaf/0.1/knows'),
                     BlankNode('john'))})
     self.assertNotEqual(self.graph, graph)
     self.assertNotEqual(graph, self.graph)
Example #10
0
 def entails(self, s, e):
     if s >= e:
         return True
     entailed = Graph(s | self.axioms)
     context = Context()
     num_triples = -1
     while len(entailed) != num_triples:
         num_triples = len(entailed)
         for rule in self.rules:
             entailed.update(rule.apply(entailed, context))
             if len(entailed) != num_triples:
                 if e <= entailed:
                     return True
     for triple in e:
         if triple not in entailed:
             for entailed_triple in entailed:
                 pattern = Pattern(entailed_triple)
                 if pattern.matches(triple):
                     break
             else:
                 return False
     return True
Example #11
0
 def entails(self, s, e):
     if s >= e:
         return True
     entailed = Graph(s | self.axioms)
     context = Context()
     num_triples = -1
     while len(entailed) != num_triples:
         num_triples = len(entailed)
         for rule in self.rules:
             entailed.update(rule.apply(entailed, context))
             if len(entailed) != num_triples:
                 if e <= entailed:
                     return True
     for triple in e:
         if triple not in entailed:
             for entailed_triple in entailed:
                 pattern = Pattern(entailed_triple)
                 if pattern.matches(triple):
                     break
             else:
                 return False
     return True
Example #12
0
class TestMixedGraph(unittest.TestCase):
    def setUp(self):
        self.triples = {(URI('http://www.example.org/index.html'),
                         URI('http://purl.org/dc/elements/1.1/creator'),
                         BlankNode('john')),
                        (BlankNode('john'),
                         URI('http://xmlns.com/foaf/0.1/name'),
                         Literal("John Lennon", XSD.string)),
                        (BlankNode('john'),
                         URI('http://xmlns.com/foaf/0.1/knows'),
                         BlankNode('paul')),
                        (EX.a, EX.property, EX.b),
                        (EX.b, EX.property, EX.c)}
        self.bijection = {(URI('http://www.example.org/index.html'),
                           URI('http://purl.org/dc/elements/1.1/creator'),
                           BlankNode('lennon')),
                          (BlankNode('lennon'),
                           URI('http://xmlns.com/foaf/0.1/name'),
                           Literal("John Lennon", XSD.string)),
                          (BlankNode('lennon'),
                           URI('http://xmlns.com/foaf/0.1/knows'),
                           BlankNode('starr'))}
        self.graph = Graph(self.triples)
        self.subgraph = Graph(self.bijection)

    def test_is_ground_is_false(self):
        self.assertEqual(self.graph.is_ground(), False)

    def test_valid_bijection_of_subset_is_a_strict_subgraph(self):
        self.assert_(self.subgraph < self.graph)

    def test_valid_bijection_of_subset_is_a_subgraph(self):
        self.assert_(self.subgraph <= self.graph)

    def test_is_a_strict_supergraph_of_valid_bijection_of_subset(self):
        self.assert_(self.graph > self.subgraph)

    def test_is_a_supergraph_of_valid_bijection_of_subset(self):
        self.assert_(self.graph >= self.subgraph)

    def test_is_not_a_subgraph_of_valid_bijection_of_subset(self):
        self.assertFalse(self.graph <= self.subgraph)

    def test_is_not_a_strict_subgraph_of_valid_bijection_of_subset(self):
        self.assertFalse(self.graph < self.subgraph)

    def test_is_not_equal_to_subgraph(self):
        self.assert_(self.graph != self.subgraph)
        self.assert_(self.subgraph != self.graph)
Example #13
0
 def test_is_not_a_strict_supergraph_of_graph_with_valid_bijection(self):
     self.assertFalse(self.graph > Graph(self.bijection))
Example #14
0
 def test_is_supergraph_of_graph_with_valid_bijection(self):
     self.assert_(self.graph >= Graph(self.bijection))
Example #15
0
 def graph_with_valid_bijection_is_not_a_strict_supergraph(self):
     self.assertFalse(Graph(self.bijection) > self.graph)
Example #16
0
 def test_graph_with_valid_bijection_is_supergraph(self):
     self.assert_(Graph(self.bijection) >= self.graph)
Example #17
0
 def test_is_supergraph_of_empty_graph(self):
     self.assert_(self.graph >= Graph())
Example #18
0
 def test_empty_graph_is_a_supergraph(self):
     self.assertTrue(Graph() >= self.graph)
     self.assertTrue(self.graph >= Graph())
Example #19
0
class TestGroundGraph(unittest.TestCase):
    def setUp(self):
        self.triples = {(URI('http://www.example.org/index.html'),
                         URI('http://purl.org/dc/elements/1.1/creator'),
                         URI('http://www.example.org/staffid/85740')),
                        (URI('http://www.example.org/staffid/85740'),
                         URI('http://xmlns.com/foaf/0.1/name'),
                         Literal("John Lennon", XSD.string))}
        self.graph = Graph(self.triples)

    def test_repr_shows_constructor(self):
        self.assertEqual(repr(self.graph),
                         "Graph({!r})".format(self.triples))

    def test_is_ground_is_true(self):
        self.assertEqual(self.graph.is_ground(), True)

    def test_nodes_is_set_of_subjects_and_objects(self):
        self.assertEqual(self.graph.nodes(),
                         {URI('http://www.example.org/index.html'),
                          URI('http://www.example.org/staffid/85740'),
                          Literal("John Lennon", XSD.string)})

    def test_equal_to_graph_with_same_triples(self):
        self.assertEqual(self.graph, Graph(self.triples))

    def test_not_equal_to_graph_of_different_size(self):
        graph = Graph(list(self.triples)[0])
        self.assertNotEqual(self.graph, graph)
        self.assertNotEqual(graph, self.graph)

    def test_not_equal_to_graph_with_different_triples(self):
        graph = Graph({(URI('http://www.example.org/index.html'),
                        URI('http://purl.org/dc/elements/1.1/creator'),
                        URI('http://www.example.org/staffid/85740')),
                       (URI('http://www.example.org/staffid/85740'),
                        URI('http://xmlns.com/foaf/0.1/name'),
                        Literal("John Doe", XSD.string))})
        self.assertNotEqual(self.graph, graph)
        self.assertNotEqual(graph, self.graph)

    def test_equal_to_set_with_same_triples(self):
        self.assertEqual(self.triples, self.graph)
        self.assertEqual(self.graph, self.triples)

    def test_equal_to_frozenset_with_same_triples(self):
        self.assertEqual(frozenset(self.triples), self.graph)
        self.assertEqual(self.graph, frozenset(self.triples))

    def test_not_equal_to_set_with_different_triples(self):
        triples = {(URI('http://www.example.org/index.html'),
                    URI('http://purl.org/dc/elements/1.1/creator'),
                    URI('http://www.example.org/staffid/85740')),
                   (URI('http://www.example.org/staffid/85740'),
                    URI('http://xmlns.com/foaf/0.1/name'),
                    Literal("John Doe", XSD.string))}
        self.assertNotEqual(self.graph, triples)

    def test_not_equal_to_frozenset_with_different_triples(self):
        triples = {(URI('http://www.example.org/index.html'),
                    URI('http://purl.org/dc/elements/1.1/creator'),
                    URI('http://www.example.org/staffid/85740')),
                   (URI('http://www.example.org/staffid/85740'),
                    URI('http://xmlns.com/foaf/0.1/name'),
                    Literal("John Doe", XSD.string))}
        self.assertNotEqual(self.graph, frozenset(triples))

    def test_graph_with_same_triples_is_not_a_strict_subgraph(self):
        self.assertFalse(Graph(self.triples) < self.graph)
        self.assertFalse(self.graph < Graph(self.triples))

    def test_graph_with_same_triples_is_subgraph(self):
        self.assert_(Graph(self.triples) <= self.graph)
        self.assert_(self.graph <= Graph(self.triples))

    def test_empty_graph_is_a_subgraph(self):
        self.assert_(Graph() < self.graph)

    def test_empty_set_is_a_subset(self):
        self.assert_(set() < self.graph)

    def test_empty_frozenset_is_a_subset(self):
        self.assert_(frozenset() < self.graph)

    def test_set_with_same_triples_is_not_a_strict_subset(self):
        self.assertFalse(self.triples < self.graph)
        self.assertFalse(self.graph < self.triples)

    def test_set_with_same_triples_is_subset(self):
        self.assert_(self.triples <= self.graph)
        self.assert_(self.graph <= self.triples)

    def test_frozenset_with_same_triples_is_not_a_strict_subset(self):
        self.assertFalse(frozenset(self.triples) < self.graph)
        self.assertFalse(self.graph < frozenset(self.triples))

    def test_frozenset_with_same_triples_is_subset(self):
        self.assert_(frozenset(self.triples) <= self.graph)
        self.assert_(self.graph <= frozenset(self.triples))

    def test_graph_with_same_triples_is_not_a_strict_supergraph(self):
        self.assertFalse(Graph(self.triples) > self.graph)
        self.assertFalse(self.graph > Graph(self.triples))

    def test_graph_with_same_triples_is_supergraph(self):
        self.assert_(Graph(self.triples) >= self.graph)
        self.assert_(self.graph >= Graph(self.triples))

    def test_empty_graph_is_not_a_supergraph(self):
        self.assertFalse(Graph() >= self.graph)

    def test_is_supergraph_of_empty_graph(self):
        self.assert_(self.graph >= Graph())

    def test_is_superset_of_empty_set(self):
        self.assert_(self.graph > set())

    def test_is_superset_of_empty_frozenset(self):
        self.assert_(self.graph > frozenset())

    def test_set_with_same_triples_is_not_a_strict_superset(self):
        self.assertFalse(self.triples > self.graph)
        self.assertFalse(self.graph > self.triples)

    def test_set_with_same_triples_is_superset(self):
        self.assert_(self.triples >= self.graph)
        self.assert_(self.graph >= self.triples)

    def test_frozenset_with_same_triples_is_not_a_strict_superset(self):
        self.assertFalse(frozenset(self.triples) > self.graph)
        self.assertFalse(self.graph > frozenset(self.triples))

    def test_frozenset_with_same_triples_is_superset(self):
        self.assert_(frozenset(self.triples) >= self.graph)
        self.assert_(self.graph >= frozenset(self.triples))
Example #20
0
 def test_apply_with_empty_antecedent_to_empty_graph_does_not_yield_consequent(
         self):
     graph = Graph(self.rule_se1.apply(self.empty_graph, self.context))
     self.assertEqual(graph, Graph())
Example #21
0
 def test_empty_graph_is_a_subgraph(self):
     graph = Graph()
     self.assertTrue(graph <= self.graph)
     self.assertTrue(self.graph <= graph)
Example #22
0
 def test_graph_with_same_triples_is_supergraph(self):
     self.assert_(Graph(self.triples) >= self.graph)
     self.assert_(self.graph >= Graph(self.triples))
Example #23
0
class TestUngroundGraph(unittest.TestCase):
    def setUp(self):
        self.triples = {(URI('http://www.example.org/index.html'),
                         URI('http://purl.org/dc/elements/1.1/creator'),
                         BlankNode('john')),
                        (BlankNode('john'),
                         URI('http://xmlns.com/foaf/0.1/name'),
                         Literal("John Lennon", XSD.string)),
                        (BlankNode('john'),
                         URI('http://xmlns.com/foaf/0.1/knows'),
                         BlankNode('paul'))}
        self.bijection = {(URI('http://www.example.org/index.html'),
                           URI('http://purl.org/dc/elements/1.1/creator'),
                           BlankNode('lennon')),
                          (BlankNode('lennon'),
                           URI('http://xmlns.com/foaf/0.1/name'),
                           Literal("John Lennon", XSD.string)),
                          (BlankNode('lennon'),
                           URI('http://xmlns.com/foaf/0.1/knows'),
                           BlankNode('starr'))}
        self.graph = Graph(self.triples)

    def test_is_ground_is_false(self):
        self.assertEqual(self.graph.is_ground(), False)

    def test_nodes_is_set_of_subjects_and_objects(self):
        self.assertEqual(self.graph.nodes(),
                         {URI('http://www.example.org/index.html'),
                          BlankNode('john'), BlankNode('paul'),
                          Literal("John Lennon", XSD.string)})

    def test_names_is_set_of_uris_and_literals(self):
        self.assertEqual(self.graph.names(),
                         {URI('http://www.example.org/index.html'),
                          URI('http://purl.org/dc/elements/1.1/creator'),
                          URI('http://xmlns.com/foaf/0.1/name'),
                          URI('http://xmlns.com/foaf/0.1/knows'),
                          Literal("John Lennon", XSD.string),
                          XSD.string})

    def test_vocabulary_is_set_of_names_without_datatypes(self):
        self.assertEqual(self.graph.vocabulary(),
                         {URI('http://www.example.org/index.html'),
                          URI('http://purl.org/dc/elements/1.1/creator'),
                          URI('http://xmlns.com/foaf/0.1/name'),
                          URI('http://xmlns.com/foaf/0.1/knows'),
                          Literal("John Lennon", XSD.string)})

    def test_vocabulary_including_datatypes_is_set_of_names(self):
        self.assertEqual(self.graph.vocabulary(include_datatypes=True),
                         self.graph.names())

    def test_not_equal_to_graph_with_different_number_of_unground_triples(self):
        graph = Graph({(URI('http://www.example.org/index.html'),
                        URI('http://purl.org/dc/elements/1.1/creator'),
                        URI('john')),
                       (URI('john'),
                        URI('http://xmlns.com/foaf/0.1/name'),
                        Literal("John Lennon", XSD.string))})
        self.assertNotEqual(self.graph, graph)        
        self.assertNotEqual(graph, self.graph)        

    def test_not_equal_to_graph_with_different_number_of_unique_blank_nodes(self):
        graph = Graph({(URI('http://www.example.org/index.html'),
                        URI('http://purl.org/dc/elements/1.1/creator'),
                        BlankNode('john')),
                       (BlankNode('john'),
                        URI('http://xmlns.com/foaf/0.1/name'),
                        Literal("John Lennon", XSD.string)),
                       (BlankNode('john'),
                        URI('http://xmlns.com/foaf/0.1/knows'),
                        BlankNode("john"))})
        self.assertNotEqual(self.graph, graph)        
        self.assertNotEqual(graph, self.graph)        

    def test_equal_to_graph_with_valid_bijection(self):
        graph = Graph(self.bijection)
        self.assertEqual(self.graph, graph)
        self.assertEqual(graph, self.graph)

    def test_not_equal_to_graph_without_valid_bijection(self):
        graph = Graph({(URI('http://www.example.org/index.html'),
                        URI('http://purl.org/dc/elements/1.1/creator'),
                        BlankNode('john')),
                       (BlankNode('john'),
                        URI('http://xmlns.com/foaf/0.1/name'),
                        Literal("John Lennon", XSD.string)),
                       (BlankNode('paul'),
                        URI('http://xmlns.com/foaf/0.1/knows'),
                        BlankNode('john'))})
        self.assertNotEqual(self.graph, graph)
        self.assertNotEqual(graph, self.graph)
    
    def test_not_equal_to_set_with_valid_bijection(self):
        self.assertNotEqual(self.graph, self.bijection)

    def test_not_equal_to_frozenset_with_valid_bijection(self):
        self.assertNotEqual(self.graph, frozenset(self.bijection))

    def test_equal_to_set_with_identical_triples(self):
        self.assertEqual(self.graph, self.triples)

    def test_equal_to_frozenset_with_identical_triples(self):
        self.assertEqual(self.graph, frozenset(self.triples))

    def test_graph_with_valid_bijection_is_subgraph(self):
        self.assert_(Graph(self.bijection) <= self.graph)

    def graph_with_valid_bijection_is_not_a_strict_subgraph(self):
        self.assertFalse(Graph(self.bijection) < self.graph)

    def test_is_subgraph_of_graph_with_valid_bijection(self):
        self.assert_(self.graph <= Graph(self.bijection))

    def test_is_not_a_strict_subgraph_of_graph_with_valid_bijection(self):
        self.assertFalse(self.graph < Graph(self.bijection))

    def test_graph_with_valid_bijection_is_supergraph(self):
        self.assert_(Graph(self.bijection) >= self.graph)

    def graph_with_valid_bijection_is_not_a_strict_supergraph(self):
        self.assertFalse(Graph(self.bijection) > self.graph)

    def test_is_supergraph_of_graph_with_valid_bijection(self):
        self.assert_(self.graph >= Graph(self.bijection))

    def test_is_not_a_strict_supergraph_of_graph_with_valid_bijection(self):
        self.assertFalse(self.graph > Graph(self.bijection))
Example #24
0
 def test_empty_graph_is_a_subgraph(self):
     self.assertTrue(Graph() <= self.graph)
     self.assertTrue(self.graph <= Graph())
Example #25
0
 def setUp(self):
     super().setUp()
     self.input_graph = Graph()
     for document in self.test.input_documents:
         self.input_graph.update(document.read(self.opener))
     self.output_graph = Graph(self.test.output_document.read(self.opener))
Example #26
0
 def test_empty_graph_is_not_a_strict_supergraph(self):
     self.assertFalse(Graph() > self.graph)
     self.assertFalse(self.graph > Graph())
Example #27
0
 def test_graph_with_same_triples_is_not_a_strict_supergraph(self):
     self.assertFalse(Graph(self.triples) > self.graph)
     self.assertFalse(self.graph > Graph(self.triples))
Example #28
0
 def setUp(self):
     super().setUp()
     self.input_graph = Graph()
     for document in self.test.input_documents:
         self.input_graph.update(document.read(self.opener))
     self.output_graph = Graph(self.test.output_document.read(self.opener))
Example #29
0
 def test_empty_graph_is_not_a_supergraph(self):
     self.assertFalse(Graph() >= self.graph)
Example #30
0
 def test_empty_graph_is_not_a_strict_subgraph(self):
     self.assertFalse(Graph() < self.graph)
     self.assertFalse(self.graph < Graph())
Example #31
0
 def test_empty_graph_is_a_subgraph(self):
     self.assert_(Graph() < self.graph)
Example #32
0
 def test_apply_empty_antecedent_to_graph_yields_consequent(self):
     graph = Graph(self.rule.apply(self.graph, self.context))
     self.assertEqual(graph, Graph({(RDF.type, RDF.type, RDF.Property)}))
Example #33
0
 def test_empty_graphs_are_equal(self):
     self.assertEqual(self.graph, Graph())
Example #34
0
 def test_apply_to_graph_yields_consequent(self):
     graph = Graph(self.rule_se1.apply(self.graph, self.context))
     self.assertEqual(
         graph,
         Graph({(EX.a, EX.property, BlankNode()),
                (EX.b, EX.property, BlankNode())}))
Example #35
0
 def test_equal_to_graph_with_valid_bijection(self):
     graph = Graph(self.bijection)
     self.assertEqual(self.graph, graph)
     self.assertEqual(graph, self.graph)