def __init__(self, ontology=None, classes=None, version='0.1', collid=None): if ontology is None: self.onto = get_ontology() self.onto.load() elif isinstance(ontology, str): self.onto = get_ontology(ontology) self.onto.load() else: self.onto = ontology self.version = version self.iri = self.onto.base_iri self.namespace = self.onto.base_iri.rstrip('#') self.coll = dlite.Collection(collid) # To avoid infinite recursion when adding cyclic dependent entities # we keep track of all labels we add (or are going to add) to the # collection self.labels = set() if classes is None: classes = self.onto.classes() elif isinstance(classes, str): classes = [classes] for cls in classes: self.add_class(cls)
def __init__(self, ontology=None, classes=None, version='0.1', collid=None): if ontology is None: self.onto = get_ontology() self.onto.load() elif isinstance(ontology, str): self.onto = get_ontology(ontology) self.onto.load() else: self.onto = ontology self.version = version self.iri = self.onto.base_iri self.namespace = self.onto.base_iri.rstrip('#') self.coll = dlite.Collection(collid) if classes is None: classes = self.onto.classes() elif isinstance(classes, str): classes = [classes] for cls in classes: self.add_class(cls)
def __dir__(self): """Include classes in dir() listing.""" f = lambda s: s[s.rindex('.') + 1: ] if '.' in s else s s = set(object.__dir__(self)) for onto in [get_ontology(uri) for uri in self._namespaces.keys()]: s.update([f(repr(cls)) for cls in onto.classes()]) return sorted(s)
def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( 'output', help='name of output file.') parser.add_argument( '--root', '-r', default='entity', help='Name of root node. Defaults to "entity".') #parser.add_argument( # '--resoner', action='store_true', default=True, # help='Run the reasoner (the default)') parser.add_argument( '--no-resoner', '-n', dest='reasoner', action='store_false', help='Do not run the reasoner.') parser.add_argument( '--format', '-f', help='Format of output file. By default it is inferred from ' 'the output file extension.') parser.add_argument( '--rankdir', '-R', default='BT', choices=['BT', 'TB', 'RL', 'LR'], help='Graph direction (from leaves to root). Possible values are: ' '"BT" (bottom-top, default), "TB" (top-bottom), "RL" (right-left) and ' '"LR" (left-right).') parser.add_argument( '--relations', action='store_true', help='Create a graph with only relations.') args = parser.parse_args() onto = emmo.get_ontology('emmo.owl') onto.load() if args.reasoner: onto.sync_reasoner() #rankdir = 'RL' if args.horizontal else 'BT' format = args.format if args.format else os.path.splitext( args.output)[1][1:] if args.relations: graph = onto.get_dot_relations_graph(rankdir=args.rankdir) else: graph = onto.get_dot_graph(args.root, rankdir=args.rankdir) try: graph.write(path=args.output, format=format) except pydot.InvocationException as e: sys.stderr.write(str(e)) sys.exit(1)
#!/usr/bin/env python3 # # This Python script generates the graphs in this directory from the # EMMO owl sources using EMMO-python. import sys import os from emmo import get_ontology from emmo.graph import OntoGraph from emmo.graph import (plot_modules, get_module_dependencies, check_module_dependencies) # Default inferred ontology emmo = get_ontology() emmo.load() # Visualise some core parts of EMMO g = OntoGraph(emmo, emmo.EMMO, leafs=[emmo.Perspective, emmo.Elementary], relations='all', edgelabels=False) g.add_legend() g.save('top.png') leafs = set() for s in emmo.Perspective.subclasses(): leafs.update(s.subclasses()) g = OntoGraph(emmo, emmo.Perspective, leafs=leafs, parents=1, relations='all', edgelabels=False) g.add_legend()
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Plots the user case ontology created with the script `define_ontology.py`. """ from emmo import get_ontology # Load usercase ontology # Create a new ontology with out extensions that imports EMMO onto = get_ontology('usercase_ontology.owl') onto.load() #onto.base_iri = 'http://www.emmc.info/emmc-csa/demo#' # # Visualise our new EMMO-based ontology # ===================================== # Update the uml-style to generate del onto._uml_style['class']['shape'] del onto._uml_style['defined_class']['shape'] # Save graph with our new classes graph = onto.get_dot_graph(list(onto.classes()), relations=True, style='uml', constraint=None) graph.write_svg('usercase_ontology.svg') # Categories of classes units = [c for c in onto.classes() if issubclass(c, onto.SIUnit)] properties = [
parent = tree[node]["ancestors"][0] graph.node(node) graph.edge(parent, node) graph.view() class Node(): def __init__(self, name): self.name = name self.children = [] self.ancestors = [] def addChildren(self, children): self.children = children if __name__ == '__main__': onto = get_ontology('emmo.owl') onto.load() # onto.sync_reasoner() state = onto.state subclasslist = list(onto.state.subclasses()) print(subclasslist) Tree = makeTaggedTree(onto, "state") print(Tree.makeTaggedTree()) makeDotGraph(Tree.makeTaggedTree())
#!/usr/bin/env python3 import sys import os # Add emmo to sys path thisdir = os.path.abspath(os.path.dirname(__file__)) sys.path.insert(1, os.path.abspath(os.path.join(thisdir, '..', '..'))) from emmo import get_ontology from emmo.ontodoc import OntoDoc, DocPP emmo = get_ontology() emmo.load() baseiri = 'http://emmo.info/' iris = set(c.namespace.base_iri for c in emmo.classes()) iris.update(set(c.namespace.base_iri for c in emmo.object_properties())) #iris.update(set(c.namespace.base_iri for c in emmo.annotation_properties())) for s in sorted(iris): print(s) inputdir = os.path.abspath( os.path.join(thisdir, '..', '..', 'examples', 'emmodoc')) inputfile = os.path.join(thisdir, 'doc.md') ontodoc = OntoDoc(emmo) with open(inputfile, 'rt') as f: template = f.read() docpp = DocPP(template, ontodoc, os.path.dirname(inputfile))
This program is part of the ProcessModelling suite """ __project__ = "ProcessModeller Suite" __author__ = "PREISIG, Heinz A" __copyright__ = "Copyright 2015, PREISIG, Heinz A" __since__ = "06.09.2019" __license__ = "GPL planned -- until further notice for Bio4Fuel & MarketPlace internal use only" __version__ = "5.04" __email__ = "*****@*****.**" __status__ = "beta" from emmo import get_ontology from owlready2 import sync_reasoner_pellet emmo = get_ontology() emmo.load() # sync_reasoner_pellet([emmo]) onto = get_ontology("play.owl", ) onto.imported_ontologies.append(emmo) # onto.base_iri = 'http://www.emmc.info/emmc-csa/demo#' onto.base_iri = emmo.base_iri print("classes : ", list(emmo.classes())) print("individuals :", list(emmo.individuals())) print("object_properties :", list(emmo.object_properties())) print("properties :", emmo.search(iri="physical_quantity")) print("base iri :", emmo.base_iri) print("base iri :", onto.base_iri)
# return n # return min(self._number_of_generations(parent, ancestor, n + 1) # for parent in descendant.get_parents() # if ancestor in parent.ancestors()) # # def closest_common_ancestors(self, cls1, cls2): # """Returns a list with closest_common_ancestor to cls1 and cls2""" # distances = {} # for ancestor in self.common_ancestors(cls1, cls2): # distances[ancestor] = self.number_of_generations(cls1, ancestor) + self.number_of_generations(cls2, ancestor) # return [ancestor for ancestor, distance in distances.items() if distance == min(distances.values())] onto_path.append(("../emmo/rdfxml")) emmo = get_ontology("emmo-properties.owl") #emmo-all-inferred.owl") namespace = emmo.get_namespace(emmo.base_iri) print("emmon", emmo) emmo.load() onto = get_ontology("play.owl", ) onto.imported_ontologies.append(emmo) onto.base_iri = emmo.base_iri print("base iri : ", default_world.ontologies) print("classes : ", list(emmo.classes())) print("individuals :", list(emmo.individuals())) print("object_properties :", list(emmo.object_properties())) print("properties :", emmo.search(iri="variable"))
#!/usr/bin/env python3 import sys import os # Add emmo to sys path thisdir = os.path.abspath(os.path.dirname(__file__)) sys.path.insert(1, os.path.abspath(os.path.join(thisdir, '..', '..'))) from emmo import get_ontology # noqa: E402, F401 # Check that the defaults works emmo = get_ontology('emmo').load() # owl format assert emmo.Atom.prefLabel.first() == 'Atom' emmo = get_ontology('emmo-inferred').load() assert emmo.Atom.prefLabel.first() == 'Atom' emmo = get_ontology('emmo-development').load() # ttl format assert emmo.Atom.prefLabel.first() == 'Atom' # Load a local ontology with catalog testonto = os.path.join(os.path.dirname(__file__), 'testonto', 'testonto.ttl') o = get_ontology(testonto).load() assert o.TestClass.prefLabel.first() == 'TestClass' # Use catalog file when downloading from web o = get_ontology('https://raw.githubusercontent.com/BIG-MAP/BattINFO/master/' 'battinfo.ttl').load() assert o.Electrolyte.prefLabel.first() == 'Electrolyte'
# Test importing foaf # # This test serves more like an example from emmo import get_ontology skos = get_ontology('http://www.w3.org/2004/02/skos/core#').load() foaf = get_ontology("http://xmlns.com/foaf/0.1/") # Needed since foaf refer to skos without importing it foaf.imported_ontologies.append(skos) # Turn off label lookup. Needed because foaf uses labels that are not # valid Python identifiers foaf._special_labels = () # Now we can load foaf foaf.load() emmo = get_ontology().load() with emmo: class Person(emmo.Interpreter): equivalent_to = [foaf.Person]
if onto.base_iri in modules: modules[onto.base_iri].add(o.base_iri) else: modules[onto.base_iri] = set([o.base_iri]) if o.base_iri not in modules: modules[o.base_iri] = set() setmodules(o, modules) def getname(iri): """Returns the name part of an iri.""" return os.path.basename(os.path.splitext(iri)[0]) if load_emmo: emmo = get_ontology('http://emmo.info/emmo/1.0.0-alpha') emmo.load() modules = {} setmodules(emmo, modules) modules = {k: list(v) for k, v in modules.items()} with open('emmo-modules.json', 'wt') as f: json.dump(modules, f, indent=4) else: with open('emmo-modules.json', 'rt') as f: modules = json.load(f) # Plot module dependencies from graphviz import Digraph base = 'http://emmo.info/'
"""A script that uses EMMO to map output data to ontology in this thermo-mechanical example. See also https://pythonhosted.org/Owlready2/ """ import emmo as em import owlready2 import re owlready2.set_log_level(0) # Load EMMO #emmo = get_ontology() #Causes error in emmo-all-inferred #emmo = get_ontology("http://www.emmc.info/emmc-csa/emmo-core#") #emmo = owlready2.get_ontology("emmo-all-inferred.owl") #emmo = owlready2.get_ontology("emmo-all-inferred.owl").load() #emmo = get_ontology("http://test.org/onto.owl") emmo = em.get_ontology() #overloaded method emmo.load() #emmo = get_ontology("http://www.emmc.info/emmo-models#") #emmo = get_ontology("http://www.emmc.info/emmc-csa/properties") #emmo = get_ontology("emmo-all-inferred.owl") #emmo.load() #print(emmo.electron_cloud) #print(emmo.has_part) #emmo.sync_attributes() #emmo.sync_reasoner(reasoner="Pellet") # Create a new ontology based on emmo onto = owlready2.get_ontology('onto.owl') onto.imported_ontologies.append(emmo) onto.base_iri = 'http://www.emmc.info/emmc-csa/demo#'
#!/usr/bin/env python3 import sys import os # Add emmo to sys path thisdir = os.path.abspath(os.path.dirname(__file__)) sys.path.insert(1, os.path.abspath(os.path.join(thisdir, '..', '..'))) from emmo import get_ontology ########################################################## ## EMMO base ########################################################## emmo = get_ontology('emmo.owl') emmo.load() graph = emmo.get_dot_graph(relations=True, style='uml') graph.write_svg('graph-noreason.svg') graph.write_pdf('graph-noreason.pdf') emmo.sync_reasoner() #graph = emmo.get_dot_graph(relations=True, style='uml') relations = ( 'is_a', 'equivalent_to', 'disjoint_with', 'inverse_property', ) graph = emmo.get_dot_graph(relations=relations, style='uml') graph.write_svg('graph.svg') graph.write_pdf('graph.pdf')
relations between the entities in the collection. The only relations that are treated especially are `has_property` and `is_property_for`, that are mapped into properties of the generated metadata entities. The generated metadata is finally serialised into a JSON file. """ import os from emmo import get_ontology from emmo2meta import EMMO2Meta # Load our ontology from the vertical case ontopath = os.path.abspath( os.path.join(os.path.dirname(__file__), '..', 'vertical', 'usercase_ontology.owl')) onto = get_ontology(ontopath) onto.load() # hmm, the base iri has cnanged... - bug in owlready2? onto.base_iri = 'http://www.emmc.info/emmc-csa/demo#' # Generate metadata and store it in a JSON file # # This does not include all of EMMO, but only the new classes, # e-bonded_atom and all classes that these relates to. classes = list(onto.classes()) + [onto['e-bonded_atom']] e = EMMO2Meta(ontology=onto, classes=classes, collid='usercase_ontology') e.save('json', 'usercase_metadata.json', 'mode=w') print('Generated metadata for the usercase ontology:') print(' %d instances' % e.coll.count())
__status__ = "beta" # from owlready2 import default_world from emmo import get_ontology from owlready2 import default_world from owlready2 import Nothing from owlready2 import onto_path from owlready2 import Ontology from owlready2 import set_render_func, sync_reasoner, get_namespace import os # Load EMMO emmo = get_ontology() emmo.load() #emmo.sync_reasoner() # Create a new ontology with out extensions that imports EMMO onto = get_ontology('onto.owl') onto.imported_ontologies.append(emmo) onto.base_iri = 'http://www.emmc.info/emmc-csa/demo#' class ProMoOwlOntology(Ontology): def __init__(self): name = None Ontology.__init__(self, default_world, onto.base_iri, name=name) def setupOnto(self):
import os import sys root_dir = os.path.join(os.path.dirname(__file__), '..') # Needed for mercurial version of owlready2 because it doesn't # follow the standard python package conventions sys.path.append(os.path.join(os.environ['VIRTUAL_ENV'], 'src')) # Add EMMO to path sys.path.append(os.path.join(root_dir, 'EMMO')) import emmo import owlready2 emmoOntology = emmo.get_ontology('emmo-0.3_2017-10-26.owl') emmoOntology.load() onto = owlready2.get_ontology('https://ontology-of-electonics.github.io') import electronic_quantity electronic_quantity.electronic_quantity(onto, emmoOntology) import electronic_component electronic_component.electronic_component(onto, emmoOntology) onto.save(file=os.path.join(root_dir, 'owl/electronics.owl'), format='rdfxml')