def test_petri_net(self): """ Testting basic properties of petri net """ with self.assertRaises(Exception): PetriNet(0.5) with self.assertRaises(Exception): PetriNet(0) mark1 = OmegaMarking(np.array([1, 2, 3, float("inf")])) mark2 = OmegaMarking(np.array([1, 2, 3, float("inf"), 5])) tran1 = OmegaTransition(np.array([1, 2, 3, 5]), np.array([1, 4, 3, -5])) tran2 = OmegaTransition(np.array([1, float("inf"), 3, 5]), np.array([2, 3, 3, -5])) tran3 = OmegaTransition(np.array([1, float("inf"), 2, 5]), np.array([5, 5, float("inf"), -5])) petri = PetriNet(4) with self.assertRaises(Exception): petri.mark_the_petri_net(mark2) petri.mark_the_petri_net(mark1) petri.add_transition(tran1) petri.add_transition(tran2) petri.add_transition(tran3) self.assertEqual(len(petri.get_transitions()), 3)
def load_petri_net_from_pnml(filename): places_names = [] transitions_names = [] pre_transitions = [] incidence_transitions = [] init_marking = [] tree = ET.parse(filename) root = tree.getroot() net = root.find("{http://www.pnml.org/version-2009/grammar/pnml}net").find( "{http://www.pnml.org/version-2009/grammar/pnml}page") for place in net.iter( '{http://www.pnml.org/version-2009/grammar/pnml}place'): places_names.append(place.attrib["id"]) init = place.find( "{http://www.pnml.org/version-2009/grammar/pnml}initialMarking") if init is None: init_marking.append(0) else: i = init.find( "{http://www.pnml.org/version-2009/grammar/pnml}text").text init_marking.append(int(i)) dim = len(places_names) for tran in net.iter( '{http://www.pnml.org/version-2009/grammar/pnml}transition'): transitions_names.append(tran.attrib["id"]) pre_transitions.append(numpy.zeros(dim)) incidence_transitions.append(numpy.zeros(dim)) for arc in net.iter('{http://www.pnml.org/version-2009/grammar/pnml}arc'): source = arc.attrib["source"] target = arc.attrib["target"] if source in places_names: p = places_names.index(source) t = transitions_names.index(target) pre_transitions[t][p] += 1 else: p = places_names.index(target) t = transitions_names.index(source) incidence_transitions[t][p] += 1 petri_net = PetriNet(dim) petri_net.mark_the_petri_net(OmegaMarking(np.array(init_marking))) for i in range(len(transitions_names)): petri_net.add_transition( OmegaTransition(pre_transitions[i], incidence_transitions[i])) return petri_net
def test_banana_land(self): petri = PetriNet(6) # Adding transitions: petri.add_transition( OmegaTransition(np.array([1, 0, 0, 0, 0, 0]), np.array([-1, 1, 1, 0, 0, 0]))) petri.add_transition( OmegaTransition(np.array([1, 0, 0, 0, 0, 0]), np.array([-1, 1, 0, 1, 0, 0]))) petri.add_transition( OmegaTransition(np.array([1, 0, 0, 0, 0, 0]), np.array([-1, 0, 1, 1, 0, 0]))) petri.add_transition( OmegaTransition(np.array([0, 1, 0, 0, 0, 0]), np.array([0, 0, 0, 0, 1, 0]))) petri.add_transition( OmegaTransition(np.array([0, 0, 0, 1, 0, 0]), np.array([0, 0, 0, -1, 0, 1]))) petri.add_transition( OmegaTransition(np.array([0, 0, 1, 0, 1, 0]), np.array([0, 0, 0, 0, -1, 1]))) # Marking the net: petri.mark_the_petri_net(OmegaMarking(np.array([1, 0, 0, 0, 0, 0]))) # Initializing the tree: cov_tree = CovTree(petri, petri.get_mark()) anti_chain = cov_tree.generate_cov_tree(True) self.assertEqual(len(anti_chain), 4) markings = [] for node in anti_chain: markings.append(node.get_mark()) self.assertTrue(OmegaMarking(np.array([1, 0, 0, 0, 0, 0])) in markings) self.assertTrue( OmegaMarking(np.array([0, 1, 1, 0, float("inf"), float("inf")])) in markings) self.assertTrue( OmegaMarking(np.array([0, 1, 0, 1, float("inf"), 0])) in markings) self.assertTrue(OmegaMarking(np.array([0, 0, 1, 1, 0, 0])) in markings) self.assertTrue(len(cov_tree._accelerations) == 2)
def test_Alain_2005(self): petri = PetriNet(7) # Adding transitoins: petri.add_transition( OmegaTransition(np.array([1, 0, 0, 0, 0, 0, 0]), np.array([-1, 1, 0, 0, 0, 0, 0]))) petri.add_transition( OmegaTransition(np.array([1, 0, 0, 0, 0, 0, 0]), np.array([-1, 0, 0, 0, 0, 0, 1]))) petri.add_transition( OmegaTransition(np.array([1, 0, 0, 0, 0, 0, 0]), np.array([-1, 0, 0, 0, 0, 1, 0]))) petri.add_transition( OmegaTransition(np.array([0, 0, 0, 0, 0, 1, 0]), np.array([0, 0, 0, 1, 2, -1, 0]))) petri.add_transition( OmegaTransition(np.array([0, 1, 0, 0, 0, 0, 0]), np.array([0, -1, 1, 0, 0, 0, 0]))) petri.add_transition( OmegaTransition(np.array([0, 0, 1, 0, 0, 0, 0]), np.array([0, 0, -1, 1, 0, 0, 0]))) petri.add_transition( OmegaTransition(np.array([0, 0, 0, 1, 0, 0, 0]), np.array([0, 0, 1, -1, 1, 0, 0]))) petri.add_transition( OmegaTransition(np.array([0, 0, 0, 0, 0, 0, 1]), np.array([0, 1, 0, 0, 1, 0, -1]))) # Marking the net: petri.mark_the_petri_net(OmegaMarking(np.array([1, 0, 0, 0, 0, 0, 0]))) # Initializing the tree: cov_tree = CovTree(petri, petri.get_mark()) anti_chain = cov_tree.generate_cov_tree() self.assertEqual(len(anti_chain), 6)
def load_petri_net_from_apt(filename): MODES = ['.places', 'rules', 'init', 'target', 'invariants'] num_places = 0 places = [] with open(filename) as input_file: places = 0 for row in input_file: if row.strip() == MODES[0]: data = (next(input_file)).strip() num_places = len(data.split(' ')) break petri = PetriNet(num_places) with open(filename) as input_file: mode = 'none' rules_acc = '' acc = '' taracc = '' init_mark = numpy.zeros(num_places) target = numpy.zeros(num_places) for row in input_file: data = row.strip() # Ignore empty/commented lines if len(data) == 0 or data[0] == '#': continue if data in MODES: mode = data if mode == MODES[1]: places_indices = { value: key for key, value in enumerate(places) } else: # Places if mode == MODES[0]: places.extend(data.split(' ')) # Rules elif mode == MODES[1]: rules_acc += data pos = rules_acc.find(';') if pos >= 0: add_transition(petri, places, rules_acc[:pos]) rules_acc = rules_acc[pos + 1:] # Initial values elif mode == MODES[2]: acc = add_initial_mark(acc + data, places_indices, init_mark) # print(init_mark) # Target values elif mode == MODES[3]: taracc = add_initial_target(taracc + data, places_indices, target) # print("") # currently don't care about the target petri.mark_the_petri_net(init_mark) if withTarget: return petri, target else: return petri