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)