Class ConcurrentInternalAction

java.lang.Object
jason.asSemantics.ConcurrentInternalAction
All Implemented Interfaces:
InternalAction

public abstract class ConcurrentInternalAction extends Object implements 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 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 interface InternalAction
    • 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 interface InternalAction
    • prepareArguments

      public Term[] prepareArguments(Literal body, Unifier un)
      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 interface InternalAction
    • execute

      public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception
      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 interface InternalAction
      Throws:
      Exception
    • suspendInt

      public String suspendInt(TransitionSystem ts, String basekey, int timeout)
      Suspend the current intention, put it in the PendingIntention (PI) structure and assigns it to a key.
      Parameters:
      ts - the "engine" of the agent
      basekey - 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

      public void startInternalAction(TransitionSystem ts, Runnable code)
    • timeout

      public abstract void timeout(TransitionSystem ts, String intentionKey)
      called back when some intention should be resumed/failed by timeout
    • resumeInt

      public void resumeInt(TransitionSystem ts, String intentionKey)
      resume the intention identified by intentionKey
    • failInt

      public void failInt(TransitionSystem ts, String intentionKey)
      fails the intention identified by intentionKey
    • resume

      public static void resume(TransitionSystem ts, String intentionKey, boolean abort, List<Term> failAnnots)
    • destroy

      public void destroy() throws Exception
      Specified by:
      destroy in interface InternalAction
      Throws:
      Exception