Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
 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);
 }
Exemplo n.º 4
0
        public override ITerm CApply(Unifier u)
        {
            if (u != null)
            {
                ITerm vl = u.Get(this);
                if (vl != null)
                {
                    if (!vl.IsCyclicTerm() && vl.HasVar(this, u))
                    {
                        u.Remove(this);
                        ITerm tempVl = vl.CApply(u);
                        u.Bind(this, vl);

                        CyclicTerm ct          = new CyclicTerm(tempVl as Literal, this);
                        Unifier    renamedVars = new Unifier();
                        ct.MakeVarsAnnon(renamedVars);
                        renamedVars.Remove(this);
                        u.Compose(renamedVars);
                        vl = ct;
                    }

                    vl = vl.CApply(u);

                    if (vl.IsLiteral())
                    {
                        if (GetNS() != Literal.DefaultNS)
                        {
                            vl = (vl.CloneNS(GetNS().CApply(u) as Atom) as Literal);
                        }
                        if (Negated())
                        {
                            ((Literal)vl).SetNegated(Literal.LNeg);
                        }
                    }

                    if (vl.IsLiteral() && this.HasAnnot())
                    {
                        vl = ((Literal)vl).ForceFullLiteralImpl().AddAnnots((IListTerm)this.GetAnnots().CApply(u));
                    }
                    return(vl);
                }
            }
            return((ITerm)Clone()); // Como uso el Clone de C# lo que clono son object que luego hay que castear...
        }
Exemplo n.º 5
0
        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);
        }