Programming in SPARK - TU Dresden · The SPARK System SPARK – Stanford Research Institute...

Post on 31-Jul-2020

1 views 0 download

Transcript of Programming in SPARK - TU Dresden · The SPARK System SPARK – Stanford Research Institute...

VIb. Reactive Action Programs 1

Programming in SPARK

VIb. Reactive Action Programs 2

The SPARK System

SPARK – Stanford Research Institute Procedural Agent Realization Kit

Large­scale, practical applications of reactive agent programs

BDI­model, PRS­architecture (like AgentSpeak)

More expressive means to control agents in rich and dynamic domains

VIb. Reactive Action Programs 3

SPARK Syntax

Belief literals :<=> belief atoms or their negation

Actions :<=> primitive actions or names for complex behaviors

Pre­defined belief atoms are:

­ Desire(a) where a action

­ Success(a) where a action

­ Fail(a) where a action

­ Desire() where  belief literal

­ Success() where  belief literal

­ Fail() where  belief literal

VIb. Reactive Action Programs 4

Triggers and Procedures

Triggers are of the form

­ Do(a) where a action

­ Achieve() where  belief literal

­ + where  belief literal

Procedures are of the form

e :         ←

where e  trigger,   formula using belief atoms,   task description

VIb. Reactive Action Programs 5

Programming Language for Task Descriptions

Task Meaning

noop  do nothing

fail  fail

 add the fact to the beliefs

 delete the fact from the beliefs

 perform the action

conclude retract 

do a

achieve   attempt to achieve seq(1, 2)  execute 1 then 2

if(, 1, 2)  if  is true, execute 1, else 2

try( ,  1, 2)  if   succeeds, execute  1, else 2

wait(,  )  wait until  is true, then execute while(, 1, 2)  repeat 1 until  has no solution, then execute 2

VIb. Reactive Action Programs 6

Example Procedures

Do(ForwardMessage(x)) : ¬IsSpam(x)     ← try ( achieve ClassifyMessage(x,y),

  do AddToFolder(x,y),

  do Forward(x)

     )

Do(ForwardMessage(x)) : IsSpam(x)     ← do Delete(x)

VIb. Reactive Action Programs 7

SPARK Interpreter Loop

VIb. Reactive Action Programs 8

Operational Semantics: State Transitions

Semantics of a task description given by a finite automaton

Distinguished states in this automaton:

  ­ s0 initial state

  ­ s+ success state

  ­ s­ failure state

Labeled state transitions

  s   →s'c∣e

VIb. Reactive Action Programs 9

Conditions and Effects

In a labeled state transition s   →s', the

conditions                     are

  ­ presence of belief literal 

  ­ absence of belief literal 

effects                  are

  ­  addition of belief atom 

  ­ removal of belief atom 

Empty conditions and/or effects are simply omitted

c∣e

c=c1 , ... ,cn

e=e1 , ... ,em

c i=

c i=

ei=

ei=

VIb. Reactive Action Programs 10

Example (1)

Do(ForwardMessage(x)) : IsSpam(x) ← do Delete(x)

| Desire(Delete(x))     Success(Delete(x)) |

      Fail(Delete(x)) |

so

s+

VIb. Reactive Action Programs 11

Recursive Construction of State Machine

M(noop) :<=> {s0 → s+}

  M(fail) :<=> {s0 → s­}

M(conclude ) :<=> {s0 → s+}

M(retract ) :<=> {s0 → s+}

M(do a) :<=> {s0    →    s, s      →       s+, s    →    s­}

M(achieve ) :<=> { s0  →  s+, s0      →     s, s     →      s+, s     →    s­}

∣Desire a Success a ∣ Fail a∣

∣ ∣Desire Success ∣ Fail ∣

VIb. Reactive Action Programs 12

