def setUp(self): '''set up ''' # new the to unit test class object self.tclass = petri_net_reachability_analyse.PetriNetReachabilityAnalyser( ) # p1->arc1->t1->arc2->p2 p1 = petri_net_element.Place("p1", "p1", [], ["arc1"]) arc1 = petri_net_element.Arc("arc1", "arc1", "p1", "t1") t1 = petri_net_element.Transition("t1", "t1", ["arc1"], ["arc2"]) arc2 = petri_net_element.Arc("arc2", "arc2", "t1", "p2") p2 = petri_net_element.Place("p2", "p2", ["arc2"], []) places_dict = {} transitions_dict = {} arcs_dict = {} places_dict["p1"] = p1 places_dict["p2"] = p2 transitions_dict["t1"] = t1 arcs_dict["arc1"] = arc1 arcs_dict["arc2"] = arc2 petri_net = petri_net_element.PetriNet(places_dict, transitions_dict, arcs_dict, "petri_net") self.petri_net = petri_net
def setUp(self): '''setup ''' # new the to unit test class object self.tclass = get_reachability_graph.PetriToReachabilityGraph() # place1->arc1->transition1->arc2->place2 place1 = petri_net_element.Place("place1", "place1", [], ["arc1"]) arc1 = petri_net_element.Arc("arc1", "arc1", "place1", "transition1") transition1 = petri_net_element.Transition("transition1", "transition1", ["arc1"], ["arc2"]) arc2 = petri_net_element.Arc("arc2", "arc2", "transition1", "place2") place2 = petri_net_element.Place("place2", "place2", ["arc2"], []) places_dict = {} transitions_dict = {} arcs_dict = {} places_dict["place1"] = place1 places_dict["place2"] = place2 transitions_dict["transition1"] = transition1 arcs_dict["arc1"] = arc1 arcs_dict["arc2"] = arc2 petri_net = petri_net_element.PetriNet(places_dict,\ transitions_dict, arcs_dict, "petri_net") self.petri_net = petri_net
def setUp(self): '''set up ''' # new the to unit test class object self.tclass = petri_net_analyse.PetriNetAnalyser() # place->arc1->transition->arc2->place place = petri_net_element.Place("place", "place", ["arc2"], ["arc1"]) arc1 = petri_net_element.Arc("arc1", "arc1", "place", "transition") transition = petri_net_element.Transition("transition", "transition", ["arc1"], ["arc2"]) arc2 = petri_net_element.Arc("arc2", "arc2", "transition", "place") places_dict = {} transitions_dict = {} arcs_dict = {} places_dict["place"] = place transitions_dict["transition"] = transition arcs_dict["arc1"] = arc1 arcs_dict["arc2"] = arc2 petri_net = petri_net_element.PetriNet(places_dict, transitions_dict, arcs_dict, "petri_net") self.petri_net = petri_net
def eliminate_self_loop(self, place, arc, petri_net): '''eliminate the self_loop(place-->transition-->arc-->place) place-->transition-->arc-->new_place-->new_arc1-->new_transition-->new_arc2-->place step1.separate the place and arc step2.create the new object,put it in the dict step3.connect the object to the front and back object ''' places_dict = petri_net.places_dict transitions_dict = petri_net.transitions_dict arcs_dict = petri_net.arcs_dict # separate the place and arc place.incomings.remove(arc.pt_id) arc.target = None # create a new place new_place_pt_id = self._get_new_id("place", places_dict) new_place = petri_net_element.Place(new_place_pt_id, new_place_pt_id, [arc.pt_id], []) places_dict[new_place_pt_id] = new_place arc.target = new_place_pt_id # create a new arc1 new_arc1_pt_id = self._get_new_id("arc", arcs_dict) new_arc1 = petri_net_element.Arc(new_arc1_pt_id, new_arc1_pt_id, new_place_pt_id, None) arcs_dict[new_arc1_pt_id] = new_arc1 new_place.outgoings.append(new_arc1_pt_id) # create a new ImmediateTransition new_transition_pt_id = self._get_new_id("transition", transitions_dict) new_transition = petri_net_element.ImmediateTransition(new_transition_pt_id,\ new_transition_pt_id, [new_arc1_pt_id], []) transitions_dict[new_transition_pt_id] = new_transition new_arc1.target = new_transition_pt_id # create a new arc2 new_arc2_pt_id = self._get_new_id("arc", arcs_dict) new_arc2 = petri_net_element.Arc(new_arc2_pt_id, new_arc2_pt_id, new_transition_pt_id, None) arcs_dict[new_arc2_pt_id] = new_arc2 new_transition.outgoings.append(new_arc2_pt_id) new_arc2.target = place.pt_id place.incomings.append(new_arc2_pt_id)
def create_pt_object(self, node1, node2, prob, petri_net): '''create p or t object ''' places_dict = petri_net.places_dict transitions_dict = petri_net.transitions_dict arcs_dict = petri_net.arcs_dict # the id is nodeId of the umlActivityElement , also the pt_id of the petri_net_element id1 = node1.xmi_id id2 = node2.xmi_id # the 2 nodes are place if node1.place and node2.place: # create a transition and put it in the transitionsDict # place1 -(arc1)-> transition -(arc2)-> place2 pt_id = id1 + id2 if pt_id not in transitions_dict: # create one immediate transition transition = pt_element.ImmediateTransition(pt_id, name=None, incomings=[], outgoings=[]) transitions_dict[pt_id] = transition # place1 place1 = places_dict[id1] # place2 place2 = places_dict[id2] # create one arc, place1 -(arc1)-> transition, # and put it in the arcs_dict self.create_arc(arcs_dict, place1, transition, prob) # create the other arc, transition -(arc2)-> place2, # and put it in the arcs_dict self.create_arc(arcs_dict, transition, place2) else: log.show_warn("id1-->id2 has more than one connection") # the 2 nodes are transition elif node1.transition and node2.transition: # create a place and put it in the placesDict # transition1 -(arc1)-> place -(arc2)-> transition2 pt_id = id1 + id2 if pt_id not in places_dict: # create one place place = pt_element.Place(pt_id, name=None, incomings=[], outgoings=[]) places_dict[pt_id] = place # transition1 transition1 = transitions_dict[id1] # transition2 transition2 = transitions_dict[id2] # create one arc, transition1 -(arc1)-> place, # and put it in the arcs_dict self.create_arc(arcs_dict, transition1, place) # create the other arc, place -(arc2)-> transition2, # and put it in the arcs_dict self.create_arc(arcs_dict, place, transition2) else: log.show_warn("id1-->id2 has more than one connection") # p --> t #TODO more than one connection elif node1.place and node2.transition: # just create one arc, p -(arc)-> t place = places_dict[id1] transition = transitions_dict[id2] self.create_arc(arcs_dict, place, transition, prob) # t --> p elif node1.transition and node2.place: # just create one arc, t -(arc)-> p transition = transitions_dict[id1] place = places_dict[id2] self.create_arc(arcs_dict, transition, place) else: # Error print "type error +create_pt_object():", node1.xmi_type, node2.xmi_type
def transform_to_petri_net(self, activity): ''' transform the UmlActivityDiagramElemnt objects to the petri_net_element objects ''' # the activity elements activity_name = activity.name nodes_dict = activity.nodes_dict edges_dict = activity.edges_dict # the Petri net elements # name:Place (class petri_net_element.Place) places_dict = {} # name:Transition (class petri_net_element.Transition) transitions_dict = {} # name:Arc (class petri_net_element.Arc) arcs_dict = {} # create a petri net object petri_net = pt_element.PetriNet(places_dict, transitions_dict, arcs_dict, activity_name) # create Place,Transition objects from the nodes, if node.petri=True for node_id in nodes_dict: node = nodes_dict[node_id] # nodes_dict[node_id].petri = True if node.petri: # get the id, name, incomings, outgoings pt_id = node.xmi_id name = node.name incomings = [] # list outgoings = [] # new Place object, and put it in the places_dict if node.place: # initial Node is "s1" and start:True if node.xmi_type == uml_element.Types.INITIAL_NODE: place = pt_element.Place(pt_id, name, incomings, outgoings) place.start = True place.tokens = 1 places_dict[pt_id] = place elif node.xmi_type == uml_element.Types.ACTIVITY_FINAL_NODE: place = pt_element.Place(pt_id, name, incomings, outgoings) place.final = True places_dict[pt_id] = place elif node.xmi_type == uml_element.Types.MERGE_NODE: place = pt_element.Place(pt_id, name, incomings, outgoings) places_dict[pt_id] = place # if the max_tokens of the merge is not 1 # see example:payOrder if node.max_tokens != 1: place.max_tokens = node.max_tokens else: place = pt_element.Place(pt_id, name, incomings, outgoings) places_dict[pt_id] = place # new Transition object, and put it in the transitions_dict elif node.transition: # fork,join,action without <<Time>> stereotyping if node.immediate: transition = pt_element.ImmediateTransition( pt_id, name, incomings, outgoings) # transition.delay =0 # action with <<Time>> stereotyping and property time and "const" elif node.deterministic: transition = pt_element.DeterministicTransition( pt_id, name, incomings, outgoings) transition.delay = node.time # action with <<Time>> stereotyping and property time and "exp" elif node.exponential: transition = pt_element.ExponentialTransition( pt_id, name, incomings, outgoings) transition.delay = node.time transitions_dict[pt_id] = transition # edge--> arc for edge_id in edges_dict: # get the source, target of the edge edge = edges_dict[edge_id] # if it is control flows if edge.xmi_type == uml_element.Types.CONTROL_FLOW: source = edge.source target = edge.target # get the body of the guard. # arc_label = "" guard = edge.guard if guard is not None: pass # arc_label = guard.body # get the node in nodes_dict from the ID if source in nodes_dict: node1_id = source node1 = nodes_dict[node1_id] # pin,pout else: log.show_warn("the source is not a node") # get the node in nodes_dict from the ID if target in nodes_dict: node2_id = target node2 = nodes_dict[node2_id] else: log.show_warn("the target is not a node") if node1.petri and node2.petri: # node1 , node2 -- P(merge...) or T(action...) self.create_pt_object(node1, node2, edge.prob, petri_net) else: log.show_warn("node1 and node2 are not both petri") # object flow, pass else: pass return petri_net
def test_get_petri_type(self): '''get_petri_type ''' petri_net = self.petri_net places_dict = petri_net.places_dict transitions_dict = petri_net.transitions_dict arcs_dict = petri_net.arcs_dict # p1->arc1->transition1->arc2->place2 petri_type = self.tclass.get_petri_type(petri_net) self.assertTrue( petri_net_element.PetriNetTypes.MARKED_GRAPHS in petri_type, "test_get_petri_type() assertTrue1 failed") self.assertTrue( petri_net_element.PetriNetTypes.STATE_MACHINES in petri_type, "test_get_petri_type() assertTrue2 failed") self.assertTrue( len(petri_type) == 2, "test_get_petri_type() assertTrue3 failed") # p1->arc3->t2->arc4->p3 p1 = petri_net.places_dict["p1"] p1.outgoings.append("arc3") arc3 = petri_net_element.Arc("arc3", "arc3", "p1", "t2") t2 = petri_net_element.Transition("t2", "t2", ["arc3"], ["arc4"]) arc4 = petri_net_element.Arc("arc4", "arc4", "t2", "p3") p3 = petri_net_element.Place("p3", "p3", ["arc4"], []) places_dict["p3"] = p3 transitions_dict["t2"] = t2 arcs_dict["arc3"] = arc3 arcs_dict["arc4"] = arc4 petri_type = self.tclass.get_petri_type(petri_net) self.assertTrue( petri_net_element.PetriNetTypes.STATE_MACHINES in petri_type, "test_get_petri_type() assertTrue4 failed") self.assertTrue( len(petri_type) == 1, "test_get_petri_type() assertTrue5 failed") # t2->arc5->p4 t2.outgoings.append("arc5") arc5 = petri_net_element.Arc("arc5", "arc5", "t2", "p4") p4 = petri_net_element.Place("p4", "p4", ["arc5"], []) places_dict["p4"] = p4 arcs_dict["arc5"] = arc5 petri_type = self.tclass.get_petri_type(petri_net) self.assertTrue( petri_net_element.PetriNetTypes.FREE_CHOICE in petri_type, "test_get_petri_type() assertTrue6 failed") self.assertTrue( len(petri_type) == 1, "test_get_petri_type() assertTrue7 failed") # p5->arc6->t2 t2.incomings.append("arc6") arc6 = petri_net_element.Arc("arc6", "arc6", "p5", "t2") p5 = petri_net_element.Place("p5", "p5", [], ["arc6"]) places_dict["p5"] = p5 arcs_dict["arc6"] = arc6 petri_type = self.tclass.get_petri_type(petri_net) self.assertTrue( petri_net_element.PetriNetTypes.NOT_FREE_CHOICE in petri_type, "test_get_petri_type() assertTrue8 failed") self.assertTrue( len(petri_type) == 1, "test_get_petri_type() assertTrue9 failed")