From e357d5358444c5332486bd4f36e114d5f1d56d7f Mon Sep 17 00:00:00 2001 From: "kevin.shehu" Date: Thu, 17 Jun 2021 10:29:11 +0200 Subject: [PATCH] FINALIZED : Json_Unpack ADD : KnowledgeBase TODO : Rule Engine --- pom.xml | 74 ++++-- src/main/java/Data/securityToken.txt | 2 +- src/main/java/JSON2RDF/JSON2RDF.java | 154 +++++------ .../java/JSON2RDF/JsonStreamRDFWriter.java | 248 +++++++++--------- .../java/JSON_Unpack/ReadSensors4Partner.java | 2 +- src/main/java/Knowledgebase/DroolsTest.java | 34 +++ src/main/java/Knowledgebase/Plants.java | 31 +++ src/main/java/MainExe.java | 2 +- src/main/java/RuleEngine/KalunaRules.java | 5 - src/main/java/Sensors/Sensors.java | 24 -- src/main/resources/META-INF/kmodule.xml | 6 + src/main/resources/RuleEngine/TestRule.drl | 14 + target/classes/RuleEngine/TestRule.drl | 14 + 13 files changed, 359 insertions(+), 251 deletions(-) create mode 100644 src/main/java/Knowledgebase/DroolsTest.java create mode 100644 src/main/java/Knowledgebase/Plants.java delete mode 100644 src/main/java/RuleEngine/KalunaRules.java delete mode 100644 src/main/java/Sensors/Sensors.java create mode 100644 src/main/resources/META-INF/kmodule.xml diff --git a/pom.xml b/pom.xml index eec0fbf..e910fb5 100644 --- a/pom.xml +++ b/pom.xml @@ -19,24 +19,10 @@ + + 7.48.0.Final + - - org.apache.jena - apache-jena-libs - pom - 3.16.0 - - - org.apache.jena - jena-fuseki-war - war - 3.16.0 - - - org.apache.jena - jena-fuseki-main - 3.16.0 - info.picocli picocli @@ -60,7 +46,7 @@ - + org.drools drools-core @@ -78,6 +64,14 @@ drools-decisiontables 7.48.0.Final + + + org.drools + kiebase-inclusion + 7.48.0.Final + runtime + + org.kie @@ -85,11 +79,55 @@ 7.48.0.Final provided + + + org.kie + kie-ci + 7.48.0.Final + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/Data/securityToken.txt b/src/main/java/Data/securityToken.txt index c3a8a1e..33696d3 100644 --- a/src/main/java/Data/securityToken.txt +++ b/src/main/java/Data/securityToken.txt @@ -1 +1 @@ -OnomznAUvxd8lmIUu7FRiIkfYdoUCGN1x84B4AjS9yE4TW3xLpmh2rOxlBtBD0IJ \ No newline at end of file +PlixJiy6sktkQZcj1WJVqLZCR4abCgnNaMlG2DYGZ2ga75aOA00E9jTzBmSC88tw \ No newline at end of file diff --git a/src/main/java/JSON2RDF/JSON2RDF.java b/src/main/java/JSON2RDF/JSON2RDF.java index 3981edd..829a935 100644 --- a/src/main/java/JSON2RDF/JSON2RDF.java +++ b/src/main/java/JSON2RDF/JSON2RDF.java @@ -1,77 +1,77 @@ -package JSON2RDF; - -import java.io.*; -import java.net.URI; -import java.net.URISyntaxException; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; - - -import org.json.simple.JSONArray; -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; - -import org.apache.jena.riot.system.StreamRDF; -import org.apache.jena.riot.system.StreamRDFLib; -import picocli.CommandLine; - -@CommandLine.Command(name = "json2rdf") -public class JSON2RDF { - private final InputStream jsonIn; - private final OutputStream rdfOut; - - - @CommandLine.Parameters(paramLabel = "https://localhost/" , index = "0", description = "Base URI of the RDF output data") - private URI baseURI; - - { - try { - baseURI = new URI("https://localhost/"); - } catch (URISyntaxException e) { - e.printStackTrace(); - } - } - -// @CommandLine.Parameters(paramLabel = "D:\\WORK\\GGD\\src\\MainExe\\java\\Data", description = "json file") - @CommandLine.Option(names = { "--input-charset" }, description = "Input charset (default: ${DEFAULT-VALUE})") - private final Charset inputCharset = StandardCharsets.UTF_8; - - @CommandLine.Option(names = { "--output-charset" }, description = "Output charset (default: ${DEFAULT-VALUE})") - private final Charset outputCharset = StandardCharsets.UTF_8; - - public static void main(String[] args) throws IOException - { - JSON2RDF json2rdf = new JSON2RDF(System.in, System.out); - - try - { - CommandLine.ParseResult parseResult = new CommandLine(json2rdf).parseArgs(args); - if (!CommandLine.printHelpIfRequested(parseResult)) json2rdf.convert(); - } - catch (CommandLine.ParameterException ex) - { // command line arguments could not be parsed - System.err.println(ex.getMessage()); - ex.getCommandLine().usage(System.err); - } - } - - public JSON2RDF(InputStream csvIn, OutputStream rdfOut) { - - - this.jsonIn = csvIn; - this.rdfOut = rdfOut; - } - - - public void convert() throws IOException { - if (jsonIn.available() == 0) throw new IllegalStateException("JSON input not provided"); - - try (Reader reader = new BufferedReader(new InputStreamReader(jsonIn, inputCharset))) - { - StreamRDF rdfStream = StreamRDFLib.writer(new BufferedWriter(new OutputStreamWriter(rdfOut, outputCharset))); - new JsonStreamRDFWriter(reader, rdfStream, baseURI.toString()).convert(); - } - } - -} - +//package JSON2RDF; +// +//import java.io.*; +//import java.net.URI; +//import java.net.URISyntaxException; +//import java.nio.charset.Charset; +//import java.nio.charset.StandardCharsets; +// +// +//import org.json.simple.JSONArray; +//import org.json.simple.parser.JSONParser; +//import org.json.simple.parser.ParseException; +// +//import org.apache.jena.riot.system.StreamRDF; +//import org.apache.jena.riot.system.StreamRDFLib; +//import picocli.CommandLine; +// +//@CommandLine.Command(name = "json2rdf") +//public class JSON2RDF { +// private final InputStream jsonIn; +// private final OutputStream rdfOut; +// +// +// @CommandLine.Parameters(paramLabel = "https://localhost/" , index = "0", description = "Base URI of the RDF output data") +// private URI baseURI; +// +// { +// try { +// baseURI = new URI("https://localhost/"); +// } catch (URISyntaxException e) { +// e.printStackTrace(); +// } +// } +// +//// @CommandLine.Parameters(paramLabel = "D:\\WORK\\GGD\\src\\MainExe\\java\\Data", description = "json file") +// @CommandLine.Option(names = { "--input-charset" }, description = "Input charset (default: ${DEFAULT-VALUE})") +// private final Charset inputCharset = StandardCharsets.UTF_8; +// +// @CommandLine.Option(names = { "--output-charset" }, description = "Output charset (default: ${DEFAULT-VALUE})") +// private final Charset outputCharset = StandardCharsets.UTF_8; +// +// public static void main(String[] args) throws IOException +// { +// JSON2RDF json2rdf = new JSON2RDF(System.in, System.out); +// +// try +// { +// CommandLine.ParseResult parseResult = new CommandLine(json2rdf).parseArgs(args); +// if (!CommandLine.printHelpIfRequested(parseResult)) json2rdf.convert(); +// } +// catch (CommandLine.ParameterException ex) +// { // command line arguments could not be parsed +// System.err.println(ex.getMessage()); +// ex.getCommandLine().usage(System.err); +// } +// } +// +// public JSON2RDF(InputStream csvIn, OutputStream rdfOut) { +// +// +// this.jsonIn = csvIn; +// this.rdfOut = rdfOut; +// } +// +// +// public void convert() throws IOException { +// if (jsonIn.available() == 0) throw new IllegalStateException("JSON input not provided"); +// +// try (Reader reader = new BufferedReader(new InputStreamReader(jsonIn, inputCharset))) +// { +// StreamRDF rdfStream = StreamRDFLib.writer(new BufferedWriter(new OutputStreamWriter(rdfOut, outputCharset))); +// new JsonStreamRDFWriter(reader, rdfStream, baseURI.toString()).convert(); +// } +// } +// +//} +// diff --git a/src/main/java/JSON2RDF/JsonStreamRDFWriter.java b/src/main/java/JSON2RDF/JsonStreamRDFWriter.java index d75e8df..b92ba43 100644 --- a/src/main/java/JSON2RDF/JsonStreamRDFWriter.java +++ b/src/main/java/JSON2RDF/JsonStreamRDFWriter.java @@ -1,124 +1,124 @@ -package JSON2RDF; - -import java.io.InputStream; -import java.io.Reader; -import java.util.ArrayDeque; -import java.util.Deque; -import java.util.HashMap; -import java.util.Map; -import javax.json.Json; -import javax.json.stream.JsonParser; -import org.apache.jena.datatypes.xsd.XSDDatatype; -import org.apache.jena.graph.Node; -import org.apache.jena.graph.NodeFactory; -import org.apache.jena.graph.Triple; -import org.apache.jena.riot.system.IRIResolver; -import org.apache.jena.riot.system.StreamRDF; - - -public class JsonStreamRDFWriter -{ - - private final JsonParser parser; - private final StreamRDF rdfStream; - private final IRIResolver iriResolver; - - public JsonStreamRDFWriter(Reader reader, StreamRDF rdfStream, String baseURI) - { - this(Json.createParser(reader), rdfStream, baseURI); - } - - public JsonStreamRDFWriter(InputStream is, StreamRDF rdfStream, String baseURI) - { - this(Json.createParser(is), rdfStream, baseURI); - } - - public JsonStreamRDFWriter(JsonParser parser, StreamRDF rdfStream, String baseURI) - { - this.parser = parser; - this.rdfStream = rdfStream; - this.iriResolver = IRIResolver.create(baseURI); - } - - public void convert() - { - getStreamRDF().start(); - - write(getParser(), getStreamRDF(), getIRIResolver()); - - getStreamRDF().finish(); - } - - public static void write(JsonParser parser, StreamRDF rdfStream, IRIResolver iriResolver) - { - Deque subjectStack = new ArrayDeque<>(); - Map arrayProperties = new HashMap<>(); - - Node property = null; - while (parser.hasNext()) - { - JsonParser.Event event = parser.next(); - - switch (event) - { - case START_ARRAY: - if (!subjectStack.isEmpty() && property != null) arrayProperties.put(subjectStack.getLast(), property); - break; - case END_ARRAY: - if (!subjectStack.isEmpty()) arrayProperties.remove(subjectStack.getLast()); - break; - case START_OBJECT: - Node subject = NodeFactory.createBlankNode(); - // add triple with current array property, if any - if (property != null && !subjectStack.isEmpty()) rdfStream.triple(new Triple(subjectStack.getLast(), property, subject)); - subjectStack.addLast(subject); - break; - case END_OBJECT: - subjectStack.removeLast(); - // restore previous array property, if there was any - if (!subjectStack.isEmpty() && arrayProperties.containsKey(subjectStack.getLast())) property = arrayProperties.get(subjectStack.getLast()); - break; - case VALUE_FALSE: - rdfStream.triple(new Triple(subjectStack.getLast(), property, NodeFactory.createLiteralByValue(Boolean.FALSE, XSDDatatype.XSDboolean))); - break; - case VALUE_TRUE: - rdfStream.triple(new Triple(subjectStack.getLast(), property, NodeFactory.createLiteralByValue(Boolean.TRUE, XSDDatatype.XSDboolean))); - break; - case KEY_NAME: - property = NodeFactory.createURI(iriResolver.resolveToString("#" + parser.getString())); - break; - case VALUE_STRING: - if (property != null) rdfStream.triple(new Triple(subjectStack.getLast(), property, NodeFactory.createLiteral(parser.getString()))); - break; - case VALUE_NUMBER: - try - { - rdfStream.triple(new Triple(subjectStack.getLast(), property,NodeFactory.createLiteralByValue(Integer.valueOf(parser.getString()), XSDDatatype.XSDint))); - } - catch (NumberFormatException ex) - { - rdfStream.triple(new Triple(subjectStack.getLast(), property,NodeFactory.createLiteralByValue(Float.valueOf(parser.getString()), XSDDatatype.XSDfloat))); - } - break; - case VALUE_NULL: - break; - } - } - } - - protected JsonParser getParser() - { - return parser; - } - - protected StreamRDF getStreamRDF() - { - return rdfStream; - } - - protected IRIResolver getIRIResolver() - { - return iriResolver; - } - -} \ No newline at end of file +//package JSON2RDF; +// +//import java.io.InputStream; +//import java.io.Reader; +//import java.util.ArrayDeque; +//import java.util.Deque; +//import java.util.HashMap; +//import java.util.Map; +//import javax.json.Json; +//import javax.json.stream.JsonParser; +//import org.apache.jena.datatypes.xsd.XSDDatatype; +//import org.apache.jena.graph.Node; +//import org.apache.jena.graph.NodeFactory; +//import org.apache.jena.graph.Triple; +//import org.apache.jena.riot.system.IRIResolver; +//import org.apache.jena.riot.system.StreamRDF; +// +// +//public class JsonStreamRDFWriter +//{ +// +// private final JsonParser parser; +// private final StreamRDF rdfStream; +// private final IRIResolver iriResolver; +// +// public JsonStreamRDFWriter(Reader reader, StreamRDF rdfStream, String baseURI) +// { +// this(Json.createParser(reader), rdfStream, baseURI); +// } +// +// public JsonStreamRDFWriter(InputStream is, StreamRDF rdfStream, String baseURI) +// { +// this(Json.createParser(is), rdfStream, baseURI); +// } +// +// public JsonStreamRDFWriter(JsonParser parser, StreamRDF rdfStream, String baseURI) +// { +// this.parser = parser; +// this.rdfStream = rdfStream; +// this.iriResolver = IRIResolver.create(baseURI); +// } +// +// public void convert() +// { +// getStreamRDF().start(); +// +// write(getParser(), getStreamRDF(), getIRIResolver()); +// +// getStreamRDF().finish(); +// } +// +// public static void write(JsonParser parser, StreamRDF rdfStream, IRIResolver iriResolver) +// { +// Deque subjectStack = new ArrayDeque<>(); +// Map arrayProperties = new HashMap<>(); +// +// Node property = null; +// while (parser.hasNext()) +// { +// JsonParser.Event event = parser.next(); +// +// switch (event) +// { +// case START_ARRAY: +// if (!subjectStack.isEmpty() && property != null) arrayProperties.put(subjectStack.getLast(), property); +// break; +// case END_ARRAY: +// if (!subjectStack.isEmpty()) arrayProperties.remove(subjectStack.getLast()); +// break; +// case START_OBJECT: +// Node subject = NodeFactory.createBlankNode(); +// // add triple with current array property, if any +// if (property != null && !subjectStack.isEmpty()) rdfStream.triple(new Triple(subjectStack.getLast(), property, subject)); +// subjectStack.addLast(subject); +// break; +// case END_OBJECT: +// subjectStack.removeLast(); +// // restore previous array property, if there was any +// if (!subjectStack.isEmpty() && arrayProperties.containsKey(subjectStack.getLast())) property = arrayProperties.get(subjectStack.getLast()); +// break; +// case VALUE_FALSE: +// rdfStream.triple(new Triple(subjectStack.getLast(), property, NodeFactory.createLiteralByValue(Boolean.FALSE, XSDDatatype.XSDboolean))); +// break; +// case VALUE_TRUE: +// rdfStream.triple(new Triple(subjectStack.getLast(), property, NodeFactory.createLiteralByValue(Boolean.TRUE, XSDDatatype.XSDboolean))); +// break; +// case KEY_NAME: +// property = NodeFactory.createURI(iriResolver.resolveToString("#" + parser.getString())); +// break; +// case VALUE_STRING: +// if (property != null) rdfStream.triple(new Triple(subjectStack.getLast(), property, NodeFactory.createLiteral(parser.getString()))); +// break; +// case VALUE_NUMBER: +// try +// { +// rdfStream.triple(new Triple(subjectStack.getLast(), property,NodeFactory.createLiteralByValue(Integer.valueOf(parser.getString()), XSDDatatype.XSDint))); +// } +// catch (NumberFormatException ex) +// { +// rdfStream.triple(new Triple(subjectStack.getLast(), property,NodeFactory.createLiteralByValue(Float.valueOf(parser.getString()), XSDDatatype.XSDfloat))); +// } +// break; +// case VALUE_NULL: +// break; +// } +// } +// } +// +// protected JsonParser getParser() +// { +// return parser; +// } +// +// protected StreamRDF getStreamRDF() +// { +// return rdfStream; +// } +// +// protected IRIResolver getIRIResolver() +// { +// return iriResolver; +// } +// +//} \ No newline at end of file diff --git a/src/main/java/JSON_Unpack/ReadSensors4Partner.java b/src/main/java/JSON_Unpack/ReadSensors4Partner.java index 3c1772f..2fa0a6d 100644 --- a/src/main/java/JSON_Unpack/ReadSensors4Partner.java +++ b/src/main/java/JSON_Unpack/ReadSensors4Partner.java @@ -40,7 +40,7 @@ public class ReadSensors4Partner { BoxCall4Partner boxCall4Partner = new BoxCall4Partner(); String outputBox = boxCall4Partner.FetchBox(cName,token); - String outputBoxFixed=outputBox.replace("[[","[") + outputBox.replace("]]","]"); // + String outputBoxFixed=outputBox.replace("[[","[") + outputBox.replace("]]","]"); //to make the JSONArray jsonArray = new JSONArray(outputBoxFixed); ArrayList list=new ArrayList(); diff --git a/src/main/java/Knowledgebase/DroolsTest.java b/src/main/java/Knowledgebase/DroolsTest.java new file mode 100644 index 0000000..bd6ca78 --- /dev/null +++ b/src/main/java/Knowledgebase/DroolsTest.java @@ -0,0 +1,34 @@ +package Knowledgebase; + +import org.kie.api.KieServices; +import org.kie.api.runtime.KieContainer; +import org.kie.api.runtime.KieSession; +import org.kie.api.runtime.rule.FactHandle; + +public class DroolsTest { + + 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"); + + Product product = new Product(); + product.setType("gold"); + + FactHandle fact1; + + fact1 = kSession.insert(product); + kSession.fireAllRules(); + + System.out.println("The discount for the jewellery product " + + product.getType() + " is " + product.getDiscount()); + + } catch (Throwable t) { + t.printStackTrace(); + } + } + +} diff --git a/src/main/java/Knowledgebase/Plants.java b/src/main/java/Knowledgebase/Plants.java new file mode 100644 index 0000000..1270b83 --- /dev/null +++ b/src/main/java/Knowledgebase/Plants.java @@ -0,0 +1,31 @@ +package Knowledgebase; + +public class Plants { + private String plantType; + private String sensorTag; + private double outputValue; + + public String getPlantType() { + return plantType; + } + + public void setPlantType(String plantType) { + this.plantType = plantType; + } + + public String getSensorTag() { + return sensorTag; + } + + public void setSensorTag(String sensorTag) { + this.sensorTag = sensorTag; + } + + public double getOutputValue() { + return outputValue; + } + + public void setOutputValue(double outputValue) { + this.outputValue = outputValue; + } +} diff --git a/src/main/java/MainExe.java b/src/main/java/MainExe.java index d30440f..6841416 100644 --- a/src/main/java/MainExe.java +++ b/src/main/java/MainExe.java @@ -7,7 +7,7 @@ public class MainExe { Timer time = new Timer(); // Instantiate Timer Object ScheduledTask scheduledTask = new ScheduledTask(); // Instantiate SheduledTask class - time.schedule(scheduledTask, 0, 1000 * 60 * 1 ); // Create Repetitively task for every 1 secs + time.schedule(scheduledTask, 0, 1000 * 60 * 1 ); // Create Repetitively task for every 1 min } } diff --git a/src/main/java/RuleEngine/KalunaRules.java b/src/main/java/RuleEngine/KalunaRules.java deleted file mode 100644 index aaa7a7d..0000000 --- a/src/main/java/RuleEngine/KalunaRules.java +++ /dev/null @@ -1,5 +0,0 @@ -package RuleEngine; - -public class KalunaRules { - -} diff --git a/src/main/java/Sensors/Sensors.java b/src/main/java/Sensors/Sensors.java deleted file mode 100644 index 541c764..0000000 --- a/src/main/java/Sensors/Sensors.java +++ /dev/null @@ -1,24 +0,0 @@ -package Sensors; - -public class Sensors { - public enum SensorType { - temperature, humidity - } - public enum Type { - KALUNA, TULIPS, TOMATOs - } - private int sensorId; - private String sensorDescription; - private int sensorBattery; - private int sensorOutput; - private int latitude; - private int longitude; - - - - - - - - -} diff --git a/src/main/resources/META-INF/kmodule.xml b/src/main/resources/META-INF/kmodule.xml new file mode 100644 index 0000000..6d1c326 --- /dev/null +++ b/src/main/resources/META-INF/kmodule.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/resources/RuleEngine/TestRule.drl b/src/main/resources/RuleEngine/TestRule.drl index e69de29..0b8dba7 100644 --- a/src/main/resources/RuleEngine/TestRule.drl +++ b/src/main/resources/RuleEngine/TestRule.drl @@ -0,0 +1,14 @@ +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(25); + end diff --git a/target/classes/RuleEngine/TestRule.drl b/target/classes/RuleEngine/TestRule.drl index e69de29..0b8dba7 100644 --- a/target/classes/RuleEngine/TestRule.drl +++ b/target/classes/RuleEngine/TestRule.drl @@ -0,0 +1,14 @@ +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(25); + end