diff --git a/README.md b/README.md index 999b345..319cae3 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ The service of decision support system is inferring knowledge from the available data of the greenhouse, the user input, and historical data what a user/the gardener should do & which action a gardener should execute to be successful with the growing process of the plants. These actions are called goals. The list of inferred active goals is offered to other services of the system. - The approach of the DSS is based on three MainExe concepts: + The approach of the DSS is based on three eu.hsrw.ias.ggd.MainExe concepts: **Fact knowledge (Knowledgebase)** diff --git a/src/main/java/ApiCall.java b/src/main/java/ApiCall.java deleted file mode 100644 index 1504d8f..0000000 --- a/src/main/java/ApiCall.java +++ /dev/null @@ -1,64 +0,0 @@ -import Credentials.UserCredentials; -import Files.WriteFile; -import JSON_Unpack.ReadSensors4Partner; -import Knowledgebase.SensorReadings; -import org.json.JSONArray; -import org.json.JSONObject; - -import java.lang.reflect.Array; -import java.util.ArrayList; - -public class ApiCall { - private static String token = "0"; - private static String oldId = "1"; - - - public static String FetchDataFromApi(String extension) throws Exception { - UserCredentials userCredentials = new UserCredentials(); - final String username = userCredentials.getUsername(); - final String password = userCredentials.getPassword(); - final String homeRoot = userCredentials.getHomeRoot(); - String outputToken; - WriteFile writingIntoFile = new WriteFile(); - - if (token.equals(oldId)) { - //do nothing - } else { - try { - outputToken = SecurityToken.OnCallMethod(username, password); - JSONObject obj = new JSONObject(outputToken); - token = obj.getString("id"); - - writingIntoFile.WriteFile(homeRoot+"securityToken.txt", token); - oldId = token; - } catch (Exception e) { - e.printStackTrace(); - } - } - ReadSensors4Partner readSensors4Partner = new ReadSensors4Partner(); - SensorReadings sensorReadings = new SensorReadings(); - String outputValue = (readSensors4Partner.FetchSensor4Box("Germes",token)); - System.out.println(outputValue); - String outputValueFixed=outputValue.replace("[[","[") +outputValue.replace("[{","{")+outputValue.replace("}]","}")+ outputValue.replace("]]","]"); - System.out.println(outputValueFixed); - JSONArray jsonArray = new JSONArray(outputValueFixed); - System.out.println(jsonArray.length()); - for(int j=0; j FetchSensor4Box(String companyName, String token) throws Exception { switch (companyName) { case "Germes": - return String.valueOf(SensorRead("Germes", token)); + return SensorRead("Germes", token); case "Heufs": - return String.valueOf(SensorRead("Heufs", token)); + return SensorRead("Heufs", token); case "Jacobs": - return String.valueOf(SensorRead("Jacobs", token)); + return SensorRead("Jacobs", token); case "Nica": - return String.valueOf(SensorRead("Nica", token)); + return SensorRead("Nica", token); } return null; } @@ -39,23 +39,27 @@ public class ReadSensors4Partner { String outputBox = boxCall4Partner.FetchBox(cName,token); String outputBoxFixed=outputBox.replace("[[","[") + outputBox.replace("]]","]");// To convert in Jsonarray we need to remove the array list brackets JSONArray jsonArray = new JSONArray(outputBoxFixed); - System.out.println("The working array:"+jsonArray); + System.out.println("FINAL WORKING ARRAY:"+jsonArray); ArrayList list=new ArrayList(); int loopTerminator = 0; char par = '"'; + for(int j=0; j currMap= new HashMap<>(); +// +// public static final void main(String[] args) { +// try { +// KieServices ks = KieServices.Factory.get(); +// KieContainer kContainer = ks.getKieClasspathContainer(); +////Get the session named kseesion-rule that we defined in kmodule.xml above. +////Also by default the session returned is always stateful. +// KieSession kSession = kContainer.newKieSession("ksession-rule"); +// +// currMap.put("asd",12.2); +// setCurrMap("soilMoisture",10.1); +// currMap.put("soilConductivity",0.14); +// setCurrMap set= new SetCurrMap(); +// se +// +// +// FactHandle fact1; +// +// fact1 = kSession.insert(currMap); +// System.out.println(fact1); +// kSession.fireAllRules(); +// +//// System.out.println("The discount for the jewellery product " +//// + sensorReadings.getSensorTag() + " is " + sensorReadings.getValue()); +// +// } catch (Throwable t) { +// t.printStackTrace(); +// } +// } +// public Map getCurrMap() { +// return currMap; +// } +// +// public void setCurrMap(Map currMap) { +// this.currMap = currMap; +// } +// +//} diff --git a/src/main/java/Knowledgebase/SensorMap.java b/src/main/java/Knowledgebase/SensorMap.java new file mode 100644 index 0000000..3145470 --- /dev/null +++ b/src/main/java/Knowledgebase/SensorMap.java @@ -0,0 +1,17 @@ +package Knowledgebase; + +import java.util.HashMap; + +public class SensorMap { + private HashMap sensors = new HashMap(); + + public HashMap getSensors() { + return sensors; + } + + public void setSensors(HashMap sensors) { + this.sensors = sensors; + } + + +} diff --git a/src/main/java/Knowledgebase/SensorReadings.java b/src/main/java/Knowledgebase/SensorReadings.java deleted file mode 100644 index 9ff4a21..0000000 --- a/src/main/java/Knowledgebase/SensorReadings.java +++ /dev/null @@ -1,22 +0,0 @@ -package Knowledgebase; - -public class SensorReadings { - private String sensorTag; - private double value; - - public String getSensorTag() { - return sensorTag; - } - - public void setSensorTag(String sensorTag) { - this.sensorTag = sensorTag; - } - - public double getValue() { - return value; - } - - public void setValue(double value) { - this.value = value; - } -} diff --git a/src/main/java/ScheduledTask.java b/src/main/java/ScheduledTask.java deleted file mode 100644 index 9359cd4..0000000 --- a/src/main/java/ScheduledTask.java +++ /dev/null @@ -1,46 +0,0 @@ -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; - -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.util.ArrayList; -import java.util.TimerTask; -import java.util.Date; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeoutException; - -// Create a class extends with TimerTask -public class ScheduledTask extends TimerTask { - - Date now; // to display current time - - // Add your task here - public void run() { - // Display current time - now = new Date(); // initialize date - System.out.println("Time is :" + now); - String outputDevices = null; - String devicesExtension = "devices"; - String sensorExtension = "sensors"; //read and readMultiple - try { - outputDevices = ApiCall.FetchDataFromApi(devicesExtension); -// outputDevices = ApiCall.FetchDataFromApi(sensorExtension); - - } catch (InterruptedException e) { - e.printStackTrace(); - } catch (ExecutionException e) { - e.printStackTrace(); - } catch (TimeoutException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); - } - System.out.println("Output :" + outputDevices); // Display current output - - - - } -} \ No newline at end of file diff --git a/src/main/java/eu/hsrw/ias/ggd/ApiCall.java b/src/main/java/eu/hsrw/ias/ggd/ApiCall.java new file mode 100644 index 0000000..1a33e52 --- /dev/null +++ b/src/main/java/eu/hsrw/ias/ggd/ApiCall.java @@ -0,0 +1,98 @@ +package eu.hsrw.ias.ggd; + +import Credentials.UserCredentials; +import Files.WriteFile; +import JSON_Unpack.ReadSensors4Partner; +import org.json.JSONArray; +import org.json.JSONObject; + +import javax.swing.*; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; + +public class ApiCall { + private static String token = "0"; + private static String oldId = "1"; + + + public static HashMap FetchDataFromApi() throws Exception { + + + UserCredentials userCredentials = new UserCredentials(); + final String username = userCredentials.getUsername(); + final String password = userCredentials.getPassword(); + final String homeRoot = userCredentials.getHomeRoot(); + String outputToken; + WriteFile writingIntoFile = new WriteFile(); + Date now; // to display current time + now = new Date(); + if (token.equals(oldId)) { + //do nothing + } else { + try { + outputToken = SecurityToken.OnCallMethod(username, password); + JSONObject obj = new JSONObject(outputToken); + token = obj.getString("id"); + + writingIntoFile.WriteFile(homeRoot+"securityToken.txt", token); + oldId = token; + } catch (Exception e) { + e.printStackTrace(); + } + } + ReadSensors4Partner readSensors4Partner = new ReadSensors4Partner(); + ArrayList outputValue = (readSensors4Partner.FetchSensor4Box("Germes",token)); + String workingArray = String.valueOf(outputValue).replace("[{","{").replace("}]","}"); + + JSONArray jsonArray = new JSONArray(workingArray); + + HashMap sensors = new HashMap(); + for(int j = 0; j listOfCompanies = new ArrayList<>(); +// String germesOutput = FetchData("Germes",token); +// listOfCompanies.add(FetchData(c1,token)); +// listOfCompanies.add(FetchData("Heufs",token)); + + JOptionPane.showMessageDialog(null,sensors,"Germes sensors"+now,JOptionPane.INFORMATION_MESSAGE); + + return sensors; + + } + +// public static void FetchData(String companyName, String token){ +// ReadSensors4Partner readSensors4Partner = new ReadSensors4Partner(); +// ArrayList outputValue = null; +// try { +// outputValue = (readSensors4Partner.FetchSensor4Box(companyName,token)); +// } catch (Exception e) { +// e.printStackTrace(); +// } +// String workingArray = String.valueOf(outputValue).replace("[{","{").replace("}]","}"); +// JSONArray jsonArray = new JSONArray(workingArray); +// +// HashMap sensors = new HashMap(); +// for(int j = 0; j outputDevices ; + + // Add your task here + public void run() { + // Display current time + now = new Date(); // initialize date + System.out.println("Time is :" + now); + + try { + outputDevices = ApiCall.FetchDataFromApi(); + System.out.println("Final Size"+outputDevices.size()+outputDevices); + KieServices ks = KieServices.Factory.get(); + KieContainer kContainer = ks.getKieClasspathContainer(); + //Get the session named kseesion-rule that we defined in kmodule.xml above. + //Also by default the session returned is always stateful. + KieSession kSession = kContainer.newKieSession("ksession-rule"); + FactHandle fact1; + fact1 = kSession.insert(outputDevices); + System.out.println(fact1); + kSession.fireAllRules(); + + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } catch (TimeoutException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + + + } + +} \ No newline at end of file diff --git a/src/main/java/SecurityToken.java b/src/main/java/eu/hsrw/ias/ggd/SecurityToken.java similarity index 96% rename from src/main/java/SecurityToken.java rename to src/main/java/eu/hsrw/ias/ggd/SecurityToken.java index 1176b8d..65691a7 100644 --- a/src/main/java/SecurityToken.java +++ b/src/main/java/eu/hsrw/ias/ggd/SecurityToken.java @@ -1,3 +1,5 @@ +package eu.hsrw.ias.ggd; + import HttpCall.HttpPost; import java.util.concurrent.ExecutionException; @@ -12,9 +14,6 @@ public class SecurityToken { HttpPost httpMethodPost = new HttpPost(); return httpMethodPost.HttpCallPost(baseURL, body); - - - } diff --git a/src/main/resources/rules/Battery.drl b/src/main/resources/rules/Battery.drl new file mode 100644 index 0000000..800cb9d --- /dev/null +++ b/src/main/resources/rules/Battery.drl @@ -0,0 +1,18 @@ +import java.util.List; +import java.util.HashMap; +import java.util.Map; +import java.util.Iterator; +import eu.hsrw.ias.ggd.ScheduledTask +import javax.swing.JOptionPane; + +rule "Low Battery Level" + +when + $map : Map (this["battery"]<5.5) + + then + System.out.println("Low Battery : "+$map.get("battery")+"! Please swap the battery soon"); + JOptionPane.showMessageDialog(null,"Low Battery : "+$map.get("battery")+"! Please swap the battery soon","Low Battery Level",JOptionPane.INFORMATION_MESSAGE); + + +end \ No newline at end of file diff --git a/src/main/resources/rules/SoilConductivity.drl b/src/main/resources/rules/SoilConductivity.drl new file mode 100644 index 0000000..93f696e --- /dev/null +++ b/src/main/resources/rules/SoilConductivity.drl @@ -0,0 +1,21 @@ +import java.util.List; +import java.util.HashMap; +import java.util.Map; +import java.util.Iterator; +import eu.hsrw.ias.ggd.ScheduledTask; +import javax.swing.JOptionPane; + +rule "Optimal EC levels in the soil" + +when + $map : Map (this["soilConductivity"]<0.57 && this["soilConductivity"]>0.11) + + then + System.out.println("Optimal EC levels in the soil : "+$map.get("soilConductivity")); + JOptionPane.showMessageDialog(null,"Optimal EC levels in the soil : "+$map.get("soilConductivity"),"Soil Conductivity",JOptionPane.INFORMATION_MESSAGE); + +end + + + + diff --git a/src/main/resources/rules/SoilMoisture.drl b/src/main/resources/rules/SoilMoisture.drl new file mode 100644 index 0000000..a3828c0 --- /dev/null +++ b/src/main/resources/rules/SoilMoisture.drl @@ -0,0 +1,29 @@ +import java.util.List; +import java.util.HashMap; +import java.util.Map; +import java.util.Iterator; +import eu.hsrw.ias.ggd.ScheduledTask; +import javax.swing.JOptionPane; +rule "Dry Soil Moisture" + +when + $map : Map (this["soilMoisture"]<40) + + then + System.out.println("It is recommended to have a Soil Moisture higher than : "+$map.get("soilMoisture")); + JOptionPane.showMessageDialog(null,"It is recommended to have a Soil Moisture higher than : "+$map.get("soilMoisture"),"Dry Soil Moisture",JOptionPane.INFORMATION_MESSAGE); + +end + +rule "Wet Soil Mositure" +when + $map : Map (this["soilMoisture"]>70) + + then + System.out.println("Be careful the soil is extremly wet :"+$map.get("soilMoisture")+ "please dont apply water"); + JOptionPane.showMessageDialog(null,"Be careful the soil is extremly wet :"+$map.get("soilMoisture")+ "please dont apply water","Wet Soil Moisture",JOptionPane.INFORMATION_MESSAGE); + + +end + + diff --git a/src/main/resources/rules/TestRule.drl b/src/main/resources/rules/TestRule.drl deleted file mode 100644 index 48ffa36..0000000 --- a/src/main/resources/rules/TestRule.drl +++ /dev/null @@ -1,14 +0,0 @@ -import Knowledgebase.Product - -rule "Offer for Diamond" - when - productObject: Product(type=="diamond") - then - productObject.setDiscount(15); - end -rule "Offer for Gold" - when - productObject: Product(type=="gold") - then - productObject.setDiscount(10); - end