[TASK] Begin implementing a query to detect breaking down cars.

This commit is contained in:
Jan Philipp Timme 2016-07-08 12:12:20 +02:00
parent fb942ded5d
commit 612b401947

View File

@ -18,7 +18,10 @@ public class Main {
private static Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
System.out.println("Hallo!");
// Determines the time the engine will be run for
final long millisecondsToRun = 50 * 1000;
// Load configuration for log4j
try {
PropertyConfigurator.configure("./src/main/resources/log4j.properties");
} catch(Throwable t) {
@ -28,8 +31,10 @@ public class Main {
// Instantiate and initialize engine
CsparqlEngine engine = new CsparqlEngineImpl();
engine.initialize(true);
logger.debug("CWD: " + System.getProperty("user.dir"));
logger.debug("Engine from: " + engine.getClass().getProtectionDomain().getCodeSource());
// Debugging: Needed to verify which engine is being used
//logger.debug("CWD: " + System.getProperty("user.dir"));
//logger.debug("Engine from: " + engine.getClass().getProtectionDomain().getCodeSource());
// Create and register stream generator at specific URI
RdfStream carStreamGenerator = new CarStreamGenerator("http://myexample.org/cars");
@ -40,7 +45,7 @@ public class Main {
t.start();
// Now build a query to run - interchangeable
String query = Main.getAcceleratingCarsQuery();
String query = Main.getBreakingDownCarsQuery();
// Create a result proxy by registering the query at the engine
CsparqlQueryResultProxy resultProxy = null;
@ -53,9 +58,9 @@ public class Main {
// Add ConsoleFormatter as observer so it gets notified of every query result
resultProxy.addObserver(new ConsoleFormatter());
// Let it all run for a couple of seconds (3 min, 20 seconds)
// Let it all run for some time
try {
Thread.sleep(20000);
Thread.sleep(millisecondsToRun);
} catch (InterruptedException e) {
logger.error(e.getMessage(), e);
}
@ -86,7 +91,7 @@ public class Main {
}
private static String getIncreasingTemperatureQuery() {
return "REGISTER QUERY getIncreasingTemperature AS "
return "REGISTER QUERY IncreasingTemperature AS "
+ "PREFIX f: <http://larkc.eu/csparql/sparql/jena/ext#> "
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "
+ "PREFIX cars: <http://myexample.org/cars#> "
@ -99,13 +104,13 @@ public class Main {
+ " ?e2 cars:carID ?car . "
+ " ?e2 cars:currentSpeed ?speed2 . "
+ " ?e2 cars:currentTemperature ?temp2 . "
+ " FILTER(f:timestamp(?e1,cars:carID,?car) > f:timestamp(?e2,cars:carID,?car)) "
+ " FILTER(f:timestamp(?e1,cars:carID,?car) < f:timestamp(?e2,cars:carID,?car)) "
+ " FILTER(?temp1 < ?temp2) "
+ "}";
}
private static String getAcceleratingCarsQuery() {
return "REGISTER QUERY getAcceleratingCars AS "
return "REGISTER QUERY AcceleratingCars AS "
+ "PREFIX f: <http://larkc.eu/csparql/sparql/jena/ext#> "
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "
+ "PREFIX cars: <http://myexample.org/cars#> "
@ -118,9 +123,31 @@ public class Main {
+ " ?e2 cars:carID ?car . "
+ " ?e2 cars:currentSpeed ?speed2 . "
+ " ?e2 cars:currentTemperature ?temp2 . "
+ " FILTER(f:timestamp(?e1,cars:carID,?car) > f:timestamp(?e2,cars:carID,?car)) "
+ " FILTER(f:timestamp(?e1,cars:carID,?car) < f:timestamp(?e2,cars:carID,?car)) "
+ " FILTER(?speed1 < ?speed2) "
+ "}";
}
private static String getBreakingDownCarsQuery() {
return "REGISTER QUERY BreakingDownCars AS "
+ "PREFIX f: <http://larkc.eu/csparql/sparql/jena/ext#> "
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "
+ "PREFIX cars: <http://myexample.org/cars#> "
+ "SELECT ?car (?speed2 - ?speed1 AS ?deltaspeed) (?temp2 - ?temp1 AS ?deltatemp) ?speed2 ?temp2 "
+ "FROM STREAM <http://myexample.org/cars> [RANGE 5s STEP 1s] "
+ "WHERE { "
+ " ?e1 cars:carID ?car ; "
+ " cars:currentSpeed ?speed1 ; "
+ " cars:currentTemperature ?temp1 . "
+ " ?e2 cars:carID ?car ; "
+ " cars:currentSpeed ?speed2 ; "
+ " cars:currentTemperature ?temp2 . "
+ " FILTER(f:timestamp(?e1,cars:carID,?car) < f:timestamp(?e2,cars:carID,?car)) "
+ " FILTER(f:timestamp(?e2,cars:carID,?car) - f:timestamp(?e1,cars:carID,?car) < 1800) "
+ " FILTER(?speed1 > ?speed2) "
+ " FILTER(?temp2 > ?temp1) "
+ " FILTER(?temp2 > 90) "
+ "}";
}
}