def export_petri_to_spec(petri_net: PetriNet, filename, write_initial_marking=False): file = open(filename, "a") place_names = [] for p in petri_net.get_places(): place_names.append("x" + str(p)) file.write("vars\n\t") for place in place_names: file.write(place + " ") file.write("\n\nrules\n") for tran in petri_net.get_transitions(): pre = tran.get_pre() first = True for p in petri_net.get_places(): if pre[p] > 0: if first: file.write("\t") else: file.write(" , ") file.write(place_names[p] + " >= " + str(int(pre[p]))) first = False file.write(" ->\n") first = True incidence = tran.get_incidence() for p in petri_net.get_places(): if incidence[p] != 0: if not first: file.write(",\n") if incidence[p] > 0: file.write("\t\t" + place_names[p] + "' = " + place_names[p] + "+" + str(int(incidence[p]))) else: file.write("\t\t" + place_names[p] + "' = " + place_names[p] + "-" + str(int(incidence[p]) * (-1))) first = False file.write(";\n\n") if not write_initial_marking: file.close() return file.write("init\n\t") first = True init_mark = (petri_net.get_mark()) for p in petri_net.get_places(): if not first: file.write(" , ") if init_mark[p] == float("inf"): file.write(place_names[p] + " >= 1") else: file.write(place_names[p] + " = " + str(int(init_mark[p]))) first = False file.close()
def export_petri_to_valmari(petri_net: PetriNet, filename): file = open(filename, "a") # file.write("samples = []") # file.write("omega = float('inf')") # file.write("def Init():") file.write("%d %d \n" % (petri_net.get_dim(), len(petri_net.get_transitions()))) trun_num = 1 for tran in petri_net.get_transitions(): place_num = 1 for p in (tran.get_incidence() + tran.get_pre()): if p <= 0: place_num += 1 continue file.write("-%d \n" % trun_num) file.write("0 %d \n" % p) file.write("%d \n" % place_num) place_num += 1 place_num = 1 for p in tran.get_pre(): if p <= 0: place_num += 1 continue file.write("%d \n" % place_num) file.write("0 %d \n" % p) file.write("-%d \n" % trun_num) place_num += 1 trun_num += 1 file.write("0 \n") place_num = 1 for p in petri_net.get_mark(): if p <= 0: place_num += 1 continue if p == float("inf"): file.write("-32767 \n") file.write("%d \n" % place_num) else: file.write("-%d \n" % p) file.write("%d \n" % place_num) place_num += 1 file.write("0 \n") file.close()
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 run_benchmark(name, petri: petri_net.PetriNet, time_out, init_marking=None, with_acc=True): benchmark = Benchmark(name) print("starting %s" % name + " numb of trans: " + str(len(petri.get_transitions())) + " num of places: " + str(petri.get_dim())) start_time = time.time() if init_marking is None: cov = CovTree(petri, petri.get_mark()) else: cov = CovTree(petri, init_marking) cov.keep_accelerations = with_acc cov.type_of_graph_traversal = "DFS" cov.use_z3_to_guess_accelerations = False cov.check_for_correctness = False cov.verbose = True cov.timeout = time_out benchmark.clover = cov.generate_cov_tree() benchmark.timeout = benchmark.clover is None elapsed_time = time.time() - start_time benchmark.max_vertices = cov.max_size benchmark.max_accelerations = cov.max_size_of_acc benchmark.final_accelerations = len(cov._accelerations) benchmark.final_vertices = len(cov._verSet) benchmark.used_accelerations = cov.use_of_acc benchmark.time = elapsed_time benchmark.places = len(petri.get_places()) benchmark.transitions = len(petri.get_transitions()) benchmark.num_of_comparisons = cov.num_of_comparisons benchmark.num_of_recheck = cov.num_of_rechecks return benchmark, cov
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 export_petri_to_MP(petri_net: PetriNet, filename, name): file = open(filename, "a") # file.write("samples = []") # file.write("omega = float('inf')") # file.write("def Init():") file.write("#======================================\n") file.write("#======== %s ==============\n" % name) file.write("#======================================\n") file.write("tplus = []\n") file.write("tmin = []\n") i = 0 for tran in petri_net.get_transitions(): file.write("#t%d\n" % i) i += 1 file.write("tplus += [[") is_first = True for p2 in (tran.get_incidence() + tran.get_pre()): if p2 < 0: p2 = 0 # p2 = int(p2*(-1)) if is_first: is_first = False file.write("%d" % p2) continue file.write(", %d" % p2) file.write("]]\n") file.write("tmin += [[") is_first = True for p in tran.get_pre(): if p < 0: p = 0 if is_first: is_first = False file.write("%d" % p) continue file.write(", %d" % p) file.write("]]\n") file.write("t0 = [") is_first = True for p in petri_net.get_mark().get_marking(): if not is_first: file.write(", ") else: is_first = False if p == float("inf"): file.write("omega") else: file.write("%d" % p) file.write("]\n") file.write("samples.append(Sample(\"%s\", tplus, tmin, t0, None)) \n" % name) file.write("#======================================\n") file.write("#================END===================\n") file.write("#======================================\n") file.close()