This notebook demonstrates how to retrieve, query and search data using the Forge.
from kgforge.core import KnowledgeGraphForge
forge = KnowledgeGraphForge("../../configurations/demo-forge.yml")
from kgforge.core import Resource
jane = Resource(type="Person", name="Jane Doe")
forge.register(jane)
<action> _register_one <succeeded> True
resource = forge.retrieve(jane.id)
resource == jane
True
jane = Resource(type="Person", name="Jane Doe")
forge.register(jane)
<action> _register_one <succeeded> True
forge.tag(jane, "v1")
<action> _tag_one <succeeded> True
jane.email = "jane.doe@epfl.ch"
forge.update(jane)
<action> _update_one <succeeded> True
jane._store_metadata.version
2
jane_v1 = forge.retrieve(jane.id, version=1)
jane_v1_tag = forge.retrieve(jane.id, version="v1")
jane_v1 == jane_v1_tag
True
print(jane_v1)
{ id: 9e689d89-3425-46d8-95e7-7058e9724ea4 type: Person name: Jane Doe }
resource = forge.retrieve("123")
<action> retrieve <error> RetrievalError: resource not found
print(resource)
None
jane = Resource(type="Person", name="Jane Doe")
john = Resource(type="Person", name="John Smith")
association_jane = Resource(type="Association", agent=jane)
association_john = Resource(type="Association", agent=john)
associations = [association_jane, association_john]
forge.register(associations)
<count> 2 <action> _register_one <succeeded> True
Association
is a known type by the Model, and the paths
method will load the data structure for the given type. Refer to the 11 - Modeling.ipynb
notebook to learn about Modeling and Types.
p = forge.paths("Association")
<info> DemoModel does not distinguish values and constraints in templates for now. <info> DemoModel does not automatically include nested schemas for now.
You have autocompletion on p
and this can be used to build a search.
resources = forge.search(p.type == "Association")
<info> DemoStore does not support handling of errors with QueryingError for now. <info> DemoStore does not support traversing lists for now.
type(resources)
list
len(resources)
2
type(resources[0])
kgforge.core.resource.Resource
forge.as_dataframe(resources)
id | type | agent.type | agent.name | |
---|---|---|---|---|
0 | 40cf0989-175a-4948-ae49-014546efbd8e | Association | Person | Jane Doe |
1 | b5c39b73-cc11-4339-8c70-47f802f8ddaf | Association | Person | John Smith |
forge.as_dataframe(resources, store_metadata=True)
id | type | agent.type | agent.name | deprecated | version | |
---|---|---|---|---|---|---|
0 | 40cf0989-175a-4948-ae49-014546efbd8e | Association | Person | Jane Doe | False | 1 |
1 | b5c39b73-cc11-4339-8c70-47f802f8ddaf | Association | Person | John Smith | False | 1 |
You have autocompletion on p
but also on nested properties like p.agent
.
resources = forge.search(p.type == "Association", p.agent.name == "John Smith")
<info> DemoStore does not support handling of errors with QueryingError for now. <info> DemoStore does not support traversing lists for now.
len(resources)
1
# print(resources[0])
SPARQL is used to traverse the graph. The user can provide simplified queries: no prefixes, no compacted URIs, nor full URIs is required, only the term (property) available in the Model to the user. Refer to the 11 - Modeling.ipynb
notebook to learn about Templates.
jane = Resource(type="Person", name="Jane Doe")
john = Resource(type="Person", name="John Smith")
association = Resource(type="Association", agent=[jane, john])
forge.register(association)
<action> _register_one <succeeded> True
forge.template("Association")
<info> DemoModel does not distinguish values and constraints in templates for now. <info> DemoModel does not automatically include nested schemas for now. { type: Association agent: { type: Person name: hasattr } }
query = """
SELECT ?x ?name
WHERE {
?x a Association ;
agent ?agent .
?agent name ?name .
}
"""
# resources = forge.sparql(query)
# type(resources)
# len(resources)
# type(resources[0])
forge.sparql(query, debug=True)
Submitted query: PREFIX prov: <http://www.w3.org/ns/prov#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX schema: <http://schema.org/> SELECT ?x ?name WHERE { ?x rdf:type prov:Association ; prov:agent ?agent . ?agent schema:name ?name . } <action> not_supported <error> NotSupportedError: DemoStore is not supporting _sparql()
jane = Resource(type="Person", name="Jane Doe")
! ls -p ../../data | egrep -v /$
associations.tsv persons.csv
distribution = forge.attach("../../data")
association = Resource(type="Association", agent=jane, distribution=distribution)
# forge.register(association)
# forge.download(association, "distribution.contentUrl", "./downloaded/")
# ! ls ./downloaded/