source: git/modules/openmathserver/objects.py @ bfcf68

spielwiese
Last change on this file since bfcf68 was bfcf68, checked in by Michael Brickenstein <bricken@…>, 18 years ago
*bricken: can do this all day git-svn-id: file:///usr/local/Singular/svn/trunk@8362 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 7.4 KB
Line 
1from omexceptions import *
2from exceptions import *
3#from cd import *
4class XMLattribute(object):
5    def __init__(self, name, value):
6        self.name=name
7        self.value=value
8    def encode(self, context):
9        return "".join([self.name,"=\"",self.value,"\""])
10class OMobject(object):
11    """ at the moment only a base class"""
12    def __init__(self):
13        self.attributes={}
14    def __getChildren(self):
15        try:
16            return self.getChildren()
17        except AttributeError:
18            try:
19                return self.__children
20            except AttributeError:
21                return []
22    def __delChildren(self):
23        try:
24            self.delChildren()
25            return
26        except AttributeError:
27            try:
28                del self.__children
29            except AttributeError:
30                pass
31    def __setChildren(self,children):
32        try:
33            self.setChildren(children)
34        except AttributeError:
35                self.__children=children
36    def __getBody(self):
37        try:
38            return self.getBody()
39        except AttributeError:
40            try:
41                return self.__body
42            except AttributeError:
43                return None
44    def __delBody(self):
45        try:
46            self.delBody()
47            return
48        except AttributeError:
49            try:
50                del self.__body
51            except AttributeError:
52                pass
53    def __setBody(self,body):
54        try:
55            self.setBody(body)
56        except AttributeError:
57                self.__body=body
58    def __getXMLattributes(self):
59        try:
60            return self.getXMLattributes()
61        except AttributeError:
62            try:
63                return self.__XMLattributes
64            except AttributeError:
65                #do return None, cause if modifiying a new list, changes will not be saved
66                return []
67    def __delXMLattributes(self):
68        try:
69            self.delXMLattributes()
70            return
71        except AttributeError:
72            try:
73                del self.__XMLattributes
74            except AttributeError:
75                pass
76    def __setXMLattributes(self,XMLattributes):
77        try:
78            self.setBody(XMLattributes)
79        except AttributeError:
80                self.__XMLattributes=XMLattributes
81    children=property(__getChildren, __setChildren,__delChildren,\
82                      """ children in an OMtree""")
83    body=property(__getBody,__setBody,__delBody,\
84        "xml body,FIXME: at the moment only char data")
85    XMLattributes=property(__getXMLattributes,__setXMLattributes,__delXMLattributes,\
86        "xml attributes")
87    def XMLencode(self, context):
88       
89        attr=self.XMLattributes
90        if attr:
91            attrstr=" "+" ".join([a.encode(context) for a in attr])
92        else:
93            attrstr=""
94        opening="".join(["<", self.XMLtag, attrstr,">"])
95        children=self.children
96        if children:
97            body="".join([context.XMLEncodeObject(c) for c in children])
98        else:
99            body=self.body
100            if not body:
101                body=""
102            assert body!=None
103            body=context.XMLEncodeBody(body)
104            assert body!=None
105        closing="".join(["</"+self.XMLtag+">"])
106        return "".join([opening,body,closing])
107class OMvar(OMobject):
108    def __init__(self,name):
109        super(OMvar,self).__init__()
110        self.name=name
111    def evaluate(self,context):
112        try:
113            return context[self.name]
114        except OutOfScopeError:
115            return self
116    def __str__(self):
117        return "OMV(" + self.name +")"
118    XMLtag="OMV"
119    def getXMLattributes(self):
120        return [XMLattribute("name", self.name)]
121class OMapplication(OMobject):
122    def __init__(self, func, args):
123        super(OMapplication,self).__init__()
124        self.func=func
125        self.args=args
126    def evaluate(self,context):
127        efunc=context.evaluate(self.func)
128        eargs=[context.evaluate(a) for a in self.args]
129        if callable(efunc):
130            try:
131                return context.apply(efunc, eargs)
132            except EvaluationFailedError, NotImplementedError:
133                return OMapplication(efunc, eargs)
134                #return self
135        else:
136            return OMapplication(efunc, eargs)
137    XMLtag="OMA"
138    def getChildren(self):
139        return [self.func]+self.args
140    def setChildren(self):
141        raise UnsupportedOperationError
142class OMsymbol(OMobject):
143    def __init__(self,name,cd=None):
144        super(OMsymbol,self).__init__()
145        self.cd=cd
146        self.name=name
147    def __eq__(self, other):
148        return bool(other.name==self.name and self.cd==other.cd)
149    def __str__(self):
150        return "OMS("+self.name+", "+self.cd.name + ")"
151    def __hash__(self):#
152        return hash((self.name,self.cd.__hash__()))
153    def evaluate(self,context):
154        return context.evaluateSymbol(self)
155    XMLtag="OMS"
156    def getXMLattributes(self):
157        return [XMLattribute("name", self.name),\
158                 XMLattribute("cdbase",self.cd.base),\
159                 XMLattribute("cd",self.cd.name)]
160    def setXMLattributes(self):
161        raise UnsupportedOperationError
162class SimpleValue(OMobject):
163    def __init__(self,value):
164        super(SimpleValue,self).__init__()
165        if (isinstance(value, str)):
166            value=self.parse(value)
167        self.value=value
168    def evaluate(self, context):
169        return self
170    def getValue(self):
171        return self.value
172    def __str__(self):
173        return "OM("+repr(self.value)+")"
174
175class OMint(SimpleValue):
176    def __init__(self,value):
177        super(OMint,self).__init__(value)
178    def parse(self,value):
179        """FIXME: Not fully standard compliant,
180        -> hex encodings"""
181        return int(value,10)
182    def __str__(self):
183        return "OMint("+repr(self.value)+")"
184    def getBody(self):
185        return str(self.value)
186    def setBody(self, value):
187        raise UnsupportedOperationError
188    XMLtag="OMI"
189class OMfloat(SimpleValue):
190    def __init__(self,value):
191        super(OMfloat,self).__init__(value)
192    def parse(self, value):
193        """FIXME: Not fully standard compliant,
194        -> hex encodings"""
195        return float(value)
196    def __str__(self):
197        return "OMfloat("+repr(self.value)+")"
198    XMLtag="OMF"
199    def getXMLattributes(self):
200        return [XMLattribute("dec",str(self.value))]
201       
202if __name__=='__main__':
203    from context import *
204
205    from binding import *
206
207    context=Context()
208
209    context.push({})
210
211    context["x"]=OMint(1)
212
213    x=OMvar("x")
214
215    y=OMvar("y")
216
217    print context.evaluate(x)
218    print context.evaluate(y)
219    firstArg=OMbinding(lambdasym,[OMvar("x"), OMvar("y")], OMvar("x"))
220    #print context.evaluate(firstArg)
221    application=OMapplication(firstArg, [x,y])
222    print context.evaluate(application)
223    application=OMapplication(firstArg,[y,x])
224    print context.evaluate(application)
225    import arith1
226    context.addCDImplementation(arith1.implementation)
227    #print type(context.lookupImplementation(arith1.plussym))
228    #application=OMapplication(arith1.plussym,[x])
229    #application=OMapplication(arith1.plussym,[x,x])
230    application=OMapplication(OMsymbol("plus",arith1.content),[x,x])
231   
232    print context.evaluate(application)
233    application=OMapplication(OMsymbol("plus",arith1.content),[x,x,x])
234   
235    print context.evaluate(application)
236    i=OMint(22482489)
237    print i.body
238    print i.XMLencode(context)
239    #i.body="dshj"
240   
Note: See TracBrowser for help on using the repository browser.