Search in sources :

Example 26 with BProgramRunner

use of il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner 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 27 with BProgramRunner

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

the class BProgramTest method testPrependSource.

@Test
public void testPrependSource() throws InterruptedException {
    String prependedCode = "var e1=bp.Event(\"1\");\n" + "var e2=bp.Event(\"2\");\n" + "var e3=bp.Event(\"3\");\n" + "bp.log.info('Prepended code evaluated');";
    String coreSource = "bp.registerBThread(function() {\n" + "    bsync( {request: e1} );\n" + "    bsync( {request: e2} );\n" + "    bsync( {request: e3} );\n" + "});" + "bp.log.info('Source code evaluated');";
    BProgramRunner rnr = new BProgramRunner();
    rnr.addListener(new PrintBProgramRunnerListener());
    InMemoryEventLoggingListener el = rnr.addListener(new InMemoryEventLoggingListener());
    BProgram sut = new StringBProgram(coreSource);
    sut.prependSource(prependedCode);
    rnr.setBProgram(sut);
    rnr.run();
    assertEquals(asList("1", "2", "3"), el.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) InMemoryEventLoggingListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.InMemoryEventLoggingListener) Test(org.junit.Test)

Example 28 with BProgramRunner

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

the class BProgramTest method testPrependAndAppendSource.

@Test
public void testPrependAndAppendSource() throws InterruptedException {
    System.out.println("\n\ntestPrependAndAppendSource");
    String prependedCode = "var e1=bp.Event(\"1\");\n" + "var e2=bp.Event(\"2\");\n" + "var e3=bp.Event(\"3\");\n" + "bp.log.info('prepended code evaluated');\n" + "var o1=1; var o2=1; var o3=1;";
    String coreSource = "bp.registerBThread(function() {\n" + "    bsync( {request: e1} );\n" + "    bsync( {request: e2} );\n" + "    bsync( {request: e3} );\n" + "});" + "bp.log.info('main code evaluated');\n" + "var o2=2; var o3=2;";
    String appendedSource = "bp.registerBThread(function(){\n" + "   bsync({waitFor: e2});\n" + "   bsync({request: bp.Event(\"2a\"),\n" + "            block: e3});\n" + "});\n" + "bp.log.info('appended code evaluated');\n" + "var o3=3;";
    BProgramRunner rnr = new BProgramRunner();
    rnr.addListener(new PrintBProgramRunnerListener());
    InMemoryEventLoggingListener el = rnr.addListener(new InMemoryEventLoggingListener());
    BProgram sut = new StringBProgram(coreSource);
    sut.prependSource(prependedCode);
    sut.appendSource(appendedSource);
    rnr.setBProgram(sut);
    rnr.run();
    assertEquals(asList("1", "2", "2a", "3"), el.getEvents().stream().map(BEvent::getName).collect(toList()));
    assertEquals(Optional.of(1.0), sut.getFromGlobalScope("o1", Number.class));
    assertEquals(Optional.of(2.0), sut.getFromGlobalScope("o2", Number.class));
    assertEquals(Optional.of(3.0), sut.getFromGlobalScope("o3", Number.class));
    System.out.println("-- testPrependAndAppendSource DONE");
}
Also used : PrintBProgramRunnerListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener) BProgramRunner(il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner) InMemoryEventLoggingListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.InMemoryEventLoggingListener) Test(org.junit.Test)

Example 29 with BProgramRunner

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

the class BProgramTest method testGlobalScopeAccessors.

@Test
public void testGlobalScopeAccessors() throws InterruptedException {
    BProgram sut = new SingleResourceBProgram("RandomProxy.js");
    new BProgramRunner(sut).run();
    assertEquals(1000.0, sut.getFromGlobalScope("TOTAL_COUNT", Double.class).get(), 3);
    assertFalse(sut.getFromGlobalScope("does-not-exist", Double.class).isPresent());
}
Also used : BProgramRunner(il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner) Test(org.junit.Test)

Example 30 with BProgramRunner

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

the class PutInContextTest method testPutInContext_global.

@Test
public void testPutInContext_global() throws InterruptedException {
    BProgram sut = new StringBProgram("bp.log.info(obj);var len=obj.length;");
    String outsideObject = "I'm an outside object";
    sut.putInGlobalScope("obj", outsideObject);
    new BProgramRunner(sut).run();
    assertEquals(sut.getFromGlobalScope("len", Long.class).get().longValue(), outsideObject.length());
}
Also used : BProgramRunner(il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner) Test(org.junit.Test)

Aggregations

BProgramRunner (il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner)43 Test (org.junit.Test)38 PrintBProgramRunnerListener (il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener)30 SingleResourceBProgram (il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram)29 InMemoryEventLoggingListener (il.ac.bgu.cs.bp.bpjs.execution.listeners.InMemoryEventLoggingListener)26 BEvent (il.ac.bgu.cs.bp.bpjs.model.BEvent)13 EventPattern (il.ac.bgu.cs.bp.bpjs.analysis.eventpattern.EventPattern)10 BProgram (il.ac.bgu.cs.bp.bpjs.model.BProgram)10 StringBProgram (il.ac.bgu.cs.bp.bpjs.model.StringBProgram)4 PrioritizedBSyncEventSelectionStrategy (il.ac.bgu.cs.bp.bpjs.model.eventselection.PrioritizedBSyncEventSelectionStrategy)4 Scriptable (org.mozilla.javascript.Scriptable)3 BProgramRunnerListenerAdapter (il.ac.bgu.cs.bp.bpjs.execution.listeners.BProgramRunnerListenerAdapter)2 LoggingEventSelectionStrategyDecorator (il.ac.bgu.cs.bp.bpjs.model.eventselection.LoggingEventSelectionStrategyDecorator)2 SimpleEventSelectionStrategy (il.ac.bgu.cs.bp.bpjs.model.eventselection.SimpleEventSelectionStrategy)2 Before (org.junit.Before)2 FailedAssertion (il.ac.bgu.cs.bp.bpjs.model.FailedAssertion)1 EventSelectionStrategy (il.ac.bgu.cs.bp.bpjs.model.eventselection.EventSelectionStrategy)1 EventSet (il.ac.bgu.cs.bp.bpjs.model.eventsets.EventSet)1 JsEventSet (il.ac.bgu.cs.bp.bpjs.model.eventsets.JsEventSet)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1