Skip to content

Commit

Permalink
implemented conversion of legacy set definitions to RDF (in theory at…
Browse files Browse the repository at this point in the history
… least, untested still), issue proycon/folia#14
  • Loading branch information
proycon committed Oct 12, 2016
1 parent 94a8d9c commit 4f8fac0
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions formats/foliaset.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,16 @@ def json(self):
jsonnode['subclasses'].append(subclass.json())
return jsonnode

def rdf(self,graph, basens,parentset, parentclass=None):
graph.add((rdflib.term.URIRef(basens + '#' + self.id), rdflib.RDF.type, rdflib.term.URIRef(NSFOLIASETDEFINITION + '#Class')))
graph.add((rdflib.term.URIRef(basens + '#' + self.id), rdflib.term.URIRef(NSFOLIASETDEFINITION + '#id'), rdflib.term.Literal(self.id)))
graph.add((rdflib.term.URIRef(basens + '#' + self.id), rdflib.term.URIRef(NSFOLIASETDEFINITION + '#label'), rdflib.term.Literal(self.label)))
graph.add((rdflib.term.URIRef(basens + '#' + self.id), rdflib.term.URIRef(NSFOLIASETDEFINITION + '#memberOf'), rdflib.term.URIRef(basens + '#Set.' + self.id) ))
if parentclass:
graph.add((rdflib.term.URIRef(basens + '#' + self.id), rdflib.term.URIRef(NSFOLIASETDEFINITION + '#parentClass'), rdflib.term.URIRef(basens + '#parentClass.' + self.id) ))

for subclass in self.subclasses:
subclass.rdf(graph,basens,parentset, self.id)

class LegacySetDefinition(AbstractDefinition):
def __init__(self, id, type, classes = [], subsets = [], label =None):
Expand Down Expand Up @@ -142,11 +151,6 @@ def parsexml(Class, node):

return LegacySetDefinition(node.attrib['{http://www.w3.org/XML/1998/namespace}id'],type,classes, subsets, label)

def testclass(self,cls):
raise NotImplementedError #TODO, IMPLEMENT!

def testsubclass(self, cls, subset, subclass):
raise NotImplementedError #TODO, IMPLEMENT!

def json(self):
jsonnode = {'id': self.id}
Expand All @@ -168,29 +172,25 @@ def json(self):
jsonnode['classorder'].append( c.id )
return jsonnode

def rdf(self,graph,namespace, parent=None):
#TODO
graph.add((rdflib.URIRef(

if self.type == SetType.OPEN:

s = "<#" + self.id + ">\n"
props = []
def rdf(self,graph, basens=None,parent=None):
if not basens:
basens = "http://folia.science.ru.nl/setdefinitions/" + self.id
graph.add((rdflib.term.URIRef(basens + '#Set.' + self.id), rdflib.RDF.type, rdflib.term.URIRef(NSFOLIASETDEFINITION + '#Set')))
if self.id:
graph.add((rdflib.term.URIRef(basens + '#Set.' + self.id), rdflib.term.URIRef(NSFOLIASETDEFINITION + '#id'), rdflib.term.Literal(self.id)))
if self.type == SetType.OPEN:
s += "a fds:OpenSet"
elif self.type == SetType.CLOSED:
s += "a fds:ClosedSet"
elif self.type == SetType.MIXED:
s += "a fds:MixedSet"
graph.add((rdflib.term.URIRef(basens + '#Set.' + self.id), rdflib.term.URIRef(NSFOLIASETDEFINITION + '#open'), rdflib.term.Literal(True)))
if self.label:
s += " fds:label \"" + self.label.replace('"','\\"') + "\""
graph.add((rdflib.term.URIRef(basens + '#Set.' + self.id), rdflib.term.URIRef(NSFOLIASETDEFINITION + '#label'), rdflib.term.Literal(self.label)))
if self.parent:
s += " fds:subsetOf <#" + self.id + "> .\n"
graph.add((rdflib.term.URIRef(basens + '#Set.' + self.id), rdflib.term.URIRef(NSFOLIASETDEFINITION + '#subsetOf'), rdflib.term.URIRef(basens + '#Set.' + self.parent)))

for c in self.classes:
c.rdf(graph, basens, self.id)

for s in self.subsets:
s.rdf(graph, basens, self.id)

s += "\n"
for subset in self.subsets:
s += subset.turtle(self)
return s

def xmltreefromstring(s):
"""Internal function, deals with different Python versions, unicode strings versus bytes, and with the leak bug in lxml"""
Expand Down Expand Up @@ -253,8 +253,11 @@ def __init__(self, url, format=None):
self.graph = rdflib.Graph()
self.graph.parse(location=url, format=format)

def json(self):
#TODO: serialise to json
#for subj, rel, obj in self.graph:
def testclass(self,cls):
raise NotImplementedError #TODO, IMPLEMENT!

def testsubclass(self, cls, subset, subclass):
raise NotImplementedError #TODO, IMPLEMENT!

def json(self):
raise NotImplementedError #TODO, IMPLEMENT!

0 comments on commit 4f8fac0

Please sign in to comment.