Search in sources :

Example 1 with EventSelectionStrategy

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

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

the class BProgramRunner method run.

@Override
public void run() {
    try {
        // setup bprogram and runtime parts.
        execSvc = ExecutorServiceMaker.makeWithName("BProgramRunner-" + instanceNum);
        failedAssertion = null;
        listeners.forEach(l -> l.starting(bprog));
        BProgramSyncSnapshot cur = bprog.setup();
        cur.getBThreadSnapshots().forEach(sn -> listeners.forEach(l -> l.bthreadAdded(bprog, sn)));
        // start it
        listeners.forEach(l -> l.started(bprog));
        cur = cur.start(execSvc);
        boolean go = true;
        if (!cur.isStateValid()) {
            failedAssertion = cur.getFailedAssertion();
            listeners.forEach(l -> l.assertionFailed(bprog, failedAssertion));
            go = false;
        }
        // while snapshot not empty, select an event and get the next snapshot.
        while ((!cur.noBThreadsLeft()) && go) {
            // see which events are selectable
            Set<BEvent> possibleEvents = bprog.getEventSelectionStrategy().selectableEvents(cur.getStatements(), cur.getExternalEvents());
            if (possibleEvents.isEmpty()) {
                // No events available or selection. Terminate or wait for external one (in daemon mode).
                if (bprog.isDaemonMode()) {
                    listeners.forEach(l -> l.superstepDone(bprog));
                    // and now we wait.
                    BEvent next = bprog.takeExternalEvent();
                    if (next == null) {
                        // program is not a daemon anymore.
                        go = false;
                    } else {
                        cur.getExternalEvents().add(next);
                    }
                } else {
                    // Ending the program - no selectable event.
                    listeners.forEach(l -> l.superstepDone(bprog));
                    go = false;
                }
            } else {
                // we can select some events - select one and advance.
                Optional<EventSelectionResult> res = bprog.getEventSelectionStrategy().select(cur.getStatements(), cur.getExternalEvents(), possibleEvents);
                if (res.isPresent()) {
                    EventSelectionResult esr = res.get();
                    if (!esr.getIndicesToRemove().isEmpty()) {
                        // the event selection affcted the external event queue.
                        List<BEvent> updatedExternals = new ArrayList<>(cur.getExternalEvents());
                        esr.getIndicesToRemove().stream().sorted(reverseOrder()).forEach(idxObj -> updatedExternals.remove(idxObj.intValue()));
                        cur = cur.copyWith(updatedExternals);
                    }
                    listeners.forEach(l -> l.eventSelected(bprog, esr.getEvent()));
                    cur = cur.triggerEvent(esr.getEvent(), execSvc, listeners);
                    if (!cur.isStateValid()) {
                        failedAssertion = cur.getFailedAssertion();
                        listeners.forEach(l -> l.assertionFailed(bprog, failedAssertion));
                        go = false;
                    }
                } else {
                    // edge case: we can select events, but we didn't. Might be a bug in the EventSelectionStrategy.
                    go = false;
                }
            }
        }
        listeners.forEach(l -> l.ended(bprog));
    } catch (InterruptedException itr) {
        System.err.println("BProgramRunner interrupted: " + itr.getMessage());
    } finally {
        execSvc.shutdown();
    }
}
Also used : BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) BProgramSyncSnapshot(il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot) EventSelectionStrategy(il.ac.bgu.cs.bp.bpjs.model.eventselection.EventSelectionStrategy) Set(java.util.Set) ArrayList(java.util.ArrayList) List(java.util.List) FailedAssertion(il.ac.bgu.cs.bp.bpjs.model.FailedAssertion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collections.reverseOrder(java.util.Collections.reverseOrder) Optional(java.util.Optional) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) BProgramRunnerListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.BProgramRunnerListener) EventSelectionResult(il.ac.bgu.cs.bp.bpjs.model.eventselection.EventSelectionResult) ExecutorService(java.util.concurrent.ExecutorService) ExecutorServiceMaker(il.ac.bgu.cs.bp.bpjs.internal.ExecutorServiceMaker) EventSelectionResult(il.ac.bgu.cs.bp.bpjs.model.eventselection.EventSelectionResult) ArrayList(java.util.ArrayList) BProgramSyncSnapshot(il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent)

Aggregations

BProgram (il.ac.bgu.cs.bp.bpjs.model.BProgram)2 EventSelectionStrategy (il.ac.bgu.cs.bp.bpjs.model.eventselection.EventSelectionStrategy)2 BProgramRunner (il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner)1 BProgramRunnerListener (il.ac.bgu.cs.bp.bpjs.execution.listeners.BProgramRunnerListener)1 PrintBProgramRunnerListener (il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener)1 ExecutorServiceMaker (il.ac.bgu.cs.bp.bpjs.internal.ExecutorServiceMaker)1 BEvent (il.ac.bgu.cs.bp.bpjs.model.BEvent)1 BProgramSyncSnapshot (il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot)1 FailedAssertion (il.ac.bgu.cs.bp.bpjs.model.FailedAssertion)1 EventSelectionResult (il.ac.bgu.cs.bp.bpjs.model.eventselection.EventSelectionResult)1 LoggingEventSelectionStrategyDecorator (il.ac.bgu.cs.bp.bpjs.model.eventselection.LoggingEventSelectionStrategyDecorator)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 ArrayList (java.util.ArrayList)1 Collections.reverseOrder (java.util.Collections.reverseOrder)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1