예제 #1
0
        public void MinTestEvaluate()
        {
            Reasoner r = new Reasoner(new Agent(), new Circumstance(), new AgentArchitecture(), new Settings());

            ITerm[] args = new ITerm[2];
            args[0] = new NumberTermImpl();
            args[1] = new NumberTermImpl();

            double n0     = ((INumberTerm)args[0]).Solve();
            double n1     = ((INumberTerm)args[1]).Solve();
            double minAux = Math.Min(n0, n1);

            Assert.AreEqual(minAux, a.Evaluate(r, args));
            //hasta aquí funciona

            args[0] = new ListTermImpl();
            minAux  = double.MaxValue;
            foreach (ITerm item in (IListTerm)args[0])
            {
                if (item.IsNumeric())
                {
                    double n = ((INumberTerm)item).Solve();
                    if (n > minAux)
                    {
                        minAux = n;
                    }
                }
            }
            Assert.AreEqual(minAux, a.Evaluate(r, args));
        }
예제 #2
0
        public static ITerm AddAnnotToList(ITerm l, DefaultTerm source)
        {
            if (l.IsList())
            {
                IListTerm result = new ListTermImpl();
                foreach (ITerm lTerm in (IListTerm)l)
                {
                    ITerm t = AddAnnotToList(lTerm, source);
                    if (t != null)
                    {
                        result.Add(t);
                    }
                }
                return(result);
            }
            else if (l.IsLiteral())
            {
                Literal result = ((Literal)l).ForceFullLiteralImpl().Copy();

                //Create the source annots
                Literal ts = Pred.CreateSource(source).AddAnnots(result.GetAnnots("source"));

                result.DelSources();
                result.AddAnnots(ts);
                return(result);
            }
            else
            {
                return(l);
            }
        }
예제 #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);
 }
예제 #4
0
        public void ParseListTermSimplePasses()
        {
            IListTerm lt        = new ListTermImpl(new Atom("s1"), new Atom("s2"));
            IListTerm resultado = AsSyntax.ParseList("[bigfuckinggun]");

            Assert.AreEqual(lt.ToString(), resultado.ToString());
        }
예제 #5
0
            public MyListIterator(int startIndex, ListTermImpl list)
            {
                this.list       = list;
                pos             = startIndex;
                this.startIndex = startIndex;
                last            = -1;
                ListTermImpl lti = new ListTermImpl();

                size = lti.Size();
            }
예제 #6
0
        public override object Execute(Reasoner ts, Unifier un, ITerm[] args)
        {
            CheckArguments(args);

            Trigger te = null;

            try {
                te = Trigger.TryToGetTrigger(args[0]);
            } catch (ParseException e) {}
            if (te == null)
            {
                throw JasonityException.CreateWrongArgument(this, "first argument '" + args[0] + "' must follow the syntax of a trigger.");
            }

            IListTerm labels = new ListTermImpl();
            IListTerm lt     = new ListTermImpl();
            IListTerm last   = lt;

            if (!te.GetLiteral().HasSource())
            {
                // the ts.relevantPlans requires a source to work properly
                te.SetLiteral(te.GetLiteral().ForceFullLiteralImpl());
                te.GetLiteral().AddSource(new UnnamedVar());
            }
            List <Option> rp = ts.RelevantPlans(te);

            if (rp != null)
            {
                foreach (Option opt in rp)
                {
                    // remove sources (this IA is used for communication)
                    Plan np = (Plan)opt.GetPlan().Clone();
                    if (np.GetLabel() != null)
                    {
                        np.GetLabel().DelSources();
                    }
                    np.SetAsPlanTerm(true);
                    np.MakeVarsAnnon();
                    last = last.Append(np);
                    if (args.Length == 3)
                    {
                        labels.Add(np.GetLabel());
                    }
                }
            }

            bool ok = un.Unifies(lt, args[1]); // args[1] is a var;

            if (ok && args.Length == 3)
            {
                ok = un.Unifies(labels, args[2]);
            }

            return(ok);
        }
