Skip to content

Commit

Permalink
linter fix
Browse files Browse the repository at this point in the history
  • Loading branch information
francescalb committed Dec 20, 2023
1 parent 477dc72 commit b32cc6f
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 70 deletions.
65 changes: 41 additions & 24 deletions dlite_cuds/dlite2cuds.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
"""Generates an example ABox"""
# from osp.core.namespaces import ex
from pathlib import Path
from typing import Optional

from simphony_osp.session.session import Session
from simphony_osp.ontology.namespace import OntologyNamespace
from simphony_osp.ontology.individual import OntologyIndividual
import dlite
import tripper
from typing import Optional
from simphony_osp.ontology.namespace import OntologyNamespace
from simphony_osp.session.session import Session

from dlite_cuds.utils.utils import DLiteCUDSError


def dlite2cuds(
def dlite2cuds( # pylint: disable=too-many-arguments, too-many-locals
simphony_session: Session,
ontology: OntologyNamespace,
instance: dlite.Instance,
mappings: dlite.Collection, # Should accept other formats eventually
relations: Optional[dlite.Collection]=None, # Should accept other formats eventually
instance: dlite.Instance,
mappings: dlite.Collection, # Should accept other formats eventually
relations: Optional[
dlite.Collection
] = None, # Should accept other formats eventually
mapping_iri: Optional[str] = "http://emmo.info/domain-mappings#mapsTo",
):
"""
Convert a DLite instance to a CUDS object.
"""
mappings_ts = tripper.Triplestore("collection", collection=mappings)
instance_mapping = list(mappings_ts.objects(subject=instance.meta.uri, predicate=mapping_iri))
instance_mapping = list(
mappings_ts.objects(subject=instance.meta.uri, predicate=mapping_iri)
)
if len(instance_mapping) != 1:
raise DLiteCUDSError(
f"Instance {instance} is mapped {len(instance_mapping)} objects. Currently"
"only one mapping is supported.")

"only one mapping is supported."
)

