-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from bThink-BGU/0.0.4
b_thread deepcopy bug fix and external event support
- Loading branch information
Showing
11 changed files
with
68 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from bppy import * | ||
|
||
class External(BEvent): | ||
pass | ||
|
||
|
||
any_external = EventSet(lambda event: isinstance(event, External)) | ||
|
||
@b_thread | ||
def add_external(): | ||
b_program.enqueue_external_event(External("A")) | ||
b_program.enqueue_external_event(External("B")) | ||
b_program.enqueue_external_event(External("C")) | ||
while True: | ||
yield {waitFor: All()} | ||
|
||
@b_thread | ||
def act_on_external(): | ||
while True: | ||
# triggers external events if exists, else terminates the bprogram | ||
event = yield {block: All(), waitFor: any_external} | ||
yield {request: BEvent(event.name)} | ||
|
||
|
||
if __name__ == "__main__": | ||
b_program = BProgram(bthreads=[add_external(), act_on_external()], | ||
event_selection_strategy=SimpleEventSelectionStrategy(), | ||
listener=PrintBProgramRunnerListener()) | ||
b_program.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from bppy import * | ||
import pygame | ||
# import pygame | ||
|
||
H = 250 | ||
W = 250 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 6 additions & 71 deletions
77
bppy/model/event_selection/experimental_smt_event_selection_strategy.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,7 @@ | ||
import sys | ||
import os | ||
from bppy.model.event_selection.event_selection_strategy import EventSelectionStrategy | ||
from bppy.utils.z3helper import * | ||
from bppy.model.event_selection.smt_event_selection_strategy import SMTEventSelectionStrategy | ||
|
||
|
||
class Request: | ||
def __init__(self, variables=None): | ||
self.variables = variables | ||
|
||
|
||
class WaitFor: | ||
pass | ||
|
||
|
||
class Block: | ||
pass | ||
|
||
|
||
class ExperimentalSMTEventSelectionStrategy(EventSelectionStrategy): | ||
|
||
def __init__(self, debug=False): | ||
self.debug = debug | ||
|
||
def is_satisfied(self, event, statement): | ||
return is_true(event.eval(statement.get(WaitFor, true))) | ||
|
||
def select(self, statements): | ||
sl = Solver() | ||
|
||
# Collect the blocking constraints | ||
blocking = Not(Or([l.get(Block, false) for l in statements])) | ||
sl.add(blocking) | ||
|
||
# A dictionary that maps each variable to a disjunction of all the requests for the variable | ||
requests = {} | ||
|
||
# Make sure that the model assigns a value to each of the variables that appear in any of the statements | ||
for l in statements: | ||
for v in getVariables(l.get(Block, false)): | ||
requests[v] = false | ||
|
||
for v in getVariables(l.get(Request, false)): | ||
requests[v] = false | ||
|
||
# Fill the requests dictionary | ||
for l in statements: | ||
for key, req in l.items(): | ||
if isinstance(key, Request) or key == Request: | ||
if key == Request or key.variables is None: | ||
vars = getVariables(req) | ||
else: | ||
vars = key.variables | ||
|
||
for v in vars: | ||
requests[v] = Or(requests.get(v, false), req) | ||
|
||
# Add each of the disjunctionin requests to the solver | ||
for r in requests.values(): | ||
sl.add(r) | ||
|
||
# Use this to debug the assertions | ||
if self.debug: | ||
print(">> Block=", simplify(blocking)) | ||
print(">> Requests=") | ||
for key, value in requests.items(): | ||
print(">>\t {} -> {}".format(key,simplify(value))) | ||
|
||
if sl.check() == sat: | ||
return sl.model() | ||
else: | ||
return None | ||
def ExperimentalSMTEventSelectionStrategy(): | ||
from warnings import warn | ||
warn('Class ExperimentalSMTEventSelectionStrategy is deprecated. Returned SMTEventSelectionStrategy instead.', | ||
DeprecationWarning, stacklevel=2) | ||
return SMTEventSelectionStrategy() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
bppy/model/event_selection/solver_based_event_selection_strategy.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from bppy.model.event_selection.event_selection_strategy import EventSelectionStrategy | ||
from abc import abstractmethod | ||
|
||
class SolverBasedEventSelectionStrategy(EventSelectionStrategy): | ||
@abstractmethod | ||
def select(self, statements, external_events_queue=[]): | ||
pass | ||
|
||
@abstractmethod | ||
def is_satisfied(self, event, statement): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters