Beispiel #1
0
            //public bool HasNext()
            //{
            //    if (c == null) // the first call of hasNext should find the first response
            //        Find();
            //    return c != null;
            //}

            //public Unifier Next()
            //{
            //    if (c == null) Find();
            //    Unifier b = c;
            //    Find(); // find next response
            //    return b;
            //}

            void Find()
            {
                while (listOutter != null && listOutter.Count != 0)
                {
                    while (list.Count != 0)
                    {
                        IListTerm candidate = AsSyntax.AsSyntax.CreateList(list);
                        list.Remove(list.ElementAt(list.Count - 1));
                        c = un.Clone();
                        if (c.UnifiesNoUndo(sublist, candidate))
                        {
                            return; // found another sublist, c is the current response
                        }
                    }
                    listOutter = listOutter.GetNext();
                    if (listOutter == null || listOutter.IsVar()) // the case of lists with tail
                    {
                        break;
                    }
                    list = listOutter.GetAsList();
                }
                if (!triedEmpty)
                {
                    triedEmpty = true;
                    c          = un.Clone();
                    if (c.UnifiesNoUndo(sublist, AsSyntax.AsSyntax.CreateList()))
                    {
                        return; // found another sublist, c is the current response
                    }
                }
                c = null; // no more sublists found
            }
Beispiel #2
0
        public void RemovePendingEvents(Trigger te, Unifier un)
        {
            IEnumerator <Event> ie = PE.Values.GetEnumerator();

            while (ie.MoveNext())
            {
                Event   ev = ie.Current;
                Trigger t  = ev.GetTrigger();
                if (ev.GetIntention() != Intention.emptyInt)
                {
                    t = t.Capply(ev.GetIntention().Peek().GetUnif());
                }
                if (un.Clone().UnifiesNoUndo(te, t))
                {
                    ie.Dispose();

                    if (listeners != null && ev.GetIntention() != null)
                    {
                        foreach (ICircumstanceListener el in listeners)
                        {
                            el.IntentionDropped(ev.GetIntention());
                        }
                    }
                }
            }
        }
Beispiel #3
0
 override public object Execute(Reasoner ts, Unifier un, ITerm[] args)
 {
     try {
         Literal   pattern = (Literal)args[0];
         IListTerm result  = new ListTermImpl();
         //synchronized(ts.GetAgent().GetBB().GetLock()) {
         IEnumerator <Literal> i = ts.GetAgent().GetBB().GetCandidateBeliefs(pattern, un);
         while (i.MoveNext())
         {
             Literal l = i.Current;
             if (l.IsRule())
             {
                 if (un.Clone().Unifies(pattern, l))
                 {
                     l = l.Copy();
                     l.DelSources();
                     ((Rule)l).SetAsTerm(true);
                     result.Add(l);
                 }
             }
         }
         //}
         return(un.Unifies(args[1], result));
     } catch (Exception e) {
         //ts.GetLogger().Warning("Error in internal action 'get_rules'! "+e);
     }
     return(false);
 }
        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);
        }
Beispiel #5
0
        public object Clone()
        {
            IntendedPlan c = new IntendedPlan();

            c.unif = unif.Clone();
            if (planBody != null)
            {
                c.planBody = planBody.ClonePB();
            }
            c.trigger = trigger.Clone();
            c.plan    = plan;
            return(c);
        }
Beispiel #6
0
            //public bool HasNext()
            //{
            //    if (c == null)
            //        Find();
            //    return c != null;
            //}

            //public Unifier Next()
            //{
            //    if (c == null)
            //        Find();
            //    Unifier b = c;
            //    Find(); // find next response
            //    return b;
            //}

            void Find()
            {
                while (!(list.Count == 0))
                {
                    IListTerm candidate = AsSyntax.AsSyntax.CreateList(list);
                    list.Remove(list.ElementAt(list.Count - 1));
                    c = un.Clone();
                    if (c.UnifiesNoUndo(sublist, candidate))
                    {
                        return; // found another sublist, c is the current response
                    }
                }
                if (!triedEmpty)
                {
                    triedEmpty = true;
                    c          = un.Clone();
                    if (c.UnifiesNoUndo(sublist, AsSyntax.AsSyntax.CreateList()))
                    {
                        return; // found another sublist, c is the current response
                    }
                }
                c = null; // no more sublists found
            }
Beispiel #7
0
            //public bool HasNext()
            //{
            //    if (c == null) // the first call of hasNext should find the first response
            //        Find();
            //    return c != default;
            //}

            //public Unifier Next()
            //{
            //    if (c == null)
            //        Find(); // find next response
            //    Unifier b = c;
            //    Find();
            //    return b;
            //}

            void Find()
            {
                while (ilist.MoveNext())
                {
                    index++;
                    ITerm candidate = ilist.Current;
                    c = un.Clone();
                    if (c.UnifiesNoUndo(args[2], candidate))
                    {
                        c.Unifies(args[0], AsSyntax.AsSyntax.CreateNumberTerm(index));
                        return; // found another
                    }
                }
                c = null;
            }