cuds_class = ontology.from_iri(instance_mapping[0])
# Not that this only works if there is exavet match in properties
# between the cuds class and the DLite entity (of the instance).
Expand All @@ -33,15 +42,20 @@ def dlite2cuds(
for inst_prop in instance.properties:
# Check all concepts the property is mapped to
# This can be more than one if it is mapped to more than on ontology
property_mappings = list(mappings_ts.objects(subject=instance.meta.uri + "#" + inst_prop,
predicate=mapping_iri))
property_mappings = list(
mappings_ts.objects(
subject=instance.meta.uri + "#" + inst_prop, predicate=mapping_iri
)
)
# Find the mapping that matches the attribute in the cuds class
for mapping in property_mappings:
if mapping in class_attributes:
cuds_prop = mapping.split("#")[-1]
cuds_prop = mapping.split("#")[-1]
property_dictionary[cuds_prop] = instance.properties[inst_prop]

cuds = cuds_class(**property_dictionary, iri = "https://www.simphony-osp.eu/entity#"+instance.uuid)
cuds = cuds_class(
**property_dictionary, iri="https://www.simphony-osp.eu/entity#" + instance.uuid
)
if relations:
dlite2cuds_add_relations(
simphony_session=simphony_session,
Expand All @@ -51,17 +65,20 @@ def dlite2cuds(

return cuds


def dlite2cuds_add_relations(
simphony_session: Session,
ontology: OntologyNamespace,
relations: dlite.Collection, # Should accept other formats eventually
):
for subject, predicate, object in relations.get_relations(): #s=instance.uuid):
simphony_session: Session,
ontology: OntologyNamespace,
relations: dlite.Collection, # Should accept other formats eventually
):
"""
Add relations to the CUDS object
"""
for subject, predicate, obj in relations.get_relations(): # s=instance.uuid):
if predicate in ["_is-a", "_has-meta", "_has-uuid"]:
continue
target = simphony_session.get("https://www.simphony-osp.eu/entity#"+object)
source = simphony_session.get("https://www.simphony-osp.eu/entity#"+subject)
target = simphony_session.get("https://www.simphony-osp.eu/entity#" + obj)
source = simphony_session.get("https://www.simphony-osp.eu/entity#" + subject)

if source and target:
source.connect(target, rel=ontology.from_iri(predicate))

30 changes: 20 additions & 10 deletions dlite_cuds/utils/rdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,17 @@ def get_unique_triple(
graph,
subj,
predicate="http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
debug=False,
debug=True,
dtype=False,
):
"""Get unique triple"""
predicate_m = "<" + predicate + ">"
subj_m = "<" + subj + ">"
query = f"""SELECT ?o WHERE {{ {subj_m} {predicate_m} ?o . }}"""

print("subj_m", subj_m)
print("predicate_m", predicate_m)
qres = graph.query(query)
print(qres)
if debug:
print(query, len(qres))

Expand Down Expand Up @@ -184,21 +186,24 @@ def get_object_props_uri(graph, subj, relations):

# get the uri of the prop that is in relation with the subj Datum
subj_m = "<" + subj + ">"
print("subj_m", subj_m)

# build value list from relations
value_list = ""
for rel in relations:
value_list += " <" + rel + "> "
print("rel", rel)
print("value_list", value_list)
value_list = "VALUES ?p { " + value_list + " }"

query = f"""SELECT ?o WHERE {{ {value_list} {subj_m} ?p ?o . }}"""

print("query", query)
qres = graph.query(query)

print("qres", qres)
list_prop_uri = []
for row in qres:
list_prop_uri.append(str(row.o))

print("list_prop_uri", list_prop_uri)
return list_prop_uri


Expand All @@ -213,10 +218,12 @@ def get_value_prop(
if the property is missing one of this element, return an empty dict
"""
dict_prop = {}
print("in get_value_prop to get concept")
concept = get_unique_triple(graph, prop_uri)
print("concept", concept)
if concept is None:
return {}

value_predicate = "http://www.w3.org/2002/07/owl#DatatypeProperty"
dict_prop["concept"] = concept

# unit = get_unique_triple(graph, prop_uri, predicate=unit_predicate)
Expand All @@ -225,8 +232,10 @@ def get_value_prop(
# dict_prop["unit"] = unit

value, datatype = get_unique_triple(
graph, prop_uri, predicate=value_predicate, dtype=True
graph, prop_uri, predicate=value_predicate, dtype=False
)
print("valeu", value)
print("datatype", datatype)
if value is None:
return {}

Expand Down Expand Up @@ -257,13 +266,14 @@ def get_unique_prop_fromlist_uri(
# build value list from relations
valuelist = ""
for subj in listsubj:
valuelist += " <" + subj + "> "
valuelist = subj
# valuelist += " <" + subj + "> "
valuelist = "VALUES ?s { " + valuelist + " }"

query = f"""SELECT ?s WHERE {{ {valuelist} ?s {predicatem} {objm} . }}"""

print("query", query)
qres = graph.query(query)

print("aaaaa", len(qres))
if len(qres) != 1:
print("getUniquePropFromListURI: not exactly one object for uniquetriple query")
subj = None
Expand Down
3 changes: 3 additions & 0 deletions docs/api_reference/dlite2cuds.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# dlite2cuds

::: dlite_cuds.dlite2cuds
3 changes: 0 additions & 3 deletions docs/api_reference/strategies/parse.md

This file was deleted.

6 changes: 5 additions & 1 deletion tests/test_cuds2dlite_create_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ def test_cuds2dlite_simphony_create_instances(repo_dir):

# mappings to be provided as a DLite collection in current implementation
mappings_path = (
repo_dir / "tests" / "inputfiles_dlite2cuds" / "mappings" / "mappings_example.json"
repo_dir
/ "tests"
/ "inputfiles_dlite2cuds"
/ "mappings"
/ "mappings_example.json"
)
mappings = dlite.Collection.from_url(f"json://{str(mappings_path)}")

Expand Down
47 changes: 31 additions & 16 deletions tests/test_dlite2cuds_add_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,54 @@
functionality created with the use of simphony
"""
from pathlib import Path
#if True:


# if True:
def test_dlite2cuds_create_cuds(repo_dir):
"""
Test creation of cuds instances from dlite instances.
Here all instances are first created.
Here all instances are first created.
Relations are added at the end after all individuals are created.
"""
import dlite
import pytest
from simphony_osp.session import core_session
#from simphony_osp.tools import export_file

# from simphony_osp.tools import export_file
from simphony_osp.tools.pico import install

from dlite_cuds.dlite2cuds import dlite2cuds, dlite2cuds_add_relations

# Install the ontology if not already done
install(repo_dir / "tests" / "ontologies" / "example_sim.ttl.yml")
from simphony_osp.namespaces import ex
core_session.clear(force=True)

core_session.clear(force=True)
# import DLite instance
dlite.storage_path.append(repo_dir / "tests" / "inputfiles_dlite2cuds" / "entities")

relations = dlite.Collection.from_location("json", repo_dir / "tests" / "inputfiles_dlite2cuds" / "instances" / "instances_and_relations.json",
id="3a74c3c5-4eb8-4549-817e-c06eeaa4b600")

relations = dlite.Collection.from_location(
"json",
repo_dir
/ "tests"
/ "inputfiles_dlite2cuds"
/ "instances"
/ "instances_and_relations.json",
id="3a74c3c5-4eb8-4549-817e-c06eeaa4b600",
)
# Get the instance we want to convert
inst1 = relations.get("6ce00d67-46dc-404f-901b-41d17ba7b0a0")

# Get the mappings
mappings = dlite.Collection.from_location("json", repo_dir / "tests" / "inputfiles_dlite2cuds" / "mappings" / "mappings_example.json")
mappings = dlite.Collection.from_location(
"json",
repo_dir
/ "tests"
/ "inputfiles_dlite2cuds"
/ "mappings"
/ "mappings_example.json",
)
mapping_iri = "http://emmo.info/domain-mappings#mapsTo"
# Convert the instance
cuds1 = dlite2cuds(
Expand All @@ -42,11 +61,10 @@ def test_dlite2cuds_create_cuds(repo_dir):
mapping_iri=mapping_iri,
relations=relations,
)
assert cuds1.dpOne == 'a'
assert cuds1.dpOne == "a"
assert cuds1.dpTwo == 1
assert set(cuds1.relationships_iter(return_rel=True)) == set()


# Cuds2a and 2b are indivuduals of the same class
inst2a = relations.get("a67ea05c-0134-497e-bbf5-db5a7050d126")
cuds2a = dlite2cuds(
Expand Down Expand Up @@ -81,19 +99,16 @@ def test_dlite2cuds_create_cuds(repo_dir):
)

assert cuds3.dpTwo == 3


dlite2cuds_add_relations(
simphony_session=core_session,
ontology=ex,
relations=relations,
)
assert cuds1.dpOne == 'a'
assert cuds1.dpOne == "a"
assert cuds1.dpTwo == 1

assert set(cuds1.relationships_iter(return_rel=True)) == {
(cuds2b, ex.opOne),
(cuds3, ex.opTwo),
}


Loading

0 comments on commit b32cc6f

Please sign in to comment.