Exemple #1
0
        override public object Execute(Reasoner ts, Unifier un, ITerm[] args)
        {
            CheckArguments(args);

            if (args[0].IsList())
            {
                // list reverse
                if (!args[1].IsVar() && !args[1].IsList())
                {
                    throw JasonityException.CreateWrongArgument(this, "last argument '" + args[1] + "' must be a list or a variable.");
                }

                return(un.Unifies(((IListTerm)args[0]).Reverse(), args[1]));
            }
            else
            {
                // string reverse
                if (!args[1].IsVar() && !args[1].IsString())
                {
                    throw JasonityException.CreateWrongArgument(this, "last argument '" + args[1] + "' must be a string or a variable.");
                }
                string vl = args [0].ToString();
                if (args [0].IsString())
                {
                    vl = ((IStringTerm)args [0]).GetString();
                }

                /*All this shit it's because c#'s StringBuilder doesn't have reverse method in */
                char[] charArray = new StringBuilder(vl).ToString().ToCharArray();
                Array.Reverse(charArray);
                /********************************************************************/
                return(un.Unifies(new StringTermImpl(new string(charArray) /*new StringBuilder(vl).Reverse().ToString()*/), args[1]));
            }
        }
Exemple #2
0
 protected virtual void CheckArguments(ITerm[] args)
 {
     if (args.Length < GetMinArgs() || args.Length > GetMaxArgs())
     {
         throw JasonityException.CreateWrongArgumentNb(this);
     }
 }
Exemple #3
0
        private Plan Transform2Plan(ITerm t)
        {
            Plan p = null;

            if (t.IsString())
            {
                string sPlan = ((IStringTerm)t).GetString();
                // remove quotes \" -> "
                StringBuilder sTemp = new StringBuilder();
                for (int c = 0; c < sPlan.Length; c++)
                {
                    if (sPlan.ElementAt(c) != '\\')
                    {
                        sTemp.Append(sPlan.ElementAt(c));
                    }
                }
                sPlan = sTemp.ToString();
                p     = AsSyntax.AsSyntax.ParsePlan(sPlan);
            }
            else if (t.GetType() == typeof(Plan))
            {
                p = (Plan)t;
            }
            else
            {
                throw JasonityException.CreateWrongArgument(this, "The term '" + t + "' (" + t.GetType().Name + ") can not be used as a plan for .add_plan.");
            }
            if (p.GetLabel() != null && p.GetLabel().GetFunctor().StartsWith("l__"))
            {
                // if the label is automatic label, remove it
                p.DelLabel();
            }
            return(p);
        }
Exemple #4
0
 protected override void CheckArguments(ITerm[] args)
 {
     base.CheckArguments(args);
     if (!args[0].IsAtom())
     {
         throw JasonityException.CreateWrongArgument(this, "illocutionary force argument must be an atom");
     }
 }
Exemple #5
0
 protected override void CheckArguments(ITerm[] args)
 {
     base.CheckArguments(args);
     if (!(args[0].GetType() == typeof(ILogicalFormula)))
     {
         throw JasonityException.CreateWrongArgument(this, "first argument must be a formula");
     }
 }
Exemple #6
0
 override protected void CheckArguments(ITerm[] args)
 {
     base.CheckArguments(args); // check number of arguments
     if (!args[0].IsAtom() & !args[0].IsVar())
     {
         throw JasonityException.CreateWrongArgument(this, "first argument must be an atom or variable.");
     }
 }
Exemple #7
0
 protected override void CheckArguments(ITerm[] args)
 {
     base.CheckArguments(args); //check number of arguments
     if (!args[0].IsString())
     {
         throw JasonityException.CreateWrongArgument(this, "first argument must be a string (the format)");
     }
 }
Exemple #8
0
 override protected void CheckArguments(ITerm[] args)
 {
     base.CheckArguments(args); // check number of arguments
     if (args.Length == 1 && !args[0].IsLiteral())
     {
         throw JasonityException.CreateWrongArgument(this, "first argument must be a literal");
     }
 }
Exemple #9
0
 protected override void CheckArguments(ITerm[] args)
 {
     base.CheckArguments(args); // check number of arguments
     if (!args[0].IsLiteral() && !args[0].IsVar())
     {
         throw JasonityException.CreateWrongArgument(this, "first argument must be a literal or variable");
     }
 }
Exemple #10
0
 override protected void CheckArguments(ITerm[] args)
 {
     base.CheckArguments(args); // check number of arguments
     if (!(args[0].GetType() == typeof(Atom)))
     {
         throw JasonityException.CreateWrongArgument(this, "first argument must be 'and' or 'or'.");
     }
 }
