Ejemplo n.º 1
0
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()
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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()
Ejemplo n.º 5
0
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()