Construction of State Machine (cont'd)

M(seq(1, 2)) :<=> M(1){s+/s}   M(2){s0/s}

M(if (, 1, 2)) :<=>     M(1)           M(2)

M(try ( ,  1, 2)) :<=> M(){s+/s1, s­/s2}      M(1){s0/s1}   M(2){s0/s2}

M(wait (, )) :<=> {s0 → s}       M( ){ s0/s}

M(while (, 1, 2)) :<=> {s0 → s}       M(1){s0/s, s+/s}          M(2){s0/s}

VIb. Reactive Action Programs 13

Example (2)

so

         ClassifyMessage(x,y) |               | Desire(AddToFolder(x,y))         Success(AddToFolder(x,y)) |

      ClassifyMessage(x,y) |       Desire(ClassifyMessage(x,y))                                           Fail(AddToFolder(x,y)) |

                                    Success(ClassifyMessage(x,y)) 

                                        Fail(Forward(x)) |

                                      Fail(ClassifyMessage(x,y)) |   

    

                                                              | Desire(Forward(x))               Success(Forward(x)) |

s+

VIb. Reactive Action Programs 14

Derivations

⟨B, D, I, ⟩ state

B set of belief literals

D set of Desire(a), Desire(), or +

I set of triples ⟨d, S, s⟩­ d   desire­ S   state machine instance­ s   current state in S

   {Sense, Select, Act}

⟨B, {}, {}, Sense⟩ initial state

VIb. Reactive Action Programs 15

Selection Functions

SD selects an element from the current desires

SP selects an applicable procedure for a desire

SI selects an element ⟨d, S, s1⟩   I with an allowed state transition s1 → s2

c∣e

VIb. Reactive Action Programs 16

Derivation Rules (1/5)

B ' :<=> B  updated according to sensing result

D '  :<=> D plus all sensed desires

⟨B ,D , I ,Sense ⟩

⟨B ' ,D ' , I ,Select⟩

VIb. Reactive Action Programs 17

Derivation Rules (2/5)

If SD (D) = Desire(x) and there is applicable procedure for Do(a) (if x = a) or   Achieve() (if x = )

where S state machine from SP(Desire(x))

If SD (D) = Desire(x) and no procedure applicable:

where S = {s0  →  s+}

⟨B ,{}, I ,Select⟩

⟨B , {}, I ,Act⟩

⟨B ,D , I ,Select⟩

⟨B ,D \ {Desire x } , I∪{⟨Desire x ,S ,s0 ⟩ } ,Act⟩

⟨B ,D , I ,Select⟩

⟨B ,D \ {Desire x } , I∪{⟨Desire x ,S ,s0 ⟩ } ,Act⟩∣Fail x

VIb. Reactive Action Programs 18

Derivation Rules (3/5)

If SD (D) = + and there is applicable procedure:

where I' :<=> all ⟨+, S, s0⟩ for applicable procedures

If SD (D) = + and no applicable procedure:

⟨B ,D , I ,Select⟩

⟨B ,D \ { } , I ∪ I ' ,Act⟩

⟨B ,D , I ,Select⟩

⟨B ,D \ { } , I ,Select⟩

VIb. Reactive Action Programs 19

Derivation Rules (4/5)

If SI (I) is ⟨d, S, s1⟩ along with s1 → s2:

a) If s2   {∉s+,s­}

b) If d = + and s2   {   s+,s­}:

⟨B ,D ,{},Act⟩

⟨B ,D ,{},Sense ⟩

c∣e

⟨B ,D , I ,Act⟩

⟨B ' ,D ' , I \ {⟨d ,S ,s1 ⟩ }∪{ ⟨d ,S , s2 ⟩ } ,Sense ⟩

⟨B ,D , I ,Act⟩

⟨B ' ,D ' , I \ { ⟨d ,S ,s1 ⟩ } ,Sense ⟩

VIb. Reactive Action Programs 20

Derivation Rules (5/5)

c) If d = Desire(x) and s2 = s+

d) If d = Desire(x) and s2 = s­

⟨B ,D , I ,Act ⟩

⟨B ' ∪{Success x } ,D ' , I \ {⟨d ,S ,s1 ⟩ },Sense⟩

