From: <jom...@us...> - 2016-04-07 16:21:47
|
Revision: 1887 http://sourceforge.net/p/jason/svn/1887 Author: jomifred Date: 2016-04-07 16:21:43 +0000 (Thu, 07 Apr 2016) Log Message: ----------- include async arch Modified Paths: -------------- trunk/demos/cArtAgOandJasonEnv/cArtAgOandJasonEnv.mas2j trunk/lib/jacamo.jar trunk/src/jason/asSemantics/Agent.java trunk/src/jason/asSemantics/Circumstance.java trunk/src/jason/asSemantics/ConcurrentInternalAction.java trunk/src/jason/asSemantics/GoalListenerForMetaEvents.java trunk/src/jason/asSemantics/Intention.java trunk/src/jason/asSemantics/TransitionSystem.java trunk/src/jason/infra/centralised/CentralisedAgArch.java trunk/src/jason/infra/centralised/CentralisedEnvironment.java trunk/src/jason/infra/centralised/RunCentralisedMAS.java Added Paths: ----------- trunk/src/jason/infra/centralised/CentralisedAgArchAsynchronous.java trunk/src/jason/infra/components/ trunk/src/jason/infra/components/ActComponent.java trunk/src/jason/infra/components/AgentComponent.java trunk/src/jason/infra/components/CircumstanceListenerComponents.java trunk/src/jason/infra/components/DeliberateComponent.java trunk/src/jason/infra/components/SenseComponent.java Modified: trunk/demos/cArtAgOandJasonEnv/cArtAgOandJasonEnv.mas2j =================================================================== --- trunk/demos/cArtAgOandJasonEnv/cArtAgOandJasonEnv.mas2j 2016-04-06 16:33:52 UTC (rev 1886) +++ trunk/demos/cArtAgOandJasonEnv/cArtAgOandJasonEnv.mas2j 2016-04-07 16:21:43 UTC (rev 1887) @@ -1,5 +1,5 @@ /* - * This example shows how to develop an aplication that + * This example shows how to develop an application that * uses both * - a usual Jason environment and * - a CArtAgO environment Modified: trunk/lib/jacamo.jar =================================================================== (Binary files differ) Modified: trunk/src/jason/asSemantics/Agent.java =================================================================== --- trunk/src/jason/asSemantics/Agent.java 2016-04-06 16:33:52 UTC (rev 1886) +++ trunk/src/jason/asSemantics/Agent.java 2016-04-07 16:21:43 UTC (rev 1887) @@ -274,7 +274,10 @@ } public void stopAg() { - bb.stop(); + synchronized (bb.getLock()) { + bb.stop(); + } + //if (qProfiling != null) // qProfiling.show(); @@ -313,7 +316,9 @@ if (this.getTS().getSettings().verbose() >= 0) a.logger.setLevel(this.getTS().getSettings().logLevel()); - a.bb = this.bb.clone(); + synchronized (getBB().getLock()) { + a.bb = this.bb.clone(); + } a.pl = this.pl.clone(); try { fixAgInIAandFunctions(a); @@ -332,14 +337,16 @@ private void fixAgInIAandFunctions(Agent a) throws Exception { // find all internal actions and functions and change the pointer for agent - for (Plan p: a.getPL()) { - // search context - if (p.getContext() instanceof Literal) - fixAgInIAandFunctions(a, (Literal)p.getContext()); - - // search body - if (p.getBody() instanceof Literal) - fixAgInIAandFunctions(a, (Literal)p.getBody()); + synchronized (getPL().getLock()) { + for (Plan p: a.getPL()) { + // search context + if (p.getContext() instanceof Literal) + fixAgInIAandFunctions(a, (Literal)p.getContext()); + + // search body + if (p.getBody() instanceof Literal) + fixAgInIAandFunctions(a, (Literal)p.getBody()); + } } } @@ -620,7 +627,7 @@ for (Literal g: a.initialGoals) this.addInitialGoal(g); - + for (Plan p: a.getPL()) this.getPL().add(p, false); @@ -870,18 +877,20 @@ * The unifier <i>un</i> is updated by the method. */ public Literal findBel(Literal bel, Unifier un) { - Iterator<Literal> relB = bb.getCandidateBeliefs(bel, un); - if (relB != null) { - while (relB.hasNext()) { - Literal b = relB.next(); - - // recall that order is important because of annotations! - if (!b.isRule() && un.unifies(bel, b)) { - return b; + synchronized (bb.getLock()) { + Iterator<Literal> relB = bb.getCandidateBeliefs(bel, un); + if (relB != null) { + while (relB.hasNext()) { + Literal b = relB.next(); + + // recall that order is important because of annotations! + if (!b.isRule() && un.unifies(bel, b)) { + return b; + } } - } - } - return null; + } + return null; + } } @@ -918,46 +927,48 @@ position = 1; List<Literal>[] result = null; - try { - if (beliefToAdd != null) { - if (logger.isLoggable(Level.FINE)) logger.fine("Doing (add) brf for " + beliefToAdd); - - if (getBB().add(position, beliefToAdd)) { - result = new List[2]; - result[0] = Collections.singletonList(beliefToAdd); - result[1] = Collections.emptyList(); + synchronized (bb.getLock()) { + try { + if (beliefToAdd != null) { + if (logger.isLoggable(Level.FINE)) logger.fine("Doing (add) brf for " + beliefToAdd); + + if (getBB().add(position, beliefToAdd)) { + result = new List[2]; + result[0] = Collections.singletonList(beliefToAdd); + result[1] = Collections.emptyList(); + } } - } - - if (beliefToDel != null) { - Unifier u = null; - try { - u = i.peek().unif; // get from current intention - } catch (Exception e) { - u = new Unifier(); - } - - if (logger.isLoggable(Level.FINE)) logger.fine("Doing (del) brf for " + beliefToDel + " in BB=" + believes(beliefToDel, u)); - - boolean removed = getBB().remove(beliefToDel); - if (!removed && !beliefToDel.isGround()) { // then try to unify the parameter with a belief in BB - if (believes(beliefToDel, u)) { - beliefToDel = (Literal)beliefToDel.capply(u); - removed = getBB().remove(beliefToDel); + + if (beliefToDel != null) { + Unifier u = null; + try { + u = i.peek().unif; // get from current intention + } catch (Exception e) { + u = new Unifier(); } - } + + if (logger.isLoggable(Level.FINE)) logger.fine("Doing (del) brf for " + beliefToDel + " in BB=" + believes(beliefToDel, u)); - if (removed) { - if (logger.isLoggable(Level.FINE)) logger.fine("Removed:" + beliefToDel); - if (result == null) { - result = new List[2]; - result[0] = Collections.emptyList(); + boolean removed = getBB().remove(beliefToDel); + if (!removed && !beliefToDel.isGround()) { // then try to unify the parameter with a belief in BB + if (believes(beliefToDel, u)) { + beliefToDel = (Literal)beliefToDel.capply(u); + removed = getBB().remove(beliefToDel); + } } - result[1] = Collections.singletonList(beliefToDel); + + if (removed) { + if (logger.isLoggable(Level.FINE)) logger.fine("Removed:" + beliefToDel); + if (result == null) { + result = new List[2]; + result[0] = Collections.emptyList(); + } + result[1] = Collections.singletonList(beliefToDel); + } } + } catch (Exception e) { + logger.log(Level.WARNING, "Error at BRF.",e); } - } catch (Exception e) { - logger.log(Level.WARNING, "Error at BRF.",e); } return result; } @@ -1003,22 +1014,24 @@ public void abolish(Literal bel, Unifier un) throws RevisionFailedException { List<Literal> toDel = new ArrayList<Literal>(); if (un == null) un = new Unifier(); - Iterator<Literal> il = getBB().getCandidateBeliefs(bel, un); - if (il != null) { - while (il.hasNext()) { - Literal inBB = il.next(); - if (!inBB.isRule()) { - // need to clone unifier since it is changed in previous iteration - if (un.clone().unifiesNoUndo(bel, inBB)) { - toDel.add(inBB); + synchronized (bb.getLock()) { + Iterator<Literal> il = getBB().getCandidateBeliefs(bel, un); + if (il != null) { + while (il.hasNext()) { + Literal inBB = il.next(); + if (!inBB.isRule()) { + // need to clone unifier since it is changed in previous iteration + if (un.clone().unifiesNoUndo(bel, inBB)) { + toDel.add(inBB); + } } } } + + for (Literal l: toDel) { + delBel(l); + } } - - for (Literal l: toDel) { - delBel(l); - } } private void checkCustomSelectOption() { Modified: trunk/src/jason/asSemantics/Circumstance.java =================================================================== --- trunk/src/jason/asSemantics/Circumstance.java 2016-04-06 16:33:52 UTC (rev 1886) +++ trunk/src/jason/asSemantics/Circumstance.java 2016-04-07 16:21:43 UTC (rev 1887) @@ -39,7 +39,6 @@ import java.util.Queue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.concurrent.CopyOnWriteArrayList; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -68,10 +67,12 @@ private Map<String, Intention> PI; // pending intentions, intentions suspended by any other reason private Map<String, Event> PE; // pending events, events suspended by .suspend - private List<CircumstanceListener> listeners = new CopyOnWriteArrayList<CircumstanceListener>(); + private Queue<CircumstanceListener> listeners = new ConcurrentLinkedQueue<CircumstanceListener>(); private TransitionSystem ts = null; + public Object syncApPlanSense = new Object(); + public Circumstance() { create(); reset(); @@ -95,14 +96,26 @@ /** set null for A, RP, AP, SE, SO, and SI */ public void reset() { - A = null; + resetSense(); + resetDeliberate(); + resetAct(); + } + + public void resetSense() { + } + + public void resetDeliberate() { RP = null; AP = null; SE = null; - SO = null; + SO = null; + } + + public void resetAct() { + A = null; SI = null; } - + public Event addAchvGoal(Literal l, Intention i) { Event evt = new Event(new Trigger(TEOperator.add, TEType.achieve, l), i); addEvent(evt); Modified: trunk/src/jason/asSemantics/ConcurrentInternalAction.java =================================================================== --- trunk/src/jason/asSemantics/ConcurrentInternalAction.java 2016-04-06 16:33:52 UTC (rev 1886) +++ trunk/src/jason/asSemantics/ConcurrentInternalAction.java 2016-04-07 16:21:43 UTC (rev 1887) @@ -94,7 +94,7 @@ if (timeout > 0) { // schedule a future test of the end of the action - ts.getAg().getScheduler().schedule( new Runnable() { + Agent.getScheduler().schedule( new Runnable() { public void run() { // finish the IA by timeout if (C.getPendingIntentions().get(key) != null) { // test if the intention is still there @@ -107,7 +107,7 @@ } public void startInternalAction(TransitionSystem ts, Runnable code) { - ts.getAg().getScheduler().execute(code); + Agent.getScheduler().execute(code); //new Thread(code).start(); } @@ -147,7 +147,7 @@ } } }); - ts.getUserAgArch().wake(); + ts.getUserAgArch().wakeUpDeliberate(false); } public void destroy() throws Exception { Modified: trunk/src/jason/asSemantics/GoalListenerForMetaEvents.java =================================================================== --- trunk/src/jason/asSemantics/GoalListenerForMetaEvents.java 2016-04-06 16:33:52 UTC (rev 1886) +++ trunk/src/jason/asSemantics/GoalListenerForMetaEvents.java 2016-04-07 16:21:43 UTC (rev 1887) @@ -47,8 +47,9 @@ Trigger eEnd = new Trigger(TEOperator.goalState, type, newGoal); if (ts.getAg().getPL().hasCandidatePlan(eEnd)) ts.getC().insertMetaEvent(new Event(eEnd, null)); + } }); + ts.getUserAgArch().wakeUpDeliberate(false); } - } Modified: trunk/src/jason/asSemantics/Intention.java =================================================================== --- trunk/src/jason/asSemantics/Intention.java 2016-04-06 16:33:52 UTC (rev 1886) +++ trunk/src/jason/asSemantics/Intention.java 2016-04-07 16:21:43 UTC (rev 1887) @@ -176,19 +176,21 @@ Trigger failTrigger = new Trigger(TEOperator.del, tevent.getType(), tevent.getLiteral()); Iterator<IntendedMeans> ii = iterator(); int posInStak = size(); - while (!pl.hasCandidatePlan(failTrigger) && ii.hasNext()) { - // TODO: pop IM until +!g or *!g (this TODO is valid only if meta events are pushed on top of the intention) - // If *!g is found first, no failure event - // - while popping, if some meta event (* > !) is in the stack, stop and simple pop instead of producing an failure event - IntendedMeans im = ii.next(); - tevent = im.getTrigger(); - failTrigger = new Trigger(TEOperator.del, tevent.getType(), tevent.getLiteral()); - posInStak--; + synchronized (pl.getLock()) { + while (!pl.hasCandidatePlan(failTrigger) && ii.hasNext()) { + // TODO: pop IM until +!g or *!g (this TODO is valid only if meta events are pushed on top of the intention) + // If *!g is found first, no failure event + // - while popping, if some meta event (* > !) is in the stack, stop and simple pop instead of producing an failure event + IntendedMeans im = ii.next(); + tevent = im.getTrigger(); + failTrigger = new Trigger(TEOperator.del, tevent.getType(), tevent.getLiteral()); + posInStak--; + } + if (tevent.isGoal() && tevent.isAddition() && pl.hasCandidatePlan(failTrigger)) + return new Pair<Event, Integer>(new Event(failTrigger.clone(), this), posInStak); + else + return new Pair<Event, Integer>(null, 0); } - if (tevent.isGoal() && tevent.isAddition() && pl.hasCandidatePlan(failTrigger)) - return new Pair<Event, Integer>(new Event(failTrigger.clone(), this), posInStak); - else - return new Pair<Event, Integer>(null, 0); } Modified: trunk/src/jason/asSemantics/TransitionSystem.java =================================================================== --- trunk/src/jason/asSemantics/TransitionSystem.java 2016-04-06 16:33:52 UTC (rev 1886) +++ trunk/src/jason/asSemantics/TransitionSystem.java 2016-04-07 16:21:43 UTC (rev 1887) @@ -53,6 +53,7 @@ import jason.asSyntax.VarTerm; import jason.asSyntax.parser.ParseException; import jason.bb.BeliefBase; +import jason.infra.centralised.CentralisedAgArchAsynchronous; import jason.jeditplugin.Config; import jason.runtime.Settings; import jason.stdlib.add_nested_source; @@ -82,7 +83,13 @@ private AgArch agArch = null; private Circumstance C = null; private Settings setts = null; - private State step = State.StartRC; // first step of the SOS + //private State step = State.StartRC; // first step of the SOS + + private State stepSense = State.StartRC; + private State stepDeliberate = State.SelEv; + private State stepAct = State.ProcAct; + + private int nrcslbr = Settings.ODefaultNRC; // number of reasoning cycles since last belief revision private boolean sleepingEvt = false; @@ -205,7 +212,42 @@ /** ******************************************************************* */ /* SEMANTIC RULES */ /** ******************************************************************* */ + + private void applySemanticRuleSense() throws JasonException { + switch (stepSense) { + case StartRC: applyProcMsg(); break; + default: + break; + } + } + + private void applySemanticRuleDeliberate() throws JasonException { + switch (stepDeliberate) { + case SelEv: applySelEv(); break; + case RelPl: applyRelPl(); break; + case ApplPl: applyApplPl(); break; + case SelAppl: applySelAppl(); break; + case FindOp: applyFindOp(); break; + case AddIM: applyAddIM(); break; + default: + break; + } + } + private void applySemanticRuleAct() throws JasonException { + switch (stepAct) { + case ProcAct: applyProcAct(); break; + case SelInt: applySelInt(); break; + case ExecInt: applyExecInt(); break; + case ClrInt: confP.stepAct = State.StartRC; + applyClrInt(conf.C.SI); + break; + default: + break; + } + } + + /* private void applySemanticRule() throws JasonException { // check the current step in the reasoning cycle // only the main parts of the interpretation appear here @@ -227,13 +269,14 @@ break; } } + */ // the semantic rules are referred to in comments in the functions below private final String kqmlReceivedFunctor = Config.get().getKqmlFunctor(); private void applyProcMsg() throws JasonException { - confP.step = State.SelEv; + confP.stepSense = State.SelEv; if (conf.C.hasMsg()) { Message m = conf.ag.selectMessage(conf.C.getMailBox()); if (m == null) return; @@ -355,14 +398,14 @@ // Rule for atomic, if there is an atomic intention, do not select event if (C.hasAtomicIntention()) { - confP.step = State.ProcAct; // need to go to ProcAct to see if an atomic intention received a feedback action + confP.stepDeliberate = State.ProcAct; // need to go to ProcAct to see if an atomic intention received a feedback action return; } // Rule for atomic, events from atomic intention have priority confP.C.SE = C.removeAtomicEvent(); if (confP.C.SE != null) { - confP.step = State.RelPl; + confP.stepDeliberate = State.RelPl; return; } @@ -373,15 +416,15 @@ logger.fine("Selected event "+confP.C.SE); if (confP.C.SE != null) { if (ag.hasCustomSelectOption() || setts.verbose() == 2) // verbose == 2 means debug mode - confP.step = State.RelPl; + confP.stepDeliberate = State.RelPl; else - confP.step = State.FindOp; + confP.stepDeliberate = State.FindOp; return; } } // Rule SelEv2 // directly to ProcAct if no event to handle - confP.step = State.ProcAct; + confP.stepDeliberate = State.ProcAct; } private void applyRelPl() throws JasonException { @@ -391,7 +434,7 @@ // Rule Rel1 if (confP.C.RP != null || setts.retrieve()) // retrieve is mainly for Coo-AgentSpeak - confP.step = State.ApplPl; + confP.stepDeliberate = State.ApplPl; else applyRelApplPlRule2("relevant"); } @@ -402,14 +445,14 @@ // Rule Appl1 if (confP.C.AP != null || setts.retrieve()) // retrieve is mainly for Coo-AgentSpeak - confP.step = State.SelAppl; + confP.stepDeliberate = State.SelAppl; else applyRelApplPlRule2("applicable"); } /** generates goal deletion event */ private void applyRelApplPlRule2(String m) throws JasonException { - confP.step = State.ProcAct; // default next step + confP.stepDeliberate = State.ProcAct; // default next step if (conf.C.SE.trigger.isGoal() && !conf.C.SE.trigger.isMetaEvent()) { // can't carry on, no relevant/applicable plan. try { @@ -431,16 +474,16 @@ // the programmer just wanted to add the belief and it was // relevant by chance, so just carry on instead of dropping the // intention - confP.C.SI = conf.C.SE.intention; - joinRenamedVarsIntoIntentionUnifier(confP.C.SI.peek(), confP.C.SI.peek().unif); - updateIntention(); + Intention i = conf.C.SE.intention; + joinRenamedVarsIntoIntentionUnifier(i.peek(), i.peek().unif); + updateIntention(i); } else if (setts.requeue()) { // if external, then needs to check settings confP.C.addEvent(conf.C.SE); } else { // current event is external and irrelevant, // discard that event and select another one - confP.step = State.SelEv; + confP.stepDeliberate = State.SelEv; } } @@ -450,13 +493,13 @@ confP.C.SO = conf.ag.selectOption(confP.C.AP); if (confP.C.SO != null) { - confP.step = State.AddIM; + confP.stepDeliberate = State.AddIM; if (logger.isLoggable(Level.FINE)) logger.fine("Selected option "+confP.C.SO+" for event "+confP.C.SE); } else { logger.fine("** selectOption returned null!"); generateGoalDeletionFromEvent(JasonException.createBasicErrorAnnots("no_option", "selectOption returned null")); // can't carry on, no applicable plan. - confP.step = State.ProcAct; + confP.stepDeliberate = State.ProcAct; } } @@ -468,7 +511,7 @@ * @since 1.1 */ private void applyFindOp() throws JasonException { - confP.step = State.AddIM; // default next step + confP.stepDeliberate = State.AddIM; // default next step // get all relevant plans for the selected event //Trigger te = (Trigger) conf.C.SE.trigger.clone(); @@ -526,8 +569,6 @@ Term t = top.unif.get(vvl); if (t != null) { // if v has got a value in top unif, put the value in the unifier if (t instanceof Literal) { - //Literal l= (Literal)t.clone(); - //l.apply(top.unif); Literal l= (Literal)t.capply(top.unif); l.makeVarsAnnon(top.renamedVars); im.unif.function.put(vvl, l); @@ -550,11 +591,11 @@ confP.C.SE.intention.push(im); confP.C.addIntention(confP.C.SE.intention); } - confP.step = State.ProcAct; + confP.stepDeliberate = State.ProcAct; } private void applyProcAct() throws JasonException { - confP.step = State.SelInt; // default next step + confP.stepAct = State.SelInt; // default next step if (conf.C.hasFeedbackAction()) { ActionExec a = null; synchronized (conf.C.getFeedbackActions()) { @@ -569,7 +610,7 @@ if (C.removePendingAction(confP.C.SI.getId()) != null) { if (a.getResult()) { // add the intention back in I - updateIntention(); + updateIntention(conf.C.SI); applyClrInt(confP.C.SI); if (hasGoalListener()) @@ -593,7 +634,7 @@ } private void applySelInt() throws JasonException { - confP.step = State.ExecInt; // default next step + confP.stepAct = State.ExecInt; // default next step // Rule for Atomic Intentions confP.C.SI = C.removeAtomicIntention(); @@ -611,12 +652,12 @@ } } - confP.step = State.StartRC; + confP.stepAct = State.StartRC; } @SuppressWarnings("unchecked") private void applyExecInt() throws JasonException { - confP.step = State.ClrInt; // default next step + confP.stepAct = State.ClrInt; // default next step if (conf.C.SI.isFinished()) { return; @@ -629,7 +670,7 @@ if (im.isFinished()) { // for empty plans! may need unif, etc - updateIntention(); + updateIntention(conf.C.SI); return; } Unifier u = im.unif; @@ -669,7 +710,6 @@ if (bTerm instanceof Literal) body = (Literal)bTerm; - switch (h.getBodyType()) { case none: break; @@ -703,7 +743,7 @@ } if (ok && !ia.suspendIntention()) - updateIntention(); + updateIntention(conf.C.SI); } catch (JasonException e) { errorAnnots = e.getErrorTerms(); if (!generateGoalDeletion(conf.C.SI, errorAnnots)) @@ -724,7 +764,7 @@ Iterator<Unifier> iu = ((LogicalFormula)bTerm).logicalConsequence(ag, u); if (iu.hasNext()) { im.unif = iu.next(); - updateIntention(); + updateIntention(conf.C.SI); } else { String msg = "Constraint "+h+" was not satisfied ("+h.getSrcInfo()+") un="+u; generateGoalDeletion(conf.C.SI, JasonException.createBasicErrorAnnots(new Atom("constraint_failed"), msg)); @@ -736,7 +776,7 @@ case achieve: body = prepareBodyForEvent(body, u, conf.C.SI.peek()); Event evt = conf.C.addAchvGoal(body, conf.C.SI); - confP.step = State.StartRC; + confP.stepAct = State.StartRC; checkHardDeadline(evt); break; @@ -745,14 +785,14 @@ body = prepareBodyForEvent(body, u, null); evt = conf.C.addAchvGoal(body, Intention.EmptyInt); checkHardDeadline(evt); - updateIntention(); + updateIntention(conf.C.SI); break; // Rule Test case test: LogicalFormula f = (LogicalFormula)bTerm; if (conf.ag.believes(f, u)) { - updateIntention(); + updateIntention(conf.C.SI); } else { boolean fail = true; // generate event when using literal in the test (no events for log. expr. like ?(a & b)) @@ -764,7 +804,7 @@ if (ag.getPL().hasCandidatePlan(te)) { if (logger.isLoggable(Level.FINE)) logger.fine("Test Goal '" + h + "' failed as simple query. Generating internal event for it: "+te); conf.C.addEvent(evt); - confP.step = State.StartRC; + confP.stepAct = State.StartRC; fail = false; } } @@ -825,10 +865,10 @@ // generate events updateEvents(result,newfocus); if (!isSameFocus) { - updateIntention(); + updateIntention(conf.C.SI); } } else { - updateIntention(); + updateIntention(conf.C.SI); } } catch (RevisionFailedException re) { generateGoalDeletion(conf.C.SI, null); @@ -851,10 +891,10 @@ // generate events updateEvents(result,newfocus); if (!setts.sameFocus()) { - updateIntention(); + updateIntention(conf.C.SI); } } else { - updateIntention(); + updateIntention(conf.C.SI); } } catch (RevisionFailedException re) { generateGoalDeletion(conf.C.SI, null); @@ -1025,36 +1065,38 @@ } public List<Option> applicablePlans(List<Option> rp) throws JasonException { - List<Option> ap = null; - if (rp != null) { - //ap = new ApplPlanTimeOut().get(rp); - - for (Option opt: rp) { - LogicalFormula context = opt.getPlan().getContext(); - if (context == null) { // context is true - if (ap == null) ap = new LinkedList<Option>(); - ap.add(opt); - } else { - boolean allUnifs = opt.getPlan().isAllUnifs(); - Iterator<Unifier> r = context.logicalConsequence(ag, opt.getUnifier()); - if (r != null) { - while (r.hasNext()) { - opt.setUnifier(r.next()); - - if (ap == null) ap = new LinkedList<Option>(); - ap.add(opt); - - if (!allUnifs) break; // returns only the first unification - if (r.hasNext()) { - // create a new option for the next loop step - opt = new Option(opt.getPlan(), null); + synchronized (C.syncApPlanSense) { + List<Option> ap = null; + if (rp != null) { + //ap = new ApplPlanTimeOut().get(rp); + + for (Option opt: rp) { + LogicalFormula context = opt.getPlan().getContext(); + if (context == null) { // context is true + if (ap == null) ap = new LinkedList<Option>(); + ap.add(opt); + } else { + boolean allUnifs = opt.getPlan().isAllUnifs(); + Iterator<Unifier> r = context.logicalConsequence(ag, opt.getUnifier()); + if (r != null) { + while (r.hasNext()) { + opt.setUnifier(r.next()); + + if (ap == null) ap = new LinkedList<Option>(); + ap.add(opt); + + if (!allUnifs) break; // returns only the first unification + if (r.hasNext()) { + // create a new option for the next loop step + opt = new Option(opt.getPlan(), null); + } } } } - } - } + } + } + return ap; } - return ap; } public void updateEvents(List<Literal>[] result, Intention focus) { @@ -1084,10 +1126,10 @@ } /** remove the top action and requeue the current intention */ - private void updateIntention() { - if (!conf.C.SI.isFinished()) { - conf.C.SI.peek().removeCurrentStep(); - confP.C.addIntention(conf.C.SI); + private void updateIntention(Intention i) { + if (!i.isFinished()) { + i.peek().removeCurrentStep(); + confP.C.addIntention(i); } else { logger.fine("trying to update a finished intention!"); } @@ -1251,7 +1293,7 @@ e1.printStackTrace(); } - getAg().getScheduler().schedule(new Runnable() { + Agent.getScheduler().schedule(new Runnable() { public void run() { runAtBeginOfNextCycle(new Runnable() { public void run() { @@ -1271,7 +1313,7 @@ } } }); - getUserAgArch().wake(); + getUserAgArch().wakeUpSense(false); } }, deadline, TimeUnit.MILLISECONDS); } @@ -1330,9 +1372,21 @@ || (!conf.C.hasEvent() && !conf.C.hasIntention() && !conf.C.hasFeedbackAction() && !conf.C.hasMsg() && - //taskForBeginOfCycle.isEmpty() && + taskForBeginOfCycle.isEmpty() && getUserAgArch().canSleep()); } + + public boolean canSleepSense() { + return !C.hasMsg() && getUserAgArch().canSleep(); + } + + public boolean canSleepDeliberate() { + return !C.hasEvent() && taskForBeginOfCycle.isEmpty() && C.getSelectedEvent() == null && getUserAgArch().canSleep(); + } + + public boolean canSleepAct() { + return !C.hasIntention() && !C.hasFeedbackAction() && !C.hasPendingAction() && C.getSelectedIntention() == null && getUserAgArch().canSleep(); + } /** * Schedule a task to be executed in the begin of the next reasoning cycle. @@ -1349,14 +1403,132 @@ /* plus the other parts of the agent architecture besides */ /* the actual transition system of the AS interpreter */ /**********************************************************************/ - //Circumstance pc1, pc2, pc3, pc4; + public boolean reasoningCycle() { + if (!sense()) + return false; + if (!deliberate()) + return false; + if (!act()) + return false; + + return true; + } + public boolean sense() { + try { + if (logger.isLoggable(Level.FINE)) logger.fine("Start new reasoning cycle"); + getUserAgArch().reasoningCycleStarting(); + + C.resetSense(); + + if (nrcslbr >= setts.nrcbp()) { + nrcslbr = 0; + synchronized (C.syncApPlanSense) { + ag.buf(getUserAgArch().perceive()); + } + getUserAgArch().checkMail(); + } + nrcslbr++; // counting number of cycles since last belief revision + + if (!(getUserAgArch() instanceof CentralisedAgArchAsynchronous)) { + if (canSleep()) { + if (!sleepingEvt) { + sleepingEvt = true; + if (ag.pl.getCandidatePlans(PlanLibrary.TE_JAG_SLEEPING) != null) + C.addExternalEv(PlanLibrary.TE_JAG_SLEEPING); + } else { + getUserAgArch().sleep(); + return false; + } + } else if (sleepingEvt) { // code to turn idleEvt false again + if (C.hasMsg()) { // the agent has messages + sleepingEvt = false; + } else if (C.hasEvent()) { + // check if there is an event in C.E not produced by idle intention + for (Event e: C.getEvents()) { + Intention i = e.getIntention(); + if ( !e.getTrigger().equals(PlanLibrary.TE_JAG_SLEEPING) + || + (i != null && i.hasTrigger(PlanLibrary.TE_JAG_SLEEPING, new Unifier())) + ) { + sleepingEvt = false; + break; + } + } + } + if (!sleepingEvt && ag.pl.getCandidatePlans(PlanLibrary.TE_JAG_AWAKING) != null) { + C.addExternalEv(PlanLibrary.TE_JAG_AWAKING); + } + } + } + + stepSense = State.StartRC; + do { + if (!getUserAgArch().isRunning()) return false; + applySemanticRuleSense(); + } while (stepSense != State.SelEv); + + } catch (Exception e) { + logger.log(Level.SEVERE, "*** ERROR in the transition system (sense). "+conf.C+"\nCreating a new C!", e); + conf.C.create(); + } + return true; + } + public boolean deliberate() { + try { + C.resetDeliberate(); + + // run tasks allocated to be performed in the begin of the cycle + Runnable r = taskForBeginOfCycle.poll(); + while (r != null) { + r.run(); //It is processed only things related to operations on goals/intentions resumed/suspended/finished It can be placed in the deliberate stage, but the problem is the sleep when the synchronous execution is adopted + r = taskForBeginOfCycle.poll(); + } + + stepDeliberate = State.SelEv; + do { + if (!getUserAgArch().isRunning()) return false; + applySemanticRuleDeliberate(); + } while (stepDeliberate != State.ProcAct); + + } catch (Exception e) { + logger.log(Level.SEVERE, "*** ERROR in the transition system (deliberate). "+conf.C+"\nCreating a new C!", e); + conf.C.create(); + } + return true; + } + + public boolean act() { + try { + C.resetAct(); + + stepAct = State.ProcAct; + do { + if (!getUserAgArch().isRunning()) return false; + applySemanticRuleAct(); + } while (stepAct != State.StartRC); + + + ActionExec action = C.getAction(); + if (action != null) { + C.addPendingAction(action); + // We need to send a wrapper for FA to the user so that add method then calls C.addFA (which control atomic things) + getUserAgArch().act(action, C.getFeedbackActionsWrapper()); + } + } catch (Exception e) { + logger.log(Level.SEVERE, "*** ERROR in the transition system (act). "+conf.C+"\nCreating a new C!", e); + conf.C.create(); + } + return true; + } + + /* public boolean reasoningCycle() { if (logger.isLoggable(Level.FINE)) logger.fine("Start new reasoning cycle"); getUserAgArch().reasoningCycleStarting(); - + */ /* used to find bugs (ignore) int is = C.getIntentions().size(); int es = C.getEvents().size(); @@ -1383,7 +1555,7 @@ pc3 = pc2; pc2 = pc1; pc1 = C.clone();*/ - + /* try { C.reset(); @@ -1451,7 +1623,7 @@ return true; } - +*/ // Auxiliary functions // (for Internal Actions to be able to access the configuration) public Agent getAg() { @@ -1462,9 +1634,9 @@ return C; } - public State getStep() { + /*public State getStep() { return step; - } + }*/ public Settings getSettings() { return setts; Modified: trunk/src/jason/infra/centralised/CentralisedAgArch.java =================================================================== --- trunk/src/jason/infra/centralised/CentralisedAgArch.java 2016-04-06 16:33:52 UTC (rev 1886) +++ trunk/src/jason/infra/centralised/CentralisedAgArch.java 2016-04-07 16:21:43 UTC (rev 1887) @@ -33,6 +33,7 @@ import jason.asSemantics.Message; import jason.asSemantics.TransitionSystem; import jason.asSyntax.Literal; +import jason.infra.centralised.RunCentralisedMAS.RConf; import jason.mas2j.ClassParameters; import jason.runtime.RuntimeServicesInfraTier; import jason.runtime.Settings; @@ -182,17 +183,79 @@ myThread.setName(agName); } + public void startThread() { + myThread.start(); + } + public boolean isRunning() { return running; } + protected boolean reasoningCycle() { + TransitionSystem ts = getTS(); + + int i = 0; + while (running && i < cyclesSense) { + if (!ts.sense()) { + return false; + } else if (ts.canSleepSense()) { + break; + } + i++; + } + + i = 0; + while (running && i < cyclesDeliberate) { + if (!ts.deliberate()) { + return false; + } else if (ts.canSleepDeliberate()) { + break; + } + i++; + } + + /* TODO execute 1 deed from each intention + * - use -1 as a parameter for number of cycles + * - make a clone of the intentions and execute one by one (careful with concurrent access) + * - careful when intentions are removed/suspended + * */ + + i = 0; + if (cyclesAct == 9999) { //TODO adjust parameter for executing one deed from each intention + + while (running && i < ts.getC().getIntentions().size()) { + + //System.out.println("# Executing one deed from each intention #" + i + " -> " + getTS().getC().getIntentions().size()); + + if (!ts.act()) { + return false; + } else if (ts.canSleepAct()) { + break; + } + i++; + } + } else { + while (running && i < cyclesAct) { + if (!ts.act()) { + return false; + } else if (ts.canSleepAct()) { + break; + } + i++; + } + } + + return true; + } + + public void run() { synchronized (syncStopRun) { TransitionSystem ts = getTS(); while (running) { if (ts.getSettings().isSync()) { waitSyncSignal(); - ts.reasoningCycle(); + reasoningCycle(); boolean isBreakPoint = false; try { isBreakPoint = ts.getC().getSelectedOption().getPlan().hasBreakpoint(); @@ -203,7 +266,7 @@ informCycleFinished(isBreakPoint, getCycleNumber()); } else { incCycleNumber(); - ts.reasoningCycle(); + reasoningCycle(); } } } @@ -366,4 +429,53 @@ public RuntimeServicesInfraTier getRuntimeServices() { return new CentralisedRuntimeServices(masRunner); } + + private RConf conf; + + private int cycles = 0; + + private int cyclesSense = 0; + private int cyclesDeliberate = 0; + private int cyclesAct = 0; + + public int getCycles() { + return cycles; + } + + public void setConf(RConf conf) { + this.conf = conf; + } + + public RConf getConf() { + return conf; + } + + public void setCycles(int cycles) { + this.cycles = cycles; + } + + public int getCyclesSense() { + return cyclesSense; + } + + public void setCyclesSense(int cyclesSense) { + this.cyclesSense = cyclesSense; + } + + public int getCyclesDeliberate() { + return cyclesDeliberate; + } + + public void setCyclesDeliberate(int cyclesDeliberate) { + this.cyclesDeliberate = cyclesDeliberate; + } + + public int getCyclesAct() { + return cyclesAct; + } + + public void setCyclesAct(int cyclesAct) { + this.cyclesAct = cyclesAct; + } + } Added: trunk/src/jason/infra/centralised/CentralisedAgArchAsynchronous.java =================================================================== --- trunk/src/jason/infra/centralised/CentralisedAgArchAsynchronous.java (rev 0) +++ trunk/src/jason/infra/centralised/CentralisedAgArchAsynchronous.java 2016-04-07 16:21:43 UTC (rev 1887) @@ -0,0 +1,162 @@ +package jason.infra.centralised; + +import jason.asSemantics.ActionExec; +import jason.asSemantics.CircumstanceListener; +import jason.asSemantics.Message; +import jason.infra.components.ActComponent; +import jason.infra.components.DeliberateComponent; +import jason.infra.components.SenseComponent; + +import java.util.Queue; +import java.util.concurrent.ExecutorService; + +public class CentralisedAgArchAsynchronous extends CentralisedAgArch implements Runnable { + private SenseComponent senseComponent; + private DeliberateComponent deliberateComponent; + private ActComponent actComponent; + + private ExecutorService executorSense; + private ExecutorService executorDeliberate; + private ExecutorService executorAct; + + private boolean sleepingSense = false; + private boolean sleepingDeliberate = false; + private boolean sleepingAct = false; + + public Object objSense = new Object(); + public Object objDeliberate = new Object(); + public Object objAct = new Object(); + + public CentralisedAgArchAsynchronous() { + super(); + + senseComponent = new SenseComponent(this); + deliberateComponent = new DeliberateComponent(this); + actComponent = new ActComponent(this); + } + + + public void wakeUpSense(boolean ts) { + synchronized (objSense) { + if (ts || sleepingSense) { + sleepingSense = false; + senseComponent.enqueueExecutor(ts); + } + } + } + + public void wakeUpDeliberate(boolean ts) { + synchronized (objDeliberate) { + if (ts || sleepingDeliberate) { + sleepingDeliberate = false; + deliberateComponent.enqueueExecutor(ts); + } + } + } + + public void wakeUpAct(boolean ts) { + synchronized (objAct) { + if (ts || sleepingAct) { + sleepingAct = false; + actComponent.enqueueExecutor(ts); + } + } + } + + public void sleepSense() { + sleepingSense = true; + } + + public boolean isSleepingSense() { + return sleepingSense; + } + + + public void sleepDeliberate() { + sleepingDeliberate = true; + } + + public boolean isSleepingDeliberate() { + return sleepingDeliberate; + } + + public void sleepAct() { + sleepingAct = true; + } + + public boolean isSleepingAct() { + return sleepingAct; + } + + public boolean canSleepSense() { + return getTS().canSleepSense(); + } + + public boolean canSleepDeliberate() { + return getTS().canSleepDeliberate(); + } + + public boolean canSleepAct() { + return getTS().canSleepAct(); + } + + public SenseComponent getSenseComponent() { + return senseComponent; + } + + public DeliberateComponent getDeliberateComponent() { + return deliberateComponent; + } + + public ActComponent getActComponent() { + return actComponent; + } + + public ExecutorService getExecutorSense() { + return executorSense; + } + + + public ExecutorService getExecutorDeliberate() { + return executorDeliberate; + } + + public ExecutorService getExecutorAct() { + return executorAct; + } + + public void setExecutorAct(ExecutorService executorAct) { + this.executorAct = executorAct; + } + + public void setExecutorSense(ExecutorService executorSense) { + this.executorSense = executorSense; + } + + public void setExecutorDeliberate(ExecutorService executorDeliberate) { + this.executorDeliberate = executorDeliberate; + } + + public void setSenseComponent(SenseComponent senseComponent) { + this.senseComponent = senseComponent; + } + + public void addListenerToC(CircumstanceListener listener) { + getTS().getC().addEventListener(listener); + } + + public void receiveMsg(Message m) { + synchronized (objSense) { + ((Queue<Message>) getMBox()).offer(m); + wakeUpSense(false); + } + } + + /** called the the environment when the action was executed */ + public void actionExecuted(ActionExec action) { + synchronized (objSense) { + getTS().getC().addFeedbackAction(action); + wakeUpSense(false); + } + } +} Modified: trunk/src/jason/infra/centralised/CentralisedEnvironment.java =================================================================== --- trunk/src/jason/infra/centralised/CentralisedEnvironment.java 2016-04-06 16:33:52 UTC (rev 1886) +++ trunk/src/jason/infra/centralised/CentralisedEnvironment.java 2016-04-07 16:21:43 UTC (rev 1887) @@ -99,13 +99,17 @@ public void informAgsEnvironmentChanged(String... agents) { if (agents.length == 0) { for (CentralisedAgArch ag: masRunner.getAgs().values()) { - ag.wake(); + ag.getTS().getUserAgArch().wakeUpSense(false); } } else { for (String agName: agents) { CentralisedAgArch ag = masRunner.getAg(agName); if (ag != null) { - ag.wake(); + if (ag instanceof CentralisedAgArchAsynchronous) { + ((CentralisedAgArchAsynchronous) ag.getTS().getUserAgArch()).wakeUpSense(false); + } else { + ag.wake(); + } } else { logger.log(Level.SEVERE, "Error sending message notification: agent " + agName + " does not exist!"); } @@ -121,7 +125,7 @@ for (String agName: agentsToNotify) { CentralisedAgArch ag = masRunner.getAg(agName); if (ag != null) { - ag.wake(); + ag.getTS().getUserAgArch().wakeUpSense(false); } else { logger.log(Level.SEVERE, "Error sending message notification: agent " + agName + " does not exist!"); } Modified: trunk/src/jason/infra/centralised/RunCentralisedMAS.java =================================================================== --- trunk/src/jason/infra/centralised/RunCentralisedMAS.java 2016-04-06 16:33:52 UTC (rev 1886) +++ trunk/src/jason/infra/centralised/RunCentralisedMAS.java 2016-04-07 16:21:43 UTC (rev 1887) @@ -29,6 +29,7 @@ import jason.asSyntax.directives.Include; import jason.bb.DefaultBeliefBase; import jason.control.ExecutionControlGUI; +import jason.infra.components.CircumstanceListenerComponents; import jason.infra.repl.ReplAgGUI; import jason.jeditplugin.Config; import jason.mas2j.AgentParameters; @@ -95,6 +96,14 @@ public JButton btDebug; + public enum RConf { + TRHEADED, + POOL_SYNCH, + POOL_SYNCH_SCHEDULED, + ASYNCH, + ASYNCH_SHARED_POOLS + } + public RunCentralisedMAS() { runner = this; } @@ -361,7 +370,6 @@ //f.getContentPane().add(BorderLayout.CENTER,mindPanel); final JTextField n = new JTextField(30); n.addActionListener(new ActionListener() { - @Override public void actionPerformed(ActionEvent e) { f.setVisible(false); createReplAg(n.getText()); @@ -422,10 +430,26 @@ env = new CentralisedEnvironment(project.getEnvClass(), this); } } + public void createAgs() throws JasonException { - boolean isPool = project.getInfrastructure().hasParameter("pool"); - if (isPool) logger.info("Creating agents...."); + + RConf generalConf; + if (project.getInfrastructure().hasParameter("pool")) { + generalConf = RConf.POOL_SYNCH; + } else if (project.getInfrastructure().hasParameter("synch_scheduled")) { + generalConf = RConf.POOL_SYNCH_SCHEDULED; + } else if (project.getInfrastructure().hasParameter("asynch")) { + generalConf = RConf.ASYNCH; + } else if (project.getInfrastructure().hasParameter("asynch_shared")) { + generalConf = RConf.ASYNCH_SHARED_POOLS; + } else { + generalConf = RConf.TRHEADED; + } + + //boolean isPool = project.getInfrastructure().hasParameter("pool") || project.getInfrastructure().hasParameter("synch_scheduled"); + //boolean isAsynch = project.getInfrastructure().hasParameter("asynch") || project.getInfrastructure().hasParameter("asynch_shared"); + if (generalConf != RConf.TRHEADED) logger.info("Creating agents...."); int nbAg = 0; Agent pag = null; @@ -453,22 +477,107 @@ logger.fine("Creating agent " + numberedAg + " (" + (cAg + 1) + "/" + ap.getNbInstances() + ")"); CentralisedAgArch agArch; - if (isPool) { + + RConf agentConf; + if (ap.getOption("rc") != null) { + if (ap.getOption("rc").equals("pool")) { + agentConf = RConf.POOL_SYNCH; + } else if (ap.getOption("rc").equals("synch_scheduled")) { + agentConf = RConf.POOL_SYNCH_SCHEDULED; + } else if (ap.getOption("rc").equals("asynch")) { + agentConf = RConf.ASYNCH; + } else if (ap.getOption("rc").equals("asynch_shared")) { + agentConf = RConf.ASYNCH_SHARED_POOLS; + } else { + agentConf = RConf.TRHEADED; + } + } else { + agentConf = generalConf; + } + + //Get the number of reasoning cycles or number of cycles for each stage + int cycles = 0; + int cyclesSense = 0; + int cyclesDeliberate = 0; + int cyclesAct = 0; + + if (ap.getOption("cycles") != null) { + cycles = Integer.valueOf(ap.getOption("cycles")); + } + if (ap.getOption("cycles_sense") != null) { + cyclesSense = Integer.valueOf(ap.getOption("cycles_sense")); + } + if (ap.getOption("cycles_deliberate") != null) { + cyclesDeliberate = Integer.valueOf(ap.getOption("cycles_deliberate")); + } + if (ap.getOption("cycles_act") != null) { + cyclesAct = Integer.valueOf(ap.getOption("cycles_act")); + } + + //Create agents according to the specific architecture + if (agentConf == RConf.POOL_SYNCH) { agArch = new CentralisedAgArchForPool(); + } else if (agentConf == RConf.POOL_SYNCH_SCHEDULED) { + agArch = new CentralisedAgArchSynchronousScheduled(); + if (cycles != 0) { + if (cyclesSense == 0) { + cyclesSense = cycles; + } + if (cyclesDeliberate == 0) { + cyclesDeliberate = cycles; + } + if (cyclesAct == 0) { + cyclesAct = cycles; + } + cycles = 1; + } + } else if (agentConf == RConf.ASYNCH || agentConf == RConf.ASYNCH_SHARED_POOLS) { + agArch = new CentralisedAgArchAsynchronous(); + if (cycles != 0) { + if (cyclesSense == 0) { + cyclesSense = cycles; + } + if (cyclesDeliberate == 0) { + cyclesDeliberate = cycles; + } + if (cyclesAct == 0) { + cyclesAct = cycles; + } + cycles = 1; + } } else { agArch = new CentralisedAgArch(); + if (cycles != 0) { + if (cyclesSense == 0) { + cyclesSense = cycles; + } + if (cyclesDeliberate == 0) { + cyclesDeliberate = cycles; + } + if (cyclesAct == 0) { + cyclesAct = cycles; + } + cycles = 1; + } } + + agArch.setCycles(cycles); + agArch.setCyclesSense(cyclesSense); + agArch.setCyclesDeliberate(cyclesDeliberate); + agArch.setCyclesAct(cyclesAct); + + agArch.setConf(agentConf); agArch.setAgName(numberedAg); agArch.setEnvInfraTier(env); - if (isPool && cAg > 0 && ap.getAgArchClasses().isEmpty() && ap.getBBClass().equals(DefaultBeliefBase.class.getName())) { + if ((generalConf != RConf.TRHEADED) && cAg > 0 && ap.getAgArchClasses().isEmpty() && ap.getBBClass().equals(DefaultBeliefBase.class.getName())) { // creation by cloning previous agent (which is faster -- no parsing, for instance) - // used if not customisations are defined agArch.createArchs(ap.getAgArchClasses(), pag, this); } else { // normal creation agArch.createArchs(ap.getAgArchClasses(), ap.agClass.getClassName(), ap.getBBClass(), ap.asSource.toString(), ap.getAsSetts(debug, project.getControlClass() != null), this); } addAg(agArch); + ... [truncated message content] |