Beispiel #8
0
            //public bool HasNext()
            //{
            //    if (c == null) // the first call of hasNext should find the first response
            //        Find();
            //    return c != null;
            //}

            //public Unifier Next()
            //{
            //    if (c == null) Find();
            //        Unifier b = c;
            //    Find(); // find next response
            //    return b;
            //}

            void Find()
            {
                if (pos < s1.Length)
                {
                    pos = s1.IndexOf(s0, pos);
                    if (pos >= 0)
                    {
                        c = (Unifier)un.Clone();
                        c.UnifiesNoUndo(args[2], new NumberTermImpl(pos));
                        pos++;
                        return;
                    }
                    pos = s1.Length; // to stop searching
                }
                c = null;
            }
Beispiel #9
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);
        }
Beispiel #10
0
            //public bool HasNext()
            //{
            //    if (c == null) // the first call of hasNext should find the first response
            //        Find();
            //    return c != null;
            //}

            //public Unifier Next()
            //{
            //    if (c == null)
            //        Find();
            //    Unifier b = c;
            //    Find(); // find next response
            //    return b;
            //}

            void Find()
            {
                while (list.MoveNext())
                {
                    IListTerm l = list.Current;
                    if (l.IsVar()) // the case of the tail of the list
                    {
                        break;
                    }
                    c = un.Clone();
                    if (c.UnifiesNoUndo(sublist, AsSyntax.AsSyntax.CreateList(l)))
                    {
                        return; // found another sublist, c is the current response
                    }
                }
                c = null; // no more sublists found
            }
Beispiel #11
0
        /**
         * Drops an intention based on a goal argument
         *
         * returns true if the current intention is dropped
         */
        public virtual bool DropInt(Circumstance C, Literal goal, Unifier un)
        {
            Unifier bak                  = un.Clone();
            Trigger g                    = new Trigger(TEOperator.add, TEType.achieve, goal);
            bool    isCurrentInt         = false;
            IEnumerator <Intention> iint = C.GetAllIntentions();

            while (iint.Current != null)
            {
                Intention i = iint.Current;
                if (i.HasTrigger(g, un))
                {
                    C.DropIntention(i);
                    isCurrentInt = isCurrentInt || i.Equals(C.GetSelectedIntention());
                    un           = bak.Clone();
                }
            }
            return(isCurrentInt);
        }
Beispiel #12
0
        IListTerm DeleteFromList(ITerm element, IListTerm l, Unifier un)
        {
            Unifier   bak  = un;
            IListTerm r    = new ListTermImpl();
            IListTerm last = r;

            foreach (ITerm t in l)
            {
                if (un.Unifies(element, t))
                {
                    un = bak.Clone();
                }
                else
                {
                    last = last.Append((ITerm)t.Clone()); // Como uso el Clone de C# lo que clono son object que luego hay que castear...
                }
            }
            return(r);
        }
Beispiel #13
0
        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).");
        }
Beispiel #14
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);
        }
Beispiel #15
0
        public void CloneTest()
        {
            Unifier resultado = un.Clone();

            Assert.AreEqual(un, resultado);
        }
