#!/usr/bin/env python # coding: utf-8 # # Reshape # # [Reshaping](https://nexus-forge.readthedocs.io/en/latest/interaction.html#reshaping) will create a copy of a Resource while keeping a set of specified properties. # 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 # ## Reshaping # ### basics # In[ ]: jane = Resource(type="Person", name="Jane Doe") # In[ ]: john = Resource(type="Person", name="John Smith") # In[ ]: persons = [jane, john] # In[ ]: resources = forge.reshape(persons, keep=["name"]) # In[ ]: type(resources) # In[ ]: type(resources[0]) # In[ ]: print(jane) # In[ ]: print(resources[0]) # ### advanced # In[ ]: jane = Resource(type="Person", name="Jane Doe") # In[ ]: association = Resource(type="Association", agent=jane) # In[ ]: resource = forge.reshape(association, keep=["type", "agent.name"]) # In[ ]: print(association) # In[ ]: print(resource) # ### reshape versioned # In[ ]: jane = Resource(type="Person", name="Jane Doe") # In[ ]: forge.register(jane) # In[ ]: resource = forge.reshape(jane, keep=["id", "type"], versioned=True) # In[ ]: forge.as_json(jane) # In[ ]: print(resource) # ### error handling # In[ ]: john = Resource(type="Person", name="John Smith") # In[ ]: resource = forge.reshape(john, keep=["id", "type"], versioned=True) # In[ ]: resource is None