The configured Model
in the forge will provide predefined properties of a set of Types if may contain.
from kgforge.core import KnowledgeGraphForge
forge = KnowledgeGraphForge("../../configurations/demo-forge.yml")
import json
from kgforge.core import Resource
from kgforge.specializations.resources import Dataset
Prefixes are namespaces that are used to put Resource properties within a context.
forge.prefixes()
Used prefixes: prov http://www.w3.org/ns/prov# rdf http://www.w3.org/1999/02/22-rdf-syntax-ns# schema http://schema.org/
The type
property of a Resource can be associated to the available types in the Model. These types have a pre-defined set of properties.
forge.types()
Managed entity types: - Association - Person
The template will provide a set of properties for the givent type that is recomended to be used when creating Resources.
forge.template("Person")
<info> DemoModel does not distinguish values and constraints in templates for now. <info> DemoModel does not automatically include nested schemas for now. { type: Person name: hasattr }
# forge.template("Person", only_required=True)
# forge.template("Association")
forge.template("Person", output="json")
<info> DemoModel does not distinguish values and constraints in templates for now. <info> DemoModel does not automatically include nested schemas for now. { "type": "Person", "name": "hasattr" }
data = {
"type": "Person",
"name": "Jane"
}
resource_json = forge.from_json(data)
print(resource_json)
{ type: Person name: Jane }
template = forge.template("Person", output="dict")
<info> DemoModel does not distinguish values and constraints in templates for now. <info> DemoModel does not automatically include nested schemas for now.
template["name"] = "Jane"
resource_dict = forge.from_json(template)
print(resource_dict)
{ type: Person name: Jane }
It is possible to verify that a Resource is compliant with the suggested type schema available in the Model.
jane = Resource(type="Person", name="Jane Doe")
john = Resource(type="Person", name="John Smith")
persons = [jane, john]
forge.validate(persons)
<count> 2 <action> _validate_one <succeeded> True
jane._last_action
Action(error=None, message=None, operation='_validate_one', succeeded=True)
jane._validated
True
jane.email = "jane.doe@epfl.ch"
jane._validated
False
! cat ../../data/persons.csv
type,name,distribution Person,Marie Curie,../../data/scientists-database/marie_curie.txt Person,Albert Einstein,../../data/scientists-database/albert_einstein.txt
distribution = forge.attach("../../data/persons.csv")
jane = Resource(type="Person", name="Jane Doe", distribution=distribution)
forge.validate(jane)
<action> _validate_one <succeeded> False <error> ValidationError: resource has lazy actions which need to be executed before
# forge.validate(jane, execute_actions_before=True)
mistake = Resource(type="Person")
resource = Resource(type="Association", agent=mistake)
forge.validate(resource)
<action> _validate_one <succeeded> False <error> ValidationError: name is missing
resource._last_action
Action(error='ValidationError', message='name is missing', operation='_validate_one', succeeded=False)
resource._validated
False