Browse Source

Add files

Signed-off-by: wmb <wmb@teknik.io>
master
wmb 1 year ago
parent
commit
7b2f0198c3
38 changed files with 1010 additions and 0 deletions
  1. 11
    0
      PSSR-Jade/.classpath
  2. 17
    0
      PSSR-Jade/.project
  3. 14
    0
      PSSR-Jade/.settings/org.eclipse.jdt.core.prefs
  4. 1
    0
      PSSR-Jade/APDescription.txt
  5. 1
    0
      PSSR-Jade/MTPs-Main-Container.txt
  6. 52
    0
      PSSR-Jade/src/org/example/pssr/jade/tp/Main.java
  7. 33
    0
      PSSR-Jade/src/org/example/pssr/jade/tp/SimpleAgent.java
  8. 11
    0
      PSSR-Jade3/.classpath
  9. 17
    0
      PSSR-Jade3/.project
  10. 14
    0
      PSSR-Jade3/.settings/org.eclipse.jdt.core.prefs
  11. 1
    0
      PSSR-Jade3/APDescription.txt
  12. 1
    0
      PSSR-Jade3/MTPs-Main-Container.txt
  13. 39
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/behaviours/Main.java
  14. 70
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/behaviours/TPBehaviours.java
  15. 39
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages/Main.java
  16. 18
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages/MyAgent.java
  17. 14
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages/MyBehaviour.java
  18. 14
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages/MyCyclicBehaviour.java
  19. 19
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages/ReceiveHelloBehaviour.java
  20. 28
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages/SendHelloBehaviour.java
  21. 47
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/Agent1Behaviour.java
  22. 44
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/Agent2Behaviour.java
  23. 46
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/Main.java
  24. 18
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/MyAgent.java
  25. 14
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/MyBehaviour.java
  26. 14
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/MyCyclicBehaviour.java
  27. 118
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/three_agents/ControllerAgentBehaviour.java
  28. 71
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/three_agents/CounterAgentBehaviour.java
  29. 46
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/three_agents/Main.java
  30. 18
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/three_agents/MyAgent.java
  31. 14
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/three_agents/MyBehaviour.java
  32. 14
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/three_agents/MyCyclicBehaviour.java
  33. 39
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages2/Main.java
  34. 18
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages2/MyAgent.java
  35. 14
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages2/MyBehaviour.java
  36. 14
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages2/MyCyclicBehaviour.java
  37. 19
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages2/ReceiveBehaviour.java
  38. 28
    0
      PSSR-Jade3/src/org/example/pssr/jade/tp/messages2/SendBehaviour.java

+ 11
- 0
PSSR-Jade/.classpath View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-15">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/jade"/>
<classpathentry kind="output" path="bin"/>
</classpath>

+ 17
- 0
PSSR-Jade/.project View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>PSSR-Jade</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

+ 14
- 0
PSSR-Jade/.settings/org.eclipse.jdt.core.prefs View File

@@ -0,0 +1,14 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=15
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=15
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=15

+ 1
- 0
PSSR-Jade/APDescription.txt View File

