include "primitives.alh"
include "modelling.alh"
include "object_operations.alh"

Composite schedule {
    {Contains} Success success {}
    {Contains} Failure failure {}

    {Contains} ForAll link {
        LHS {
            Pre_Architecture/Group pre_l_0{
                label = "0"
            }
            Pre_Architecture/Port pre_l_1{
                label = "1"
            }
            Pre_Architecture/Contains pre_l_10 (pre_l_0, pre_l_1){
                label = "10"
            }
            Pre_Encapsulated_PetriNet/Port pre_l_2{
                label = "2"
            }
            Pre_Architecture/Group pre_l_3{
                label = "3"
            }
            Pre_Architecture/Port pre_l_4{
                label = "4"
            }
            Pre_Architecture/Contains pre_l_11 (pre_l_3, pre_l_4){
                label = "11"
            }
            Pre_Encapsulated_PetriNet/Port pre_l_5{
                label = "5"
            }
            Pre_Architecture/Connects (pre_l_1, pre_l_4){
                label = "6"
            }

            constraint = $
                Boolean function constraint(model : Element, mapping : Element):
                    String name_0
                    String name_1
                    String name_2
                    String name_3
                    String name_4
                    String name_5

                    name_0 = read_attribute(model, mapping["0"], "name")
                    name_1 = read_attribute(model, mapping["1"], "name")
                    name_2 = read_attribute(model, mapping["2"], "name")
                    name_3 = read_attribute(model, mapping["3"], "name")
                    name_4 = read_attribute(model, mapping["4"], "name")
                    name_5 = read_attribute(model, mapping["5"], "name")

                    if bool_not(name_0 + "/" + name_1 == name_2):
                        return False!
                    if bool_not(name_3 + "/" + name_4 == name_5):
                        return False!
                    return True!
                $
        }
        RHS {
            Post_Architecture/Group post_l_0{
                label = "0"
            }
            Post_Architecture/Port post_l_1{
                label = "1"
            }
            Post_Architecture/Contains post_l_10 (post_l_0, post_l_1){
                label = "10"
            }
            Post_Encapsulated_PetriNet/Port post_l_2{
                label = "2"
            }
            Post_Architecture/Group post_l_3{
                label = "3"
            }
            Post_Architecture/Port post_l_4{
                label = "4"
            }
            Post_Architecture/Contains post_l_11 (post_l_3, post_l_4){
                label = "11"
            }
            Post_Encapsulated_PetriNet/Port post_l_5{
                label = "5"
            }
            Post_Architecture/Connects (post_l_1, post_l_4){
                label = "6"
            }
            Post_Encapsulated_PetriNet/Related (post_l_2, post_l_5){
                label = "7"
            }
        }
    }

    {Contains} ForAll remove_old {
        LHS {
            Pre_Encapsulated_PetriNet/Port pre_ro_0 {
                label = "0"
            }
            Pre_Encapsulated_PetriNet/Port pre_ro_1 {
                label = "1"
            }
            Pre_Encapsulated_PetriNet/Place pre_ro_2 {
                label = "2"
            }
            Pre_Encapsulated_PetriNet/Related (pre_ro_0, pre_ro_1) {
                label = "3"
            }
            Pre_Encapsulated_PetriNet/PortPlace (pre_ro_1, pre_ro_2) {
                label = "4"
            }
        }
        RHS {
            Post_Encapsulated_PetriNet/Port {
                label = "0"
            }
        }
    }

    {Contains} ForAll merge_P2T {
        LHS {
            Pre_Encapsulated_PetriNet/Place pre_p2t_1 {
                label = "1"
            }
            Pre_Encapsulated_PetriNet/Port pre_p2t_2 {
                label = "2"
            }
            Pre_Encapsulated_PetriNet/PortPlace (pre_p2t_2, pre_p2t_1){
                label = "3"
            }
            Pre_Encapsulated_PetriNet/Place pre_p2t_4 {
                label = "4"
            }
            Pre_Encapsulated_PetriNet/Port pre_p2t_5 {
                label = "5"
            }
            Pre_Encapsulated_PetriNet/PortPlace (pre_p2t_5, pre_p2t_4){
                label = "6"
            }
            Pre_Encapsulated_PetriNet/Transition pre_p2t_7 {
                label = "7"
            }
            Pre_Encapsulated_PetriNet/P2T (pre_p2t_4, pre_p2t_7) {
                label = "8"
            }
            Pre_Encapsulated_PetriNet/Related (pre_p2t_2, pre_p2t_5) {
                label = "10"
            }
        }
        RHS {
            Post_Encapsulated_PetriNet/Place post_p2t_1 {
                label = "1"
            }
            Post_Encapsulated_PetriNet/Port post_p2t_2 {
                label = "2"
            }
            Post_Encapsulated_PetriNet/PortPlace (post_p2t_2, post_p2t_1){
                label = "3"
            }
            Post_Encapsulated_PetriNet/Place post_p2t_4 {
                label = "4"
            }
            Post_Encapsulated_PetriNet/Port post_p2t_5 {
                label = "5"
            }
            Post_Encapsulated_PetriNet/PortPlace (post_p2t_5, post_p2t_4){
                label = "6"
            }
            Post_Encapsulated_PetriNet/Transition post_p2t_7 {
                label = "7"
            }
            Post_Encapsulated_PetriNet/P2T (post_p2t_4, post_p2t_7) {
                label = "8"
            }
            Post_Encapsulated_PetriNet/P2T (post_p2t_1, post_p2t_7) {
                label = "9"
            }
            Post_Encapsulated_PetriNet/Related (post_p2t_2, post_p2t_5) {
                label = "10"
            }
        }
    }

    {Contains} ForAll merge_T2P {
        LHS {
            Pre_Encapsulated_PetriNet/Place pre_t2p_1 {
                label = "1"
            }
            Pre_Encapsulated_PetriNet/Port pre_t2p_2 {
                label = "2"
            }
            Pre_Encapsulated_PetriNet/PortPlace (pre_t2p_2, pre_t2p_1){
                label = "3"
            }
            Pre_Encapsulated_PetriNet/Place pre_t2p_4 {
                label = "4"
            }
            Pre_Encapsulated_PetriNet/Port pre_t2p_5 {
                label = "5"
            }
            Pre_Encapsulated_PetriNet/PortPlace (pre_t2p_5, pre_t2p_4){
                label = "6"
            }
            Pre_Encapsulated_PetriNet/Transition pre_t2p_7 {
                label = "7"
            }
            Pre_Encapsulated_PetriNet/T2P (pre_t2p_7, pre_t2p_4) {
                label = "8"
            }

            Pre_Encapsulated_PetriNet/Related (pre_t2p_2, pre_t2p_5) {
                label = "10"
            }
        }
        RHS {
            Post_Encapsulated_PetriNet/Place post_t2p_1 {
                label = "1"
            }
            Post_Encapsulated_PetriNet/Port post_t2p_2 {
                label = "2"
            }
            Post_Encapsulated_PetriNet/PortPlace (post_t2p_2, post_t2p_1){
                label = "3"
            }
            Post_Encapsulated_PetriNet/Place post_t2p_4 {
                label = "4"
            }
            Post_Encapsulated_PetriNet/Port post_t2p_5 {
                label = "5"
            }
            Post_Encapsulated_PetriNet/PortPlace (post_t2p_5, post_t2p_4){
                label = "6"
            }
            Post_Encapsulated_PetriNet/Transition post_t2p_7 {
                label = "7"
            }
            Post_Encapsulated_PetriNet/T2P (post_t2p_7, post_t2p_4) {
                label = "8"
            }
            Post_Encapsulated_PetriNet/T2P (post_t2p_7, post_t2p_1) {
                label = "9"
            }
            Post_Encapsulated_PetriNet/Related (post_t2p_2, post_t2p_5) {
                label = "10"
            }
        }
    }

    {Contains} ForAll copy_transitions {
        LHS {
            Pre_Encapsulated_PetriNet/Transition {
                label = "0"
            }
        }
        RHS {
            Post_Encapsulated_PetriNet/Transition ct1 {
                label = "0"
            }
            Post_PetriNet/Transition ct2 {
                label = "1"
                value_name = $
                    Integer function value(model : Element, name : String, mapping : Element):
                        return read_attribute(model, mapping["0"], "name")!
                    $
            }
            Post_EPN2PN_transition_link (ct1, ct2){
                label = "2"
            }
        }
    }
    {Contains} ForAll copy_places {
        LHS {
            Pre_Encapsulated_PetriNet/Place {
                label = "0"
            }
        }
        RHS {
            Post_Encapsulated_PetriNet/Place cp1 {
                label = "0"
            }
            Post_PetriNet/Place cp2 {
                label = "1"
                value_tokens = $
                    Integer function value(model : Element, name : String, mapping : Element):
                        return read_attribute(model, mapping["0"], "tokens")!
                    $
                value_name = $
                    Integer function value(model : Element, name : String, mapping : Element):
                        return read_attribute(model, mapping["0"], "name")!
                    $
            }
            Post_EPN2PN_place_link (cp1, cp2){
                label = "2"
            }
        }
    }
    {Contains} ForAll copy_P2T {
        LHS {
            Pre_Encapsulated_PetriNet/Place cp2t_p{
                label = "0"
            }
            Pre_Encapsulated_PetriNet/Transition cp2t_t{
                label = "1"
            }
            Pre_Encapsulated_PetriNet/P2T (cp2t_p, cp2t_t){
                label = "2"
            }
            Pre_PetriNet/Place cp2t_p2{
                label = "3"
            }
            Pre_PetriNet/Transition cp2t_t2{
                label = "4"
            }
            Pre_EPN2PN_place_link (cp2t_p, cp2t_p2){
                label = "5"
            }
            Pre_EPN2PN_transition_link (cp2t_t, cp2t_t2){
                label = "6"
            }
        }
        RHS {
            Post_Encapsulated_PetriNet/Place rhs_cp2t_p{
                label = "0"
            }
            Post_Encapsulated_PetriNet/Transition rhs_cp2t_t{
                label = "1"
            }
            Post_Encapsulated_PetriNet/P2T rhs_cp2t_p2t (rhs_cp2t_p, rhs_cp2t_t){
                label = "2"
            }
            Post_PetriNet/Place rhs_cp2t_p2 {
                label = "3"
            }
            Post_PetriNet/Transition rhs_cp2t_t2 {
                label = "4"
            }
            Post_EPN2PN_place_link (rhs_cp2t_p, rhs_cp2t_p2){
                label = "5"
            }
            Post_EPN2PN_transition_link (rhs_cp2t_t, rhs_cp2t_t2){
                label = "6"
            }
            Post_PetriNet/P2T rhs_cp2t_p2t2(rhs_cp2t_p2, rhs_cp2t_t2) {
                label = "7"
                value_weight = $
                    Integer function value(host_model : Element, name : String, mapping : Element):
                        return 1!
                    $
            }
        }
    }
    {Contains} ForAll copy_T2P {
        LHS {
            Pre_Encapsulated_PetriNet/Place ct2p_p{
                label = "0"
            }
            Pre_Encapsulated_PetriNet/Transition ct2p_t{
                label = "1"
            }
            Pre_Encapsulated_PetriNet/T2P (ct2p_t, ct2p_p){
                label = "2"
            }
            Pre_PetriNet/Place ct2p_p2{
                label = "3"
            }
            Pre_PetriNet/Transition ct2p_t2{
                label = "4"
            }
            Pre_EPN2PN_place_link (ct2p_p, ct2p_p2){
                label = "5"
            }
            Pre_EPN2PN_transition_link (ct2p_t, ct2p_t2){
                label = "6"
            }
        }
        RHS {
            Post_Encapsulated_PetriNet/Place rhs_ct2p_p{
                label = "0"
            }
            Post_Encapsulated_PetriNet/Transition rhs_ct2p_t{
                label = "1"
            }
            Post_Encapsulated_PetriNet/T2P (rhs_ct2p_t, rhs_ct2p_p){
                label = "2"
            }
            Post_PetriNet/Place rhs_ct2p_p2 {
                label = "3"
            }
            Post_PetriNet/Transition rhs_ct2p_t2 {
                label = "4"
            }
            Post_EPN2PN_place_link (rhs_ct2p_p, rhs_ct2p_p2){
                label = "5"
            }
            Post_EPN2PN_transition_link (rhs_ct2p_t, rhs_ct2p_t2){
                label = "6"
            }
            Post_PetriNet/T2P (rhs_ct2p_t2, rhs_ct2p_p2) {
                label = "7"
                value_weight = $
                    Integer function value(host_model : Element, name : String, mapping : Element):
                        return 1!
                    $
            }
        }
    }
}

Initial (schedule, link) {}

OnSuccess (link, merge_P2T) {}
OnFailure (link, merge_P2T) {}

OnSuccess (merge_P2T, merge_T2P) {}
OnFailure (merge_P2T, merge_T2P) {}

OnSuccess (merge_T2P, remove_old) {}
OnFailure (merge_T2P, remove_old) {}

OnSuccess (remove_old, copy_places) {}
OnFailure (remove_old, copy_places) {}

OnSuccess (copy_places, copy_transitions) {}
OnSuccess (copy_transitions, copy_P2T) {}
OnSuccess (copy_P2T, copy_T2P) {}
OnSuccess (copy_T2P, success) {}
OnFailure (copy_places, copy_transitions) {}
OnFailure (copy_transitions, copy_P2T) {}
OnFailure (copy_P2T, copy_T2P) {}
OnFailure (copy_T2P, success) {}
