コード例 #1
0
ファイル: as2jHeader.cs プロジェクト: nasa03/Jasonity
 private InternalActionLiteral CheckInternalActionsInContext(ILogicalFormula f, Agent ag)
 {
     if (f != null)
     {
         if (f.GetType() == typeof(InternalActionLiteral))
         {
             InternalActionLiteral ial = (InternalActionLiteral)f;
             if (!ial.GetIA(ag).CanBeUsedInContext())
             {
                 return(ial);
             }
         }
         else if (f.GetType() == typeof(LogExpr))
         {
             LogExpr le = (LogExpr)f;
             InternalActionLiteral ial = CheckInternalActionsInContext(le.GetLHS(), ag);
             if (ial != null)
             {
                 return(ial);
             }
             if (!le.IsUnary())
             {
                 return(CheckInternalActionsInContext(le.GetRHS(), ag));
             }
         }
     }
     return(null);
 }
コード例 #2
0
ファイル: IfThenElseStdLib.cs プロジェクト: nasa03/Jasonity
        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);
        }
コード例 #3
0
 private void FixAgInIAandFunctions(Agent a, Literal l)
 {
     // if l is internal action/function
     if (l.GetType() == typeof(InternalActionLiteral))
     {
         ((InternalActionLiteral)l).SetIA(null);  // reset the IA in the literal, the IA there will be updated next getIA call
     }
     if (l.GetType() == typeof(ArithFunctionTerm))
     {
         ((ArithFunctionTerm)l).SetAgent(a);
     }
     if (l.GetType() == typeof(Rule))
     {
         ILogicalFormula f = ((Rule)l).GetBody();
         if (f.GetType() == typeof(Literal))
         {
             FixAgInIAandFunctions(a, (Literal)f);
         }
     }
     for (int i = 0; i < l.GetArity(); i++)
     {
         if (l.GetTerm(i).GetType() == typeof(Literal))
         {
             FixAgInIAandFunctions(a, (Literal)l.GetTerm(i));
         }
     }
 }
コード例 #4
0
 public void SetContext(ILogicalFormula le)
 {
     context = le;
     if (Literal.LTrue.Equals(le))
     {
         context = null;
     }
 }
コード例 #5
0
        public static ILogicalFormula ParseFormula(string sExpr)
        {
            as2j            parser = new as2j(new StringReader(sExpr));
            ILogicalFormula l      = (ILogicalFormula)parser.Log_expr();

            if (parser.getNextToken().kind != as2jConstants.EOF)
            {
                throw new ParseException("Expected <EOF> after " + l + " for parameter '" + sExpr + "'");
            }
            return(l);
        }
コード例 #6
0
ファイル: WaitStdLib.cs プロジェクト: nasa03/Jasonity
 public MyRunnable2(Circumstance c, Intention si, string sEvt, bool dropped, bool stopByTimeout, Trigger te, ILogicalFormula formula, ITerm elapsedTimeTerm, Reasoner rs, Unifier un, long startTime)
 {
     this.c               = c;
     this.si              = si;
     this.sEvt            = sEvt;
     this.dropped         = dropped;
     this.stopByTimeout   = stopByTimeout;
     this.te              = te;
     this.formula         = formula;
     this.elapsedTimeTerm = elapsedTimeTerm;
     this.rs              = rs;
     this.un              = un;
     this.startTime       = startTime;
 }
コード例 #7
0
        public override object Execute(Reasoner ts, Unifier un, ITerm[] args)
        {
            CheckArguments(args);
            ILogicalFormula       logExpr = args[0] as ILogicalFormula;
            int                   n       = 0;
            IEnumerator <Unifier> iu      = logExpr.LogicalConsequence(ts.GetAgent(), un);

            while (iu.Current != null)
            {
                iu.MoveNext();
                n++;
            }
            return(un.Unifies(args[1], new NumberTermImpl(n)));
        }
コード例 #8
0
        public override object Execute(Reasoner ts, Unifier un, ITerm[] args)
        {
            CheckArguments(args);
            ILogicalFormula       logExpr = args[1] as ILogicalFormula;
            IEnumerator <Unifier> iu      = logExpr.LogicalConsequence(ts.GetAgent(), un);

            if (iu.Current != null)
            {
                return(un.Unifies(args[0], Literal.LTrue));
            }
            else
            {
                return(un.Unifies(args[0], Literal.LFalse));
            }
        }
コード例 #9
0
ファイル: SetOffStdLib.cs プロジェクト: nasa03/Jasonity
        override public object Execute(Reasoner ts, Unifier un, ITerm[] args)
        {
            CheckArguments(args);

            ITerm                 var     = args[0];
            ILogicalFormula       logExpr = (ILogicalFormula)args[1];
            ISet <ITerm>          all     = new HashSet <ITerm>();
            IEnumerator <Unifier> iu      = logExpr.LogicalConsequence(ts.GetAgent(), un);

            while (iu.MoveNext())
            {
                all.Add(var.CApply(iu.Current));
            }
            return(un.Unifies(args[2], SetToList(all)));
        }
コード例 #10
0
ファイル: FindAllStdLib.cs プロジェクト: nasa03/Jasonity
        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));
        }
