Post on 18-Dec-2015
transcript
Action Rules for Programming Constraint Propagators and
Interactive User Interfaces
Neng-Fa ZhouCUNY Brooklyn College & GC
zhou@sci.brooklyn.cuny.eduwww.sci.brooklyn.cuny.edu/~zhou
Outline of Talk
Motivation Action Rules Programming Constraint Propagators
(www.sci.brooklyn.cuny.edu/~zhou/papers/arule.ps)
CGLIB: Constraint-based Graphics Library(www.sci.brooklyn.cuny.edu/~zhou/papers/acmdoc01.pdf)
Demo of B-Prolog v.6 (www.probp.com)
Motivation
The need for a language for programming reactive systems– Constraint propagators– Event handling– Concurrency– Interactive user interfaces– Agents
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
Agent, ConditionSeq, {EventSet} => ActionSeqAgent, ConditionSeq, {EventSet} => ActionSeq
Syntax
Action Rule
Posting 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– …
Programming 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
Constraint Propagation
X = Y+1 (X,Y in 1..5)X = Y+1 (X,Y in 1..5)
Algorithm Change Algorithm Change PropagationPropagation
Node consistencyNode consistency X=3X=3 X = 3, Y = 2X = 3, Y = 2
Interval consistencyInterval consistency generatedgenerated X in 2..5, Y in 1..4X in 2..5, Y in 1..45 notin X 5 notin X X in 2..4, Y in 1..3X in 2..4, Y in 1..3
Arc consistencyArc consistency 4 notin X4 notin X X in [2,3,5], Y in [1,2,4]X in [2,3,5], Y in [1,2,4]
Events 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
Propagators for aX=bY+c
Forward checking
'aX=bY+c_forward'(A,X,B,Y,C),var(X),var(Y),{ins(X),ins(Y)} => true.
'aX=bY+c_forward'(A,X,B,Y,C),var(X) => T is B*Y+C,Ex is T//A,
(A*Ex=:=T->X = Ex; true). 'aX=bY+c_forward'(A,X,B,Y,C) => T is A*X-C, Ey is T//B,
(B*Ey=:=T->Y is Ey;true).
When either X or Y is instantiated, instantiate the other variable.
Propagators for aX=bY+c
Interval consistency
'aX in bY+c_interval'(A,X,B,Y,C),var(X),var(Y),{minmax(Y)} => 'aX in bY+c_reduce_domain'(A,X,B,Y,C).
'aX in bY+c_interval'(A,X,B,Y,C) => true.
Whenever the bound of Y’s domain is updated,reduce X’s domain to achieve interval consistency.
Propagators for aX=bY+c
Arc consistency
'aX in bY+c_arc'(A,X,B,Y,C),var(X),var(Y),{dom(Y,Ey)} => T is B*Ey+C, Ex is T//A, (A*Ex=:=T -> exclude(X,Ex);true).
'aX in bY+c_arc'(A,X,B,Y,C) => true.
When an element Ey is excluded from Y’s domain,exclude Ey’s counterpart Ex from X’s domain.
Propagator for A1*X1+...+An*Xn+C = 0
'A1*X1+...+An*Xn+C=0'(C,A1,A2,...,An,X1,X2,..,Xn),n_vars_gt(n,2),{ins(X1),minmax(X1),...,ins(Xn),minmax(Xn)}
=> reduce domains of X1,..,Xn to achieve ic. 'A1*X1+...+An*Xn+C=0'(C,A1,A2,...,An,X1,X2,..,Xn)
=> 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 binaryarchive arc consistency.
CGLIB --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.
An Illustrative Example
go:-
cgButton(B), (1)
B^text #= “Hello World!”, (2)
handleButtonClick(B), (3)
cgShow(B). (4)
handleButtonClick(B), (5)
{actionPerformed(B)} (6)
=> (7)
halt. (8)
Attributes of Objects
Base Derivative
x
y
width
height
B is a button
B^windowB^xB^y B^widthB^heightB^text B^colorB^font
B^rightXB^bottomYB^centerXB^centerYB^leftTopPointB^leftBottomPointB^rightTopPointB^rightBottomPointB^center
Constraints
Arithmetic constraints•O1^x +O1^width//2 #= O2^x •O1^centerX #= O2^x
Positioning constraints•cgLeft(O1,O2)
Same property constraints•cgSame([O1,O2],size)•cgSame([O1,O2],width,100)
Constraints (Cont.)
Grid constraints
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),
Constraints (Cont.)
Table constraints
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),
Constraints (Cont.)
Tree constraintsT=node(C0,[node(C1,[node(C3,[]
node(C4,[])]), node(C2,[node(C5,[]),
node(C6,[])])]),cgTree(T,top_down,10,10,centered).
Event Handling
Action rule
Examples
Agent ConditionSeq {Event} => ActionSeq
handleButtonClick(B),{actionPerformed(B)} => …
handleMousePress(O),{mousePressed(O,E)} =>E^x #= X, E^y #= Y,
write(mouse_pressed_at(X,Y)),nl.
handleKeyType(O),{keyTyped(O,E)} =>E^char #= Char, write(Char).
Other Primitives in CGLIB
Packing and showing objects• cgPack(O)& cgShow(O)
Altering and moving objects• O^attr #:= Value• cgScale(O,F)& cgResize(O,W,H)• cgMove(O,X,Y)
Animation• cgSleep(Time)
Generating Java applets• cgStartRecord(FileName) & cgStopRecord
Demo of B-Prolog
B-Prolog (Version 6.0)(www.probp.com)
– Graphical user interfaces
– Animation
– Information visualization
– Constraint satisfaction problems
– Games
Related 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