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); } }
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); } }
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); }