Class ConcurrentInternalAction

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

public abstract class ConcurrentInternalAction
extends java.lang.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 an 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)
    }
  }
  
Author:
jomi
  • Field Summary

    Fields
    Modifier and Type Field Description
    private static java.util.concurrent.atomic.AtomicInteger actcount  
  • Constructor Summary

    Constructors
    Constructor Description
    ConcurrentInternalAction()  
  • Method Summary

    Modifier and Type Method Description
    boolean canBeUsedInContext()
    Return true if the internal action can be used in plans' context
    void destroy()  
    java.lang.Object execute​(TransitionSystem ts, Unifier un, Term[] args)
    Executes the internal action.
    void failInt​(TransitionSystem ts, java.lang.String intentionKey)
    fails the intention identified by intentionKey
    Term[] prepareArguments​(Literal body, Unifier un)
    Prepare body's terms to be used in 'execute', normally it consist of cloning and applying each term
    static void resume​(TransitionSystem ts, java.lang.String intentionKey, boolean abort, java.util.List<Term> failAnnots)  
    void resumeInt​(TransitionSystem ts, java.lang.String intentionKey)
    resume the intention identified by intentionKey
    void startInternalAction​(TransitionSystem ts, java.lang.Runnable code)  
    java.lang.String suspendInt​(TransitionSystem ts, java.lang.String basekey, int timeout)
    Suspend the current intention, put it in the PendingIntention (PI) structure and assigns it to a key.
    boolean suspendIntention()
    Returns true if the internal action (IA) should suspend the intention where the IA is called
    abstract void timeout​(TransitionSystem ts, java.lang.String intentionKey)
    called back when some intention should be resumed/failed by timeout

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • actcount

      private static java.util.concurrent.atomic.AtomicInteger actcount
  • 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 java.lang.Object execute​(TransitionSystem ts, Unifier un, Term[] args) throws java.lang.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:
      java.lang.Exception
    • suspendInt

      public java.lang.String suspendInt​(TransitionSystem ts, java.lang.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, java.lang.Runnable code)
    • timeout

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

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

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

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

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