comparison planemo/lib/python3.7/site-packages/rdflib/plugins/serializers/n3.py @ 1:56ad4e20f292 draft

"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author guerler
date Fri, 31 Jul 2020 00:32:28 -0400
parents
children
comparison
equal deleted inserted replaced
0:d30785e31577 1:56ad4e20f292
1 """
2 Notation 3 (N3) RDF graph serializer for RDFLib.
3 """
4 from rdflib.graph import Graph
5 from rdflib.namespace import Namespace, OWL
6 from rdflib.plugins.serializers.turtle import (
7 TurtleSerializer, SUBJECT, OBJECT)
8
9 __all__ = ['N3Serializer']
10
11 SWAP_LOG = Namespace("http://www.w3.org/2000/10/swap/log#")
12
13
14 class N3Serializer(TurtleSerializer):
15
16 short_name = "n3"
17
18 def __init__(self, store, parent=None):
19 super(N3Serializer, self).__init__(store)
20 self.keywords.update({
21 OWL.sameAs: '=',
22 SWAP_LOG.implies: '=>'
23 })
24 self.parent = parent
25
26 def reset(self):
27 super(N3Serializer, self).reset()
28 self._stores = {}
29
30 def subjectDone(self, subject):
31 super(N3Serializer, self).subjectDone(subject)
32 if self.parent:
33 self.parent.subjectDone(subject)
34
35 def isDone(self, subject):
36 return (super(N3Serializer, self).isDone(subject)
37 and (not self.parent or self.parent.isDone(subject)))
38
39 def startDocument(self):
40 super(N3Serializer, self).startDocument()
41 # if not isinstance(self.store, N3Store):
42 # return
43 #
44 # all_list = [self.label(var) for var in
45 # self.store.get_universals(recurse=False)]
46 # all_list.sort()
47 # some_list = [self.label(var) for var in
48 # self.store.get_existentials(recurse=False)]
49 # some_list.sort()
50 #
51 # for var in all_list:
52 # self.write('\n'+self.indent()+'@forAll %s. '%var)
53 # for var in some_list:
54 # self.write('\n'+self.indent()+'@forSome %s. '%var)
55 #
56 # if (len(all_list) + len(some_list)) > 0:
57 # self.write('\n')
58
59 def endDocument(self):
60 if not self.parent:
61 super(N3Serializer, self).endDocument()
62
63 def indent(self, modifier=0):
64 indent = super(N3Serializer, self).indent(modifier)
65 if self.parent is not None:
66 indent += self.parent.indent() # modifier)
67 return indent
68
69 def preprocessTriple(self, triple):
70 super(N3Serializer, self).preprocessTriple(triple)
71 if isinstance(triple[0], Graph):
72 for t in triple[0]:
73 self.preprocessTriple(t)
74 if isinstance(triple[2], Graph):
75 for t in triple[2]:
76 self.preprocessTriple(t)
77
78 def getQName(self, uri, gen_prefix=True):
79 qname = None
80 if self.parent is not None:
81 qname = self.parent.getQName(uri, gen_prefix)
82 if qname is None:
83 qname = super(N3Serializer, self).getQName(uri, gen_prefix)
84 return qname
85
86 def statement(self, subject):
87 self.subjectDone(subject)
88 properties = self.buildPredicateHash(subject)
89 if len(properties) == 0:
90 return False
91 return (self.s_clause(subject)
92 or super(N3Serializer, self).statement(subject))
93
94 def path(self, node, position, newline=False):
95 if not self.p_clause(node, position):
96 super(N3Serializer, self).path(node, position, newline)
97
98 def s_clause(self, subject):
99 if isinstance(subject, Graph):
100 self.write('\n' + self.indent())
101 self.p_clause(subject, SUBJECT)
102 self.predicateList(subject)
103 self.write(' .')
104 return True
105 else:
106 return False
107
108 def p_clause(self, node, position):
109 if isinstance(node, Graph):
110 self.subjectDone(node)
111 if position is OBJECT:
112 self.write(' ')
113 self.write('{')
114 self.depth += 1
115 serializer = N3Serializer(node, parent=self)
116 serializer.serialize(self.stream)
117 self.depth -= 1
118 self.write(self.indent() + '}')
119 return True
120 else:
121 return False