7. Managing ongoing activities
Some work is a finite sequence: set a light, observe the result, return. Other work must keep responding while a goal is active, continuously correct a changing state or protect shared updates. ASTRA provides different mechanisms for these jobs.
Choose the execution model
Requirement |
Mechanism |
Completion |
|---|---|---|
Perform a finite strategy |
Ordinary plan and subgoals |
The plan body finishes |
React only while pursuing a particular goal |
Encapsulated goal with inner plans |
Its goal condition holds or it explicitly ends |
Repeatedly select an action from current state |
Teleo-reactive function |
A plan or the function deactivates it |
Adopt a delayed or periodic achievement goal |
|
Cancel future firings when no longer needed |
Handle periodic tick events |
|
Cancel the timer when no longer needed |
Pause the current plan before continuing |
|
The delay expires |
An encapsulated goal groups an activity’s body and relevant reactions. For example, reactions to device faults may belong inside a commissioning goal, rather than remaining active for the whole application. Its inner plans share the goal’s intention and stop being relevant when the goal ends. Ending the goal can also stop an unfinished inner plan, so put necessary cleanup where it will actually run.
A teleo-reactive function repeatedly selects the first condition/action rule that applies. Write achieved-state cases before corrective cases. An action may be repeated on every cycle while its condition holds, so it must be safe to repeat and quick to execute. An agent can run at most one TR function at a time.
Schedule goals or events
Use Agenda when a deadline or interval should adopt an independent top-level achievement goal. Declare module Agenda agenda; and formula scan(string); in the agent’s module and types declarations. A plan body can schedule work as follows:
agenda.after("initial-scan", 0, scan("hall"));
agenda.every("periodic-scan", 60000, scan("hall"));
Handle it with an ordinary goal plan:
plan +!scan(string Room) {
console.println("scan " + Room);
}
This fragment assumes the agent also declares module Console console;. Times are milliseconds. after accepts a non-negative delay; every requires a positive interval and first fires after one interval. Each firing adopts an independent goal rather than making the scheduling plan wait for completion.
Names are local to the agent. Reusing "periodic-scan" replaces its future firings; agenda.cancel("periodic-scan"); cancels them. Already adopted goals continue in both cases. agenda.scheduled("periodic-scan") tests for future firings, not whether a goal is still running. Agent termination cancels schedules; schedules are not persisted to disk or recovered after a restart.
Periodic Agenda firings are skipped while a matching goal is queued or running. Matching considers the predicate and argument values, even for a goal posted elsewhere or by another schedule name; it also tracks its own in-flight invocation if its arguments change. Different arguments are not mutually excluded, and one-shot after calls do not perform this check. Ground scalar values, lists and nested function terms are supported and copied for each firing; unbound variables, formulas and arbitrary Java objects are not supported.
Choose |
When |
Scheduling behavior |
|---|---|---|
|
Work is an achievement goal, once or periodically |
Fixed-rate periodic goals skip busy matches; no backlog of skipped firings |
|
Work is naturally a |
Periodic tick events have no Agenda-style matching-goal suppression |
|
This plan should pause and then continue |
Delays the calling action; does not create a named schedule or independent goal |
See the Agenda reference for signatures and copying restrictions, and Timer for event-handler syntax.
See continuous selection in a small example
This example uses a counter rather than device I/O so that the repeated execution is visible:
agent ActivityCounter {
module Console console;
module System system;
types counting { formula count(int); }
initial count(0);
plan +!main(list args) {
activate countTo(3);
wait(count(3), 2000);
query(count(3));
console.println("target reached");
system.exit();
}
function countTo(int Target) {
count(Target) -> deactivate
count(int Current) -> {
-count(Current);
+count(Current + 1)
}
}
}
The function advances the count until its first rule applies. activate starts it and allows the main plan to continue to its wait; activation does not itself wait for the function to finish.
For the light controller, a persistent policy could repeatedly compare the desired state with device state and perform a corrective action. Ordinary event plans are often simpler when a sensor already reports changes. Choose continuous selection only when it improves how the policy is expressed.
The sample project includes AgendaDemo, a complete runnable agent that schedules and cancels scans.
Protect concurrent updates
Several intentions can read the same belief before either updates it. A read/remove/add sequence can then lose an update. A named synchronized block protects that sequence when every competing path uses the same lock:
synchronized(counter) {
query(total(int Current));
-total(Current);
+total(Current + 1);
}
Locks belong to one agent. They do not protect another agent’s beliefs or a shared Java service. Coordinate shared environment resources at their own boundary or through a protocol.
An atomic plan gives its intention scheduling priority over other intentions. It is broader than a named critical section and does not provide rollback. Keep it short and avoid waits that require another intention to run. Perception and event-handling phases still occur between its statements.
Make lifetimes explicit
Cancel Agenda schedules, stop timers and continuous control when an activity ends. Keep critical sections short. Avoid a synchronized wait whose completion requires another intention to acquire the same lock. Give encapsulated goals clear completion conditions so that an activity cannot remain active accidentally.
Next: Reusing agent behaviour.