def add_class_construct_entity(self): """Adds class construct metadata to collection and returns a reference to it.""" uri = self.get_uri("ClassConstruct") uuid = self.get_uuid(uri) if not self.coll.has('ClassConstruct'): props = [ Property('type', type='string', description='Type of ' 'class construct. Valid values for `type` are: ' '"not", "inverse", "and" or "or".'), ] e = Instance( uri, [], props, "Class construct. For each instance of a class " "construct there should be one or more relations " "of type\n" "\n" " (c.label, \"has_argument\", c.value.label)\n" "\n" "where `c.label` is the label associated with the " "class construct, `c.value.label` is the label of " "an argument.") self.coll.add('ClassConstruct', e) return self.coll.get('ClassConstruct')
def add_restriction_entity(self): """Adds restriction metadata to collection and returns a reference to it.""" uri = self.get_uri("Restriction") uuid = self.get_uuid(uri) if not self.coll.has('Restriction'): props = [ Property('type', type='string', description='Type of ' 'restriction. Valid values for `type` are: ' '"only", "some", "exact", "min" and "max".'), Property('cardinality', type='int', description='The ' 'cardinality. Unused for "only" and ' '"some" restrictions.'), ] e = Instance( uri, [], props, "Class restriction. For each instance of a class " "restriction there should be a relation\n" "\n" " (r.label, r.property, r.value.label)\n" "\n" "where `r.label` is the label associated with the " "restriction, `r.property` is a relation and " "`r.value.label` is the label of the value of the " "restriction.") self.coll.add('Restriction', e) return self.coll.get('Restriction')
def add_class(self, cls): """Adds owl class `cls` to collection and returns a reference to the new metadata.""" if isinstance(cls, str): cls = self.onto[cls] label = cls.label.first() if not self.coll.has(label): uri = self.get_uri(label) dims, props = self.get_properties(cls) e = Instance(uri, dims, props, self.get_description(cls)) self.coll.add(label, e) for r in cls.is_a: if r is owlready2.Thing: pass elif isinstance(r, owlready2.ThingClass): self.coll.add_relation(label, "is_a", r.label.first()) self.add_class(r) elif isinstance(r, owlready2.Restriction): if (issubclass(r.property, self.onto.has_property) and isinstance(r.value, owlready2.ThingClass) and isinstance(r.value, self.onto.property)): self.add_class(r.value) else: self.add_restriction(r) elif isinstance(r, owlready2.ClassConstruct): self.add_class_construct(r) else: raise TypeError('Unexpected is_a member: %s' % type(r)) return self.coll.get(label)
#!/usr/bin/env python # -*- coding: utf-8 -*- import os import dlite from dlite import Instance, Dimension, Property, Relation assert len(dlite.istore_get_uuids()) == 3 + 3 thisdir = os.path.abspath(os.path.dirname(__file__)) url = 'json://' + thisdir + '/MyEntity.json' # myentity is already defined via test_global_dlite_state, no new instance is added to istore myentity = Instance.create_from_url(url) assert myentity.uri == "http://onto-ns.com/meta/0.1/MyEntity" assert len(dlite.istore_get_uuids()) == 3 + 3 i1 = Instance.create_from_metaid(myentity.uri, [2, 3], 'myid') assert i1.uri == "myid" assert i1.uuid in dlite.istore_get_uuids() assert len(dlite.istore_get_uuids()) == 3 + 4
from global_dlite_state_mod1 import assert_exists_in_module thisdir = os.path.abspath(os.path.dirname(__file__)) assert len(dlite.istore_get_uuids()) == 3 # 3 Hardcoded dlite instances coll = dlite.Collection() # (1) assert dlite.has_instance(coll.uuid) assert coll.uuid in dlite.istore_get_uuids() assert len(dlite.istore_get_uuids()) == 3 + 1 # Must exist in imported dlite in different module (mod1) assert_exists_in_module(coll.uuid) url = 'json://' + thisdir + '/MyEntity.json' + "?mode=r" e = Instance.create_from_url(url) # (2) assert len(dlite.istore_get_uuids()) == 3 + 2 inst1 = Instance.create_from_metaid(e.uri, [3, 2]) # (3) assert len(dlite.istore_get_uuids()) == 3 + 3 inst2 = Instance.create_from_metaid(e.uri, (3, 4), 'myinst') # (4) assert len(dlite.istore_get_uuids()) == 3 + 4 del inst1 assert len(dlite.istore_get_uuids()) == 3 + 3 # Use compile and exec with dlite defined in globals env = globals().copy() filename = os.path.join(thisdir, 'global_dlite_state_mod2.py')
#!/usr/bin/env python # -*- coding: utf-8 -*- import os import pickle import numpy as np import dlite from dlite import Instance, Dimension, Property, Relation thisdir = os.path.abspath(os.path.dirname(__file__)) url = 'json://' + thisdir + '/MyEntity.json' # Load metadata (i.e. an instance of meta-metadata) from url myentity = Instance.create_from_url(url) print(myentity.uuid) # Check some properties of the entity assert myentity.uuid == 'a0e63529-3397-5c4f-a56c-14bf07ecc219' assert myentity.uri == 'http://onto-ns.com/meta/0.1/MyEntity' assert myentity.dimensions == {'ndimensions': 2, 'nproperties': 14} assert not myentity.is_data assert myentity.is_meta assert not myentity.is_metameta # Store the entity to a new file myentity.save('json://xxx.json?mode=w') # Try to overwrite without mode - should fail because metadata is immutable try:
import dlite from dlite import Instance, Collection thisdir = os.path.abspath(os.path.dirname(__file__)) coll = Collection() coll.add_relation('cat', 'is-a', 'animal') coll.add_relation('dog', 'is-a', 'animal') itr = coll.get_iter() while itr.poll(): print(itr.find()) url = 'json://' + thisdir + '/MyEntity.json' + "?mode=r" # Load metadata (i.e. an instance of meta-metadata) from url e = Instance(url) inst1 = Instance(e.uri, [3, 2]) inst2 = Instance(e.uri, (3, 4), 'myinst') coll.add('inst1', inst1) coll.add('inst2', inst2) assert coll.count() == 2 #assert coll.has print(list(coll))
#!/usr/bin/env python # -*- coding: utf-8 -*- import os import sys import numpy as np import dlite from dlite import Storage, Instance thisdir = os.path.abspath(os.path.dirname(__file__)) url = 'json://' + thisdir + '/MyEntity.json' #+ "?mode=r" # Load metadata (i.e. an instance of meta-metadata) from url s = Storage(url) myentity = Instance(s, 'http://meta.sintef.no/0.1/MyEntity') del s with Storage(url) as s2: myentity2 = Instance(s2, 'http://meta.sintef.no/0.1/MyEntity')
# -*- coding: utf-8 -*- import os import sys import pickle import numpy as np import dlite from dlite import Instance, Dimension, Property, Relation thisdir = os.path.abspath(os.path.dirname(__file__)) url = 'json://' + thisdir + '/MyEntity.json' #+ "?mode=r" # Load metadata (i.e. an instance of meta-metadata) from url myentity = Instance(url) print(myentity.uuid) # Check some properties of the entity assert myentity.uuid == 'ea34bc5e-de88-544d-bcba-150b7292873d' assert myentity.uri == 'http://meta.sintef.no/0.1/MyEntity' assert myentity.dimensions == {'ndimensions': 2, 'nproperties': 14} assert not myentity.is_data assert myentity.is_meta assert not myentity.is_metameta # Store the entity to a new file myentity.save('json://xxx.json') # Create an instance of `myentity` with dimensions 2, 3 # For convinience, we give it an unique label "myid" that can be used