예제 #1
0
 public ITerm RemoveCurrentStep()
 {
     if (IsFinished())
     {
         return(null);
     }
     else
     {
         ITerm r = planBody.GetBodyTerm();
         planBody = planBody.GetBodyNext();
         return(r);
     }
 }
예제 #2
0
        public override bool DropInt(Circumstance C, Literal goal, Unifier un)
        {
            Unifier bak                  = un.Clone();
            bool    isCurrentInt         = false;
            IEnumerator <Intention> iint = C.GetAllIntentions();

            while (iint.Current != null)
            {
                Intention i  = iint.Current;
                IPlanBody pb = i.Peek().GetPlan().GetBody();
                while (pb != null)
                {
                    if (pb.GetBodyType() == BodyType.Body_Type.achieve || pb.GetBodyType() == BodyType.Body_Type.achieveNF)
                    {
                        if (un.Unifies(pb.GetBodyTerm(), goal))
                        {
                            C.DropIntention(i);
                            isCurrentInt = isCurrentInt || i.Equals(C.GetSelectedIntention());
                            un           = bak.Clone();
                            break;
                        }
                    }
                    pb = pb.GetBodyNext();
                }
            }
            return(isCurrentInt);
        }
예제 #3
0
파일: Trigger.cs 프로젝트: nasa03/Jasonity
 public static Trigger TryToGetTrigger(ITerm t)
 {
     if (t.GetType() == typeof(Trigger))
     {
         return((Trigger)t);
     }
     if (t.IsPlanBody())
     {
         IPlanBody p = (IPlanBody)t;
         if (p.GetPlanSize() == 1)
         {
             TEOperator op = null;
             if (p.GetBodyType() == BodyType.Body_Type.addBel)
             {
                 op = TEOperator.add;
             }
             else if (p.GetBodyType() == BodyType.Body_Type.delBel)
             {
                 op = TEOperator.del;
             }
             if (op != null)
             {
                 Literal l = ((Literal)p.GetBodyTerm().Clone()).ForceFullLiteralImpl();
                 l.DelAnnot(BeliefBase.TSelf);
                 return(new Trigger(op, TEType.belief, l));
             }
         }
     }
     if (t.IsString())
     {
         return(AsSyntax.ParseTrigger(((IStringTerm)t).GetString()));
     }
     return(null);
 }
예제 #4
0
        public override ITerm GetTerm(int i)
        {
            switch (i)
            {
            case 0:
                return((label == null) ? noLabelAtom : label);

            case 1:
                return(tevent);

            case 2:
                return((context == null) ? Literal.LTrue : context);

            case 3:
                if (body.GetBodyNext() == null && body.GetBodyTerm().IsVar())
                {
                    return(body.GetBodyTerm());
                }
                return(body);

            default:
                return(null);
            }
        }
예제 #5
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);
        }
예제 #6
0
파일: Unifier.cs 프로젝트: nasa03/Jasonity
        public bool UnifyTerms(ITerm term, ITerm term2)
        {
            if (term.IsArithExpr())
            {
                term = term.CApply(this);
            }
            if (term2.IsArithExpr())
            {
                term2 = term2.CApply(this);
            }

            bool termIsVar  = term.IsVar();
            bool term2IsVar = term2.IsVar();

            // One of them is a variable
            if (termIsVar || term2IsVar)
            {
                VarTerm termv  = termIsVar ? (VarTerm)term : null;
                VarTerm term2v = term2IsVar ? (VarTerm)term2 : null;

                // Get their values
                ITerm termvl  = termIsVar ? Get(termv) : term;
                ITerm term2vl = term2IsVar ? Get(term2v) : term2;

                if (termvl != null && term2vl != null) // Unify the values of the two variables
                {
                    return(UnifiesNoUndo(termvl, term2vl));
                }
                else if (termvl != null) // Unify a variable with a value
                {
                    return(Bind(term2v, termvl));
                }
                else if (term2vl != null)
                {
                    return(Bind(termv, term2vl));
                }
                else // Unify two variables
                {
                    if (!UnifyTerms(termv.GetNS(), term2v.GetNS()))
                    {
                        return(false);
                    }
                    if (termv.Negated() != term2v.Negated())
                    {
                        return(false);
                    }
                    Bind(termv, term2v);
                    return(true);
                }
            }

            // Both terms are not vars
            // If any of the terms is not a literal (is a number or string), they must be equal
            // For unification, lists are literals
            if (!term.IsLiteral() && !term.IsList() || !term2.IsLiteral() && !term2.IsList())
            {
                return(term.Equals(term2));
            }

            // Case of plan body
            if (term.IsPlanBody() && term2.IsPlanBody())
            {
                IPlanBody pb1 = (IPlanBody)term;
                IPlanBody pb2 = (IPlanBody)term2;

                if (pb1.GetBodyTerm() == null && pb2.GetBodyTerm() == null)
                {
                    return(true);
                }
                if (pb1.GetBodyTerm() == null && pb2.GetBodyTerm() != null)
                {
                    return(false);
                }
                if (pb1.GetBodyTerm() != null && pb2.GetBodyTerm() == null)
                {
                    return(false);
                }

                if (pb1.GetBodyTerm().IsVar() && pb2.GetBodyTerm().IsVar())
                {
                    if (UnifiesNoUndo(pb1.GetBodyTerm(), pb2.GetBodyTerm()))
                    {
                        return(UnifiesNoUndo(pb1.GetBodyNext(), pb2.GetBodyNext()));
                    }
                    else
                    {
                        return(false);
                    }
                }

                if (pb1.GetBodyTerm().IsVar())
                {
                    if (pb1.GetBodyNext() == null)
                    {
                        return(UnifiesNoUndo(pb1.GetBodyTerm(), pb2));
                    }
                    else
                    {
                        if (pb2.GetBodyTerm() == null)
                        {
                            return(false);
                        }
                        if (UnifiesNoUndo(pb1.GetBodyTerm(), pb2.GetHead()))
                        {
                            if (pb2.GetBodyNext() == null)
                            {
                                if (pb1.GetBodyNext() != null && pb1.GetBodyNext().GetBodyTerm().IsVar() && pb1.GetBodyNext().GetBodyNext() == null)
                                {
                                    return(UnifiesNoUndo(pb1.GetBodyNext().GetBodyTerm(), new PlanBodyImpl()));
                                }
                                return(false);
                            }
                            else
                            {
                                return(UnifiesNoUndo(pb1.GetBodyNext(), pb2.GetBodyNext()));
                            }
                        }
                    }
                }
                else if (pb2.GetBodyTerm().IsVar())
                {
                    return(Unifies(pb2, pb1));
                }
            }

            // Both terms are literal
            Literal t1s = (Literal)term;
            Literal t2s = (Literal)term2;

            // Different arities
            int ts = t1s.GetArity();

            if (ts != t2s.GetArity())
            {
                return(false);
            }

            // If both are literal, they must have the same negated
            if (t1s.Negated() != t2s.Negated())
            {
                return(false);
            }

            // Different functor
            if (!t1s.GetFunctor().Equals(t2s.GetFunctor()))
            {
                return(false);
            }

            // Different namespace
            if (!UnifiesNamespace(t1s, t2s))
            {
                return(false);
            }

            // Unify inner terms
            for (int i = 0; i < ts; i++)
            {
                if (!UnifiesNoUndo(t1s.GetTerm(i), t2s.GetTerm(i)))
                {
                    return(false);
                }
            }

            // The first's annotations must be a subset of the second's annotations
            if (!t1s.HasSubsetAnnot(t2s, this))
            {
                return(false);
            }

            return(true);
        }
예제 #7
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);
        }