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

spielwiese
Last change on this file since 9122086 was 9122086, checked in by Michael Brickenstein <bricken@…>, 19 years ago
*bricken: refactoring git-svn-id: file:///usr/local/Singular/svn/trunk@8374 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 7.8 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 OMObjectBase(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                raise UnsupportedOperationError
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                raise UnsupportedOperationError
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            raise UnsupportedOperationError
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(OMObjectBase):
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)]
121       
122class OMApply(OMObjectBase):
123    def __init__(self, func, args):
124        super(OMApply,self).__init__()
125        self.func=func
126        self.args=args
127    def evaluate(self,context):
128        efunc=context.evaluate(self.func)
129        eargs=[context.evaluate(a) for a in self.args]
130        if callable(efunc):
131            try:
132                return context.apply(efunc, eargs)
133            except EvaluationFailedError, NotImplementedError:
134                return OMApply(efunc, eargs)
135                #return self
136        else:
137            return OMApply(efunc, eargs)
138    XMLtag="OMA"
139    def getChildren(self):
140        return [self.func]+self.args
141    def setChildren(self):
142        raise UnsupportedOperationError
143       
144class OMSymbol(OMObjectBase):
145    def __init__(self,name,cd=None):
146        super(OMSymbol,self).__init__()
147        self.cd=cd
148        self.name=name
149    def __eq__(self, other):
150        try:
151            return bool(other.name==self.name and self.cd==other.cd)
152        except:
153            return False
154    def __str__(self):
155        return "OMS("+self.name+", "+self.cd.name + ")"
156    def __hash__(self):#
157        return hash((self.name,self.cd.__hash__()))
158    def evaluate(self,context):
159        return context.evaluateSymbol(self)
160    XMLtag="OMS"
161    def getXMLAttributes(self):
162        return [XMLAttribute("name", self.name),\
163                 XMLAttribute("cdbase",self.cd.base),\
164                 XMLAttribute("cd",self.cd.name)]
165    def setXMLAttributes(self):
166        raise UnsupportedOperationError
167class SimpleValue(OMObjectBase):
168    def __init__(self,value):
169        super(SimpleValue,self).__init__()
170        if (isinstance(value, str)):
171            value=self.parse(value)
172        self.value=value
173    def evaluate(self, context):
174        return self
175    def getValue(self):
176        return self.value
177    def __str__(self):
178        return "OM("+repr(self.value)+")"
179
180class OMint(SimpleValue):
181    def __init__(self,value):
182        if not isinstance(value,int):
183            value=self.parse(value)
184        super(OMint,self).__init__(value)
185    def parse(self,value):
186        """FIXME: Not fully standard compliant,
187        -> hex encodings"""
188        return int(value,10)
189    def __str__(self):
190        return "OMint("+repr(self.value)+")"
191    def getBody(self):
192        return str(self.value)
193    def setBody(self, value):
194        raise UnsupportedOperationError
195    XMLtag="OMI"
196class OMfloat(SimpleValue):
197    def __init__(self,value):
198        super(OMfloat,self).__init__(value)
199    def parse(self, value):
200        """FIXME: Not fully standard compliant,
201        -> hex encodings"""
202        return float(value)
203    def __str__(self):
204        return "OMfloat("+repr(self.value)+")"
205    XMLtag="OMF"
206    def getXMLAttributes(self):
207        return [XMLAttribute("dec",str(self.value))]
208class OMRef(OMObjectBase):
209    def __init__(self, ref):
210        self.ref=ref
211    def evaluate(self, context):
212        return context.evaluate(self.ref)
213    def XMLencode(self, context):
214        "FIXME: maybe it should also be able to encode as reference"
215        return context.XMLEncodeObject(self.ref)
216if __name__=='__main__':
217    from context import *
218
219    from binding import *
220
221    context=Context()
222
223    context.push({})
224
225    context["x"]=OMint(1)
226
227    x=OMVar("x")
228
229    y=OMVar("y")
230
231    print context.evaluate(x)
232    print context.evaluate(y)
233    firstArg=OMBinding(lambdasym,[OMVar("x"), OMVar("y")], OMVar("x"))
234    #print context.evaluate(firstArg)
235    application=OMApply(firstArg, [x,y])
236    print context.evaluate(application)
237    application=OMApply(firstArg,[y,x])
238    print context.evaluate(application)
239    import arith1
240    context.addCDImplementation(arith1.implementation)
241    #print type(context.lookupImplementation(arith1.plussym))
242    #application=OMApply(arith1.plussym,[x])
243    #application=OMApply(arith1.plussym,[x,x])
244    application=OMApply(OMSymbol("plus",arith1.content),[x,x])
245   
246    print context.evaluate(application)
247    application=OMApply(OMSymbol("plus",arith1.content),[x,x,x])
248   
249    print context.evaluate(application)
250    i=OMint(22482489)
251    print i.body
252    print i.XMLencode(context)
253    #i.body="dshj"
254   
Note: See TracBrowser for help on using the repository browser.