Exemple #11
0
 protected override void CheckArguments(ITerm[] args)
 {
     base.CheckArguments(args);
     if (!args[0].IsLiteral())
     {
         throw JasonityException.CreateWrongArgument(this, "first argument must be a literal");
     }
 }
Exemple #12
0
 protected override void CheckArguments(ITerm[] args)
 {
     base.CheckArguments(args); // check number of arguments
     if (!(args[1].GetType() == typeof(ILogicalFormula)))
     {
         throw JasonityException.CreateWrongArgument(this, "second argument must be a formula");
     }
 }
Exemple #13
0
 override protected void CheckArguments(ITerm[] args)
 {
     base.CheckArguments(args); // check number of arguments
     if (!(args[0].IsList()))
     {
         throw JasonityException.CreateWrongArgument(this, "first argument must be a list");
     }
 }
 protected override void CheckArguments(ITerm[] args)
 {
     base.CheckArguments(args);
     if (args.Length > 0 && !args[0].IsLiteral() && !args[0].IsVar())
     {
         throw JasonityException.CreateWrongArgument(this, "first argument '" + args[0] +
                                                     "' must be a literal or variable");
     }
 }
        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);
        }
Exemple #16
0
 protected override void CheckArguments(ITerm[] args)
 {
     base.CheckArguments(args);
     if (!args[0].IsLiteral())
     {
         if (!args[0].IsGround() && !args[0].IsRule())
         {
             throw JasonityException.CreateWrongArgument(this, "first argument must be a ground literal (or rule).");
         }
     }
 }
Exemple #17
0
 override protected void CheckArguments(ITerm[] args)
 {
     base.CheckArguments(args); // check number of arguments
     if (!args[0].IsNumeric() && !args[0].IsVar())
     {
         throw JasonityException.CreateWrongArgument(this, "the first argument should be numeric or a variable -- not '" + args[0] + "'.");
     }
     if (!args[1].IsList() && !args[1].IsString())
     {
         throw JasonityException.CreateWrongArgument(this, "the second argument should be a list or string and not '" + args[1] + "'.");
     }
 }
Exemple #18
0
 // improve the check of the arguments to also check the type of the arguments
 override protected void CheckArguments(ITerm[] args)
 {
     base.CheckArguments(args); // check number of arguments
     if (!args[0].IsList() && !args[0].IsVar())
     {
         throw JasonityException.CreateWrongArgument(this, "first argument must be a list or a variable");
     }
     if (!args[1].IsList())
     {
         throw JasonityException.CreateWrongArgument(this, "second argument must be a list");
     }
 }
Exemple #19
0
 override protected void CheckArguments(ITerm[] args)
 {
     base.CheckArguments(args); // check number of arguments
     if (!(args[0].GetType() == typeof(ILogicalFormula)))
     {
         throw JasonityException.CreateWrongArgument(this, "first argument (test) must be a logical formula.");
     }
     if (!args[1].IsPlanBody())
     {
         throw JasonityException.CreateWrongArgument(this, "second argument must be a plan body term.");
     }
 }
Exemple #20
0
 override protected void CheckArguments(ITerm[] args)
 {
     base.CheckArguments(args); // check number of arguments
     if (!args[0].IsVar())
     {
         throw JasonityException.CreateWrongArgument(this, "first argument must be a variable.");
     }
     if (args.Length == 2 && !args[1].IsNumeric())
     {
         throw JasonityException.CreateWrongArgument(this, "second argument must be a number.");
     }
 }
Exemple #21
0
 protected override void CheckArguments(ITerm[] args)
 {
     base.CheckArguments(args);
     if (args.Length > 1 && !args[1].IsString())
     {
         throw JasonityException.CreateWrongArgument(this, "second argument must be a string");
     }
     if (args.Length == 3 && !args[2].IsList())
     {
         throw JasonityException.CreateWrongArgument(this, "third argument must be a list");
     }
 }
Exemple #22
0
        override protected void CheckArguments(ITerm[] args)
        {
            base.CheckArguments(args); // check number of arguments
            if (!args[0].IsAtom() && !args[0].IsList() && !args[0].IsString())
            {
                throw JasonityException.CreateWrongArgument(this, "TO parameter ('" + args[0] + "') must be an atom, a string or a list of receivers!");
            }

            if (!args[1].IsAtom())
            {
                throw JasonityException.CreateWrongArgument(this, "illocutionary force parameter ('" + args[1] + "') must be an atom!");
            }
        }
