-
Notifications
You must be signed in to change notification settings - Fork 12
/
cwm_math.py
executable file
·251 lines (193 loc) · 7.78 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
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
#!/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
from term import LightBuiltIn, Function, ReverseFunction, ArgumentNotLiteral, Literal
from local_decimal import Decimal
import types
# from RDFSink import DAML_LISTS, RDF_type_URI, DAML_sameAs_URI
MATH_NS_URI = 'http://www.w3.org/2000/10/swap/math#'
from diag import progress
import sys, traceback
def obsolete():
progress("Warning: Obsolete math built-in used.")
traceback.print_stack()
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
def numeric(s):
if type(s) == types.IntType or \
type(s) == types.LongType or \
type(s) is types.FloatType or \
isinstance(s,Decimal): return s
if not isinstance(s, (Literal, str, unicode)):
raise ArgumentNotLiteral(s)
s = s.strip() # 2009 in practice e.g. OFX values have leading spaces
if s.find('.') < 0 and s.find('e') < 0 : return long(s)
if 'e' not in s and 'E' not in s: return Decimal(s)
return float(s)
class BI_absoluteValue(LightBuiltIn, Function):
def evaluateObject(self, subj_py):
return abs(numeric(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 += numeric(x)
return t
class BI_sumOf(LightBuiltIn, ReverseFunction):
def evaluateSubject(self, obj_py):
t = 0
obsolete()
for x in obj_py: t += numeric(x)
return t
class BI_difference(LightBuiltIn, Function):
def evaluateObject(self, subj_py):
if len(subj_py) == 2:
return numeric(subj_py[0]) - numeric(subj_py[1])
class BI_differenceOf(LightBuiltIn, ReverseFunction):
def evaluateSubject(self, obj_py):
obsolete()
if len(obj_py) == 2: return numeric(obj_py[0]) - numeric(obj_py[1])
class BI_product(LightBuiltIn, Function):
def evaluateObject(self, subj_py):
t = 1
for x in subj_py: t *= numeric(x)
return t
class BI_factors(LightBuiltIn, ReverseFunction):
def evaluateSubject(self, obj_py):
obsolete()
t = 1
for x in obj_py: t *= numeric(x)
return t
class BI_quotient(LightBuiltIn, Function):
def evaluateObject(self, subj_py):
if len(subj_py) == 2:
if isinstance(numeric(subj_py[0]), long):
return numeric(subj_py[1]).__rtruediv__(numeric(subj_py[0]))
return numeric(subj_py[0]).__truediv__(numeric(subj_py[1]))
class BI_integerQuotient(LightBuiltIn, Function):
def evaluateObject(self, subj_py):
if len(subj_py) == 2: return long(subj_py[0]) / long(subj_py[1])
class BI_bit(LightBuiltIn, Function):
"""@@needs a test."""
def evaluateObject(self, subj_py):
if len(subj_py) == 2:
x = subj_py[0]
b = subj_py[1]
return (x >> b) & 1
class BI_quotientOf(LightBuiltIn, ReverseFunction):
def evaluateSubject(self, obj_py):
obsolete()
if len(obj_py) == 2: return numeric(obj_py[0]).__truediv__(numeric(obj_py[1]))
# remainderOf and negationOf
class BI_remainder(LightBuiltIn, Function):
def evaluateObject(self, subj_py):
if len(subj_py) == 2: return numeric(subj_py[0]) % numeric(subj_py[1])
class BI_remainderOf(LightBuiltIn, ReverseFunction):
def evaluateSubject(self, obj_py):
obsolete()
if len(obj_py) == 2: return numeric(obj_py[0]) % numeric(obj_py[1])
class BI_negation(LightBuiltIn, Function, ReverseFunction):
def evaluateSubject(self, obj_py):
return -numeric(obj_py)
def evaluateObject(self, subj_py):
return -numeric(subj_py)
# Power
class BI_exponentiation(LightBuiltIn, Function):
def evaluateObject(self, subj_py):
if len(subj_py) == 2: return numeric(subj_py[0]) ** numeric(subj_py[1])
class BI_exponentiationOf(LightBuiltIn, ReverseFunction):
def evaluateSubject(self, obj_py):
obsolete()
if len(obj_py) == 2: return numeric(obj_py[0]) ** numeric(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):
try:
return (float(subject) != float(object))
except (ValueError, AttributeError):
return None # AttributeError: Symbol instance has no attribute '__float__'
# or: ValueError: invalid literal for float(): PT1H
# 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.symbol(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('bit', BI_bit)
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__)