Title: Action Rules for Programming Constraint Propagators and Interactive User Interfaces
1Action Rules for Programming Constraint
Propagators and Interactive User Interfaces
- Neng-Fa Zhou
- CUNY Brooklyn College GC
- zhou_at_sci.brooklyn.cuny.edu
- www.sci.brooklyn.cuny.edu/zhou
2Outline of Talk
- Motivation
- Action Rules
- Programming Constraint Propagators(www.sci.brookl
yn.cuny.edu/zhou/papers/arule.ps) - CGLIB Constraint-based Graphics
Library(www.sci.brooklyn.cuny.edu/zhou/papers/ac
mdoc01.pdf) - Demo of B-Prolog v.6
- (www.probp.com)
3Motivation
- The need for a language for programming reactive
systems - Constraint propagators
- Event handling
- Concurrency
- Interactive user interfaces
- Agents
4Action Rule
Agent, ConditionSeq, EventSet gt ActionSeq
- Operational semantics
- An agent (sub-goal) C will be suspended if
- C matches Agent,
- ConditionSeq is met, and
- EventSet is not empty
- C is activated whenever an event in EventSet is
posted - ActionSeq is executed when C is activated and
ConditionSeq is met - C is suspended again after ActionSeq is executed
- The next rule is tried if ConditionSeq fails
- C fails if ActionSeq fails
5Posting Eventspost(E)
- event(X,O)
- X suspension variable
- O event object
- Events for programming propagators
- ins(X) X is instantiated
-
- Events for graphics programming
- actionPerformed(O) action performed on O
6Example 1An Echoing Agent
echo_agent(X), event(X,Message) gt
write(Message),nl.
7Example 2freeze(X,G)
freeze(X,G), var(X), ins(X) gt
true. freeze(X,G) gt call(G).
8Programming Constraint Propagators
- Action rules are expressive
- Propagators for maintaining node, interval, arc
consistency - Propagators for global constraints
- Action rules are efficient
- B-Prolog is competitive with Ilog solver and
GNU-Prolog
9Constraint Propagation
X Y1 (X,Y in 1..5)
Algorithm Change Propagation
Node consistency X3 X 3, Y 2 Interval
consistency generated X in 2..5, Y in 1..4 5
notin X X in 2..4, Y in 1..3 Arc
consistency 4 notin X X in 2,3,5, Y in 1,2,4
10Events for Programming Propagators
- ins(X)
- X is instantiated
- minmax(X)
- The bound of X is updated
- dom(X,E)
- An inner element E has been excluded from X
11Propagators for aXbYc
'aXbYc_forward'(A,X,B,Y,C),var(X),var(Y), ins(
X),ins(Y) gt true. 'aXbYc_forward'(A,X,B,Y,
C),var(X) gt T is BYC,Ex is T//A,
(AExT-gtX Ex true). 'aXbYc_forward'(
A,X,B,Y,C) gt T is AX-C, Ey is T//B,
(BEyT-gtY is Eytrue).
When either X or Y is instantiated, instantiate
the other variable.
12Propagators for aXbYc
'aX in bYc_interval'(A,X,B,Y,C),var(X),var(Y),
minmax(Y) gt 'aX in
bYc_reduce_domain'(A,X,B,Y,C). 'aX in
bYc_interval'(A,X,B,Y,C) gt true.
Whenever the bound of Ys domain is
updated, reduce Xs domain to achieve interval
consistency.
13Propagators for aXbYc
'aX in bYc_arc'(A,X,B,Y,C),var(X),var(Y), dom(
Y,Ey) gt T is BEyC,
Ex is T//A, (AExT -gt
exclude(X,Ex)true). 'aX in
bYc_arc'(A,X,B,Y,C) gt true.
When an element Ey is excluded from Ys
domain, exclude Eys counterpart Ex from Xs
domain.
14Propagator for A1X1...AnXnC 0
'A1X1...AnXnC0'(C,A1,A2,...,An,X1,X2,..,Xn)
, n_vars_gt(n,2), ins(X1),minmax(X1),...,ins(Xn
),minmax(Xn) gt reduce domains of
X1,..,Xn to achieve ic. 'A1X1...AnXnC0'(
C,A1,A2,...,An,X1,X2,..,Xn) gt
nary_to_binary(NewC,B1,B2,Y1,Y2),
call_binary_propagator(NewC,B1,Y1,B2,Y2).
When the constraint has more than 2
variables, achieve interval consistency. When
the constraint is binary archive arc consistency.
15Performance Evaluation
Fast
B-Prolog Ilog GNU-Prolog
1
2
CHIP
Eclipse Sicstus Mozart Oz
4
Slow
16CGLIB --A Constraint-based Graphics Library
- Features
- Use constraints to specify the layouts of objects
- Use action rules to specify interactions
- Implementation
- Implemented in B-Prolog, Java, and C
- Applications
- Drawing editors, interactive user interfaces,
document authoring, animation, information
visualization, intelligent agents, and games.
17An Illustrative Example
go- cgButton(B), (1)
Btext Hello World!, (2) handleButtonClick(
B), (3) cgShow(B). (4) handleButtonClick(B
), (5) actionPerformed(B) (6) gt
(7) halt. (8)
18Objects
- cgButton(X)
- Create a button X
- cgButton(X1,,Xn)
- Create a list of buttons
19Attributes of Objects
B is a button
y
Bwindow Bx By Bwidth Bheight Btext
Bcolor Bfont
BrightX BbottomY BcenterX BcenterY BleftTopPo
int BleftBottomPoint BrightTopPoint BrightBotto
mPoint Bcenter
x
height
width
20Constraints
- Arithmetic constraints
- O1x O1width//2 O2x
- O1centerX O2x
- Positioning constraints
- cgLeft(O1,O2)
- Same property constraints
- cgSame(O1,O2,size)
- cgSame(O1,O2,width,100)
21Constraints (Cont.)
cgGrid(_, S1,_, S2,S3,S4,
_,S5,_)
cgGrid(Bc,Bdiv,Bmul,Bsub, B7,B8, B9,
Badd, B4,B5, B6, Badd, B1,B2, B3,
Beq, B0,B0, Bdot,Beq,1,1),
22Constraints (Cont.)
cgTable(Larc,Arc,Button,Lbutton,
Lcheckbox, Checkbox, Choice, Lchoice,
Lcircle,Circle,Image,Limage,
Llabel,Label,Line,Lline,
Llist,List,Oval,Loval,
Lrrectangle,Rrectangle,Rectangle,Lrectangle,
Lpolygon,Polygon,Square,Lsquare,
Lstar,Star,TextArea,Ltextarea,
Ltextfield,TextField,TextBox,Ltextbox,
Ltriangle,Triangle,_,_,20,20),
23Constraints (Cont.)
Tnode(C0,node(C1,node(C3,
node(C4,)), node(C2,node(C5,),
node(C6,))), cgTree(T,top_down,10,10,centered)
.
24Event Handling
Agent ConditionSeq Event gt ActionSeq
handleButtonClick(B),actionPerformed(B) gt
handleMousePress(O),mousePressed(O,E)
gt Ex X, Ey Y, write(mouse_pressed
_at(X,Y)),nl. handleKeyType(O),keyTyped(O,E)
gt Echar Char, write(Char).
25Other Primitives in CGLIB
- Packing and showing objects
- cgPack(O) cgShow(O)
- Altering and moving objects
- Oattr Value
- cgScale(O,F) cgResize(O,W,H)
- cgMove(O,X,Y)
- Animation
- cgSleep(Time)
- Generating Java applets
- cgStartRecord(FileName) cgStopRecord
26Demo of B-Prolog
- B-Prolog (Version 6.0)(www.probp.com)
- Graphical user interfaces
- Animation
- Information visualization
- Constraint satisfaction problems
- Games
27Related Work
- Programming Constraint Propagators
- Indexicals (GNU-Prolog Sicstus)
- Delay constructs
- Attribute variables (Eclipse)
- Constraint handling rules
- Graphics programming
- Postscript Latex
- Every detail must be specified
- Java Tcl/Tk
- Layout managers are helpful for certain layouts
- Constraints Constraint programming
- Sketchpad, ThinkLab, Amulet, SubArctic