public override object Execute(Reasoner ts, Unifier un, ITerm[] args) { CheckArguments(args); ForkData fd = new ForkData(((Atom)args[0]).Equals(aAnd)); Intention currentInt = (Intention)ts.GetCircumstance().GetSelectedIntention(); for (int iPlans = 1; iPlans < args.Length; iPlans++) { Intention i = new ForkIntention(currentInt, fd); fd.AddIntention(i); i.Pop(); // remove the top IM, it will be introduced back later (modified) IntendedPlan im = (IntendedPlan)currentInt.Peek().Clone(); // adds the .join in the plan InternalActionLiteral joinL = new InternalActionLiteral(joinS, ts.GetAgent()); joinL.AddTerm(new ObjectTermImpl(fd)); IPlanBody joinPB = new PlanBodyImpl(BodyType.Body_Type.internalAction, joinL); joinPB.SetBodyNext(im.GetCurrentStep().GetBodyNext()); // adds the argument in the plan (before join) IPlanBody whattoadd = (IPlanBody)args[iPlans].Clone(); whattoadd.Add(joinPB); whattoadd.SetAsBodyTerm(false); im.InsertAsNextStep(whattoadd); im.RemoveCurrentStep(); // remove the .fork i.Push(im); ts.GetCircumstance().AddRunningIntention(i); } return(true); }
public override object Execute(Reasoner reasoner, Unifier un, ITerm[] args) { CheckArguments(args); ILogicalFormula logExpr = (ILogicalFormula)args[0]; IPlanBody whattoadd = null; IEnumerator <Unifier> iu = logExpr.LogicalConsequence(reasoner.GetAgent(), un); if (iu.MoveNext()) { whattoadd = (IPlanBody)args[1].Clone(); un.Compose(iu.Current); } else if (args.Length == 3) { whattoadd = (IPlanBody)args[2].Clone(); } if (whattoadd != null) { IntendedPlan ip = reasoner.GetCircumstance().GetSelectedIntention().Peek(); whattoadd.Add(ip.GetCurrentStep().GetBodyNext()); whattoadd.SetAsBodyTerm(false); ip.InsertAsNextStep(whattoadd); } return(true); }
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 override object Execute(Reasoner reasoner, Unifier un, ITerm[] args) { IntendedPlan ip = reasoner.GetCircumstance().GetSelectedIntention().Peek(); IPlanBody whileia = ip.GetCurrentStep(); // if the IA has a backup unifier, use that (it is an object term) if (args.Length == 2) { // first execution of while CheckArguments(args); // add backup unifier in the IA whileia = new PlanBodyImpl(BodyType.Body_Type.internalAction, (ITerm)whileia.GetBodyTerm().Clone()); // Como uso el Clone de C# lo que clono son object que luego hay que castear... whileia.Add(ip.GetCurrentStep().GetBodyNext()); ((Structure)whileia.GetBodyTerm()).AddTerm(new ObjectTermImpl(un.Clone())); } else if (args.Length == 3) { // restore the unifier of previous iterations Unifier ubak = (Unifier)((IObjectTerm)args[2]).GetObject(); un.Clear(); un.Compose(ubak); } else { throw JasonityException.CreateWrongArgumentNb(this); } ILogicalFormula logExpr = (ILogicalFormula)args[0]; // perform one iteration of the loop IEnumerator <Unifier> iu = logExpr.LogicalConsequence(reasoner.GetAgent(), un); if (iu.MoveNext()) { un.Compose(iu.Current); // add in the current intention: // 1. the body argument and // 2. the while internal action after the execution of the body // (to test the loop again) IPlanBody whattoadd = (IPlanBody)args[1].Clone(); whattoadd.Add(whileia); // the add clones whileia whattoadd.SetAsBodyTerm(false); ip.InsertAsNextStep(whattoadd); } return(true); }
public override object Execute(Reasoner reasoner, Unifier un, ITerm[] args) { IntendedPlan im = reasoner.GetCircumstance().GetSelectedIntention().Peek(); IPlanBody foria = im.GetCurrentStep(); IEnumerator <Unifier> iu; if (args.Length == 2) { // first execution of while CheckArguments(args); // get all solutions for the loop // Note: you should get all solutions here, otherwise a concurrent modification will occur for the iterator ILogicalFormula logExpr = (ILogicalFormula)args[0]; iu = logExpr.LogicalConsequence(reasoner.GetAgent(), un); List <Unifier> allsol = new List <Unifier>(); while (iu.MoveNext()) { allsol.Add(iu.Current); } if (allsol.Count == 0) { return(true); } iu = allsol.GetEnumerator(); foria = new PlanBodyImpl(BodyType.Body_Type.internalAction, (ITerm)foria.GetBodyTerm().Clone()); // Como uso el Clone de C# lo que clono son object que luego hay que castear... foria.Add(im.GetCurrentStep().GetBodyNext()); Structure forstructure = (Structure)foria.GetBodyTerm(); forstructure.AddTerm(new ObjectTermImpl(iu)); // store all solutions forstructure.AddTerm(new ObjectTermImpl(un.Clone())); // backup original unifier } else if (args.Length == 4) { // restore the solutions iu = (IEnumerator <Unifier>)((IObjectTerm)args[2]).GetObject(); } else { throw JasonityException.CreateWrongArgumentNb(this); } un.Clear(); if (iu.MoveNext()) { // add in the current intention: // 1. the body argument of for and // 2. the for internal action after the execution of the body // (to perform the next iteration) un.Compose(iu.Current); IPlanBody whattoadd = (IPlanBody)args[1].Clone(); whattoadd.Add(foria); whattoadd.SetAsBodyTerm(false); im.InsertAsNextStep(whattoadd); } else { un.Compose((Unifier)((IObjectTerm)args[3]).GetObject()); } return(true); }