@@ -0,0 +1 @@
( ap-description :name "192.168.8.65:1099/JADE" :ap-services (set ( ap-service :name fipa.mts.mtp.http.std :type fipa.mts.mtp.http.std :addresses (sequence http://DESKTOP-IQVBM6D:7778/acc))))

+ 1
- 0
PSSR-Jade/MTPs-Main-Container.txt View File

@@ -0,0 +1 @@
http://DESKTOP-IQVBM6D:7778/acc

+ 52
- 0
PSSR-Jade/src/org/example/pssr/jade/tp/Main.java View File

@@ -0,0 +1,52 @@
package org.example.pssr.jade.tp;

import java.util.HashMap;
import java.util.Map;

import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.core.Runtime;
import jade.util.ExtendedProperties;
import jade.util.leap.Properties;

public class Main {

private static final boolean USE_GUI = true;
private static final int PORT = 49317;

/*
* Port 49317 because 49317 kind looks like AGENT Well, maybe 1 doesn't look
* like N, but no other number does, so whatever.
*/

public static void main(String[] args) {
final String className = SimpleAgent.class.getName();
final Map<String, String[]> agents = new HashMap<>();

agents.put("Edsger", new String[] { "Dijkstra" });
agents.put("Tony", new String[] { "Hoare" });

System.out.println(formatAgents(agents, className));

Properties props = new ExtendedProperties();
props.setProperty(Profile.GUI, String.valueOf(USE_GUI));
props.setProperty(Profile.AGENTS, formatAgents(agents, className));
props.setProperty(Profile.LOCAL_PORT, String.valueOf(PORT));

Runtime.instance().createMainContainer(new ProfileImpl(props));
}

private static String formatAgents(Map<String, String[]> agents, String className) {
String[] agentStrings = new String[agents.size()];
int i = 0;
for (String agent : agents.keySet()) {
agentStrings[i++] = formatAgentString(agent, agents.get(agent), className);
}
return String.join(";", agentStrings);
}

private static String formatAgentString(String name, String[] args, String className) {
return name + ':' + className + '(' + String.join(", ", args) + ')';
}

}

+ 33
- 0
PSSR-Jade/src/org/example/pssr/jade/tp/SimpleAgent.java View File

@@ -0,0 +1,33 @@
package org.example.pssr.jade.tp;

import jade.core.Agent;
import jade.core.behaviours.OneShotBehaviour;
import jade.lang.acl.ACLMessage;

class SimpleAgent extends Agent {

private static final long serialVersionUID = 6292294520184474619L;

@Override
protected void setup() {
addBehaviour(new SayHelloBehaviour());
}

protected static class SayHelloBehaviour extends OneShotBehaviour {

private static final long serialVersionUID = 5317361712594775656L;

@Override
public void action() {
final Agent agent = getAgent();
final String name = "[" + agent.getClass().toString() + "] " + agent.getName() + " "
+ (String) agent.getArguments()[0];
System.out.println(name);
ACLMessage m = new ACLMessage(ACLMessage.INFORM);
m.setContent("Oi m8! Got a loicense for that loicense? 3Head");
m.addReceiver(agent.getAID());
agent.send(m);
}

}
}

+ 11
- 0
PSSR-Jade3/.classpath View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-15">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/jade"/>
<classpathentry kind="output" path="bin"/>
</classpath>

+ 17
- 0
PSSR-Jade3/.project View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>PSSR-Jade3</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

+ 14
- 0
PSSR-Jade3/.settings/org.eclipse.jdt.core.prefs View File

@@ -0,0 +1,14 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=15
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=15
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=15

+ 1
- 0
PSSR-Jade3/APDescription.txt View File

@@ -0,0 +1 @@
( ap-description :name "192.168.8.65:49317/JADE" :ap-services (set ( ap-service :name fipa.mts.mtp.http.std :type fipa.mts.mtp.http.std :addresses (sequence http://DESKTOP-IQVBM6D:7778/acc))))

+ 1
- 0
PSSR-Jade3/MTPs-Main-Container.txt View File

@@ -0,0 +1 @@
http://DESKTOP-IQVBM6D:7778/acc

+ 39
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/behaviours/Main.java View File

@@ -0,0 +1,39 @@
package org.example.pssr.jade.tp.behaviours;

import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.core.Runtime;
import jade.util.ExtendedProperties;
import jade.util.leap.Properties;

public class Main {
private static final String cname = TPBehaviours.class.getName();
private static final boolean USE_GUI = true;
private static final int PORT = 49317;

private static final String[] agents = { "S1", "S2", "S3" };

public static void main(String[] args) {

Properties props = new ExtendedProperties();
props.setProperty(Profile.GUI, String.valueOf(USE_GUI));
props.setProperty(Profile.AGENTS, formatAgents(agents, cname));
props.setProperty(Profile.LOCAL_PORT, String.valueOf(PORT));

Runtime.instance().createMainContainer(new ProfileImpl(props));
}

private static String formatAgents(String[] agents, String className) {
String[] agentStrings = new String[agents.length];
int i = 0;
for (String agent : agents) {
agentStrings[i++] = formatAgentString(agent, className);
}
return String.join(";", agentStrings);
}

private static String formatAgentString(String name, String className) {
return name + ':' + className;
}

}

+ 70
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/behaviours/TPBehaviours.java View File

@@ -0,0 +1,70 @@
package org.example.pssr.jade.tp.behaviours;

import java.util.Random;

import jade.core.Agent;
import jade.core.behaviours.Behaviour;
import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.OneShotBehaviour;

public class TPBehaviours extends Agent {

private static final long serialVersionUID = -7098674788713541138L;

@Override
protected void setup() {
final String name = getLocalName();

if (name.equals("S1"))
addBehaviour(new B1());
else if (name.equals("S2"))
addBehaviour(new B2());
else if (name.equals("S3"))
addBehaviour(new B3());
}

public class B1 extends OneShotBehaviour {

private static final long serialVersionUID = 2835970726343489667L;

@Override
public void action() {
System.out.println("[" + getAgent().getLocalName() + "]: This is a OneShotBehaviour");
}

}

public class B2 extends CyclicBehaviour {

private static final long serialVersionUID = 2921563878760137185L;

private Random rnd = new Random();

@Override
public void action() {
System.out.println("[" + getAgent().getLocalName() + "]: This is a CyclicBehaviour ["
+ String.valueOf(rnd.nextLong()) + "]");
getAgent().doWait(100);
}

}

public class B3 extends Behaviour {

private static final long serialVersionUID = -2400529907735070153L;

private int step = 0;

@Override
public void action() {
System.out.println("[" + getAgent().getLocalName() + "]: case " + step++);
}

@Override
public boolean done() {
return step == 3;
}

}

}

+ 39
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages/Main.java View File

@@ -0,0 +1,39 @@
package org.example.pssr.jade.tp.messages;

import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.core.Runtime;
import jade.util.ExtendedProperties;
import jade.util.leap.Properties;

public class Main {
private static final String cname = MyAgent.class.getName();
private static final boolean USE_GUI = true;
private static final int PORT = 49317;

private static final String[] agents = { "Agent1", "Agent2" };

public static void main(String[] args) {

Properties props = new ExtendedProperties();
props.setProperty(Profile.GUI, String.valueOf(USE_GUI));
props.setProperty(Profile.AGENTS, formatAgents(agents, cname));
props.setProperty(Profile.LOCAL_PORT, String.valueOf(PORT));

Runtime.instance().createMainContainer(new ProfileImpl(props));
}

private static String formatAgents(String[] agents, String className) {
String[] agentStrings = new String[agents.length];
int i = 0;
for (String agent : agents) {
agentStrings[i++] = formatAgentString(agent, className);
}
return String.join(";", agentStrings);
}

private static String formatAgentString(String name, String className) {
return name + ':' + className;
}

}

+ 18
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages/MyAgent.java View File

@@ -0,0 +1,18 @@
package org.example.pssr.jade.tp.messages;

import jade.core.Agent;

public class MyAgent extends Agent {

private static final long serialVersionUID = -7286689673225791751L;

@Override
protected void setup() {
final String name = getLocalName();

assert name.equals("Agent1") || name.equals("Agent2");

addBehaviour(name.equals("Agent1") ? new SendHelloBehaviour() : new ReceiveHelloBehaviour());
}

}

+ 14
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages/MyBehaviour.java View File

@@ -0,0 +1,14 @@
package org.example.pssr.jade.tp.messages;

import jade.core.behaviours.Behaviour;

public abstract class MyBehaviour extends Behaviour {

private static final long serialVersionUID = 2662387109156194369L;

public void say(String str) {
final String name = myAgent.getLocalName();
System.out.println("[" + name + "]: " + str);
}

}

+ 14
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages/MyCyclicBehaviour.java View File

@@ -0,0 +1,14 @@
package org.example.pssr.jade.tp.messages;

import jade.core.behaviours.CyclicBehaviour;

public abstract class MyCyclicBehaviour extends CyclicBehaviour {

private static final long serialVersionUID = -6611209587851286991L;

public void say(String str) {
final String name = myAgent.getLocalName();
System.out.println("[" + name + "]: " + str);
}

}

+ 19
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages/ReceiveHelloBehaviour.java View File

@@ -0,0 +1,19 @@
package org.example.pssr.jade.tp.messages;

import jade.lang.acl.ACLMessage;

public class ReceiveHelloBehaviour extends MyCyclicBehaviour {

private static final long serialVersionUID = -5031148908558775888L;

@Override
public void action() {
say("agent recepteur");

final ACLMessage message = myAgent.blockingReceive();

if (message != null)
say("message recu : " + message.getContent());
}

}

+ 28
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages/SendHelloBehaviour.java View File

@@ -0,0 +1,28 @@
package org.example.pssr.jade.tp.messages;

import jade.core.AID;
import jade.lang.acl.ACLMessage;

public class SendHelloBehaviour extends MyCyclicBehaviour {

private static final long serialVersionUID = -5031148908558775888L;

private static final String HELLO_MESSAGE_CONTENT = "Bonjour!";
private static final long WAIT_TIME_MILLIS = 5000;

@Override
public void action() {
final String receiverName = "Agent2";
final AID receiver = new AID(receiverName, AID.ISLOCALNAME);
final ACLMessage message = new ACLMessage(ACLMessage.INFORM);

message.setContent(HELLO_MESSAGE_CONTENT);
message.addReceiver(receiver);

say("agent emetteur");

myAgent.send(message);

block(WAIT_TIME_MILLIS);
}
}

+ 47
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/Agent1Behaviour.java View File

@@ -0,0 +1,47 @@
package org.example.pssr.jade.tp.messages.array_sum;

import jade.lang.acl.ACLMessage;

public class Agent1Behaviour extends MyBehaviour {

private static final long serialVersionUID = -5031148908558775888L;

private int i = 0;
private int halfSum1 = 0;
private int halfSum2 = 0;
private boolean finished = false;
private boolean gotResult = false;
private boolean printedResult = false;

@Override
public void action() {
if (!finished) {
halfSum1 += Main.T[i++];
if (i == Main.T_HALF) {
finished = true;
say("finished counting halfSum1 (halfSum1 == " + halfSum1 + ")");
}

} else if (!gotResult) {
final ACLMessage halfSum2Message = myAgent.blockingReceive();
if (halfSum2Message != null) {
halfSum2 = Integer.valueOf(halfSum2Message.getContent());
say("received halfSum2 from Agent2 (halfSum2 == " + halfSum2 + ")");
gotResult = true;
} else {
// should not happen
say("halfSum2Message is null!");
this.myAgent.doSuspend();
}
} else if (!printedResult) {
final int sum = halfSum1 + halfSum2;
say("Sum = " + sum);
printedResult = true;
}
}

@Override
public boolean done() {
return finished && gotResult && printedResult;
}
}

+ 44
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/Agent2Behaviour.java View File

@@ -0,0 +1,44 @@
package org.example.pssr.jade.tp.messages.array_sum;

import jade.core.AID;
import jade.lang.acl.ACLMessage;

public class Agent2Behaviour extends MyBehaviour {

private static final long serialVersionUID = -8369180815301328931L;

private int i = Main.T_HALF;
private int halfSum2 = 0;
private boolean finished = false;
private boolean sentResult = false;

@Override
public void action() {
if (!finished) {
halfSum2 += Main.T[i++];
if (i == Main.T.length) {
finished = true;
say("finished counting halfSum2 (halfSum2 == " + halfSum2 + ")");
}

} else if (!sentResult) {
final String receiverName = "Agent1";
final AID receiver = new AID(receiverName, AID.ISLOCALNAME);
final ACLMessage message = new ACLMessage(ACLMessage.INFORM);

message.setContent(String.valueOf(halfSum2));
message.addReceiver(receiver);

myAgent.send(message);

say("sent message");

sentResult = true;
}
}

@Override
public boolean done() {
return finished && sentResult;
}
}

+ 46
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/Main.java View File

@@ -0,0 +1,46 @@
package org.example.pssr.jade.tp.messages.array_sum;

import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.core.Runtime;
import jade.util.ExtendedProperties;
import jade.util.leap.Properties;

public class Main {
private static final String CNAME = MyAgent.class.getName();
private static final boolean USE_GUI = true;
private static final int PORT = 49317;

private static final String[] AGENTS = { "Agent1", "Agent2" };

public static final int[] T = new int[100];
public static final int T_HALF = T.length / 2;

public static void main(String[] args) {

for (int i = 0; i < T.length; i++) {
T[i] = i + 1;
}

Properties props = new ExtendedProperties();
props.setProperty(Profile.GUI, String.valueOf(USE_GUI));
props.setProperty(Profile.AGENTS, formatAgents(AGENTS, CNAME));
props.setProperty(Profile.LOCAL_PORT, String.valueOf(PORT));

Runtime.instance().createMainContainer(new ProfileImpl(props));
}

private static String formatAgents(String[] agents, String className) {
String[] agentStrings = new String[agents.length];
int i = 0;
for (String agent : agents) {
agentStrings[i++] = formatAgentString(agent, className);
}
return String.join(";", agentStrings);
}

private static String formatAgentString(String name, String className) {
return name + ':' + className;
}

}

+ 18
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/MyAgent.java View File

@@ -0,0 +1,18 @@
package org.example.pssr.jade.tp.messages.array_sum;

import jade.core.Agent;

public class MyAgent extends Agent {

private static final long serialVersionUID = -7286689673225791751L;

@Override
protected void setup() {
final String name = getLocalName();

assert name.equals("Agent1") || name.equals("Agent2");

addBehaviour(name.equals("Agent1") ? new Agent1Behaviour() : new Agent2Behaviour());
}

}

+ 14
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/MyBehaviour.java View File

@@ -0,0 +1,14 @@
package org.example.pssr.jade.tp.messages.array_sum;

import jade.core.behaviours.Behaviour;

public abstract class MyBehaviour extends Behaviour {

private static final long serialVersionUID = 2662387109156194369L;

public void say(String str) {
final String name = myAgent.getLocalName();
System.out.println("[" + name + "]: " + str);
}

}

+ 14
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/MyCyclicBehaviour.java View File

@@ -0,0 +1,14 @@
package org.example.pssr.jade.tp.messages.array_sum;

import jade.core.behaviours.CyclicBehaviour;

public abstract class MyCyclicBehaviour extends CyclicBehaviour {

private static final long serialVersionUID = -6611209587851286991L;

public void say(String str) {
final String name = myAgent.getLocalName();
System.out.println("[" + name + "]: " + str);
}

}

+ 118
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/three_agents/ControllerAgentBehaviour.java View File

@@ -0,0 +1,118 @@
package org.example.pssr.jade.tp.messages.array_sum.three_agents;

import java.io.IOException;

import jade.core.AID;
import jade.lang.acl.ACLMessage;

public class ControllerAgentBehaviour extends MyBehaviour {

private static final long serialVersionUID = -931615535885130993L;

private static final String AGENT1 = "Agent1";
private static final String AGENT2 = "Agent2";

private static final int[] T1 = new int[Main.T.length - Main.T_HALF];
private static final int[] T2 = new int[Main.T_HALF];

private int firstSum;
private int secondSum;

private boolean filledTables = false;
private boolean sentFirstHalf = false;
private boolean sentSecondHalf = false;
private boolean receivedFirstSum = false;
private boolean receivedSecondSum = false;
private boolean printedTotalSum = false;

@Override
public void action() {

if (!filledTables) {
for (int j = 0, i = 0; j < T1.length; i += 2)
T1[j++] = Main.T[i];

for (int j = 0, i = 1; j < T2.length; i += 2)
T2[j++] = Main.T[i];

say("T1:");
for (int n : T1)
say("\t" + n);

say("T2:");
for (int n : T2)
say("\t" + n);

filledTables = true;
return;
}

if (!sentFirstHalf) {
sendHalfTable(T1, AGENT1);
say("sent T1 to " + AGENT1);
sentFirstHalf = true;
return;
}

if (!sentSecondHalf) {
sendHalfTable(T2, AGENT2);
say("sent T2 to " + AGENT2);
sentSecondHalf = true;
}

if (!receivedFirstSum) {
firstSum = receiveHalfSum();
say("received firstSum (firstSum == " + firstSum + ")");
receivedFirstSum = true;
}

if (!receivedSecondSum) {
secondSum = receiveHalfSum();
say("received secondSum (secondSum == " + secondSum + ")");
receivedSecondSum = true;
}

if (!printedTotalSum) {
final int totalSum = firstSum + secondSum;
say("total sum = " + totalSum);
printedTotalSum = true;
}
}

private void sendHalfTable(int[] table, String agentName) {
final AID receiver = new AID(agentName, AID.ISLOCALNAME);
final ACLMessage message = new ACLMessage(ACLMessage.INFORM);

try {
message.setContentObject(table);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

message.addReceiver(receiver);

myAgent.send(message);
}

private int receiveHalfSum() {
int halfSum = 0;
final ACLMessage message = myAgent.blockingReceive();

if (message != null) {
halfSum = Integer.valueOf(message.getContent());
} else {
// should not happen
say("receiveHalfSum(): message is null!");
this.myAgent.doSuspend();
}

return halfSum;
}

@Override
public boolean done() {
return filledTables && sentFirstHalf && sentSecondHalf && receivedFirstSum && receivedSecondSum
&& printedTotalSum;
}
}

+ 71
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/three_agents/CounterAgentBehaviour.java View File

@@ -0,0 +1,71 @@
package org.example.pssr.jade.tp.messages.array_sum.three_agents;

import jade.core.AID;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.UnreadableException;

public class CounterAgentBehaviour extends MyBehaviour {

private static final long serialVersionUID = -8103402175686572976L;

private static final String CONTROLLER_AGENT_NAME = "Controller";

private boolean receivedTable = false;
private boolean calculatedSum = false;
private boolean sentResult = false;

private int sum = 0;
private int i = 0;

private int[] table;

@Override
public void action() {

if (!receivedTable) {
table = receiveTable();
receivedTable = true;
return;
}

if (!calculatedSum) {
sum += table[i++];
if (i == table.length)
calculatedSum = true;
return;
}

if (!sentResult) {
final AID controller = new AID(CONTROLLER_AGENT_NAME, AID.ISLOCALNAME);
final ACLMessage message = new ACLMessage(ACLMessage.INFORM);
message.setContent(String.valueOf(sum));
message.addReceiver(controller);
myAgent.send(message);
sentResult = true;
return;
}
}

private int[] receiveTable() {
final ACLMessage message = myAgent.blockingReceive();

if (message != null) {
try {
return (int[]) message.getContentObject();
} catch (UnreadableException e) {
e.printStackTrace();
}
} else {
// should not happen
say("receiveHalfSum(): message is null!");
this.myAgent.doSuspend();
}
return null;
}

@Override
public boolean done() {
return receivedTable && calculatedSum && sentResult;
}

}

+ 46
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/three_agents/Main.java View File

@@ -0,0 +1,46 @@
package org.example.pssr.jade.tp.messages.array_sum.three_agents;

import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.core.Runtime;
import jade.util.ExtendedProperties;
import jade.util.leap.Properties;

public class Main {
private static final String CNAME = MyAgent.class.getName();
private static final boolean USE_GUI = true;
private static final int PORT = 49317;

private static final String[] AGENTS = { "Controller", "Agent1", "Agent2" };

public static final int[] T = new int[100];
public static final int T_HALF = T.length / 2;

public static void main(String[] args) {

for (int i = 0; i < T.length; i++) {
T[i] = i + 1;
}

Properties props = new ExtendedProperties();
props.setProperty(Profile.GUI, String.valueOf(USE_GUI));
props.setProperty(Profile.AGENTS, formatAgents(AGENTS, CNAME));
props.setProperty(Profile.LOCAL_PORT, String.valueOf(PORT));

Runtime.instance().createMainContainer(new ProfileImpl(props));
}

private static String formatAgents(String[] agents, String className) {
String[] agentStrings = new String[agents.length];
int i = 0;
for (String agent : agents) {
agentStrings[i++] = formatAgentString(agent, className);
}
return String.join(";", agentStrings);
}

private static String formatAgentString(String name, String className) {
return name + ':' + className;
}

}

+ 18
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/three_agents/MyAgent.java View File

@@ -0,0 +1,18 @@
package org.example.pssr.jade.tp.messages.array_sum.three_agents;

import jade.core.Agent;

public class MyAgent extends Agent {

private static final long serialVersionUID = -7286689673225791751L;

@Override
protected void setup() {
final String name = getLocalName();

assert name.equals("Controller") || name.equals("Agent1") || name.equals("Agent2");

addBehaviour(name.equals("Controller") ? new ControllerAgentBehaviour() : new CounterAgentBehaviour());
}

}

+ 14
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/three_agents/MyBehaviour.java View File

@@ -0,0 +1,14 @@
package org.example.pssr.jade.tp.messages.array_sum.three_agents;

import jade.core.behaviours.Behaviour;

public abstract class MyBehaviour extends Behaviour {

private static final long serialVersionUID = 2662387109156194369L;

public void say(String str) {
final String name = myAgent.getLocalName();
System.out.println("[" + name + "]: " + str);
}

}

+ 14
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages/array_sum/three_agents/MyCyclicBehaviour.java View File

@@ -0,0 +1,14 @@
package org.example.pssr.jade.tp.messages.array_sum.three_agents;

import jade.core.behaviours.CyclicBehaviour;

public abstract class MyCyclicBehaviour extends CyclicBehaviour {

private static final long serialVersionUID = -6611209587851286991L;

public void say(String str) {
final String name = myAgent.getLocalName();
System.out.println("[" + name + "]: " + str);
}

}

+ 39
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages2/Main.java View File

@@ -0,0 +1,39 @@
package org.example.pssr.jade.tp.messages2;

import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.core.Runtime;
import jade.util.ExtendedProperties;
import jade.util.leap.Properties;

public class Main {
private static final String cname = MyAgent.class.getName();
private static final boolean USE_GUI = true;
private static final int PORT = 49317;

private static final String[] agents = { "Agent1", "Agent2" };

public static void main(String[] args) {

Properties props = new ExtendedProperties();
props.setProperty(Profile.GUI, String.valueOf(USE_GUI));
props.setProperty(Profile.AGENTS, formatAgents(agents, cname));
props.setProperty(Profile.LOCAL_PORT, String.valueOf(PORT));

Runtime.instance().createMainContainer(new ProfileImpl(props));
}

private static String formatAgents(String[] agents, String className) {
String[] agentStrings = new String[agents.length];
int i = 0;
for (String agent : agents) {
agentStrings[i++] = formatAgentString(agent, className);
}
return String.join(";", agentStrings);
}

private static String formatAgentString(String name, String className) {
return name + ':' + className;
}

}

+ 18
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages2/MyAgent.java View File

@@ -0,0 +1,18 @@
package org.example.pssr.jade.tp.messages2;

import jade.core.Agent;

public class MyAgent extends Agent {

private static final long serialVersionUID = -7286689673225791751L;

@Override
protected void setup() {
final String name = getLocalName();

assert name.equals("Agent1") || name.equals("Agent2");

addBehaviour(name.equals("Agent1") ? new SendBehaviour() : new ReceiveBehaviour());
}

}

+ 14
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages2/MyBehaviour.java View File

@@ -0,0 +1,14 @@
package org.example.pssr.jade.tp.messages2;

import jade.core.behaviours.Behaviour;

public abstract class MyBehaviour extends Behaviour {

private static final long serialVersionUID = 2662387109156194369L;

public void say(String str) {
final String name = myAgent.getLocalName();
System.out.println("[" + name + "]: " + str);
}

}

+ 14
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages2/MyCyclicBehaviour.java View File

@@ -0,0 +1,14 @@
package org.example.pssr.jade.tp.messages2;

import jade.core.behaviours.CyclicBehaviour;

public abstract class MyCyclicBehaviour extends CyclicBehaviour {

private static final long serialVersionUID = -6611209587851286991L;

public void say(String str) {
final String name = myAgent.getLocalName();
System.out.println("[" + name + "]: " + str);
}

}

+ 19
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages2/ReceiveBehaviour.java View File

@@ -0,0 +1,19 @@
package org.example.pssr.jade.tp.messages2;

import jade.lang.acl.ACLMessage;

public class ReceiveBehaviour extends MyCyclicBehaviour {

private static final long serialVersionUID = -5031148908558775888L;

@Override
public void action() {
say("agent recepteur");

final ACLMessage message = myAgent.blockingReceive();

if (message != null)
say("message recu : " + message.getContent());
}

}

+ 28
- 0
PSSR-Jade3/src/org/example/pssr/jade/tp/messages2/SendBehaviour.java View File

@@ -0,0 +1,28 @@
package org.example.pssr.jade.tp.messages2;

import jade.core.AID;
import jade.lang.acl.ACLMessage;

public class SendBehaviour extends MyCyclicBehaviour {

private static final long serialVersionUID = -5031148908558775888L;

private static final String HELLO_MESSAGE_CONTENT = "Bonjour!";
private static final long WAIT_TIME_MILLIS = 5000;

@Override
public void action() {
final String receiverName = "Agent2";
final AID receiver = new AID(receiverName, AID.ISLOCALNAME);
final ACLMessage message = new ACLMessage(ACLMessage.INFORM);

message.setContent(HELLO_MESSAGE_CONTENT);
message.addReceiver(receiver);

say("agent emetteur");

myAgent.send(message);

block(WAIT_TIME_MILLIS);
}
}

Loading…
Cancel
Save