diff options
Diffstat (limited to 'Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/designspace/api/TrajectoryInfo.java')
-rw-r--r-- | Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/designspace/api/TrajectoryInfo.java | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/designspace/api/TrajectoryInfo.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/designspace/api/TrajectoryInfo.java new file mode 100644 index 00000000..acd88416 --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/designspace/api/TrajectoryInfo.java | |||
@@ -0,0 +1,154 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2014, Miklos Foldenyi, Andras Szabolcs Nagy, Abel Hegedus, Akos Horvath, Zoltan Ujhelyi and Daniel Varro | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package org.eclipse.viatra.dse.designspace.api; | ||
10 | |||
11 | import java.util.ArrayList; | ||
12 | import java.util.Collections; | ||
13 | import java.util.List; | ||
14 | import java.util.Map; | ||
15 | import java.util.Objects; | ||
16 | |||
17 | import org.eclipse.viatra.dse.api.DSEException; | ||
18 | import org.eclipse.viatra.dse.api.SolutionTrajectory; | ||
19 | import org.eclipse.viatra.dse.base.DesignSpaceManager; | ||
20 | import org.eclipse.viatra.dse.statecode.IStateCoderFactory; | ||
21 | import org.eclipse.viatra.transformation.runtime.emf.rules.batch.BatchTransformationRule; | ||
22 | |||
23 | public class TrajectoryInfo { | ||
24 | |||
25 | private final List<Object> trajectory; | ||
26 | private final List<Object> trajectoryView; | ||
27 | private final List<BatchTransformationRule<?, ?>> rules; | ||
28 | private final List<BatchTransformationRule<?, ?>> rulesView; | ||
29 | private final List<Object> stateIds; | ||
30 | private final List<Object> stateIdsView; | ||
31 | private final List<Map<String, Double>> measuredCosts; | ||
32 | |||
33 | public TrajectoryInfo(Object initialStateId) { | ||
34 | Objects.requireNonNull(initialStateId); | ||
35 | |||
36 | stateIds = new ArrayList<>(); | ||
37 | stateIds.add(initialStateId); | ||
38 | |||
39 | trajectory = new ArrayList<>(); | ||
40 | rules = new ArrayList<>(); | ||
41 | measuredCosts = new ArrayList<>(); | ||
42 | |||
43 | trajectoryView = Collections.unmodifiableList(trajectory); | ||
44 | stateIdsView = Collections.unmodifiableList(stateIds); | ||
45 | rulesView = Collections.unmodifiableList(rules); | ||
46 | } | ||
47 | |||
48 | /** | ||
49 | * Copy constructor | ||
50 | * | ||
51 | * @since 0.17 | ||
52 | */ | ||
53 | public TrajectoryInfo(TrajectoryInfo other) { | ||
54 | this(other.stateIds, other.trajectory, other.rules, other.measuredCosts); | ||
55 | } | ||
56 | |||
57 | protected TrajectoryInfo(List<Object> stateIds, List<Object> trajectory, List<BatchTransformationRule<?, ?>> rules, List<Map<String, Double>> measuredCosts) { | ||
58 | |||
59 | this.stateIds = new ArrayList<>(stateIds); | ||
60 | this.trajectory = new ArrayList<>(trajectory); | ||
61 | this.rules = new ArrayList<>(rules); | ||
62 | trajectoryView = Collections.unmodifiableList(trajectory); | ||
63 | stateIdsView = Collections.unmodifiableList(stateIds); | ||
64 | rulesView = Collections.unmodifiableList(rules); | ||
65 | this.measuredCosts = new ArrayList<>(measuredCosts); | ||
66 | } | ||
67 | |||
68 | public void addStep(Object activationId, BatchTransformationRule<?, ?> rule, Object newStateId, Map<String, Double> measuredCosts) { | ||
69 | stateIds.add(newStateId); | ||
70 | trajectory.add(activationId); | ||
71 | rules.add(rule); | ||
72 | this.measuredCosts.add(measuredCosts); | ||
73 | } | ||
74 | |||
75 | public void backtrack() { | ||
76 | int size = trajectory.size(); | ||
77 | |||
78 | if (size == 0) { | ||
79 | throw new DSEException("Cannot step back any further!"); | ||
80 | } | ||
81 | |||
82 | trajectory.remove(size - 1); | ||
83 | rules.remove(size - 1); | ||
84 | stateIds.remove(size); | ||
85 | measuredCosts.remove(size - 1); | ||
86 | } | ||
87 | |||
88 | public Object getInitialStateId() { | ||
89 | return stateIds.get(0); | ||
90 | } | ||
91 | |||
92 | public Object getCurrentStateId() { | ||
93 | return stateIds.get(stateIds.size() - 1); | ||
94 | } | ||
95 | |||
96 | public Object getLastActivationId() { | ||
97 | return trajectory.get(trajectory.size() - 1); | ||
98 | } | ||
99 | |||
100 | public List<Object> getTrajectory() { | ||
101 | return trajectoryView; | ||
102 | } | ||
103 | |||
104 | public List<Object> getStateTrajectory() { | ||
105 | return stateIdsView; | ||
106 | } | ||
107 | |||
108 | public List<BatchTransformationRule<?, ?>> getRules() { | ||
109 | return rulesView; | ||
110 | } | ||
111 | |||
112 | public int getDepth() { | ||
113 | return trajectory.size(); | ||
114 | } | ||
115 | |||
116 | public List<Map<String, Double>> getMeasuredCosts() { | ||
117 | return measuredCosts; | ||
118 | } | ||
119 | |||
120 | public SolutionTrajectory createSolutionTrajectory(final IStateCoderFactory stateCoderFactory, final IBacktrackListener listener) { | ||
121 | |||
122 | List<Object> activationIds = new ArrayList<>(trajectory); | ||
123 | List<BatchTransformationRule<?, ?>> copiedRules = new ArrayList<>(rules); | ||
124 | |||
125 | return new SolutionTrajectory(activationIds, copiedRules, stateCoderFactory, listener); | ||
126 | } | ||
127 | |||
128 | public boolean canStepBack() { | ||
129 | return !trajectory.isEmpty(); | ||
130 | } | ||
131 | |||
132 | @Override | ||
133 | public String toString() { | ||
134 | StringBuilder sb = new StringBuilder("Trajectory:\n"); | ||
135 | for (Object activationId : trajectory) { | ||
136 | sb.append(activationId); | ||
137 | sb.append("\n"); | ||
138 | } | ||
139 | return sb.toString(); | ||
140 | } | ||
141 | |||
142 | /** | ||
143 | * This method is only used by the {@link DesignSpaceManager}. | ||
144 | * @param stateCode | ||
145 | * @return false if the initial state code is the last one, otherwise true. | ||
146 | */ | ||
147 | public boolean modifyLastStateCode(Object stateCode) { | ||
148 | if (stateIds.size() == 1) { | ||
149 | return false; | ||
150 | } | ||
151 | stateIds.set(stateIds.size() - 1, stateCode); | ||
152 | return true; | ||
153 | } | ||
154 | } | ||