Skip to content

Commit

Permalink
Merge pull request #2 from versae/master
Browse files Browse the repository at this point in the history
Adding a filter option when dumping or saving an ontology and its individuals
  • Loading branch information
pwin authored Dec 21, 2019
2 parents 7ed94a6 + cb0ce55 commit b56fac6
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def context_2_user_context(self, context): raise NotImplementedError

def parse(self, f): raise NotImplementedError

def save(self, f, format = "pretty-xml"): raise NotImplementedError
def save(self, f, format = "pretty-xml", filter=None): raise NotImplementedError

def _abbreviate (self, iri, create_if_missing = True): return iri
def _unabbreviate(self, iri): return iri
Expand Down Expand Up @@ -238,11 +238,13 @@ def _guess_format(f):
return "rdfxml"


def _save(f, format, graph):
def _save(f, format, graph, filter=None):
if format == "ntriples":
_unabbreviate = lru_cache(None)(graph._unabbreviate)

for s,p,o,d in graph._iter_triples():
if filter and callable(filter) and not filter(graph, s, p, o, d):
continue
if s < 0: s = "_:%s" % (-s)
else: s = "<%s>" % _unabbreviate(s)
p = "<%s>" % _unabbreviate(p)
Expand All @@ -263,6 +265,8 @@ def _save(f, format, graph):
c_2_iri = { c : iri for c, iri in graph._iter_ontology_iri() }

for c,s,p,o,d in graph._iter_triples(True):
if filter and callable(filter) and not filter(graph, s, p, o, d, c):
continue
if s < 0: s = "_:%s" % (-s)
else: s = "<%s>" % _unabbreviate(s)
p = "<%s>" % _unabbreviate(p)
Expand Down Expand Up @@ -423,6 +427,8 @@ def purge():
s_lines = []
current_s = ""
for s,p,o,d in graph._iter_triples(False, True):
if filter and callable(filter) and not filter(graph, s, p, o, d):
continue
if s != current_s:
if current_s: purge()
current_s = s
Expand Down Expand Up @@ -518,3 +524,4 @@ def flatten(l, deep = ""):


f.write(b"""\n\n</rdf:RDF>\n""")

0 comments on commit b56fac6

Please sign in to comment.