Hello World
Contents
Hello World#
Install GenDifS: Until further notice, there is no installation via pip. Installation by hand is simple:
Download the module
gd06.py
into any directory, e.g. the directory../py/
.Include this directory in the Python import path.
import sys
sys.path.append('../py/')
from gd06 import GenDifS_Map
import pandas as pd
We recommend to create mindmaps in a separate directory, e.g. ../mm/
.
mindmap = "hello_world" # filename without .mm extension
o = GenDifS_Map(f"../mm/{mindmap}.mm", verbose = 1)
cwd into input_dir: /home/dsci/a/l/LA_2023_ss/gendifs/mm
reading /home/dsci/a/l/LA_2023_ss/gendifs/mm > hello_world.mm (9 nodes)
If you plan to write back the current mindmap it might be wise to create a backup?
backup_mindmap = True
import time
if backup_mindmap:
mindmap_backup_filename = f"../mm/{mindmap}_backup_{time.strftime('%Y-%m-%dT%H-%M-%S')}"
o.mindmap.write(mindmap_backup_filename, pretty_print=True)
print(f"wrote backup to {mindmap_backup_filename}")
wrote backup to ../mm/hello_world_backup_2023-04-04T17-10-26
Compile the mindmap into a ttl representation:#
o.compile()
rdflib: 77 triples.
# The compiled code is put into a pandas data frame
# o.code_df.head(6)
o.compile()
generates all necessary class attributes, but does not perform inferencing. To get all instances which can be inferred use o.owlrl()
:
o.owlrl()
owlrl: 388 triples.
SPARQL#
Feel free to query the graph with SPARQL. Some useful default namespaces are contained in the attribute o.sparql_namespaces
.
Q = o.sparql_namespaces + """
SELECT ?sub ?super
WHERE { ?sub skos:broader ?super }
"""
qres = o.rdflib_graph.query(Q)
for row in qres:
print(row)
(rdflib.term.URIRef('http://example.net/namespace/cpt#milk'), rdflib.term.URIRef('http://example.net/namespace/cpt#topConcept'))
(rdflib.term.URIRef('http://example.net/namespace/cpt#milk_of_a_cow'), rdflib.term.URIRef('http://example.net/namespace/cpt#milk'))
(rdflib.term.URIRef('http://example.net/namespace/cpt#goat_milk'), rdflib.term.URIRef('http://example.net/namespace/cpt#milk'))
Save the ontologies#
We recommend saving the generated taxonomy in Turtle format in a separate directory, e.g. ../ttl/
.
This is what the ttl code looks like, generated directly by GenDifS:
with open(f"../ttl/{mindmap}_mm.ttl", "w") as file:
file.write(o.ttl_code)
Export of all triples through rdflib, without inferencing:
len(o.rdflib_graph.serialize(destination=f"../ttl/{mindmap}_rdflib.ttl"))
77
Export of all triples through rdflib, after inferencing:
len(o.owlrl_graph.serialize(destination=f"../ttl/{mindmap}_owlrl.ttl"))
388
Save back the mindmap#
update_mindmap = True
if update_mindmap:
update_mindmap_filename = f"../mm/{mindmap}.mm"
o.mindmap.write(update_mindmap_filename,pretty_print=True)
print(f"updated mindmap {update_mindmap_filename}")
updated mindmap ../mm/hello_world.mm