Commit fad59d46 authored by Linxiao Zeng's avatar Linxiao Zeng

Initial commit

parents
Pipeline #7258 failed with stages
in 23 seconds
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="C:/Users/zengl/Documents/supelec/3A/Cours/Minor/SMA/projectJade/ProjectSMA/jade.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ProjectSMA</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>
( ap-description :name "192.168.56.1:8888/JADE" :ap-services (set ( ap-service :name fipa.mts.mtp.http.std :type fipa.mts.mtp.http.std :addresses (sequence http://DESKTOP-BAJBFRR:7778/acc))))
http://DESKTOP-BAJBFRR:7778/acc
transf:transfo-1;[produit1:1];[ligne2:produit2:2];2000;.02;(200,100)
transf:entree-1;[];[produit1:1];0;0;(100,100)
transf:sortie-1;[produit2:1];[];0;0;(300,100)
ligne:ligne1;entree-1;transfo-1;5
ligne:ligne2;transfo-1;sortie-1;1
\ No newline at end of file
File added
package gui;
import java.util.List;
import jade.core.behaviours.Behaviour;
public class AddProduct extends Behaviour {
int num = 0;
@Override
public void action() {
// TODO Auto-generated method stub
Painting p = ((GUI)myAgent).getPainint();
List<Product> productList = p.getProductList();
Product padd = new Product(0, (num+1)*30);
System.out.println("add Product"+padd.getID());
productList.add(padd);
num++;
block(100);
}
@Override
public boolean done() {
// TODO Auto-generated method stub
if(num>=((GUI)myAgent).getCapacity()) {
System.out.println("fin of addProduct");
return true;
}
return false;
}
}
package gui;
import jade.core.AID;
import jade.core.behaviours.Behaviour;
import jade.lang.acl.ACLMessage;
public class DemandeMoveMessage extends Behaviour {
public static final int DEMANDE_MOVE = 100;
public static final int DEMANDE_DESTROY = 404;
int state = 0;
int count = 0;
@Override
public void action() {
// TODO Auto-generated method stub
ACLMessage m;
if(state == 0){
if(count<=5) {
count++;
}else {
state++;
}
block(100);
}else if(state>0 && state<=4) {
m = new ACLMessage(DEMANDE_MOVE);
m.setContent("4;50");// move product id(4) with distance of 50
m.addReceiver(new AID("interface",false));// send this message to GUI
System.out.println("->"+m.getContent());
myAgent.send(m);
state++;
block(1000);
}else if(state>4 && state<=6){
m = new ACLMessage(DEMANDE_DESTROY);
m.setContent(""+(state-5));// remove product id(0 and 1)
m.addReceiver(new AID("interface",false));// send this message to GUI
System.out.println("->"+m.getContent());
myAgent.send(m);
state++;
block(2000);
}else {
state++;
}
}
@Override
public boolean done() {
// TODO Auto-generated method stub
return (state>8);
}
}
package gui;
import java.util.List;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
public class DestoryMessageHandle extends CyclicBehaviour {
// can use this when the product going to the Transformer or Sortie
@Override
public void action() {
// TODO Auto-generated method stub
ACLMessage m = myAgent.receive(MessageTemplate.MatchPerformative(DemandeMoveMessage.DEMANDE_DESTROY));
if(m == null) {
block();
}else {
int pid = Integer.parseInt(m.getContent());
System.out.println(pid+" remove ");
Painting paint = ((GUI)myAgent).getPainint();
List<Product> plist = paint.getProductList();
Product p2die = null;
for(Product p:plist) {
if(p.getID() == pid) {
//int pos = p.getPosition();
p2die = p;
}
}
plist.remove(p2die);
}
}
}
package gui;
import java.awt.Dimension;
import javax.swing.JFrame;
import jade.core.Agent;
public class GUI extends Agent {
private int capacity;// = rand.nextInt(10);
private int state;
//JFrame f;
private Painting p;
public int getCapacity() {
return capacity;
}
public int getState() {
return state;
}
public Painting getPainint() {
return p;
}
public void setup() {
System.out.println(getName());//getLocalName()
Object[] args = getArguments();
capacity = (Integer)args[0];
System.out.println("Lancer Interface: " + capacity);
JFrame f = new JFrame("ma fenetre");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new Painting();
p.setPreferredSize(new Dimension(800,600));
f.add(p);
f.pack();
f.setVisible(true);
// behavior
addBehaviour(new AddProduct());
addBehaviour(new Repaint(this, 1000));
addBehaviour(new MoveMessageHandle());
addBehaviour(new DestoryMessageHandle());
// addBehaviour(new Repaint());
}
}
package gui;
import jade.core.Runtime;
import java.util.Random;
import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.wrapper.AgentContainer;
import jade.wrapper.AgentController;
import jade.wrapper.StaleProxyException;
public class Lanceur {
private static final Random rand = new Random();
public static void main(String[] args) throws StaleProxyException {
Runtime rt = Runtime.instance();
rt.setCloseVM(true);
Profile pMain = new ProfileImpl("localhost", 8888,null);
AgentContainer mc = rt.createMainContainer(pMain);
AgentController ac = mc.createNewAgent("interface", "gui.GUI",
new Object[] {5});//5 produit
AgentController sp = mc.createNewAgent("sp2", "gui.Sender",
new Object[] {2, rand.nextInt(10)});
sp.start();
ac.start();
// for(int i=0;i<2;i++) {
// AgentController ac = mc.createNewAgent("toto"+i, "Producteur",
// new Object[] {i, rand.nextInt(10)});
// ac.start();
// }
// remote monitoring agent (RMA)
// AgentController gui = mc.createNewAgent("gui", "jade.tools.rma.rma", new Object[0]);
// gui.start();
// AgentController sniffer = mc.createNewAgent("sniffer", "jade.tools.sniffer.Sniffer", new Object[0]);
// sniffer.start();
}
}
package gui;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LoadConfiguration {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
FileReader fr = new FileReader("conf.txt");
BufferedReader br = new BufferedReader(fr);
String ligne;
while ((ligne=br.readLine())!=null) {
// System.out.println("lu: "+ligne);
String [] t = ligne.split(":",2);
if(t[0].equals("transf")) {
String [] params = t[1].split(";");
if(params.length == 6) {
String name = params[0];
//input
//output
int processTime = Integer.parseInt(params[3]);
double errorProb = Double.parseDouble(params[4]);
//position
Matcher m = Pattern.compile("\\((\\d+),(\\d+)\\)").matcher(params[5]);
int positionx=0,positiony=0;
while(m.find()) {
// System.out.println(" goupe 1:" + m.group(1));
// System.out.println(" goupe 2:" + m.group(2));
positionx = Integer.parseInt(m.group(1));
positiony = Integer.parseInt(m.group(2));
}
//
System.out.println(name+":"+"pT#"+processTime+",errorPb#"+errorProb+",position#("+positionx+","+positiony+")");
}else {
System.out.println("file is not in correct format!");
throw new IOException("file is not in correct format, can't parse!");
}
}else if(t[0].equals("ligne")) {
String [] params = t[1].split(";");
if(params.length == 4) {
String name = params[0];
String Msource = params[1];
String Mcible = params[2];
int capacity = Integer.parseInt(params[3]);
System.out.println(name+":"+Msource+" -> "+Mcible+", capacity#"+capacity);
}else {
System.out.println("file is not in correct format!");
throw new IOException("file is not in correct format, can't parse!");
}
}else {
System.out.println("unknown type!");
throw new IOException("unknown type, can't parse!");
}
}
} catch (FileNotFoundException e) {
System.err.println("lignes.txt pas trouv");
} catch (IOException e) {
System.err.println("erreur lors de la lecture du fichier");
}
}
}
package gui;
public class Machine {
private int ID;
private String type;
private int processTime;
private int position;
public Machine(int iD, String type, int processTime, int position) {
super();
ID = iD;
this.type = type;
this.processTime = processTime;
this.position = position;
}
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getProcessTime() {
return processTime;
}
public void setProcessTime(int processTime) {
this.processTime = processTime;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
}
package gui;
import java.util.List;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
public class MoveMessageHandle extends CyclicBehaviour {
@Override
public void action() {
// TODO Auto-generated method stub
ACLMessage m = myAgent.receive(MessageTemplate.MatchPerformative(DemandeMoveMessage.DEMANDE_MOVE));
if(m == null) {
block();
}else {
String[] list= m.getContent().split(";");
int pid = Integer.parseInt(list[0]);
int moveLength = Integer.parseInt(list[1]);
System.out.println(pid+" move "+moveLength);
Painting paint = ((GUI)myAgent).getPainint();
List<Product> plist = paint.getProductList();
for(Product p:plist) {
if(p.getID() == pid) {
int pos = p.getPosition();
p.setPosition(pos+moveLength);
}
}
// ACLMessage rep = new ACLMessage(ACLMessage.INFORM);
// rep.addReceiver(m.getSender());
// myAgent.send(rep);
}
}
}
package gui;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
public class Painting extends JPanel {
private static final int RectHight = 50;
private static final int RectWidth = 100;
private static final int LineLength = 150;
private static final int Center = 55;//Center=x+RectHight/2
// private int posInput;
// --------------------------------
private int stateInput;// state of Input(Entry) :busy or not
private int stateLineET;// state of Line between Input(Entry) and Transformer
private int stateTransf;// state of Transformer
private int stateLineTS;// state of Line between Transformer and Output(Exit)
private int stateOutput;// state of Output(Exit)
// private int capacityLineIn;
// private int capacityLineOut;
private List<Machine> machineList;
// private Product[] productList;// list of product on the line
private List<Product> productList;
// ------------ -----------------
public Painting() {
super();
this.stateInput = 0;
this.stateLineET = 0;
this.stateTransf = 0;
this.stateLineTS = 0;
this.stateOutput = 0;
this.productList = new ArrayList<Product>();
}
/*
public Painting(int stateInput, int stateLineET, int stateTransf, int stateLineTS, int stateOutput,
int capacityLineIn, int capacityLineOut) {
super();
this.stateInput = stateInput;
this.stateLineET = stateLineET;
this.stateTransf = stateTransf;
this.stateLineTS = stateLineTS;
this.stateOutput = stateOutput;
this.capacityLineIn = capacityLineIn;
this.capacityLineOut = capacityLineOut;
this.productList = new Product[capacityLineIn+capacityLineOut];
}
*/
public List<Product> getProductList(){
return productList;
}
// The position of product
private int posx = 30;
// private int posy = 40;
public void setX(int x) {
posx = x;
// repaint();
}
@Override
public void paint(Graphics g){
// file the Rect Area with background color: use to reset the painting before repaint
g.clearRect(0, 0, 800, 600);
g.setColor(Color.GRAY);
g.fillRect(30, 30, RectWidth, RectHight);
g.setColor(Color.BLACK);
g.drawRect(30, 30, RectWidth, RectHight);
g.drawString("Entree", 30+RectWidth/3, Center);
g.drawLine(30+RectWidth, Center, 30+RectWidth+LineLength, Center);
g.setColor(Color.GRAY);
g.fillRect(30+RectWidth+LineLength, 30, RectWidth, RectHight);
g.setColor(Color.BLACK);
g.drawRect(30+RectWidth+LineLength, 30, RectWidth, RectHight);
g.drawString("Transf", 30+(RectWidth+LineLength)+RectWidth/3, 30+RectHight/2);
g.drawLine(30+RectWidth*2+LineLength, Center, 30+(RectWidth+LineLength)*2, Center);
g.setColor(Color.GRAY);
g.fillRect(30+(RectWidth+LineLength)*2, 30, RectWidth, RectHight);
g.setColor(Color.BLACK);
g.drawRect(30+(RectWidth+LineLength)*2, 30, RectWidth, RectHight);
g.drawString("Sortie", 30+(RectWidth+LineLength)*2+RectWidth/3, 30+RectHight/2);
for(Product p : productList) {
g.setColor(Color.RED);
g.fillRect(p.getPosition(), 40, 10, 10);
g.setColor(Color.BLACK);
g.drawRect(p.getPosition(), 40, 10, 10);
}
}
}
package gui;
public class Product{
private static int unique = 0;
private int ID;
private int type;
private int position;
public Product(int type, int position) {
super();
this.ID = unique;
this.type = type;
this.position = position;
unique++;
}
public Product(int ID, int type, int position) {
super();
this.ID = ID;
this.type = type;
this.position = position;
}
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
}
package gui;
import jade.core.Agent;
import jade.core.behaviours.TickerBehaviour;
public class Repaint extends TickerBehaviour {
GUI agent;
Painting p;
public Repaint(GUI a, long period) {
super(a, period);
// TODO Auto-generated constructor stub
this.agent = a;
this.p = a.getPainint();
}
@Override
protected void onTick() {
// TODO Auto-generated method stub
p.repaint();
}
}
package gui;
import jade.core.Agent;
public class Sender extends Agent {
private int numero;
private int capacity;// = rand.nextInt(10);
public void setup() {
// PRODUCTEUR.setName("les agents producteurs");
System.out.println(getName());//getLocalName()
Object[] args = getArguments();
numero = (Integer)args[0];//unique++;
capacity = (Integer)args[1];
System.out.println("agent " + numero + " capacity: " + capacity + " --> je suis pret a m'executer");
// behavior
addBehaviour(new DemandeMoveMessage());// Agent of type Product should send this message -> wait the main package
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment