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_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 _empty_property(self, element, parent, ids): # 7.2.21 Production emptyPropertyElt id_ = self._id(element, ids) literal_attrs = _XML_ATTRS | {QName(RDF, 'ID')} if all(attr in literal_attrs for attr in element.keys()): object_ = PlainLiteral("", element.language) triple = (parent.subject, element.uri, object_) yield triple if id_ is not None: for triple in self._reify(id_, triple): yield triple else: resource = element.attrib.get(QName(RDF, 'resource')) node_id = element.attrib.get(QName(RDF, 'nodeID')) if resource is not None: if node_id is None: object_ = self._uri(resource, element.base_uri) else: raise ParseError elif node_id is not None: if _NCNAME.match(node_id): object_ = BlankNode(node_id) else: raise ParseError( "rdf:nodeID does not match NCName: {!r}".format( node_id)) else: object_ = BlankNode() triple = (parent.subject, element.uri, object_) yield triple if id_ is not None: for triple in self._reify(id_, triple): yield triple subject = object_ property_attrs = set(element.keys()) property_attrs -= literal_attrs | { QName(RDF, 'resource'), QName(RDF, 'nodeID') } for attr in property_attrs: predicate = URI(QName(attr)) if predicate in self.XML_TERMS: continue elif predicate in self.ILLEGAL_PROPERTY_ATTRS: raise ParseError value = element.get(attr) if predicate != RDF.type: object_ = PlainLiteral(value, element.language) else: object_ = self._uri(value, element.base_uri) yield (subject, predicate, object_)
def test_matches_only_typed_literals(self): self.assertFalse(BlankNode() in self.type) self.assertFalse(URI('test') in self.type) self.assertFalse(PlainLiteral('1.5') in self.type) self.assertFalse(PlainLiteral('1.5', 'en') in self.type) self.assert_(TypedLiteral('1.5', XSD.float) in self.type) self.assert_(TypedLiteral('1.5', XSD.string) in self.type)
def _parse_type_collection_property(self, element, parent, ids): # 7.2.19 Production parseTypeCollectionPropertyElt node_ids = [] for node_element in element: for triple in self._node_element(node_element, ids): yield triple node_ids.append((node_element, BlankNode())) for node_element, object_ in node_ids: break else: object_ = RDF.nil triple = (parent.subject, element.uri, object_) yield triple id_ = self._id(element, ids) if id_ is not None: # 7.3 Reification Rules for triple in self._reify(id_, triple): yield triple for i, (node_element, object_) in enumerate(node_ids): yield (object_, RDF.first, node_element.subject) try: next_pair = node_ids[i + 1] except IndexError: next_object = RDF.nil else: next_element, next_object = next_pair yield (object_, RDF.rest, next_object)
def test_find_yields_valid_matches(self): triples = {(1, EX.prop, "foo"), (EX.a, EX.prop, Literal("bar")), (BlankNode('x'), RDF.type, BlankNode('y')), (EX.b, BlankNode(), EX.c)} matches = list(self.pattern.find(triples)) self.assertEqual(len(matches), 2) self.assert_({ self.uuu: EX.a, self.aaa: EX.prop, self.xxx: Literal("bar") } in matches) self.assert_({ self.uuu: BlankNode('x'), self.aaa: RDF.type, self.xxx: BlankNode('y') } in matches)
def _subject(self, element, ids): id_ = self._id(element, ids) node_id = element.get(QName(RDF, 'nodeID')) about = element.get(QName(RDF, 'about')) if id_ is not None: if node_id is None: if about is None: return id_ raise ParseError raise ParseError elif node_id is not None: if about is None: if _NCNAME.match(node_id): return BlankNode(node_id) raise ParseError raise ParseError elif about is not None: return self._uri(about, element.base_uri) return BlankNode()
def test_contains_instances_of_classes_in_class_set(self): uri = EX.test bnode = BlankNode() literal = Literal("cat") self.assert_(self.uuu.contains(uri)) self.assert_(self.uuu.contains(bnode)) self.assertFalse(self.uuu.contains(literal)) self.assert_(self.aaa.contains(uri)) self.assertFalse(self.aaa.contains(bnode)) self.assertFalse(self.aaa.contains(literal)) self.assert_(self.xxx.contains(uri)) self.assert_(self.xxx.contains(bnode)) self.assert_(self.xxx.contains(literal))
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_equal_to_blank_node_with_same_node_id(self): self.assertEqual(self.bnode, BlankNode('b1'))
def test_does_not_contain_bnodes(self): self.assertFalse(self.cmp.contains(BlankNode()))
def test_not_equal_to_blank_node_with_different_node_id(self): self.assertNotEqual(self.bnode, BlankNode('b2'))
def test_hash_equal_to_blank_node_with_same_node_id(self): self.assertEqual(hash(self.bnode), hash(BlankNode('b1')))
def test_hash_not_equal_to_blank_node_with_different_node_id(self): self.assertNotEqual(hash(self.bnode), hash(BlankNode('b2')))
def test_compares_greater_than_blank_node(self): bnode = BlankNode() self.assert_(self.literal > bnode) self.assert_(not self.literal < bnode)
def test_triple_2_blank_node_subject(self): triple = self._get_triple(1) self.assertEqual(triple, (BlankNode('anon'), EX.property, EX.resource2))
def test_compares_greater_than_blank_node_with_greater_hash(self): bnode = BlankNode() self.assertEqual(bnode > self.bnode, hash(bnode) > hash(self.bnode))
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())}))
def setUp(self): self.bnode = BlankNode()
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_not_equal_to_blank_node_without_node_id(self): self.assertNotEqual(self.bnode, BlankNode())
def test_hash_not_equal_to_blank_node_without_node_id(self): self.assertNotEqual(hash(self.bnode), hash(BlankNode()))
def _node_id(self, token): return BlankNode(token[2:])
def test_blanknode_not_in_type(self): self.assert_(BlankNode() not in self.cmp)
def allocate(self, node, blank_node=None): if blank_node is None: blank_node = BlankNode() return self.blank_node_allocations.setdefault(node, blank_node)
def test_blank_node_instance_not_in_uri_type(self): self.assert_(BlankNode() not in self.predicates)
def test_triple_16_optional_space_before_dot(self): triple = self._get_triple(15) self.assertEqual(triple, (EX.resource15, EX.property, BlankNode('anon')))
def test_blank_node_instance_in_uri_and_blank_node_type(self): self.assert_(BlankNode() in self.subjects)
def test_triple_3_blank_node_object(self): triple = self._get_triple(2) self.assertEqual(triple, (EX.resource2, EX.property, BlankNode('anon')))
def test_compares_less_than_blank_node_with_lesser_hash(self): bnode = BlankNode() self.assertEqual(bnode < self.bnode, hash(bnode) < hash(self.bnode))