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]);
}
}
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();
}
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();
}
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();
}
}
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)+$"));
}
Aggregations