Search in sources :

Example 1 with FailedAssertionException

use of il.ac.bgu.cs.bp.bpjs.execution.tasks.FailedAssertionException in project BPjs by bThink-BGU.

the class BProgram method setup.

/**
 * Sets up the program scope and evaluates the program source.
 *
 * <em>This method can only be called once per instance.</em>
 *
 * @return a snapshot of the program, after source code was executed, and
 *         before any registered b-threads have run.
 * @throws IllegalStateException for repeated calls.
 */
public BProgramSyncSnapshot setup() {
    if (started) {
        throw new IllegalStateException("Program already set up.");
    }
    Set<BThreadSyncSnapshot> bthreads = drainRecentlyRegisteredBthreads();
    if (eventSelectionStrategy == null) {
        eventSelectionStrategy = new SimpleEventSelectionStrategy();
    }
    FailedAssertion failedAssertion = null;
    try {
        Context cx = ContextFactory.getGlobal().enterContext();
        // must use interpreter mode
        cx.setOptimizationLevel(-1);
        initProgramScope(cx);
        if (prependedCode != null) {
            prependedCode.forEach(s -> evaluate(s, "prependedCode"));
        }
        setupProgramScope(programScope);
        bthreads.forEach(bt -> bt.setupScope(programScope));
        if (appendedCode != null) {
            appendedCode.forEach(s -> evaluate(s, "appendedCode"));
        }
    } catch (FailedAssertionException fae) {
        failedAssertion = new FailedAssertion(fae.getMessage(), "---init_code");
    } finally {
        Context.exit();
    }
    started = true;
    return new BProgramSyncSnapshot(this, bthreads, Collections.emptyList(), failedAssertion);
}
Also used : Context(org.mozilla.javascript.Context) FailedAssertionException(il.ac.bgu.cs.bp.bpjs.execution.tasks.FailedAssertionException) SimpleEventSelectionStrategy(il.ac.bgu.cs.bp.bpjs.model.eventselection.SimpleEventSelectionStrategy)

Aggregations

FailedAssertionException (il.ac.bgu.cs.bp.bpjs.execution.tasks.FailedAssertionException)1 SimpleEventSelectionStrategy (il.ac.bgu.cs.bp.bpjs.model.eventselection.SimpleEventSelectionStrategy)1 Context (org.mozilla.javascript.Context)1