-
Notifications
You must be signed in to change notification settings - Fork 1
/
RunSchedulingGenerator.xtend
272 lines (238 loc) · 6.89 KB
/
RunSchedulingGenerator.xtend
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package fr.eseo.atlc.example.scheduling.transfo
import com.google.common.collect.BiMap
import com.google.common.collect.HashBiMap
import fr.eseo.atlc.constraints.Constraint
import fr.eseo.atlc.constraints.ConstraintGroup
import fr.eseo.atlc.example.scheduling.Period
import fr.eseo.atlc.example.scheduling.Project
import fr.eseo.atlc.example.scheduling.SchedulingFactory
import fr.eseo.atlc.example.scheduling.SchedulingPackage
import fr.eseo.atlc.example.scheduling.Task
import fr.eseo.atlc.example.scheduling.transfo.JFX.Figure
import fr.eseo.atol.gen.AbstractRule
import fr.eseo.atol.gen.plugin.constraints.solvers.Constraints2Cassowary
import fr.eseo.atol.gen.plugin.constraints.solvers.Constraints2Choco
import java.util.HashMap
import java.util.List
import java.util.Map
import java.util.Random
import javafx.application.Application
import javafx.scene.Node
import javafx.scene.Scene
import javafx.scene.input.KeyEvent
import javafx.scene.input.MouseButton
import javafx.scene.layout.Pane
import javafx.scene.shape.Line
import javafx.scene.shape.Rectangle
import javafx.scene.text.Text
import javafx.stage.Stage
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl
import org.eclipse.papyrus.aof.core.AOFFactory
import org.eclipse.papyrus.aof.core.IBox
import org.eclipse.papyrus.aof.core.IOne
import static extension fr.eseo.aof.exploration.OCLByEquivalence.*
import static extension fr.eseo.atol.gen.JFXUtils.*
class RunSchedulingGenerator extends Application {
val IBox<Node> nodes
val IBox<ConstraintGroup> constraints
var Constraints2Cassowary c2cas
var Constraints2Choco c2cho
val r = new Random
val root = new Pane
val transfo = new Scheduling2JFX
val Project project
val hideLine = AOFFactory.INSTANCE.createOne(false)
val BiMap<EObject, Node> transformationCache = HashBiMap.create
val Map<Rectangle, Period> semesterCache = new HashMap
def static void main(String[] args) {
launch(args)
}
new() {
extension val JFX = transfo.JFXMM
extension val CR = transfo.SchedulingMM
// Init meta model
val rs = new ResourceSetImpl
rs.resourceFactoryRegistry.extensionToFactoryMap.put(
"xmi",
new XMIResourceFactoryImpl
)
rs.packageRegistry.put(
SchedulingPackage.eNS_URI,
SchedulingPackage.eINSTANCE
)
// create a resource
val resource = rs.getFileResource("data.xmi")
// val resource = rs.getFileResource("data-simplified.xmi")
project = resource.contents.get(0) as Project
val NbPeriods = project.numberOfPeriods
project.periods.addAll(
(0..<NbPeriods).map[ n |
SchedulingFactory.eINSTANCE.createPeriod => [
number = n
]
]
)
val refiningTransfo = new SchedulingConstraints
refiningTransfo.refine(resource)
project.tasks.forEach[
period = project.periods.get(0)
]
val courses_pairs = project._tasks.collect[it -> transfo.Task(it).g]
val courses = courses_pairs.collect[value]
val semesters_pairs = project._periods.collect[it -> transfo.Period(it).g]
val semesters = semesters_pairs.collect[value]
courses_pairs.forEach[p |
p.value.nodes.filter(Rectangle).forEach[
transformationCache.put(p.key, it)
]
]
semesters_pairs.forEach[p |
p.value.nodes.filter(Rectangle).forEach[
transformationCache.put(p.key, it)
semesterCache.put(it, p.key)
]
]
val figures = semesters.concat(courses).closure[
it._children
]
nodes = figures.collectMutable[
it?._nodes ?: AbstractRule.emptySequence
]
constraints = figures.collectMutable[
it?._constraints ?: AbstractRule.emptySequence
]
val allConstraints =
constraints
.union(
refiningTransfo.allContents(resource, null).collect[
refiningTransfo.trace.get(it)
].select[it !== null].collectMutable[
if(it === null) {
return AbstractRule.emptySequence
}
AOFFactory.INSTANCE.createSequence(
values.filter(ConstraintGroup) as ConstraintGroup[]
)
]
)
c2cho = new Constraints2Choco(transfo.JFXMM, transfo.SchedulingMM)
c2cho.defaultLowerBound = 0
c2cho.defaultUpperBound = 6
c2cho.apply(allConstraints.select[solver == "choco"].collect[it as Constraint])
c2cho.solve
c2cas = new Constraints2Cassowary(transfo.JFXMM, transfo.SchedulingMM)
c2cas.apply(allConstraints.select[solver == "cassowary"].collect[it as Constraint])
c2cas.solve
nodes.filter[_movable.get].forEach[
onPress
onDrag
]
nodes.filter[_hideable.get].forEach[
visibleProperty.toBox.bind(hideLine)
]
}
// target element accessor for ListPatterns
def static g(List<Object> l) {
l.get(0) as Figure
}
override start(Stage stage) throws Exception {
// Setup GUI stuff
val scene = new Scene(root, 800, 800);
stage.setScene(scene);
stage.setTitle("scheduling");
stage.show();
//Quit app on escape keypress
scene.addEventHandler(KeyEvent.KEY_PRESSED, [KeyEvent t |
switch t.getCode {
case ESCAPE: {
stage.close
}
case SPACE: {
c2cas.debug
c2cho.debug
}
case H: hideLine.toggle
case R: {
val maxX = scene.width * 0.9
val maxY = 600 * 0.9
randomizePositions(maxX, maxY)
}
default: {}
}
]);
root.children.toBox.bind(
nodes.select(Arrow).collect[it as Node]
.concat(
nodes.select(Rectangle).collect[it as Node]
).concat(
nodes.select(Text).collect[it as Node]
).concat(
nodes.select(Line).collect[it as Node]
)
)
root.style = "-fx-background-color: #ffffff;"
randomizePositions(scene.width * 0.9, 350 * 0.9)
}
def randomizePositions(double maxX, double maxY) {
for (n : nodes) {
switch (n) {
Rectangle: {
c2cas.suggestValue(n.xProperty, r.nextDouble * maxX)
c2cas.suggestValue(n.yProperty, r.nextDouble * maxY)
}
}
}
}
def toggle(IOne<Boolean> it) {
set(!get)
}
var dx = 0.0
var dy = 0.0
def onPress(Node it) {
onMousePressed = [e |
val t = e.target
switch t {
Rectangle: {
dx = e.x - t.x
dy = e.y - t.y
e.consume
}
}
]
}
def onDrag(Node it) {
onMouseDragged = [e |
val t = e.target
switch t {
Rectangle:
switch (e.button) {
case MouseButton.PRIMARY: {
val smNodes = semesterCache.filter[k, v | k.contains(e.x, e.y)]
if (!smNodes.empty) {
val p = smNodes.entrySet.get(0)
val s = p.value
val c = transformationCache.inverse.get(t) as Task //TODO; check if exists elsewhere
if (c.period != s) {
c2cho.suggest(c, 'period', s)
if (!c2cho.solve) {
println("No solution found, ignoring suggestion")
}
}
c2cas.suggestValue(t.xProperty, e.x - dx)
c2cas.suggestValue(t.yProperty, e.y - dy)
}
e.consume
}
default: {}
}
}
]
}
def getFileResource(ResourceSet rs, String file) {
rs.getResource(URI.createFileURI(file), true)
}
}