Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When visualising restrictions, annotate the edges with the restriction type by default #339

Merged
merged 3 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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