diff options
Diffstat (limited to 'Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/designspace')
4 files changed, 294 insertions, 0 deletions
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/designspace/api/DesignSpace.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/designspace/api/DesignSpace.java new file mode 100644 index 00000000..bc0f08d4 --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/designspace/api/DesignSpace.java | |||
@@ -0,0 +1,106 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Andras Szabolcs Nagy 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.Collection; | ||
13 | import java.util.Collections; | ||
14 | import java.util.HashMap; | ||
15 | import java.util.HashSet; | ||
16 | import java.util.List; | ||
17 | import java.util.Map; | ||
18 | import java.util.Random; | ||
19 | import java.util.Set; | ||
20 | |||
21 | public class DesignSpace implements IDesignSpace { | ||
22 | |||
23 | Set<Object> statesView; | ||
24 | |||
25 | Set<Object> rootStates; | ||
26 | Set<Object> rootStatesView; | ||
27 | |||
28 | Map<Object, List<Object>> statesAndActivations; | ||
29 | |||
30 | Random random = new Random(); | ||
31 | |||
32 | public DesignSpace() { | ||
33 | rootStates = new HashSet<>(); | ||
34 | rootStatesView = Collections.unmodifiableSet(rootStates); | ||
35 | |||
36 | statesAndActivations = new HashMap<>(); | ||
37 | statesView = statesAndActivations.keySet(); | ||
38 | } | ||
39 | |||
40 | @Override | ||
41 | public synchronized Collection<Object> getStates() { | ||
42 | return statesView; | ||
43 | } | ||
44 | |||
45 | @Override | ||
46 | public synchronized Collection<Object> getRoots() { | ||
47 | return rootStatesView; | ||
48 | } | ||
49 | |||
50 | @Override | ||
51 | public synchronized void addState(Object sourceStateId, Object firedActivationId, Object newStateId) { | ||
52 | |||
53 | List<Object> activationIds = statesAndActivations.get(newStateId); | ||
54 | |||
55 | if (activationIds == null) { | ||
56 | activationIds = new ArrayList<Object>(); | ||
57 | statesAndActivations.put(newStateId, activationIds); | ||
58 | |||
59 | if (sourceStateId == null) { | ||
60 | rootStates.add(newStateId); | ||
61 | return; | ||
62 | } | ||
63 | } | ||
64 | |||
65 | activationIds = statesAndActivations.get(sourceStateId); | ||
66 | |||
67 | if (activationIds == null) { | ||
68 | activationIds = new ArrayList<Object>(); | ||
69 | activationIds.add(firedActivationId); | ||
70 | statesAndActivations.put(sourceStateId, activationIds); | ||
71 | } else { | ||
72 | activationIds.add(firedActivationId); | ||
73 | } | ||
74 | } | ||
75 | |||
76 | public synchronized boolean isTraversed(Object stateId) { | ||
77 | return statesAndActivations.containsKey(stateId); | ||
78 | } | ||
79 | |||
80 | @Override | ||
81 | public synchronized Collection<Object> getActivationIds(Object stateId) { | ||
82 | return statesAndActivations.get(stateId); | ||
83 | } | ||
84 | |||
85 | @Override | ||
86 | public synchronized Object getRandomActivationId(Object stateId) { | ||
87 | List<Object> activations = statesAndActivations.get(stateId); | ||
88 | int index = random.nextInt(activations.size()); | ||
89 | return activations.get(index); | ||
90 | } | ||
91 | |||
92 | @Override | ||
93 | public synchronized long getNumberOfStates() { | ||
94 | return statesAndActivations.size(); | ||
95 | } | ||
96 | |||
97 | @Override | ||
98 | public synchronized long getNumberOfTransitions() { | ||
99 | int numberOfTransitions = 0; | ||
100 | for (List<Object> activations : statesAndActivations.values()) { | ||
101 | numberOfTransitions += activations.size(); | ||
102 | } | ||
103 | return numberOfTransitions; | ||
104 | } | ||
105 | |||
106 | } | ||
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/designspace/api/IBacktrackListener.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/designspace/api/IBacktrackListener.java new file mode 100644 index 00000000..5c688276 --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/designspace/api/IBacktrackListener.java | |||
@@ -0,0 +1,7 @@ | |||
1 | package org.eclipse.viatra.dse.designspace.api; | ||
2 | |||
3 | public interface IBacktrackListener { | ||
4 | void forwardWorked(long nanos); | ||
5 | |||
6 | void backtrackWorked(long nanos); | ||
7 | } | ||
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/designspace/api/IDesignSpace.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/designspace/api/IDesignSpace.java new file mode 100644 index 00000000..a1d64bbf --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/designspace/api/IDesignSpace.java | |||
@@ -0,0 +1,27 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Andras Szabolcs Nagy 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.Collection; | ||
12 | |||
13 | public interface IDesignSpace { | ||
14 | |||
15 | Collection<Object> getStates(); | ||
16 | Collection<Object> getRoots(); | ||
17 | void addState(Object sourceStateId, Object firedActivationId, Object newStateId); | ||
18 | |||
19 | boolean isTraversed(Object stateId); | ||
20 | |||
21 | Collection<Object> getActivationIds(Object stateId); | ||
22 | Object getRandomActivationId(Object stateId); | ||
23 | |||
24 | long getNumberOfStates(); | ||
25 | long getNumberOfTransitions(); | ||
26 | |||
27 | } | ||
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 | } | ||