Exemple #1
0
        private void DelegateSendToArch(ITerm to, Reasoner ts, Message m)
        {
            if (!to.IsAtom() && !to.IsString())
            {
                throw new JasonityException("The TO parameter ('" + to + "') of the internal action 'send' is not an atom!");
            }

            string rec = null;

            if (to.IsString())
            {
                rec = ((IStringTerm)to).GetString();
            }
            else if (to.IsAtom())
            {
                rec = ((Atom)to).GetFunctor();      // remove annotations
            }
            else
            {
                rec = to.ToString();
            }
            if (rec.Equals("self"))
            {
                rec = ts.GetUserAgArch().GetAgentName();
            }
            m.SetReceiver(rec);
            ts.GetUserAgArch().SendMessage(m);
        }
Exemple #2
0
        public override object Execute(Reasoner ts, Unifier un, ITerm[] args)
        {
            string           agName   = ((IStringTerm)args[0]).GetString();
            IRuntimeServices services = ts.GetUserAgArch().GetRuntimeServices();

            services.Clone(ts.GetAgent(), ts.GetUserAgArch().GetAgArchClassesChain(), agName);

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

            ITerm ilf  = args[0];
            ITerm pcnt = args[0];

            Message m = new Message(ilf.ToString(), ts.GetUserAgArch().GetAgentName(), null, pcnt);

            ts.GetUserAgArch().Broadcast(m);
            return(true);
        }
Exemple #4
0
        public override object Execute(Reasoner reasoner, Unifier un, ITerm[] args)
        {
            CheckArguments(args);
            string name;

            if (args[0].IsString())
            {
                name = ((IStringTerm)args[0]).GetString();
            }
            else
            {
                name = args[0].ToString();
            }
            return(reasoner.GetUserAgArch().GetRuntimeServices().KillAgent(name, reasoner.GetUserAgArch().GetAgentName()));
        }
Exemple #5
0
        override public object Execute(Reasoner ts, Unifier un, ITerm[] args)
        {
            CheckArguments(args);

            IRuntimeServices rs = ts.GetUserAgArch().GetRuntimeServices();

            rs.StopMAS();
            return(true);
        }
Exemple #6
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));
        }
Exemple #7
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));
        }
Exemple #8
0
 public void Run()
 {
     try
     {
         if (!cancelled)
         {
             ts.GetCircumstance().AddEvent(@event);
             ts.GetUserAgArch().Wake();
         }
     }
     finally
     {
         CheckDeadline result;
         ats.TryRemove(id, out result);
     }
 }
Exemple #9
0
        public override object Execute(Reasoner ts, Unifier un, ITerm[] args)
        {
            CheckArguments(args);
            string name   = GetName(args);
            string source = GetSource(args);

            List <string> agArchClasses = GetAgArchClasses(args);

            string          agClass = null;
            ClassParameters bbPars  = null;

            if (args.Length > 2)
            {
                foreach (ITerm t in args[2] as IListTerm)
                {
                    if (t.IsStructure())
                    {
                        Structure s = t as Structure;
                        if (s.GetFunctor().Equals("beliefBaseClass"))
                        {
                            bbPars = new ClassParameters(TestString(s.GetTerm(0)));
                        }
                        else if (s.GetFunctor().Equals("agentClass"))
                        {
                            agClass = TestString(s.GetTerm(0)).ToString();
                        }
                    }
                }
            }
            IRuntimeServices rs = ts.GetUserAgArch().GetRuntimeServices();

            name = rs.CreateAgent(name, source, agClass, agArchClasses, GetSettings(), ts.GetAgent());
            rs.StartAgent(name);

            if (args[0].IsVar())
            {
                return(un.Unifies(new StringTermImpl(name), args[0]));
            }
            else
            {
                return(true);
            }
        }
Exemple #10
0
            public void Resume(bool stopByTimeout)
            {
                // unregister (to not receive intentionAdded again)
                c.RemoveEventListener(this);

                // invoke changes in C latter, so to avoid concurrent changes in C
                ts.RunAtBeginOfNextCycle(new MyRunnable2(c, si, sEvt, dropped, stopByTimeout, te, formula, elapsedTimeTerm, ts, un, startTime));

                /*{
                 * 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
                 *                  PlanBody body = si.peek().getPlan().getBody();
                 *                  body.add(1, new PlanBodyImpl(BodyType.internalAction, new InternalActionLiteral(".fail")));
                 *                  c.addPendingIntention(suspend.SUSPENDED_INT+si.getId(), si);
                 *              } else {
                 *                  ts.generateGoalDeletion(si, JasonException.createBasicErrorAnnots("wait_timeout", "timeout in .wait"));
                 *              }
                 *          } else if (! si.isFinished()) {
                 *              si.peek().removeCurrentStep();
                 *
                 *              if (elapsedTimeTerm != null) {
                 *                  long elapsedTime = System.currentTimeMillis() - startTime;
                 *                  un.unifies(elapsedTimeTerm, new NumberTermImpl(elapsedTime));
                 *              }
                 *              if (si.isSuspended()) { // if the intention was suspended by .suspend
                 *                  c.addPendingIntention(suspend.SUSPENDED_INT+si.getId(), si);
                 *              } else {
                 *                  c.resumeIntention(si);
                 *              }
                 *          }
                 *      }
                 *  } catch (Exception e) {
                 *      ts.getLogger().log(Level.SEVERE, "Error at .wait thread", e);
                 *  }
                 * }
                 * }*/
                ts.GetUserAgArch().WakeUpDeliberate();
            }
