use of il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram in project BPjs by bThink-BGU.
the class DynamicBThreadAdditionTest method test.
@Test
public void test() throws InterruptedException {
BProgramRunner sut = new BProgramRunner(new SingleResourceBProgram("dynamicBthreadAddition.js"));
sut.addListener(new PrintBProgramRunnerListener());
InMemoryEventLoggingListener eventLogger = sut.addListener(new InMemoryEventLoggingListener());
sut.run();
Set<String> expected = new HashSet<>(Arrays.asList("e0", "e1", "e2", "e3"));
assertEquals(expected, eventLogger.getEvents().stream().map(BEvent::getName).collect(toSet()));
}
use of il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram 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.SingleResourceBProgram in project BPjs by bThink-BGU.
the class ExternalEventsTest method superStepTest.
@Test
public void superStepTest() throws InterruptedException {
final BEvent in1a = new BEvent("in1a");
final BEvent ext1 = new BEvent("ext1");
final BEvent in1b = new BEvent("in1b");
final SingleResourceBProgram bprog = new SingleResourceBProgram("ExternalEvents.js");
bprog.setEventSelectionStrategy(new LoggingEventSelectionStrategyDecorator(bprog.getEventSelectionStrategy()));
final BProgramRunner sut = new BProgramRunner(bprog);
sut.addListener(new PrintBProgramRunnerListener());
InMemoryEventLoggingListener eventLogger = sut.addListener(new InMemoryEventLoggingListener());
sut.getBProgram().enqueueExternalEvent(ext1);
sut.run();
eventLogger.getEvents().forEach(e -> System.out.println(e));
EventPattern expected = new EventPattern().append(in1a).append(ext1).append(in1b);
assertTrue(expected.matches(eventLogger.getEvents()));
}
use of il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram in project BPjs by bThink-BGU.
the class TicTacToeVerMain method main.
public static void main(String[] args) throws InterruptedException {
// Create a program
BProgram bprog = new SingleResourceBProgram("BPJSTicTacToe.js");
bprog.setEventSelectionStrategy(new PrioritizedBSyncEventSelectionStrategy());
bprog.setDaemonMode(true);
String simulatedPlayer = "bp.registerBThread('XMoves', function() {\n" + "while (true) {\n" + "bsync({ request:[ X(0, 0), X(0, 1), X(0, 2), X(1, 0), \n" + "X(1, 1), X(1, 2), X(2, 0), X(2, 1), X(2, 2) ] }, 10); \n" + "}\n" + "});\n";
// This bthread models the requirement that X never wins.
String xCantWinRequirementBThread = "bp.registerBThread( \"req:NoXWin\", function(){\n" + " bp.sync({waitFor:StaticEvents.XWin});\n" + " bp.ASSERT(false, \"Found a trace where X wins.\");\n" + "});";
bprog.appendSource(simulatedPlayer);
bprog.appendSource(xCantWinRequirementBThread);
// bprog.appendSource(infiBThread);
try {
DfsBProgramVerifier vfr = new DfsBProgramVerifier();
vfr.setDetectDeadlocks(false);
vfr.setMaxTraceLength(70);
// vfr.setDebugMode(true);
vfr.setVisitedNodeStore(new BProgramStateVisitedStateStore(false));
vfr.setProgressListener(new BriefPrintDfsVerifierListener());
final VerificationResult res = vfr.verify(bprog);
if (res.isCounterExampleFound()) {
System.out.println("Found a counterexample");
res.getCounterExampleTrace().forEach(nd -> System.out.println(" " + nd.getLastEvent()));
} else {
System.out.println("No counterexample found.");
}
System.out.printf("Scanned %,d states\n", res.getScannedStatesCount());
System.out.printf("Time: %,d milliseconds\n", res.getTimeMillies());
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
System.out.println("end of run");
}
use of il.ac.bgu.cs.bp.bpjs.model.SingleResourceBProgram in project BPjs by bThink-BGU.
the class Mazes method verify.
public void verify() throws InterruptedException {
SingleResourceBProgram bprog = prepareProgram();
bprog.appendSource(Requirements.eventNotSelected(targetFoundEvent.getName()));
try {
DfsBProgramVerifier vfr = new DfsBProgramVerifier();
vfr.setProgressListener(new BriefPrintDfsVerifierListener());
vfr.setIterationCountGap(10);
vfr.setVisitedNodeStore(new BProgramStateVisitedStateStore(false));
// vfr.setVisitedNodeStore(new ForgetfulVisitedStateStore());
// prevent from detecting cases where we ust hit a wall.
vfr.setDetectDeadlocks(false);
final VerificationResult res = vfr.verify(bprog);
char[][] maze = getMaze(bprog);
printMaze(maze);
if (res.isCounterExampleFound()) {
System.out.println("Found a counterexample");
for (Node nd : res.getCounterExampleTrace()) {
System.out.println(" " + nd.getLastEvent());
if (nd.getLastEvent() != null) {
String name = nd.getLastEvent().getName();
if (name.startsWith("Enter")) {
String loc = name.split("\\(")[1].replace(")", "").trim();
String[] coord = loc.split(",");
int col = Integer.parseInt(coord[0]);
int row = Integer.parseInt(coord[1]);
maze[row][col] = '•';
}
}
}
printMaze(maze);
} else {
System.out.println("No counterexample found.");
}
System.out.printf("Scanned %,d states\n", res.getScannedStatesCount());
System.out.printf("Time: %,d milliseconds\n", res.getTimeMillies());
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
}
Aggregations