From a9e6639e572853ffacb439d0b0488db528afc48c Mon Sep 17 00:00:00 2001 From: fab Date: Mon, 17 Feb 2020 12:38:17 +0100 Subject: [PATCH] introduce the OrganizationSnapshot class as an alias for the Organization data structure --- .classpath | 9 +-- resource/VERSION | 9 +++ resource/madkitKernelBuild.properties | 4 +- src/madkit/agr/OrganizationSnapshot.java | 55 +++++++++++++++++++ src/madkit/kernel/AbstractAgent.java | 6 +- src/madkit/kernel/Activator.java | 4 +- src/madkit/kernel/KernelConnection.java | 10 ++-- src/madkit/kernel/MadkitKernel.java | 18 ++++-- src/madkit/kernel/NetworkAgent.java | 7 ++- src/madkit/kernel/Role.java | 2 +- src/madkit/kernel/madkit.properties | 2 +- .../DateBasedDiscreteEventActivator.java | 2 +- .../simulation/scheduler/package-info.java | 48 ++++++++++++++++ .../abstractAgent/KillAbstractAgentTest.java | 32 ++++++++--- 14 files changed, 177 insertions(+), 31 deletions(-) create mode 100644 src/madkit/agr/OrganizationSnapshot.java create mode 100644 src/madkit/simulation/scheduler/package-info.java diff --git a/.classpath b/.classpath index cd170c45..24ce7fd2 100644 --- a/.classpath +++ b/.classpath @@ -1,11 +1,12 @@ + + + - - + + - - diff --git a/resource/VERSION b/resource/VERSION index 4f55d70e..0fe3b4f6 100644 --- a/resource/VERSION +++ b/resource/VERSION @@ -15,6 +15,15 @@ It also contains generic MAS simulation authoring tools. ---------------------------------------------------------------------------------- +---------------------------------------------------------------------------------- +------------------------------- MaDKit 5.3.1 ------------------------------------- +---------------------------------------------------------------------------------- + +Changes: + + Adding an event-based scheduling : + + madkit.simulation.activator.DateBasedDiscreteEventActivator + + madkit.simulation.scheduler.DateBasedDiscreteEventScheduler + ---------------------------------------------------------------------------------- ------------------------------- MaDKit 5.3 --------------------------------------- ---------------------------------------------------------------------------------- diff --git a/resource/madkitKernelBuild.properties b/resource/madkitKernelBuild.properties index 1e0ec27f..4e2f032a 100644 --- a/resource/madkitKernelBuild.properties +++ b/resource/madkitKernelBuild.properties @@ -24,8 +24,8 @@ web.codebase=${madkit.web}/${madkit.ftp.dir}/${javaws.ftp.dir} github.url=https://github.com/fmichel/MaDKit #build resources -sun.api.url=http://docs.oracle.com/javase/7/docs/api/ -jnlp.api.url=http://docs.oracle.com/javase/7/docs/jre/api/javaws/jnlp/ +sun.api.url=https://docs.oracle.com/javase/8/docs/api/ +jnlp.api.url=https://docs.oracle.com/javase/8/docs/jre/api/javaws/jnlp/ #files ref jar.file=${release.dir}/${madkit.jar.name} diff --git a/src/madkit/agr/OrganizationSnapshot.java b/src/madkit/agr/OrganizationSnapshot.java new file mode 100644 index 00000000..982f86ce --- /dev/null +++ b/src/madkit/agr/OrganizationSnapshot.java @@ -0,0 +1,55 @@ +/* + * Copyright or © or Copr. Fabien Michel, Olivier Gutknecht, Jacques Ferber (1997) + +fmichel@lirmm.fr +olg@no-distance.net +ferber@lirmm.fr + +This software is a computer program whose purpose is to +provide a lightweight Java library for designing and simulating Multi-Agent Systems (MAS). + +This software is governed by the CeCILL-C license under French law and +abiding by the rules of distribution of free software. You can use, +modify and/ or redistribute the software under the terms of the CeCILL-C +license as circulated by CEA, CNRS and INRIA at the following URL +"http://www.cecill.info". + +As a counterpart to the access to the source code and rights to copy, +modify and redistribute granted by the license, users are provided only +with a limited warranty and the software's author, the holder of the +economic rights, and the successive licensors have only limited +liability. + +In this respect, the user's attention is drawn to the risks associated +with loading, using, modifying and/or developing or reproducing the +software by the user in light of its specific status of free software, +that may mean that it is complicated to manipulate, and that also +therefore means that it is reserved for developers and experienced +professionals having in-depth computer knowledge. Users are therefore +encouraged to load and test the software's suitability as regards their +requirements in conditions enabling the security of their systems and/or +data to be ensured and, more generally, to use and operate it in the +same conditions as regards security. + +The fact that you are presently reading this means that you have had +knowledge of the CeCILL-C license and that you accept its terms. + */ +package madkit.agr; + +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; + +import madkit.kernel.AgentAddress; + + +/** + * This class is just an alias for using the underlying data structure + * + * @author Fabien Michel + * @since MaDKit 5.3.1 + * @version 5.0 + */ +public class OrganizationSnapshot extends TreeMap>>> { + +} diff --git a/src/madkit/kernel/AbstractAgent.java b/src/madkit/kernel/AbstractAgent.java index a6d673b5..8934a586 100644 --- a/src/madkit/kernel/AbstractAgent.java +++ b/src/madkit/kernel/AbstractAgent.java @@ -58,7 +58,6 @@ import java.util.List; import java.util.Map; import java.util.ResourceBundle; -import java.util.Set; import java.util.TreeSet; import java.util.concurrent.BlockingDeque; import java.util.concurrent.ConcurrentHashMap; @@ -84,6 +83,7 @@ import madkit.agr.LocalCommunity; import madkit.agr.LocalCommunity.Groups; import madkit.agr.NetworkCommunity; +import madkit.agr.OrganizationSnapshot; import madkit.gui.AgentFrame; import madkit.gui.AgentStatusPanel; import madkit.gui.OutputPanel; @@ -919,6 +919,7 @@ public void setName(final String name) {// TODO trigger gui changes and so on ne * @see #logger * @deprecated as of MaDKit 5.2, {@link AgentLogger#setLevel(Level)} should now be used through {@link #getLogger()} */ + @Deprecated public void setLogLevel(final Level newLevel) { getLogger().setLevel(newLevel); // setKernel(kernel.getLoggedKernel());//TODO @@ -1831,7 +1832,7 @@ public void setupFrame(final AgentFrame frame) { * if true this takes into account agents coming from other connected kernels * @return a data containing all the organization structure */ - public Map>>> getOrganizationSnapShot(boolean global) { + public OrganizationSnapshot getOrganizationSnapShot(boolean global) { return getKernel().getOrganizationSnapShot(global); } @@ -2827,6 +2828,7 @@ public enum ReturnCode { // static ResourceBundle messages = // I18nUtilities.getResourceBundle(ReturnCode.class); + @Override public String toString() { return messages.getString(name()); } diff --git a/src/madkit/kernel/Activator.java b/src/madkit/kernel/Activator.java index c462b113..5cadfe30 100644 --- a/src/madkit/kernel/Activator.java +++ b/src/madkit/kernel/Activator.java @@ -84,7 +84,7 @@ public Activator(String community, String group, String role) { } /** - * The priority of this activator when {@link #getNextActivationDate()} is conflicting with another Activator. A + * The priority of this activator when conflicting with another Activator. A * lesser priority means that the activator will be triggered first. * When the activator's priority is not set, a default priority is defined using the order in which the * activators are added to the simulation engine using {@link Scheduler#addActivator(Activator)}, that is @@ -182,6 +182,7 @@ protected void multicoreExecute(final Object... args) { final int index = i; workers.add(new Callable() { + @Override public Void call() throws Exception { int firstIndex = nbOfAgentsPerTask * index;// TODO check that using junit execute(list.subList(firstIndex, firstIndex + nbOfAgentsPerTask), args); @@ -191,6 +192,7 @@ public Void call() throws Exception { } workers.add(new Callable() { + @Override public Void call() throws Exception { execute(list.subList(nbOfAgentsPerTask * cpuCoreNb, list.size()), args); return null; diff --git a/src/madkit/kernel/KernelConnection.java b/src/madkit/kernel/KernelConnection.java index 30f2e4ef..c92469dd 100644 --- a/src/madkit/kernel/KernelConnection.java +++ b/src/madkit/kernel/KernelConnection.java @@ -44,10 +44,10 @@ import java.net.Socket; import java.net.SocketException; import java.net.UnknownHostException; -import java.util.Map; -import java.util.Set; import java.util.logging.Level; +import madkit.agr.OrganizationSnapshot; + /** * @author Fabien Michel * @version 0.9 @@ -100,8 +100,8 @@ public KernelConnection(NetworkAgent netAgent, Socket kernelClient) throws IOExc * @throws IOException */ @SuppressWarnings("unchecked") - Map>>> waitForDistantOrg() throws IOException, ClassNotFoundException { - return (Map>>>) ois.readObject(); + OrganizationSnapshot waitForDistantOrg() throws IOException, ClassNotFoundException { + return (OrganizationSnapshot) ois.readObject(); } /** @@ -119,7 +119,7 @@ KernelAddress waitForDistantKernelAddress() throws IOException, ClassNotFoundExc * @param map * @throws IOException */ - void sendConnectionInfo(KernelAddress myKA, Map>>> map) throws IOException { + void sendConnectionInfo(KernelAddress myKA, OrganizationSnapshot map) throws IOException { oos.writeObject(myKA); oos.writeObject(map); } diff --git a/src/madkit/kernel/MadkitKernel.java b/src/madkit/kernel/MadkitKernel.java index a4f0a94e..ab9fbf32 100644 --- a/src/madkit/kernel/MadkitKernel.java +++ b/src/madkit/kernel/MadkitKernel.java @@ -69,7 +69,6 @@ import java.util.Properties; import java.util.Set; import java.util.TimerTask; -import java.util.TreeMap; import java.util.TreeSet; import java.util.concurrent.Callable; import java.util.concurrent.CancellationException; @@ -96,6 +95,7 @@ import madkit.agr.LocalCommunity; import madkit.agr.LocalCommunity.Groups; import madkit.agr.LocalCommunity.Roles; +import madkit.agr.OrganizationSnapshot; import madkit.gui.ConsoleAgent; import madkit.gui.MASModel; import madkit.i18n.ErrorMessages; @@ -123,6 +123,7 @@ class MadkitKernel extends Agent { private static final ThreadGroup SYSTEM = new ThreadGroup("MK_SYSTEM") { + @Override public void uncaughtException(Thread t, Throwable e) { System.err.println("\n------------uncaught exception on " + t); } @@ -132,6 +133,7 @@ public void uncaughtException(Thread t, Throwable e) { // Runtime.getRuntime().availableProcessors() + 1, 2, Integer.MAX_VALUE, 2L, TimeUnit.SECONDS, new SynchronousQueue(), new ThreadFactory() { + @Override public Thread newThread(Runnable r) { final Thread t = new Thread(SYSTEM, r); t.setPriority(Thread.MAX_PRIORITY); @@ -219,6 +221,7 @@ AgentThreadFactory getNormalAgentThreadFactory() { daemonAgentThreadFactory = new AgentThreadFactory(kernelAddress, true); lifeExecutor = new ThreadPoolExecutor(2, Integer.MAX_VALUE, 1L, TimeUnit.SECONDS, new SynchronousQueue(), new ThreadFactory() { + @Override public Thread newThread(Runnable r) { final Thread t = new Thread(normalAgentThreadFactory.getThreadGroup(), r); // t.setPriority(Thread.MIN_PRIORITY); @@ -485,6 +488,7 @@ private void launchConfigAgents() { for (int i = 0; i < number; i++) { lifeExecutor.execute(new Runnable() { + @Override public void run() { if (!shuttedDown) { try { @@ -1126,6 +1130,7 @@ final List createBucket(final String agentClass, int bucketSize, for (int i = 0; i < cpuCoreNb; i++) { ecs.submit(new Callable>() { + @Override public List call() throws InvocationTargetException, InstantiationException, IllegalAccessException { final List list = new ArrayList<>(nbOfAgentsPerTask); for (int j = nbOfAgentsPerTask; j > 0; j--) { @@ -1173,6 +1178,7 @@ ReturnCode launchAgent(final AbstractAgent requester, final AbstractAgent agent, // holds for Integer.MAX_VALUE final ReturnCode returnCode = lifeExecutor.submit(new Callable() { + @Override public ReturnCode call() { return launchingAgent(agent, defaultGUI); } @@ -1217,6 +1223,7 @@ private ReturnCode launchingAgent(final AbstractAgent agent, boolean defaultGUI) ReturnCode r = AGENT_CRASH; final Future activationAttempt = lifeExecutor.submit(new Callable() { + @Override public ReturnCode call() { return agent.activation(); } @@ -1275,6 +1282,7 @@ ReturnCode killAgent(final AbstractAgent requester, final AbstractAgent target, } final Future killAttempt = serviceExecutor.submit(new Callable() { + @Override public ReturnCode call() { return killingAgent(target, timeOutSeconds); } @@ -1365,6 +1373,7 @@ final ReturnCode startEndBehavior(final AbstractAgent target, int timeOutSeconds if (timeOutSeconds != 0) { final Future endAttempt = executor.submit(new Callable() { + @Override public Boolean call() { return target.ending(); } @@ -1603,6 +1612,7 @@ public MadkitProperties getMadkitConfig() { return platform.getConfigOption(); } + @Override MadkitKernel getMadkitKernel() { return this; } @@ -1659,7 +1669,7 @@ boolean isRole(@SuppressWarnings("unused") final AbstractAgent requester, final } } - final void importDistantOrg(final Map>>> distantOrg) { + final void importDistantOrg(final OrganizationSnapshot distantOrg) { if (logger != null) logger.finer(() -> "Importing org..." + distantOrg); synchronized (organizations) { @@ -1675,8 +1685,8 @@ final void importDistantOrg(final Map>>> getOrganizationSnapShot(boolean global) { - Map>>> export = new TreeMap<>(); + public final OrganizationSnapshot getOrganizationSnapShot(boolean global) { + OrganizationSnapshot export = new OrganizationSnapshot(); synchronized (organizations) { for (Map.Entry org : organizations.entrySet()) { Map>> currentOrg = org.getValue().getOrgMap(global); diff --git a/src/madkit/kernel/NetworkAgent.java b/src/madkit/kernel/NetworkAgent.java index 047c7103..9fb36c41 100644 --- a/src/madkit/kernel/NetworkAgent.java +++ b/src/madkit/kernel/NetworkAgent.java @@ -56,6 +56,7 @@ import madkit.agr.LocalCommunity.Groups; import madkit.agr.LocalCommunity.Roles; import madkit.agr.NetworkCommunity; +import madkit.agr.OrganizationSnapshot; import madkit.gui.AgentStatusPanel; import madkit.kernel.Madkit.LevelOption; import madkit.message.EnumMessage; @@ -414,8 +415,8 @@ private boolean sendingConnectionInfo(final KernelConnection kc) { return true; } - private Map>>> cleanUp(Map>>> organizationSnapShot, KernelAddress from) { - for (Iterator>>>> iterator = organizationSnapShot.entrySet().iterator(); iterator.hasNext();) { + private OrganizationSnapshot cleanUp(OrganizationSnapshot organizationSnapshot, KernelAddress from) { + for (Iterator>>>> iterator = organizationSnapshot.entrySet().iterator(); iterator.hasNext();) { Entry>>> org = iterator.next(); for (Iterator>>> iterator2 = org.getValue().entrySet().iterator(); iterator2.hasNext();) { Entry>> group = iterator2.next(); @@ -438,7 +439,7 @@ private Map>>> cleanUp(Map list = new ArrayList<>(100); + @Override protected void activate() { // getLogger().setLevel(Level.ALL); startTimer(); @@ -155,6 +160,7 @@ protected void activate() { @Test public void returnTimeOut() { launchTestV2(new AbstractAgent() { + @Override protected void activate() { getLogger().setLevel(Level.ALL); TimeOutAA to = new TimeOutAA(true, true); @@ -174,6 +180,7 @@ protected void activate() { public void returnAleradyKilled() { addMadkitArgs(LevelOption.kernelLogLevel.toString(), "ALL"); launchTestV2(new AbstractAgent() { + @Override protected void activate() { NormalAA target = new NormalAA(); getLogger().setLevel(Level.ALL); @@ -188,6 +195,7 @@ protected void activate() { public void noTimeoutKill() { addMadkitArgs(LevelOption.agentLogLevel.toString(), "ALL"); launchTestV2(new AbstractAgent() { + @Override protected void activate() { getLogger().setLevel(Level.ALL); NormalAA target = new NormalAA(); @@ -201,6 +209,7 @@ protected void activate() { public void returnAgentCrash() { addMadkitArgs(LevelOption.kernelLogLevel.toString(), "ALL"); launchTestV2(new AbstractAgent() { + @Override protected void activate() { getLogger().setLevel(Level.ALL); FaultyAA f = new FaultyAA(true); @@ -213,6 +222,7 @@ protected void activate() { @Test public void selfKill() { launchTestV2(new AbstractAgent() { + @Override protected void activate() { getLogger().setLevel(Level.OFF); @@ -228,6 +238,7 @@ protected void activate() { @Test public void selfKillInActivate() { launchTestV2(new AbstractAgent() { + @Override protected void activate() { getLogger().setLevel(Level.OFF); assertEquals(SUCCESS, launchAgent(new SelfKillAA(true, true))); @@ -239,6 +250,7 @@ protected void activate() { @Test public void selfKillInActivateWTO() { launchTestV2(new AbstractAgent() { + @Override protected void activate() { getLogger().setLevel(Level.OFF); assertEquals(SUCCESS, createGroup(COMMUNITY, GROUP)); @@ -252,6 +264,7 @@ protected void activate() { @Test public void selfKilling() { launchTestV2(new AbstractAgent() { + @Override protected void activate() { // the time out should not change anything because target == this assertEquals(SUCCESS, createGroup(COMMUNITY, GROUP)); @@ -274,13 +287,15 @@ protected void activate() { @Test public void killFaulty() { launchTestV2(new AbstractAgent() { + @Override protected void activate() { AbstractAgent a; assertEquals(AGENT_CRASH, launchAgent(a = new FaultyAA(true, false))); ReturnCode r = killAgent(a); assertTrue(r == SUCCESS || r == ALREADY_KILLED); - assertEquals(TIMEOUT, launchAgent(a = new FaultyAA(true, false), 0)); + r = launchAgent(a = new FaultyAA(true, false), 0); + assertTrue(r == TIMEOUT || r == AGENT_CRASH); pause(10); r = killAgent(a); assertTrue(r == SUCCESS || r == ALREADY_KILLED); @@ -293,12 +308,6 @@ protected void activate() { assertEquals(SUCCESS, launchAgent(a = new FaultyAA(false, true))); assertEquals(SUCCESS, killAgent(a)); - assertEquals(SUCCESS, launchAgent(a = new FaultyAA(false, true))); - assertEquals(SUCCESS, killAgent(a)); - - assertEquals(SUCCESS, launchAgent(a = new FaultyAA(false, true))); - assertEquals(SUCCESS, killAgent(a)); - assertEquals(SUCCESS, launchAgent(a = new FaultyAA(false, true))); assertEquals(SUCCESS, killAgent(a, 0)); pause(10);// avoid interleaving @@ -311,6 +320,7 @@ protected void activate() { @Test public void selfKillInActivateAndEnd() { launchTestV2(new AbstractAgent() { + @Override protected void activate() { SelfAbstractKill a = new SelfAbstractKill(true, true, 0); assertEquals(SUCCESS, launchAgent(a)); @@ -322,6 +332,7 @@ protected void activate() { @Test public void selfKillinActivateWithTimeout() { launchTestV2(new AbstractAgent() { + @Override protected void activate() { SelfAbstractKill a = new SelfAbstractKill(true, false, 1); assertEquals(SUCCESS, launchAgent(a)); @@ -334,6 +345,7 @@ protected void activate() { @Test public void selfKillinEndAndWaitKill() { launchTestV2(new AbstractAgent() { + @Override protected void activate() { assertEquals(SUCCESS, launchAgent(new SelfAbstractKill(false, true, Integer.MAX_VALUE))); } @@ -343,6 +355,7 @@ protected void activate() { @Test public void cascadeKills() { launchTestV2(new AbstractAgent() { + @Override protected void activate() { Killer a = new Killer(); Killer b = new Killer(); @@ -359,6 +372,7 @@ protected void activate() { @Test public void returnSuccessAfterLaunchTimeOut() { launchTestV2(new AbstractAgent() { + @Override protected void activate() { TimeOutAA to = new TimeOutAA(true, true); assertEquals(TIMEOUT, launchAgent(to, 1)); @@ -371,6 +385,7 @@ protected void activate() { @Test public void killUnstopable() { launchTestV2(new AbstractAgent() { + @Override protected void activate() { AbstractAgent unstopableAgent = new UnstopableAbstractAgent(); unstopableAgent.getLogger().setLevel(Level.FINER); @@ -386,6 +401,7 @@ protected void activate() { @Test public void brutalKillUnstopable() { launchTestV2(new AbstractAgent() { + @Override protected void activate() { AbstractAgent unstopableAgent = new UnstopableAbstractAgent(); assertEquals(TIMEOUT, launchAgent(unstopableAgent, 1)); @@ -399,6 +415,7 @@ protected void activate() { @Test public void brutalKillUnstopableUsingSelfRef() { launchTestV2(new AbstractAgent() { + @Override protected void activate() { AbstractAgent unstopableAgent = new UnstopableAbstractAgent(); ReturnCode r = launchAgent(unstopableAgent, 2); @@ -412,6 +429,7 @@ protected void activate() { @Test public void killUnstopableUsingSelfRef() { launchTestV2(new AbstractAgent() { + @Override protected void activate() { AbstractAgent unstopableAgent = new UnstopableAbstractAgent(); ReturnCode r = launchAgent(unstopableAgent, 2);