Exemple #11
0
        public override object Execute(Reasoner ts, Unifier un, ITerm[] args)
        {
            CheckArguments(args);
            ITerm to   = args[0];
            ITerm ilf  = args[1];
            ITerm pcnt = args[2];

            // create a message to be sent
            Message m = new Message(ilf.ToString(), ts.GetUserAgArch().GetAgentName(), null, pcnt);

            // async ask has a fourth argument and should suspend the intention
            lastSendWasSynAsk = m.IsAsk() && args.Length > 3;
            if (lastSendWasSynAsk)
            {
                m.SetSyncAskMsgID();
                ts.GetCircumstance().AddPendingIntention(m.GetMsgID(), ts.GetCircumstance().GetSelectedIntention());
            }

            // (un)tell or unknown performative with 4 args is a reply to
            if ((m.IsTell() || m.IsUntell() || !m.IsKnownPerformative()) && args.Length > 3)
            {
                ITerm mid = args[3];
                if (!mid.IsAtom())
                {
                    throw new JasonityException("The Message ID ('" + mid + "') parameter of the internal action 'send' is not an atom!");
                }
                m.SetInReplyTo(mid.ToString());
            }

            // send the message
            if (to.IsList())
            {
                foreach (ITerm t in (IListTerm)to)
                {
                    DelegateSendToArch(t, ts, m);
                }
            }
            else
            {
                DelegateSendToArch(to, ts, m);
            }

            if (lastSendWasSynAsk && args.Length == 5)
            {
                // get the timeout deadline
                ITerm tto = args[4];
                if (tto.IsNumeric())
                {
                    //    Agent.GetScheduler().schedule(new Runnable()
                    //    {
                    //        public void run()
                    //        {
                    //            // if the intention is still in PI, brings it back to C.I with the timeout
                    //            Intention intention = ts.GetCircumstance().RemovePendingIntention(m.GetMsgId());
                    //            if (intention != null)
                    //            {
                    //                // unify "timeout" with the fourth parameter of .send
                    //                Structure send = (Structure)intention.Peek().RemoveCurrentStep();
                    //                ITerm timeoutAns = null;
                    //                if (to.IsList())
                    //                {
                    //                    VarTerm answers = new VarTerm("AnsList___" + m.GetMsgId());
                    //                    Unifier un = intention.Peek().GetUnif();
                    //                    timeoutAns = un.Get(answers);
                    //                    if (timeoutAns == null)
                    //                        timeoutAns = new ListTermImpl();
                    //                }
                    //                else
                    //                {
                    //                    timeoutAns = new Atom("timeout");
                    //                }
                    //                intention.Peek().GetUnif().Unifies(send.GetTerm(3), timeoutAns);
                    //                // add the intention back in C.I
                    //                ts.GetCircumstance().ResumeIntention(intention);
                    //                ts.GetUserAgArch().WakeUpAct();
                    //            }
                    //        }
                    //}, (long)((NumberTerm)tto).Solve(), TimeUnit.MILLISECONDS);
                }
                else
                {
                    throw new JasonityException("The 5th parameter of send must be a number (timeout) and not '" + tto + "'!");
                }
            }

            return(true);
        }
Exemple #12
0
 public override object Execute(Reasoner ts, Unifier un, ITerm[] args)
 {
     CheckArguments(args);
     ts.GetUserAgArch().GetRuntimeServices().DfRegister(ts.GetUserAgArch().GetAgentName(), GetService(args), GetType(args));
     return(true);
 }
Exemple #13
0
 override public object Execute(Reasoner ts, Unifier un, ITerm[] args)
 {
     CheckArguments(args);
     ts.GetAgent().Buf(ts.GetUserAgArch().Perceive());
     return(true);
 }
Exemple #14
0
 // we always have a next random number
 public bool MoveNext()
 {
     return((n < max || max == 0) && ts.GetUserAgArch().IsRunning());
 }
Exemple #15
0
 private void GenerateDesireStateEvent(Literal desire, TEType type, DesireStates state, String reason)
 {
     reasoner.RunAtBeginOfNextCycle(new RunnableImpl(desire, state, reason, type, reasoner));
     reasoner.GetUserAgArch().WakeUpDeliberate();
 }
Exemple #16
0
 override public object Execute(Reasoner ts, Unifier un, ITerm[] args)
 {
     CheckArguments(args);
     return(un.Unifies(args[0], new Atom(ts.GetUserAgArch().GetAgentName())));
 }