Exemplo n.º 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));
        }
Exemplo n.º 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);
            }
        }
Exemplo n.º 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);
 }
Exemplo n.º 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());
        }
Exemplo n.º 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();
            }
Exemplo n.º 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);
        }
Exemplo n.º 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));
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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));
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);

        }
Exemplo n.º 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));
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
            }
Exemplo n.º 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));
        }
Exemplo n.º 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);
        }
Exemplo n.º 18
0
        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);
        }
Exemplo n.º 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);
 }
Exemplo n.º 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);
        }