Beispiel #16
0
            //public bool HasNext()
            //{
            //    if (needsUpdate)
            //        Get();
            //    return current != null;
            //}

            //public Unifier Next()
            //{
            //    if (needsUpdate)
            //        Get();
            //    if (current != null)
            //        needsUpdate = true;
            //    return current;
            //}

            private void Get()
            {
                needsUpdate = false;
                current     = default;
                if (arch != null && !arch.IsRunning())
                {
                    return;
                }

                if (annotsOptions != null)
                {
                    while (annotsOptions.MoveNext())
                    {
                        Literal belToTry = belInBB.Copy().SetAnnots(null).AddAnnots(annotsOptions.Current);
                        Unifier u        = un.Clone();
                        if (u.UnifiesNoUndo(lit, belToTry))
                        {
                            current = u;
                            return;
                        }
                    }
                    annotsOptions = null;
                }

                if (ruleIt != null)
                {
                    while (ruleIt.MoveNext())
                    {
                        Unifier ruleUn = ruleIt.Current;
                        Literal rHead  = rule.HeadCApply(ruleUn);
                        UseDerefVars(rHead, ruleUn);
                        rHead.MakeVarsAnnon();

                        Unifier unC = un.Clone();
                        if (unC.UnifiesNoUndo(lit, rHead))
                        {
                            current = unC;
                            return;
                        }
                    }
                    ruleIt = null;
                }

                while (il.MoveNext())
                {
                    belInBB = il.Current;
                    if (belInBB.IsRule())
                    {
                        rule = (Rule)belInBB;
                        if (cloneAnnon == null)
                        {
                            cloneAnnon = (Literal)lit.CApply(un);
                            cloneAnnon.MakeVarsAnnon();
                        }

                        Unifier ruleUn = new Unifier();
                        if (ruleUn.UnifiesNoUndo(cloneAnnon, rule))
                        {
                            ruleIt = rule.GetBody().LogicalConsequence(ag, ruleUn);
                            Get();
                            if (current != null)
                            {
                                return;
                            }
                        }
                        else
                        {
                            if (nbAnnots > 0)
                            {
                                if (belInBB.HasAnnot())
                                {
                                    int nbAnnotsB = belInBB.GetAnnots().Count;
                                    if (nbAnnotsB >= nbAnnots)
                                    {
                                        annotsOptions = belInBB.GetAnnots().SubSets(nbAnnots);
                                        Get();
                                        if (current != null)
                                        {
                                            return;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                Unifier u = un.Clone();
                                if (u.UnifiesNoUndo(lit, belInBB))
                                {
                                    current = u;
                                    return;
                                }
                            }
                        }
                    }
                }
            }
Beispiel #17
0
        public void FindDesireAndDrop(Reasoner rs, Literal l, Unifier un)
        {
            Trigger      g   = new Trigger(TEOperator.add, TEType.achieve, l);
            Circumstance C   = rs.GetCircumstance();
            Unifier      bak = un.Clone();

            IEnumerator <Intention> itinit = C.GetRunningIntentionsPlusAtomic();

            while (itinit.MoveNext())
            {
                Intention i = itinit.Current;
                if (DropDesire(i, g, rs, un) > 1)
                {
                    C.DropRunningIntention(i);
                    un = bak.Clone();
                }
            }

            // dropping the current intention?
            DropDesire(C.GetSelectedIntention(), g, rs, un);
            un = bak.Clone();

            //dropping G in Events
            IEnumerator <Event> ie = C.GetEventsPlusAtomic();

            while (ie.MoveNext())
            {
                Event e = ie.Current;
                //Test in the intention
                Intention i = e.GetIntention();
                int       r = DropDesire(i, g, rs, un);
                if (r > 0)
                {
                    C.RemoveEvent(e);
                    if (r == 1)
                    {
                        C.ResumeIntention(i);
                    }
                    un = bak.Clone();
                }
                else
                {
                    //Test in the event
                    Trigger t = e.GetTrigger();
                    if (i != Intention.emptyInt && !i.IsFinished())
                    {
                        t = t.Capply(i.Peek().GetUnif());
                    }
                    if (un.Unifies(g, t))
                    {
                        DropDesireInEvent(rs, e, i);
                        un = bak.Clone();
                    }
                }
            }

            //dropping G in Pending Events
            foreach (string ek in C.GetPendingEvents().Keys)
            {
                //Test in the intention
                Event     e = C.GetPendingEvents()[ek];
                Intention i = e.GetIntention();
                int       r = DropDesire(i, g, rs, un);
                if (r > 0)
                {
                    C.RemovePendingEvent(ek);
                    if (r == 1)
                    {
                        C.ResumeIntention(i);
                    }
                    un = bak.Clone();
                }
                else
                {
                    //test in the event
                    Trigger t = e.GetTrigger();
                    if (i != Intention.emptyInt && !i.IsFinished())
                    {
                        t = t.Capply(i.Peek().GetUnif());
                    }
                    if (un.Unifies(g, t))
                    {
                        DropDesireInEvent(rs, e, i);
                        un = bak.Clone();
                    }
                }
            }

            //Dropping from pending Actions
            foreach (ExecuteAction a in C.GetPendingActions().Values)
            {
                Intention i = a.GetIntention();
                int       r = DropDesire(i, g, rs, un);
                if (r > 0)                            //i was changed
                {
                    C.RemovePendingAction(i.GetID()); // remove i from PA
                    if (r == 1)                       // i must continue running
                    {
                        C.ResumeIntention(i);         // and put the intention back in I
                    }                                 // if r > 1, the event was generated and i will be back soon
                    un = bak.Clone();
                }
            }

            //Dropping from pending intentions
            foreach (Intention i in C.GetPendingIntentions().Values)
            {
                int r = DropDesire(i, g, rs, un);
                if (r > 0)
                {
                    C.RemovePendingIntention(i.GetID());
                    if (r == 1)
                    {
                        C.ResumeIntention(i);
                    }
                    un = bak.Clone();
                }
            }
        }