Пример #1
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);
                }
            }
Пример #2
0
            public WaitEvent(Trigger te, ILogicalFormula f, Unifier un, Reasoner ts, long timeout, ITerm elapsedTimeTerm)
            {
                this.te              = te;
                this.formula         = f;
                this.un              = un;
                this.ts              = ts;
                c                    = ts.GetCircumstance();
                si                   = c.GetSelectedIntention();
                this.elapsedTimeTerm = elapsedTimeTerm;

                // register listener
                c.AddEventListener(this);

                if (te != null)
                {
                    sEvt = te.ToString();
                }
                else if (formula != null)
                {
                    sEvt = formula.ToString();
                }
                else
                {
                    sEvt = "time" + (timeout);
                }
                sEvt = si.GetID() + "/" + sEvt;
                c.AddPendingIntention(sEvt, si);

                //startTime = System.currentTimeMillis(); //hay que usar el de c# o el de unity? MISTERIO
                startTime = DateTime.Now.Millisecond;
                // Seguramente lo que querramos en C# no son los milisegundos desde el año 1970... sino algo así: Environment.TickCount
                DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

                startTime = (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;

                if (timeout >= 0)
                {
                    Agent.GetExecutor().AddTask(new MyRunnable1(this));

                    //agent.getscheduler().schedule(new runnable()
                    //{
                    //    public void run()
                    //    {
                    //        resume(true);
                    //    }
                    //    }, timeout, timeunit.milliseconds);
                }
            }
Пример #3
0
        public override object Execute(Reasoner ts, Unifier un, ITerm[] args)
        {
            CheckArguments(args);

            suspendIntention = false;

            Circumstance C = ts.GetCircumstance();

            if (args.Length == 0)
            {
                // suspend the current intention
                Intention i = C.GetSelectedIntention();
                suspendIntention = true;
                i.SetSuspended(true);
                C.AddPendingIntention(SELF_SUSPENDED_INT + i.GetID(), i);
                return(true);
            }

            // use the argument to select the intention to suspend.

            Trigger g = new Trigger(TEOperator.add, TEType.achieve, (Literal)args[0]);

            // ** Must test in PA/PI first since some actions (as .suspend) put intention in PI

            // suspending from Pending Actions
            foreach (ExecuteAction a in C.GetPendingActions().Values)
            {
                Intention ia = a.GetIntention();
                if (ia.HasTrigger(g, un))
                {
                    ia.SetSuspended(true);
                    C.AddPendingIntention(SUSPENDED_INT + ia.GetID(), ia);
                }
            }

            // suspending from Pending Intentions
            foreach (Intention ii in C.GetPendingIntentions().Values)
            {
                if (ii.HasTrigger(g, un))
                {
                    ii.SetSuspended(true);
                }
            }

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

            while (itint.MoveNext())
            {
                Intention i = itint.Current;
                if (i.HasTrigger(g, un))
                {
                    i.SetSuspended(true);
                    C.RemoveRunningIntention(i);
                    C.AddPendingIntention(SUSPENDED_INT + i.GetID(), i);
                }
            }

            // suspending the current intention? <-(Esta interrogación ya venía, lo juro)
            Intention ci = C.GetSelectedIntention();

            if (ci != null && ci.HasTrigger(g, un))
            {
                suspendIntention = true;
                ci.SetSuspended(true);
                C.AddPendingIntention(SELF_SUSPENDED_INT + ci.GetID(), ci);
            }

            // suspending G in Events
            int c = 0;
            IEnumerator <Event> ie = C.GetEventsPlusAtomic();

            while (ie.MoveNext())
            {
                Event e = ie.Current;
                ci = e.GetIntention();
                if (un.Unifies(g, e.GetTrigger()) || (ci != null && ci.HasTrigger(g, un)))
                {
                    C.RemoveEvent(e);
                    C.AddPendingEvent(SUSPENDED_INT + e.GetTrigger() + (c++), e);
                    if (ci != null)
                    {
                        ci.SetSuspended(true);
                    }
                }
            }
            return(true);
        }
Пример #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();
                }
            }
        }
Пример #5
0
        override public object Execute(Reasoner ts, Unifier un, ITerm[] args)
        {
            CheckArguments(args);

            Trigger      g = new Trigger(TEOperator.add, TEType.achieve, (Literal)args[0]);
            Circumstance C = ts.GetCircumstance();

            // Search the goal in PI
            IEnumerator <string> ik = C.GetPendingIntentions().Keys.GetEnumerator();

            while (ik.MoveNext())
            {
                string    k = ik.Current;
                Intention i = C.GetPendingIntentions()[k];
                if (i.IsSuspended() && i.HasTrigger(g, un))
                {
                    i.SetSuspended(false);
                    bool notify = true;
                    if (k.StartsWith(SuspendStdLib.SUSPENDED_INT))   // if not SUSPENDED_INT, it was suspended while already in PI, so, do not remove it from PI, just change the suspeded status
                    {
                        ik.Dispose();

                        // add it back in I if not in PA
                        if (!C.GetPendingActions().ContainsKey(i.GetID()))
                        {
                            C.ResumeIntention(i);
                            notify = false; // the resumeIntention already notifies
                        }
                    }

                    // notify meta event listeners
                    if (notify && C.GetListeners() != null)
                    {
                        foreach (ICircumstanceListener el in C.GetListeners())
                        {
                            el.IntentionResumed(i);
                        }
                    }

                    // remove the IA .suspend in case of self-suspend
                    if (k.StartsWith(SuspendStdLib.SELF_SUSPENDED_INT))
                    {
                        i.Peek().RemoveCurrentStep();
                    }

                    //System.out.println("res "+g+" from I "+i.getId());
                }
            }

            // Search the goal in PE
            ik = C.GetPendingEvents().Keys.GetEnumerator();
            while (ik.MoveNext())
            {
                string k = ik.Current;
                if (k.StartsWith(SuspendStdLib.SUSPENDED_INT))
                {
                    Event     e = C.GetPendingEvents()[k];
                    Intention i = e.GetIntention();
                    if (un.Unifies(g, e.GetTrigger()) || (i != null && i.HasTrigger(g, un)))
                    {
                        ik.Dispose();
                        C.AddEvent(e);
                        if (i != null)
                        {
                            i.SetSuspended(false);
                        }
                    }
                }
            }
            return(true);
        }