Skip to content

Commit

Permalink
Merge pull request #339 from emmo-repo/visualise-restriction-type
Browse files Browse the repository at this point in the history
When visualising restrictions, annotate the edges with the restriction type by default
  • Loading branch information
jesper-friis authored Jan 5, 2022
2 parents 0413909 + 3804dc7 commit 154dca8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
27 changes: 19 additions & 8 deletions ontopy/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
},
"hasPart": {"color": "blue"},
"hasProperPart": {"color": "blue", "style": "dashed"},
"hasMember": {"color": "blue", "style": "dotted"},
"hasParticipant": {"color": "red"},
"hasProperParticipant": {"color": "red", "style": "dashed"},
"hasSpatialPart": {"color": "darkgreen"},
Expand Down Expand Up @@ -193,7 +194,7 @@ class OntoGraph: # pylint: disable=too-many-instance-attributes
- edges : attribute for individual edges (E)
If style is None or "default", the default style is used.
See https://www.graphviz.org/doc/info/attrs.html
edgelabels : bool | dict
edgelabels : None | bool | dict
Whether to add labels to the edges of the generated graph.
It is also possible to provide a dict mapping the
full labels (with cardinality stripped off for restrictions)
Expand Down Expand Up @@ -231,7 +232,7 @@ def __init__( # pylint: disable=too-many-arguments,too-many-locals
entities=None,
relations="isA",
style=None,
edgelabels=True,
edgelabels=None,
addnodes=False,
addconstructs=False,
included_namespaces=(),
Expand Down Expand Up @@ -315,7 +316,7 @@ def add_entities( # pylint: disable=too-many-arguments
self,
entities=None,
relations="isA",
edgelabels=True,
edgelabels=None,
addnodes=False,
addconstructs=False,
nodeattrs=None,
Expand Down Expand Up @@ -346,7 +347,7 @@ def add_branch( # pylint: disable=too-many-arguments,too-many-locals
strict_leafs=False,
exclude=None,
relations="isA",
edgelabels=True,
edgelabels=None,
addnodes=False,
addconstructs=False,
included_namespaces=(),
Expand Down Expand Up @@ -471,17 +472,27 @@ def add_edge(self, subject, predicate, obj, edgelabel=None, **attrs):
raise RuntimeError(f'`object` "{obj}" must have been added')
key = (subject, predicate, obj)
if key not in self.edges:

if edgelabel is None:
edgelabel = self.edgelabels

if isinstance(edgelabel, str):
label = None
if edgelabel is None:
tokens = predicate.split()
if len(tokens) == 2 and tokens[1] in ("some", "only"):
label = tokens[1]
elif len(tokens) == 3 and tokens[1] in (
"exactly",
"min",
"max",
):
label = f"{tokens[1]} {tokens[2]}"
elif isinstance(edgelabel, str):
label = edgelabel
if isinstance(edgelabel, dict):
elif isinstance(edgelabel, dict):
label = edgelabel.get(predicate, predicate)
elif edgelabel:
label = predicate
else:
label = None

kwargs = self.get_edge_attrs(predicate, attrs=attrs)
self.dot.edge(subject, obj, label=label, **kwargs)
Expand Down
13 changes: 8 additions & 5 deletions ontopy/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -1180,11 +1180,14 @@ def _number_of_generations(self, descendant, ancestor, counter):
distance between a ancestor-descendant pair (counter+1)."""
if descendant.name == ancestor.name:
return counter
return min(
self._number_of_generations(parent, ancestor, counter + 1)
for parent in descendant.get_parents()
if ancestor in parent.ancestors()
)
try:
return min(
self._number_of_generations(parent, ancestor, counter + 1)
for parent in descendant.get_parents()
if ancestor in parent.ancestors()
)
except ValueError:
return counter

def closest_common_ancestors(self, cls1, cls2):
"""Returns a list with closest_common_ancestor for cls1 and cls2"""
Expand Down

0 comments on commit 154dca8

Please sign in to comment.