⟨B ,D , I,Act ⟩

⟨B ' ∪{Fail x },D ' , I \ {⟨d ,S ,s1 ⟩ },Sense⟩

VIb. Reactive Action Programs 21

Programming in SPARK: Packages

Package statement    package: sri.foo

Declarations

Definitions

Additional facts/beliefs for the knowledge base

Import statements    importfrom: spark.lang.list Member                         importall: spark.lang.list

Export statements    export: my_sym  

File requirements    requires: foo.bar

Comments     # comment

VIb. Reactive Action Programs 22

Programming in SPARK: Declarations

Variables    $foo

Strings    “this is a string“

Lists    [item1 item2 item3]

Actions                                                                                                                            {defaction (_myPrivateAction +$var1 $var2)                    doc: “_myPrivateAction is local to this file“}

Predicates                                                                                                                       {defpredicate (MyPred $var1 $var2)                            doc: “tests whether $var1 and $var2 are related“}

Functions                                                                                                                         {deffunction (myFun $var1 $var2)                              doc: “applies myFun to $var1 and $var2“}

Constants    {defconstant person23}

VIb. Reactive Action Programs 23

Procedures (Example)

{defprocedure paintHouse_havePaint                            cue: [do: (paintHouse $color)]                            precondition: (HavePaint $color)                          body: [seq: [do: (placeDropCloths)]                                   [do: (applyPaintCoat $color)]                             [do: (cleanUp)]                                     ]

}

VIb. Reactive Action Programs 24

Procedures: Cue

Direct action request    [do: (action_identifier variable*)]

New fact    [newfact: (Happy $person)]

Achievement    [achieve: (Happy $person)]                                                                                 

Indicating argument mode for calling a procedure:                                                                                                                                                                                                    [do: (paintHouse +$color)]                                 [do: (paintHouse +“blue“)]                                                                                            [do: (paintHouse ­“blue“)]

A procedure is relevant when its cue event occurs.                                     There are 3 types of cues:

VIb. Reactive Action Programs 25

Procedures: Precondition

any declared predicate

(True) or   (False)

Logically connected expressions                                                                                                                                                                                                                (and (> 4 $x) (!= $x 2) $person))                           (or (AtHome $person) (AtWork $person) (AtSchool $person))                                                            

Existential quantifier    (exists [$person] (AtHome $person))          

A procedure is applicable when its precondition logical expression is true.        Logical expressions are

VIb. Reactive Action Programs 26

Procedures: Body (1)

Actions    [do: (applyPaint wall red)]

Achievements    [achieve: (ColorOf wall red)]

Noop    [succeed:] or   []

Failure  [fail: outOfPaintError]

Variable binding     [context: (HasParent $x $y)]                                [set: $x (+ 3 4)]

Sequence                                                                                                                                     [seq: [context: (and (MyFavoriteColor $col1)                                    (YourFavoriteColor $col2))]                 [do: (applyPaint wall $col1)]                              [do: (applyPaint door $col2)]

Parallel     [parallel: [do: (rubStomach Bob)]                                     [do: (patHead Bob)]]

VIb. Reactive Action Programs 27

Procedures: Body (2)

Select     [select: (InCar Bob) [do: (drive Bob home)]                         (True)      [do: (talkOnCellPhone Bob)]]      

Wait    [wait: (AtHome Bob) [do: (talkOnCellPhone Bob)]]       

Loop   [while: [$x] (and (MyPet $x) (Hungry $x))                               [do: (feed $x)]]                              

Find all solutions      [forall: [$p] (InCar $p)                                                 [do: (drive $p home)]]

VIb. Reactive Action Programs 28

Procedures: Body (3) – Special Effects

Add to knowledge base     [conclude: (Happy Bill)]                

Remove from knowledge base     [retract: (Scared Bill)]                                   [retractall: [$p] (Scared $p)]       

Remove   [while: [$x] (and (MyPet $x) (Hungry $x))                               [do: (feed $x)]]