From aee306686be4b4b84a64fad4e0a831668a079c1f Mon Sep 17 00:00:00 2001 From: Sarath Menon Date: Wed, 20 Sep 2023 08:37:32 +0200 Subject: [PATCH] update network --- pyscal_rdf/network/network.py | 10 +++++----- pyscal_rdf/network/term.py | 21 ++++++++++++++++++++- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/pyscal_rdf/network/network.py b/pyscal_rdf/network/network.py index 0ff5faf..c00710d 100644 --- a/pyscal_rdf/network/network.py +++ b/pyscal_rdf/network/network.py @@ -21,9 +21,11 @@ def __init__(self, infile=None, delimiter='/'): self.g = nx.DiGraph() self.onto = OntoParser(infile, delimiter=delimiter) + self.onto.attributes['data_node'] = [] self.data_prefix = 'value' self._parse_all() + def _parse_all(self): #call methods self._add_class_nodes() @@ -87,6 +89,7 @@ def _add_data_properties(self): self.g.add_edge(d, val.name) for r in val.range: data_node = f'{val.name}{self.data_prefix}' + self.onto.attributes['data_node'].append(data_node) self.g.add_node(data_node, node_type='literal', data_type=r) self.g.add_edge(val.name, data_node) @@ -146,11 +149,8 @@ def draw(self, styledict = {"class": {"shape":"box"}, return dot def get_path_from_sample(self, target): - path = self.get_shortest_path(source="Sample", target=target) - triplets = [] - for x in range(len(path)//2): - triplets.append(path[2*x:2*x+3]) - return triplets + path = self.get_shortest_path(source="cmso:ComputationalSample", target=target, triples=True) + return path def phrase_to_sparql(self, phrase): def _extract_operation(phr): diff --git a/pyscal_rdf/network/term.py b/pyscal_rdf/network/term.py index 53e5515..005e67c 100644 --- a/pyscal_rdf/network/term.py +++ b/pyscal_rdf/network/term.py @@ -1,3 +1,4 @@ +from rdflib import Namespace class OntoTerm: def __init__(self, uri, node_type=None, @@ -38,6 +39,24 @@ def name(self): return ":".join(uri_split[-2:]) else: return self.uri - + + @property + def namespace(self): + uri_split = self.uri.split(self.delimiter) + if len(uri_split)>1: + return uri_split[-2] + else: + return self.uri + + @property + def namespace_object(self): + uri_split = self.uri.split(self.delimiter) + if len(uri_split)>1: + namespace = self.delimiter.join(uri_split[:-1]) + self.delimiter + prop = uri_split[-1] + return getattr(Namespace(namespace), prop) + else: + return self.uri + def __repr__(self): return str(self.name) \ No newline at end of file