Skip to content

Commit

Permalink
Update mappings to use new triplestore (#284)
Browse files Browse the repository at this point in the history
* Throughout update of mappings.py. 
* Added the Step class to represent different mapping routes and easy selection of the best one
* Added cost for different routes for automatic suggestion and sorting of routes
* Handle units correctly
* Added functions for instantiating dlite instances.
* Use the triplestore sub-package - use a Triplestore instance instead of a list of tuples
* Also ensured that functions IRIs does not change between sessions, via the new function_id() help function.
* Made MappingError a subclass of DLiteError as suggested by Francesca.
* Added getprop() method to metadata for easy access to its properties.
* Infer function_repo from triplestore.
* Apply suggestions from code review by Francesca

Co-authored-by: Francesca L. Bleken <48128015+francescalb@users.noreply.github.com>
  • Loading branch information
jesper-friis and francescalb authored Aug 4, 2022
1 parent 14a7231 commit cca64d2
Show file tree
Hide file tree
Showing 9 changed files with 986 additions and 313 deletions.
1 change: 1 addition & 0 deletions bindings/python/__init__standalone.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ storage_path.append(str(thispath / "share/dlite/storages/*.json"))
# DLITE_TEMPLATE_DIRS: Search path for DLite templates.


from .dlite import * # noqa: F401, F403
from .factory import classfactory, objectfactory, instancefactory # noqa: F401

del thispath
Expand Down
15 changes: 14 additions & 1 deletion bindings/python/dlite-entity-python.i
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ class Metadata(Instance):
def __repr__(self):
return f"<Metadata: uri='{self.uri}'>"

def getprop(self, name):
"""Returns the metadata property object with the given name."""
lst = [p for p in self.properties["properties"] if p.name == name]
if lst:
return lst[0]
raise DLiteError(f"Metadata {self.uri} has no such property: {name}")


def standardise(v, prop, asdict=False):
"""Represent property value `v` as a standard python type.
Expand Down Expand Up @@ -98,7 +105,10 @@ def get_instance(id: "str", metaid: "str"=None, check_storages: "bool"=True) ->
Returns:
DLite instance.
"""
inst = _dlite.get_instance(id, metaid, check_storages)
if isinstance(id, dlite.Instance):
inst = id
else:
inst = _dlite.get_instance(id, metaid, check_storages)
if inst is None:
raise DLiteError(f"no such instance: {id}")
elif inst.is_meta:
Expand Down Expand Up @@ -284,6 +294,9 @@ def get_instance(id: "str", metaid: "str"=None, check_storages: "bool"=True) ->
if isinstance(dims, dict):
meta = get_instance(metaid)
dims = [dims[dim.name] for dim in meta.properties['dimensions']]
# Allow metaid to be an Instance
if isinstance(metaid, dlite.Instance):
metaid = metaid.uri
return Instance(
metaid=metaid, dims=dims, id=id,
dimensions=(), properties=() # arrays must not be None
Expand Down
Loading

0 comments on commit cca64d2

Please sign in to comment.