Пример #1
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);
        }
Пример #2
0
        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);
        }
Пример #3
0
 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 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);
        }
Пример #5
0
        public void ParsePlanBody()
        {
            string    pb        = "<- .println(hello world)";
            string    pb2       = "<- glhjanusa";
            ITerm     t         = new Atom(pb);
            IPlanBody planBody  = new PlanBodyImpl(BodyType.Body_Type.internalAction, t);
            IPlanBody planBody2 = AsSyntax.ParsePlanBody(pb2);

            Assert.AreEqual(planBody.ToString(), planBody2.ToString());
        }
Пример #6
0
        public static IPlanBody ParsePlanBody(string sPlanBody)
        {
            as2j      parser = new as2j(new StringReader(sPlanBody));
            IPlanBody p      = parser.Plan_body();

            if (parser.getNextToken().kind != as2jConstants.EOF)
            {
                throw new ParseException("Expected <EOF> after " + p + " for parameter '" + sPlanBody + "'");
            }
            return(p);
        }
Пример #7
0
 public ITerm RemoveCurrentStep()
 {
     if (IsFinished())
     {
         return(null);
     }
     else
     {
         ITerm r = planBody.GetBodyTerm();
         planBody = planBody.GetBodyNext();
         return(r);
     }
 }
Пример #8
0
        public IntendedPlan(Option opt, Trigger te)
        {
            plan     = opt.GetPlan();
            planBody = plan.GetBody();
            unif     = opt.GetUnifier();

            if (te == null)
            {
                trigger = plan.GetTrigger().Capply(unif);
            }
            else
            {
                trigger = te.Capply(unif);
            }
        }
Пример #9
0
 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);
     }
 }
Пример #10
0
            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);
                }
            }
Пример #11
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);
        }
Пример #12
0
        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;
            }
        }
Пример #13
0
        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);
        }
Пример #14
0
 // Used by if/for/loop internal actions
 public IPlanBody InsertAsNextStep(IPlanBody pb)
 {
     planBody = new PlanBodyImpl(planBody.GetBodyType(), planBody.GetBodyTerm());
     planBody.SetBodyNext(pb);
     return(planBody);
 }
Пример #15
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);
        }