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
Prefixes are namespaces that are used to put Resource properties within a context.
forge.prefixes()
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()
The template will provide a set of properties for the givent type that is recomended to be used when creating Resources.
forge.template("Person")
{ id: "" additionalName: "" affiliation: { id: "" type: Organization address: "" email: "" identifier: "" name: "" parentOrganization: "" } email: "" familyName: "" givenName: "" identifier: { id: "" propertyID: "" value: { id: "" } } }
forge.template("Person", only_required=True)
{ id: "" }
forge.template("Person", output="json", only_required=True)
{ "id": "" }
data = {
"type": "Person",
"name": "Jane"
}
resource_json = forge.from_json(data)
print(resource_json)
{ type: Person name: Jane }
template = forge.template("Person", output="dict", only_required=True)
template["name"] = "Jane"
resource_dict = forge.from_json(template)
print(resource_dict)
{ id: "" 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_many <succeeded> True
jane._last_action
Action(error=None, message=None, operation='_validate_many', succeeded=True)
jane._validated
True
jane.email = "jane.doe@epfl.ch"
jane._validated
False
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
Note: DemoStore doesn't implement file operations yet. Please use another store for the following cell.
forge.validate(jane, execute_actions_before=True)
<action> _validate_one <succeeded> True
By default the resource(s) will be validated against the schema of the type
attribute of the resource.
However, it is possible to specify the schema to validate against by passing the type_
argument in .validate()
forge.validate(jane, execute_actions_before=True, type_="Person")
<action> _validate_one <succeeded> True
Note: DemoModel and RdfModel schemas have not been synchronized yet. This section is to be run with RdfModel. Commented lines are for DemoModel.
mistake = Resource(type="Person")
# resource = Resource(type="Association", agent=mistake)
resource = Resource(type="Dataset", contribution=mistake)
forge.validate(resource)
<action> _validate_one <succeeded> False <error> ValidationError: Validation Report Conforms: False Results (1): Constraint Violation in NodeConstraintComponent (http://www.w3.org/ns/shacl#NodeConstraintComponent): Severity: sh:Violation Source Shape: this5:DatasetShape Focus Node: [ <https://neuroshapes.org/contribution> [ rdf:type <http://schema.org/Person> ] ; rdf:type <http://schema.org/Dataset> ] Value Node: [ <https://neuroshapes.org/contribution> [ rdf:type <http://schema.org/Person> ] ; rdf:type <http://schema.org/Dataset> ] Message: Value does not conform to Shape this6:MINDSShape
resource._last_action
Action(error='ValidationError', message='\nValidation Report\nConforms: False\nResults (1):\nConstraint Violation in NodeConstraintComponent (http://www.w3.org/ns/shacl#NodeConstraintComponent):\n\tSeverity: sh:Violation\n\tSource Shape: this5:DatasetShape\n\tFocus Node: [ <https://neuroshapes.org/contribution> [ rdf:type <http://schema.org/Person> ] ; rdf:type <http://schema.org/Dataset> ]\n\tValue Node: [ <https://neuroshapes.org/contribution> [ rdf:type <http://schema.org/Person> ] ; rdf:type <http://schema.org/Dataset> ]\n\tMessage: Value does not conform to Shape this6:MINDSShape\n', operation='_validate_one', succeeded=False)
resource._validated
False