コード例 #11
0
ファイル: WaitStdLib.cs プロジェクト: nasa03/Jasonity
            public WaitEvent(Trigger te, ILogicalFormula f, Unifier un, Reasoner ts, long timeout, ITerm elapsedTimeTerm)
            {
                this.te              = te;
                this.formula         = f;
                this.un              = un;
                this.ts              = ts;
                c                    = ts.GetCircumstance();
                si                   = c.GetSelectedIntention();
                this.elapsedTimeTerm = elapsedTimeTerm;

                // register listener
                c.AddEventListener(this);

                if (te != null)
                {
                    sEvt = te.ToString();
                }
                else if (formula != null)
                {
                    sEvt = formula.ToString();
                }
                else
                {
                    sEvt = "time" + (timeout);
                }
                sEvt = si.GetID() + "/" + sEvt;
                c.AddPendingIntention(sEvt, si);

                //startTime = System.currentTimeMillis(); //hay que usar el de c# o el de unity? MISTERIO
                startTime = DateTime.Now.Millisecond;
                // Seguramente lo que querramos en C# no son los milisegundos desde el año 1970... sino algo así: Environment.TickCount
                DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

                startTime = (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;

                if (timeout >= 0)
                {
                    Agent.GetExecutor().AddTask(new MyRunnable1(this));

                    //agent.getscheduler().schedule(new runnable()
                    //{
                    //    public void run()
                    //    {
                    //        resume(true);
                    //    }
                    //    }, timeout, timeunit.milliseconds);
                }
            }
コード例 #12
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);
     }
 }
コード例 #13
0
 public Rule(Literal head, ILogicalFormula body) : base(head)
 {
     if (head.IsRule())
     {
         //logger.log(Level.SEVERE, "The rule head (" + head + ") is a rule!", new Exception());
     }
     else if (IsInternalAction())
     {
         //logger.log(Level.SEVERE,"The rule head ("+head+") can not be an internal action!", new Exception());
     }
     else if (head == LTrue || head == LFalse)
     {
         //logger.log(Level.SEVERE,"The rule head ("+head+") can not be a true or false!", new Exception());
     }
     this.body = body;
 }
コード例 #14
0
ファイル: LoopStdLib.cs プロジェクト: nasa03/Jasonity
        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);
        }
コード例 #15
0
 public bool Believes(ILogicalFormula bel, Unifier un)
 {
     try
     {
         IEnumerator <Unifier> iun = bel.LogicalConsequence(this, un);
         if (iun != null && iun.MoveNext())
         {
             un.Compose(iun.Current);
             return(true);
         }
     }
     catch (Exception e)
     {
         //logger.log(Level.SEVERE, "** Error in method believes(" + bel + "," + un + ").", e);
     }
     return(false);
 }
コード例 #16
0
        public override double Evaluate(Reasoner reasoner, ITerm[] args)
        {
            if (reasoner == null)
            {
                throw new JasonityException("The TransitionSystem parameter of the function '.count' cannot be null.");
            }
            ILogicalFormula       logExpr = (ILogicalFormula)args[0];
            int                   n       = 0;
            IEnumerator <Unifier> iu      = logExpr.LogicalConsequence(reasoner.GetAgent(), new Unifier());
            Unifier               aux     = new Unifier();

            while (iu.MoveNext())
            {
                aux = iu.Current;
                n++;
            }
            return(n);
        }
コード例 #17
0
ファイル: WaitStdLib.cs プロジェクト: nasa03/Jasonity
        override public object Execute(Reasoner ts, Unifier un, ITerm[] args)
        {
            CheckArguments(args);

            long            timeout     = -1;
            Trigger         te          = null;
            ILogicalFormula f           = null;
            ITerm           elapsedTime = null;

            if (args[0].IsNumeric())
            {
                // time in milliseconds
                INumberTerm time = (INumberTerm)args[0];
                timeout = (long)time.Solve();
            }
            else
            {
                te = Trigger.TryToGetTrigger(args[0]);   // wait for event
                if (te == null && args[0].GetType() == typeof(ILogicalFormula))
                {
                    // wait for an expression to become true
                    f = (ILogicalFormula)args[0];
                    if (ts.GetAgent().Believes(f, un))
                    {
                        // if the agent already believes f
                        // place current intention back in I, since .wait usually does not do that
                        Intention si = ts.GetCircumstance().GetSelectedIntention();
                        si.Peek().RemoveCurrentStep();
                        ts.GetCircumstance().AddRunningIntention(si);
                        return(true);
                    }
                }
                if (args.Length >= 2)
                {
                    timeout = (long)((INumberTerm)args[1]).Solve();
                }
                if (args.Length == 3)
                {
                    elapsedTime = args[2];
                }
            }
            new WaitEvent(te, f, un, ts, timeout, elapsedTime);
            return(true);
        }
コード例 #18
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;
            }
        }
コード例 #19
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);
        }
コード例 #20
0
 public static Rule CreateRule(Literal head, ILogicalFormula body)
 {
     return(new Rule(head, body));
 }
コード例 #21
0
 public Rule(Rule r, Unifier u) : base(r, u)
 {
     isTerm = r.isTerm;
     body   = (ILogicalFormula)r.body.CApply(u);
     predicateIndicatorCache = null;
 }
コード例 #22
0
 public LogExpr(LogicalOp oper, ILogicalFormula f) : base(oper.ToString(), f) => op = oper;
コード例 #23
0
 public LogExpr(ILogicalFormula f1, LogicalOp oper, ILogicalFormula f2) : base(f1, oper.ToString(), f2)
 {
     op = oper;
 }