Beispiel #1
0
 /* returns: >0 the intention was changed
  *           1 = intention must continue running
  *           2 = fail event was generated and added in C.E
  *           3 = simply removed without event
  */
 public virtual int DropDesire(Intention i, Trigger g, Reasoner rs, Unifier un)
 {
     if (i != null && i.DropDesire(g, un))
     {
         if (rs.HasDesireListener())
         {
             foreach (Desire gl in rs.GetDesiresListeners())
             {
                 gl.DesireFinished(g, Desire.FinishStates.achieved);
             }
         }
         //continue the intention
         if (!i.IsFinished())
         {
             if (rs.GetCircumstance().GetSelectedIntention() != i)
             {
                 i.Peek().RemoveCurrentStep();
             }
             rs.ApplyClrInt(i);
             return(1);
         }
         else
         {
             rs.ApplyClrInt(i);
             return(3);
         }
     }
     return(0);
 }
Beispiel #2
0
            public void Run()
            {
                try
                {
                    // add SI again in C.I if (1) it was not removed (2) is is not running (by some other reason) -- but this test does not apply to atomic intentions --, and (3) this wait was not dropped
                    if (c.RemovePendingIntention(sEvt) == si && (si.IsAtomic() || !c.HasRunningIntention(si)) && !dropped)
                    {
                        if (stopByTimeout && (te != null || formula != null) && elapsedTimeTerm == null)
                        {
                            // fail the .wait by timeout
                            if (si.IsSuspended())
                            { // if the intention was suspended by .suspend
                                IPlanBody body = si.Peek().GetPlan().GetBody();
                                body.Add(1, new PlanBodyImpl(BodyType.Body_Type.internalAction, new InternalActionLiteral(".fail")));
                                c.AddPendingIntention(SuspendStdLib.SUSPENDED_INT + si.GetID(), si);
                            }
                            else
                            {
                                rs.GenerateDesireDeletion(si, (List <ITerm>)JasonityException.CreateBasicErrorAnnots("wait_timeout", "timeout in .wait"));
                            }
                        }
                        else if (!si.IsFinished())
                        {
                            si.Peek().RemoveCurrentStep();

                            if (elapsedTimeTerm != null)
                            {
                                long elapsedTime = DateTime.Now.Millisecond - startTime;
                                //long elapsedTime = System.currentTimeMillis() - startTime;
                                un.Unifies(elapsedTimeTerm, new NumberTermImpl(elapsedTime));
                            }
                            if (si.IsSuspended())
                            { // if the intention was suspended by .suspend
                                c.AddPendingIntention(SuspendStdLib.SUSPENDED_INT + si.GetID(), si);
                            }
                            else
                            {
                                c.ResumeIntention(si);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    //rs.getLogger().log(Level.SEVERE, "Error at .wait thread", e);
                }
            }
Beispiel #3
0
            void Find()
            {
                switch (curStep)
                {
                case Step.selEvt:
                    curStep = Step.evt;     // set next step

                    // we need to check the selected event in this cycle (already removed from E)
                    if (C.GetSelectedEvent() != null)
                    {
                        Trigger   t = C.GetSelectedEvent().GetTrigger();
                        Intention i = C.GetSelectedEvent().GetIntention();
                        if (i != Intention.emptyInt && !i.IsFinished())
                        {
                            t = (Trigger)t.Capply(i.Peek().GetUnif());
                        }
                        solution = un.Clone();
                        if (solution.UnifiesNoUndo(teFromL, t))
                        {
                            return;
                        }
                    }
                    Find();
                    return;

                case Step.evt:
                    if (evtIterator == null)
                    {
                        evtIterator = C.GetEventsPlusAtomic();
                    }

                    if (evtIterator.MoveNext())
                    {
                        Event     ei = evtIterator.Current;
                        Trigger   t  = ei.GetTrigger();
                        Intention i  = ei.GetIntention();
                        if (i != Intention.emptyInt && !i.IsFinished())
                        {
                            t = t.Capply(i.Peek().GetUnif());
                        }
                        solution = un.Clone();
                        if (solution.UnifiesNoUndo(teFromL, t))
                        {
                            return;
                        }
                    }
                    else
                    {
                        curStep = Step.useIntends;     // set next step
                    }
                    Find();
                    return;

                case Step.useIntends:
                    if (intendInterator == null)
                    {
                        intendInterator = AllIntentions(C, l, intAsTerm, un);
                    }

                    if (intendInterator.MoveNext())
                    {
                        solution = intendInterator.Current;
                        return;
                    }
                    else
                    {
                        curStep = Step.end;     // set next step
                    }
                    break;

                case Step.end:
                    break;
                }
                solution = null; //nothing found
            }
Beispiel #4
0
        public void FindDesireAndDrop(Reasoner rs, Literal l, Unifier un)
        {
            Trigger      g   = new Trigger(TEOperator.add, TEType.achieve, l);
            Circumstance C   = rs.GetCircumstance();
            Unifier      bak = un.Clone();

            IEnumerator <Intention> itinit = C.GetRunningIntentionsPlusAtomic();

            while (itinit.MoveNext())
            {
                Intention i = itinit.Current;
                if (DropDesire(i, g, rs, un) > 1)
                {
                    C.DropRunningIntention(i);
                    un = bak.Clone();
                }
            }

            // dropping the current intention?
            DropDesire(C.GetSelectedIntention(), g, rs, un);
            un = bak.Clone();

            //dropping G in Events
            IEnumerator <Event> ie = C.GetEventsPlusAtomic();

            while (ie.MoveNext())
            {
                Event e = ie.Current;
                //Test in the intention
                Intention i = e.GetIntention();
                int       r = DropDesire(i, g, rs, un);
                if (r > 0)
                {
                    C.RemoveEvent(e);
                    if (r == 1)
                    {
                        C.ResumeIntention(i);
                    }
                    un = bak.Clone();
                }
                else
                {
                    //Test in the event
                    Trigger t = e.GetTrigger();
                    if (i != Intention.emptyInt && !i.IsFinished())
                    {
                        t = t.Capply(i.Peek().GetUnif());
                    }
                    if (un.Unifies(g, t))
                    {
                        DropDesireInEvent(rs, e, i);
                        un = bak.Clone();
                    }
                }
            }

            //dropping G in Pending Events
            foreach (string ek in C.GetPendingEvents().Keys)
            {
                //Test in the intention
                Event     e = C.GetPendingEvents()[ek];
                Intention i = e.GetIntention();
                int       r = DropDesire(i, g, rs, un);
                if (r > 0)
                {
                    C.RemovePendingEvent(ek);
                    if (r == 1)
                    {
                        C.ResumeIntention(i);
                    }
                    un = bak.Clone();
                }
                else
                {
                    //test in the event
                    Trigger t = e.GetTrigger();
                    if (i != Intention.emptyInt && !i.IsFinished())
                    {
                        t = t.Capply(i.Peek().GetUnif());
                    }
                    if (un.Unifies(g, t))
                    {
                        DropDesireInEvent(rs, e, i);
                        un = bak.Clone();
                    }
                }
            }

            //Dropping from pending Actions
            foreach (ExecuteAction a in C.GetPendingActions().Values)
            {
                Intention i = a.GetIntention();
                int       r = DropDesire(i, g, rs, un);
                if (r > 0)                            //i was changed
                {
                    C.RemovePendingAction(i.GetID()); // remove i from PA
                    if (r == 1)                       // i must continue running
                    {
                        C.ResumeIntention(i);         // and put the intention back in I
                    }                                 // if r > 1, the event was generated and i will be back soon
                    un = bak.Clone();
                }
            }

            //Dropping from pending intentions
            foreach (Intention i in C.GetPendingIntentions().Values)
            {
                int r = DropDesire(i, g, rs, un);
                if (r > 0)
                {
                    C.RemovePendingIntention(i.GetID());
                    if (r == 1)
                    {
                        C.ResumeIntention(i);
                    }
                    un = bak.Clone();
                }
            }
        }