Search in sources :

Example 1 with SimpleEventSelectionStrategy

use of il.ac.bgu.cs.bp.bpjs.model.eventselection.SimpleEventSelectionStrategy in project BPjs by bThink-BGU.

the class EventsWithDataTest method testEventsWithData.

@Test
public void testEventsWithData() throws Exception {
    BProgram bpr = new SingleResourceBProgram("EventsWithData.js", "programName", new SimpleEventSelectionStrategy(99l));
    BProgramRunner rnr = new BProgramRunner(bpr);
    rnr.addListener(new PrintBProgramRunnerListener());
    InMemoryEventLoggingListener events = rnr.addListener(new InMemoryEventLoggingListener());
    rnr.run();
    assertEquals(Arrays.asList("e1", "e2", "e1e2"), events.getEvents().stream().map(BEvent::getName).collect(toList()));
}
Also used : PrintBProgramRunnerListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener) BProgramRunner(il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner) SimpleEventSelectionStrategy(il.ac.bgu.cs.bp.bpjs.model.eventselection.SimpleEventSelectionStrategy) SingleResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram) BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) SingleResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram) InMemoryEventLoggingListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.InMemoryEventLoggingListener) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) Test(org.junit.Test)

Example 2 with SimpleEventSelectionStrategy

use of il.ac.bgu.cs.bp.bpjs.model.eventselection.SimpleEventSelectionStrategy in project BPjs by bThink-BGU.

the class BPJsCliRunner method main.

public static void main(String[] args) {
    if (args.length == 0) {
        printUsageAndExit();
    }
    BProgram bpp = new BProgram("BPjs") {

        @Override
        protected void setupProgramScope(Scriptable scope) {
            for (String arg : args) {
                if (arg.equals("-")) {
                    println(" [READ] stdin");
                    try {
                        evaluate(System.in, "stdin");
                    } catch (EvaluatorException ee) {
                        logScriptExceptionAndQuit(ee, arg);
                    }
                } else {
                    if (!arg.startsWith("-")) {
                        Path inFile = Paths.get(arg);
                        println(" [READ] %s", inFile.toAbsolutePath().toString());
                        if (!Files.exists(inFile)) {
                            println("File %s does not exit", inFile.toAbsolutePath().toString());
                            System.exit(-2);
                        }
                        try (InputStream in = Files.newInputStream(inFile)) {
                            evaluate(in, arg);
                        } catch (EvaluatorException ee) {
                            logScriptExceptionAndQuit(ee, arg);
                        } catch (IOException ex) {
                            println("Exception while processing " + arg + ": " + ex.getMessage());
                            Logger.getLogger(BPJsCliRunner.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
                println(" [ OK ] %s", arg);
            }
        }

        private void logScriptExceptionAndQuit(EvaluatorException ee, String arg) {
            println("Error in source %s:", arg);
            println(ee.details());
            println("line: " + ee.lineNumber() + ":" + ee.columnNumber());
            println("source: " + ee.lineSource());
            System.exit(-3);
        }
    };
    SimpleEventSelectionStrategy sess = new SimpleEventSelectionStrategy();
    EventSelectionStrategy ess = switchPresent("-v", args) ? new LoggingEventSelectionStrategyDecorator(sess) : sess;
    bpp.setEventSelectionStrategy(ess);
    BProgramRunner bpr = new BProgramRunner(bpp);
    if (!switchPresent("-v", args)) {
        bpr.addListener(new PrintBProgramRunnerListener());
    }
    bpr.run();
}
Also used : Path(java.nio.file.Path) LoggingEventSelectionStrategyDecorator(il.ac.bgu.cs.bp.bpjs.model.eventselection.LoggingEventSelectionStrategyDecorator) BProgramRunner(il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner) SimpleEventSelectionStrategy(il.ac.bgu.cs.bp.bpjs.model.eventselection.SimpleEventSelectionStrategy) InputStream(java.io.InputStream) IOException(java.io.IOException) Scriptable(org.mozilla.javascript.Scriptable) EventSelectionStrategy(il.ac.bgu.cs.bp.bpjs.model.eventselection.EventSelectionStrategy) SimpleEventSelectionStrategy(il.ac.bgu.cs.bp.bpjs.model.eventselection.SimpleEventSelectionStrategy) PrintBProgramRunnerListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener) EvaluatorException(org.mozilla.javascript.EvaluatorException) BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram)

Example 3 with SimpleEventSelectionStrategy

use of il.ac.bgu.cs.bp.bpjs.model.eventselection.SimpleEventSelectionStrategy 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

SimpleEventSelectionStrategy (il.ac.bgu.cs.bp.bpjs.model.eventselection.SimpleEventSelectionStrategy)3 BProgramRunner (il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner)2 PrintBProgramRunnerListener (il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener)2 BProgram (il.ac.bgu.cs.bp.bpjs.model.BProgram)2 InMemoryEventLoggingListener (il.ac.bgu.cs.bp.bpjs.execution.listeners.InMemoryEventLoggingListener)1 FailedAssertionException (il.ac.bgu.cs.bp.bpjs.execution.tasks.FailedAssertionException)1 BEvent (il.ac.bgu.cs.bp.bpjs.model.BEvent)1 SingleResourceBProgram (il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram)1 EventSelectionStrategy (il.ac.bgu.cs.bp.bpjs.model.eventselection.EventSelectionStrategy)1 LoggingEventSelectionStrategyDecorator (il.ac.bgu.cs.bp.bpjs.model.eventselection.LoggingEventSelectionStrategyDecorator)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1 Test (org.junit.Test)1 Context (org.mozilla.javascript.Context)1 EvaluatorException (org.mozilla.javascript.EvaluatorException)1 Scriptable (org.mozilla.javascript.Scriptable)1