Commit 890360f1 authored by liorpeak's avatar liorpeak
Browse files

first commit.

parents
/*******************************************************************************
* Copyright 2014 Davide Barbieri, Emanuele Della Valle, Marco Balduini
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Acknowledgements:
*
* This work was partially supported by the European project LarKC (FP7-215535)
* and by the European project MODAClouds (FP7-318484)
******************************************************************************/
package eu.larkc.csparql.sr4ld2014;
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import eu.larkc.csparql.common.utils.CsparqlUtils;
import eu.larkc.csparql.common.utils.ReasonerChainingType;
import eu.larkc.csparql.core.engine.ConsoleFormatter;
import eu.larkc.csparql.core.engine.CsparqlEngineImpl;
import eu.larkc.csparql.core.engine.CsparqlQueryResultProxy;
import eu.larkc.csparql.sr4ld2014.streamer.SensorsStreamer;
public class Example {
private static Logger logger = LoggerFactory.getLogger(Example.class);
public static void main(String[] args) {
try{
//Configure log4j logger for the csparql engine
PropertyConfigurator.configure("log4j_configuration/csparql_readyToGoPack_log4j.properties");
//Create csparql engine instance
CsparqlEngineImpl engine = new CsparqlEngineImpl();
//Initialize the engine instance
//The initialization creates the static engine (SPARQL) and the stream engine (CEP)
engine.initialize();
String queryBody = "REGISTER QUERY reasoning AS "
+ "PREFIX :<http://onto#> "
+ "SELECT ?s ?p ?r "
//+ "FROM STREAM <http://streamreasoning.org/streams/fb> [RANGE 1s STEP 1s] "
+ "FROM STREAM <http://streamreasoning.org/streams/sensors> [RANGE 1s STEP 1s] "
+ "WHERE { "
//+ "?s :madeObservation ?o ."
//+ "?o :hasSimpleResult ?p . "
+ "?s :madeObservation [ :hasSimpleResult ?p ; :hasTime ?r ] "
+ "FILTER (?p = 2). "
+ "} ";
//FacebookStreamer fb = new FacebookStreamer("http://streamreasoning.org/streams/fb", "http://www.streamreasoning.org/ontologies/sr4ld2014-onto#", 1000L);
SensorsStreamer s = new SensorsStreamer("http://streamreasoning.org/streams/sensors", "http://onto#", 10000L);
//Register new streams in the engine
engine.registerStream(s);
//Thread fbThread = new Thread(fb);
Thread sThread = new Thread(s);
//Register new query in the engine
CsparqlQueryResultProxy c = engine.registerQuery(queryBody, false);
//Attach a result consumer to the query result proxy to print the results on the console
c.addObserver(new ConsoleFormatter());
//Start streaming data
sThread.start();
engine.updateReasoner(c.getSparqlQueryId(), CsparqlUtils.fileToString("examples_files/rdfs.rules"), ReasonerChainingType.FORWARD, CsparqlUtils.serializeRDFFile("examples_files/tbox.rdf"));
}catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
}
\ No newline at end of file
/*******************************************************************************
* Copyright 2014 Davide Barbieri, Emanuele Della Valle, Marco Balduini
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Acknowledgements:
*
* This work was partially supported by the European project LarKC (FP7-215535)
* and by the European project MODAClouds (FP7-318484)
******************************************************************************/
package eu.larkc.csparql.sr4ld2014.streamer;
import java.sql.Timestamp;
import java.time.ZonedDateTime;
import java.util.Random;
import eu.larkc.csparql.cep.api.RdfQuadruple;
import eu.larkc.csparql.cep.api.RdfStream;
public class SensorsStreamer extends RdfStream implements Runnable {
private long sleepTime;
private String baseUri;
public SensorsStreamer(String iri, String baseUri,long sleepTime) {
super(iri);
this.sleepTime = sleepTime;
this.baseUri = baseUri;
}
public void run() {
Random random = new Random();
int result;
int observationIndex = 0;
int timeIndex = 0;
while(true){
try{
result = random.nextInt(5);
//ZonedDateTime zdt = ZonedDateTime.now();
//java.util.Date date = java.util.Date.from( zdt.toInstant() );
Timestamp date = new Timestamp(System.currentTimeMillis());
//observationIndex = random.nextInt(Integer.MAX_VALUE);
RdfQuadruple q = new RdfQuadruple(baseUri + "Nacelle", baseUri + "hosts", baseUri + "sensorTempNacelle", System.currentTimeMillis());
System.out.println(q);
this.put(q);
q = new RdfQuadruple(baseUri + "sensorTempNacelle", baseUri + "madeObservation", baseUri + "obs" + observationIndex, System.currentTimeMillis());
System.out.println(q);
this.put(q);
q = new RdfQuadruple(baseUri + "obs" + observationIndex, baseUri + "observedProperty", baseUri + "Nacelle_Temp", System.currentTimeMillis());
System.out.println(q);
this.put(q);
q = new RdfQuadruple(baseUri + "obs" + observationIndex, baseUri + "hasSimpleResult", result + "^^http://www.w3.org/2001/XMLSchema#integer", System.currentTimeMillis());
System.out.println(q);
this.put(q);
q = new RdfQuadruple(baseUri + "obs" + observationIndex, baseUri + "hasTime", baseUri + "T" + timeIndex, System.currentTimeMillis());
System.out.println(q);
this.put(q);
q = new RdfQuadruple(baseUri + "T" + timeIndex, baseUri + "inXSDDateTime", date + "^^http://www.w3.org/2001/XMLSchema#dateTimeStamp", System.currentTimeMillis());
System.out.println(q);
this.put(q);
observationIndex++;
timeIndex++;
Thread.sleep(sleepTime);
} catch(Exception e){
e.printStackTrace();
}
}
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment