Search in sources :

Example 26 with BProgram

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

the class NodeEqualsTest method test1.

@Test
public void test1() throws Exception {
    // Create a program
    final BProgram bprog = new StringBProgram(P1);
    Node[] nodes = new Node[10];
    BEvent eventX = new BEvent("X");
    // Discard initial node, as it has no event, and so can't
    // be used in the even/odd equalities later.
    nodes[0] = Node.getInitialNode(bprog, exSvc).getNextNode(eventX, exSvc);
    for (int i = 1; i < 10; i++) {
        nodes[i] = nodes[i - 1].getNextNode(eventX, exSvc);
    }
    for (int i = 1; i < 10; i += 2) {
        final BThreadSyncSnapshot sysState0 = nodes[0].getSystemState().getBThreadSnapshots().iterator().next();
        final BThreadSyncSnapshot sysStatei_1 = nodes[i - 1].getSystemState().getBThreadSnapshots().iterator().next();
        assertTrue(sysState0.equals(sysStatei_1));
        assertEquals(nodes[0], nodes[i - 1]);
        assertEquals(nodes[1], nodes[i]);
    }
}
Also used : SingleResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram) BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) BThreadSyncSnapshot(il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) Test(org.junit.Test)

Example 27 with BProgram

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

the class BEventSetsJsTest method setUp.

@Before
public void setUp() throws InterruptedException {
    prog = new BProgram() {

        @Override
        protected void setupProgramScope(Scriptable aScope) {
            aScope.put("events", aScope, Context.javaToJS(events, aScope));
            aScope.put("eventSets", aScope, Context.javaToJS(eventSets, aScope));
            aScope.put("test", aScope, Context.javaToJS(BEventSetsJsTest.this, aScope));
            evaluate("eventSets.put(\"esName\", bp.EventSet( 'x', function(e){ " + "bp.log.info('esName');\n" + "bp.log.info(e);\n" + "bp.log.info(e.name);\n" + "bp.log.info( e.name==='Name' );\n" + "return e.name=='Name'; }) );\n" + "eventSets.put(\"esDataObjVizIsViz\", bp.EventSet( 'x',  function(e){ " + "bp.log.info('esDataObjVizIsViz');\n" + "bp.log.info(e);\n" + "return (e.data) ? e.data.viz=='viz' : false; }) );\n" + "eventSets.put(\"esDataIsViz\", bp.EventSet( 'x',  function(e){ " + "bp.log.info('esDataIsViz');\n" + "bp.log.info(e);\n" + "bp.log.info(e.data);\n" + "return e.data==\"viz\"; }) );\n" + "events.put( \"eName\", bp.Event(\"Name\") );\n" + "events.put( \"eNotName\", bp.Event(\"NotName\") );\n" + "events.put( \"eVizObj\", bp.Event(\"aName\", {viz:\"viz\"}) );\n" + "events.put( \"eViz\", bp.Event('aName', 'viz') );", "inline script");
        }
    };
    new BProgramRunner(prog).run();
}
Also used : BProgramRunner(il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner) BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) Scriptable(org.mozilla.javascript.Scriptable) Before(org.junit.Before)

Example 28 with BProgram

use of il.ac.bgu.cs.bp.bpjs.model.BProgram 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 29 with BProgram

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

the class ContinuationGames method main.

