Skip to content

File ReplicateToReachRGAggregate.java

File List > behaviour > replication > ReplicateToReachRGAggregate.java

Go to the documentation of this file

package skydata.behaviour.replication;

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

import jade.core.behaviours.TickerBehaviour;
import skydata.internal.agents.SKAgent;
import skydata.internal.agents.SKD;
import skydata.internal.behaviours.SKAgentBehaviour;
import skydata.internal.message.SKAID;

public class ReplicateToReachRGAggregate extends SKAgentBehaviour {

    String propertyName = "available";

    private SKD skd;

    public ReplicateToReachRGAggregate(SKAgent agent) {
        super(agent);
        skd = (SKD) agent;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private SKAID select(HashMap<SKAID, HashMap<String, Comparable>> harboursStats, String property) {
        agent.print("Here !");
        SKAID myH = skd.myHarbour();
        if (!harboursStats.containsKey(myH))
            return null;
        Comparable myValue = harboursStats.get(myH).get(property);
        if (myValue == null)
            return null;

        SKAID bestH = null;
        Comparable bestValue = null;
        for (Map.Entry<SKAID, HashMap<String, Comparable>> harbourStats : harboursStats.entrySet()) {
            Comparable value = harbourStats.getValue().get(property);
            if ((Integer) harbourStats.getValue().get(propertyName) >= skd.size) {
                if (bestH == null) {
                    bestValue = value;
                    bestH = harbourStats.getKey();
                } else if (value.compareTo(bestValue) < 0) {
                    bestValue = value;
                    bestH = harbourStats.getKey();
                }
            }
        }
        return bestH;
    }

    @SuppressWarnings("rawtypes")
    @Override
    public void action() {
        SKD agent = (SKD) this.agent;
        int rg = (Integer) (agent.args.get("rg"));
        HashMap<SKAID, HashMap<String, Comparable>> harboursStats = agent.getHarboursStats();



        TickerBehaviour tickerBehaviour = new TickerBehaviour(agent, 5000) {
            @Override
            public void onTick() {
                if (agent.canReplicate()) {
                    if (agent.getFamilySize() < rg) {
                        agent.print("Je veux créer un clone");
                        SKAID harbour = select(harboursStats, propertyName);
                        if (harbour == null) {
                            return;// Available harbour not found
                        }
                        agent.print("Je veux créer un clone sur " + harbour.getName());
                        agent.clone(harbour.toAID(),
                                agent.getLocalName().split("_")[0] + "." + (agent.lastReplicateIndex + 1));

                    }
                }
            }
        };
        agent.addBehaviour(tickerBehaviour);
    }

}