~tapaal-contributor/tapaal/cpn-gui-dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package dk.aau.cs.model.tapn;

import java.util.ArrayList;
import java.util.List;

import dk.aau.cs.model.CPN.Color;
import dk.aau.cs.model.CPN.ColorType;
import dk.aau.cs.model.CPN.ColoredTimeInvariant;
import pipe.dataLayer.Template;
import pipe.gui.CreateGui;
import dk.aau.cs.util.Tuple;

public class SharedPlace extends TimedPlace{
    private ColorType colorType;
    private List<ColoredTimeInvariant> ctiList = new ArrayList<ColoredTimeInvariant>() {{
        add(ColoredTimeInvariant.LESS_THAN_INFINITY_DYN_COLOR(Color.STAR_COLOR));
    }};
    private TimedArcPetriNetNetwork network;

    public SharedPlace(String name){
		this(name, TimeInvariant.LESS_THAN_INFINITY, ColoredTimeInvariant.LESS_THAN_INFINITY_AND_DOT);
	}
	
	public SharedPlace(String name, TimeInvariant invariant, ColoredTimeInvariant CTI){
		setName(name);
		setInvariant(invariant);
		setColorTimeInvariant(CTI);
	}

    public void setNetwork(TimedArcPetriNetNetwork network) {
		this.network = network;		
	}
	
	public TimedArcPetriNetNetwork network(){
		return network;
	}
	

    //TODO: check that colored time invariant is copied correctly
	public TimedPlace copy() {
		return new SharedPlace(this.name(), this.invariant().copy(), this.getColoredTimeInvariant().copy());
	}

	public boolean isShared() {
		return true;
	}

	
	public ArrayList<String> getComponentsUsingThisPlace(){
		ArrayList<String> components = new ArrayList<String>();
		for(Template t : CreateGui.getCurrentTab().allTemplates()){
			TimedPlace tp = t.model().getPlaceByName(SharedPlace.this.name);
			if(tp != null){
				components.add(t.model().name());
			}
		}
		return components;
	}
	
	@Override
	public String toString() {
		return name;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof SharedPlace))
			return false;
		SharedPlace other = (SharedPlace) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
	public Tuple<PlaceType, Integer> extrapolate(){
		if(extrapolation.value2() > -2)	return extrapolation;
		
		PlaceType type = PlaceType.Dead;
		int cmax = -1;
		
		extrapolation = new Tuple<TimedPlace.PlaceType, Integer>(type, cmax);
		
		for(Template t : CreateGui.getCurrentTab().activeTemplates()){
			TimedPlace tp = t.model().getPlaceByName(SharedPlace.this.name);
			if(tp != null){
				cmax = Math.max(cmax, tp.extrapolate().value2());
				if(tp.extrapolate().value1() == PlaceType.Invariant || (type == PlaceType.Dead && tp.extrapolate().value1() == PlaceType.Standard)){
					type = tp.extrapolate().value1();
				}
				extrapolation = new Tuple<TimedPlace.PlaceType, Integer>(type, cmax);
			}
		}
		
		extrapolation = new Tuple<TimedPlace.PlaceType, Integer>(PlaceType.Dead, -2);
		
		return new Tuple<TimedPlace.PlaceType, Integer>(type, cmax);
	}
    @Override
    public List<ColoredTimeInvariant> getCtiList() {
        return ctiList;
    }

    //TODO: is this the right way to set this list
    public void setCtiList(List<ColoredTimeInvariant> ctiList) {
        List<ColoredTimeInvariant> found = new ArrayList<ColoredTimeInvariant>();
        for (ColoredTimeInvariant timeInvariant : ctiList) {
            if (timeInvariant == null) {
                found.add(timeInvariant);
            }
        }
        ctiList.removeAll(found);
        this.ctiList = ctiList;
    }

    public void setColorType(ColorType colorType) {
        this.colorType = colorType;
    }
    @Override
    public ColorType getColorType() {return colorType;}
}