public static void main(String[] args) throws Exception {
    // Create a program
    BProgram bprog = new StringBProgram(SRC);
    // Run the top-level code (b-threads are registered but not yet run)
    BProgramSyncSnapshot cur = bprog.setup();
    // Run to first bsync
    cur = cur.start(ExecutorServiceMaker.makeWithName("TEST"));
    // Get a snapshot
    final BThreadSyncSnapshot snapshot = cur.getBThreadSnapshots().iterator().next();
    System.out.println(snapshot);
    // Serialize snapshot
    byte[] serializedContinuationAndScope = null;
    Object bp = null;
    try {
        // need Javascript environment for this, even though we're not executing code per se.
        Context ctxt = Context.enter();
        // first, get bp out of the scope
        Object cnt = snapshot.getContinuation();
        final Scriptable scope = snapshot.getScope();
        for (Scriptable sc = scope; sc != null; sc = sc.getParentScope()) {
            System.out.println("SCOPE START");
            if (sc.has("bp", sc)) {
                bp = sc.get("bp", sc);
                sc.delete("bp");
                System.out.println("bp deleted.");
            }
            if (sc.has("j", sc)) {
                System.out.println("Found j:" + sc.get("j", sc));
            }
            System.out.println("SCOPE END\n");
        }
        // second, serialize
        final Scriptable topLevelScope = ctxt.initSafeStandardObjects();
        try (ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            ScriptableOutputStream outs = new ScriptableOutputStream(bytes, topLevelScope)) {
            outs.writeObject(cnt);
            outs.flush();
            serializedContinuationAndScope = bytes.toByteArray();
        }
        System.out.println("Seriazlied to " + serializedContinuationAndScope.length + " bytes.");
    } finally {
        Context.exit();
    }
    // Run the BThread a few times:
    try {
        Context ctxt = ContextFactory.getGlobal().enterContext();
        // must use interpreter mode
        ctxt.setOptimizationLevel(-1);
        final Scriptable topLevelScope = ctxt.initSafeStandardObjects();
        for (int i = 0; i < 10; i++) {
            try (ScriptableInputStream sis = new ScriptableInputStream(new ByteArrayInputStream(serializedContinuationAndScope), topLevelScope)) {
                // read cnt and scope
                Scriptable cnt2 = (Scriptable) sis.readObject();
                // re-add bp to the scope
                cnt2.getParentScope().put("bp", cnt2.getParentScope(), new BProgramJsProxy(bprog));
                // go - we can push whichever event we want.
                ctxt.resumeContinuation(cnt2, cnt2, new BEvent("e-" + i));
                // this extra run will use the same control flow, but the variable
                // values will be from the previous run. So don't do this :-)
                ctxt.resumeContinuation(cnt2, cnt2, new BEvent("arbitrary/" + i));
            }
        }
        // create three continuation objects that should be the same.
        Scriptable[] cnts = new Scriptable[3];
        for (int i = 0; i < 3; i++) {
            try (ScriptableInputStream sis = new ScriptableInputStream(new ByteArrayInputStream(serializedContinuationAndScope), topLevelScope)) {
                // read cnt and scope
                cnts[i] = (Scriptable) sis.readObject();
            }
        }
        System.out.println(cnts[0].equals(cnts[0]));
        System.out.println(cnts[0].equals(cnts[1]));
        System.out.println("continuationsEq = " + continuationEq((NativeContinuation) cnts[0], (NativeContinuation) cnts[1]));
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) ScriptableInputStream(org.mozilla.javascript.serialize.ScriptableInputStream) ScriptableOutputStream(org.mozilla.javascript.serialize.ScriptableOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Scriptable(org.mozilla.javascript.Scriptable) BProgramJsProxy(il.ac.bgu.cs.bp.bpjs.execution.jsproxy.BProgramJsProxy) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) ByteArrayInputStream(java.io.ByteArrayInputStream) BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) BProgramSyncSnapshot(il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot) BEvent(il.ac.bgu.cs.bp.bpjs.model.BEvent) BThreadSyncSnapshot(il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot)

Example 30 with BProgram

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

the class DfsBProgramVerifierTest method testAAABRun.

@Test
public void testAAABRun() throws Exception {
    BProgram program = new SingleResourceBProgram("AAABTrace.js");
    BProgramRunner rnr = new BProgramRunner(program);
    rnr.addListener(new PrintBProgramRunnerListener());
    InMemoryEventLoggingListener eventLogger = rnr.addListener(new InMemoryEventLoggingListener());
    rnr.run();
    eventLogger.getEvents().forEach(System.out::println);
    assertTrue(eventNamesString(eventLogger.getEvents(), "").matches("^(AAAB)+$"));
}
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) BProgram(il.ac.bgu.cs.bp.bpjs.model.BProgram) StringBProgram(il.ac.bgu.cs.bp.bpjs.model.StringBProgram) SingleResourceBProgram(il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram) InMemoryEventLoggingListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.InMemoryEventLoggingListener) Test(org.junit.Test)

Aggregations

BProgram (il.ac.bgu.cs.bp.bpjs.model.BProgram)36 Test (org.junit.Test)29 SingleResourceBProgram (il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram)23 StringBProgram (il.ac.bgu.cs.bp.bpjs.model.StringBProgram)21 BProgramRunner (il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner)11 BProgramSyncSnapshot (il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot)10 BThreadSyncSnapshot (il.ac.bgu.cs.bp.bpjs.model.BThreadSyncSnapshot)9 BEvent (il.ac.bgu.cs.bp.bpjs.model.BEvent)8 PrintBProgramRunnerListener (il.ac.bgu.cs.bp.bpjs.execution.listeners.PrintBProgramRunnerListener)7 NativeContinuation (org.mozilla.javascript.NativeContinuation)7 BriefPrintDfsVerifierListener (il.ac.bgu.cs.bp.bpjs.analysis.listeners.BriefPrintDfsVerifierListener)5 Scriptable (org.mozilla.javascript.Scriptable)5 InMemoryEventLoggingListener (il.ac.bgu.cs.bp.bpjs.execution.listeners.InMemoryEventLoggingListener)4 DfsBProgramVerifier (il.ac.bgu.cs.bp.bpjs.analysis.DfsBProgramVerifier)3 VerificationResult (il.ac.bgu.cs.bp.bpjs.analysis.VerificationResult)3 BPjsCodeEvaluationException (il.ac.bgu.cs.bp.bpjs.exceptions.BPjsCodeEvaluationException)2 ExecutorServiceMaker (il.ac.bgu.cs.bp.bpjs.internal.ExecutorServiceMaker)2 EventSelectionStrategy (il.ac.bgu.cs.bp.bpjs.model.eventselection.EventSelectionStrategy)2 PrioritizedBSyncEventSelectionStrategy (il.ac.bgu.cs.bp.bpjs.model.eventselection.PrioritizedBSyncEventSelectionStrategy)2 SimpleEventSelectionStrategy (il.ac.bgu.cs.bp.bpjs.model.eventselection.SimpleEventSelectionStrategy)2