def test_nodes_init_with_nodes(self): uri = URI('rdf:type') self.assertIsInstance(URI(uri), URI) self.assertEqual(URI(uri), uri) bnode = BNode() self.assertIsInstance(BNode(bnode), BNode) self.assertEqual(BNode(bnode), bnode) lit = Literal('name', 'en', LiteralType.string) self.assertIsInstance(Literal(lit), Literal) self.assertEqual(Literal(lit), lit)
def __init__(self, p1, p2=None, statement=None): """ Make reification. :param p1: URI, predicate for reification :param p2: Optional[URI], URI -> second predicate for reification None -> reuse p1 as second predicate :param statement: Optional[Union[URI, BNode]], URI or BNode -> use provided node as statement term None -> generate a new BNode as statement term """ if isinstance(p1, URI): self.p1 = p1 else: raise InvalidParameter( 'Reification predicate needs to be a valid URI') if p2 is None: self.p2 = p1 elif isinstance(p2, URI): self.p2 = p2 else: raise InvalidParameter( 'Reification second predicate needs to be a valid URI or None') if statement is None: self.statement = BNode() elif isinstance(statement, (URI, BNode)): self.statement = statement else: raise InvalidParameter( 'Reification statement term needs to be a valid URI or BNode or None' ) super().__init__(self.statement)
def _property_shape(self, property_: Union[rdflib.URIRef, str, URI]): if isinstance(property_, URI): property_ = property_.value property_ = rdflib.URIRef(property_) p_shape = Subject(BNode()) p_shape.add_property(URI('sh:path'), URI(property_)) ranges = list(self.onto_graph.objects(property_, RDFS.range)) self._build_property_ranges(p_shape, ranges) return p_shape
def _add_list(self, list_): """ (:a :b :c) === [ rdf:first :a ; rdf:rest [ rdf:first :b ; rdf:rest [ rdf:first :c ; rdf:rest rdf:nil]]] """ if not list_: return URI('rdf:nil') head = Subject(BNode()) head.add_property(URI('rdf:first'), list_[0]) head.add_property(URI('rdf:rest'), self._add_list(list_[1:])) return head
def test_nodes_bnode(self): bnode = BNode() bnode2 = BNode() self.assertNotEqual(bnode, bnode2) self.assertEqual(len({bnode, bnode2}), 2) self.assertTrue(bnode.is_valid()) # named bnode test_bnode = BNode('test') test_bnode2 = BNode('test') self.assertEqual(test_bnode, test_bnode2) self.assertNotEqual(test_bnode, bnode) self.assertEqual(hash(test_bnode), hash(test_bnode2)) self.assertNotEqual(hash(test_bnode), hash(bnode)) self.assertTrue(test_bnode.is_valid())
def _build_property_ranges(self, p_shape, ranges): if not ranges: return if len(ranges) == 1: range_ = ranges[0] type_ = URI('sh:datatype') if self._is_datatype(range_) else URI('sh:class') p_shape.add_property(type_, URI(ranges[0])) else: or_list = [] for range_ in ranges: range_subject = Subject(BNode()) type_ = URI('sh:datatype') if self._is_datatype(range_) else URI('sh:class') range_subject.add_property(type_, URI(range_)) or_list.append(range_subject) p_shape.add_property(URI('sh:or'), self._add_list(or_list))
def test_add_list(self): converter = SHACLOntoConverter() list_subject = Subject(BNode()) list_subject.add_property( URI(':list'), converter._add_list([URI(':a'), URI(':b'), URI(':c')])) list_graph = subject_to_graph(list_subject) self.assertTrue( list_graph._g.query(""" PREFIX : <http://example.org/> ASK { ?s :list ( ?a ?b ?c ) ; } """))
def test_subject_exception(self): with self.assertRaises(InvalidParameter): Subject(None) with self.assertRaises(InvalidParameter): Subject('ex:ex1') with self.assertRaises(InvalidParameter): Subject(Literal('test')) s = Subject(URI('ex:ex1')) with self.assertRaises(InvalidParameter): s.add_property(URI('rdf:type'), 'dig:Person') with self.assertRaises(InvalidParameter): s.add_property(BNode(), URI('dig:Person'))
def test_add_subject(self): kg = self.doc.kg subject = Subject(URI('http://xxx/1')) subject.add_property(URI('dig:name'), Literal('Jack', lang='en')) subject.add_property(URI('dig:age'), Literal('18', type_=LiteralType.int)) friend = Subject(BNode()) friend.add_property(URI('rdf:type'), Literal('dig:Person')) friend.add_property(URI('dig:name'), Literal('José', lang='es')) friend.add_property(URI('dig:age'), Literal('19', type_=LiteralType.int)) friend.add_property(URI('dig:friend'), subject) subject.add_property(URI('dig:friend'), friend) kg.add_subject(subject) self.assertEqual(len(kg._g), 8) self.assertIn((rdflib.URIRef('http://xxx/1'), DIG.name, rdflib.Literal('Jack', lang='en')), kg._g)
def test_graph(self): g = Graph() g.bind('ex', 'http://ex.com/') g.bind('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#') g.bind('foaf', 'http://xmlns.com/foaf/0.1/') t_nest = Subject(BNode()) t_nest.add_property(URI('rdf:type'), URI('foaf:Person')) t_nest.add_property(URI('foaf:name'), Literal('Alfred')) t_nest.add_property(URI('foaf:age'), Literal('36', type_=LiteralType.int)) t = Subject(URI('ex:john')) t.add_property(URI('rdf:type'), URI('foaf:Person')) t.add_property(URI('foaf:name'), Literal('John')) t.add_property(URI('foaf:age'), Literal('12', type_=LiteralType.int)) t.add_property(URI('foaf:knows'), t_nest) g.add_subject(t) self.assertEqual(len(g._g), 7)
def cnode(self, class_): class_ = str(class_) if class_ not in self._class_nodes: self._class_nodes[class_] = BNode() return self._class_nodes[class_]