forked from linkeddata/swap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcwm_math.py
executable file
·224 lines (167 loc) · 6.59 KB
/
cwm_math.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
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
#!/usr/bin/python
"""
Matematical Built-Ins for CWM/Llyn
Allows CWM to do addition, multiplication, subtraction, division,
remainders, negation, exponentiation, count the members in a DAML
list, and do the normal truth checking functions, only sub classed
for numeric values.
Note: see maths with an s for te string-oriented versions.
cf. http://www.w3.org/2000/10/swap/cwm.py and
http://ilrt.org/discovery/chatlogs/rdfig/2001-12-01.txt from
"01:20:58" onwards.
"""
__author__ = 'Sean B. Palmer'
__cvsid__ = '$Id$'
__version__ = '$Revision$'
import sys, string, re, urllib
import thing, notation3
from thing import *
LITERAL_URI_prefix = 'data:application/n3,'
DAML_LISTS = notation3.DAML_LISTS
RDF_type_URI = notation3.RDF_type_URI
DAML_equivalentTo_URI = notation3.DAML_equivalentTo_URI
MATH_NS_URI = 'http://www.w3.org/2000/10/swap/math#'
from diag import progress
def obsolete():
progress("Warning: Obsolete math built-in used.")
def tidy(x):
#DWC bugfix: "39.03555" got changed to "393555"
if x == None: return None
s = str(x)
if s[-2:] == '.0': s=s[:-2]
return s
def isString(x):
# in 2.2, evidently we can test for isinstance(types.StringTypes)
return type(x) is type('') or type(x) is type(u'')
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# M A T H E M A T I C A L B U I L T - I N s
#
# Some mathematical built-ins: the heaviest one gets the amount of list
# members in a DAML list.
#
# Thanks to deltab, bijan, and oierw for helping me to name the
# properties, and to TimBL for CWM and the built-in templates in the
# first place.
#
# Light Built-in classes - these are all reverse functions
# add, take, multiply, divide
class BI_absoluteValue(LightBuiltIn, Function):
def evaluateObject(self, subj_py):
return abs(float(subj_py))
class BI_rounded(LightBuiltIn, Function):
def evaluateObject(self, subj_py):
return round(float(subj_py))
class BI_sum(LightBuiltIn, Function):
def evaluateObject(self, subj_py):
t = 0
for x in subj_py: t += float(x)
return t
class BI_sumOf(LightBuiltIn, ReverseFunction):
def evaluateSubject(self, obj_py):
t = 0
obsolete()
for x in obj_py: t += float(x)
return t
class BI_difference(LightBuiltIn, Function):
def evaluateObject(self, subj_py):
if len(subj_py) == 2:
return float(subj_py[0]) - float(subj_py[1])
class BI_differenceOf(LightBuiltIn, ReverseFunction):
def evaluateSubject(self, obj_py):
obsolete()
if len(obj_py) == 2: return float(obj_py[0]) - float(obj_py[1])
class BI_product(LightBuiltIn, Function):
def evaluateObject(self, subj_py):
t = 1
for x in subj_py: t *= float(x)
return tidy(t)
class BI_factors(LightBuiltIn, ReverseFunction):
def evaluateSubject(self, obj_py):
obsolete()
t = 1
for x in obj_py: t *= float(x)
return t
class BI_quotient(LightBuiltIn, Function):
def evaluateObject(self, subj_py):
if len(subj_py) == 2: return float(subj_py[0]) / float(subj_py[1])
class BI_integerQuotient(LightBuiltIn, Function):
def evaluateObject(self, subj_py):
if len(subj_py) == 2: return int(subj_py[0]) / int(subj_py[1])
class BI_quotientOf(LightBuiltIn, ReverseFunction):
def evaluateSubject(self, obj_py):
obsolete()
if len(obj_py) == 2: return float(obj_py[0]) / float(obj_py[1])
# remainderOf and negationOf
class BI_remainder(LightBuiltIn, Function):
def evaluateObject(self, subj_py):
if len(subj_py) == 2: return float(subj_py[0]) % float(subj_py[1])
class BI_remainderOf(LightBuiltIn, ReverseFunction):
def evaluateSubject(self, obj_py):
obsolete()
if len(obj_py) == 2: return float(obj_py[0]) % float(obj_py[1])
class BI_negation(LightBuiltIn, Function, ReverseFunction):
def evaluateSubject(self, obj_py):
return -float(obj_py)
def evaluateObject(self, subj_py):
return -float(subj_py)
# Power
class BI_exponentiation(LightBuiltIn, Function):
def evaluateObject(self, subj_py):
if len(subj_py) == 2: return float(subj_py[0]) ** float(subj_py[1])
class BI_exponentiationOf(LightBuiltIn, ReverseFunction):
def evaluateSubject(self, obj_py):
obsolete()
if len(obj_py) == 2: return float(obj_py[0]) ** float(obj_py[1])
# Math greater than and less than etc., modified from cwm_string.py
# These are truth testing things - Binary logical operators
class BI_greaterThan(LightBuiltIn):
def evaluate(self, subject, object):
return (float(subject) > float(object))
class BI_notGreaterThan(LightBuiltIn):
def evaluate(self, subject, object):
return (float(subject) <= float(object))
class BI_lessThan(LightBuiltIn):
def evaluate(self, subject, object):
return (float(subject) < float(object))
class BI_notLessThan(LightBuiltIn):
def evaluate(self, subject, object):
return (float(subject) >= float(object))
class BI_equalTo(LightBuiltIn):
def evaluate(self, subject, object):
return (float(subject) == float(object))
class BI_notEqualTo(LightBuiltIn):
def evaluate(self, subject, object):
return (float(subject) != float(object))
# memberCount - this is a proper forward function
class BI_memberCount(LightBuiltIn, Function):
def evaluateObject(self, subj_py):
return len(subj_py)
# Register the string built-ins with the store
def register(store):
str = store.internURI(MATH_NS_URI[:-1])
str.internFrag('sum', BI_sum)
str.internFrag('difference', BI_difference)
str.internFrag('product', BI_product)
str.internFrag('quotient', BI_quotient)
str.internFrag('integerQuotient', BI_integerQuotient)
str.internFrag('remainder', BI_remainder)
str.internFrag('exponentiation', BI_exponentiation)
str.internFrag('sumOf', BI_sumOf)
str.internFrag('differenceOf', BI_differenceOf)
str.internFrag('factors', BI_factors)
str.internFrag('quotientOf', BI_quotientOf)
str.internFrag('remainderOf', BI_remainderOf)
str.internFrag('exponentiationOf', BI_exponentiationOf)
str.internFrag('negation', BI_negation)
str.internFrag('absoluteValue', BI_absoluteValue)
str.internFrag('rounded', BI_rounded)
str.internFrag('greaterThan', BI_greaterThan)
str.internFrag('notGreaterThan', BI_notGreaterThan)
str.internFrag('lessThan', BI_lessThan)
str.internFrag('notLessThan', BI_notLessThan)
str.internFrag('equalTo', BI_equalTo)
str.internFrag('notEqualTo', BI_notEqualTo)
str.internFrag('memberCount', BI_memberCount)
if __name__=="__main__":
print string.strip(__doc__)