예제 #7
0
        public override object Execute(Reasoner ts, Unifier un, ITerm[] args)
        {
            CheckArguments(args);
            IListTerm lt = new ListTermImpl();

            foreach (string a in ts.GetUserAgArch().GetRuntimeServices().DfSearch(GetService(args), GetType(args)))
            {
                lt.Add(new Atom(a));
            }
            return(un.Unifies(args[args.Length - 1], lt));
        }
예제 #8
0
        // copy the set to a new list
        private IListTerm SetToList(ISet <ITerm> set)
        {
            IListTerm result = new ListTermImpl();
            IListTerm tail   = result;

            foreach (ITerm t in set)
            {
                tail = tail.Append((ITerm)t.Clone()); // Como uso el Clone de C# lo que clono son object que luego hay que castear...
            }
            return(result);
        }
예제 #9
0
        public override object Execute(Reasoner ts, Unifier un, ITerm[] args)
        {
            CheckArguments(args);

            string fileName = null;

            if (args[0].IsString())
            {
                fileName = ((IStringTerm)args[0]).GetString();
            }
            else
            {
                fileName = args[0].ToString();
            }

            IListTerm goals = new ListTermImpl();

            if (args.Length > 1)
            {
                goals = (IListTerm)args[1];
            }

            StreamWriter bs = new StreamWriter(new FileStream(fileName, FileMode.Open)); // Not sure if this works, but it doesn't give an error

            // ORIGINAL: BufferedWriter bs = new BufferedWriter(new FileWriter(fileName));

            // store beliefs (and rules)
            //Hemos cambiado todos los Append por Write
            bs.Write("// beliefs and rules\n");
            foreach (Literal b in ts.GetAgent().GetBB())
            {
                //b = b.copy();
                //b.delSource(BeliefBase.ASelf);
                //out.append(b + ".\n");
                var baux = b.Copy();
                baux.DelSource(BeliefBase.ASelf);
                bs.Write(baux + ".\n");
            }

            // store initial goals
            bs.Write("\n\n// initial goals\n");
            foreach (ITerm g in goals)
            {
                bs.Write("!" + g + ".\n");
            }


            // store plans
            bs.Write(ts.GetAgent().GetPL().GetAsTxt(false));
            bs.Close();
            return(true);
        }
예제 #10
0
        public override object Execute(Reasoner ts, Unifier un, ITerm[] args)
        {
            CheckArguments(args);
            IRuntimeServices rs   = ts.GetUserAgArch().GetRuntimeServices();
            IListTerm        ln   = new ListTermImpl();
            IListTerm        tail = ln;

            foreach (string a in rs.GetAgentsNames())
            {
                tail = tail.Append(new Atom(a));
            }
            return(un.Unifies(args[0], ln));
        }
예제 #11
0
        public ITerm GetAsTerm()
        {
            Structure intention = new Structure("intention");

            intention.AddTerm(new NumberTermImpl(GetID()));
            IListTerm lt = new ListTermImpl();

            foreach (IntendedPlan ip in plans)
            {
                lt.Add(ip.GetAsTerm());
            }
            intention.AddTerm(lt);
            return(intention);
        }
예제 #12
0
        public void LengthTestEvaluate()
        {
            Reasoner r = new Reasoner(new Agent(), new Circumstance(), new AgentArchitecture(), new Settings());
            ITerm[] args = new ITerm[1];
            args[0] = new ListTermImpl();

            double result = a.Evaluate(r, args);
            Assert.AreEqual(((IListTerm)args[0]).Size(),result);

            args[0] = new StringTermImpl();
            result = a.Evaluate(r, args);
            Assert.AreEqual(((IStringTerm)args[0]).GetString().Length, result);

        }
