#!/usr/bin/env python # coding: utf-8 # # Modeling # # [Modeling](https://nexus-forge.readthedocs.io/en/latest/interaction.html#modeling) enables the definition and access of data types and properties defined in schemas (e.g JSON templates, [W3C SHACL](https://www.w3.org/TR/shacl)). # Models are configured in the `Model` section of the configuration file. # In[ ]: 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](00%20-%20Initialization.ipynb). # In[ ]: forge = KnowledgeGraphForge("../../configurations/forge.yml") # ## Imports # In[ ]: from kgforge.core import Resource # ## Prefixes # Prefixes are namespaces that are used to put Resource properties within a context. # In[ ]: forge.prefixes() # ## Types # 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. # In[ ]: forge.types() # ## Templates # The template will provide a set of properties for the givent type that is recomended to be used when creating Resources. # ### showing the properties of a type + getting the template of a Mapping for a type # In[ ]: forge.template("Person") # In[ ]: forge.template("Person", only_required=True) # ### creating (a) Resource instance(s) # #### manually (JSON) # In[ ]: forge.template("Person", output="json", only_required=True) # In[ ]: data = { "type": "Person", "name": "Jane" } # In[ ]: resource_json = forge.from_json(data) # In[ ]: print(resource_json) # #### programmatically (Dict) # In[ ]: template = forge.template("Person", output="dict", only_required=True) # In[ ]: template["name"] = "Jane" # In[ ]: resource_dict = forge.from_json(template) # In[ ]: print(resource_dict) # ## Validation # It is possible to verify that a Resource is compliant with the suggested type schema available in the Model. # In[ ]: jane = Resource(type="Person", name="Jane Doe") # In[ ]: john = Resource(type="Person", name="John Smith") # In[ ]: persons = [jane, john] # In[ ]: forge.validate(persons) # In[ ]: jane._last_action # In[ ]: jane._validated # ### automatic status update # In[ ]: jane.email = "jane.doe@epfl.ch" # In[ ]: jane._validated # ### lazy actions handling # In[ ]: distribution = forge.attach("../../data/persons.csv") # In[ ]: jane = Resource(type="Person", name="Jane Doe", distribution=distribution) # In[ ]: forge.validate(jane) # Note: DemoStore doesn't implement file operations yet. Please use another store for the following cell. # In[ ]: forge.validate(jane, execute_actions_before=True) # ### error handling # Note: DemoModel and RdfModel schemas have not been synchronized yet. This section is to be run with RdfModel. Commented lines are for DemoModel. # In[ ]: mistake = Resource(type="Person") # In[ ]: # resource = Resource(type="Association", agent=mistake) resource = Resource(type="Dataset", contribution=mistake) # In[ ]: forge.validate(resource) # In[ ]: resource._last_action # In[ ]: resource._validated