def __pnmldump__(self): """ >>> Cluster(['a', 'b'], ... [Cluster(['1', '2'], ... [Cluster(['A'])]), ... Cluster(['3', '4', '5'], ... [Cluster(['C', 'D'])])]).__pnmldump__() <?xml version="1.0" encoding="utf-8"?> <pnml>... <clusters> <node> ... </node> <node> ... </node> <clusters> ... </clusters> </clusters> </pnml> """ result = Tree(self.__pnmltag__, None) for node in self._nodes: result.add_child(Tree("node", node)) for child in self._children: result.add_child(Tree.from_obj(child)) return result
def __pnmldump__(self): """ >>> p = Place('p', pos=(1, 2)) >>> p.__pnmldump__() <?xml version="1.0" encoding="utf-8"?> <pnml> <place id="p"> <type domain="universal"/> <initialMarking> <multiset/> </initialMarking> <graphics> <position x="1" y="2"/> </graphics> </place> </pnml> """ t = module.Place.__pnmldump__(self) try: gfx = t.child("graphics") except SnakesError: gfx = Tree("graphics", None) t.add_child(gfx) gfx.add_child( Tree("position", None, x=str(self.pos.x), y=str(self.pos.y))) return t
def __pnmldump__(self): t = module.Transition.__pnmldump__(self) t.add_child(Tree("min_time", None, Tree.from_obj(self.min_time))) if self.max_time is not None: t.add_child( Tree("max_time", None, Tree.from_obj(self.max_time))) return t
def __pnmldump__(self): """ >>> MultiSet([1, 2, 3, 4, 1, 2]).__pnmldump__() <?xml version="1.0" encoding="utf-8"?> <pnml> <multiset> <item> <value> <object type="int"> ... </object> </value> <multiplicity> ... </multiplicity> </item> <item> <value> <object type="int"> ... </object> </value> <multiplicity> ... </multiplicity> </item> <item> <value> <object type="int"> ... </object> </value> <multiplicity> ... </multiplicity> </item> <item> <value> <object type="int"> ... </object> </value> <multiplicity> ... </multiplicity> </item> </multiset> </pnml> """ nodes = [] for value in hdict.__iter__(self): nodes.append( Tree("item", None, Tree("value", None, Tree.from_obj(value)), Tree("multiplicity", str(self[value])))) return Tree(self.__pnmltag__, None, *nodes)
def __pnmldump__(self): """ >>> Symbol('egg', 'spam').__pnmldump__() <?xml version="1.0" encoding="utf-8"?> <pnml> <symbol name="egg"> <object type="str"> spam </object> </symbol> </pnml> >>> Symbol('foo').__pnmldump__() <?xml version="1.0" encoding="utf-8"?> <pnml> <symbol name="foo"/> </pnml> >>> Symbol('bar', False).__pnmldump__() <?xml version="1.0" encoding="utf-8"?> <pnml> <symbol name="bar"> <object type="bool"> False </object> </symbol> </pnml> """ if self.name == self._export: children = [] else: children = [Tree.from_obj(self._export)] return Tree(self.__pnmltag__, None, *children, **dict(name=self.name))
def __pnmldump__(self): """ >>> t = Transition('t') >>> t.label(foo='bar', spam=42) >>> t.__pnmldump__() <?xml version="1.0" encoding="utf-8"?> <pnml> <transition id="t"> <label name="foo"> <object type="str"> bar </object> </label> <label name="spam"> <object type="int"> 42 </object> </label> </transition> </pnml> """ t = module.Transition.__pnmldump__(self) if hasattr(self, "_labels"): for key, val in self._labels.items(): t.add_child( Tree("label", None, Tree.from_obj(val), name=key)) return t
def __pnmldump__(self): """ >>> p = Place('p') >>> p.label(foo='bar', spam=42) >>> p.__pnmldump__() <?xml version="1.0" encoding="utf-8"?> <pnml> <place id="p"> <type domain="universal"/> <initialMarking> <multiset/> </initialMarking> <label name="foo"> <object type="str"> bar </object> </label> <label name="spam"> <object type="int"> 42 </object> </label> </place> </pnml> """ t = module.Place.__pnmldump__(self) if hasattr(self, "_labels"): for key, val in self._labels.items(): t.add_child( Tree("label", None, Tree.from_obj(val), name=key)) return t
def __pnmldump__ (self) : """ >>> t = Transition('t', pos=(2, 1)) >>> t.__pnmldump__() <?xml version="1.0" encoding="utf-8"?> <pnml> <transition id="t"> <graphics> <position x="2" y="1"/> </graphics> </transition> </pnml> """ t = module.Transition.__pnmldump__(self) t.add_child(Tree("graphics", None, Tree("position", None, x=str(self.pos.x), y=str(self.pos.y)))) return t
def __pnmldump__ (self) : """Dumps a substitution to a PNML tree >>> Substitution(x=1, y=2).__pnmldump__() <?xml version="1.0" encoding="utf-8"?> <pnml> <substitution> <item> <name> ... </name> <value> <object type="int"> ... </object> </value> </item> <item> <name> ... </name> <value> <object type="int"> ... </object> </value> </item> </substitution> </pnml> @return: PNML representation @rtype: `snakes.pnml.Tree` """ nodes = [] for name, value in self._dict.items() : nodes.append(Tree("item", None, Tree("name", name), Tree("value", None, Tree.from_obj(value)))) return Tree(self.__pnmltag__, None, *nodes)
def __pnmldump__(self): """Dump a `Status` as a PNML tree @return: PNML tree @rtype: `pnml.Tree` >>> Status('foo', 42).__pnmldump__() <?xml version="1.0" encoding="utf-8"?> <pnml>... <status> <name> foo </name> <value> <object type="int"> 42 </object> </value> </status> </pnml> """ return Tree(self.__pnmltag__, None, Tree("name", self._name), Tree("value", None, Tree.from_obj(self._value)))
def run(self): while True: data, address = self.recvfrom() data = data.strip() if self._verbose: print("# query from %s:%u" % address) try: if self._verbose > 1: print(data) res = loads(data).run(self._env) if res is None: res = Tree("answer", None, status="ok") else: res = Tree("answer", None, Tree.from_obj(res), status="ok") except: cls, val, tb = sys.exc_info() res = Tree("answer", str(val).strip(), error=cls.__name__, status="error") if self._verbose > 1: print("# error") for entry in traceback.format_exception(cls, val, tb): for line in entry.splitlines(): print("## %s" % line) if self._verbose: if self._verbose > 1: print("# answer") print(res.to_pnml()) elif res["status"] == "error": print("# answer: %s: %s" % (res["error"], res.data)) else: print("# answer: %s" % res["status"]) self.sendto(res.to_pnml(), address)
def __pnmldump__(self): """ >>> Action('a', True, [Value(1), Variable('x')]).__pnmldump__() <?xml version="1.0" encoding="utf-8"?> <pnml>... <action name="a" send="True"> <value> <object type="int">1</object> </value> <variable>x</variable> </action> </pnml> """ result = Tree( self.__pnmltag__, None, name=self.name, send=str(self.send)) for param in self.params: result.add_child(Tree.from_obj(param)) return result
def __pnmldump__(self): """ >>> MultiAction([Action('a', True, [Variable('x')]), ... Action('b', False, [Variable('y'), Value(2)]) ... ]).__pnmldump__() <?xml version="1.0" encoding="utf-8"?> <pnml>... <multiaction> <action name="a" send="True"> <variable>x</variable> </action> <action name="b" send="False"> <variable>y</variable> <value> <object type="int">2</object> </value> </action> </multiaction> </pnml> """ return Tree(self.__pnmltag__, None, *(Tree.from_obj(action) for action in self._actions))
def __pnmldump__(self): """ >>> Query('set', 'x', 42).__pnmldump__() <?xml version="1.0" encoding="utf-8"?> <pnml> <query name="set"> <argument> <object type="str"> x </object> </argument> <argument> <object type="int"> 42 </object> </argument> </query> </pnml> >>> Query('test', x=1).__pnmldump__() <?xml version="1.0" encoding="utf-8"?> <pnml> <query name="test"> <keyword name="x"> <object type="int"> 1 </object> </keyword> </query> </pnml> >>> Query('test', 'x', 42, y=1).__pnmldump__() <?xml version="1.0" encoding="utf-8"?> <pnml> <query name="test"> <argument> <object type="str"> x </object> </argument> <argument> <object type="int"> 42 </object> </argument> <keyword name="y"> <object type="int"> 1 </object> </keyword> </query> </pnml> >>> Query('set', 'x', Query('call', 'x.upper')).__pnmldump__() <?xml version="1.0" encoding="utf-8"?> <pnml> <query name="set"> <argument> <object type="str"> x </object> </argument> <argument> <query name="call"> <argument> <object type="str"> x.upper </object> </argument> </query> </argument> </query> </pnml> """ children = [] for arg in self._larg: children.append(Tree("argument", None, Tree.from_obj(arg))) for name, value in self._karg.items(): children.append( Tree("keyword", None, Tree.from_obj(value), name=name)) return Tree(self.__pnmltag__, None, *children, **{"name": self._name})