예제 #13
0
        public override object Execute(Reasoner reasoner, Unifier un, ITerm[] args)
        {
            CheckArguments(args);
            ITerm                 var     = args[0];
            ILogicalFormula       logExpr = (ILogicalFormula)args[1];
            IListTerm             all     = new ListTermImpl();
            IListTerm             tail    = all;
            IEnumerator <Unifier> iu      = logExpr.LogicalConsequence(reasoner.GetAgent(), un);

            while (iu.MoveNext())
            {
                tail = tail.Append(var.CApply(iu.Current));
            }
            return(un.Unifies(args[2], all));
        }
예제 #14
0
        IListTerm DeleteFromList(int index, int end, IListTerm l)
        {
            IListTerm r    = new ListTermImpl();
            IListTerm last = r;
            int       i    = 0;

            foreach (ITerm t in l)
            {
                if (i < index || i >= end)
                {
                    last = last.Append((ITerm)t.Clone()); // Como uso el Clone de C# lo que clono son object que luego hay que castear...
                }
                i++;
            }
            return(r);
        }
예제 #15
0
            //public bool HasNext()
            //{
            //    if (open == null)
            //    {
            //        open = new List<SubSetSearchState>();
            //        ListTermImpl lti = new ListTermImpl();
            //        thisAsArray = lti.GetAsList().ToArray();
            //        //ORIGINAL: ToArray(thisAsArray);
            //        open.Insert(open.Count, new SubSetSearchState(0, k, null, null));
            //        //open.AddAfter(new SubSetSearchState(0, k, null, null));
            //    }
            //    if (next == null)
            //    {
            //        GetNext();
            //    }
            //    return next != null;
            //}

            public bool MoveNext()
            {
                if (open == null)
                {
                    open = new List <SubSetSearchState>();
                    ListTermImpl lti = new ListTermImpl();
                    thisAsArray = lti.GetAsList().ToArray();
                    //ORIGINAL: ToArray(thisAsArray);
                    open.Insert(open.Count, new SubSetSearchState(0, k, null, null));
                    //open.AddAfter(new SubSetSearchState(0, k, null, null));
                }
                if (next == null)
                {
                    GetNext();
                }
                return(next != null);
            }
예제 #16
0
        override public object Execute(Reasoner ts, Unifier un, ITerm[] args)
        {
            CheckArguments(args);

            List <ITerm> lt = ((IListTerm)args[0]).GetAsList();
            //Collections.shuffle(lt); //programarse un shuffler
            Random r = new Random();

            lt = lt.OrderBy(x => r.Next()).ToList();
            IListTerm ls = new ListTermImpl();

            foreach (ITerm i in lt)
            {
                ls.Add(i);
            }
            return(un.Unifies(args[1], ls));
        }
예제 #17
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);
        }
예제 #18
0
파일: Unifier.cs 프로젝트: nasa03/Jasonity
        public ITerm GetAsTerm()
        {
            IListTerm lf   = new ListTermImpl();
            IListTerm tail = lf;

            foreach (VarTerm k in function.Keys)
            {
                ITerm vl;
                function.TryGetValue(k, out vl);
                vl.Clone(); // Como uso el Clone de C# lo que clono son object que luego hay que castear...
                if (vl is Literal)
                {
                    ((Literal)vl).MakeVarsAnnon();
                }
                // Variable must be changed to avoid cyclic references later
                Structure pair = AsSyntax.AsSyntax.CreateStructure("dictionary", UnnamedVar.Create(k.ToString()), vl);
                tail = tail.Append(pair);
            }
            return(lf);
        }
예제 #19
0
 protected ITerm AddAnnotToList(Unifier unif, ITerm l, ITerm annot)
 {
     if (l.IsList())
     {
         IListTerm result = new ListTermImpl();
         foreach (ITerm lTerm in (IListTerm)l)
         {
             ITerm t = AddAnnotToList(unif, lTerm, annot);
             if (t != null)
             {
                 result.Add(t);
             }
         }
         return(result);
     }
     else if (l.IsLiteral())
     {
         return(((Literal)l).ForceFullLiteralImpl().Copy().AddAnnots(annot));
     }
     return(l);
 }
예제 #20
0
        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);
        }