From 6d034168f315f8ac40471bc298252c096f6941f6 Mon Sep 17 00:00:00 2001 From: Stefan Woltmann <56683670+IngStefan@users.noreply.github.com> Date: Sun, 27 Jun 2021 12:44:13 +0200 Subject: [PATCH] Bugfixes and minor changes --- .../java/mas/JADE_VPP/ContainerManager.java | 8 +- .../java/mas/JADE_VPP/ProducingRest_TU.java | 47 +- .../java/mas/JADE_VPP/ProducingRest_VPP.java | 1 + .../src/main/java/mas/JADE_VPP/Startup.java | 220 +- .../src/main/java/mas/JADE_VPP/TU_Agent.java | 1720 ++++++++++++++ .../main/java/mas/JADE_VPP/TuVariables.java | 1 + .../src/main/java/mas/JADE_VPP/VPP_Agent.java | 2087 +++++++++++++++++ .../ontology/CancelOperationCancel.java | 31 + .../JADE_VPP/ontology/VPP_DR_Ontology.java | 30 +- 9 files changed, 4015 insertions(+), 130 deletions(-) create mode 100644 JADE_VPP/src/main/java/mas/JADE_VPP/TU_Agent.java create mode 100644 JADE_VPP/src/main/java/mas/JADE_VPP/VPP_Agent.java create mode 100644 JADE_VPP/src/main/java/mas/JADE_VPP/ontology/CancelOperationCancel.java diff --git a/JADE_VPP/src/main/java/mas/JADE_VPP/ContainerManager.java b/JADE_VPP/src/main/java/mas/JADE_VPP/ContainerManager.java index 8d1f03a..8d32f1a 100644 --- a/JADE_VPP/src/main/java/mas/JADE_VPP/ContainerManager.java +++ b/JADE_VPP/src/main/java/mas/JADE_VPP/ContainerManager.java @@ -29,20 +29,20 @@ private void init(){ jade.core.Runtime runtime = jade.core.Runtime.instance(); //runtime.setCloseVM(true); - ////***** for MAIN CONTAINER (VPP) ************ + //************ For creation as Main Container (VPP) ************ //Creates a profile for the start of the Main Container (for VPP Agent) Profile p = new jade.core.ProfileImpl(); p.setParameter(jade.core.Profile.CONTAINER_NAME, "VPP"); p.setParameter(jade.core.Profile.MAIN_HOST, "localhost"); - p.setParameter(jade.core.Profile.GUI, "true"); //starts the JADE user interface + p.setParameter(jade.core.Profile.GUI, "true"); //enables JADE UI containerController = runtime.createMainContainer(p); - ////***** for Agent CONTAINER (TU) ************ + //************ For creation as Agent Container (TU) ************ //Alternative profile to join a Main Container (for TU-Agents) // Profile p = new jade.core.ProfileImpl(); // p.setParameter(jade.core.Profile.CONTAINER_NAME, "TU_Company3"); -// p.setParameter(jade.core.Profile.MAIN_HOST, "192.168.152.233"); // IP of the Main Host the TUs should connect to +// p.setParameter(jade.core.Profile.MAIN_HOST, "192.168.152.233"); // IP-Address of MainContainer // containerController = runtime.createAgentContainer(p); } diff --git a/JADE_VPP/src/main/java/mas/JADE_VPP/ProducingRest_TU.java b/JADE_VPP/src/main/java/mas/JADE_VPP/ProducingRest_TU.java index eedea1c..ee5ff25 100644 --- a/JADE_VPP/src/main/java/mas/JADE_VPP/ProducingRest_TU.java +++ b/JADE_VPP/src/main/java/mas/JADE_VPP/ProducingRest_TU.java @@ -125,7 +125,6 @@ public String operationCancelFail( //**************************** INTERFACE TO NODE-RED (TU) ******************************* - //************************ TU Control ************************** @RequestMapping (method=RequestMethod.PUT, value = "/tu-agent/control_newSetpointFailed") @@ -164,6 +163,52 @@ public InterfacePayloadBalancing newBalancingRefuse(@RequestParam(name = "tuName return payload; } + @RequestMapping (method=RequestMethod.PUT, value = "/tu-agent/balancing_informInstant") + public InterfacePayloadBalancing newBalancingInformInstant( + @RequestParam(name = "tuName") String _tuName, + @RequestParam(name = "feedIn") int _feedIn, + @RequestParam(name = "operatingPoint") int _operatingPoint, + @RequestParam(name = "leadingOperatingPoint") int _leadingOperatingPoint, + @RequestParam(name = "currentValueFR") int _currentValueFR, + @RequestParam(name = "assignedPool") int _assignedPool, + @RequestParam(name = "status") int _status, + @RequestParam(name = "frequency") int _frequency, + @RequestParam(name = "aFRRsetpoint") int _aFRRsetpoint, + @RequestParam(name = "aFRRsetpointEcho") int _aFRRsetpointEcho, + @RequestParam(name = "setpointFR") int _setpointFR, + @RequestParam(name = "aFRRGradientPOS") int _aFRRGradientPOS, + @RequestParam(name = "aFRRGradientNEG") int _aFRRGradientNEG, + @RequestParam(name = "capacityPOS") int _capacityPOS, + @RequestParam(name = "capacityNEG") int _capacityNEG, + @RequestParam(name = "holdingCapacityPOS") int _holdingCapacityPOS, + @RequestParam(name = "holdingCapacityNEG") int _holdingCapacityNEG, + @RequestParam(name = "controlBandPOS") int _controlBandPOS, + @RequestParam(name = "controlBandNEG") int _controlBandNEG) { + InterfacePayloadBalancing payload = new InterfacePayloadBalancing(_tuName); + TuVariables.balancingTuName = _tuName; + TuVariables.feedIn = _feedIn; //Einspeisung + TuVariables.operatingPoint = _operatingPoint; //Arbeitspunkt + TuVariables.leadingOperatingPoint = _leadingOperatingPoint; //vorauseilender Arbeitspunkt + TuVariables.currentValueFR = _currentValueFR; //Regelleistungsistwert + TuVariables.assignedPool = _assignedPool; //Poolzuordnung + TuVariables.status = _status; //Status (Meldung) + TuVariables.frequency = _frequency; //Frequenz + TuVariables.aFRRsetpoint = _aFRRsetpoint; //aFRR-Soll (ÜNB -> POOL) + TuVariables.aFRRsetpointEcho = _aFRRsetpointEcho; //aFRR-Soll-Echo (Pool -> ÜNB) + TuVariables.setpointFR = _setpointFR; //Regelleistungs-Soll + TuVariables.aFRRGradientPOS = _aFRRGradientPOS; //aFRR-Gradient POS + TuVariables.aFRRGradientNEG = _aFRRGradientNEG; //aFRR-Gradient NEG + TuVariables.capacityPOS = _capacityPOS; //Arbeitsvermögen POS (bei begrenztem Energiespeicher) + TuVariables.capacityNEG = _capacityNEG; //Arbeitsvermögen NEG (bei begrenzten Energiespeicher) + TuVariables.holdingCapacityPOS = _holdingCapacityPOS; //Aktuelle Vorhalteleistung POS + TuVariables.holdingCapacityNEG = _holdingCapacityNEG; //Aktuelle Vorhalteleistung NEG + TuVariables.controlBandPOS = _controlBandPOS; //Regelband POS + TuVariables.controlBandNEG = _controlBandNEG; //Regelband NEG + TuVariables.balancingInformInstantTrigger = true; + return payload; + } + + @RequestMapping (method=RequestMethod.PUT, value = "/tu-agent/balancing_inform") public InterfacePayloadBalancing newBalancingInform( @RequestParam(name = "tuName") String _tuName, diff --git a/JADE_VPP/src/main/java/mas/JADE_VPP/ProducingRest_VPP.java b/JADE_VPP/src/main/java/mas/JADE_VPP/ProducingRest_VPP.java index 467b600..dbb49e9 100644 --- a/JADE_VPP/src/main/java/mas/JADE_VPP/ProducingRest_VPP.java +++ b/JADE_VPP/src/main/java/mas/JADE_VPP/ProducingRest_VPP.java @@ -16,6 +16,7 @@ @RestController public class ProducingRest_VPP { + //**************************** INTERFACE TO NODE-RED (VPP) ******************************* //********************** Scheduling ********************** @RequestMapping (method=RequestMethod.PUT, value = "/vpp-agent/scheduling_request") diff --git a/JADE_VPP/src/main/java/mas/JADE_VPP/Startup.java b/JADE_VPP/src/main/java/mas/JADE_VPP/Startup.java index 19bb197..5f4d17d 100644 --- a/JADE_VPP/src/main/java/mas/JADE_VPP/Startup.java +++ b/JADE_VPP/src/main/java/mas/JADE_VPP/Startup.java @@ -27,122 +27,122 @@ public static void main(String[] args) //******* start the platform (changes to the container can be made in the "ContainerManager" class) try { - //******* starting the VPP agent of the platform (1st parameter: AgentName, 2nd parameter: class name) - ContainerManager.getInstance().instantiateAgent("VPP_Aggregator_1", "mas.JADE_VPP.VPP"); - //******* starting an agent with additional arguments as 3rd parameter - //Object[] agentArgs = {500, 511, 399, 555, 1500, 1600, 200, 900, 65, 1233, 426, 468, 934}; // - //ContainerManager.getInstance().instantiateAgent("VPP2", "mas.JADE_VPP.VPP", agentArgs); + //Creation of an agent at startup: + //ContainerManager.getInstance().instantiateAgent(, , ); + ContainerManager.getInstance().instantiateAgent("VPP_Aggregator_1", "mas.JADE_VPP.VPP_Agent"); + +// ContainerManager.getInstance().instantiateAgent("TU_Company1_PRL", "mas.JADE_VPP.TU_Agent", new String[]{"PRL"}); +// ContainerManager.getInstance().instantiateAgent("TU_Company1_SRL", "mas.JADE_VPP.TU_Agent", new String[]{"SRL"}); -// //******* starting TU Agents (1st parameter: AgentName, 2nd parameter: class name, 3rd parameter: service description in comma separation) /* - ContainerManager.getInstance().instantiateAgent("TU_Company1_PRL", "mas.JADE_VPP.TU", new String[]{"PRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company2_PRL", "mas.JADE_VPP.TU", new String[]{"PRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company3_PRL", "mas.JADE_VPP.TU", new String[]{"PRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company1_SRL", "mas.JADE_VPP.TU", new String[]{"SRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company2_SRL", "mas.JADE_VPP.TU", new String[]{"SRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company3_SRL", "mas.JADE_VPP.TU", new String[]{"SRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company2_SRL(BACKUP)", "mas.JADE_VPP.TU", new String[]{"SRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company3_SRL(BACKUP)", "mas.JADE_VPP.TU", new String[]{"SRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company4_SRL", "mas.JADE_VPP.TU", new String[]{"SRL,FLEX"}); - ContainerManager.getInstance().instantiateAgent("TU_Company5_SRL", "mas.JADE_VPP.TU", new String[]{"SRL,FLEX"}); - ContainerManager.getInstance().instantiateAgent("TU_Company1_MRL", "mas.JADE_VPP.TU", new String[]{"MRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company2_MRL", "mas.JADE_VPP.TU", new String[]{"MRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company3_MRL", "mas.JADE_VPP.TU", new String[]{"MRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company4_MRL", "mas.JADE_VPP.TU", new String[]{"MRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company5_MRL_A", "mas.JADE_VPP.TU", new String[]{"MRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company5_MRL_B", "mas.JADE_VPP.TU", new String[]{"MRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company5_MRL_C", "mas.JADE_VPP.TU", new String[]{"MRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company6_SOL", "mas.JADE_VPP.TU", new String[]{"SOL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company7_SOL", "mas.JADE_VPP.TU", new String[]{"SOL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company8_SOL", "mas.JADE_VPP.TU", new String[]{"SOL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company9_SOL", "mas.JADE_VPP.TU", new String[]{"SOL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company10_SNL", "mas.JADE_VPP.TU", new String[]{"SNL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company11_SNL", "mas.JADE_VPP.TU", new String[]{"SNL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company12_SNL", "mas.JADE_VPP.TU", new String[]{"SNL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company13_SNL", "mas.JADE_VPP.TU", new String[]{"SNL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company14_SNL", "mas.JADE_VPP.TU", new String[]{"SNL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company15_SNL", "mas.JADE_VPP.TU", new String[]{"SNL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company16_FLEX_pla", "mas.JADE_VPP.TU", new String[]{"FLEX_pla"}); - ContainerManager.getInstance().instantiateAgent("TU_Company16_FLEX_act", "mas.JADE_VPP.TU", new String[]{"FLEX_act"}); - ContainerManager.getInstance().instantiateAgent("TU_Company16_FLEX_acc", "mas.JADE_VPP.TU", new String[]{"FLEX_acc"}); - ContainerManager.getInstance().instantiateAgent("TU_Company17_FLEX_pla", "mas.JADE_VPP.TU", new String[]{"FLEX_pla"}); - ContainerManager.getInstance().instantiateAgent("TU_Company17_FLEX_act", "mas.JADE_VPP.TU", new String[]{"FLEX_act"}); - ContainerManager.getInstance().instantiateAgent("TU_Company17_FLEX_acc", "mas.JADE_VPP.TU", new String[]{"FLEX_acc"}); - ContainerManager.getInstance().instantiateAgent("TU_Company18_FLEX_pla", "mas.JADE_VPP.TU", new String[]{"FLEX_pla"}); - ContainerManager.getInstance().instantiateAgent("TU_Company18_FLEX_act", "mas.JADE_VPP.TU", new String[]{"FLEX_act"}); - ContainerManager.getInstance().instantiateAgent("TU_Company18_FLEX_acc", "mas.JADE_VPP.TU", new String[]{"FLEX_acc"}); - ContainerManager.getInstance().instantiateAgent("TU_Company19_FLEX_pla", "mas.JADE_VPP.TU", new String[]{"FLEX_pla"}); - ContainerManager.getInstance().instantiateAgent("TU_Company19_FLEX_act", "mas.JADE_VPP.TU", new String[]{"FLEX_act"}); - ContainerManager.getInstance().instantiateAgent("TU_Company19_FLEX_acc", "mas.JADE_VPP.TU", new String[]{"FLEX_acc"}); - ContainerManager.getInstance().instantiateAgent("TU_Company20_FLEX_pla", "mas.JADE_VPP.TU", new String[]{"FLEX_pla"}); - ContainerManager.getInstance().instantiateAgent("TU_Company20_FLEX_act", "mas.JADE_VPP.TU", new String[]{"FLEX_act"}); - ContainerManager.getInstance().instantiateAgent("TU_Company20_FLEX_acc", "mas.JADE_VPP.TU", new String[]{"FLEX_acc"}); - ContainerManager.getInstance().instantiateAgent("TU_Company21_FLEX", "mas.JADE_VPP.TU", new String[]{"FLEX"}); - ContainerManager.getInstance().instantiateAgent("TU_Company22_FLEX", "mas.JADE_VPP.TU", new String[]{"FLEX"}); - ContainerManager.getInstance().instantiateAgent("TU_Company23_FLEX", "mas.JADE_VPP.TU", new String[]{"FLEX"}); - ContainerManager.getInstance().instantiateAgent("TU_Company24_FLEX", "mas.JADE_VPP.TU", new String[]{"FLEX"}); - ContainerManager.getInstance().instantiateAgent("TU_Company25_FLEX", "mas.JADE_VPP.TU", new String[]{"FLEX"}); - ContainerManager.getInstance().instantiateAgent("TU_Company26_FLEX", "mas.JADE_VPP.TU", new String[]{"FLEX"}); - ContainerManager.getInstance().instantiateAgent("TU_Company27_FLEX", "mas.JADE_VPP.TU", new String[]{"FLEX"}); - ContainerManager.getInstance().instantiateAgent("TU_Company28_FLEX", "mas.JADE_VPP.TU", new String[]{"ANN"}); - ContainerManager.getInstance().instantiateAgent("TU_Company29_FLEX", "mas.JADE_VPP.TU", new String[]{"ANN"}); - ContainerManager.getInstance().instantiateAgent("TU_Company30_FLEX", "mas.JADE_VPP.TU", new String[]{"ANN"}); + ContainerManager.getInstance().instantiateAgent("TU_Company1_PRL", "mas.JADE_VPP.TU_Agent", new String[]{"PRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company2_PRL", "mas.JADE_VPP.TU_Agent", new String[]{"PRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company3_PRL", "mas.JADE_VPP.TU_Agent", new String[]{"PRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company1_SRL", "mas.JADE_VPP.TU_Agent", new String[]{"SRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company2_SRL", "mas.JADE_VPP.TU_Agent", new String[]{"SRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company3_SRL", "mas.JADE_VPP.TU_Agent", new String[]{"SRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company2_SRL(BACKUP)", "mas.JADE_VPP.TU_Agent", new String[]{"SRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company3_SRL(BACKUP)", "mas.JADE_VPP.TU_Agent", new String[]{"SRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company4_SRL", "mas.JADE_VPP.TU_Agent", new String[]{"SRL,FLEX"}); + ContainerManager.getInstance().instantiateAgent("TU_Company5_SRL", "mas.JADE_VPP.TU_Agent", new String[]{"SRL,FLEX"}); + ContainerManager.getInstance().instantiateAgent("TU_Company1_MRL", "mas.JADE_VPP.TU_Agent", new String[]{"MRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company2_MRL", "mas.JADE_VPP.TU_Agent", new String[]{"MRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company3_MRL", "mas.JADE_VPP.TU_Agent", new String[]{"MRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company4_MRL", "mas.JADE_VPP.TU_Agent", new String[]{"MRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company5_MRL_A", "mas.JADE_VPP.TU_Agent", new String[]{"MRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company5_MRL_B", "mas.JADE_VPP.TU_Agent", new String[]{"MRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company5_MRL_C", "mas.JADE_VPP.TU_Agent", new String[]{"MRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company6_SOL", "mas.JADE_VPP.TU_Agent", new String[]{"SOL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company7_SOL", "mas.JADE_VPP.TU_Agent", new String[]{"SOL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company8_SOL", "mas.JADE_VPP.TU_Agent", new String[]{"SOL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company9_SOL", "mas.JADE_VPP.TU_Agent", new String[]{"SOL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company10_SNL", "mas.JADE_VPP.TU_Agent", new String[]{"SNL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company11_SNL", "mas.JADE_VPP.TU_Agent", new String[]{"SNL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company12_SNL", "mas.JADE_VPP.TU_Agent", new String[]{"SNL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company13_SNL", "mas.JADE_VPP.TU_Agent", new String[]{"SNL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company14_SNL", "mas.JADE_VPP.TU_Agent", new String[]{"SNL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company15_SNL", "mas.JADE_VPP.TU_Agent", new String[]{"SNL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company16_FLEX_pla", "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_pla"}); + ContainerManager.getInstance().instantiateAgent("TU_Company16_FLEX_act", "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_act"}); + ContainerManager.getInstance().instantiateAgent("TU_Company16_FLEX_acc", "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_acc"}); + ContainerManager.getInstance().instantiateAgent("TU_Company17_FLEX_pla", "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_pla"}); + ContainerManager.getInstance().instantiateAgent("TU_Company17_FLEX_act", "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_act"}); + ContainerManager.getInstance().instantiateAgent("TU_Company17_FLEX_acc", "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_acc"}); + ContainerManager.getInstance().instantiateAgent("TU_Company18_FLEX_pla", "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_pla"}); + ContainerManager.getInstance().instantiateAgent("TU_Company18_FLEX_act", "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_act"}); + ContainerManager.getInstance().instantiateAgent("TU_Company18_FLEX_acc", "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_acc"}); + ContainerManager.getInstance().instantiateAgent("TU_Company19_FLEX_pla", "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_pla"}); + ContainerManager.getInstance().instantiateAgent("TU_Company19_FLEX_act", "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_act"}); + ContainerManager.getInstance().instantiateAgent("TU_Company19_FLEX_acc", "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_acc"}); + ContainerManager.getInstance().instantiateAgent("TU_Company20_FLEX_pla", "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_pla"}); + ContainerManager.getInstance().instantiateAgent("TU_Company20_FLEX_act", "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_act"}); + ContainerManager.getInstance().instantiateAgent("TU_Company20_FLEX_acc", "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_acc"}); + ContainerManager.getInstance().instantiateAgent("TU_Company21_FLEX", "mas.JADE_VPP.TU_Agent", new String[]{"FLEX"}); + ContainerManager.getInstance().instantiateAgent("TU_Company22_FLEX", "mas.JADE_VPP.TU_Agent", new String[]{"FLEX"}); + ContainerManager.getInstance().instantiateAgent("TU_Company23_FLEX", "mas.JADE_VPP.TU_Agent", new String[]{"FLEX"}); + ContainerManager.getInstance().instantiateAgent("TU_Company24_FLEX", "mas.JADE_VPP.TU_Agent", new String[]{"FLEX"}); + ContainerManager.getInstance().instantiateAgent("TU_Company25_FLEX", "mas.JADE_VPP.TU_Agent", new String[]{"FLEX"}); + ContainerManager.getInstance().instantiateAgent("TU_Company26_FLEX", "mas.JADE_VPP.TU_Agent", new String[]{"FLEX"}); + ContainerManager.getInstance().instantiateAgent("TU_Company27_FLEX", "mas.JADE_VPP.TU_Agent", new String[]{"FLEX"}); + ContainerManager.getInstance().instantiateAgent("TU_Company28_FLEX", "mas.JADE_VPP.TU_Agent", new String[]{"ANN"}); + ContainerManager.getInstance().instantiateAgent("TU_Company29_FLEX", "mas.JADE_VPP.TU_Agent", new String[]{"ANN"}); + ContainerManager.getInstance().instantiateAgent("TU_Company30_FLEX", "mas.JADE_VPP.TU_Agent", new String[]{"ANN"}); //**** Adding additional Agents with a multiplier for(int i = 0 ; i < factor ; i++){ - ContainerManager.getInstance().instantiateAgent("TU_Company1_PRL+"+i, "mas.JADE_VPP.TU", new String[]{"PRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company2_PRL+"+i, "mas.JADE_VPP.TU", new String[]{"PRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company3_PRL+"+i, "mas.JADE_VPP.TU", new String[]{"PRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company1_SRL+"+i, "mas.JADE_VPP.TU", new String[]{"SRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company2_SRL+"+i, "mas.JADE_VPP.TU", new String[]{"SRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company3_SRL+"+i, "mas.JADE_VPP.TU", new String[]{"SRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company2_SRL(BACKUP)+"+i, "mas.JADE_VPP.TU", new String[]{"SRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company3_SRL(BACKUP)+"+i, "mas.JADE_VPP.TU", new String[]{"SRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company4_SRL,FLEX+"+i, "mas.JADE_VPP.TU", new String[]{"SRL,FLEX"}); - ContainerManager.getInstance().instantiateAgent("TU_Company5_SRL,MRL+"+i, "mas.JADE_VPP.TU", new String[]{"SRL,MRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company1_MRL+"+i, "mas.JADE_VPP.TU", new String[]{"MRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company2_MRL+"+i, "mas.JADE_VPP.TU", new String[]{"MRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company3_MRL+"+i, "mas.JADE_VPP.TU", new String[]{"MRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company4_MRL+"+i, "mas.JADE_VPP.TU", new String[]{"MRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company5_MRL_A+"+i, "mas.JADE_VPP.TU", new String[]{"MRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company5_MRL_B+"+i, "mas.JADE_VPP.TU", new String[]{"MRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company5_MRL_C+"+i, "mas.JADE_VPP.TU", new String[]{"MRL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company6_SOL+"+i, "mas.JADE_VPP.TU", new String[]{"SOL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company7_SOL+"+i, "mas.JADE_VPP.TU", new String[]{"SOL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company8_SOL+"+i, "mas.JADE_VPP.TU", new String[]{"SOL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company9_SOL+"+i, "mas.JADE_VPP.TU", new String[]{"SOL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company10_SNL+"+i, "mas.JADE_VPP.TU", new String[]{"SNL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company11_SNL+"+i, "mas.JADE_VPP.TU", new String[]{"SNL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company12_SNL+"+i, "mas.JADE_VPP.TU", new String[]{"SNL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company13_SNL+"+i, "mas.JADE_VPP.TU", new String[]{"SNL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company14_SNL+"+i, "mas.JADE_VPP.TU", new String[]{"SNL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company15_SNL+"+i, "mas.JADE_VPP.TU", new String[]{"SNL"}); - ContainerManager.getInstance().instantiateAgent("TU_Company16_FLEX_pla+"+i, "mas.JADE_VPP.TU", new String[]{"FLEX_pla"}); - ContainerManager.getInstance().instantiateAgent("TU_Company16_FLEX_act+"+i, "mas.JADE_VPP.TU", new String[]{"FLEX_act"}); - ContainerManager.getInstance().instantiateAgent("TU_Company16_FLEX_acc+"+i, "mas.JADE_VPP.TU", new String[]{"FLEX_acc"}); - ContainerManager.getInstance().instantiateAgent("TU_Company17_FLEX_pla+"+i, "mas.JADE_VPP.TU", new String[]{"FLEX_pla"}); - ContainerManager.getInstance().instantiateAgent("TU_Company17_FLEX_act+"+i, "mas.JADE_VPP.TU", new String[]{"FLEX_act"}); - ContainerManager.getInstance().instantiateAgent("TU_Company17_FLEX_acc+"+i, "mas.JADE_VPP.TU", new String[]{"FLEX_acc"}); - ContainerManager.getInstance().instantiateAgent("TU_Company18_FLEX_pla+"+i, "mas.JADE_VPP.TU", new String[]{"FLEX_pla"}); - ContainerManager.getInstance().instantiateAgent("TU_Company18_FLEX_act+"+i, "mas.JADE_VPP.TU", new String[]{"FLEX_act"}); - ContainerManager.getInstance().instantiateAgent("TU_Company18_FLEX_acc+"+i, "mas.JADE_VPP.TU", new String[]{"FLEX_acc"}); - ContainerManager.getInstance().instantiateAgent("TU_Company19_FLEX_pla+"+i, "mas.JADE_VPP.TU", new String[]{"FLEX_pla"}); - ContainerManager.getInstance().instantiateAgent("TU_Company19_FLEX_act+"+i, "mas.JADE_VPP.TU", new String[]{"FLEX_act"}); - ContainerManager.getInstance().instantiateAgent("TU_Company19_FLEX_acc+"+i, "mas.JADE_VPP.TU", new String[]{"FLEX_acc"}); - ContainerManager.getInstance().instantiateAgent("TU_Company20_FLEX_pla+"+i, "mas.JADE_VPP.TU", new String[]{"FLEX_pla"}); - ContainerManager.getInstance().instantiateAgent("TU_Company20_FLEX_act+"+i, "mas.JADE_VPP.TU", new String[]{"FLEX_act"}); - ContainerManager.getInstance().instantiateAgent("TU_Company20_FLEX_acc+"+i, "mas.JADE_VPP.TU", new String[]{"FLEX_acc"}); - ContainerManager.getInstance().instantiateAgent("TU_Company21_FLEX"+i, "mas.JADE_VPP.TU", new String[]{"FLEX"}); - ContainerManager.getInstance().instantiateAgent("TU_Company22_FLEX"+i, "mas.JADE_VPP.TU", new String[]{"FLEX"}); - ContainerManager.getInstance().instantiateAgent("TU_Company23_FLEX"+i, "mas.JADE_VPP.TU", new String[]{"FLEX"}); - ContainerManager.getInstance().instantiateAgent("TU_Company24_FLEX"+i, "mas.JADE_VPP.TU", new String[]{"FLEX"}); - ContainerManager.getInstance().instantiateAgent("TU_Company25_FLEX"+i, "mas.JADE_VPP.TU", new String[]{"FLEX"}); - ContainerManager.getInstance().instantiateAgent("TU_Company26_FLEX"+i, "mas.JADE_VPP.TU", new String[]{"FLEX"}); - ContainerManager.getInstance().instantiateAgent("TU_Company27_FLEX"+i, "mas.JADE_VPP.TU", new String[]{"FLEX"}); - ContainerManager.getInstance().instantiateAgent("TU_Company28_FLEX"+i, "mas.JADE_VPP.TU", new String[]{"ANN"}); - ContainerManager.getInstance().instantiateAgent("TU_Company29_FLEX"+i, "mas.JADE_VPP.TU", new String[]{"ANN"}); - ContainerManager.getInstance().instantiateAgent("TU_Company30_FLEX"+i, "mas.JADE_VPP.TU", new String[]{"ANN"}); + ContainerManager.getInstance().instantiateAgent("TU_Company1_PRL+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"PRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company2_PRL+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"PRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company3_PRL+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"PRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company1_SRL+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"SRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company2_SRL+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"SRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company3_SRL+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"SRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company2_SRL(BACKUP)+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"SRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company3_SRL(BACKUP)+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"SRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company4_SRL,FLEX+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"SRL,FLEX"}); + ContainerManager.getInstance().instantiateAgent("TU_Company5_SRL,MRL+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"SRL,MRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company1_MRL+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"MRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company2_MRL+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"MRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company3_MRL+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"MRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company4_MRL+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"MRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company5_MRL_A+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"MRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company5_MRL_B+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"MRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company5_MRL_C+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"MRL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company6_SOL+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"SOL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company7_SOL+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"SOL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company8_SOL+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"SOL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company9_SOL+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"SOL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company10_SNL+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"SNL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company11_SNL+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"SNL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company12_SNL+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"SNL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company13_SNL+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"SNL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company14_SNL+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"SNL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company15_SNL+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"SNL"}); + ContainerManager.getInstance().instantiateAgent("TU_Company16_FLEX_pla+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_pla"}); + ContainerManager.getInstance().instantiateAgent("TU_Company16_FLEX_act+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_act"}); + ContainerManager.getInstance().instantiateAgent("TU_Company16_FLEX_acc+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_acc"}); + ContainerManager.getInstance().instantiateAgent("TU_Company17_FLEX_pla+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_pla"}); + ContainerManager.getInstance().instantiateAgent("TU_Company17_FLEX_act+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_act"}); + ContainerManager.getInstance().instantiateAgent("TU_Company17_FLEX_acc+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_acc"}); + ContainerManager.getInstance().instantiateAgent("TU_Company18_FLEX_pla+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_pla"}); + ContainerManager.getInstance().instantiateAgent("TU_Company18_FLEX_act+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_act"}); + ContainerManager.getInstance().instantiateAgent("TU_Company18_FLEX_acc+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_acc"}); + ContainerManager.getInstance().instantiateAgent("TU_Company19_FLEX_pla+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_pla"}); + ContainerManager.getInstance().instantiateAgent("TU_Company19_FLEX_act+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_act"}); + ContainerManager.getInstance().instantiateAgent("TU_Company19_FLEX_acc+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_acc"}); + ContainerManager.getInstance().instantiateAgent("TU_Company20_FLEX_pla+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_pla"}); + ContainerManager.getInstance().instantiateAgent("TU_Company20_FLEX_act+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_act"}); + ContainerManager.getInstance().instantiateAgent("TU_Company20_FLEX_acc+"+i, "mas.JADE_VPP.TU_Agent", new String[]{"FLEX_acc"}); + ContainerManager.getInstance().instantiateAgent("TU_Company21_FLEX"+i, "mas.JADE_VPP.TU_Agent", new String[]{"FLEX"}); + ContainerManager.getInstance().instantiateAgent("TU_Company22_FLEX"+i, "mas.JADE_VPP.TU_Agent", new String[]{"FLEX"}); + ContainerManager.getInstance().instantiateAgent("TU_Company23_FLEX"+i, "mas.JADE_VPP.TU_Agent", new String[]{"FLEX"}); + ContainerManager.getInstance().instantiateAgent("TU_Company24_FLEX"+i, "mas.JADE_VPP.TU_Agent", new String[]{"FLEX"}); + ContainerManager.getInstance().instantiateAgent("TU_Company25_FLEX"+i, "mas.JADE_VPP.TU_Agent", new String[]{"FLEX"}); + ContainerManager.getInstance().instantiateAgent("TU_Company26_FLEX"+i, "mas.JADE_VPP.TU_Agent", new String[]{"FLEX"}); + ContainerManager.getInstance().instantiateAgent("TU_Company27_FLEX"+i, "mas.JADE_VPP.TU_Agent", new String[]{"FLEX"}); + ContainerManager.getInstance().instantiateAgent("TU_Company28_FLEX"+i, "mas.JADE_VPP.TU_Agent", new String[]{"ANN"}); + ContainerManager.getInstance().instantiateAgent("TU_Company29_FLEX"+i, "mas.JADE_VPP.TU_Agent", new String[]{"ANN"}); + ContainerManager.getInstance().instantiateAgent("TU_Company30_FLEX"+i, "mas.JADE_VPP.TU_Agent", new String[]{"ANN"}); } */ diff --git a/JADE_VPP/src/main/java/mas/JADE_VPP/TU_Agent.java b/JADE_VPP/src/main/java/mas/JADE_VPP/TU_Agent.java new file mode 100644 index 0000000..8f8c891 --- /dev/null +++ b/JADE_VPP/src/main/java/mas/JADE_VPP/TU_Agent.java @@ -0,0 +1,1720 @@ +package mas.JADE_VPP; + + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import java.util.Locale; +import java.util.TimeZone; +import java.util.Timer; +import java.util.TimerTask; + +import jade.content.ContentElement; +import jade.content.ContentElementList; +import jade.content.ContentManager; +import jade.content.Predicate; +import jade.content.lang.Codec; +import jade.content.lang.Codec.CodecException; +import jade.content.lang.sl.SLCodec; +import jade.content.onto.Ontology; +import jade.content.onto.OntologyException; +import jade.content.onto.basic.Action; + +import jade.core.AID; +import jade.core.Agent; +import jade.core.behaviours.Behaviour; +import jade.core.behaviours.CyclicBehaviour; +import jade.core.behaviours.OneShotBehaviour; +import jade.core.behaviours.ThreadedBehaviourFactory; + +import jade.domain.DFService; +import jade.domain.FIPAException; +import jade.domain.FIPAAgentManagement.DFAgentDescription; +import jade.domain.FIPAAgentManagement.ServiceDescription; + +import java.lang.Object; +import jade.lang.acl.ACLMessage; +import jade.lang.acl.MessageTemplate; +import mas.JADE_VPP.ontology.*; + +public class TU_Agent extends Agent { + private static final long serialVersionUID = 1L; + private ServiceDescription sd; + private Codec codec = new SLCodec(); + //private Codec codec2 = new LEAPCodec(); //required for data transfer via bytesequences (i.e. Scheduling Plans), can also be done with SL Codec + private Ontology ontology = VPP_DR_Ontology.getInstance(); + private ThreadedBehaviourFactory tbf = new ThreadedBehaviourFactory(); //tbf is needed for extra thread generation, which is needed to prevent a cpu usage of 100% + + //*********** Agent startup *********** + protected void setup(){ + ArrayList sdList = new ArrayList(); + //Setting Language and Ontology + //getContentManager().registerLanguage(codec2); + getContentManager().registerLanguage(codec); + getContentManager().registerOntology(ontology); + //****Getting Arguments from the Startup.class**** + Object[] args = getArguments(); + //the local name is user-set and given at the startup + String arguments = String.valueOf(args[0]); //get the arguments given from the Startup.java + arguments = arguments.replaceAll("\\s+",""); //remove all whitespaces + System.out.println("********* TU-Agent online: " + getAID().getName()+ " with Service Descriptions: " +String.valueOf(args[0])+ " *********"); + + if (args != null && args.length > 0) + { + List serviceDescriptionList = new ArrayList(); + serviceDescriptionList = Arrays.asList(arguments.split(",")); + for (int i = 0; i < serviceDescriptionList.size(); i++){ + sdList.add(serviceDescriptionList.get(i)); + } + //register / publishing own service + DFAgentDescription dfd = new DFAgentDescription(); + dfd.setName(getAID()); + dfd.addLanguages(codec.getName()); + //dfd.addLanguages(codec2.getName()); + dfd.addOntologies(ontology.getName()); + sd = new ServiceDescription(); + for(int i = 0; i switch to the next case + if(TuVariables.schedulingTrigger && (TuVariables.referenceID.equals(conversationID))){ + validOffer = true; + tuName = TuVariables.schedulingTUName; + TuVariables.schedulingTrigger = false; + step = 2; + }else if ((TuVariables.refuseSchedulingTrigger && (TuVariables.referenceID.equals(conversationID))) || dateNow.after(date)){ + validOffer = false; + TuVariables.refuseSchedulingTrigger = false; + step = 2; + } + + break; + case 2: + //******* sending an answer to the VPP according to the reaction of the ERP system ********** + try{ + if(validOffer) { + //Sending a Propose-Answer + ACLMessage reply = msg.createReply(); //create a new ACLMessage that is a reply to this message. In particular, it sets the following parameters of the new message: receiver, language, ontology, protocol, conversation-id, in-reply-to, reply-with + reply.setLanguage(codec.getName()); + reply.setPerformative(ACLMessage.PROPOSE); + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + CFPSchedulingSequencePropose newSchedulingSequencePropose = new CFPSchedulingSequencePropose(); + newSchedulingSequencePropose.setSchedulingPlan(TuVariables.schedulingPlan); + newSchedulingSequencePropose.setAgentName(myAgent.getLocalName()); + newSchedulingSequencePropose.setTuName(tuName); + cel.add(newSchedulingSequencePropose); + cm.fillContent(reply, cel); + myAgent.send(reply); + } + if(!validOffer) { + //Sending a REFUSE-Answer + ACLMessage reply = msg.createReply(); + reply.setPerformative(ACLMessage.REFUSE); + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + CFPSchedulingSequenceRefuse newSchedulingSequenceRefuse = new CFPSchedulingSequenceRefuse(); + newSchedulingSequenceRefuse.setAgentName(myAgent.getLocalName()); + newSchedulingSequenceRefuse.setTuName(tuName); + cel.add(newSchedulingSequenceRefuse); + cm.fillContent(reply, cel); + myAgent.send(reply); + step = 99; + } + } + catch (CodecException ce) { + ce.printStackTrace(); + } + catch (OntologyException oe) { + oe.printStackTrace(); + } + validOffer = false; + step = 3; + break; + //********** Receiving answers from VPP-Agent and informing the Planning-System ********** + case 3: + MessageTemplate mt = MessageTemplate.and( + MessageTemplate.MatchOntology(ontology.getName()),MessageTemplate.and( + MessageTemplate.MatchConversationId(conversationID),MessageTemplate.and( + MessageTemplate.MatchLanguage(codec.getName()),MessageTemplate.or( + MessageTemplate.MatchPerformative(ACLMessage.ACCEPT_PROPOSAL), + MessageTemplate.MatchPerformative(ACLMessage.REJECT_PROPOSAL))))); + msg = receive(mt); //returns the first message of the message queue with the corresponding template + if (msg != null){ + ContentElement ce = null; + if (msg.getPerformative() == ACLMessage.ACCEPT_PROPOSAL) { + System.out.println(this.getAgent().getAID().getLocalName()+"******* ACCEPT Message Received *******"); + try { + ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + //***************** CFPSchedulingSequence Accept ******************* + if(_pc instanceof CFPSchedulingSequenceAccept){ + timeBegin = ((CFPSchedulingSequenceAccept) _pc).getSchedulingStart(); + timeEnd = ((CFPSchedulingSequenceAccept) _pc).getSchedulingEnd(); + //********** Inform the ERP-System about the results********** + //******* the sending the new SchedulingPlan to the Planning System ******** + putInstance = new ConsumingRest_TU(); + payload = new InterfacePayloadPlanning(timeBegin, timeEnd, conversationID); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.SCHEDULINGACCEPTED, payload); + step = 4; + } + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + }else if (msg.getPerformative() == ACLMessage.REJECT_PROPOSAL) { + System.out.println(this.getAgent().getAID().getLocalName()+"******* REJECT Message Received *******"); + try { + ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + //***************** CFPSchedulingSequence Reject ******************* + if(_pc instanceof CFPSchedulingSequenceReject){ + //********** Inform the Planning-System about the results********** + //******* the sending the reject info to the Planning System ******** + putInstance = new ConsumingRest_TU(); + payload = new InterfacePayloadPlanning(conversationID); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.SCHEDULINGREJECTED, payload); + step = 99; + } + }catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + } + }else { + block(); + } + break; + //********** Receiving Info from Planning-System about the results********** + case 4: + dateNow = new Date(); + if(TuVariables.schedulingInformTrigger && (TuVariables.referenceID.equals(conversationID))){ + TuVariables.schedulingInformTrigger = false; + validOffer = true; + tuName = TuVariables.schedulingTUName; + step = 5; + }else if ((TuVariables.schedulingFailureTrigger && (TuVariables.referenceID.equals(conversationID))) || dateNow.after(dateTimeBegin)){ + TuVariables.schedulingFailureTrigger = false; + validOffer = false; + tuName = TuVariables.schedulingTUName; + step = 5; + } + break; + //********** Confirm the VPP-Agent about the results from the ERP-System********** + case 5: + try{ + if(validOffer) { + //Sending a INFORM-Done-Answer + ACLMessage reply = msg.createReply(); //create a new ACLMessage that is a reply to this message. In particular, it sets the following parameters of the new message: receiver, language, ontology, protocol, conversation-id, in-reply-to, reply-with + reply.setLanguage(codec.getName()); + reply.setPerformative(ACLMessage.INFORM); + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + CFPSchedulingSequenceDone newSchedulingSequenceDone = new CFPSchedulingSequenceDone(); + newSchedulingSequenceDone.setAgentName(myAgent.getLocalName()); + newSchedulingSequenceDone.setTuName(tuName); + cel.add(newSchedulingSequenceDone); + cm.fillContent(reply, cel); + myAgent.send(reply); + step = 99; + } + else{ + //Sending a FAILURE-Answer + ACLMessage reply = msg.createReply(); + reply.setPerformative(ACLMessage.FAILURE); + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + CFPSchedulingSequenceFailure newSchedulingSequenceFailure = new CFPSchedulingSequenceFailure(); + newSchedulingSequenceFailure.setAgentName(myAgent.getLocalName()); + newSchedulingSequenceFailure.setTuName(tuName); + cel.add(newSchedulingSequenceFailure); + cm.fillContent(reply, cel); + myAgent.send(reply); + step = 99; + } + } + catch (CodecException ce) { + ce.printStackTrace(); + } + catch (OntologyException oe) { + oe.printStackTrace(); + } + break; + case 99: + reset(); + step = 100; + break; + default: + step = 99; + } + } + + public boolean done(){ + return step == 100; + } + } + + //********************** 2. TU Control Sequence ********************** + private class ControlSequencePerformer extends Behaviour { + private static final long serialVersionUID = 1L; + private int step = 0; //step counter for state machine + private boolean newSetpointVersion = false; + private ACLMessage msg; + private String tuName; + private byte[] newLoadProfile; + private int newSetpoint; + + private ControlSequencePerformer(ACLMessage _msg, String _tuName, int _newSetpoint) { + msg = _msg; + tuName = _tuName; + newSetpoint = _newSetpoint; + newSetpointVersion = true; + } + + private ControlSequencePerformer(ACLMessage _msg, String _tuName, byte[] _newLoadProfile) { + msg = _msg; + tuName = _tuName; + newLoadProfile = _newLoadProfile; + } + + public void onStart(){ + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending newSetpoint to TU *******"); + } + + public void action(){ + switch(step){ + case 0: + //******* sending the new control information to the TU system ******* + if(newSetpointVersion) { + ConsumingRest_TU putInstance = new ConsumingRest_TU(); + InterfacePayloadNewSetpoint payload = new InterfacePayloadNewSetpoint(newSetpoint, tuName); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.SETPOINT, payload); + }else { + ConsumingRest_TU putInstanceOne = new ConsumingRest_TU(); + InterfacePayloadAgentReference payloadOne = new InterfacePayloadAgentReference(getAID().getLocalName(), tuName); + putInstanceOne.putNodeRed(Addresses.URL_NODERED, PutVariable.LOADPROFILEINFO, payloadOne); + ConsumingRest_TU putInstanceTwo = new ConsumingRest_TU(); + putInstanceTwo.putNodeRed(Addresses.URL_NODERED, PutVariable.LOADPROFILE, newLoadProfile); + } + step = 1; + break; + case 1: + if(TuVariables.requestDoneTrigger) { + TuVariables.requestDoneTrigger = false; + step = 99; + }else if(TuVariables.requestFailureTrigger) { + TuVariables.requestFailureTrigger = false; + System.out.println(this.getAgent().getAID().getLocalName()+"******* Setting newSetpoint failed *******"); + tuName=TuVariables.tuName; + //Sending a FAILURE-Answer + try { + ACLMessage reply = msg.createReply(); + reply.setPerformative(ACLMessage.FAILURE); + reply.setConversationId("tuControlSequenceFailed"); + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + TUControlSequenceFailure newTUControlSequenceFailure = new TUControlSequenceFailure(); + newTUControlSequenceFailure.setTuName(tuName); + newTUControlSequenceFailure.setAgentName(getAID().getLocalName()); + //newTUControlSequenceFailure.setTuControlSequenceFailed("tuControlSequenceFailed"); + cel.add(newTUControlSequenceFailure); + cm.fillContent(reply, cel); + myAgent.send(reply); + } catch (CodecException e) { + e.printStackTrace(); + } catch (OntologyException e) { + e.printStackTrace(); + } + step = 99; + } + break; + case 99: + reset(); + step = 100; + break; + default: + step = 99; + } + } + + + public boolean done(){ + return step == 100; + } + + } + + //********************** 3. TU Balancing Sequence ********************** + private class BalancingSequencePerformer extends Behaviour { + private static final long serialVersionUID = 1L; + private int step = 0; + private int updateRate = 0; + private String balancingStart; + private String balancingEnd; + private String tuName = "noNameSet"; + private Date balancingStartDate; + private Date balancingEndDate; + private ACLMessage msg; + private Timer timer = new Timer(); //timer need to start the balancing updating process + + public BalancingSequencePerformer(ACLMessage _msg) { + msg = _msg; + } + + public void onStart(){ + System.out.println(this.getAgent().getAID().getLocalName()+"******* BalancingRequestBehaviour started ********"); + try { + ContentElement ce; + Action _ac; + ce = getContentManager().extractContent(msg); + _ac = (Action) ce; + BalancingSequenceSubscribe _bss = (BalancingSequenceSubscribe)_ac.getAction(); + balancingStart = _bss.getBalancingStart(); + balancingEnd = _bss.getBalancingEnd(); + tuName = _bss.getTuName(); + updateRate = _bss.getUpdateRate(); + } catch (CodecException | OntologyException e) { + step = 99; + e.printStackTrace(); + } + try { + //setting the expiration time for the request of the VPP + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.GERMAN); + formatter.setTimeZone(TimeZone.getTimeZone("Europe/Berlin")); + //also setting the balancingEnd date for the proper was of ending this sequence + balancingEndDate = formatter.parse(balancingEnd); + balancingStartDate = formatter.parse(balancingStart); + } catch (ParseException e) { + step = 99; + e.printStackTrace(); + } + } + + public void action(){ + + + switch(step){ + case 0: + //******* sending the balancing request information to the TU system ******* + ConsumingRest_TU putInstance = new ConsumingRest_TU(); + InterfacePayloadBalancing payload = new InterfacePayloadBalancing(balancingStart, balancingEnd, tuName); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.BALANCING, payload); + step = 1; + break; + case 1: + //getting the current expiration time and date so the TU can send a refuse message if the deadline is done + Date dateNow = new Date(); + if(TuVariables.balancingAgreeTrigger && tuName.equals(TuVariables.balancingTuName)) { + TuVariables.balancingAgreeTrigger = false; + System.out.println(this.getAgent().getAID().getLocalName()+"******* Balancing Request accepted *******"); + //Sending an AGREE-Answer to the VPP + try { + ACLMessage reply = msg.createReply(); + reply.setPerformative(ACLMessage.AGREE); + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + BalancingSequenceAgree newBalancingSequenceAgree = new BalancingSequenceAgree(); + newBalancingSequenceAgree.setTuName(TuVariables.balancingTuName); + newBalancingSequenceAgree.setAgentName(getAID().getLocalName()); + cel.add(newBalancingSequenceAgree); + cm.fillContent(reply, cel); + myAgent.send(reply); + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + step = 99; + } + step = 2; + }else if(TuVariables.balancingRefuseTrigger && tuName.equals(TuVariables.balancingTuName) || dateNow.after(balancingEndDate)) { + TuVariables.balancingRefuseTrigger = false; + System.out.println(this.getAgent().getAID().getLocalName()+"******* Balancing Request Failed *******"); + //Sending a REFUSE-Answer to the VPP + try { + ACLMessage reply = msg.createReply(); + reply.setPerformative(ACLMessage.REFUSE); + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + BalancingSequenceRefuse newBalancingSequenceRefuse = new BalancingSequenceRefuse(); + newBalancingSequenceRefuse.setTuName(TuVariables.balancingTuName); + newBalancingSequenceRefuse.setAgentName(getAID().getLocalName()); + cel.add(newBalancingSequenceRefuse); + cm.fillContent(reply, cel); + myAgent.send(reply); + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + step = 99; + } + break; + case 2: + Date dateNow2 = new Date(); + //start a separate performer that replies every n second to the VPP + if (dateNow2.after(balancingStartDate)){ + step = 3; + timer.schedule(new balancingUpdatePerformer(msg.shallowClone()), 0, updateRate); + } + break; + + case 3: + if(TuVariables.balancingInformTrigger && tuName.equals(TuVariables.balancingTuName)) { + TuVariables.balancingInformTrigger = false; + }else if(TuVariables.balancingInformInstantTrigger && tuName.equals(TuVariables.balancingTuName)) { + TuVariables.balancingInformInstantTrigger = false; + try { + ACLMessage reply = msg.createReply(); + reply.setPerformative(ACLMessage.INFORM); + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + BalancingSequenceInform newBalancingSequenceInform = new BalancingSequenceInform(); + //giving the data to the newTUDataSet + TUDataSet newTUDataSet = new TUDataSet(TuVariables.feedIn, TuVariables.operatingPoint, + TuVariables.leadingOperatingPoint, TuVariables.currentValueFR, TuVariables.assignedPool, + TuVariables.status, TuVariables.frequency, TuVariables.aFRRsetpoint, TuVariables.aFRRsetpointEcho, + TuVariables.setpointFR, TuVariables.aFRRGradientPOS, TuVariables.aFRRGradientNEG, + TuVariables.capacityPOS, TuVariables.capacityNEG, TuVariables.holdingCapacityPOS, + TuVariables.holdingCapacityNEG, TuVariables.controlBandPOS, TuVariables.controlBandNEG); + newBalancingSequenceInform.setTUDataSet(newTUDataSet); + newBalancingSequenceInform.setTuName(TuVariables.balancingTuName); + newBalancingSequenceInform.setAgentName(getAID().getLocalName()); + cel.add(newBalancingSequenceInform); + cm.fillContent(reply, cel); + myAgent.send(reply); + System.out.println(getAgent().getAID().getLocalName()+"******* Spontaneous Inform sent *******"); + TuVariables.resetBalancing(); + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + } + else if(TuVariables.balancingFailureTrigger && tuName.equals(TuVariables.balancingTuName)) { + TuVariables.balancingFailureTrigger = false; + System.out.println(this.getAgent().getAID().getLocalName()+"******* Balancing Request Failed *******"); + //Sending a FAILURE-Answer + try { + ACLMessage reply = msg.createReply(); + reply.setPerformative(ACLMessage.FAILURE); + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + BalancingSequenceFailure newBalancingSequenceFailure = new BalancingSequenceFailure(); + newBalancingSequenceFailure.setTuName(TuVariables.balancingTuName); + newBalancingSequenceFailure.setAgentName(getAID().getLocalName()); + cel.add(newBalancingSequenceFailure); + cm.fillContent(reply, cel); + myAgent.send(reply); + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + step = 99; + } + //End the balancing after the balancing end Time + Date _dateNow = new Date(); + if (_dateNow.after(balancingEndDate)){ + step = 99; + } + break; + case 99: + TuVariables.resetScheduling(); + reset(); + timer.cancel(); //stopping the updating behaviour + step = 100; + break; + default: + step = 99; + } + } + public boolean done(){ + return step == 100; + } + + private class balancingUpdatePerformer extends TimerTask { + private ACLMessage msg; + + balancingUpdatePerformer(ACLMessage _msg){ + msg = _msg; + } + public void run() { + //Sending a INFORM-Answer + try { + ACLMessage reply = msg.createReply(); + reply.setPerformative(ACLMessage.INFORM); + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + BalancingSequenceInform newBalancingSequenceInform = new BalancingSequenceInform(); + //giving the data to the newTUDataSet + if(tuName.equals(TuVariables.balancingTuName)) { + TUDataSet newTUDataSet = new TUDataSet(TuVariables.feedIn, TuVariables.operatingPoint, + TuVariables.leadingOperatingPoint, TuVariables.currentValueFR, TuVariables.assignedPool, + TuVariables.status, TuVariables.frequency, TuVariables.aFRRsetpoint, TuVariables.aFRRsetpointEcho, + TuVariables.setpointFR, TuVariables.aFRRGradientPOS, TuVariables.aFRRGradientNEG, + TuVariables.capacityPOS, TuVariables.capacityNEG, TuVariables.holdingCapacityPOS, + TuVariables.holdingCapacityNEG, TuVariables.controlBandPOS, TuVariables.controlBandNEG); + newBalancingSequenceInform.setTUDataSet(newTUDataSet); + newBalancingSequenceInform.setTuName(TuVariables.balancingTuName); + newBalancingSequenceInform.setAgentName(getAID().getLocalName()); + cel.add(newBalancingSequenceInform); + cm.fillContent(reply, cel); + myAgent.send(reply); + System.out.println(getAgent().getAID().getLocalName()+"******* Inform sent *******"); + TuVariables.resetBalancing(); + } + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + } + } + } + + //********************** 4. Accounting Sequence ********************** + private class AccountingSequencePerformer extends Behaviour { + private static final long serialVersionUID = 1L; + private int step = 0; + private String tuName = "noNameSet"; + private String agentName = "noNameSet"; + private String referenceID = "noIDSet"; + private String conversationID; + private byte[] energyConsumptionProfile; + private ArrayList vppAgents = new ArrayList(); + + AccountingSequencePerformer(byte[] _energyConsumptionProfile, String _tuName, String _referenceID){ + energyConsumptionProfile = _energyConsumptionProfile; + tuName = _tuName; + referenceID = _referenceID; + } + + public void onStart(){ + System.out.println(this.getAgent().getAID().getLocalName()+"******* AccountingSequenceBehaviour started ********"); + agentName = getAID().getLocalName(); + Date dateNow = new Date(); + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.GERMAN); + formatter.format(dateNow); + conversationID = "accountingInform"; + } + + public void action(){ + switch(step){ + // sending the energy consumption profile to the VPP + case 0: + System.out.println(this.getAgent().getAID().getLocalName()+"******* AccountingSequencePerformer started ********"); + //searching for the complete AID of the referenced agent + DFAgentDescription sdSearchTemplate = new DFAgentDescription() ; + sdSearchTemplate.addLanguages(codec.getName()); + sdSearchTemplate.addOntologies(ontology.getName()); + ServiceDescription sd = new ServiceDescription(); + sd.setType("VPP"); + sdSearchTemplate.addServices(sd); + //searching the DF for the Agents that can provide the service + try { + DFAgentDescription[] result = DFService.search(myAgent, sdSearchTemplate); + for(int j = 0; j < result.length; j++){ + vppAgents.add(result[j].getName()); + } + } + catch (FIPAException fe){ + fe.printStackTrace(); + } + sdSearchTemplate.clearAllServices(); + if(vppAgents.size()>0) { + step = 1; + }else { + System.out.println("No Agent can be found under that name"); + step = 99; + } + break; + case 1: + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending out INFORM (energy consumption profiles) to VPP *******"); + ACLMessage msg = new ACLMessage(ACLMessage.INFORM); + msg.setOntology(ontology.getName()); + msg.setLanguage(codec.getName()); + msg.setConversationId(conversationID); + AID receiver = new AID(); + for(int j = 0; j < vppAgents.size(); j++){ + receiver = vppAgents.get(j); + } + msg.addReceiver(receiver); + try { + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + AccountingSequenceInform newAccountingSequenceInform = new AccountingSequenceInform(); + newAccountingSequenceInform.setAgentName(agentName); + newAccountingSequenceInform.setTuName(tuName); + newAccountingSequenceInform.setEnergyConsumptionProfile(energyConsumptionProfile); + cel.add(newAccountingSequenceInform); + cm.fillContent(msg, cel); + myAgent.send(msg); + msg.reset(); + step = 2; + } catch (CodecException | OntologyException e){ + e.printStackTrace(); + step = 99; + } + break; + case 2: + MessageTemplate mt = MessageTemplate.and( + MessageTemplate.MatchOntology(ontology.getName()),MessageTemplate.and( + MessageTemplate.MatchConversationId(conversationID), + MessageTemplate.MatchLanguage(codec.getName()))); + msg = receive(mt); //returns the first message of the message queue with the corresponding template + if (msg != null){ + ContentElement ce = null; + if (msg.getPerformative() == ACLMessage.INFORM) { + System.out.println(this.getAgent().getAID().getLocalName()+"******* INFORM Message Received *******"); + try { + ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + if(_pc instanceof AccountingSequenceInformReceived){ + //********** Inform the EMS-System about the results********** + AccountingSequenceInformReceived _asir = (AccountingSequenceInformReceived)_pc; + InterfacePayloadAgentReference payload = new InterfacePayloadAgentReference(referenceID, getAID().getLocalName(),_asir.getTuName() ); + ConsumingRest_TU putInstance = new ConsumingRest_TU(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.ACCOUNTINGECPRECEIVED, payload); + step = 99; + } + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + }else if(msg.getPerformative() == ACLMessage.FAILURE) { + step = 99; + }else { + step = 99; + } + }else { + block(); + } + break; + case 99: + reset(); + step = 100; + break; + default: + step = 99; + } + } + + public boolean done(){ + return step == 100; + } + } + + //********************** 5.LoadTimeWindows Request Sequence ********************** + private class LoadTimeWindowsRequestPerformer extends OneShotBehaviour{ + private static final long serialVersionUID = 1L; + private String loadTimeWindowsReference; + + LoadTimeWindowsRequestPerformer( String _loadTimeWindowsReference){ + loadTimeWindowsReference = _loadTimeWindowsReference; + } + + public void action() { + ArrayList vppAgents = new ArrayList(); + System.out.println(this.getAgent().getAID().getLocalName()+"******* loadTimeWindowsSequence started ********"); + //searching for the complete AID of the referenced agent + DFAgentDescription sdSearchTemplate = new DFAgentDescription() ; + sdSearchTemplate.addLanguages(codec.getName()); + sdSearchTemplate.addOntologies(ontology.getName()); + ServiceDescription sd = new ServiceDescription(); + sd.setType("VPP"); + sdSearchTemplate.addServices(sd); + //searching the DF for the Agents that can provide the service + try { + DFAgentDescription[] result = DFService.search(myAgent, sdSearchTemplate); + for(int j = 0; j < result.length; j++){ + vppAgents.add(result[j].getName()); + } + } + catch (FIPAException fe){ + fe.printStackTrace(); + } + sdSearchTemplate.clearAllServices(); + if(vppAgents.size()>0) { + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending out Request (load time windows) to VPP *******"); + ACLMessage msg = new ACLMessage(ACLMessage.REQUEST); + msg.setOntology(ontology.getName()); + msg.setLanguage(codec.getName()); + msg.setConversationId("loadTimeWindows"); + AID receiver = new AID(); + for(int j = 0; j < vppAgents.size(); j++){ + receiver = vppAgents.get(j); + } + msg.addReceiver(receiver); + try { + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + LoadTimeWindowsShareRequest newLoadTimeWindowsShareRequest = new LoadTimeWindowsShareRequest(); + newLoadTimeWindowsShareRequest.setLoadTimeWindowsReference(loadTimeWindowsReference); + Action act = new Action(); + act.setAction(newLoadTimeWindowsShareRequest); + act.setActor(new AID("*", AID.ISGUID)); + cel.add(act); + cm.fillContent(msg, cel); + myAgent.send(msg); + msg.reset(); + } catch (CodecException | OntologyException e){ + e.printStackTrace(); + } + }else { + System.out.println("No VPP Agent can be found"); + } + } + } + + //********************** 6.A Frequency Relay Enable Sequence ********************** + private class FREnablePerformer extends Behaviour { + private static final long serialVersionUID = 1L; + private int step = 0; //step counter for state machine + private ACLMessage msg; + private String tuName = "NoNameSet"; + + private FREnablePerformer(ACLMessage _msg, String _tuName) { + msg = _msg; + tuName = _tuName; + } + + + public void onStart(){ + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending Enable Frequency Relay to TU *******"); + } + + public void action(){ + + switch(step){ + case 0: + //******* sending the request to the TU system ******* + ConsumingRest_TU putInstance = new ConsumingRest_TU(); + InterfacePayloadAgentReference payload = new InterfacePayloadAgentReference(tuName); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.FRENABLEREQUEST, payload); + step = 1; + break; + case 1: + try { + if(TuVariables.freqRelayEnableInformTrigger) { + TuVariables.freqRelayEnableInformTrigger = false; + tuName=TuVariables.freqRelayEnableTUName; + ACLMessage reply = msg.createReply(); + reply.setPerformative(ACLMessage.INFORM); + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + FreqRelayEnableInform newFreqRelayEnableInform = new FreqRelayEnableInform(); + newFreqRelayEnableInform.setTuName(tuName); + newFreqRelayEnableInform.setAgentName(getAID().getLocalName()); + cel.add(newFreqRelayEnableInform); + cm.fillContent(reply, cel); + myAgent.send(reply); + step = 99; + }else if(TuVariables.freqRelayEnableFailureTrigger) { + TuVariables.freqRelayEnableFailureTrigger = false; + tuName=TuVariables.freqRelayEnableTUName; + ACLMessage reply = msg.createReply(); + reply.setPerformative(ACLMessage.FAILURE); + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + FreqRelayEnableFailure newFreqRelayEnableFailure = new FreqRelayEnableFailure(); + newFreqRelayEnableFailure.setTuName(tuName); + newFreqRelayEnableFailure.setAgentName(getAID().getLocalName()); + cel.add(newFreqRelayEnableFailure); + cm.fillContent(reply, cel); + myAgent.send(reply); + step = 99; + } + } + catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + break; + case 99: + reset(); + step = 100; + break; + default: + step = 99; + } + } + public boolean done(){ + return step == 100; + } + } + + //********************** 6.B Frequency Relay Block Sequence ********************** + private class FRBlockPerformer extends Behaviour { + private static final long serialVersionUID = 1L; + private int step = 0; + private ACLMessage msg; + private String tuName = "NoNameSet"; + + + private FRBlockPerformer(ACLMessage _msg, String _tuName) { + msg = _msg; + tuName = _tuName; + } + + public void onStart(){ + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending Block Frequency Relay to TU *******"); + } + + public void action(){ + switch(step){ + case 0: + //******* sending the request to the TU system ******* + ConsumingRest_TU putInstance = new ConsumingRest_TU(); + InterfacePayloadAgentReference payload = new InterfacePayloadAgentReference(tuName); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.FRBLOCKREQUEST, payload); + step = 1; + break; + case 1: + try { + if(TuVariables.freqRelayDisableInformTrigger) { + TuVariables.freqRelayDisableInformTrigger = false; + tuName=TuVariables.freqRelayDisableTUName; + ACLMessage reply = msg.createReply(); + reply.setPerformative(ACLMessage.INFORM); + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + FreqRelayBlockInform newFreqRelayBlockInform = new FreqRelayBlockInform(); + newFreqRelayBlockInform.setTuName(tuName); + newFreqRelayBlockInform.setAgentName(getAID().getLocalName()); + cel.add(newFreqRelayBlockInform); + cm.fillContent(reply, cel); + myAgent.send(reply); + step = 99; + }else if(TuVariables.freqRelayDisableFailureTrigger) { + TuVariables.freqRelayDisableFailureTrigger = false; + tuName=TuVariables.freqRelayDisableTUName; + ACLMessage reply = msg.createReply(); + reply.setPerformative(ACLMessage.FAILURE); + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + FreqRelayBlockFailure newFreqRelayBlockFailure = new FreqRelayBlockFailure(); + newFreqRelayBlockFailure.setTuName(tuName); + newFreqRelayBlockFailure.setAgentName(getAID().getLocalName()); + cel.add(newFreqRelayBlockFailure); + cm.fillContent(reply, cel); + myAgent.send(reply); + step = 99; + } + } + catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + break; + case 99: + reset(); + step = 100; + break; + default: + step = 99; + } + } + + public boolean done(){ + return step == 100; + } + } + + //********************** 6.C Frequency Relay Triggered Sequence ********************** + private class FRTriggeredPerformer extends Behaviour { + private static final long serialVersionUID = 1L; + private int step = 0; + private String tuName = "noNameSet"; + private String agentName = "noNameSet"; + private String conversationID = "frequencyRelayTriggered"; + private ArrayList vppAgents = new ArrayList(); + + FRTriggeredPerformer(String _tuName){ + tuName = _tuName; + } + + public void onStart(){ + System.out.println(this.getAgent().getAID().getLocalName()+"******* Frequency Relay Triggered Behaviour started ********"); + agentName = getAID().getLocalName(); + } + + public void action(){ + switch(step){ + case 0: + //searching for the complete AID of the referenced agent + DFAgentDescription sdSearchTemplate = new DFAgentDescription() ; + sdSearchTemplate.addLanguages(codec.getName()); + sdSearchTemplate.addOntologies(ontology.getName()); + ServiceDescription sd = new ServiceDescription(); + sd.setType("VPP"); + sdSearchTemplate.addServices(sd); + //searching the DF for the Agents that can provide the service + try { + DFAgentDescription[] result = DFService.search(myAgent, sdSearchTemplate); + for(int j = 0; j < result.length; j++){ + vppAgents.add(result[j].getName()); + } + } + catch (FIPAException fe){ + fe.printStackTrace(); + } + sdSearchTemplate.clearAllServices(); + if(vppAgents.size()>0) { + step = 1; + }else { + System.out.println("No Agent can be found under that name"); + step = 99; + } + break; + case 1: + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending out INFORM (Frequency Relay Triggered) to VPP *******"); + ACLMessage msg = new ACLMessage(ACLMessage.INFORM); + msg.setOntology(ontology.getName()); + msg.setLanguage(codec.getName()); + msg.setConversationId(conversationID); + AID receiver = new AID(); + for(int j = 0; j < vppAgents.size(); j++){ + receiver = vppAgents.get(j); + } + msg.addReceiver(receiver); + try { + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + FreqRelayStatusInform newFreqRelayStatusInform = new FreqRelayStatusInform(); + newFreqRelayStatusInform.setAgentName(agentName); + newFreqRelayStatusInform.setTuName(tuName); + cel.add(newFreqRelayStatusInform); + cm.fillContent(msg, cel); + myAgent.send(msg); + msg.reset(); + step = 2; + } catch (CodecException | OntologyException e){ + e.printStackTrace(); + step = 99; + } + break; + case 2: + MessageTemplate mt = MessageTemplate.and( + MessageTemplate.MatchOntology(ontology.getName()),MessageTemplate.and( + MessageTemplate.MatchConversationId(conversationID), + MessageTemplate.MatchLanguage(codec.getName()))); + msg = receive(mt); //returns the first message of the message queue with the corresponding template + if (msg != null){ + ContentElement ce = null; + if (msg.getPerformative() == ACLMessage.INFORM) { + System.out.println(this.getAgent().getAID().getLocalName()+"******* INFORM Message Received *******"); + try { + ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + if(_pc instanceof FreqRelayStatusConfirm){ + //********** Inform the EMS-System about the results********** + FreqRelayStatusConfirm _frsc = (FreqRelayStatusConfirm)_pc; + InterfacePayloadAgentReference payload = new InterfacePayloadAgentReference(_frsc.getTuName()); + ConsumingRest_TU putInstance = new ConsumingRest_TU(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.FRCONFIRM, payload); + step = 99; + } + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + }else { + step = 99; + } + }else { + block(); + } + break; + case 99: + reset(); + step = 100; + break; + default: + step = 99; + } + } + + public boolean done(){ + return step == 100; + } + } + + //********************** 7.A Send CancelOperation Sequence ********************** + private class SendCancelOperationPerformer extends Behaviour { + private static final long serialVersionUID = 1L; + private int step = 0; + private String tuName = "noNameSet"; + private String agentName = "noNameSet"; + private String operationReference = "noIDSet"; + private String conversationID; + private ArrayList vppAgents = new ArrayList(); + + SendCancelOperationPerformer(String _tuName, String _operationReference){ + tuName = _tuName; + operationReference = _operationReference; + } + + public void onStart(){ + System.out.println(this.getAgent().getAID().getLocalName()+"******* OperationCancelBehaviour started ********"); + agentName = getAID().getLocalName(); + conversationID = "cancelOperation"; + } + + public void action(){ + switch(step){ + case 0: + DFAgentDescription sdSearchTemplate = new DFAgentDescription() ; + sdSearchTemplate.addLanguages(codec.getName()); + sdSearchTemplate.addOntologies(ontology.getName()); + ServiceDescription sd = new ServiceDescription(); + sd.setType("VPP"); + sdSearchTemplate.addServices(sd); + //searching the DF for the Agents that can provide the service + try { + DFAgentDescription[] result = DFService.search(myAgent, sdSearchTemplate); + for(int j = 0; j < result.length; j++){ + vppAgents.add(result[j].getName()); + } + } + catch (FIPAException fe){ + fe.printStackTrace(); + } + sdSearchTemplate.clearAllServices(); + if(vppAgents.size()>0) { + step = 1; + }else { + System.out.println("No Agent can be found under that name"); + step = 99; + } + break; + case 1: + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending out CANCEL (cancel operation) to VPP *******"); + + ACLMessage msg = new ACLMessage(ACLMessage.CANCEL); + msg.setOntology(ontology.getName()); + msg.setLanguage(codec.getName()); + msg.setConversationId(conversationID); + AID receiver = new AID(); + for(int j = 0; j < vppAgents.size(); j++){ + receiver = vppAgents.get(j); + } + msg.addReceiver(receiver); + try { + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + CancelOperationCancel newCancelOperationInform = new CancelOperationCancel(); + newCancelOperationInform.setAgentName(agentName); + newCancelOperationInform.setTuName(tuName); + newCancelOperationInform.setOperationReference(operationReference); + cel.add(newCancelOperationInform); + cm.fillContent(msg, cel); + myAgent.send(msg); + msg.reset(); + step = 2; + } catch (CodecException | OntologyException e){ + e.printStackTrace(); + step = 99; + } + break; + case 2: + MessageTemplate mt = MessageTemplate.and( + MessageTemplate.MatchOntology(ontology.getName()),MessageTemplate.and( + MessageTemplate.MatchConversationId(conversationID), + MessageTemplate.MatchLanguage(codec.getName()))); + msg = receive(mt); //returns the first message of the message queue with the corresponding template + if (msg != null){ + ContentElement ce = null; + if (msg.getPerformative() == ACLMessage.INFORM) { + System.out.println(this.getAgent().getAID().getLocalName()+"******* INFORM Message Received *******"); + try { + ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + if(_pc instanceof CancelOperationConfirm){ + //********** Inform the Planning-System about the results********** + CancelOperationConfirm _asir = (CancelOperationConfirm)_pc; + InterfacePayloadAgentReference payload = new InterfacePayloadAgentReference(_asir.getOperationReference(), getAID().getLocalName(),_asir.getTuName() ); + ConsumingRest_TU putInstance = new ConsumingRest_TU(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.CANCELOPERATIONCONFIRM, payload); + step = 99; + } + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + }else if(msg.getPerformative() == ACLMessage.FAILURE) { + System.out.println(this.getAgent().getAID().getLocalName()+"******* FAILURE Message Received *******"); + try { + ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + if(_pc instanceof CancelOperationFailure){ + //********** Inform the Planning-System about the results********** + CancelOperationFailure _asir = (CancelOperationFailure)_pc; + InterfacePayloadAgentReference payload = new InterfacePayloadAgentReference(_asir.getOperationReference(), getAID().getLocalName(),_asir.getTuName() ); + ConsumingRest_TU putInstance = new ConsumingRest_TU(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.CANCELOPERATIONFAILED, payload); + step = 99; + } + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + }else { + step = 99; + } + }else { + block(); + } + break; + case 99: + reset(); + step = 100; + break; + default: + step = 99; + } + } + + public boolean done(){ + return step == 100; + } + + } + + //********************** 7.B Receive CancelOperation Sequence ********************** + private class ReceiveCancelOperationPerformer extends Behaviour{ + private static final long serialVersionUID = 1L; + private int step = 0; + private String tuName = "noNameSet"; + private String agentName = "noNameSet"; + private String operationReference = "noReferenceSet"; + private ACLMessage msg; + + ReceiveCancelOperationPerformer(ACLMessage _msg) { + msg = _msg; + } + + public void onStart(){ + try { + ContentElement ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + CancelOperationCancel _coi = (CancelOperationCancel)_pc; + tuName = _coi.getTuName(); + agentName = _coi.getAgentName(); + operationReference = _coi.getOperationReference(); + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + } + + public void action() { + switch (step) { + case 0: + ConsumingRest_TU putInstance = new ConsumingRest_TU(); + InterfacePayloadAgentReference payload = new InterfacePayloadAgentReference(operationReference, agentName,tuName); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.CANCELOPERATION, payload); + step = 1; + break; + case 1: + if(TuVariables.receiveCancelOperationTrigger && operationReference.equals(TuVariables.receiveCancelOperationReference)) { + TuVariables.receiveCancelOperationTrigger = false; + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending out new INFORM to TU: " + tuName +" *******"); + try { + ACLMessage reply = msg.createReply(); + reply.setPerformative(ACLMessage.INFORM); + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + CancelOperationConfirm newCancelOperationInformReceived = new CancelOperationConfirm(); + newCancelOperationInformReceived.setTuName(tuName); + newCancelOperationInformReceived.setOperationReference(operationReference); + cel.add(newCancelOperationInformReceived); + cm.fillContent(reply, cel); + myAgent.send(reply); + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + step = 99; + }else if(TuVariables.receiveCancelOperationTriggerFail && operationReference.equals(TuVariables.receiveCancelOperationReference)) { + TuVariables.receiveCancelOperationTriggerFail = false; + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending out new INFORM to TU: " + tuName +" *******"); + try { + ACLMessage reply = msg.createReply(); + reply.setPerformative(ACLMessage.FAILURE); + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + CancelOperationFailure newCancelOperationInformReceived = new CancelOperationFailure(); + newCancelOperationInformReceived.setTuName(tuName); + newCancelOperationInformReceived.setOperationReference(operationReference); + cel.add(newCancelOperationInformReceived); + cm.fillContent(reply, cel); + myAgent.send(reply); + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + step = 99; + } + break; + case 99: //final case, here reset() must be called. A reset() in done() would result in a reset() call every cycle, because done() gets called every cycle + reset(); //any Behaviour object that has been executed once, must be reset by calling its reset() method before it can be executed again. + step = 100; + break; + default: + step = 99; + } + } + + public boolean done() { + return step == 100; + } + } + + //********************** 8.A RequestInfos Sequence ********************** + private class RequestInfoPerformer extends Behaviour { + private static final long serialVersionUID = 1L; + private int step = 0; + private String tuName = "VPPAgent"; + private String conversationID; + private byte[] infoSet; + private ArrayList vppAgents = new ArrayList(); + private Date expireDate; + + public void onStart(){ + System.out.println(this.getAgent().getAID().getLocalName()+"******* RequestInfosBehaviour started ********"); + Date dateStart = new Date(); + long t; + t = dateStart.getTime(); + expireDate=new Date(t + (5 * 60000)); //5min added + conversationID = "requestInfoOperation"; + } + + public void action(){ + switch(step){ + case 0: + DFAgentDescription sdSearchTemplate = new DFAgentDescription() ; + sdSearchTemplate.addLanguages(codec.getName()); + sdSearchTemplate.addOntologies(ontology.getName()); + ServiceDescription sd = new ServiceDescription(); + sd.setType("VPP"); + sdSearchTemplate.addServices(sd); + //searching the DF for the Agents that can provide the service + try { + DFAgentDescription[] result = DFService.search(myAgent, sdSearchTemplate); + for(int j = 0; j < result.length; j++){ + vppAgents.add(result[j].getName()); + } + } + catch (FIPAException fe){ + fe.printStackTrace(); + } + sdSearchTemplate.clearAllServices(); + if(vppAgents.size()>0) { + step = 1; + }else { + System.out.println("No Agent can be found under that name"); + step = 99; + } + break; + case 1: + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending out REQUEST (Request Infos) to VPP *******"); + + ACLMessage msg = new ACLMessage(ACLMessage.REQUEST); + msg.setOntology(ontology.getName()); + msg.setLanguage(codec.getName()); + msg.setConversationId(conversationID); + AID receiver = new AID(); + for(int j = 0; j < vppAgents.size(); j++){ + receiver = vppAgents.get(j); + } + msg.addReceiver(receiver); + try { + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + RequestInfoRequest newRequestInfoRequest = new RequestInfoRequest(); + newRequestInfoRequest.setTuName(tuName); + newRequestInfoRequest.setTuName(getAID().getLocalName()); + Action act = new Action(); + act.setAction(newRequestInfoRequest); //Adding the Action the Agent has to perform + act.setActor(new AID("*", AID.ISGUID)); //Adding an dummy Agent, because the actor field is mandatory (source: http://jade.tilab.com/pipermail/jade-develop/2010q4/016200.html) + cel.add(act); + cm.fillContent(msg, cel); + myAgent.send(msg); + msg.reset(); + step = 2; + } catch (CodecException | OntologyException e){ + e.printStackTrace(); + step = 99; + } + break; + case 2: + MessageTemplate mt = MessageTemplate.and( + MessageTemplate.MatchOntology(ontology.getName()),MessageTemplate.and( + MessageTemplate.MatchConversationId(conversationID), + MessageTemplate.MatchLanguage(codec.getName()))); + msg = receive(mt); //returns the first message of the message queue with the corresponding template + if (msg != null){ + ContentElement ce = null; + if (msg.getPerformative() == ACLMessage.INFORM) { + System.out.println(this.getAgent().getAID().getLocalName()+"******* INFORM Message Received *******"); + try { + ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + if(_pc instanceof RequestInfoInform){ + //********** Inform the VPP-System about the results********** + RequestInfoInform _rii = (RequestInfoInform)_pc; + ConsumingRest_VPP putInstance = new ConsumingRest_VPP(); + infoSet = _rii.getInfoSet(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.REQUESTINFOINFORM, infoSet); + step = 99; + } + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + }else if(msg.getPerformative() == ACLMessage.FAILURE) { + step = 99; + }else{ + step = 99; + } + }else { + Date dateNow = new Date(); + if(dateNow.after(expireDate)) { + step = 99; + } + } + break; + case 99: + reset(); + step = 100; + break; + default: + step = 99; + } + } + + public boolean done() { + return step == 100; + } + } + + //********************** 8.B RequestInfosResponse Sequence ********************** + private class RequestInfoResponsePerformer extends OneShotBehaviour{ + private static final long serialVersionUID = 1L; + private String tuName = "noNameSet"; + private byte[] infoSet; + private ACLMessage msg; + + RequestInfoResponsePerformer(ACLMessage _msg) { + msg = _msg; + } + + public void onStart(){ + try { + ContentElement ce = getContentManager().extractContent(msg); + Action _ac = (Action) ce; + if(_ac.getAction() instanceof RequestInfoRequest){ + RequestInfoRequest _rir = (RequestInfoRequest)_ac.getAction(); + tuName = _rir.getTuName(); + } + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + } + + public void action() { + ConsumingRest_TU getInstance = new ConsumingRest_TU(); + infoSet = getInstance.getNodeRed(Addresses.URL_NODERED, PutVariable.REQUESTINFO); + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending out new RequestInfo *******"); + try { + ACLMessage reply = msg.createReply(); + reply.setPerformative(ACLMessage.INFORM); + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + RequestInfoInform newRequestInfoInform = new RequestInfoInform(); + newRequestInfoInform.setTuName(tuName); + newRequestInfoInform.setAgentName(getAID().getLocalName()); + newRequestInfoInform.setInfoSet(infoSet); + cel.add(newRequestInfoInform); + cm.fillContent(reply, cel); + myAgent.send(reply); + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + } + + } + +} + + \ No newline at end of file diff --git a/JADE_VPP/src/main/java/mas/JADE_VPP/TuVariables.java b/JADE_VPP/src/main/java/mas/JADE_VPP/TuVariables.java index 100b8e2..24a2a8f 100644 --- a/JADE_VPP/src/main/java/mas/JADE_VPP/TuVariables.java +++ b/JADE_VPP/src/main/java/mas/JADE_VPP/TuVariables.java @@ -28,6 +28,7 @@ public class TuVariables { public static boolean balancingRefuseTrigger = false; public static boolean balancingAgreeTrigger = false; public static boolean balancingInformTrigger = false; + public static boolean balancingInformInstantTrigger = false; public static boolean balancingFailureTrigger = false; public static String balancingTuName = "noName"; public static int feedIn; //Einspeisung diff --git a/JADE_VPP/src/main/java/mas/JADE_VPP/VPP_Agent.java b/JADE_VPP/src/main/java/mas/JADE_VPP/VPP_Agent.java new file mode 100644 index 0000000..ae646fb --- /dev/null +++ b/JADE_VPP/src/main/java/mas/JADE_VPP/VPP_Agent.java @@ -0,0 +1,2087 @@ +package mas.JADE_VPP; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.TimeZone; +import java.util.TreeMap; +import jade.content.ContentElement; +import jade.content.ContentElementList; +import jade.content.ContentManager; +import jade.content.Predicate; +import jade.content.lang.Codec; +import jade.content.lang.Codec.CodecException; +import jade.content.lang.sl.SLCodec; +import jade.content.onto.Ontology; +import jade.content.onto.OntologyException; +import jade.content.onto.basic.Action; +import jade.core.AID; +import jade.core.Agent; +import jade.core.behaviours.Behaviour; +import jade.core.behaviours.CyclicBehaviour; +import jade.core.behaviours.OneShotBehaviour; +import jade.core.behaviours.ThreadedBehaviourFactory; +import jade.domain.DFService; +import jade.domain.FIPAException; +import jade.domain.FIPAAgentManagement.DFAgentDescription; +import jade.domain.FIPAAgentManagement.ServiceDescription; +import jade.domain.introspection.BornAgent; +import jade.domain.introspection.DeadAgent; +import jade.domain.introspection.Event; +import jade.domain.introspection.IntrospectionVocabulary; +import jade.domain.introspection.AMSSubscriber; +import jade.lang.acl.ACLMessage; +import jade.lang.acl.MessageTemplate; +import mas.JADE_VPP.ontology.*; + +public class VPP_Agent extends Agent{ + private static final long serialVersionUID = 1L; + private static long idCounter = 0; //ID Counter for the messages (64 bit from: -9 223 372 036 854 775 808 to: +9 223 372 036 854 775 807) + private ServiceDescription sd; + private Codec codec = new SLCodec(); + private Ontology ontology = VPP_DR_Ontology.getInstance(); + private HashMap idPairsScheduling = new HashMap(); + private HashMap idPairsBalancing = new HashMap(); + private ThreadedBehaviourFactory tbf = new ThreadedBehaviourFactory(); //tbf is needed for extra thread generation, which is needed to prevent a cpu usage of 100% + + //*********** Agent startup *********** + protected void setup() { + //Printout a Welcome Message + System.out.println("********* VPP-Agent online: " + getAID().getLocalName()+ " *********"); + //register agent with its content manager to be able to use the ontology and content language + getContentManager().registerLanguage(codec); + getContentManager().registerOntology(ontology); + //setting up and register / publish own service + DFAgentDescription dfd = new DFAgentDescription(); + dfd.setName(getAID()); + dfd.addLanguages(codec.getName()); + dfd.addOntologies(ontology.getName()); + sd = new ServiceDescription(); + sd.setName(getAID().getLocalName()); + sd.setType("VPP"); + dfd.addServices(sd); + try{ + DFService.register(this, dfd); + } + catch(FIPAException fe){ + fe.printStackTrace(); + } + //receiving arguments from the Startup.java +// Object[] args = getArguments(); +// if (args != null && args.length > 0){ +// int randomNum = ThreadLocalRandom.current().nextInt(0, 13 + 1); +// requestedEnergy = (Integer) args[randomNum]; +// System.out.println("Requested Energy is "+requestedEnergy); +// } +// else{ +// System.out.println("VPP Agent shutting down"); +// doDelete(); +// } + + //start the agent behaviours + addBehaviour(new VPPManager()); + addBehaviour(tbf.wrap(new VPPInputListener())); //tbf.wrap starts an extra task for the behaviour added (so this task can go into wait later on) + //subscribe to AMS Platform Events + addBehaviour(new myAMSSubscriber()); + + } + + //*********** Agent shutdown *********** + protected void takdeDown(){ + System.out.println("VPP Agent " +getAID().getName() +"terminating."); + tbf.interrupt(); + try { + DFService.deregister(this); + } + catch (FIPAException fe) { + fe.printStackTrace(); + } + } + + //********************** 0. AMS Subscriber ********************** + //informs the VPP about new agents and agents that are leaving the platform + private class myAMSSubscriber extends AMSSubscriber{ + private static final long serialVersionUID = 1L; + @SuppressWarnings({ "unchecked", "rawtypes" }) + protected void installHandlers(Map handlers) { + //creating event handler for born agents + EventHandler creationsHandler = new EventHandler() { + private static final long serialVersionUID = 1L; + public void handle(Event ev) { + BornAgent ba = (BornAgent) ev; + System.out.println("Born agent "+ba.getAgent().getName()); + ConsumingRest_VPP putInstance = new ConsumingRest_VPP(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.AGENTBORN,ba.getAgent().getName()); + } + }; + handlers.put(IntrospectionVocabulary.BORNAGENT, creationsHandler); + //creating event handler for dead agents + EventHandler terminationsHandler = new EventHandler() { + private static final long serialVersionUID = 1L; + public void handle(Event ev) { + DeadAgent da = (DeadAgent) ev; + System.out.println("Dead agent "+da.getAgent().getName()); + ConsumingRest_VPP putInstance = new ConsumingRest_VPP(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.AGENTDEAD,da.getAgent().getName()); + } + }; + handlers.put(IntrospectionVocabulary.DEADAGENT, terminationsHandler); + } + } + + //********************** 0. VPP Manager (Main behaviour) ********************** + //Manages incoming agent messages + private class VPPManager extends CyclicBehaviour{ + private static final long serialVersionUID = 1L; + public void action(){ + //******************* Collecting Messages from other Agents ************ + //******** Handling unknown messages: **************** + MessageTemplate mt = MessageTemplate.not(MessageTemplate.or(MessageTemplate.MatchOntology(ontology.getName()), + MessageTemplate.MatchOntology("JADE-Introspection"))); //filter for messages that dont use the VPP_DR_Ontology + ACLMessage msg = receive(mt); //returns the first message of the message queue with the corresponding template + if (msg != null){ //if a proper message can be found + //********** every Message that does not use the VPP_DR_Ontology can not be understood ******** + System.out.println(this.getAgent().getAID().getLocalName()+"******* Message Received that cannot be understood *******"); + System.out.println(msg.getConversationId()); + ACLMessage reply = msg.createReply(); + reply.setPerformative(ACLMessage.NOT_UNDERSTOOD); + myAgent.send(reply); + } + //******** Handling messages of the TU control sequence ************ + mt = MessageTemplate.and( + MessageTemplate.MatchOntology(ontology.getName()),MessageTemplate.and( + MessageTemplate.MatchConversationId("tuControlSequenceFailed"),MessageTemplate.and( + MessageTemplate.MatchLanguage(codec.getName()), MessageTemplate.MatchPerformative(ACLMessage.FAILURE)))); + msg = receive(mt); + if (msg != null){ + System.out.println(this.getAgent().getAID().getLocalName()+"******* FAILURE Message Received *******"); + try { + ContentElement ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + if(_pc instanceof TUControlSequenceFailure){ + TUControlSequenceFailure _csf = (TUControlSequenceFailure)_pc; + InterfacePayloadAgentReference payload = new InterfacePayloadAgentReference(_csf.getAgentName(),_csf.getTuName()); + ConsumingRest_VPP putInstance = new ConsumingRest_VPP(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.CONTROLFAILURE, payload); + } + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + } + //******** Handling messages of the Accounting sequence ************ + mt = MessageTemplate.and( + MessageTemplate.MatchOntology(ontology.getName()),MessageTemplate.and( + MessageTemplate.MatchLanguage(codec.getName()),MessageTemplate.and( + MessageTemplate.MatchConversationId("accountingInform"), + MessageTemplate.MatchPerformative(ACLMessage.INFORM)))); + msg = receive(mt); + if (msg != null){ + System.out.println(this.getAgent().getAID().getLocalName()+"******* Accounting Sequence: INFORM Message Received *******"); + try { + ContentElement ce; + Predicate _pc; + ce = getContentManager().extractContent(msg); + _pc = (Predicate) ce; + if(_pc instanceof AccountingSequenceInform){ + addBehaviour(new AccountingSequencePerformer(msg.shallowClone())); + } + + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + } + //******** Handling messages of the loadTimeWindows Sharing ************ + mt = MessageTemplate.and( + MessageTemplate.MatchOntology(ontology.getName()),MessageTemplate.and( + MessageTemplate.MatchLanguage(codec.getName()),MessageTemplate.and( + MessageTemplate.MatchConversationId("loadTimeWindows"), + MessageTemplate.MatchPerformative(ACLMessage.REQUEST)))); + msg = receive(mt); + if (msg != null){ + System.out.println(this.getAgent().getAID().getLocalName()+"******* LoadTimeWindows Sharing: REQUEST Message Received *******"); + try { + ContentElement ce = getContentManager().extractContent(msg); + Action _ac = (Action) ce; + if(_ac.getAction() instanceof LoadTimeWindowsShareRequest){ + addBehaviour(new LoadTimeWindowsRequestPerformer(msg.shallowClone())); + } + }catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + } + //******** Handling messages of the RequestInfo Requests ************ + mt = MessageTemplate.and( + MessageTemplate.MatchOntology(ontology.getName()),MessageTemplate.and( + MessageTemplate.MatchLanguage(codec.getName()),MessageTemplate.and( + MessageTemplate.MatchConversationId("requestInfoOperation"), + MessageTemplate.MatchPerformative(ACLMessage.REQUEST)))); + msg = receive(mt); + if (msg != null){ + System.out.println(this.getAgent().getAID().getLocalName()+"******* RequestInfo Request : REQUEST Message Received *******"); + try { + ContentElement ce = getContentManager().extractContent(msg); + Action _ac = (Action) ce; + if(_ac.getAction() instanceof RequestInfoRequest){ + addBehaviour(new RequestInfoResponsePerformer(msg.shallowClone())); + } + }catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + } + //******** Handling messages of the Frequency Relay Status sequence ************ + mt = MessageTemplate.and( + MessageTemplate.MatchOntology(ontology.getName()),MessageTemplate.and( + MessageTemplate.MatchLanguage(codec.getName()),MessageTemplate.and( + MessageTemplate.MatchConversationId("frequencyRelayTriggered"), + MessageTemplate.MatchPerformative(ACLMessage.INFORM)))); + msg = receive(mt); + if (msg != null){ + System.out.println(this.getAgent().getAID().getLocalName()+"******* Frequency Relay Status: INFORM Message Received *******"); + try { + ContentElement ce; + Predicate _pc; + ce = getContentManager().extractContent(msg); + _pc = (Predicate) ce; + if(_pc instanceof FreqRelayStatusInform){ + FreqRelayStatusInform _frsi = (FreqRelayStatusInform)_pc; + addBehaviour(new FRStatusPerformer(msg.shallowClone(),_frsi.getTuName(), _frsi.getAgentName())); + } + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + } + //******** Handling messages of the CancelOperation sequence ************ + mt = MessageTemplate.and( + MessageTemplate.MatchOntology(ontology.getName()),MessageTemplate.and( + MessageTemplate.MatchLanguage(codec.getName()),MessageTemplate.and( + MessageTemplate.MatchConversationId("cancelOperation"), + MessageTemplate.MatchPerformative(ACLMessage.CANCEL)))); + msg = receive(mt); + if (msg != null){ + System.out.println(this.getAgent().getAID().getLocalName()+"******* Cancel-Operation Sequence: CANCEL Message Received *******"); + try { + ContentElement ce; + Predicate _pc; + ce = getContentManager().extractContent(msg); + _pc = (Predicate) ce; + if(_pc instanceof CancelOperationCancel){ + addBehaviour(new ReceiveCancelOperationPerformer(msg.shallowClone())); + } + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + }else { + //******** going into Block (suspends the behaviour till a new message appears) for every other type of message ************ + block(); + } + } + } + + //********************** 0. VPP Input Listener ********************** + //Manages incoming Webservice requests from the ProducingRest_VPP class + private class VPPInputListener extends CyclicBehaviour{ + //this input listener is called as an threaded behaviour so the extra thread can go into wait and resume if the rest ws notifies via the LOCK + //it processes the Node-RED inputs and this threaded approach is needed to prevent an increased CPU usage + private static final long serialVersionUID = 1L; + + public void action() { + synchronized(VppVariables.LOCK){ + try { + VppVariables.LOCK.wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + //******************* Starting the Scheduling Sequence (generic behaviour) *************** + if (VppVariables.schedulingRequestTrigger == true) { + VppVariables.schedulingRequestTrigger = false; + myAgent.addBehaviour(new SchedulingSequencePerformer(VppVariables.schedulingStart,VppVariables.schedulingEnd, + VppVariables.expiration, VppVariables.referenceID, VppVariables.serviceDescriptions)); + VppVariables.resetScheduling(); + } + + //******************* Starting the TU Control Sequence (one shot behaviour) *************** + if (VppVariables.newSetpointTrigger) { + VppVariables.newSetpointTrigger = false; + myAgent.addBehaviour(new ControlSequencePerformer(VppVariables.newSetpoint,VppVariables.tuName)); + VppVariables.resetNewSetpoint(); + } + + if (VppVariables.newLoadProfileTriggerUpdated) { + VppVariables.newLoadProfileTriggerUpdated = false; + myAgent.addBehaviour(new ControlSequencePerformer(VppVariables.newLoadProfile,VppVariables.tuName)); + VppVariables.resetNewLoadProfile(); + } + + //******************* Starting Balancing Sequence (generic behaviour) *************** + if (VppVariables.balancingTrigger) { + VppVariables.balancingTrigger = false; + myAgent.addBehaviour(new BalancingSequencePerformer(VppVariables.balancingStart,VppVariables.balancingEnd, + VppVariables.balancingTUName, VppVariables.balancingUpdateRate, VppVariables.balancingReferenceID)); + VppVariables.resetBalancing(); + } + + //******************* Starting loadTimeWindowsBroadcast Sequence (one shot behaviour) *************** + if (VppVariables.loadTimeWindowsBroadcastTrigger) { + VppVariables.loadTimeWindowsBroadcastTrigger = false; + myAgent.addBehaviour(new LoadTimeWindowsBroadcastPerformer( + VppVariables.loadTimeWindowsReference,VppVariables.windowHighBegin, + VppVariables.windowHighEnd ,VppVariables.windowLowBegin ,VppVariables.windowLowEnd )); + VppVariables.resetloadTimeWindows(); + } + + //******************* Starting Frequency Relay Enable Sequence (generic behaviour) *************** + if (VppVariables.freqRelayEnableTrigger) { + VppVariables.freqRelayEnableTrigger = false; + myAgent.addBehaviour(new FREnablePerformer(VppVariables.freqRelayEnableTUName)); + VppVariables.resetfreqRelayEnable(); + } + + //******************* Starting Frequency Relay Block Sequence (generic behaviour) *************** + if (VppVariables.freqRelayDisableTrigger) { + VppVariables.freqRelayDisableTrigger = false; + myAgent.addBehaviour(new FRBlockPerformer(VppVariables.freqRelayDisableTUName)); + VppVariables.resetfreqRelayEnable(); + } + //******************* Starting the operationCancel Sequence (generic behaviour) *************** + if (VppVariables.sendCancelOperationTrigger) { + VppVariables.sendCancelOperationTrigger = false; + myAgent.addBehaviour(new SendCancelOperationPerformer(VppVariables.sendCancelOperationTuName, VppVariables.sendCancelOperationReference)); + } + //******************* Starting the RequestInfos Sequence (generic behaviour) *************** + if (VppVariables.requestInfosTrigger) { + VppVariables.requestInfosTrigger = false; + myAgent.addBehaviour(new RequestInfoPerformer(VppVariables.requestInfosTuName)); + } + } + } + } + + //********************** 1. Scheduling Sequence ********************** + private class SchedulingSequencePerformer extends Behaviour{ + private static final long serialVersionUID = 1L; + private int step = 0; + private int agentAmount = 0; + private int responseCounter = 0; + private int receivedMessages = 0; + private String schedulingStart; + private String schedulingEnd; + private String expiration; + private String referenceID; + private String conversationID = "empty"; + private Date date; //offer expiration date + private ArrayList loadAgentList = new ArrayList(); + private TreeMap agentIDOfferList = new TreeMap(); + private ArrayList sdList = new ArrayList (); + private ArrayList agentList = new ArrayList(); + + + //constructor that prepares the schedulingSequencePerformer + public SchedulingSequencePerformer(String _schedulingStart, String _schedulingEnd, + String _expiration, String _referenceID, String _serviceDescriptions) { + schedulingStart = _schedulingStart; + schedulingEnd = _schedulingEnd; + expiration = _expiration; + referenceID = _referenceID; + _serviceDescriptions = _serviceDescriptions.replaceAll("\\s+",""); //remove all whitespaces + List serviceDescriptionList = new ArrayList(); + serviceDescriptionList = Arrays.asList(_serviceDescriptions.split(",")); + for (int i = 0; i < serviceDescriptionList.size(); i++){ + sdList.add(serviceDescriptionList.get(i)); + } + } + + public void onStart(){ + idCounter++; + conversationID = ("scheduling-"+idCounter); + System.out.println(this.getAgent().getAID().getLocalName()+"******* SchedulingSequencePerformer started ********"); + //before executing the scheduling sequence, checking the if the service descriptions have been set, if not: abort + String empty = "empty"; + if(sdList.get(0).equals(empty)){ + System.out.println("********** No ServiceDescription set, abort SchedulingSequence. ************"); + step=99; + } + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.GERMAN); + formatter.setTimeZone(TimeZone.getTimeZone("Europe/Berlin")); + try { + date = formatter.parse(expiration); + } catch (ParseException e) { + e.printStackTrace(); + } + } + + public void action(){ + switch(step){ + case 0: + //********** searching the DF for the suitable Agents ********** + //pairing the referenceID from the VPP to the conversationID given by the AgentSystem + idPairsScheduling.put(referenceID, conversationID); + //setting up the service description that will be searched + DFAgentDescription sdSearchTemplate = new DFAgentDescription() ; //contains the service description list that the schedulingSequence uses + sdSearchTemplate.addLanguages(codec.getName()); + sdSearchTemplate.addOntologies(ontology.getName()); + DFAgentDescription sdSearchTemplate_pla = new DFAgentDescription() ; + sdSearchTemplate_pla.addLanguages(codec.getName()); + sdSearchTemplate_pla.addOntologies(ontology.getName()); + System.out.println(this.getAgent().getAID().getLocalName()+"******* Will search for: ********"); + for(int i = 0; i < sdList.size(); i++){ + ServiceDescription sd = new ServiceDescription(); + sd.setType(sdList.get(i)); + System.out.println("****** Service Description: "+sd.getType()+" ********"); + sdSearchTemplate.addServices(sd); + //adding the planning specific services (for the agents that exclusively plan) + ServiceDescription sd_pla = new ServiceDescription(); + sd_pla.setType(sdList.get(i)+"_pla"); + sdSearchTemplate_pla.addServices(sd_pla); + //searching the DF for the Agents that can provide the service + try { + DFAgentDescription[] result = DFService.search(myAgent, sdSearchTemplate); + System.out.println("******* Found the following load agents: ********"); + //loadAgent = new AID[result.length]; + for(int j = 0; j < result.length; j++){ + //loadAgent[j] = result[j].getName(); + loadAgentList.add(result[j].getName()); + System.out.println("******* "+result[j].getName()+" ********"); + } + //doing the same search but now with the planning specific services + DFAgentDescription[] result_pla = DFService.search(myAgent, sdSearchTemplate_pla); + for(int j = 0; j < result_pla.length; j++){ + loadAgentList.add(result_pla[j].getName()); + System.out.println("******* "+result_pla[j].getName()+" ********"); + } + } + catch (FIPAException fe){ + fe.printStackTrace(); + } + sdSearchTemplate.clearAllServices(); + sdSearchTemplate_pla.clearAllServices(); + } + if(loadAgentList.size()>0) { + step = 1; + }else { + System.out.println("No Agent can be found with that service description"); + step = 99; + } + step = 1; + break; + case 1: + //********** sending out the CFP message to the agents that can provide the service + ACLMessage msg = new ACLMessage(ACLMessage.CFP); + agentAmount = loadAgentList.size(); + for(int i = 0; i < loadAgentList.size(); i++){ + msg.addReceiver(loadAgentList.get(i)); + } + loadAgentList.clear(); + msg.setOntology(ontology.getName()); + msg.setLanguage(codec.getName()); + msg.setConversationId(conversationID); + msg.setReplyByDate(date); + try { + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + CFPSchedulingSequence newSchedulingSequence = new CFPSchedulingSequence(); + newSchedulingSequence.setTimeBegin(schedulingStart); + newSchedulingSequence.setTimeEnd(schedulingEnd); + Action act = new Action(); + act.setAction(newSchedulingSequence); //Adding the Action the Agent has to perform + act.setActor(new AID("*", AID.ISGUID)); //Adding an dummy Agent, because the actor field is mandatory (source: http://jade.tilab.com/pipermail/jade-develop/2010q4/016200.html) + cel.add(act); + cm.fillContent(msg, cel); + myAgent.send(msg); + msg.reset(); + } + catch (OntologyException oe){ + oe.printStackTrace(); + } + catch (CodecException ce){ + ce.printStackTrace(); + } + step = 2; + break; + case 2: + //************* collecting responses from the TUs **************** + MessageTemplate mt = MessageTemplate.and( + MessageTemplate.MatchOntology(ontology.getName()),MessageTemplate.and( + MessageTemplate.MatchConversationId(conversationID),MessageTemplate.and( + MessageTemplate.MatchLanguage(codec.getName()),MessageTemplate.or( + MessageTemplate.MatchPerformative(ACLMessage.PROPOSE), + MessageTemplate.MatchPerformative(ACLMessage.REFUSE))))); + msg = receive(mt); //returns the first message of the message queue with the corresponding template + if (msg != null){ + receivedMessages++; + int substep = msg.getPerformative(); + ContentElement ce = null; + InterfacePayloadAgentReference payload; + switch(substep){ + case (ACLMessage.PROPOSE): + System.out.println(this.getAgent().getAID().getLocalName()+"******* PROPOSE Message Received from "+msg.getSender().getLocalName()+" *******"); + try { + ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + if(_pc instanceof CFPSchedulingSequencePropose){ + CFPSchedulingSequencePropose _cfpp = (CFPSchedulingSequencePropose)_pc; + agentIDOfferList.put((msg.getSender().getLocalName()+", "+referenceID), msg.getSender()); + payload = new InterfacePayloadAgentReference(msg.getSender().getLocalName()+", "+referenceID,_cfpp.getAgentName(), _cfpp.getTuName()); + ConsumingRest_VPP putInstance = new ConsumingRest_VPP(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.SCHEDULINGPLANINFOS, payload); + // Saving / sending the propose with the scheduling plan to the VPP + byte[] buffer; + buffer = _cfpp.getSchedulingPlan(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.SCHEDULINGPLAN, buffer); + } + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + step = 99; + } + break; + case (ACLMessage.REFUSE): + System.out.println(this.getAgent().getAID().getLocalName()+"******* REFUSE Message Received from "+msg.getSender().getLocalName()+" *******"); + try { + ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + if(_pc instanceof CFPSchedulingSequenceRefuse){ + CFPSchedulingSequenceRefuse _cfpr = (CFPSchedulingSequenceRefuse)_pc; + payload = new InterfacePayloadAgentReference(msg.getSender().getLocalName()+", "+referenceID,_cfpr.getAgentName(), _cfpr.getTuName()); + ConsumingRest_VPP putInstance = new ConsumingRest_VPP(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.SCHEDULINGREFUSE, payload); + } + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + step = 99; + } + break; + default: + System.out.println(this.getAgent().getAID().getName()+"******* nothing *******"); + } + if(receivedMessages >= agentAmount ) { + step = 3; + receivedMessages = 0; + agentAmount = 0; + } + } else { + //checking if the expiration time for the offer transfer is over, then move to the next step + Date dateNow = new Date(); + long t= date.getTime(); + Date afterAddingOneMin=new Date(t + (1 * 60000)); + if(dateNow.after(afterAddingOneMin)) { + if(receivedMessages == 0) { + step = 99; + }else { + step = 3; + } + receivedMessages = 0; + agentAmount = 0; + } + block(); //behaviour is ‘blocked’ so that the agent no longer schedules it for execution. + } + break; + case 3: + //************* sending out new accept- and reject-proposals **************** + //************* accept-proposals ************** + boolean atLeastOneAccepted = false; + if(VppVariables.schedulingAcceptTrigger){ + VppVariables.schedulingAcceptTrigger = false; + atLeastOneAccepted = true; + //checking if the answer from the VPP is referencing to one of these conversations + if(agentIDOfferList.containsKey(VppVariables.schedulingPlanReference)) { + if(!agentList.contains(VppVariables.schedulingPlanReference)) { + responseCounter++; + agentList.add(VppVariables.schedulingPlanReference); + } + System.out.println(this.getAgent().getAID().getLocalName()+"******* "+VppVariables.schedulingPlanReference+" accepted *******"); + //*********** Sending accept-proposal to the corresponding agent *********** + msg = new ACLMessage(ACLMessage.ACCEPT_PROPOSAL); + msg.setOntology(ontology.getName()); + msg.setLanguage(codec.getName()); + msg.setConversationId(conversationID); + AID receiver = new AID(); + receiver = agentIDOfferList.get(VppVariables.schedulingPlanReference); + msg.addReceiver(receiver); + try { + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + CFPSchedulingSequenceAccept newSchedulingSequenceAccept = new CFPSchedulingSequenceAccept(); + newSchedulingSequenceAccept.setSchedulingStart(VppVariables.schedulingStart); + newSchedulingSequenceAccept.setSchedulingEnd(VppVariables.schedulingEnd); + cel.add(newSchedulingSequenceAccept); + cm.fillContent(msg, cel); + myAgent.send(msg); + agentAmount++; //increase the agent amount counter to wait for the correct amount of agents that responds later + msg.reset(); + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + } + }//************* reject-proposals ************** + else if (VppVariables.schedulingRejectTrigger) { + VppVariables.schedulingRejectTrigger = false; + if(agentIDOfferList.containsKey(VppVariables.schedulingPlanReference)) { + if(!agentList.contains(VppVariables.schedulingPlanReference)) { + responseCounter++; + agentList.add(VppVariables.schedulingPlanReference); + } + System.out.println(this.getAgent().getAID().getLocalName()+"******* "+VppVariables.schedulingPlanReference+" rejected *******"); + //*********** Sending reject-proposal to the corresponding agent *********** + msg = new ACLMessage(ACLMessage.REJECT_PROPOSAL); + msg.setOntology(ontology.getName()); + msg.setLanguage(codec.getName()); + msg.setConversationId(conversationID); + AID receiver = new AID(); + receiver = agentIDOfferList.get(VppVariables.schedulingPlanReference); + msg.addReceiver(receiver); + try { + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + CFPSchedulingSequenceReject newSchedulingSequenceReject = new CFPSchedulingSequenceReject(); + cel.add(newSchedulingSequenceReject); + cm.fillContent(msg, cel); + myAgent.send(msg); + msg.reset(); + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + } + } + //if all answers have been sent, change to the next state + if((responseCounter >= agentIDOfferList.size())) { + if(!atLeastOneAccepted) { //if no agent has been accepted, end the behaviour + step = 99; + }else { + step = 4; + } + } + break; + case 4: + //************* collecting responses from the TUs **************** + mt = MessageTemplate.and( + MessageTemplate.MatchOntology(ontology.getName()),MessageTemplate.and( + MessageTemplate.MatchConversationId(conversationID),MessageTemplate.and( + MessageTemplate.MatchLanguage(codec.getName()), MessageTemplate.or( + MessageTemplate.MatchPerformative(ACLMessage.INFORM), + MessageTemplate.MatchPerformative(ACLMessage.FAILURE))))); + + msg = receive(mt); //returns the first message of the message queue with the corresponding template + if (msg != null){ + int substep = msg.getPerformative(); + ContentElement ce = null; + InterfacePayloadAgentReference payload; + switch(substep){ + case (ACLMessage.INFORM): + receivedMessages++; + System.out.println(this.getAgent().getAID().getLocalName()+"******* INFORM Message received from: "+msg.getSender().getLocalName()+ " *******"); + try { + //msg.getConversationId(); + ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + if(_pc instanceof CFPSchedulingSequenceDone){ + CFPSchedulingSequenceDone _cfpd = (CFPSchedulingSequenceDone)_pc; + payload = new InterfacePayloadAgentReference(msg.getSender().getLocalName()+", "+referenceID,_cfpd.getAgentName(), _cfpd.getTuName()); + //********** Inform the VPP-System about the results********** + //******* sending the INFROM info to the VPP ******** + ConsumingRest_VPP putInstance = new ConsumingRest_VPP(); + putInstance = new ConsumingRest_VPP(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.SCHEDULINGDONE, payload); + }else { + step = 99; + } + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + step = 99; + } + break; + case (ACLMessage.FAILURE): + receivedMessages++; + System.out.println(this.getAgent().getAID().getLocalName()+"******* FAILURE Message received from: "+msg.getSender().getLocalName()+ " *******"); + try { + //msg.getConversationId(); + ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + if(_pc instanceof CFPSchedulingSequenceFailure){ + CFPSchedulingSequenceFailure _cfpf = (CFPSchedulingSequenceFailure)_pc; + //********** Inform the VPP-System about the results********** + //******* sending the FAILURE info to the VPP ******** + payload = new InterfacePayloadAgentReference(msg.getSender().getLocalName()+", "+referenceID,_cfpf.getAgentName(), _cfpf.getTuName()); + ConsumingRest_VPP putInstance = new ConsumingRest_VPP(); + putInstance = new ConsumingRest_VPP(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.SCHEDULINGFAILURE, payload); + }else { + step = 99; + } + }catch (CodecException | OntologyException e) { + e.printStackTrace(); + step = 99; + } + break; + default: + System.out.println(this.getAgent().getAID().getLocalName()+"******* nothing *******"); + } + }else { + block(); + } + if(receivedMessages >= agentAmount) { + step = 99; + } + break; + case 99: //final case, here reset() must be called. A reset() in done() would result in a reset() call every cycle, because done() gets called every cycle + reset(); //any Behaviour object that has been executed once, must be reset by calling its reset() method before it can be executed again. + step = 100; + break; + default: + step = 99; + } + } + + public boolean done() { + return step == 100; //the agent behaviour action stops if step == 10 + } + } + + //********************** 2. TU Control Sequence ********************** + private class ControlSequencePerformer extends OneShotBehaviour{ + private static final long serialVersionUID = 1L; + private boolean newSetpointVersion = false; + private int newSetpoint = 0; + private String tuName = "noNameSet"; + private String conversationID = "noID"; + private ArrayList agentIdentifiers = new ArrayList(); + private byte[] newLoadProfile; + + public ControlSequencePerformer(int _newSetpoint, String _tuName) { + tuName = _tuName; + newSetpoint = _newSetpoint; + newSetpointVersion = true; + } + + public ControlSequencePerformer(byte[] _newLoadProfile, String _tuName) { + tuName = _tuName; + newLoadProfile = _newLoadProfile; + } + + public void onStart(){ + idCounter++; + conversationID = ("loadControl-"+idCounter); + System.out.println(this.getAgent().getAID().getLocalName()+"******* TUControlSequencePerformer started ********"); + } + + public void action() { + //********** searching the DF for the Agent ********** + //searching for the complete AID of the referenced agent + DFAgentDescription sdSearchTemplate = new DFAgentDescription() ; + sdSearchTemplate.addLanguages(codec.getName()); + sdSearchTemplate.addOntologies(ontology.getName()); + System.out.println("Tu Control Performer started, will search for "+tuName); + ServiceDescription sd = new ServiceDescription(); + sd.setName(tuName); + sdSearchTemplate.addServices(sd); + //searching the DF for the Agents that can provide the service + try { + DFAgentDescription[] result = DFService.search(myAgent, sdSearchTemplate); + System.out.println("Found the following load agents: "); + //loadAgent = new AID[result.length]; + for(int j = 0; j < result.length; j++){ + //loadAgent[j] = result[j].getName(); + agentIdentifiers.add(result[j].getName()); + System.out.println(result[j].getName()); + } + } + catch (FIPAException fe){ + fe.printStackTrace(); + } + sdSearchTemplate.clearAllServices(); + if(agentIdentifiers.size()==0) { + sd.setName(tuName+"_act"); + sdSearchTemplate.addServices(sd); + //searching the DF for the Agents that can provide the service + try { + DFAgentDescription[] result = DFService.search(myAgent, sdSearchTemplate); + System.out.println("Found the following load agents: "); + //loadAgent = new AID[result.length]; + for(int j = 0; j < result.length; j++){ + //loadAgent[j] = result[j].getName(); + agentIdentifiers.add(result[j].getName()); + System.out.println(result[j].getName()); + } + } + catch (FIPAException fe){ + fe.printStackTrace(); + } + } + if(agentIdentifiers.size()>0) { + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending out new Setpoint to TU: " + tuName +". NewSetpoint:"+newSetpoint+" *******"); + //*********** Sending Request to the corresponding agent *********** + ACLMessage msg = new ACLMessage(ACLMessage.REQUEST); + msg.setOntology(ontology.getName()); + msg.setLanguage(codec.getName()); + msg.setConversationId(conversationID); + AID receiver = new AID(); + for(int j = 0; j < agentIdentifiers.size(); j++){ + receiver = agentIdentifiers.get(j); + } + msg.addReceiver(receiver); + try { + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + if(newSetpointVersion) { + TUControlSequenceRequestSetpoint newLoadControlRequestNewSetpoint = new TUControlSequenceRequestSetpoint(); + newLoadControlRequestNewSetpoint.setNewSetpoint(newSetpoint); + newLoadControlRequestNewSetpoint.setTuName(tuName); + Action act = new Action(); + act.setAction(newLoadControlRequestNewSetpoint); //Adding the Action the Agent has to perform + act.setActor(new AID("*", AID.ISGUID)); //Adding an dummy Agent, because the actor field is mandatory (source: http://jade.tilab.com/pipermail/jade-develop/2010q4/016200.html) + cel.add(act); + cm.fillContent(msg, cel); + myAgent.send(msg); + msg.reset(); + }else { + TUControlSequenceRequestLoadProfile newLoadControlRequestLoadProfile = new TUControlSequenceRequestLoadProfile(); + newLoadControlRequestLoadProfile.setNewLoadProfile(newLoadProfile); + newLoadControlRequestLoadProfile.setTuName(tuName); + Action act = new Action(); + act.setAction(newLoadControlRequestLoadProfile); //Adding the Action the Agent has to perform + act.setActor(new AID("*", AID.ISGUID)); //Adding an dummy Agent, because the actor field is mandatory (source: http://jade.tilab.com/pipermail/jade-develop/2010q4/016200.html) + cel.add(act); + cm.fillContent(msg, cel); + myAgent.send(msg); + msg.reset(); + } + + } catch (CodecException | OntologyException e){ + e.printStackTrace(); + } + }else { + System.out.println("No Agent can be found under that name"); + } + } + } + + //********************** 3. TU Balancing Sequence ********************** + private class BalancingSequencePerformer extends Behaviour{ + private static final long serialVersionUID = 1L; + private int step = 0; + private int updateRate = 1; + private String balancingStart; + private String balancingEnd; + private String tuName = "noNameSet"; + private String conversationID = "noID"; + private String referenceID = "noID"; + private Date balancingEndDate; + private ArrayList agentIdentifiers = new ArrayList(); + + public BalancingSequencePerformer(String _balancingStart, String _balancingEnd, + String _tuName, int _updateRate, String _referenceID) { + balancingStart = _balancingStart; + balancingEnd = _balancingEnd; + tuName = _tuName; + updateRate = _updateRate; + referenceID = _referenceID; + } + + public void onStart(){ + idCounter++; + conversationID = ("Balancing-"+idCounter); + + try { + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.GERMAN); + formatter.setTimeZone(TimeZone.getTimeZone("Europe/Berlin")); + balancingEndDate = formatter.parse(balancingEnd); + } catch (ParseException e) { + + e.printStackTrace(); + } + System.out.println(this.getAgent().getAID().getLocalName()+"******* BalancingSequencePerformer started ********"); + } + + public void action() { + Date dateNow = new Date(); + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.GERMAN); + formatter.format(dateNow); + + switch(step){ + //********** searching the DF for the Agent ********** + case 0: + if (dateNow.after(balancingEndDate)){ + System.out.println(this.getAgent().getAID().getLocalName()+"******* BalancingSequencePerformer stopped (EndDate not valid) ********"); + step = 99; + } + //pairing the referenceID from the VPP to the conversationID given by the AgentSystem + idPairsBalancing.put(referenceID, conversationID); + //searching for the complete AID of the referenced agent + DFAgentDescription sdSearchTemplate = new DFAgentDescription() ; + sdSearchTemplate.addLanguages(codec.getName()); + sdSearchTemplate.addOntologies(ontology.getName()); + ServiceDescription sd = new ServiceDescription(); + sd.setName(tuName); + sdSearchTemplate.addServices(sd); + //searching the DF for the Agents that can provide the service + try { + DFAgentDescription[] result = DFService.search(myAgent, sdSearchTemplate); + System.out.println("Found the following load agents: "); + //loadAgent = new AID[result.length]; + for(int j = 0; j < result.length; j++){ + //loadAgent[j] = result[j].getName(); + agentIdentifiers.add(result[j].getName()); + System.out.println(result[j].getName()); + } + } + catch (FIPAException fe){ + fe.printStackTrace(); + } + sdSearchTemplate.clearAllServices(); + if(agentIdentifiers.size()==0) { + sd.setName(tuName+"_act"); + sdSearchTemplate.addServices(sd); + //searching the DF for the Agents that can provide the service + try { + DFAgentDescription[] result = DFService.search(myAgent, sdSearchTemplate); + System.out.println("Found the following agents: "); + //loadAgent = new AID[result.length]; + for(int j = 0; j < result.length; j++){ + //loadAgent[j] = result[j].getName(); + agentIdentifiers.add(result[j].getName()); + System.out.println(result[j].getName()); + } + } + catch (FIPAException fe){ + fe.printStackTrace(); + } + } + if(agentIdentifiers.size()>0) { + step = 1; + }else { + System.out.println("No Agent can be found under that name"); + step = 99; + } + break; + //*********** Sending SUBSCRIBE Request to the corresponding agent *********** + case 1: + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending out new Subscribe to TU: " + tuName +" *******"); + ACLMessage msg = new ACLMessage(ACLMessage.SUBSCRIBE); + msg.setOntology(ontology.getName()); + msg.setLanguage(codec.getName()); + msg.setConversationId(conversationID); + SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.GERMAN); + formatter2.setTimeZone(TimeZone.getTimeZone("Europe/Berlin")); + Date date; + try { + date = formatter2.parse(balancingStart); + msg.setReplyByDate(date); + } catch (ParseException e) { + e.printStackTrace(); + } + + AID receiver = new AID(); + for(int j = 0; j < agentIdentifiers.size(); j++){ + receiver = agentIdentifiers.get(j); + } + msg.addReceiver(receiver); + try { + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + BalancingSequenceSubscribe newBalancingSequenceSubscribe = new BalancingSequenceSubscribe(); + newBalancingSequenceSubscribe.setTuName(tuName); + newBalancingSequenceSubscribe.setBalancingStart(balancingStart); + newBalancingSequenceSubscribe.setBalancingEnd(balancingEnd); + newBalancingSequenceSubscribe.setUpdateRate(updateRate); + Action act = new Action(); + act.setAction(newBalancingSequenceSubscribe); //Adding the Action the Agent has to perform + act.setActor(receiver); //Adding an dummy Agent, because the actor field is mandatory (source: http://jade.tilab.com/pipermail/jade-develop/2010q4/016200.html) + cel.add(act); + cm.fillContent(msg, cel); + myAgent.send(msg); + msg.reset(); + step = 2; + } catch (CodecException | OntologyException e){ + e.printStackTrace(); + step = 99; + } + break; + //************* collecting responses from the TUs **************** + case 2: + MessageTemplate mt = MessageTemplate.and( + MessageTemplate.MatchOntology(ontology.getName()),MessageTemplate.and( + MessageTemplate.MatchConversationId(conversationID),MessageTemplate.and( + MessageTemplate.MatchLanguage(codec.getName()),MessageTemplate.or( + MessageTemplate.MatchPerformative(ACLMessage.REFUSE), + MessageTemplate.MatchPerformative(ACLMessage.AGREE))))); + msg = receive(mt); //returns the first message of the message queue with the corresponding template + if (msg != null){ + ContentElement ce = null; + if (msg.getPerformative() == ACLMessage.AGREE) { + System.out.println(this.getAgent().getAID().getLocalName()+"******* AGREE Message Received *******"); + try { + ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + if(_pc instanceof BalancingSequenceAgree){ + //********** Inform the VPP-System about the results********** + BalancingSequenceAgree _bsa = (BalancingSequenceAgree)_pc; + InterfacePayloadAgentReference payload = new InterfacePayloadAgentReference(_bsa.getTuName(),_bsa.getAgentName()); + ConsumingRest_VPP putInstance = new ConsumingRest_VPP(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.BALANCINGAGREE, payload); + step = 3; + } + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + }else if (msg.getPerformative() == ACLMessage.REFUSE) { + System.out.println(this.getAgent().getAID().getLocalName()+"******* REJECT Message Received *******"); + try { + ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + if(_pc instanceof BalancingSequenceRefuse){ + //********** Inform the VPP-System about the results********** + BalancingSequenceRefuse _bsr = (BalancingSequenceRefuse)_pc; + InterfacePayloadAgentReference payload = new InterfacePayloadAgentReference(_bsr.getTuName(),_bsr.getAgentName()); + ConsumingRest_VPP putInstance = new ConsumingRest_VPP(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.BALANCINGREFUSE, payload); + step = 99; + } + }catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + } else { //normally can not be the case + step = 99; + } + }else { + block(); + } + break; + //************* collecting values from the TUs **************** + case 3: + mt = MessageTemplate.and( + MessageTemplate.MatchOntology(ontology.getName()),MessageTemplate.and( + MessageTemplate.MatchConversationId(conversationID),MessageTemplate.and( + MessageTemplate.MatchLanguage(codec.getName()),MessageTemplate.or( + MessageTemplate.MatchPerformative(ACLMessage.INFORM), + MessageTemplate.MatchPerformative(ACLMessage.FAILURE))))); + msg = receive(mt); //returns the first message of the message queue with the corresponding template + if (msg != null){ + ContentElement ce = null; + if (msg.getPerformative() == ACLMessage.INFORM) { + System.out.println(this.getAgent().getAID().getLocalName()+"******* INFORM Message Received *******"); + try { + ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + if(_pc instanceof BalancingSequenceInform){ + //********** Inform the VPP-System about the results********** + BalancingSequenceInform _bsi = (BalancingSequenceInform)_pc; + //InterfacePayloadAgentReference payload = new InterfacePayloadAgentReference(_bsi.getTuName(),_bsi.getAgentName()); + ConsumingRest_VPP putInstance = new ConsumingRest_VPP(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.BALANCINGINFORM, _bsi); + } + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + }else if(msg.getPerformative() == ACLMessage.FAILURE) { + System.out.println(this.getAgent().getAID().getLocalName()+"******* FAILURE Message Received *******"); + try { + ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + if(_pc instanceof BalancingSequenceFailure){ + //********** Inform the VPP-System about the results********** + BalancingSequenceFailure _bsf = (BalancingSequenceFailure)_pc; + InterfacePayloadAgentReference payload = new InterfacePayloadAgentReference(_bsf.getTuName(),msg.getSender().getLocalName()); + ConsumingRest_VPP putInstance = new ConsumingRest_VPP(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.BALANCINGFAILURE, payload); + step = 99; + } + }catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + }else { //normally can not be the case + step = 99; + } + }else { //stop the behaviour after the last message has been received and additional 5 secs to get every message from the pending queue + Date currentTime = new Date(); + long t= balancingEndDate.getTime(); + Date afterAdding5Sec=new Date(t + (1 * 5000)); + if(currentTime.after(afterAdding5Sec)) { + step = 99; + } + } + break; + case 99: //final case, here reset() must be called. A reset() in done() would result in a reset() call every cycle, because done() gets called every cycle + reset(); //any Behaviour object that has been executed once, must be reset by calling its reset() method before it can be executed again. + step = 100; + break; + default: + step = 99; + } + } + public boolean done() { + return step == 100; + } + } + + //********************** 4. Accounting Sequence ********************** + private class AccountingSequencePerformer extends Behaviour{ + private static final long serialVersionUID = 1L; + private int step = 0; + private String tuName = "noNameSet"; + private String agentName = "noNameSet"; + private byte[] energyConsumptionProfile; + private ACLMessage msg; + + AccountingSequencePerformer(ACLMessage _msg) { + msg = _msg; + } + + public void onStart(){ + try { + ContentElement ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + AccountingSequenceInform _asi = (AccountingSequenceInform)_pc; + tuName = _asi.getTuName(); + agentName = _asi.getAgentName(); + energyConsumptionProfile = _asi.getEnergyConsumptionProfile(); + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + } + + public void action() { + switch (step) { + case 0: //sending the energy consumption profiles to the VPP + ConsumingRest_VPP putInstance = new ConsumingRest_VPP(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.ENERGYCONSUMPTIONPROFILE, energyConsumptionProfile); + ConsumingRest_VPP putInstance2 = new ConsumingRest_VPP(); + InterfacePayloadAgentReference payload = new InterfacePayloadAgentReference(agentName,tuName); + putInstance2.putNodeRed(Addresses.URL_NODERED, PutVariable.ACCOUNTINGECPREFERENCE, payload); + step = 1; + break; + case 1: + if(VppVariables.accountingTrigger && tuName.equals(VppVariables.accountingTUName)) { + VppVariables.accountingTrigger = false; + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending out new INFORM to TU: " + tuName +" *******"); + try { + ACLMessage reply = msg.createReply(); + reply.setPerformative(ACLMessage.INFORM); + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + AccountingSequenceInformReceived newAccountingSequenceInformReceived = new AccountingSequenceInformReceived(); + newAccountingSequenceInformReceived.setTuName(tuName); + cel.add(newAccountingSequenceInformReceived); + cm.fillContent(reply, cel); + myAgent.send(reply); + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + step = 99; + } + break; + case 99: //final case, here reset() must be called. A reset() in done() would result in a reset() call every cycle, because done() gets called every cycle + reset(); //any Behaviour object that has been executed once, must be reset by calling its reset() method before it can be executed again. + step = 100; + break; + default: + step = 99; + } + } + + public boolean done() { + return step == 100; + } + } + + //********************** 5.A LoadTimeWindowsSharing Sequence (Request from other Agents) ********************** + private class LoadTimeWindowsRequestPerformer extends Behaviour{ + private static final long serialVersionUID = 1L; + private int step = 0; + private String loadTimeWindowsReference; + private ACLMessage msg; + + LoadTimeWindowsRequestPerformer(ACLMessage _msg){ + msg = _msg; + } + + public void onStart(){ + try { + ContentElement ce = getContentManager().extractContent(msg); + Action _ac = (Action) ce; + if(_ac.getAction() instanceof LoadTimeWindowsShareRequest){ + LoadTimeWindowsShareRequest _ltwsr = (LoadTimeWindowsShareRequest)_ac.getAction(); + loadTimeWindowsReference = _ltwsr.getLoadTimeWindowsReference(); + } + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + } + + public void action() { + switch (step) { + case 0: //sending the request with reference profiles to the VPP + ConsumingRest_VPP putInstance = new ConsumingRest_VPP(); + InterfacePayloadLoadTimeWindows payload = new InterfacePayloadLoadTimeWindows(loadTimeWindowsReference); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.LOADTIMEWINDOWSREQUEST, payload); + step = 1; + break; + case 1: + if(VppVariables.loadTimeWindowsTrigger && loadTimeWindowsReference.equals(VppVariables.loadTimeWindowsReference)) { + VppVariables.loadTimeWindowsTrigger = false; + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending out new INFORM to TU: " + msg.getSender().getLocalName() +" *******"); + try { + ACLMessage reply = msg.createReply(); + reply.setPerformative(ACLMessage.INFORM); + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + LoadTimeWindowsShareInform newLoadTimeWindowsShareInform = new LoadTimeWindowsShareInform(); + newLoadTimeWindowsShareInform.setLoadTimeWindowsReference(loadTimeWindowsReference); + newLoadTimeWindowsShareInform.setWindowHighBegin(VppVariables.windowHighBegin); + newLoadTimeWindowsShareInform.setWindowHighEnd(VppVariables.windowHighEnd); + newLoadTimeWindowsShareInform.setWindowLowBegin(VppVariables.windowLowBegin); + newLoadTimeWindowsShareInform.setWindowLowEnd(VppVariables.windowLowEnd); + VppVariables.resetloadTimeWindows(); + cel.add(newLoadTimeWindowsShareInform); + cm.fillContent(reply, cel); + myAgent.send(reply); + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + step = 99; + } + break; + case 99: //final case, here reset() must be called. A reset() in done() would result in a reset() call every cycle, because done() gets called every cycle + reset(); //any Behaviour object that has been executed once, must be reset by calling its reset() method before it can be executed again. + step = 100; + break; + default: + step = 99; + } + } + + public boolean done() { + return step == 100; + } + + } + + //********************** 5.B LoadTimeWindowsSharing Sequence (Broadcast from the VPP) ********************** + private class LoadTimeWindowsBroadcastPerformer extends OneShotBehaviour{ + private static final long serialVersionUID = 1L; + private String loadTimeWindowsReference; + private String windowHighBegin; + private String windowHighEnd; + private String windowLowBegin; + private String windowLowEnd; + + LoadTimeWindowsBroadcastPerformer(String _loadTimeWindowsReference, String _windowHighBegin, String _windowHighEnd, String _windowLowBegin, String _windowLowEnd){ + loadTimeWindowsReference = _loadTimeWindowsReference; + windowHighBegin = _windowHighBegin; + windowHighEnd = _windowHighEnd; + windowLowBegin = _windowLowBegin; + windowLowEnd = _windowLowEnd; + } + + public void action() { + ArrayList tuAgents = new ArrayList(); + System.out.println(this.getAgent().getAID().getLocalName()+"******* Informing TUs about new LoadTimeWindows ********"); + //searching for the complete AID of the referenced agent + DFAgentDescription sdSearchTemplate = new DFAgentDescription() ; + sdSearchTemplate.addLanguages(codec.getName()); + sdSearchTemplate.addOntologies(ontology.getName()); + ServiceDescription sd = new ServiceDescription(); + sd.setType("ANN"); + sdSearchTemplate.addServices(sd); + //the same for the "ANN_pla" Services + DFAgentDescription sdSearchTemplate_pla =new DFAgentDescription() ; + sdSearchTemplate_pla.addLanguages(codec.getName()); + sdSearchTemplate_pla.addOntologies(ontology.getName()); + ServiceDescription sd_pla = new ServiceDescription(); + sd_pla.setType("ANN_pla"); + sdSearchTemplate_pla.addServices(sd_pla); + //searching the DF for the Agents that can provide the service + try { + DFAgentDescription[] result = DFService.search(myAgent, sdSearchTemplate); + for(int j = 0; j < result.length; j++){ + tuAgents.add(result[j].getName()); + } + DFAgentDescription[] result_pla = DFService.search(myAgent, sdSearchTemplate_pla); + for(int j = 0; j < result_pla.length; j++){ + tuAgents.add(result_pla[j].getName()); + } + } + catch (FIPAException fe){ + fe.printStackTrace(); + } + sdSearchTemplate.clearAllServices(); + sdSearchTemplate_pla.clearAllServices(); + + if(tuAgents.size()>0) { + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending out new INFORM to TUs *******"); + try { + + ACLMessage msg = new ACLMessage(ACLMessage.INFORM); + msg.setOntology(ontology.getName()); + msg.setLanguage(codec.getName()); + msg.setConversationId("loadTimeWindows"); + //add receiver with ANN service description + for(int i = 0; i < tuAgents.size(); i++){ + msg.addReceiver(tuAgents.get(i)); + } + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + LoadTimeWindowsShareInform newLoadTimeWindowsShareInform = new LoadTimeWindowsShareInform(); + newLoadTimeWindowsShareInform.setLoadTimeWindowsReference(loadTimeWindowsReference); + newLoadTimeWindowsShareInform.setWindowHighBegin(windowHighBegin); + newLoadTimeWindowsShareInform.setWindowHighEnd(windowHighEnd); + newLoadTimeWindowsShareInform.setWindowLowBegin(windowLowBegin); + newLoadTimeWindowsShareInform.setWindowLowEnd(windowLowEnd); + cel.add(newLoadTimeWindowsShareInform); + cm.fillContent(msg, cel); + myAgent.send(msg); + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + } + } + + + } + + //********************** 6.A FrequencyRelayEnable Sequence ********************** + private class FREnablePerformer extends Behaviour{ + private static final long serialVersionUID = 1L; + private int step = 0; + private String tuName = "noNameSet"; + private String conversationID = "noID"; + private ArrayList agentIdentifiers = new ArrayList(); + + FREnablePerformer(String _tuName) { + tuName = _tuName; + } + + public void onStart(){ + idCounter++; + conversationID = ("FREnable-"+idCounter); + } + + public void action() { + switch (step) { + case 0: + System.out.println(this.getAgent().getAID().getLocalName()+"******* Frequency Relay Enable started ********"); + //searching for the complete AID of the referenced agent + DFAgentDescription sdSearchTemplate = new DFAgentDescription() ; + sdSearchTemplate.addLanguages(codec.getName()); + sdSearchTemplate.addOntologies(ontology.getName()); + ServiceDescription sd = new ServiceDescription(); + sd.setName(tuName); + sdSearchTemplate.addServices(sd); + //searching the DF for the Agents that can provide the service + try { + DFAgentDescription[] result = DFService.search(myAgent, sdSearchTemplate); + System.out.println("Found the following load agents: "); + //loadAgent = new AID[result.length]; + for(int j = 0; j < result.length; j++){ + agentIdentifiers.add(result[j].getName()); + System.out.println(result[j].getName()); + } + } + catch (FIPAException fe){ + fe.printStackTrace(); + } + sdSearchTemplate.clearAllServices(); + if(agentIdentifiers.size()>0) { + step = 1; + }else { + System.out.println("No Agent can be found under that name"); + step = 99; + } + break; + case 1://waiting for confirmation + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending out FR enable to TU: " + tuName +" *******"); + ACLMessage msg = new ACLMessage(ACLMessage.REQUEST); + msg.setOntology(ontology.getName()); + msg.setLanguage(codec.getName()); + msg.setConversationId(conversationID); + AID receiver = new AID(); + for(int j = 0; j < agentIdentifiers.size(); j++){ + receiver = agentIdentifiers.get(j); + } + msg.addReceiver(receiver); + try { + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + FreqRelayEnableRequest newFreqRelayEnableRequest = new FreqRelayEnableRequest(); + newFreqRelayEnableRequest.setTuName(tuName); + Action act = new Action(); + act.setAction(newFreqRelayEnableRequest); //Adding the Action the Agent has to perform + act.setActor(new AID("*", AID.ISGUID)); //Adding an dummy Agent, because the actor field is mandatory (source: http://jade.tilab.com/pipermail/jade-develop/2010q4/016200.html) + cel.add(act); + cm.fillContent(msg, cel); + myAgent.send(msg); + msg.reset(); + step = 2; + + } catch (CodecException | OntologyException e){ + e.printStackTrace(); + step = 99; + } + break; + case 2: + MessageTemplate mt = MessageTemplate.and( + MessageTemplate.MatchOntology(ontology.getName()),MessageTemplate.and( + MessageTemplate.MatchConversationId(conversationID),MessageTemplate.and( + MessageTemplate.MatchLanguage(codec.getName()),MessageTemplate.or( + MessageTemplate.MatchPerformative(ACLMessage.INFORM), + MessageTemplate.MatchPerformative(ACLMessage.FAILURE))))); + msg = receive(mt); //returns the first message of the message queue with the corresponding template + if (msg != null){ + try { + ContentElement ce = null; + if (msg.getPerformative() == ACLMessage.INFORM) { + System.out.println(this.getAgent().getAID().getLocalName()+"******* INFORM Message Received *******"); + ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + if(_pc instanceof FreqRelayEnableInform){ + //********** Inform the VPP-System about the results********** + FreqRelayEnableInform _fre = (FreqRelayEnableInform)_pc; + InterfacePayloadAgentReference payload = new InterfacePayloadAgentReference(_fre.getTuName(),_fre.getAgentName()); + ConsumingRest_VPP putInstance = new ConsumingRest_VPP(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.FRENABLEINFORM, payload); + step = 99; + } + }else if (msg.getPerformative() == ACLMessage.FAILURE) { + System.out.println(this.getAgent().getAID().getLocalName()+"******* FAILURE Message Received *******"); + ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + if(_pc instanceof FreqRelayEnableFailure){ + //********** Inform the VPP-System about the results********** + FreqRelayEnableFailure _fre = (FreqRelayEnableFailure)_pc; + InterfacePayloadAgentReference payload = new InterfacePayloadAgentReference(_fre.getTuName(),_fre.getAgentName()); + ConsumingRest_VPP putInstance = new ConsumingRest_VPP(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.FRENABLEFAILURE, payload); + step = 99; + } + } else { //normally can not be the case + step = 99; + } + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + step = 99; + } + }else { + block(); + } + break; + case 99: //final case, here reset() must be called. A reset() in done() would result in a reset() call every cycle, because done() gets called every cycle + reset(); //any Behaviour object that has been executed once, must be reset by calling its reset() method before it can be executed again. + step = 100; + break; + default: + step = 99; + } + } + public boolean done() { + return step == 100; + } + } + + //********************** 6.B FrequencyRelayBlock Sequence ********************** + private class FRBlockPerformer extends Behaviour{ + private static final long serialVersionUID = 1L; + private int step = 0; + private String tuName = "noNameSet"; + private String conversationID = "noID"; + private ArrayList agentIdentifiers = new ArrayList(); + + FRBlockPerformer(String _tuName) { + tuName = _tuName; + } + + public void onStart(){ + idCounter++; + conversationID = ("FRBlock-"+idCounter); + } + + public void action() { + switch (step) { + case 0: + System.out.println(this.getAgent().getAID().getLocalName()+"******* Frequency Relay Block started ********"); + //searching for the complete AID of the referenced agent + DFAgentDescription sdSearchTemplate = new DFAgentDescription() ; + sdSearchTemplate.addLanguages(codec.getName()); + sdSearchTemplate.addOntologies(ontology.getName()); + ServiceDescription sd = new ServiceDescription(); + sd.setName(tuName); + sdSearchTemplate.addServices(sd); + //searching the DF for the Agents that can provide the service + try { + DFAgentDescription[] result = DFService.search(myAgent, sdSearchTemplate); + System.out.println("Found the following load agents: "); + //loadAgent = new AID[result.length]; + for(int j = 0; j < result.length; j++){ + agentIdentifiers.add(result[j].getName()); + } + } + catch (FIPAException fe){ + fe.printStackTrace(); + } + sdSearchTemplate.clearAllServices(); + if(agentIdentifiers.size()>0) { + step = 1; + }else { + System.out.println("No Agent can be found under that name"); + step = 99; + } + break; + case 1://waiting for confirmation + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending out FR block to TU: " + tuName +" *******"); + ACLMessage msg = new ACLMessage(ACLMessage.REQUEST); + msg.setOntology(ontology.getName()); + msg.setLanguage(codec.getName()); + msg.setConversationId(conversationID); + AID receiver = new AID(); + for(int j = 0; j < agentIdentifiers.size(); j++){ + receiver = agentIdentifiers.get(j); + } + msg.addReceiver(receiver); + try { + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + FreqRelayBlockRequest newFreqRelayBlockRequest = new FreqRelayBlockRequest(); + newFreqRelayBlockRequest.setTuName(tuName); + Action act = new Action(); + act.setAction(newFreqRelayBlockRequest); //Adding the Action the Agent has to perform + act.setActor(new AID("*", AID.ISGUID)); //Adding an dummy Agent, because the actor field is mandatory (source: http://jade.tilab.com/pipermail/jade-develop/2010q4/016200.html) + cel.add(act); + cm.fillContent(msg, cel); + myAgent.send(msg); + msg.reset(); + step = 2; + + } catch (CodecException | OntologyException e){ + e.printStackTrace(); + step = 99; + } + break; + case 2: + MessageTemplate mt = MessageTemplate.and( + MessageTemplate.MatchOntology(ontology.getName()),MessageTemplate.and( + MessageTemplate.MatchConversationId(conversationID),MessageTemplate.and( + MessageTemplate.MatchLanguage(codec.getName()),MessageTemplate.or( + MessageTemplate.MatchPerformative(ACLMessage.INFORM), + MessageTemplate.MatchPerformative(ACLMessage.FAILURE))))); + msg = receive(mt); //returns the first message of the message queue with the corresponding template + if (msg != null){ + try { + ContentElement ce = null; + if (msg.getPerformative() == ACLMessage.INFORM) { + System.out.println(this.getAgent().getAID().getLocalName()+"******* INFORM Message Received *******"); + ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + if(_pc instanceof FreqRelayBlockInform){ + //********** Inform the VPP-System about the results********** + FreqRelayBlockInform _frb = (FreqRelayBlockInform)_pc; + InterfacePayloadAgentReference payload = new InterfacePayloadAgentReference(_frb.getTuName(),_frb.getAgentName()); + ConsumingRest_VPP putInstance = new ConsumingRest_VPP(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.FRBLOCKINFORM, payload); + step = 99; + } + }else if (msg.getPerformative() == ACLMessage.FAILURE) { + System.out.println(this.getAgent().getAID().getLocalName()+"******* FAILURE Message Received *******"); + ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + if(_pc instanceof FreqRelayBlockFailure){ + //********** Inform the VPP-System about the results********** + FreqRelayBlockFailure _frb = (FreqRelayBlockFailure)_pc; + InterfacePayloadAgentReference payload = new InterfacePayloadAgentReference(_frb.getTuName(),_frb.getAgentName()); + ConsumingRest_VPP putInstance = new ConsumingRest_VPP(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.FRBLOCKFAILURE, payload); + step = 99; + } + } else { //normally can not be the case + step = 99; + } + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + step = 99; + } + }else { + block(); + } + break; + case 99: //final case, here reset() must be called. A reset() in done() would result in a reset() call every cycle, because done() gets called every cycle + reset(); //any Behaviour object that has been executed once, must be reset by calling its reset() method before it can be executed again. + step = 100; + break; + default: + step = 99; + } + } + public boolean done() { + return step == 100; + } + } + + //********************** 6.C FrequencyRelayStatus Sequence ********************** + private class FRStatusPerformer extends Behaviour{ + private static final long serialVersionUID = 1L; + private int step = 0; + private ACLMessage msg; + private String tuName = "NoNameSet"; + private String agentName = "noNameSet"; + + FRStatusPerformer(ACLMessage _msg, String _tuName, String _agentName) { + msg = _msg; + tuName = _tuName; + agentName = _agentName; + } + + public void onStart(){ + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending Frequency Relay Status to VPP *******"); + + } + + public void action() { + switch(step){ + case 0: + ConsumingRest_VPP putInstance = new ConsumingRest_VPP(); + InterfacePayloadAgentReference payload = new InterfacePayloadAgentReference(agentName, tuName); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.FRTRIGGERED, payload); + step = 1; + break; + case 1: + try { + if(VppVariables.freqRelayConfirmTrigger && tuName.equals(VppVariables.freqRelayConfirmTUName)) { + VppVariables.freqRelayConfirmTrigger = false; + VppVariables.resetfreqRelayConfirm(); + ACLMessage reply = msg.createReply(); + reply.setPerformative(ACLMessage.INFORM); + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + FreqRelayStatusConfirm newFreqRelayStatusConfirm = new FreqRelayStatusConfirm(); + newFreqRelayStatusConfirm.setTuName(tuName); + newFreqRelayStatusConfirm.setAgentName(agentName); + cel.add(newFreqRelayStatusConfirm); + cm.fillContent(reply, cel); + myAgent.send(reply); + step = 99; + } + } + catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + break; + case 99: + reset(); + step = 100; + break; + default: + step = 99; + } + } + + public boolean done() { + return step == 100; + } + } + + //********************** 7.A Receive CancelOperation Sequence ********************** + private class ReceiveCancelOperationPerformer extends Behaviour{ + private static final long serialVersionUID = 1L; + private int step = 0; + private String tuName = "noNameSet"; + private String agentName = "noNameSet"; + private String operationReference = "noReferenceSet"; + private ACLMessage msg; + + ReceiveCancelOperationPerformer(ACLMessage _msg) { + msg = _msg; + } + + public void onStart(){ + try { + ContentElement ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + CancelOperationCancel _coi = (CancelOperationCancel)_pc; + tuName = _coi.getTuName(); + agentName = _coi.getAgentName(); + operationReference = _coi.getOperationReference(); + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + } + + public void action() { + switch (step) { + case 0: + ConsumingRest_VPP putInstance = new ConsumingRest_VPP(); + InterfacePayloadAgentReference payload = new InterfacePayloadAgentReference(operationReference, agentName,tuName); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.CANCELOPERATION, payload); + step = 1; + break; + case 1: + if(VppVariables.cancelOperationTrigger && operationReference.equals(VppVariables.cancelOperationReference)) { + VppVariables.cancelOperationTrigger = false; + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending out new INFORM to TU: " + tuName +" *******"); + try { + ACLMessage reply = msg.createReply(); + reply.setPerformative(ACLMessage.INFORM); + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + CancelOperationConfirm newCancelOperationInformReceived = new CancelOperationConfirm(); + newCancelOperationInformReceived.setTuName(tuName); + newCancelOperationInformReceived.setOperationReference(operationReference); + cel.add(newCancelOperationInformReceived); + cm.fillContent(reply, cel); + myAgent.send(reply); + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + step = 99; + } else if(VppVariables.cancelOperationTriggerFail && operationReference.equals(VppVariables.cancelOperationReference)) { + VppVariables.cancelOperationTriggerFail = false; + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending out new INFORM to TU: " + tuName +" *******"); + try { + ACLMessage reply = msg.createReply(); + reply.setPerformative(ACLMessage.FAILURE); + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + CancelOperationFailure newCancelOperationInformReceived = new CancelOperationFailure(); + newCancelOperationInformReceived.setTuName(tuName); + newCancelOperationInformReceived.setOperationReference(operationReference); + cel.add(newCancelOperationInformReceived); + cm.fillContent(reply, cel); + myAgent.send(reply); + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + step = 99; + } + break; + case 99: //final case, here reset() must be called. A reset() in done() would result in a reset() call every cycle, because done() gets called every cycle + reset(); //any Behaviour object that has been executed once, must be reset by calling its reset() method before it can be executed again. + step = 100; + break; + default: + step = 99; + } + } + + public boolean done() { + return step == 100; + } + } + + //********************** 7.B Send CancelOperation Sequence ********************** + private class SendCancelOperationPerformer extends Behaviour { + private static final long serialVersionUID = 1L; + private int step = 0; + private String tuName = "noNameSet"; + private String agentName = "noNameSet"; + private String operationReference = "noIDSet"; + private String conversationID; + private ArrayList tuAgents = new ArrayList(); + + SendCancelOperationPerformer(String _tuName, String _operationReference){ + tuName = _tuName; + operationReference = _operationReference; + } + + public void onStart(){ + System.out.println(this.getAgent().getAID().getLocalName()+"******* OperationCancelBehaviour started ********"); + agentName = getAID().getLocalName(); + conversationID = "cancelOperation"; + } + + public void action(){ + switch(step){ + case 0: + DFAgentDescription sdSearchTemplate = new DFAgentDescription(); + sdSearchTemplate.addLanguages(codec.getName()); + sdSearchTemplate.addOntologies(ontology.getName()); + ServiceDescription sd = new ServiceDescription(); + sd.setName(tuName); + sdSearchTemplate.addServices(sd); + //searching the DF for the Agents that can provide the service + try { + DFAgentDescription[] result = DFService.search(myAgent, sdSearchTemplate); + for(int j = 0; j < result.length; j++){ + tuAgents.add(result[j].getName()); + } + } + catch (FIPAException fe){ + fe.printStackTrace(); + } + sdSearchTemplate.clearAllServices(); + if(tuAgents.size()>0) { + step = 1; + }else { + System.out.println("No Agent can be found under that name"); + step = 99; + } + break; + case 1: + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending out CANCEL (cancel operation) to TU *******"); + + ACLMessage msg = new ACLMessage(ACLMessage.CANCEL); + msg.setOntology(ontology.getName()); + msg.setLanguage(codec.getName()); + msg.setConversationId(conversationID); + AID receiver = new AID(); + for(int j = 0; j < tuAgents.size(); j++){ + receiver = tuAgents.get(j); + } + msg.addReceiver(receiver); + try { + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + CancelOperationCancel newCancelOperationInform = new CancelOperationCancel(); + newCancelOperationInform.setAgentName(agentName); + newCancelOperationInform.setTuName(tuName); + newCancelOperationInform.setOperationReference(operationReference); + cel.add(newCancelOperationInform); + cm.fillContent(msg, cel); + myAgent.send(msg); + msg.reset(); + step = 2; + } catch (CodecException | OntologyException e){ + e.printStackTrace(); + step = 99; + } + break; + case 2: + MessageTemplate mt = MessageTemplate.and( + MessageTemplate.MatchOntology(ontology.getName()),MessageTemplate.and( + MessageTemplate.MatchConversationId(conversationID), + MessageTemplate.MatchLanguage(codec.getName()))); + msg = receive(mt); //returns the first message of the message queue with the corresponding template + if (msg != null){ + ContentElement ce = null; + if (msg.getPerformative() == ACLMessage.INFORM) { + System.out.println(this.getAgent().getAID().getLocalName()+"******* INFORM Message Received *******"); + try { + ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + if(_pc instanceof CancelOperationConfirm){ + //********** Inform the VPP-System about the results********** + CancelOperationConfirm _asir = (CancelOperationConfirm)_pc; + InterfacePayloadAgentReference payload = new InterfacePayloadAgentReference(_asir.getOperationReference(), getAID().getLocalName(),_asir.getTuName() ); + ConsumingRest_VPP putInstance = new ConsumingRest_VPP(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.CANCELOPERATIONCONFIRM, payload); + step = 99; + } + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + }else if(msg.getPerformative() == ACLMessage.FAILURE) { + System.out.println(this.getAgent().getAID().getLocalName()+"******* FAILURE Message Received *******"); + try { + ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + if(_pc instanceof CancelOperationFailure){ + //********** Inform the VPP-System about the results********** + CancelOperationFailure _asir = (CancelOperationFailure)_pc; + InterfacePayloadAgentReference payload = new InterfacePayloadAgentReference(_asir.getOperationReference(), getAID().getLocalName(),_asir.getTuName() ); + ConsumingRest_VPP putInstance = new ConsumingRest_VPP(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.CANCELOPERATIONFAILED, payload); + step = 99; + } + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + }else { + step = 99; + } + }else { + block(); + } + break; + case 99: + reset(); + step = 100; + break; + default: + step = 99; + } + } + + public boolean done(){ + return step == 100; + } + + } + + //********************** 8.A RequestInfos Sequence ********************** + private class RequestInfoPerformer extends Behaviour { + private static final long serialVersionUID = 1L; + private int step = 0; + private String tuName = "noNameSet"; + private String conversationID; + private byte[] infoSet; + private ArrayList tuAgents = new ArrayList(); + private Date expireDate; + + RequestInfoPerformer(String _tuName){ + tuName = _tuName; + } + + public void onStart(){ + System.out.println(this.getAgent().getAID().getLocalName()+"******* RequestInfosBehaviour started ********"); + Date dateStart = new Date(); + long t; + t = dateStart.getTime(); + expireDate=new Date(t + (5 * 60000)); //5min added + conversationID = "requestInfoOperation"; + } + + public void action(){ + switch(step){ + case 0: + DFAgentDescription sdSearchTemplate = new DFAgentDescription(); + sdSearchTemplate.addLanguages(codec.getName()); + sdSearchTemplate.addOntologies(ontology.getName()); + ServiceDescription sd = new ServiceDescription(); + sd.setName(tuName); + sdSearchTemplate.addServices(sd); + //searching the DF for the Agents that can provide the service + try { + DFAgentDescription[] result = DFService.search(myAgent, sdSearchTemplate); + for(int j = 0; j < result.length; j++){ + tuAgents.add(result[j].getName()); + } + } + catch (FIPAException fe){ + fe.printStackTrace(); + } + sdSearchTemplate.clearAllServices(); + if(tuAgents.size()>0) { + step = 1; + }else { + System.out.println("No Agent can be found under that name"); + step = 99; + } + break; + case 1: + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending out REQUEST (Request Infos) to TU *******"); + ACLMessage msg = new ACLMessage(ACLMessage.REQUEST); + msg.setOntology(ontology.getName()); + msg.setLanguage(codec.getName()); + msg.setConversationId(conversationID); + AID receiver = new AID(); + for(int j = 0; j < tuAgents.size(); j++){ + receiver = tuAgents.get(j); + } + msg.addReceiver(receiver); + try { + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + RequestInfoRequest newRequestInfoRequest = new RequestInfoRequest(); + newRequestInfoRequest.setTuName(tuName); + newRequestInfoRequest.setTuName(getAID().getLocalName()); + Action act = new Action(); + act.setAction(newRequestInfoRequest); //Adding the Action the Agent has to perform + act.setActor(new AID("*", AID.ISGUID)); //Adding an dummy Agent, because the actor field is mandatory (source: http://jade.tilab.com/pipermail/jade-develop/2010q4/016200.html) + cel.add(act); + cm.fillContent(msg, cel); + myAgent.send(msg); + msg.reset(); + step = 2; + } catch (CodecException | OntologyException e){ + e.printStackTrace(); + step = 99; + } + break; + case 2: + MessageTemplate mt = MessageTemplate.and( + MessageTemplate.MatchOntology(ontology.getName()),MessageTemplate.and( + MessageTemplate.MatchConversationId(conversationID), + MessageTemplate.MatchLanguage(codec.getName()))); + msg = receive(mt); //returns the first message of the message queue with the corresponding template + if (msg != null){ + ContentElement ce = null; + if (msg.getPerformative() == ACLMessage.INFORM) { + System.out.println(this.getAgent().getAID().getLocalName()+"******* INFORM Message Received *******"); + try { + ce = getContentManager().extractContent(msg); + Predicate _pc = (Predicate) ce; + if(_pc instanceof RequestInfoInform){ + //********** Inform the VPP-System about the results********** + RequestInfoInform _rii = (RequestInfoInform)_pc; + ConsumingRest_VPP putInstance = new ConsumingRest_VPP(); + infoSet = _rii.getInfoSet(); + putInstance.putNodeRed(Addresses.URL_NODERED, PutVariable.REQUESTINFOINFORM, infoSet); + step = 99; + } + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + }else if(msg.getPerformative() == ACLMessage.FAILURE) { + step = 99; + }else{ + step = 99; + } + }else { + Date dateNow = new Date(); + if(dateNow.after(expireDate)) { + step = 99; + } + } + break; + case 99: + reset(); + step = 100; + break; + default: + step = 99; + } + + } + public boolean done() { + return step == 100; + } + } + + //********************** 8.B RequestInfosResponse Sequence ********************** + private class RequestInfoResponsePerformer extends OneShotBehaviour{ + private static final long serialVersionUID = 1L; + private String tuName = "noNameSet"; + private byte[] infoSet; + private ACLMessage msg; + + RequestInfoResponsePerformer(ACLMessage _msg) { + msg = _msg; + } + + public void onStart(){ + try { + ContentElement ce = getContentManager().extractContent(msg); + Action _ac = (Action) ce; + if(_ac.getAction() instanceof RequestInfoRequest){ + RequestInfoRequest _rir = (RequestInfoRequest)_ac.getAction(); + tuName = _rir.getTuName(); + } + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + } + + public void action() { + ConsumingRest_VPP getInstance = new ConsumingRest_VPP(); + infoSet = getInstance.getNodeRed(Addresses.URL_NODERED, PutVariable.REQUESTINFO); + System.out.println(this.getAgent().getAID().getLocalName()+"******* Sending out new RequestInfo *******"); + try { + ACLMessage reply = msg.createReply(); + reply.setPerformative(ACLMessage.INFORM); + ContentManager cm = myAgent.getContentManager(); + ContentElementList cel = new ContentElementList(); + RequestInfoInform newRequestInfoInform = new RequestInfoInform(); + newRequestInfoInform.setTuName(tuName); + newRequestInfoInform.setAgentName(getAID().getLocalName()); + newRequestInfoInform.setInfoSet(infoSet); + cel.add(newRequestInfoInform); + cm.fillContent(reply, cel); + myAgent.send(reply); + } catch (CodecException | OntologyException e) { + e.printStackTrace(); + } + } + + } + +} \ No newline at end of file diff --git a/JADE_VPP/src/main/java/mas/JADE_VPP/ontology/CancelOperationCancel.java b/JADE_VPP/src/main/java/mas/JADE_VPP/ontology/CancelOperationCancel.java new file mode 100644 index 0000000..23f1e8c --- /dev/null +++ b/JADE_VPP/src/main/java/mas/JADE_VPP/ontology/CancelOperationCancel.java @@ -0,0 +1,31 @@ +package mas.JADE_VPP.ontology; + +import jade.content.Predicate; + +public class CancelOperationCancel implements Predicate { + private static final long serialVersionUID = 1L; + private String agentName; + private String tuName; + private String operationReference; + + public String getAgentName() { + return agentName; + } + public void setAgentName(String agentName) { + this.agentName = agentName; + } + public String getTuName() { + return tuName; + } + public void setTuName(String tuName) { + this.tuName = tuName; + } + public String getOperationReference() { + return operationReference; + } + public void setOperationReference(String operationReference) { + this.operationReference = operationReference; + } + + +} diff --git a/JADE_VPP/src/main/java/mas/JADE_VPP/ontology/VPP_DR_Ontology.java b/JADE_VPP/src/main/java/mas/JADE_VPP/ontology/VPP_DR_Ontology.java index 0c555d0..31b301a 100644 --- a/JADE_VPP/src/main/java/mas/JADE_VPP/ontology/VPP_DR_Ontology.java +++ b/JADE_VPP/src/main/java/mas/JADE_VPP/ontology/VPP_DR_Ontology.java @@ -71,10 +71,10 @@ public class VPP_DR_Ontology extends Ontology{ public static final String FREQRELAY_STATUSINFORM = "FreqRelayStatusInform"; public static final String FREQRELAY_STATUSCONFIRM = "FreqRelayStatusConfirm"; - public static final String CANCELOPERATION_CANCEL = "CancelOperation"; + public static final String CANCELOPERATION_CANCEL = "CancelOperationCancel"; public static final String CANCELOPERATION_CONFIRM = "CancelOperationConfirm"; public static final String CANCELOPERATION_FAILURE = "CancelOperationFailure"; - public static final String CANCELOPERATION_REFRENCE = "operationReference"; + public static final String CANCELOPERATION_REFERENCE = "operationReference"; public static final String REQUESTINFO_INFORM = "RequestInfoInform"; public static final String REQUESTINFO_DATASET = "infoSet"; @@ -141,7 +141,7 @@ private VPP_DR_Ontology() { add(new PredicateSchema (FREQRELAY_BLOCKFAILURE),FreqRelayBlockFailure.class); add(new PredicateSchema (FREQRELAY_STATUSINFORM),FreqRelayStatusInform.class); add(new PredicateSchema (FREQRELAY_STATUSCONFIRM),FreqRelayStatusConfirm.class); - add(new PredicateSchema (CANCELOPERATION_CANCEL),CancelOperation.class); + add(new PredicateSchema (CANCELOPERATION_CANCEL),CancelOperationCancel.class); add(new PredicateSchema (CANCELOPERATION_CONFIRM),CancelOperationConfirm.class); add(new PredicateSchema (CANCELOPERATION_FAILURE),CancelOperationFailure.class); add(new PredicateSchema (REQUESTINFO_INFORM),RequestInfoInform.class); @@ -182,13 +182,13 @@ private VPP_DR_Ontology() { // *************** PREDICATES PredicateSchema ps = (PredicateSchema) (getSchema(CFPSCHEDULINGSEQUENCE_REFUSE)); - ps.add(TUNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.OPTIONAL); - ps.add(AGENTNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.OPTIONAL); + ps.add(TUNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); + ps.add(AGENTNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); ps = (PredicateSchema) (getSchema(CFPSCHEDULINGSEQUENCE_PROPOSE)); ps.add(CFPSCHEDULINGSEQUENCE_SCHEDULINGPLAN, (PrimitiveSchema) getSchema(BasicOntology.BYTE_SEQUENCE), ObjectSchema.MANDATORY); - ps.add(TUNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.OPTIONAL); - ps.add(AGENTNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.OPTIONAL); + ps.add(TUNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); + ps.add(AGENTNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); ps = (PredicateSchema) (getSchema(CFPSCHEDULINGSEQUENCE_ACCEPTPROPOSAL)); ps.add(CFPSCHEDULINGSEQUENCE_SCHEDULINGSTART, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); @@ -197,20 +197,20 @@ private VPP_DR_Ontology() { ps = (PredicateSchema) (getSchema(CFPSCHEDULINGSEQUENCE_REJECTPROPOSAL)); ps = (PredicateSchema) (getSchema(CFPSCHEDULINGSEQUENCE_SCHEDULINGDONE)); - ps.add(TUNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.OPTIONAL); - ps.add(AGENTNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.OPTIONAL); + ps.add(TUNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); + ps.add(AGENTNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); ps = (PredicateSchema) (getSchema(CFPSCHEDULINGSEQUENCE_SCHEDULINGFAILURE)); - ps.add(TUNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.OPTIONAL); - ps.add(AGENTNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.OPTIONAL); + ps.add(TUNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); + ps.add(AGENTNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); ps = (PredicateSchema) (getSchema(TUCONTROLSEQUENCE_FAILURE)); ps.add(TUNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); ps.add(AGENTNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); ps = (PredicateSchema) (getSchema(BALANCINGSEQUENCE_AGREE)); - ps.add(AGENTNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); ps.add(TUNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); + ps.add(AGENTNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); ps = (PredicateSchema) (getSchema(BALANCINGSEQUENCE_REFUSE)); ps.add(AGENTNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); @@ -228,15 +228,15 @@ private VPP_DR_Ontology() { ps = (PredicateSchema) (getSchema(CANCELOPERATION_CANCEL)); ps.add(TUNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); ps.add(AGENTNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); - ps.add(CANCELOPERATION_REFRENCE, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); + ps.add(CANCELOPERATION_REFERENCE, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); ps = (PredicateSchema) (getSchema(CANCELOPERATION_CONFIRM)); ps.add(TUNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); - ps.add(CANCELOPERATION_REFRENCE, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); + ps.add(CANCELOPERATION_REFERENCE, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); ps = (PredicateSchema) (getSchema(CANCELOPERATION_FAILURE)); ps.add(TUNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); - ps.add(CANCELOPERATION_REFRENCE, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); + ps.add(CANCELOPERATION_REFERENCE, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY); ps = (PredicateSchema) (getSchema(ACCOUNTINGSEQUENCE_INFORM)); ps.add(AGENTNAME, (PrimitiveSchema) getSchema(BasicOntology.STRING), ObjectSchema.MANDATORY);