private InternalActionLiteral CheckInternalActionsInContext(ILogicalFormula f, Agent ag) { if (f != null) { if (f.GetType() == typeof(InternalActionLiteral)) { InternalActionLiteral ial = (InternalActionLiteral)f; if (!ial.GetIA(ag).CanBeUsedInContext()) { return(ial); } } else if (f.GetType() == typeof(LogExpr)) { LogExpr le = (LogExpr)f; InternalActionLiteral ial = CheckInternalActionsInContext(le.GetLHS(), ag); if (ial != null) { return(ial); } if (!le.IsUnary()) { return(CheckInternalActionsInContext(le.GetRHS(), ag)); } } } return(null); }
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); }
private void FixAgInIAandFunctions(Agent a, Literal l) { // if l is internal action/function if (l.GetType() == typeof(InternalActionLiteral)) { ((InternalActionLiteral)l).SetIA(null); // reset the IA in the literal, the IA there will be updated next getIA call } if (l.GetType() == typeof(ArithFunctionTerm)) { ((ArithFunctionTerm)l).SetAgent(a); } if (l.GetType() == typeof(Rule)) { ILogicalFormula f = ((Rule)l).GetBody(); if (f.GetType() == typeof(Literal)) { FixAgInIAandFunctions(a, (Literal)f); } } for (int i = 0; i < l.GetArity(); i++) { if (l.GetTerm(i).GetType() == typeof(Literal)) { FixAgInIAandFunctions(a, (Literal)l.GetTerm(i)); } } }
public void SetContext(ILogicalFormula le) { context = le; if (Literal.LTrue.Equals(le)) { context = null; } }
public static ILogicalFormula ParseFormula(string sExpr) { as2j parser = new as2j(new StringReader(sExpr)); ILogicalFormula l = (ILogicalFormula)parser.Log_expr(); if (parser.getNextToken().kind != as2jConstants.EOF) { throw new ParseException("Expected <EOF> after " + l + " for parameter '" + sExpr + "'"); } return(l); }
public MyRunnable2(Circumstance c, Intention si, string sEvt, bool dropped, bool stopByTimeout, Trigger te, ILogicalFormula formula, ITerm elapsedTimeTerm, Reasoner rs, Unifier un, long startTime) { this.c = c; this.si = si; this.sEvt = sEvt; this.dropped = dropped; this.stopByTimeout = stopByTimeout; this.te = te; this.formula = formula; this.elapsedTimeTerm = elapsedTimeTerm; this.rs = rs; this.un = un; this.startTime = startTime; }
public override object Execute(Reasoner ts, Unifier un, ITerm[] args) { CheckArguments(args); ILogicalFormula logExpr = args[0] as ILogicalFormula; int n = 0; IEnumerator <Unifier> iu = logExpr.LogicalConsequence(ts.GetAgent(), un); while (iu.Current != null) { iu.MoveNext(); n++; } return(un.Unifies(args[1], new NumberTermImpl(n))); }
public override object Execute(Reasoner ts, Unifier un, ITerm[] args) { CheckArguments(args); ILogicalFormula logExpr = args[1] as ILogicalFormula; IEnumerator <Unifier> iu = logExpr.LogicalConsequence(ts.GetAgent(), un); if (iu.Current != null) { return(un.Unifies(args[0], Literal.LTrue)); } else { return(un.Unifies(args[0], Literal.LFalse)); } }
override public object Execute(Reasoner ts, Unifier un, ITerm[] args) { CheckArguments(args); ITerm var = args[0]; ILogicalFormula logExpr = (ILogicalFormula)args[1]; ISet <ITerm> all = new HashSet <ITerm>(); IEnumerator <Unifier> iu = logExpr.LogicalConsequence(ts.GetAgent(), un); while (iu.MoveNext()) { all.Add(var.CApply(iu.Current)); } return(un.Unifies(args[2], SetToList(all))); }
public override object Execute(Reasoner reasoner, Unifier un, ITerm[] args) { CheckArguments(args); ITerm var = args[0]; ILogicalFormula logExpr = (ILogicalFormula)args[1]; IListTerm all = new ListTermImpl(); IListTerm tail = all; IEnumerator <Unifier> iu = logExpr.LogicalConsequence(reasoner.GetAgent(), un); while (iu.MoveNext()) { tail = tail.Append(var.CApply(iu.Current)); } return(un.Unifies(args[2], all)); }
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 Plan(Pred label, Trigger te, ILogicalFormula ct, IPlanBody bd) : base("plan", 0) { tevent = te; tevent.SetAsTriggerTerm(false); SetLabel(label); SetContext(ct); if (bd == null) { body = new PlanBodyImpl(); } else { body = bd; body.SetAsBodyTerm(false); } }
public Rule(Literal head, ILogicalFormula body) : base(head) { if (head.IsRule()) { //logger.log(Level.SEVERE, "The rule head (" + head + ") is a rule!", new Exception()); } else if (IsInternalAction()) { //logger.log(Level.SEVERE,"The rule head ("+head+") can not be an internal action!", new Exception()); } else if (head == LTrue || head == LFalse) { //logger.log(Level.SEVERE,"The rule head ("+head+") can not be a true or false!", new Exception()); } this.body = body; }
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 bool Believes(ILogicalFormula bel, Unifier un) { try { IEnumerator <Unifier> iun = bel.LogicalConsequence(this, un); if (iun != null && iun.MoveNext()) { un.Compose(iun.Current); return(true); } } catch (Exception e) { //logger.log(Level.SEVERE, "** Error in method believes(" + bel + "," + un + ").", e); } return(false); }
public override double Evaluate(Reasoner reasoner, ITerm[] args) { if (reasoner == null) { throw new JasonityException("The TransitionSystem parameter of the function '.count' cannot be null."); } ILogicalFormula logExpr = (ILogicalFormula)args[0]; int n = 0; IEnumerator <Unifier> iu = logExpr.LogicalConsequence(reasoner.GetAgent(), new Unifier()); Unifier aux = new Unifier(); while (iu.MoveNext()) { aux = iu.Current; n++; } return(n); }
override public object Execute(Reasoner ts, Unifier un, ITerm[] args) { CheckArguments(args); long timeout = -1; Trigger te = null; ILogicalFormula f = null; ITerm elapsedTime = null; if (args[0].IsNumeric()) { // time in milliseconds INumberTerm time = (INumberTerm)args[0]; timeout = (long)time.Solve(); } else { te = Trigger.TryToGetTrigger(args[0]); // wait for event if (te == null && args[0].GetType() == typeof(ILogicalFormula)) { // wait for an expression to become true f = (ILogicalFormula)args[0]; if (ts.GetAgent().Believes(f, un)) { // if the agent already believes f // place current intention back in I, since .wait usually does not do that Intention si = ts.GetCircumstance().GetSelectedIntention(); si.Peek().RemoveCurrentStep(); ts.GetCircumstance().AddRunningIntention(si); return(true); } } if (args.Length >= 2) { timeout = (long)((INumberTerm)args[1]).Solve(); } if (args.Length == 3) { elapsedTime = args[2]; } } new WaitEvent(te, f, un, ts, timeout, elapsedTime); return(true); }
public override void SetTerm(int i, ITerm t) { switch (i) { case 0: label = (Pred)t; break; case 1: tevent = (Trigger)t; break; case 2: context = (ILogicalFormula)t; break; case 3: body = (IPlanBody)t; break; } }
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); }
public static Rule CreateRule(Literal head, ILogicalFormula body) { return(new Rule(head, body)); }
public Rule(Rule r, Unifier u) : base(r, u) { isTerm = r.isTerm; body = (ILogicalFormula)r.body.CApply(u); predicateIndicatorCache = null; }
public LogExpr(LogicalOp oper, ILogicalFormula f) : base(oper.ToString(), f) => op = oper;
public LogExpr(ILogicalFormula f1, LogicalOp oper, ILogicalFormula f2) : base(f1, oper.ToString(), f2) { op = oper; }