280 likes | 302 Views
Action Rules for Programming Constraint Propagators and Interactive User Interfaces. Neng-Fa Zhou CUNY Brooklyn College & GC zhou@sci.brooklyn.cuny.edu www.sci.brooklyn.cuny.edu/~zhou. Outline of Talk. Motivation Action Rules
E N D
Action Rulesfor Programming Constraint Propagators and Interactive User Interfaces Neng-Fa Zhou CUNY Brooklyn College & GC zhou@sci.brooklyn.cuny.edu www.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
Action Rule • Syntax Agent, ConditionSeq, {EventSet} => 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 • Cfails if ActionSeq fails
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 • …
Example 1An Echoing Agent echo_agent(X), {event(X,Message)} => write(Message),nl.
Example 2freeze(X,G) freeze(X,G), var(X), {ins(X)} => true. freeze(X,G) => call(G).
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) Algorithm Change Propagation Node consistency X=3 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]
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 binary archive arc consistency.
Performance Evaluation Fast B-Prolog Ilog GNU-Prolog 1 2 CHIP Eclipse Sicstus Mozart Oz 4 Slow
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)
cgButton(X) Create a button X cgButton([X1,…,Xn]) Create a list of buttons Objects
Base Derivative Attributes of Objects B is a button y B^window B^x B^y B^width B^height B^text B^color B^font B^rightX B^bottomY B^centerX B^centerY B^leftTopPoint B^leftBottomPoint B^rightTopPoint B^rightBottomPoint B^center x height width
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 constraints T=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