diff options
Diffstat (limited to 'Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/base/ExplorerThread.java')
-rw-r--r-- | Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/base/ExplorerThread.java | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/base/ExplorerThread.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/base/ExplorerThread.java new file mode 100644 index 00000000..f2231e5c --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/base/ExplorerThread.java | |||
@@ -0,0 +1,88 @@ | |||
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.base; | ||
10 | |||
11 | import org.apache.log4j.Logger; | ||
12 | import org.eclipse.viatra.dse.api.strategy.interfaces.IStrategy; | ||
13 | import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine; | ||
14 | import org.eclipse.viatra.transformation.evm.api.RuleEngine; | ||
15 | |||
16 | /** | ||
17 | * This class implements the {@link Runnable} interface, to able to run an exploration strategy in a separate thread. It | ||
18 | * is also responsible to initialize the exploration, start the exploration (call the {@link IStrategy#explore()} | ||
19 | * method), catch any exception during exploration and to shutdown the thread correctly. | ||
20 | * | ||
21 | * @author Földenyi Miklos & Nagy Andras Szabolcs | ||
22 | * | ||
23 | */ | ||
24 | public class ExplorerThread implements Runnable { | ||
25 | |||
26 | private final ThreadContext threadContext; | ||
27 | |||
28 | private IStrategy strategy; | ||
29 | |||
30 | public ExplorerThread(final ThreadContext context) { | ||
31 | this.threadContext = context; | ||
32 | strategy = threadContext.getStrategy(); | ||
33 | } | ||
34 | |||
35 | /** | ||
36 | * Signals the {@link IStrategy} instance that execution should be stopped. By contract, the strategy is to | ||
37 | * stop execution at the next stage of execution where stopping and exiting is appropriate. | ||
38 | */ | ||
39 | public void stopRunning() { | ||
40 | strategy.interruptStrategy(); | ||
41 | } | ||
42 | |||
43 | /** | ||
44 | * Starts the design space exploration. Returns only when the {@link IStrategy#explore()} method returns. | ||
45 | */ | ||
46 | public void run() { | ||
47 | GlobalContext globalContext = threadContext.getGlobalContext(); | ||
48 | try { | ||
49 | |||
50 | threadContext.init(); | ||
51 | |||
52 | strategy.initStrategy(threadContext); | ||
53 | |||
54 | strategy.explore(); | ||
55 | |||
56 | threadContext.backtrackUntilRoot(); | ||
57 | |||
58 | } catch (Throwable e) { | ||
59 | Logger.getLogger(IStrategy.class).error("Thread stopped unexpectedly!", e); | ||
60 | globalContext.registerException(e); | ||
61 | } finally { | ||
62 | globalContext.strategyFinished(this); | ||
63 | dispose(); | ||
64 | } | ||
65 | } | ||
66 | |||
67 | /** | ||
68 | * Disposes of this strategy. Recursively calls dispose on the underlying {@link RuleEngine} and | ||
69 | * {@link ViatraQueryEngine}. Calling this is only required if the design space exploration was launched in thread, as | ||
70 | * the underlying engines get collected on the stop of the running {@link Thread}. | ||
71 | */ | ||
72 | public void dispose() { | ||
73 | threadContext.getRuleEngine().dispose(); | ||
74 | DseIdPoolHelper.INSTANCE.disposeByThread(); | ||
75 | } | ||
76 | |||
77 | /** | ||
78 | * Returns the associated {@link ThreadContext} that houses all the thread specific data about the exploration | ||
79 | * process, and is also the gateway to the {@link GlobalContext} which stores data relevant to the design space | ||
80 | * exploration process as a whole. | ||
81 | * | ||
82 | * @return the relevant {@link ThreadContext}. | ||
83 | */ | ||
84 | public ThreadContext getThreadContext() { | ||
85 | return threadContext; | ||
86 | } | ||
87 | |||
88 | } | ||