Search in sources :

Example 16 with PrintBProgramRunnerListener

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

the class TTTButton method main.

public static void main(String[] args) throws InterruptedException {
    // Create a program
    BProgram bprog = new SingleResourceBProgram("BPJSTicTacToe.js");
    bprog.setEventSelectionStrategy(new PrioritizedBSyncEventSelectionStrategy());
    bprog.setDaemonMode(true);
    JFrame f = new TicTacToeGameMain();
    BProgramRunner rnr = new BProgramRunner(bprog);
    rnr.addListener(new PrintBProgramRunnerListener());
    TTTdisplayGame = new TTTDisplayGame(bprog, rnr);
    rnr.run();
    System.out.println("end of run");
}
Also used : PrintBProgramRunnerListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener) BProgramRunner(il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner) JFrame(javax.swing.JFrame) 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) PrioritizedBSyncEventSelectionStrategy(il.ac.bgu.cs.bp.bpjs.model.eventselection.PrioritizedBSyncEventSelectionStrategy)

Example 17 with PrintBProgramRunnerListener

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

the class Mazes method run.

public void run() throws InterruptedException {
    SingleResourceBProgram bprog = prepareProgram();
    BProgramRunner rnr = new BProgramRunner(bprog);
    rnr.addListener(new PrintBProgramRunnerListener());
    rnr.run();
    printMaze(getMaze(bprog));
}
Also used : PrintBProgramRunnerListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener) BProgramRunner(il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner) SingleResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram)

Example 18 with PrintBProgramRunnerListener

use of il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener 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 19 with PrintBProgramRunnerListener

use of il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener 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 20 with PrintBProgramRunnerListener

use of il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener 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)

Aggregations

PrintBProgramRunnerListener (il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener)31 BProgramRunner (il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner)30 Test (org.junit.Test)28 InMemoryEventLoggingListener (il.ac.bgu.cs.bp.bpjs.execution.listeners.InMemoryEventLoggingListener)26 SingleResourceBProgram (il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram)25 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)6 PrioritizedBSyncEventSelectionStrategy (il.ac.bgu.cs.bp.bpjs.model.eventselection.PrioritizedBSyncEventSelectionStrategy)4 StringBProgram (il.ac.bgu.cs.bp.bpjs.model.StringBProgram)3 LoggingEventSelectionStrategyDecorator (il.ac.bgu.cs.bp.bpjs.model.eventselection.LoggingEventSelectionStrategyDecorator)2 SimpleEventSelectionStrategy (il.ac.bgu.cs.bp.bpjs.model.eventselection.SimpleEventSelectionStrategy)2 BProgramRunnerListenerAdapter (il.ac.bgu.cs.bp.bpjs.execution.listeners.BProgramRunnerListenerAdapter)1 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 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1