Exemple #23
0
 protected override void CheckArguments(ITerm[] args)
 {
     base.CheckArguments(args);// check number of arguments
     if (!args[0].IsList())
     {
         throw JasonityException.CreateWrongArgument(this, "first argument '" + args[0] + "'is not a list.");
     }
     if (!args[1].IsList())
     {
         throw JasonityException.CreateWrongArgument(this, "second argument '" + args[1] + "'is not a list.");
     }
     if (!args[2].IsVar() && !args[2].IsList())
     {
         throw JasonityException.CreateWrongArgument(this, "last argument '" + args[2] + "'is not a list nor a variable.");
     }
 }
Exemple #24
0
 override protected void CheckArguments(ITerm[] args)
 {
     base.CheckArguments(args); // check number of arguments
     if (!args[1].IsNumeric())
     {
         throw JasonityException.CreateWrongArgument(this, "second parameter ('" + args[1] + "') must be a number!");
     }
     if (!args[2].IsNumeric())
     {
         throw JasonityException.CreateWrongArgument(this, "third parameter ('" + args[2] + "') must be a number!");
     }
     if (args.Length == 4 && !args[3].IsNumeric())
     {
         throw JasonityException.CreateWrongArgument(this, "fourth parameter ('" + args[3] + "') must be a number!");
     }
 }
Exemple #25
0
 protected override void CheckArguments(ITerm[] args)
 {
     base.CheckArguments(args);
     if (!(args[0].GetType() == typeof(ILogicalFormula)))
     {
         throw JasonityException.CreateWrongArgument(this, "first argument (test) must be a logical formula.");
     }
     if (!args[1].IsPlanBody())
     {
         throw JasonityException.CreateWrongArgument(this, "second argument (test) must be a plan body term.");
     }
     if (args.Length == 3 && !args[2].IsPlanBody())
     {
         throw JasonityException.CreateWrongArgument(this, "third argument (else) must be a plan body term.");
     }
 }
Exemple #26
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);
                }
            }
Exemple #27
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);
        }
Exemple #28
0
 public override object Execute(Reasoner ts, Unifier un, ITerm[] args)
 {
     if (args[0].IsList())
     {
         if (!args[args.Length - 1].IsVar() && !args[args.Length - 1].IsList())
         {
             throw new JasonityException("Last argument of concat '" + args[args.Length - 1] + "'is not a list nor a variable.");
         }
         IListTerm result = args[0].Clone() as IListTerm;
         for (int i = 1; i < args.Length - 1; i++)
         {
             if (!args[i].IsList())
             {
                 throw JasonityException.CreateWrongArgument(this, "arg[" + i + "] is not a list");
             }
             result.Concat((IListTerm)args[i].Clone());
         }
         return(un.Unifies(result, args[args.Length - 1]));
     }
     else
     {
         if (!args[args.Length - 1].IsVar() && !args[args.Length - 1].IsString())
         {
             throw JasonityException.CreateWrongArgument(this, "Last argument '" + args[args.Length - 1] + "' is not a string nor a variable.");
         }
         string vl = args[0].ToString();
         if (args[0].IsString())
         {
             vl = ((IStringTerm)args[0]).GetString();
         }
         StringBuilder sr = new StringBuilder(vl);
         for (int i = 0; i < args.Length - 1; i++)
         {
             vl = args[i].ToString();
             if (args[i].IsString())
             {
                 vl = ((IStringTerm)args[i]).GetString();
             }
             sr.Append(vl);
         }
         return(un.Unifies(new StringTermImpl(sr.ToString()), args[args.Length - 1]));
     }
 }
Exemple #29
0
        public override object Execute(Reasoner r, Unifier un, ITerm[] args)
        {
            if (args.Length > 0) //add all arguments as annotations in the exception
            {
                //find message
                ITerm  smgs = null;
                string msg  = "FailStdLib";
                foreach (ITerm t in args)
                {
                    if (t.IsStructure() && ((Structure)t).GetFunctor().Equals("Error_Msg"))
                    {
                        smgs = t;
                        ITerm tm = ((Structure)t).GetTerm(0);
                        if (tm.IsString())
                        {
                            msg = ((IStringTerm)tm).GetString();
                        }
                        else
                        {
                            msg = tm.ToString();
                        }
                        break;
                    }
                }

                JasonityException e = new JasonityException(msg);
                foreach (ITerm t in args)
                {
                    if (t != smgs)
                    {
                        e.AddErrorAnnot(t);
                    }
                }
                throw e;
            }
            return(false);
        }
Exemple #30
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);
        }