-
Notifications
You must be signed in to change notification settings - Fork 12
/
cwm_set.py
196 lines (149 loc) · 5.72 KB
/
cwm_set.py
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
#! /usr/bin/env python
"""
$Id$
set built-ins for cwm
http://www.w3.org/2000/10/swap/cwm_list.py
See cwm.py and the os module in python
"""
from term import LightBuiltIn, Function, ReverseFunction, MultipleFunction,\
MultipleReverseFunction, \
CompoundTerm, N3Set, List, EmptyList, NonEmptyList
from set_importer import Set
from diag import verbosity, progress
import uripath
from RDFSink import List_NS, Logic_NS
SetOperationsNamespace = "http://www.w3.org/2000/10/swap/set#"
###############################################################################################
#
# List handling B U I L T - I N s
#
#
# Light Built-in classes
class BI_in(LightBuiltIn, MultipleReverseFunction):
"""Is the subject in the object?
Returnes a sequence of values."""
def eval(self, subj, obj, queue, bindings, proof, query):
if not isinstance(obj, CompoundTerm): return None
return subj in obj
def evalSubj(self, obj, queue, bindings, proof, query):
if not isinstance(obj, NonEmptyList) and not isinstance(obj, N3Set): return None
rea = None
return [x or x in obj] # [({subj:x}, rea) for x in obj]
class BI_member(LightBuiltIn, MultipleFunction):
"""Is the subject in the object?
Returnes a sequence of values."""
def eval(self, subj, obj, queue, bindings, proof, query):
if not isinstance(subj, CompoundTerm): return None
return obj in subj
def evalObj(self,subj, queue, bindings, proof, query):
if not isinstance(subj, NonEmptyList) and not isinstance(subj, N3Set): return None
rea = None
return subj # [({obj:x}, rea) for x in subj]
class BI_union(LightBuiltIn, Function):
"""Takes a set or list of sets, and finds the union
"""
def evaluateObject(self, subj):
ret = Set()
for m in subj:
ret.update(m)
return ret
class BI_intersection(LightBuiltIn, Function):
"""Takes a set or list of sets, and finds the intersection
"""
def evaluateObject(self, subj):
ret = None
for m in subj:
if ret is None:
ret = Set(m)
else:
ret.intersection_update(m)
if ret is None:
return Set()
return ret
class BI_symmetricDifference(LightBuiltIn, Function):
"""Takes a set or list of two sets, and finds the symmetric difference
"""
def evaluateObject(self, subj):
if len(subj) != 2:
raise ValueError('A symmetric difference of more than two things makes no sense')
ret = Set()
for m in subj:
ret.symmetric_difference_update(m)
return ret
class BI_difference(LightBuiltIn, Function):
"""Takes a list of two sets, and finds the difference
"""
def evaluateObject(self, subj):
if len(subj) != 2:
raise ValueError('A symmetric difference of more than two things makes no sense')
difference = N3Set.difference
return difference(subj[0], subj[1])
class BI_oneOf(LightBuiltIn, ReverseFunction):
""" Make a set from a list
"""
def evaluateSubject(self, obj):
return Set(obj)
#####################################
##
## Sets and Formulae --- set:in is the inverse of most of these
##
class BI_subjects(LightBuiltIn, Function):
"""Return the set of subjects used in a formula
"""
def evalObj(self, subj, queue, bindings, proof, query):
if not isinstance(subj, Formula):
raise ValueError('Only a formula has statements')
return N3Set([x.subject() for x in subj])
class BI_predicates(LightBuiltIn, Function):
"""Return the set of subjects used in a formula
"""
def evalObj(self, subj, queue, bindings, proof, query):
if not isinstance(subj, Formula):
raise ValueError('Only a formula has statements')
return N3Set([x.predicate() for x in subj])
class BI_objects(LightBuiltIn, Function):
"""Return the set of subjects used in a formula
"""
def evalObj(self, subj, queue, bindings, proof, query):
if not isinstance(subj, Formula):
raise ValueError('Only a formula has statements')
return N3Set([x.object() for x in subj])
class BI_triples(LightBuiltIn, Function):
"""Return the set of triple used in a formula
"""
def evalObj(self, subj, queue, bindings, proof, query):
if not isinstance(subj, Formula):
raise ValueError('Only a formula has statements')
return N3Set([x.asFormula() for x in subj])
##class BI_existentials(LightBuiltIn, Function):
## """Return the set of subjects used in a formula
##
## """
## def evalObj(self, subj, queue, bindings, proof, query):
## if not isinstance(subj, Formula):
## raise ValueError('Only a formula has statements')
## return N3Set(subj.existentials())
##
##class BI_universals(LightBuiltIn, Function):
## """Return the set of subjects used in a formula
##
## """
## def evalObj(self, subj, queue, bindings, proof, query):
## if not isinstance(subj, Formula):
## raise ValueError('Only a formula has statements')
## return N3Set(subj.universals())
# Register the string built-ins with the store
def register(store):
# Done explicitly in llyn
# list = store.internURI(List_NS[:-1])
# list.internFrag("first", BI_first)
# list.internFrag("rest", BI_rest)
ns = store.symbol(SetOperationsNamespace[:-1])
ns.internFrag("in", BI_in)
ns.internFrag("member", BI_member)
ns.internFrag("union", BI_union)
ns.internFrag("intersection", BI_intersection)
ns.internFrag("symmetricDifference", BI_symmetricDifference)
ns.internFrag("difference", BI_difference)
ns.internFrag("oneOf", BI_oneOf)
# ends