diff options
author | 2018-05-12 18:19:52 +0200 | |
---|---|---|
committer | 2018-05-12 18:19:52 +0200 | |
commit | 80ce932527acd9f15ba968c7f9e50d4cef7dfb35 (patch) | |
tree | 588e9e5ba4f4da3f9a820799ef1127759295781e /Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.visualisation/src/hu/bme/mit/inf/dslreasoner | |
parent | Project update (diff) | |
download | VIATRA-Generator-80ce932527acd9f15ba968c7f9e50d4cef7dfb35.tar.gz VIATRA-Generator-80ce932527acd9f15ba968c7f9e50d4cef7dfb35.tar.zst VIATRA-Generator-80ce932527acd9f15ba968c7f9e50d4cef7dfb35.zip |
Visualisation queue
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.visualisation/src/hu/bme/mit/inf/dslreasoner')
-rw-r--r-- | Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.visualisation/src/hu/bme/mit/inf/dslreasoner/visualisation/pi2graphviz/VisualisationQueque.xtend | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.visualisation/src/hu/bme/mit/inf/dslreasoner/visualisation/pi2graphviz/VisualisationQueque.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.visualisation/src/hu/bme/mit/inf/dslreasoner/visualisation/pi2graphviz/VisualisationQueque.xtend new file mode 100644 index 00000000..79074bfc --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.visualisation/src/hu/bme/mit/inf/dslreasoner/visualisation/pi2graphviz/VisualisationQueque.xtend | |||
@@ -0,0 +1,82 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.visualisation.pi2graphviz | ||
2 | |||
3 | import guru.nidi.graphviz.engine.Format | ||
4 | import guru.nidi.graphviz.engine.Graphviz | ||
5 | import guru.nidi.graphviz.engine.GraphvizEngine | ||
6 | import guru.nidi.graphviz.engine.GraphvizV8Engine | ||
7 | import guru.nidi.graphviz.model.Graph | ||
8 | import java.io.File | ||
9 | import java.io.IOException | ||
10 | import java.util.concurrent.BlockingQueue | ||
11 | import java.util.concurrent.CompletableFuture | ||
12 | import java.util.concurrent.LinkedBlockingQueue | ||
13 | import org.eclipse.xtend.lib.annotations.Data | ||
14 | |||
15 | class VisualisationQueque { | ||
16 | val BlockingQueue<VisualisationQueueEntry> taskQueue = new LinkedBlockingQueue | ||
17 | var Thread runnerThread = null; | ||
18 | |||
19 | def addTask(Graph document, File targetFile) { | ||
20 | val future = new CompletableFuture<String>(); | ||
21 | taskQueue.add( | ||
22 | new VisualisationQueueEntry(document,targetFile,targetFile.speficyFormat,future) | ||
23 | ) | ||
24 | return future | ||
25 | } | ||
26 | |||
27 | private def Format speficyFormat(File targetFile) { | ||
28 | val ^extension = targetFile.name.split('\\.').last | ||
29 | if(^extension === null) { | ||
30 | throw new IllegalArgumentException('''No file extension specified in path "«targetFile.path»"!''') | ||
31 | } else { | ||
32 | val extensionInUppercase = ^extension.toUpperCase | ||
33 | try { | ||
34 | return Format.valueOf(extensionInUppercase) | ||
35 | } catch(IllegalArgumentException e) { | ||
36 | throw new IllegalArgumentException('''Unsupported extension: ".«extensionInUppercase»"!''',e) | ||
37 | } | ||
38 | } | ||
39 | } | ||
40 | |||
41 | def void start() { | ||
42 | if(runnerThread !== null) { | ||
43 | // do nothing | ||
44 | } else { | ||
45 | runnerThread = new Thread(new Runnable() { | ||
46 | val engine = new GraphvizV8Engine() | ||
47 | |||
48 | override run() { | ||
49 | //println("Visualisation thread started") | ||
50 | while(true) { | ||
51 | val head = taskQueue.take | ||
52 | //println("Task scheduled: "+head.targetFile) | ||
53 | val message = engine.execute(head.document,head.targetFile,head.format) | ||
54 | //println("Task finished: "+head.targetFile) | ||
55 | head.future.complete(message) | ||
56 | } | ||
57 | } | ||
58 | }) | ||
59 | runnerThread.daemon = true | ||
60 | runnerThread.name = "GraphVizVisualiser" | ||
61 | runnerThread.start | ||
62 | } | ||
63 | } | ||
64 | |||
65 | private def execute(GraphvizEngine engine, Graph document, File targetFile, Format format) { | ||
66 | Graphviz.useEngine(engine); | ||
67 | try { | ||
68 | Graphviz.fromGraph(document).render(format).toFile(targetFile) | ||
69 | return null | ||
70 | } catch(IOException e){ | ||
71 | return e.message | ||
72 | } | ||
73 | } | ||
74 | |||
75 | @Data private static class VisualisationQueueEntry { | ||
76 | Graph document | ||
77 | File targetFile | ||
78 | Format format | ||
79 | CompletableFuture<String> future | ||
80 | } | ||
81 | } | ||
82 | |||