aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/org.eclipse.viatra.dse/src/org/eclipse/viatra/dse/base/ExplorerThread.java
blob: f2231e5cc29d271ff9b6dc517d926fb9af2d2b92 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2010-2014, Miklos Foldenyi, Andras Szabolcs Nagy, Abel Hegedus, Akos Horvath, Zoltan Ujhelyi and Daniel Varro
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-v20.html.
 * 
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.viatra.dse.base;

import org.apache.log4j.Logger;
import org.eclipse.viatra.dse.api.strategy.interfaces.IStrategy;
import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine;
import org.eclipse.viatra.transformation.evm.api.RuleEngine;

/**
 * This class implements the {@link Runnable} interface, to able to run an exploration strategy in a separate thread. It
 * is also responsible to initialize the exploration, start the exploration (call the {@link IStrategy#explore()}
 * method), catch any exception during exploration and to shutdown the thread correctly.
 * 
 * @author Földenyi Miklos & Nagy Andras Szabolcs
 * 
 */
public class ExplorerThread implements Runnable {

    private final ThreadContext threadContext;

    private IStrategy strategy;

    public ExplorerThread(final ThreadContext context) {
        this.threadContext = context;
        strategy = threadContext.getStrategy();
    }

    /**
     * Signals the {@link IStrategy} instance that execution should be stopped. By contract, the strategy is to
     * stop execution at the next stage of execution where stopping and exiting is appropriate.
     */
    public void stopRunning() {
        strategy.interruptStrategy();
    }

    /**
     * Starts the design space exploration. Returns only when the {@link IStrategy#explore()} method returns.
     */
    public void run() {
        GlobalContext globalContext = threadContext.getGlobalContext();
        try {
            
            threadContext.init();

            strategy.initStrategy(threadContext);

            strategy.explore();

            threadContext.backtrackUntilRoot();

        } catch (Throwable e) {
            Logger.getLogger(IStrategy.class).error("Thread stopped unexpectedly!", e);
            globalContext.registerException(e);
        } finally {
            globalContext.strategyFinished(this);
            dispose();
        }
    }

    /**
     * Disposes of this strategy. Recursively calls dispose on the underlying {@link RuleEngine} and
     * {@link ViatraQueryEngine}. Calling this is only required if the design space exploration was launched in thread, as
     * the underlying engines get collected on the stop of the running {@link Thread}.
     */
    public void dispose() {
        threadContext.getRuleEngine().dispose();
        DseIdPoolHelper.INSTANCE.disposeByThread();
    }

    /**
     * Returns the associated {@link ThreadContext} that houses all the thread specific data about the exploration
     * process, and is also the gateway to the {@link GlobalContext} which stores data relevant to the design space
     * exploration process as a whole.
     * 
     * @return the relevant {@link ThreadContext}.
     */
    public ThreadContext getThreadContext() {
        return threadContext;
    }

}