use of il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner 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.execution.BProgramRunner 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()));
}
use of il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner 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");
}
use of il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner in project BPjs by bThink-BGU.
the class BProgramTest method testGlobalScopeAccessors.
@Test
public void testGlobalScopeAccessors() throws InterruptedException {
BProgram sut = new SingleResourceBProgram("RandomProxy.js");
new BProgramRunner(sut).run();
assertEquals(1000.0, sut.getFromGlobalScope("TOTAL_COUNT", Double.class).get(), 3);
assertFalse(sut.getFromGlobalScope("does-not-exist", Double.class).isPresent());
}
use of il.ac.bgu.cs.bp.bpjs.execution.BProgramRunner in project BPjs by bThink-BGU.
the class PutInContextTest method testPutInContext_global.
@Test
public void testPutInContext_global() throws InterruptedException {
BProgram sut = new StringBProgram("bp.log.info(obj);var len=obj.length;");
String outsideObject = "I'm an outside object";
sut.putInGlobalScope("obj", outsideObject);
new BProgramRunner(sut).run();
assertEquals(sut.getFromGlobalScope("len", Long.class).get().longValue(), outsideObject.length());
}
Aggregations