status.py 20.5 KB
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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
"""A plugin to add nodes status.

Several status are defined by default: C{entry}, C{internal}, C{exit},
C{buffer}, C{safebuffer} for places and {tick} for transitions.

>>> import snakes.plugins
>>> snakes.plugins.load('status', 'snakes.nets', 'nets')
<module ...>
>>> from nets import *
>>> import snakes.plugins.status as status
>>> n = PetriNet('N')
>>> n.add_place(Place('p1'), status=status.entry)
>>> n.place('p1')
Place('p1', MultiSet([]), tAll, status=Status('entry'))
"""

import operator, weakref
import snakes.plugins
from snakes import ConstraintError
from snakes.plugins import new_instance
from snakes.compat import *
from snakes.data import iterate
from snakes.pnml import Tree

class Status (object) :
    "The status of a node"
    def __init__ (self, name, value=None) :
        """Initialize with a status name and an optional value

        @param name: the name of the status
        @type name: C{str}
        @param value: an optional additional value to make a
        difference between status with te same name
        @type value: hashable
        """
        self._name = name
        self._value = value
    __pnmltag__ = "status"
    def __pnmldump__ (self) :
        """Dump a C{Status} as a PNML tree

        @return: PNML tree
        @rtype: C{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)))
    @classmethod
    def __pnmlload__ (cls, tree) :
        """Create a C{Status} from a PNML tree

        @param tree: the tree to convert
        @type tree: C{pnml.Tree}
        @return: the status built
        @rtype: C{Status}

        >>> t = Status('foo', 42).__pnmldump__()
        >>> Status.__pnmlload__(t)
        Status('foo',42)
        """
        return cls(tree.child("name").data,
                   tree.child("value").child().to_obj())
    def copy (self) :
        """Return a copy of the status

        A status is normally never muted, so this may be useless,
        unless the user decides to store additional data in status.

        @return: a copy of the status
        @rtype: C{Status}
        """
        return self.__class__(self._name, self._value)
    def __str__ (self) :
        """Short textual representation

        >>> str(internal)
        'internal'
        >>> str(buffer('buf'))
        'buffer(buf)'

        @return: a textual representation
        @rtype: C{str}
        """
        if self._value is None :
            return str(self._name)
        else :
            return "%s(%s)" % (self._name, self._value)
    def __repr__ (self) :
        """Detailed textual representation

        >>> repr(internal)
        "Status('internal')"
        >>> repr(buffer('buf'))
        "Buffer('buffer','buf')"

        @return: a textual representation suitable for C{eval}
        @rtype: C{str}
        """
        if self._value is None :
            return "%s(%s)" % (self.__class__.__name__, repr(self._name))
        else :
            return "%s(%s,%s)" % (self.__class__.__name__,
                                  repr(self._name), repr(self._value))
    def __hash__ (self) :
        """Hash a status

        @return: the hash value
        @rtype: C{int}
        """
        return hash((self._name, self._value))
    def __eq__ (self, other) :
        """Compares two status for equality

        They are equal if they have the same name and value

        >>> internal == Status('internal')
        True
        >>> Status('a', 1) == Status('a', 2)
        False
        >>> Status('a', 1) == Status('b', 1)
        False

        @param other: a status
        @type other: C{Status}
        @return: C{True} is they are equal, C{False} otherwise
        @rtype: C{bool}
        """
        try :
            return (self._name, self._value) == (other._name, other._value)
        except :
            return False
    def __ne__ (self, other) :
        return not(self == other)
    def __add__ (self, other) :
        if self == other :
            return self.copy()
        else :
            raise ConstraintError("incompatible status")
    def name (self) :
        return self._name
    def value (self) :
        return self._value
    def merge (self, net, nodes, name=None) :
        """Merge C{nodes} in C{net} into a new node called C{name}

        This does nothing by default, other status will refine this
        method. Merged nodes are removed, only the newly created one
        remains.

        @param net: the Petri net where nodes should be merged
        @type net: C{PetriNet}
        @param nodes: a collection of node names to be merged
        @type nodes: iterable of C{str}
        @param name: the name of the new node or C{Node} if it should
          be generated
        @type name: C{str}
        """
        pass

entry = Status('entry')
exit = Status('exit')
internal = Status('internal')

class Buffer (Status) :
    "A status for buffer places"
    def merge (self, net, nodes, name=None) :
        """Merge C{nodes} in C{net}

        Buffer places with the status status C{Buffer('buffer', None)}
        are not merged. Other buffer places are merged exactly has
        C{PetriNet.merge_places} does.

        If C{name} is C{None} the name generated is a concatenation of
        the nodes names separated by '+', with parenthesis outside.

        >>> import snakes.plugins
        >>> snakes.plugins.load('status', 'snakes.nets', 'nets')
        <module ...>
        >>> from nets import *
        >>> n = PetriNet('N')
        >>> import snakes.plugins.status as status
        >>> buf = status.buffer('buf')
        >>> n.add_place(Place('p3', range(2), status=buf))
        >>> n.add_place(Place('p4', range(3), status=buf))
        >>> n.status.merge(buf, 'b')
        >>> p = n.place('b')
        >>> p
        Place('b', MultiSet([...]), tAll, status=Buffer('buffer','buf'))
        >>> p.tokens == MultiSet([0, 0, 1, 1, 2])
        True

        @param net: the Petri net where places should be merged
        @type net: C{PetriNet}
        @param nodes: a collection of place names to be merged
        @type nodes: iterable of C{str}
        @param name: the name of the new place or C{Node} if it should
          be generated
        @type name: C{str}
        """
        if self._value is None :
            return
        if name is None :
            name = "(%s)" % "+".join(sorted(nodes))
        net.merge_places(name, nodes, status=self)
        for src in nodes :
            net.remove_place(src)

def buffer (name) :
    """Generate a buffer status called C{name}

    @param name: the name of the buffer
    @type name: C{str}
    @return: C{Buffer('buffer', name)}
    @rtype: C{Buffer}
    """
    return Buffer('buffer', name)

class Safebuffer (Buffer) :
    "A status for safe buffers (ie, variables) places"
    def merge (self, net, nodes, name=None) :
        """Merge C{nodes} in C{net}

        Safe buffers places with the status C{Safebuffer('safebuffer',
        None)} are not merged. Other safe buffers places are merged if
        they all have the same marking, which becomes the marking of
        the resulting place. Otherwise, C{ConstraintError} is raised.

        If C{name} is C{None} the name generated is a concatenation of
        the nodes names separated by '+', with parenthesis outside.

        >>> import snakes.plugins
        >>> snakes.plugins.load('status', 'snakes.nets', 'nets')
        <module ...>
        >>> from nets import *
        >>> import snakes.plugins.status as status
        >>> n = PetriNet('N')
        >>> var = status.safebuffer('var')
        >>> n.add_place(Place('p5', [1], status=var))
        >>> n.add_place(Place('p6', [1], status=var))
        >>> n.add_place(Place('p7', [1], status=var))
        >>> n.status.merge(var, 'v')
        >>> n.place('v')
        Place('v', MultiSet([1]), tAll, status=Safebuffer('safebuffer','var'))
        >>> n.add_place(Place('p8', [3], status=var))
        >>> try : n.status.merge(var, 'vv')
        ... except ConstraintError : print(sys.exc_info()[1])
        incompatible markings

        @param net: the Petri net where places should be merged
        @type net: C{PetriNet}
        @param nodes: a collection of place names to be merged
        @type nodes: iterable of C{str}
        @param name: the name of the new place or C{Node} if it should
          be generated
        @type name: C{str}
        """
        if self._value is None :
            return
        marking = net.place(nodes[0]).tokens
        for node in nodes[1:] :
            if net.place(node).tokens != marking :
                raise ConstraintError("incompatible markings")
        if name is None :
            name = "(%s)" % "+".join(sorted(nodes))
        net.merge_places(name, nodes, status=self)
        for src in nodes :
            net.remove_place(src)
        net.set_status(name, self)
        net.place(name).reset(marking)

def safebuffer (name) :
    """Generate a safebuffer status called C{name}

    @param name: the name of the safebuffer
    @type name: C{str}
    @return: C{Safebuffer('safebuffer', name)}
    @rtype: C{Safebuffer}
    """
    return Safebuffer('safebuffer', name)

class Tick (Status) :
    "A status for tick transition"
    def merge (self, net, nodes, name=None) :
        """Merge C{nodes} in C{net}

        Tick transitions are merged exactly as
        C{PetriNet.merge_transitions} does.

        If C{name} is C{None} the name generated is a concatenation of
        the nodes names separated by '+', with parenthesis outside.

        >>> import snakes.plugins
        >>> snakes.plugins.load('status', 'snakes.nets', 'nets')
        <module ...>
        >>> from nets import *
        >>> import snakes.plugins.status as status
        >>> n = PetriNet('N')
        >>> tick = status.tick('tick')
        >>> n.add_transition(Transition('t1', Expression('x==1'), status=tick))
        >>> n.add_transition(Transition('t2', Expression('y==2'), status=tick))
        >>> n.add_transition(Transition('t3', Expression('z==3'), status=tick))
        >>> n.status.merge(tick, 't')
        >>> n.transition('t')
        Transition('t', Expression('((...) and (...)) and (...)'), status=Tick('tick','tick'))

        @param net: the Petri net where transitions should be merged
        @type net: C{PetriNet}
        @param nodes: a collection of transition names to be merged
        @type nodes: iterable of C{str}
        @param name: the name of the new transition or C{Node} if it
          should be generated
        @type name: C{str}
        """
        if self._value is None :
            return
        if name is None :
            name = "(%s)" % "+".join(nodes)
        net.merge_transitions(name, nodes, status=self)
        for src in nodes :
            net.remove_transition(src)

def tick (name) :
    """Generate a tick status called C{name}

    @param name: the name of the tick
    @type name: C{str}
    @return: C{Tick('tick', name)}
    @rtype: C{Tick}
    """
    return Tick('tick', name)

class StatusDict (object) :
    "A container to access the nodes of a net by their status"
    def __init__ (self, net) :
        """
        @param net: the Petri net for which nodes will be recorded
        @type net: C{PetriNet}
        """
        self._nodes = {}
        self._net = weakref.ref(net)
    def copy (self, net=None) :
        """
        @param net: the Petri net for which nodes will be recorded
          (C{None} if it is the same as the copied object)
        @type net: C{PetriNet}
        """
        if net is None :
            net = self._net()
        result = self.__class__(net)
        for status in self._nodes :
            result._nodes[status.copy()] = self._nodes[status].copy()
        return result
    def __iter__ (self) :
        return iter(self._nodes)
    def record (self, node) :
        """Called when C{node} is added to the net

        @param node: the added node
        @type node: C{Node}
        """
        if node.status not in self._nodes :
            self._nodes[node.status] = set([node.name])
        else :
            self._nodes[node.status].add(node.name)
    def remove (self, node) :
        """Called when C{node} is removed from the net

        @param node: the added node
        @type node: C{Node}
        """
        if node.status in self._nodes :
            self._nodes[node.status].discard(node.name)
            if len(self._nodes[node.status]) == 0 :
                del self._nodes[node.status]
    def __call__ (self, status) :
        """Return the nodes having C{status}

        @param status: the searched status
        @type status: C{Status}
        @return: the node names in the net having this status
        @rtype: C{tuple} of C{str}
        """
        return tuple(self._nodes.get(status, tuple()))
    def merge (self, status, name=None) :
        """Merge the nodes in the net having C{status}

        This is a shortcut to call C{status.merge} with the right
        parameters.

        @param status: the status for which nodes have to be merged
        """
        if status :
            nodes = self(status)
            if len(nodes) > 1 :
                status.merge(self._net(), nodes, name)

@snakes.plugins.plugin("snakes.nets")
def extend (module) :
    "Build the extended module"
    class Place (module.Place) :
        def __init__ (self, name, tokens=[], check=None, **args) :
            self.status = args.pop("status", Status(None))
            module.Place.__init__(self, name, tokens, check, **args)
        def copy (self, name=None, **args) :
            result = module.Place.copy(self, name, **args)
            result.status = self.status.copy()
            return result
        def __repr__ (self) :
            if self.status == Status(None) :
                return module.Place.__repr__(self)
            else :
                return "%s, status=%s)" % (module.Place.__repr__(self)[:-1],
                                           repr(self.status))
        def __pnmldump__ (self) :
            """
            >>> p = Place('p', status=Status('foo', 42))
            >>> p.__pnmldump__()
            <?xml version="1.0" encoding="utf-8"?>
            <pnml>...
             <place id="p">
              <type domain="universal"/>
              <initialMarking>
               <multiset/>
              </initialMarking>
              <status>
               <name>
                foo
               </name>
               <value>
                <object type="int">
                 42
                </object>
               </value>
              </status>
             </place>
            </pnml>
            """
            t = module.Place.__pnmldump__(self)
            t.add_child(Tree.from_obj(self.status))
            return t
        @classmethod
        def __pnmlload__ (cls, tree) :
            """
            >>> t = Place('p', status=Status('foo', 42)).__pnmldump__()
            >>> Place.__pnmlload__(t).status
            Status('foo',42)
            """
            result = new_instance(cls, module.Place.__pnmlload__(tree))
            try :
                result.status = tree.child("status").to_obj()
            except SnakesError :
                result.status = Status(None)
            return result
    class Transition (module.Transition) :
        def __init__ (self, name, guard=None, **args) :
            self.status = args.pop("status", Status(None)).copy()
            module.Transition.__init__(self, name, guard, **args)
        def __pnmldump__ (self) :
            """
            >>> p = Transition('p', status=Status('foo', 42))
            >>> p.__pnmldump__()
            <?xml version="1.0" encoding="utf-8"?>
            <pnml>...
             <transition id="p">
              <status>
               <name>
                foo
               </name>
               <value>
                <object type="int">
                 42
                </object>
               </value>
              </status>
             </transition>
            </pnml>
            """
            t = module.Transition.__pnmldump__(self)
            t.add_child(Tree.from_obj(self.status))
            return t
        @classmethod
        def __pnmlload__ (cls, tree) :
            """
            >>> t = Transition('p', status=Status('foo', 42)).__pnmldump__()
            >>> Transition.__pnmlload__(t).status
            Status('foo',42)
            """
            result = new_instance(cls, module.Transition.__pnmlload__(tree))
            try :
                result.status = tree.child("status").to_obj()
            except SnakesError :
                result.status = Status(None)
            return result
        def copy (self, name=None, **args) :
            result = module.Transition.copy(self, name, **args)
            result.status = self.status.copy()
            return result
        def __repr__ (self) :
            if self.status == Status(None) :
                return module.Transition.__repr__(self)
            else :
                return "%s, status=%s)" % (module.Transition.__repr__(self)[:-1],
                                           repr(self.status))
    class PetriNet (module.PetriNet) :
        def __init__ (self, name, **args) :
            module.PetriNet.__init__(self, name, **args)
            self.status = StatusDict(self)
        @classmethod
        def __pnmlload__ (cls, tree) :
            t = new_instance(cls, module.PetriNet.__pnmlload__(tree))
            t.status = StatusDict(t)
            return t
        def copy (self, name=None, **args) :
            result = module.PetriNet.copy(self, name, **args)
            result.status = self.status.copy(result)
            return result
        def add_place (self, place, **args) :
            place.status = args.pop("status", place.status)
            module.PetriNet.add_place(self, place, **args)
            self.status.record(place)
        def remove_place (self, name, **args) :
            place = self.place(name)
            self.status.remove(place)
            module.PetriNet.remove_place(self, name, **args)
        def add_transition (self, trans, **args) :
            trans.status = args.pop("status", trans.status)
            module.PetriNet.add_transition(self, trans, **args)
            self.status.record(trans)
        def remove_transition (self, name, **args) :
            trans = self.transition(name)
            self.status.remove(trans)
            module.PetriNet.remove_transition(self, name, **args)
        def set_status (self, node, status) :
            node = self.node(node)
            self.status.remove(node)
            node.status = status
            self.status.record(node)
        def rename_node (self, old, new, **args) :
            old_node = self.node(old).copy()
            module.PetriNet.rename_node(self, old, new, **args)
            self.status.remove(old_node)
            self.status.record(self.node(new))
        def copy_place (self, source, targets, **args) :
            status = args.pop("status", self.place(source).status)
            module.PetriNet.copy_place(self, source, targets, **args)
            for new in iterate(targets) :
                self.set_status(new, status)
        def copy_transition (self, source, targets, **args) :
            status = args.pop("status", self.transition(source).status)
            module.PetriNet.copy_transition(self, source, targets, **args)
            for new in iterate(targets) :
                self.set_status(new, status)
        def merge_places (self, target, sources, **args) :
            if "status" in args :
                status = args.pop("status")
            else :
                status = reduce(operator.add,
                                (self.place(s).status for s in sources))
            module.PetriNet.merge_places(self, target, sources, **args)
            self.set_status(target, status)
        def merge_transitions (self, target, sources, **args) :
            if "status" in args :
                status = args.pop("status")
            else :
                status = reduce(operator.add,
                                (self.place(s).status for s in sources))
            module.PetriNet.merge_transitions(self, target, sources, **args)
            self.set_status(target, status)
    return Place, Transition, PetriNet, Status, \
           ("entry", entry), ("exit", exit), ("internal", internal), \
           Buffer,  buffer, Safebuffer, safebuffer, Tick, tick