The Azimuth Project
delete 6 (changes)

Showing changes from revision #2 to #3: Added | Removed | Changed

This is a test.

category: delete

abc def ghi

\begin{verbatim} abc \end{verbatim}

this is code
    this is indented code

#!/usr/bin/python

print("hello petri net")   

import string
import random

class Transition:
    # this.name -- transitionName
    # this.inputs: stateName -> inputCount
    # this.outputs: stateName -> outputCount 

    def __init__(this, transitionName):
       this.transitionName = transitionName
       this.inputs = {} 
       this.outputs = {} 

    def IsEnabled(this, marking):
       for inputState in this.inputs.keys():
          if marking[inputState] < this.inputs[inputState]: 
              return False  # not enough tokens to fire

       return True # good to go 

    def Fire(this, marking):

       for inputName in this.inputs.keys():
          marking[inputName] = marking[inputName] - this.inputs[inputName] 

       for outputName in this.outputs.keys():
          marking[outputName] = marking[outputName] + this.outputs[outputName] 

class PetriNet:
    # this.transitionNames 
    # this.stateNames
    # this.transitions: transitionName -> TransitionObject
    # this.marking -- mapping (dict) from state name to count 

    def __init__(this, stateNames, transitionNames, inputMap, outputMap):
        this.stateNames = stateNames
        this.transitionNames = transitionNames 

        this.BuildTransitions(inputMap, outputMap)

    def FireOneRule(this):

        enabledTransitions = filter(
            lambda transition: transition.IsEnabled(this.marking),
            this.transitions.values())

        if len(enabledTransitions) == 0:
            return False 

        # pick a random enabled transition

        index = random.randrange(len(enabledTransitions))

        transition = enabledTransitions[index] 

        print transition.transitionName  

        transition.Fire(this.marking)

        return True 

    def RunSimulation(this, iterations, initialMarking): 

        this.PrintHeader()

        this.marking = initialMarking

        this.PrintMarking() 
        
        for i in range(iterations):
           if this.FireOneRule():
               this.PrintMarking(); 
           else:
               print "halt"
               break

        print "done"

    def BuildTransitions(this, inputSpecs, outputSpecs):

        this.transitions = {}

        for (transitionName, stateName, degree) in inputSpecs:
           this.GetTransition(transitionName).inputs[stateName] = degree 

        for (transitionName, stateName, degree) in outputSpecs:
           this.GetTransition(transitionName).outputs[stateName] = degree 
           
    def GetTransition(this, transitionName):
        if not(this.transitions.has_key(transitionName)):
            this.transitions[transitionName] = Transition(transitionName)

        return this.transitions[transitionName]

    def PrintHeader(this):
        print string.join(this.stateNames, ", ") + ", Transition"
           
    def PrintMarking(this):
        for stateName in this.stateNames:
            print str(this.marking[stateName]) + ",", 


net = PetriNet(
    ["H", "O", "H2O"],    # all states
    ["combine", "split"], # all transitions

    [("combine", "H", 2), ("combine", "O", 1), ("split", "H2O", 1)],
    [("combine", "H2O", 1), ("split", "H", 2), ("split", "O", 1)],
    #
    # i.e.,
    # combine has 2 H inputs, 1 O input, and 1 H2O output 
    # split has 1 H20 input, 2 H outputs, and 1 O output 
)


net.RunSimulation(10, {"H": 5, "O": 3, "H2O": 4})
print("bye petri net")