Package jason.asSemantics
Class ConcurrentInternalAction
java.lang.Object
jason.asSemantics.ConcurrentInternalAction
- All Implemented Interfaces:
InternalAction
This class can be used in place of DefaultInternalAction to create an IA that
suspend the intention while it is being executed.
Example: a plan may ask something to a user and wait the answer.
If DefaultInternalAction is used for that, all the agent thread is blocked until
the answer. With ConcurrentInternalAction, only the intention using the IA is
suspended. See demos/gui/gui1.
The code of an internal action that extends this class looks like:
public class ...... extends ConcurrentInternalAction { public Object execute(final TransitionSystem ts, Unifier un, final Term[] args) throws Exception { .... final String key = suspendInt(ts, "gui", 5000); // suspend the intention (max 5 seconds) startInternalAction(ts, new Runnable() { // to not block the agent thread, start a thread that performs the task and resume the intention latter public void run() { .... the code of the IA ..... if ( ... all Ok ...) resumeInt(ts, key); // resume the intention with success else failInt(ts, key); // resume the intention with fail } }); ... } public void timeout(TransitionSystem ts, String intentionKey) { // called back when the intention should be resumed/failed by timeout (after 5 seconds in this example) ... this method have to decide what to do with actions finished by timeout: resume or fail ... to call resumeInt(ts,intentionKey) or failInt(ts, intentionKey) } }
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Return true if the internal action can be used in plans' contextvoid
destroy()
execute
(TransitionSystem ts, Unifier un, Term[] args) Executes the internal action.void
failInt
(TransitionSystem ts, String intentionKey) fails the intention identified by intentionKeyTerm[]
prepareArguments
(Literal body, Unifier un) Prepare body's terms to be used in 'execute', normally it consist of cloning and applying each termstatic void
resume
(TransitionSystem ts, String intentionKey, boolean abort, List<Term> failAnnots) void
resumeInt
(TransitionSystem ts, String intentionKey) resume the intention identified by intentionKeyvoid
startInternalAction
(TransitionSystem ts, Runnable code) suspendInt
(TransitionSystem ts, String basekey, int timeout) Suspend the current intention, put it in the PendingIntention (PI) structure and assigns it to a key.boolean
Returns true if the internal action (IA) should suspend the intention where the IA is calledabstract void
timeout
(TransitionSystem ts, String intentionKey) called back when some intention should be resumed/failed by timeout
-
Constructor Details
-
ConcurrentInternalAction
public ConcurrentInternalAction()
-
-
Method Details
-
canBeUsedInContext
public boolean canBeUsedInContext()Description copied from interface:InternalAction
Return true if the internal action can be used in plans' context- Specified by:
canBeUsedInContext
in interfaceInternalAction
-
suspendIntention
public boolean suspendIntention()Description copied from interface:InternalAction
Returns true if the internal action (IA) should suspend the intention where the IA is called- Specified by:
suspendIntention
in interfaceInternalAction
-
prepareArguments
Description copied from interface:InternalAction
Prepare body's terms to be used in 'execute', normally it consist of cloning and applying each term- Specified by:
prepareArguments
in interfaceInternalAction
-
execute
Description copied from interface:InternalAction
Executes the internal action. It should return a Boolean or an Iterator. A true boolean return means that the IA was successfully executed. An Iterator result means that there is more than one answer for this IA (e.g. see member internal action). - Specified by:
execute
in interfaceInternalAction
- Throws:
Exception
-
suspendInt
Suspend the current intention, put it in the PendingIntention (PI) structure and assigns it to a key.- Parameters:
ts
- the "engine" of the agentbasekey
- the base key to form final key used to get the intention back from PI (e.g. "moise", "cartago", ...)timeout
- the max time the intention will be in PI, the value 0 means until "resume"- Returns:
- the final key used to store the intention in PI, this key is used the resume the intention
-
startInternalAction
-
timeout
called back when some intention should be resumed/failed by timeout -
resumeInt
resume the intention identified by intentionKey -
failInt
fails the intention identified by intentionKey -
resume
public static void resume(TransitionSystem ts, String intentionKey, boolean abort, List<Term> failAnnots) -
destroy
- Specified by:
destroy
in interfaceInternalAction
- Throws:
Exception
-