Search in sources :

Example 1 with LoggingEventSelectionStrategyDecorator

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

the class ExternalEventsTest method superStepTest.

@Test
public void superStepTest() throws InterruptedException {
    final BEvent in1a = new BEvent("in1a");
    final BEvent ext1 = new BEvent("ext1");
    final BEvent in1b = new BEvent("in1b");
    final SingleResourceBProgram bprog = new SingleResourceBProgram("ExternalEvents.js");
    bprog.setEventSelectionStrategy(new LoggingEventSelectionStrategyDecorator(bprog.getEventSelectionStrategy()));
    final BProgramRunner sut = new BProgramRunner(bprog);
    sut.addListener(new PrintBProgramRunnerListener());
    InMemoryEventLoggingListener eventLogger = sut.addListener(new InMemoryEventLoggingListener());
    sut.getBProgram().enqueueExternalEvent(ext1);
    sut.run();
    eventLogger.getEvents().forEach(e -> System.out.println(e));
    EventPattern expected = new EventPattern().append(in1a).append(ext1).append(in1b);
    assertTrue(expected.matches(eventLogger.getEvents()));
}
Also used : PrintBProgramRunnerListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener) EventPattern(il.ac.bgu.cs.bp.bpjs.analysis.eventpattern.EventPattern) LoggingEventSelectionStrategyDecorator(il.ac.bgu.cs.bp.bpjs.model.eventselection.LoggingEventSelectionStrategyDecorator) BProgramRunner(il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner) 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 LoggingEventSelectionStrategyDecorator

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

Aggregations

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