FINALIZED : Json_Unpack

ADD : KnowledgeBase
TODO : Rule Engine
master
Kevin Shehu 2021-06-17 10:29:11 +02:00
parent 39251400d7
commit e357d53584
13 changed files with 359 additions and 251 deletions

72
pom.xml
View File

@ -19,24 +19,10 @@
</plugin>
</plugins>
</build>
<properties>
<runtime.version>7.48.0.Final</runtime.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>apache-jena-libs</artifactId>
<type>pom</type>
<version>3.16.0</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-fuseki-war</artifactId>
<type>war</type>
<version>3.16.0</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-fuseki-main</artifactId>
<version>3.16.0</version>
</dependency>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
@ -78,6 +64,14 @@
<artifactId>drools-decisiontables</artifactId>
<version>7.48.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.drools/kiebase-inclusion -->
<dependency>
<groupId>org.drools</groupId>
<artifactId>kiebase-inclusion</artifactId>
<version>7.48.0.Final</version>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.kie/kie-api -->
<dependency>
<groupId>org.kie</groupId>
@ -85,11 +79,55 @@
<version>7.48.0.Final</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.kie/kie-ci -->
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-ci</artifactId>
<version>7.48.0.Final</version>
</dependency>
<!-- <repositories>-->
<!-- <repository>-->
<!-- <id>jboss-public-repository-group</id>-->
<!-- <name>JBoss Public Repository Group</name>-->
<!-- <url>http://repository.jboss.org/nexus/content/groups/public/</url>-->
<!-- <releases>-->
<!-- <enabled>true</enabled>-->
<!-- <updatePolicy>never</updatePolicy>-->
<!-- </releases>-->
<!-- <snapshots>-->
<!-- <enabled>true</enabled>-->
<!-- <updatePolicy>daily</updatePolicy>-->
<!-- </snapshots>-->
<!-- </repository>-->
<!-- </repositories>-->
<!-- <dependencies>-->
<!-- <dependency>-->
<!-- <groupId>org.kie</groupId>-->
<!-- <artifactId>kie-api</artifactId>-->
<!-- <version>${runtime.version}</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.drools</groupId>-->
<!-- <artifactId>drools-core</artifactId>-->
<!-- <version>${runtime.version}</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.drools</groupId>-->
<!-- <artifactId>drools-decisiontables</artifactId>-->
<!-- <version>${runtime.version}</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.jbpm</groupId>-->
<!-- <artifactId>jbpm-test</artifactId>-->
<!-- <version>${runtime.version}</version>-->
<!-- </dependency>-->
</dependencies>
</project>

View File

@ -1 +1 @@
OnomznAUvxd8lmIUu7FRiIkfYdoUCGN1x84B4AjS9yE4TW3xLpmh2rOxlBtBD0IJ
PlixJiy6sktkQZcj1WJVqLZCR4abCgnNaMlG2DYGZ2ga75aOA00E9jTzBmSC88tw

View File

@ -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();
// }
// }
//
//}
//

View File

@ -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<Node> subjectStack = new ArrayDeque<>();
Map<Node, Node> 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;
}
}
//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<Node> subjectStack = new ArrayDeque<>();
// Map<Node, Node> 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;
// }
//
//}

View File

@ -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<String> list=new ArrayList<String>();

View File

@ -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();
}
}
}

View File

@ -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;
}
}

View File

@ -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
}
}

View File

@ -1,5 +0,0 @@
package RuleEngine;
public class KalunaRules {
}

View File

@ -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;
}

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
<kbase name="rules" packages="rules">
<ksession name="ksession-rule"/>
</kbase>
</kmodule>

View File

@ -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

View File

@ -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