use of il.ac.bgu.cs.bp.bpjs.model.eventselection.SimpleEventSelectionStrategy in project BPjs by bThink-BGU.
the class EventsWithDataTest method testEventsWithData.
@Test
public void testEventsWithData() throws Exception {
BProgram bpr = new SingleResourceBProgram("EventsWithData.js", "programName", new SimpleEventSelectionStrategy(99l));
BProgramRunner rnr = new BProgramRunner(bpr);
rnr.addListener(new PrintBProgramRunnerListener());
InMemoryEventLoggingListener events = rnr.addListener(new InMemoryEventLoggingListener());
rnr.run();
assertEquals(Arrays.asList("e1", "e2", "e1e2"), events.getEvents().stream().map(BEvent::getName).collect(toList()));
}
use of il.ac.bgu.cs.bp.bpjs.model.eventselection.SimpleEventSelectionStrategy 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.eventselection.SimpleEventSelectionStrategy in project BPjs by bThink-BGU.
the class BProgram method setup.
/**
* Sets up the program scope and evaluates the program source.
*
* <em>This method can only be called once per instance.</em>
*
* @return a snapshot of the program, after source code was executed, and
* before any registered b-threads have run.
* @throws IllegalStateException for repeated calls.
*/
public BProgramSyncSnapshot setup() {
if (started) {
throw new IllegalStateException("Program already set up.");
}
Set<BThreadSyncSnapshot> bthreads = drainRecentlyRegisteredBthreads();
if (eventSelectionStrategy == null) {
eventSelectionStrategy = new SimpleEventSelectionStrategy();
}
FailedAssertion failedAssertion = null;
try {
Context cx = ContextFactory.getGlobal().enterContext();
// must use interpreter mode
cx.setOptimizationLevel(-1);
initProgramScope(cx);
if (prependedCode != null) {
prependedCode.forEach(s -> evaluate(s, "prependedCode"));
}
setupProgramScope(programScope);
bthreads.forEach(bt -> bt.setupScope(programScope));
if (appendedCode != null) {
appendedCode.forEach(s -> evaluate(s, "appendedCode"));
}
} catch (FailedAssertionException fae) {
failedAssertion = new FailedAssertion(fae.getMessage(), "---init_code");
} finally {
Context.exit();
}
started = true;
return new BProgramSyncSnapshot(this, bthreads, Collections.emptyList(), failedAssertion);
}
Aggregations