from kgforge.core import KnowledgeGraphForge
A configuration file is needed in order to create a KnowledgeGraphForge session. A configuration can be generated using the notebook 00-Initialization.ipynb.
forge = KnowledgeGraphForge("../../configurations/forge.yml")
from kgforge.core import Resource
jane = Resource(type="Person", name="Jane Doe")
forge.register(jane)
<action> _register_one <succeeded> True
graph = forge.as_graph(jane)
len(graph) == 2
True
for triple in graph:
print(triple)
(rdflib.term.URIRef('https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/22ec9959-ab76-419a-9864-131b0dc12d57'), rdflib.term.URIRef('http://schema.org/name'), rdflib.term.Literal('Jane Doe')) (rdflib.term.URIRef('https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/22ec9959-ab76-419a-9864-131b0dc12d57'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://schema.org/Person'))
graph_store_metadata = forge.as_graph(jane, store_metadata=True)
len(graph_store_metadata)
14
for triple in graph_store_metadata:
print(triple)
(rdflib.term.URIRef('https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/22ec9959-ab76-419a-9864-131b0dc12d57'), rdflib.term.URIRef('http://schema.org/name'), rdflib.term.Literal('Jane Doe')) (rdflib.term.URIRef('https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/22ec9959-ab76-419a-9864-131b0dc12d57'), rdflib.term.URIRef('https://bluebrain.github.io/nexus/vocabulary/updatedAt'), rdflib.term.Literal('2022-04-12T22:47:28.984000+00:00', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#dateTime'))) (rdflib.term.URIRef('https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/22ec9959-ab76-419a-9864-131b0dc12d57'), rdflib.term.URIRef('https://bluebrain.github.io/nexus/vocabulary/rev'), rdflib.term.Literal('1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))) (rdflib.term.URIRef('https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/22ec9959-ab76-419a-9864-131b0dc12d57'), rdflib.term.URIRef('https://bluebrain.github.io/nexus/vocabulary/createdAt'), rdflib.term.Literal('2022-04-12T22:47:28.984000+00:00', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#dateTime'))) (rdflib.term.URIRef('https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/22ec9959-ab76-419a-9864-131b0dc12d57'), rdflib.term.URIRef('https://bluebrain.github.io/nexus/vocabulary/self'), rdflib.term.URIRef('https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/22ec9959-ab76-419a-9864-131b0dc12d57')) (rdflib.term.URIRef('https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/22ec9959-ab76-419a-9864-131b0dc12d57'), rdflib.term.URIRef('https://bluebrain.github.io/nexus/vocabulary/constrainedBy'), rdflib.term.URIRef('https://bluebrain.github.io/nexus/schemas/unconstrained.json')) (rdflib.term.URIRef('https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/22ec9959-ab76-419a-9864-131b0dc12d57'), rdflib.term.URIRef('https://bluebrain.github.io/nexus/vocabulary/schemaProject'), rdflib.term.URIRef('https://bbp.epfl.ch/nexus/v1/projects/dke/kgforge')) (rdflib.term.URIRef('https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/22ec9959-ab76-419a-9864-131b0dc12d57'), rdflib.term.URIRef('https://bluebrain.github.io/nexus/vocabulary/createdBy'), rdflib.term.URIRef('https://bbp.epfl.ch/nexus/v1/realms/bbp/users/sy')) (rdflib.term.URIRef('https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/22ec9959-ab76-419a-9864-131b0dc12d57'), rdflib.term.URIRef('https://bluebrain.github.io/nexus/vocabulary/deprecated'), rdflib.term.Literal('false', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#boolean'))) (rdflib.term.URIRef('https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/22ec9959-ab76-419a-9864-131b0dc12d57'), rdflib.term.URIRef('https://bluebrain.github.io/nexus/vocabulary/updatedBy'), rdflib.term.URIRef('https://bbp.epfl.ch/nexus/v1/realms/bbp/users/sy')) (rdflib.term.URIRef('https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/22ec9959-ab76-419a-9864-131b0dc12d57'), rdflib.term.URIRef('https://bluebrain.github.io/nexus/vocabulary/outgoing'), rdflib.term.URIRef('https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/22ec9959-ab76-419a-9864-131b0dc12d57/outgoing')) (rdflib.term.URIRef('https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/22ec9959-ab76-419a-9864-131b0dc12d57'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://schema.org/Person')) (rdflib.term.URIRef('https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/22ec9959-ab76-419a-9864-131b0dc12d57'), rdflib.term.URIRef('https://bluebrain.github.io/nexus/vocabulary/project'), rdflib.term.URIRef('https://bbp.epfl.ch/nexus/v1/projects/dke/kgforge')) (rdflib.term.URIRef('https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/22ec9959-ab76-419a-9864-131b0dc12d57'), rdflib.term.URIRef('https://bluebrain.github.io/nexus/vocabulary/incoming'), rdflib.term.URIRef('https://bbp.epfl.ch/nexus/v1/resources/dke/kgforge/_/22ec9959-ab76-419a-9864-131b0dc12d57/incoming'))
#data in n3 format. Other RDF formats can be used
data = '''
@prefix schema: <http://schema.org/> .
[] a schema:Person;
schema:name "Jane Doe" ;
schema:knows <http://example.org/JohnDoe>;
schema:affiliation <https://www.grid.ac/institutes/grid.5333.6>.
<http://example.org/JohnDoe> a schema:Person;
schema:name "John Doe" .
<https://www.grid.ac/institutes/grid.5333.6> a schema:Organization;
schema:name "EPFL" .
'''
import rdflib
graph = rdflib.Graph()
graph.parse(data=data, format="n3")
<Graph identifier=Nabbd6999596d410b9d9760651f8b8bcd (<class 'rdflib.graph.Graph'>)>
len(graph)==8
True
resources = forge.from_graph(graph)
len(resources) == 3
True
frame = {
"@type": [ 'http://schema.org/Person'],
"@embed": True
}
resources = forge.from_graph(data=graph, frame= frame)
len(resources) == 2
True
forge.as_jsonld(resources)
[{'@context': {'brick': 'https://brickschema.org/schema/Brick#', 'csvw': 'http://www.w3.org/ns/csvw#', 'dc': 'http://purl.org/dc/elements/1.1/', 'dcam': 'http://purl.org/dc/dcam/', 'dcat': 'http://www.w3.org/ns/dcat#', 'dcmitype': 'http://purl.org/dc/dcmitype/', 'dcterms': 'http://purl.org/dc/terms/', 'doap': 'http://usefulinc.com/ns/doap#', 'foaf': 'http://xmlns.com/foaf/0.1/', 'odrl': 'http://www.w3.org/ns/odrl/2/', 'org': 'http://www.w3.org/ns/org#', 'owl': 'http://www.w3.org/2002/07/owl#', 'prof': 'http://www.w3.org/ns/dx/prof/', 'prov': 'http://www.w3.org/ns/prov#', 'qb': 'http://purl.org/linked-data/cube#', 'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'rdfs': 'http://www.w3.org/2000/01/rdf-schema#', 'schema': 'https://schema.org/', 'schema1': 'http://schema.org/', 'sh': 'http://www.w3.org/ns/shacl#', 'skos': 'http://www.w3.org/2004/02/skos/core#', 'sosa': 'http://www.w3.org/ns/sosa/', 'ssn': 'http://www.w3.org/ns/ssn/', 'time': 'http://www.w3.org/2006/time#', 'vann': 'http://purl.org/vocab/vann/', 'void': 'http://rdfs.org/ns/void#', 'xsd': 'http://www.w3.org/2001/XMLSchema#'}, '@type': 'schema1:Person', 'schema1:affiliation': {'@id': 'https://www.grid.ac/institutes/grid.5333.6', '@type': 'schema1:Organization', 'schema1:name': 'EPFL'}, 'schema1:knows': {'@id': 'http://example.org/JohnDoe', '@type': 'schema1:Person', 'schema1:name': 'John Doe'}, 'schema1:name': 'Jane Doe'}, {'@context': {'brick': 'https://brickschema.org/schema/Brick#', 'csvw': 'http://www.w3.org/ns/csvw#', 'dc': 'http://purl.org/dc/elements/1.1/', 'dcam': 'http://purl.org/dc/dcam/', 'dcat': 'http://www.w3.org/ns/dcat#', 'dcmitype': 'http://purl.org/dc/dcmitype/', 'dcterms': 'http://purl.org/dc/terms/', 'doap': 'http://usefulinc.com/ns/doap#', 'foaf': 'http://xmlns.com/foaf/0.1/', 'odrl': 'http://www.w3.org/ns/odrl/2/', 'org': 'http://www.w3.org/ns/org#', 'owl': 'http://www.w3.org/2002/07/owl#', 'prof': 'http://www.w3.org/ns/dx/prof/', 'prov': 'http://www.w3.org/ns/prov#', 'qb': 'http://purl.org/linked-data/cube#', 'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'rdfs': 'http://www.w3.org/2000/01/rdf-schema#', 'schema': 'https://schema.org/', 'schema1': 'http://schema.org/', 'sh': 'http://www.w3.org/ns/shacl#', 'skos': 'http://www.w3.org/2004/02/skos/core#', 'sosa': 'http://www.w3.org/ns/sosa/', 'ssn': 'http://www.w3.org/ns/ssn/', 'time': 'http://www.w3.org/2006/time#', 'vann': 'http://purl.org/vocab/vann/', 'void': 'http://rdfs.org/ns/void#', 'xsd': 'http://www.w3.org/2001/XMLSchema#'}, '@id': 'http://example.org/JohnDoe', '@type': 'schema1:Person', 'schema1:name': 'John Doe'}]
_type= ["http://schema.org/Organization"]
resources = forge.from_graph(data=graph, type= _type,use_model_context=True)
isinstance(resources, Resource)
True
print(resources)
isinstance(resources, Resource)
True
epfl_json = {'id': 'https://www.grid.ac/institutes/grid.5333.6',
'type': 'Organization',
'name': 'EPFL'}
epfl_json == forge.as_json(resources)
True
_type= ["http://schema.org/Organization"]
resources = forge.from_graph(data=graph, type= _type, use_model_context=True)
isinstance(resources, Resource)
True