From: <jom...@us...> - 2016-04-13 20:47:28
|
Revision: 1902 http://sourceforge.net/p/jason/svn/1902 Author: jomifred Date: 2016-04-13 20:47:26 +0000 (Wed, 13 Apr 2016) Log Message: ----------- remove sleep control from TS, it is handled now in ag archs Modified Paths: -------------- trunk/src/jason/asSemantics/TransitionSystem.java trunk/src/jason/infra/centralised/CentralisedAgArch.java trunk/src/jason/infra/centralised/RunCentralisedMAS.java Added Paths: ----------- trunk/src/jason/infra/centralised/CentralisedAgArchForPool.java Modified: trunk/src/jason/asSemantics/TransitionSystem.java =================================================================== --- trunk/src/jason/asSemantics/TransitionSystem.java 2016-04-12 12:37:14 UTC (rev 1901) +++ trunk/src/jason/asSemantics/TransitionSystem.java 2016-04-13 20:47:26 UTC (rev 1902) @@ -65,7 +65,6 @@ import jason.asSyntax.VarTerm; import jason.asSyntax.parser.ParseException; import jason.bb.BeliefBase; -import jason.infra.centralised.CentralisedAgArchAsynchronous; import jason.runtime.Settings; import jason.stdlib.add_nested_source; import jason.stdlib.desire; @@ -1409,18 +1408,13 @@ /* the actual transition system of the AS interpreter */ /**********************************************************************/ - public boolean reasoningCycle() { - if (!sense()) - return false; - if (!deliberate()) - return false; - if (!act()) - return false; - - return true; + public void reasoningCycle() { + sense(); + deliberate(); + act(); } - public boolean sense() { + public void sense() { try { if (logger.isLoggable(Level.FINE)) logger.fine("Start new reasoning cycle"); getUserAgArch().reasoningCycleStarting(); @@ -1436,53 +1430,48 @@ } nrcslbr++; // counting number of cycles since last belief revision - // TODO: move all sleep related code to archs classes - 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); - } + // produce sleep events + if (canSleep()) { + if (!sleepingEvt) { + sleepingEvt = true; + if (ag.pl.getCandidatePlans(PlanLibrary.TE_JAG_SLEEPING) != null) + C.addExternalEv(PlanLibrary.TE_JAG_SLEEPING); + } else { + //getUserAgArch().sleep(); // removes from here. sleep is in the archs } + } 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); + } while (stepSense != State.SelEv && getUserAgArch().isRunning()); } 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() { + public void deliberate() { try { C.resetDeliberate(); @@ -1495,24 +1484,22 @@ stepDeliberate = State.SelEv; do { - if (!getUserAgArch().isRunning()) return false; applySemanticRuleDeliberate(); - } while (stepDeliberate != State.ProcAct); + } while (stepDeliberate != State.ProcAct && getUserAgArch().isRunning()); } 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() { + public void act() { try { C.resetAct(); stepAct = State.ProcAct; do { - if (!getUserAgArch().isRunning()) return false; + if (!getUserAgArch().isRunning()) return; applySemanticRuleAct(); } while (stepAct != State.StartRC); @@ -1527,7 +1514,6 @@ logger.log(Level.SEVERE, "*** ERROR in the transition system (act). "+conf.C+"\nCreating a new C!", e); conf.C.create(); } - return true; } /* Modified: trunk/src/jason/infra/centralised/CentralisedAgArch.java =================================================================== --- trunk/src/jason/infra/centralised/CentralisedAgArch.java 2016-04-12 12:37:14 UTC (rev 1901) +++ trunk/src/jason/infra/centralised/CentralisedAgArch.java 2016-04-13 20:47:26 UTC (rev 1902) @@ -189,39 +189,25 @@ return running; } - protected boolean sense() { + protected void sense() { TransitionSystem ts = getTS(); int i = 0; - while (running && i < cyclesSense) { - if (!ts.sense()) { - return false; - } else if (ts.canSleepSense()) { - break; - } - i++; + while (running && i++ < cyclesSense && !ts.canSleepSense()) { + ts.sense(); } - - return true; } - protected boolean deliberate() { + protected void deliberate() { TransitionSystem ts = getTS(); int i = 0; - while (running && i < cyclesDeliberate) { - if (!ts.deliberate()) { - return false; - } else if (ts.canSleepDeliberate()) { - break; - } - i++; + while (running && i++ < cyclesDeliberate && !ts.canSleepDeliberate()) { + ts.deliberate(); } - - return true; } - protected boolean act() { + protected void act() { TransitionSystem ts = getTS(); int i = 0; @@ -229,23 +215,15 @@ if (cyclesAct == 9999) ca = ts.getC().getIntentions().size(); - while (running && i < ca) { - if (!ts.act()) { - return false; - } else if (ts.canSleepAct()) { - break; - } - i++; + while (running && i++ < ca && !ts.canSleepAct()) { + ts.act(); } - - return true; } - protected boolean reasoningCycle() { - if (!sense()) return false; - if (!deliberate()) return false; - if (!act()) return false; - return true; + protected void reasoningCycle() { + sense(); + deliberate(); + act(); } public void run() { @@ -265,6 +243,8 @@ } else { incCycleNumber(); reasoningCycle(); + if (ts.canSleep()) + sleep(); } } logger.fine("I finished!"); Added: trunk/src/jason/infra/centralised/CentralisedAgArchForPool.java =================================================================== --- trunk/src/jason/infra/centralised/CentralisedAgArchForPool.java (rev 0) +++ trunk/src/jason/infra/centralised/CentralisedAgArchForPool.java 2016-04-13 20:47:26 UTC (rev 1902) @@ -0,0 +1,89 @@ +package jason.infra.centralised; + +import java.util.concurrent.ExecutorService; + +/** an agent architecture for the infra based on thread pool */ +public final class CentralisedAgArchForPool extends CentralisedAgArch { + private volatile boolean runWakeAfterTS = false; + private volatile boolean isSleeping = false; + private ExecutorService executor; + + public void setExecutor(ExecutorService e) { + executor = e; + } + + /* + @Override + public void sleep() { + //if (sleepingAgs.contains(this)) + // System.out.println("*** ops already slepping "+this); + sleepingAgs.add(this); + }*/ + + @Override + public void sleep() { + //if (sleepingAgs.contains(this)) + // System.out.println("*** oops already sleeping "+this); +// sleepingAgs.add(this); + isSleeping = true; + // wake up the agent in 1 second anyway + + /*Agent.getScheduler().schedule(new Runnable() { + public void run() { + wake(); + } + }, MAX_SLEEP, TimeUnit.MILLISECONDS);*/ + //}, MAX_SLEEP, TimeUnit.MILLISECONDS); + } + + @Override + public synchronized void wake() { + if (isSleeping) { //sleepingAgs.remove(this)) { + isSleeping = false; + /*try { + ThreadPoolExecutor tp = (ThreadPoolExecutor)executor; + if (tp.getQueue().contains(this)) { + System.out.println("ops... ading ag that is already in the pool "+this); + } + } catch (Exception e) { }*/ + executor.execute(this); + } else { + runWakeAfterTS = true; + } + } + + /*@Override + public void receiveMsg(final Message m) { + executor.execute(new Runnable() { + public void run() { + CentralisedAgArchForPool.super.receiveMsg(m); + } + }); + }*/ + + + @Override + public void run() { + int number_cycles = getCycles(); + int i = 0; + + while (isRunning() && i++ < number_cycles) { + runWakeAfterTS = false; + //if (!getTS().reasoningCycle() && !runWakeAfterTS) { // the agent run a cycle (did not enter in sleep) + //if (!reasoningCycle()) { // the agent run a cycle (did not enter in sleep) + reasoningCycle(); + if (getTS().canSleep()) { + sleep(); + if (runWakeAfterTS) { + wake(); + } + return; + } + } + + if (isRunning()) { + executor.execute(this); + } + } +} + Modified: trunk/src/jason/infra/centralised/RunCentralisedMAS.java =================================================================== --- trunk/src/jason/infra/centralised/RunCentralisedMAS.java 2016-04-12 12:37:14 UTC (rev 1901) +++ trunk/src/jason/infra/centralised/RunCentralisedMAS.java 2016-04-13 20:47:26 UTC (rev 1902) @@ -850,7 +850,7 @@ } if (configuration == 0) { - int poolSize = ags.size(); + int poolSize = Math.min(8, ags.size()); if (poolSize > maxthreads) { poolSize = maxthreads; } @@ -872,6 +872,8 @@ if (ag.getCyclesAct() == 0) { ag.setCyclesAct(cyclesAct); } + if (ag instanceof CentralisedAgArchForPool) + ((CentralisedAgArchForPool)ag).setExecutor(executor); executor.execute(ag); } @@ -917,84 +919,6 @@ } /** an agent architecture for the infra based on thread pool */ - protected final class CentralisedAgArchForPool extends CentralisedAgArch { - private volatile boolean runWakeAfterTS = false; - - /* - @Override - public void sleep() { - //if (sleepingAgs.contains(this)) - // System.out.println("*** ops already slepping "+this); - sleepingAgs.add(this); - }*/ - - @Override - public void sleep() { - //if (sleepingAgs.contains(this)) - // System.out.println("*** oops already sleeping "+this); - sleepingAgs.add(this); - - // wake up the agent in 1 second anyway - - /*Agent.getScheduler().schedule(new Runnable() { - public void run() { - wake(); - } - }, MAX_SLEEP, TimeUnit.MILLISECONDS);*/ - //}, MAX_SLEEP, TimeUnit.MILLISECONDS); - } - - @Override - public void wake() { - if (sleepingAgs.remove(this)) { - /*try { - ThreadPoolExecutor tp = (ThreadPoolExecutor)executor; - if (tp.getQueue().contains(this)) { - System.out.println("ops... ading ag that is already in the pool "+this); - } - } catch (Exception e) { }*/ - executor.execute(this); - } else { - runWakeAfterTS = true; - } - } - - /*@Override - public void receiveMsg(final Message m) { - executor.execute(new Runnable() { - public void run() { - CentralisedAgArchForPool.super.receiveMsg(m); - } - }); - }*/ - - - @Override - public void run() { - int number_cycles = getCycles(); - int i = 0; - - //System.out.println(getAgName() + " " + number_cycles); - - while (isRunning() && i < number_cycles) { - runWakeAfterTS = false; - //if (!getTS().reasoningCycle() && !runWakeAfterTS) { // the agent run a cycle (did not enter in sleep) - if (!reasoningCycle()) { // the agent run a cycle (did not enter in sleep) - if (runWakeAfterTS) { - wake(); - } - return; - } - i++; - } - - if (isRunning()) { - executor.execute(this); - } - } - } - - /** an agent architecture for the infra based on thread pool */ protected final class CentralisedAgArchSynchronousScheduled extends CentralisedAgArch { private volatile boolean runWakeAfterTS = false; private int currentStep = 0; @@ -1013,7 +937,7 @@ } } - public boolean sense() { + public void sense() { int number_cycles = getCyclesSense(); int i = 0; @@ -1023,7 +947,7 @@ if (runWakeAfterTS) { wake(); } - return false; + return; } else if (getTS().canSleepSense()) { break; } @@ -1033,28 +957,22 @@ if (isRunning()) { executor.execute(this); } - - return true; } - public boolean deliberate() { - boolean result = super.deliberate(); + public void deliberate() { + super.deliberate(); if (isRunning()) { executor.execute(this); } - - return result; } - public boolean act() { - boolean result = super.act(); + public void act() { + super.act(); if (isRunning()) { executor.execute(this); } - - return result; } @Override This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |