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 void RandomTestEvaluate() { Reasoner r = new Reasoner(new Agent(), new Circumstance(), new AgentArchitecture(), new Settings()); ITerm[] args = new ITerm[1]; args[0] = new NumberTermImpl(); Random rAux = new Random(); double aux = rAux.Next() * ((INumberTerm)args[0]).Solve(); Assert.AreEqual(aux, a.Evaluate(r, args)); //The test says that the result is wrong but it's only because //we're generating two random numbers and comparing them. args[0] = new StringTermImpl(); aux = rAux.Next(); Assert.AreEqual(aux, a.Evaluate(r, args)); }
public void SumTestEvaluate() { Reasoner r = new Reasoner(new Agent(), new Circumstance(), new AgentArchitecture(), new Settings()); ITerm[] args = new ITerm[1]; args[0] = new NumberTermImpl(); double auxSum = 0; foreach (ITerm t in (IListTerm)args[0]) { if (t.IsNumeric()) { auxSum += ((INumberTerm)t).Solve(); } } Assert.AreEqual(auxSum, a.Evaluate(r, args)); }
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 object Execute(Reasoner ts, Unifier un, ITerm[] args) { if (args[0].IsList()) { if (!args[args.Length - 1].IsVar() && !args[args.Length - 1].IsList()) { throw new JasonityException("Last argument of concat '" + args[args.Length - 1] + "'is not a list nor a variable."); } IListTerm result = args[0].Clone() as IListTerm; for (int i = 1; i < args.Length - 1; i++) { if (!args[i].IsList()) { throw JasonityException.CreateWrongArgument(this, "arg[" + i + "] is not a list"); } result.Concat((IListTerm)args[i].Clone()); } return(un.Unifies(result, args[args.Length - 1])); } else { if (!args[args.Length - 1].IsVar() && !args[args.Length - 1].IsString()) { throw JasonityException.CreateWrongArgument(this, "Last argument '" + args[args.Length - 1] + "' is not a string nor a variable."); } string vl = args[0].ToString(); if (args[0].IsString()) { vl = ((IStringTerm)args[0]).GetString(); } StringBuilder sr = new StringBuilder(vl); for (int i = 0; i < args.Length - 1; i++) { vl = args[i].ToString(); if (args[i].IsString()) { vl = ((IStringTerm)args[i]).GetString(); } sr.Append(vl); } return(un.Unifies(new StringTermImpl(sr.ToString()), args[args.Length - 1])); } }
public override double Evaluate(Reasoner reasoner, ITerm[] args) { if (args[0].IsList()) { double sum = 0; int n = 0; foreach (ITerm t in (IListTerm)args[0]) { if (t.IsNumeric()) { sum += ((INumberTerm)t).Solve(); n++; } } return(sum / n); } throw new JasonityException(GetName() + " is not implemented for type '" + args[0] + "'."); }
public override object Execute(Reasoner ts, Unifier un, ITerm[] args) { CheckArguments(args); string name = GetName(args); string source = GetSource(args); List <string> agArchClasses = GetAgArchClasses(args); string agClass = null; ClassParameters bbPars = null; if (args.Length > 2) { foreach (ITerm t in args[2] as IListTerm) { if (t.IsStructure()) { Structure s = t as Structure; if (s.GetFunctor().Equals("beliefBaseClass")) { bbPars = new ClassParameters(TestString(s.GetTerm(0))); } else if (s.GetFunctor().Equals("agentClass")) { agClass = TestString(s.GetTerm(0)).ToString(); } } } } IRuntimeServices rs = ts.GetUserAgArch().GetRuntimeServices(); name = rs.CreateAgent(name, source, agClass, agArchClasses, GetSettings(), ts.GetAgent()); rs.StartAgent(name); if (args[0].IsVar()) { return(un.Unifies(new StringTermImpl(name), args[0])); } else { return(true); } }
public virtual void DropDesireInEvent(Reasoner rs, Event e, Intention i) { Circumstance C = rs.GetCircumstance(); C.RemoveEvent(e); if (i != null) { if (rs.HasDesireListener()) { foreach (Desire gl in rs.GetDesiresListeners()) { gl.DesireFinished(e.GetTrigger(), Desire.FinishStates.achieved); } i.Peek().RemoveCurrentStep(); rs.ApplyClrInt(i); C.AddRunningIntention(i); } } }
private void buildReasoner() { reasoner = new Reasoner <CakeGame>(); reasoner.scoreType = Reasoner <CakeGame> .ScoreType.Raw; var sleepConsid = new ThresholdSumConsideration <CakeGame>(game.sleepBed, 0.6f, "sleep"); sleepConsid.addAppraisal(new Sleepy(game)); sleepConsid.addAppraisal(new Backlogged(game).scale(0.3f).negate()); reasoner.addConsideration(sleepConsid); var bakeConsid = new SumConsideration <CakeGame>(game.bakeCake, "bake"); bakeConsid.addAppraisal(new Backlogged(game)); reasoner.addConsideration(bakeConsid); var shopConsid = new SumConsideration <CakeGame>(game.buyFlour, "shop"); shopConsid.addAppraisal(new LowFlour(game)); reasoner.addConsideration(shopConsid); }
private static void Example4() { var schema = new Graph(); schema.Assert("http://schema.com#employee", "owl:inverseOf", "http://schema.com#employer"); schema.Assert("http://schema.com#employee", "rdfs:domain", "http://schema.com#Company"); schema.Assert("http://schema.com#employee", "rdfs:range", "http://schema.com#Person"); var graph = new Graph(); graph.Assert("http://person.com#bob", "http://schema.com#employer", "http://person.com#acme"); var reasoner = new Reasoner(schema); reasoner.Apply(graph); foreach (var triple in graph.GetTriples()) { Console.WriteLine(triple); } }
public override object Execute(Reasoner ts, Unifier un, ITerm[] args) { CheckArguments(args); IListTerm list = null; if (args[1].IsList()) { list = (IListTerm)args[1]; } else if (args[1].IsString()) { list = new ListTermImpl(); foreach (byte b in Encoding.UTF8.GetBytes(((IStringTerm)args[1]).GetString())) { list.Add(new StringTermImpl(Encoding.UTF8.GetString(new byte[] { b }))); //Esto a la wiki } } if (args[0].IsNumeric()) { int index = (int)((INumberTerm)args[0]).Solve(); if (index < 0 || index >= list.Size()) { throw new JasonityException("nth: index " + index + " is out of bounds (" + list.Size() + ")"); } return(un.Unifies(args[2], list[index])); } if (args[0].IsVar()) { IEnumerator <ITerm> ilist = list.ListTermIterator(); //return all index for thirds arg return(new NthStdLibIterator <Unifier>(ilist, un, args)); } return(false); }
private void createReasoner() { // create utility planner reasoner = new Reasoner <DuckMind>(); Reasoner <DuckMind> .trace = true; // enable tracing reasoner.scoreType = Reasoner <DuckMind> .ScoreType.Normalized; var eatConsideration = new ThresholdConsideration <DuckMind>(eatAction, 0.3f, "eat"); eatConsideration.addAppraisal(new EatAppraisals.Hunger(mind)); // 0-1 eatConsideration.addAppraisal(new EatAppraisals.FoodAvailability(mind)); //0-1 reasoner.addConsideration(eatConsideration); var exploreConsideration = new SumConsideration <DuckMind>(exploreAction, "explore"); exploreConsideration.addAppraisal(new ExploreAppraisals.ExplorationTendency(mind)); exploreConsideration.addAppraisal(new ExploreAppraisals.Unexplored(mind)); reasoner.addConsideration(exploreConsideration); // FIGHT of fight-or-flight var fightConsideration = new ThresholdSumConsideration <DuckMind>(fightAction, 0.8f, "fight"); fightConsideration.addAppraisal(new DefenseAppraisals.NearbyThreat(mind)); fightConsideration.addAppraisal(new DefenseAppraisals.ThreatFightable(mind)); reasoner.addConsideration(fightConsideration); // FLIGHT of fight-or-flight var fleeConsideration = new ThresholdConsideration <DuckMind>(fleeAction, 0.4f, "flee"); fleeConsideration.addAppraisal(new DefenseAppraisals.NearbyThreat(mind)); fleeConsideration.addAppraisal(new DefenseAppraisals.ThreatFightable(mind).inverse()); reasoner.addConsideration(fleeConsideration); var socialConsideration = new ThresholdConsideration <DuckMind>(socialAction, 0.2f, "social"); socialConsideration.addAppraisal(new SocialAppraisals.NearbyPotentialAllies(mind)); socialConsideration.addAppraisal(new SocialAppraisals.Sociability(mind)); socialConsideration.addAppraisal(new SocialAppraisals.FriendBudget(mind)); reasoner.addConsideration(socialConsideration); }
//int sumAct = 0; int nbAct = 0; protected void Act() { Reasoner reasoner = GetReasoner(); int i = 0; int ca = cyclesAct; if (ca != 1) { // not the default value, limit the value to the number of intentions ca = Math.Min(cyclesAct, reasoner.GetCircumstance().GetNbRunningIntentions()); if (ca == 0) { ca = 1; } } while (running && i++ < ca && !reasoner.CanSleepAct()) { reasoner.Act(); } //sumAct += i; nbAct++; //System.out.println("running act "+(sumAct/nbAct)+"/"+ca); }
/* 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 override int DropDesire(Intention i, Trigger g, Reasoner rs, Unifier un) { if (i != null) { if (i.DropDesire(g, un)) { //notify listener if (rs.HasDesireListener()) { foreach (Desire gl in rs.GetDesiresListeners()) { gl.DesireFailed(g); } } //generate failure event Event failEvent = rs.FindEventForFailure(i, g); //find fail event for the goal just dropped if (failEvent != null) { failEvent = new Event(failEvent.GetTrigger().Capply(un), failEvent.GetIntention()); rs.GetCircumstance().AddEvent(failEvent); return(2); } else //i is finished or without failure plan { if (rs.HasDesireListener()) { foreach (Desire gl in rs.GetDesiresListeners()) { gl.DesireFinished(g, Desire.FinishStates.unachieved); } } i.Fail(rs.GetCircumstance()); return(3); } } } return(0); }
public override double Evaluate(Reasoner reasoner, ITerm[] args) { // create a literal to perform the query Literal r; if (literal.IndexOf(".") > 0) // is internal action { r = new InternalActionLiteral(literal); } else { r = new LiteralImpl(literal); } r.AddTerms(args); VarTerm answer = new VarTerm("__RuleToFunctionResult"); r.AddTerm(answer); // query the BB IEnumerator <Unifier> i = r.LogicalConsequence((reasoner == null ? null : reasoner.GetAgent()), new Unifier()); if (i.MoveNext()) { ITerm value = i.Current.Get(answer); if (value.IsNumeric()) { return(((INumberTerm)value).Solve()); } else { throw new JasonityException("The result of " + r + " (=" + value + ") is not numeric!"); } } else { throw new JasonityException("No solution was found for rule " + r); } }
public override object Execute(Reasoner reasoner, Unifier un, ITerm[] args) { if (args.Length == 1 && (args[0].GetType() == typeof(Trigger))) { Trigger te = Trigger.TryToGetTrigger(args[0]); if (!te.GetLiteral().HasSource()) { te.GetLiteral().AddSource(new UnnamedVar()); } foreach (Plan p in reasoner.GetAgent().GetPL()) { if (te == null || new Unifier().Unifies(p.GetTrigger(), te)) { } } } else { } return(true); }
public override object Execute(Reasoner ts, Unifier un, ITerm[] args) { CheckArguments(args); if (args[0].IsNumeric()) { int nextArg = 1; int start = (int)((args[0] as INumberTerm).Solve()); int end = start + 1; if (args.Length == 4 && args[1].IsNumeric()) { nextArg = 2; end = (int)((args[0] as INumberTerm).Solve()); } if (args[nextArg].IsString()) { return(un.Unifies(args[nextArg + 1], DeleteFromString(start, end, args[nextArg] as IStringTerm))); } else if (args[nextArg].IsList()) { return(un.Unifies(args[nextArg + 1], DeleteFromList(start, end, args[nextArg] as IListTerm))); } } if (args[0].IsString() && args[1].IsString()) { return(un.Unifies(args[2], DeleteFromString(args[0] as IStringTerm, args[1] as IStringTerm))); } if (args[0].IsString()) { return(un.Unifies(args[2], DeleteFromString(args[0] as IStringTerm, new StringTermImpl(args[1].ToString())))); } if (args[0].IsList()) { return(un.Unifies(args[2], DeleteFromList(args[0], args[1] as IListTerm, un.Clone()))); } throw new JasonityException("Incorrect use of the internal action '.delete' (see documentation)."); }
public static Agent Create(AgentArchitecture agArch, string asSrc, Settings stts) { try { Agent ag = new Agent(); Reasoner r = new Reasoner(ag, null, agArch, stts); BeliefBase bb = null; bb = new BeliefBase(); ag.SetBB(bb); ag.InitAg(); //bb.Init(ag); ag.Load(asSrc); return(ag); } catch (Exception e) { throw new JasonityException(e.Message); } }
public void AverageTestEvaluate() { Reasoner r = new Reasoner(new Agent(), new Circumstance(), new AgentArchitecture(), new Settings()); ITerm[] args = new ITerm[1]; args[0] = new NumberTermImpl(); double sumAux = 0; int nAux = 0; foreach (ITerm t in (IListTerm)args[0]) { if (t.IsNumeric()) { sumAux += ((INumberTerm)t).Solve(); nAux++; } } double result = a.Evaluate(r, args); Assert.AreEqual(sumAux / nAux, result); }
private static void Example5() { var schema = new Graph(); schema.Assert("http://schema.com#employee", "owl:inverseOf", "http://schema.com#employer"); schema.Assert("http://schema.com#employee", "rdfs:domain", "http://schema.com#Company"); schema.Assert("http://schema.com#employee", "rdfs:range", "http://schema.com#Person"); var graph = new Graph(); graph.Assert("http://person.com#bob", "http://schema.com#employer", "http://person.com#acme"); var reasoner = new Reasoner(schema); var recordingGraph = new RecordingGraph(graph); reasoner.Apply(recordingGraph); Console.WriteLine("The combined graph:"); foreach (var triple in graph.GetTriples()) { Console.WriteLine($" {triple}"); } Console.WriteLine("The inferred graph:"); foreach (var triple in recordingGraph.Asserted) { Console.WriteLine($" {triple}"); } Console.WriteLine("The original graph:"); var patch = recordingGraph.CreatePatch(); var original = graph.Minus(patch.Assert); foreach (var triple in graph.GetTriples()) { Console.WriteLine($" {triple}"); } }
public override object Execute(Reasoner r, Unifier un, ITerm[] args) { if (args.Length > 0) //add all arguments as annotations in the exception { //find message ITerm smgs = null; string msg = "FailStdLib"; foreach (ITerm t in args) { if (t.IsStructure() && ((Structure)t).GetFunctor().Equals("Error_Msg")) { smgs = t; ITerm tm = ((Structure)t).GetTerm(0); if (tm.IsString()) { msg = ((IStringTerm)tm).GetString(); } else { msg = tm.ToString(); } break; } } JasonityException e = new JasonityException(msg); foreach (ITerm t in args) { if (t != smgs) { e.AddErrorAnnot(t); } } throw e; } return(false); }
public override object Execute(Reasoner ts, Unifier un, ITerm[] args) { CheckArguments(args); Circumstance C = ts.GetCircumstance(); C.ClearRunningIntentions(); C.ClearPendingIntentions(); C.ClearPendingActions(); // drop intentions in E IEnumerator <Event> ie = C.GetEventsPlusAtomic(); while (ie.Current != null) { Event e = ie.Current; if (e.IsInternal()) { C.RemoveEvent(e); } } //drop intentions in PE foreach (string ek in C.GetPendingEvents().Keys) { Event e = C.GetPendingEvents()[ek]; if (e.IsInternal()) { C.RemovePendingEvent(ek); } } AtStdLib atia = ts.GetAgent().GetIA(AtStdLib.atAtom) as AtStdLib; atia.CancelAts(); return(true); }
public override object Execute(Reasoner reasoner, Unifier un, ITerm[] args) { CheckArguments(args); ITerm l1 = args[0]; ITerm l2 = args[1]; INumberTerm size = null; if (l1.IsList()) { IListTerm lt = (IListTerm)l1; size = new NumberTermImpl(lt.Size()); } else if (l1.IsString()) { IStringTerm st = (IStringTerm)l1; size = new NumberTermImpl(st.GetString().Length); } if (size != null) { return(un.Unifies(l2, size)); } return(false); }
override public object Execute(Reasoner ts, Unifier un, ITerm[] args) { CheckArguments(args); IListTerm list = (IListTerm)args[0]; if (list.Count == 0) { return(false); } IEnumerator <ITerm> i = list.GetEnumerator(); ITerm min = i.Current; while (i.MoveNext()) { ITerm t = i.Current; if (Compare(min, t)) { min = t; } } return(un.Unifies(args[1], (ITerm)min.Clone())); // Como uso el Clone de C# lo que clono son object que luego hay que castear... }
public override object Execute(Reasoner ts, Unifier un, ITerm[] args) { CheckArguments(args); ITerm source = BeliefBase.ASelf; if (args.Length > 1) { source = args[1]; } bool before = false; if (args.Length > 2) { before = args[2].ToString().Equals("begin"); } if (args[0].IsList()) { foreach (ITerm t in (IListTerm)args[0]) { ts.GetAgent().GetPL().Add(Transform2Plan(t), source, before); } } else { ts.GetAgent().GetPL().Add(Transform2Plan(args[0]), source, before); } if (ts.GetAgent().GetPL().HasMetaEventPlans()) { ts.AddDesireListener(new Desire(ts)); } return(true); }
public override object Execute(Reasoner ts, Unifier un, ITerm[] args) { return(un.Unifies(args[1], ((Literal)args[0]).MakeVarsAnnon())); }
public ReasonerAction(Reasoner <T> reasoner) { this.reasoner = reasoner; }
public UtilityAI(T context, Reasoner <T> rootSelector, float updatePeriod = 0.2f) { _rootReasoner = rootSelector; _context = context; UpdatePeriod = _elapsedTime = updatePeriod; }
public override double Evaluate(Reasoner reasoner, ITerm[] args) { return(TimeUtils.CurrentTimeMillis()); }