/usr/share/pyshared/swap/triple_maker.py is in python-swap 1.2.1-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | """Triple Maker
$Id: triple_maker.py,v 1.10 2005/01/21 20:54:04 syosi Exp $
Explanation of the API
the functions are addNode(),addNode(), addNode() endStatement() to add a triple
never call addNode() yourself
addSymbol() and addLiteral() are there for those
if a Node is a compound structure (like a formula) then call beginFormula(),
add all of the structure to the substructure, then all endFormula() and that
will call addNode() for you.
For your convinience, if you call IsOf() before adding the predicate, it will
reverse the subject and object in the final triple
Also for your convience, you CAN call addNode() with None as the node,
and it will just leave that as the previous node in that position.
Note that calling addNode() with None as the first triple in a formula or
bNode is an error, and will be flagged when you try to endStatement()
"""
import diag # problems importing the tracking flag, and chatty_flag must be explicit it seems diag.tracking
from diag import progress, verbosity
from term import BuiltIn, LightBuiltIn, \
HeavyBuiltIn, Function, ReverseFunction, \
Literal, Symbol, Fragment, FragmentNil, Term,\
CompoundTerm, List, EmptyList, NonEmptyList, AnonymousNode
import RDFSink
N3_forSome_URI = RDFSink.forSomeSym
N3_forAll_URI = RDFSink.forAllSym
NOTHING = -1
SUBJECT = 0
PREDICATE = 1
OBJECT = 2
FORMULA = 0
LIST = 1
ANONYMOUS = 2
NO = 0
STALE = 1
FRESH = 2
def swap(List, a, b):
q = List[a]
List[a] = List[b]
List[b] = q
class TripleMaker:
"""This is the example of the new interface.
It will convert from the abstract interface into formulas, lists
and triples
"""
def __init__(self, formula=None, store=None):
if formula is None:
formula = store.newFormula
if store is None:
store = formula.store
self.formulas = [formula]
self.store = store
self.forSome = formula.newSymbol(N3_forSome_URI)
self.forAll = formula.newSymbol(N3_forAll_URI)
def start(self):
self._parts = [NOTHING]
self._triples = [[None, None, None]]
self.lists = []
self._modes = [FORMULA]
self.bNodes = []
self.addedBNodes = [{}]
self._predIsOfs = [NO]
self._pathModes = [False]
self.store.startDoc()
def end(self):
assert len(self.formulas) == 1
assert len(self.lists) == 0
self.store.endDoc(self.formulas[0])
return self.formulas[0]
def addNode(self, node):
if self._modes[-1] == ANONYMOUS and node is not None and self._parts[-1] == NOTHING:
raise ValueError('You put a dot in a bNode')
if self._modes[-1] == FORMULA or self._modes[-1] == ANONYMOUS:
self._parts[-1] = self._parts[-1] + 1
if self._parts[-1] > 3:
raise ValueError('Try ending the statement')
if node is not None:
#print node, '+', self._triples, '++', self._parts
self._triples[-1][self._parts[-1]] = node
if self._parts[-1] == PREDICATE and self._predIsOfs[-1] == STALE:
self._predIsOfs[-1] = NO
if self._modes[-1] == ANONYMOUS and self._pathModes[-1] == True:
self.endStatement()
self.endAnonymous()
elif self._modes[-1] == LIST:
self.lists[-1].append(node)
def IsOf(self):
self._predIsOfs[-1] = FRESH
def checkIsOf(self):
return self._predIsOfs[-1]
def forewardPath(self):
if self._modes[-1] == LIST:
a = self.lists[-1].pop()
else:
a = self._triples[-1][self._parts[-1]]
self._parts[-1] = self._parts[-1] - 1
self.beginAnonymous()
self.addNode(a)
self._predIsOfs[-1] = FRESH
self._pathModes[-1] = True
def backwardPath(self):
if self._modes[-1] == LIST:
a = self.lists[-1].pop()
else:
a = self._triples[-1][self._parts[-1]]
self._parts[-1] = self._parts[-1] - 1
self.beginAnonymous()
self.addNode(a)
self._pathModes[-1] = True
def endStatement(self):
if self._parts[-1] == SUBJECT:
pass
else:
if self._parts[-1] != OBJECT:
raise ValueError('try adding more to the statement' + `self._triples`)
formula = self.formulas[-1]
if self._pathModes[-1]:
swap(self._triples[-1], PREDICATE, OBJECT)
if self._predIsOfs[-1]:
swap(self._triples[-1], SUBJECT, OBJECT)
self._predIsOfs[-1] = STALE
subj, pred, obj = self._triples[-1]
if subj == '@this':
if pred == self.forSome:
formula.declareExistential(obj)
elif pred == self.forAll:
formula.declareUniversal(obj)
else:
raise ValueError("This is useless!")
else:
#print 'I got here!!!!!!!!!!!!!!!!!!!!!!!!!!!'
formula.add(subj, pred, obj)
if self._predIsOfs[-1]:
swap(self._triples[-1], SUBJECT, OBJECT)
self._parts[-1] = NOTHING
if self._modes[-1] == ANONYMOUS and self._pathModes[-1]:
self._parts[-1] = SUBJECT
def addLiteral(self, lit, dt=None, lang=None):
if dt:
if dt[:2] == '_:':
if dt not in self.addedBNodes[-1]:
a = self.formulas[-1].newBlankNode()
self.addedBNodes[-1][dt] = a
dt = a
else:
dt = self.addedBNodes[-1][dt]
else:
dt = self.store.newSymbol(dt)
a = self.store.intern(lit, dt, lang)
self.addNode(a)
def addSymbol(self, sym):
a = self.store.newSymbol(sym)
self.addNode(a)
def beginFormula(self):
a = self.store.newFormula()
self.formulas.append(a)
self.addedBNodes.append({})
self._modes.append(FORMULA)
self._triples.append([None, None, None])
self._parts.append(NOTHING)
self._predIsOfs.append(NO)
self._pathModes.append(False)
def endFormula(self):
if self._parts[-1] != NOTHING:
self.endStatement()
a = self.formulas.pop().close()
self.addedBNodes.pop()
self._modes.pop()
self._triples.pop()
self._parts.pop()
self.addNode(a)
self._predIsOfs.pop()
self._pathModes.pop()
def beginList(self):
a = []
self.lists.append(a)
self._modes.append(LIST)
self._parts.append(NOTHING)
def endList(self):
a = self.store.newList(self.lists.pop())
self._modes.pop()
self._parts.pop()
self.addNode(a)
def addAnonymous(self, Id):
"""If an anonymous shows up more than once, this is the
function to call
"""
if Id not in self.addedBNodes[-1]:
a = self.formulas[-1].newBlankNode()
self.addedBNodes[-1][Id] = a
else:
a = self.addedBNodes[-1][Id]
self.addNode(a)
def beginAnonymous(self):
a = self.formulas[-1].newBlankNode()
self.bNodes.append(a)
self._modes.append(ANONYMOUS)
self._triples.append([a, None, None])
self._parts.append(SUBJECT)
self._predIsOfs.append(NO)
self._pathModes.append(False)
def endAnonymous(self):
if self._parts[-1] != NOTHING:
self.endStatement()
a = self.bNodes.pop()
self._modes.pop()
self._triples.pop()
self._parts.pop()
self._predIsOfs.pop()
self._pathModes.pop()
self.addNode(a)
def declareExistential(self, sym):
formula = self.formulas[-1]
a = formula.newSymbol(sym)
formula.declareExistential(a)
def declareUniversal(self, sym):
formula = self.formulas[-1]
a = formula.newSymbol(sym)
formula.declareUniversal(a)
def addQuestionMarkedSymbol(self, sym):
formula = self.formulas[-2]
a = formula.newSymbol(sym)
formula.declareUniversal(a)
self.addNode(a)
def bind(self, prefix, uri):
if prefix == "":
self.store.setDefaultNamespace(uri)
else:
self.store.bind(prefix, uri)
|