From 28afcb4b7f6df604f13cd31fdaf06e14dac648ce Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Sat, 8 Oct 2016 18:46:54 +0200 Subject: [PATCH] [TASK] Add QueryContainer class. --- .../jpt/csparqlproject/SimulationContext.java | 39 ++++++------ .../csparqlproject/misc/QueryContainer.java | 40 +++++++++++++ .../rentacar/RentACarSimulation.java | 35 +++++++---- .../util/CsparqlQueryHelper.java | 60 ------------------- 4 files changed, 87 insertions(+), 87 deletions(-) create mode 100644 src/main/java/lu/jpt/csparqlproject/misc/QueryContainer.java delete mode 100644 src/main/java/lu/jpt/csparqlproject/util/CsparqlQueryHelper.java diff --git a/src/main/java/lu/jpt/csparqlproject/SimulationContext.java b/src/main/java/lu/jpt/csparqlproject/SimulationContext.java index 7f6e47b..ee53806 100644 --- a/src/main/java/lu/jpt/csparqlproject/SimulationContext.java +++ b/src/main/java/lu/jpt/csparqlproject/SimulationContext.java @@ -9,16 +9,14 @@ import org.slf4j.LoggerFactory; import eu.larkc.csparql.cep.api.RdfStream; import eu.larkc.csparql.common.utils.CsparqlUtils; -import eu.larkc.csparql.common.utils.ReasonerChainingType; import eu.larkc.csparql.core.engine.CsparqlEngine; import eu.larkc.csparql.core.engine.CsparqlEngineImpl; import eu.larkc.csparql.core.engine.CsparqlQueryResultProxy; import eu.larkc.csparql.core.engine.RDFStreamFormatter; import lu.jpt.csparqlproject.gui.FancyTextObserverWindow; +import lu.jpt.csparqlproject.misc.QueryContainer; import lu.jpt.csparqlproject.misc.ReasoningTester; import lu.jpt.csparqlproject.rentacar.RentACarSimulation; -import lu.jpt.csparqlproject.util.CsparqlQueryHelper; -import lu.jpt.csparqlproject.util.CsparqlQueryHelper.CsparqlQueryInfo; /** * This class encapsulates the use of the C-SPARQL Engine. Here, the RentACarSimulation, @@ -159,38 +157,39 @@ public class SimulationContext { RdfStream driverStream = simulation.getDriverStream(); this.engine.registerStream(driverStream); this.registeredStreams.add(driverStream); - Collection queriesToRegister = new ArrayList(); + Collection queriesToRegister = new ArrayList(); // Collect the queries to use! //queriesToRegister.add(RentACarSimulation.getEventsQuery()); queriesToRegister.add(RentACarSimulation.getAverageDataByCarAsStream()); queriesToRegister.add(RentACarSimulation.selectFromRegisteredStream()); // Now register each query appropriately! - for(String query : queriesToRegister) { - CsparqlQueryInfo queryInfo = CsparqlQueryHelper.getQueryInfo(query); + for(QueryContainer queryContainer : queriesToRegister) { CsparqlQueryResultProxy resultProxy = null; try { - resultProxy = this.engine.registerQuery(query, true); - this.enableReasoningForResultProxy(resultProxy); + resultProxy = this.engine.registerQuery(queryContainer.query, queryContainer.reasoningEnabled); + if(queryContainer.reasoningEnabled) { + this.enableReasoningForResultProxy(resultProxy, queryContainer); + } // Take care of streams and queries differently. - if(queryInfo.isStream) { + if(queryContainer.isStream) { // If the query is a stream, we need additional components to feed it back into the engine. - String streamUri = "http://example.org/stream/"+queryInfo.name; + String streamUri = "http://example.org/stream/"+queryContainer.name; RDFStreamFormatter rdfStreamFormatter = new RDFStreamFormatter(streamUri); engine.registerStream(rdfStreamFormatter); resultProxy.addObserver(rdfStreamFormatter); - Observer resultObserver = this.createResultObserverWindow(queryInfo.name); + Observer resultObserver = this.createResultObserverWindow(queryContainer.name); resultProxy.addObserver(resultObserver); } else { // If it is a regular query, just attach a fitting observer - Observer resultObserver = this.createResultObserverWindow(queryInfo.name); + Observer resultObserver = this.createResultObserverWindow(queryContainer.name); resultProxy.addObserver(resultObserver); } this.queryResultProxies.add(resultProxy); - SimulationContext.logger.info("Successfully registered query " + queryInfo.name + ": " + query); + SimulationContext.logger.info("Successfully registered query " + queryContainer.name + ": " + queryContainer); } catch (Exception e) { SimulationContext.logger.error(e.toString()); - SimulationContext.logger.error("Could not register query "+queryInfo.name); - SimulationContext.logger.error(query); + SimulationContext.logger.error("Could not register query "+queryContainer.name); + SimulationContext.logger.error(queryContainer.query); } } // Setup complete, ready to run. @@ -216,13 +215,19 @@ public class SimulationContext { return observer; } - private void enableReasoningForResultProxy(CsparqlQueryResultProxy resultProxy) { + private void enableReasoningForResultProxy(CsparqlQueryResultProxy resultProxy, QueryContainer queryContainer) { try { - engine.updateReasoner( + /* If not using the queryContainer, these would be valid parameters: resultProxy.getSparqlQueryId(), CsparqlUtils.fileToString("data/rdfs.rules"), ReasonerChainingType.HYBRID, CsparqlUtils.serializeRDFFile("data/carSimulationTBox.rdf") + */ + engine.updateReasoner( + resultProxy.getSparqlQueryId(), + queryContainer.ruleSet, + queryContainer.reasonerChainingType, + queryContainer.TBox ); } catch(Exception e) { e.printStackTrace(); diff --git a/src/main/java/lu/jpt/csparqlproject/misc/QueryContainer.java b/src/main/java/lu/jpt/csparqlproject/misc/QueryContainer.java new file mode 100644 index 0000000..72434ab --- /dev/null +++ b/src/main/java/lu/jpt/csparqlproject/misc/QueryContainer.java @@ -0,0 +1,40 @@ +package lu.jpt.csparqlproject.misc; + +import eu.larkc.csparql.common.utils.ReasonerChainingType; + +/** + * Container class that allows for easier encapsulation of a query with + * configuration. + */ +public class QueryContainer { + public String name; + public String query; + public boolean isStream; + public boolean reasoningEnabled; + public String TBox; + public String ruleSet; + public ReasonerChainingType reasonerChainingType; + public boolean useObserverWindow; + + public QueryContainer(String name, String query, boolean isStream) { + this.name = name; + this.query = query; + this.isStream = isStream; + this.reasoningEnabled = false; + this.TBox = "REASONING NOT PROPERLY ENABLED"; + this.ruleSet = "REASONING NOT PROPERLY ENABLED"; + this.reasonerChainingType = null; + this.useObserverWindow = false; + } + + public void enableReasoning(String ruleSet, String TBox, ReasonerChainingType reasonerChainingType) { + this.reasoningEnabled = true; + this.ruleSet = ruleSet; + this.TBox = TBox; + this.reasonerChainingType = reasonerChainingType; + } + + public void useObserverWindow() { + this.useObserverWindow = true; + } +} \ No newline at end of file diff --git a/src/main/java/lu/jpt/csparqlproject/rentacar/RentACarSimulation.java b/src/main/java/lu/jpt/csparqlproject/rentacar/RentACarSimulation.java index 31909b4..2052a8c 100644 --- a/src/main/java/lu/jpt/csparqlproject/rentacar/RentACarSimulation.java +++ b/src/main/java/lu/jpt/csparqlproject/rentacar/RentACarSimulation.java @@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory; import eu.larkc.csparql.cep.api.RdfQuadruple; import eu.larkc.csparql.cep.api.RdfStream; import lu.jpt.csparqlproject.Main; +import lu.jpt.csparqlproject.misc.QueryContainer; import lu.jpt.csparqlproject.util.WindowLoggingRdfStream; /** @@ -146,8 +147,8 @@ public class RentACarSimulation implements Runnable { } - public static String getEventsQuery() { - return "REGISTER QUERY getEvents AS " + public static QueryContainer getEventsQuery() { + String query = "REGISTER QUERY getCarStatusEvents AS " + "PREFIX rdf: " + "PREFIX f: " + "PREFIX xsd: " @@ -168,10 +169,13 @@ public class RentACarSimulation implements Runnable { + " ?e car:tirePressure3 ?tirePressure3 . " + " ?e car:tirePressure4 ?tirePressure4 . " + "}"; + QueryContainer queryContainer = new QueryContainer("getCarStatusEvents", query, false); + queryContainer.useObserverWindow(); + return queryContainer; } - public static String getEventUsingBackgroundKnowledge() { - return "REGISTER QUERY getEventsCombinedWithBackgroundKnowledge AS " + public static QueryContainer getEventUsingBackgroundKnowledge() { + String query = "REGISTER QUERY getEventsCombinedWithBackgroundKnowledge AS " + "PREFIX rdf: " + "PREFIX f: " + "PREFIX xsd: " @@ -187,10 +191,13 @@ public class RentACarSimulation implements Runnable { + " ?driver car:hasName ?driverName . " + " ?driver car:hasPhoneNumber ?driverPhone . " + "}"; + QueryContainer queryContainer = new QueryContainer("getEventsCombinedWithBackgroundKnowledge", query, false); + queryContainer.useObserverWindow(); + return queryContainer; } - public static String getSpeedByCar() { - return "REGISTER QUERY getSpeedByCar AS " + public static QueryContainer getSpeedByCar() { + String query = "REGISTER QUERY getSpeedByCar AS " + "PREFIX rdf: " + "PREFIX f: " + "PREFIX xsd: " @@ -202,10 +209,13 @@ public class RentACarSimulation implements Runnable { + " ?e car:relatedCar ?car . " + " ?e car:speed ?speed . " + "}"; + QueryContainer queryContainer = new QueryContainer("getSpeedByCar", query, false); + queryContainer.useObserverWindow(); + return queryContainer; } - public static String getAverageDataByCarAsStream() { - return "REGISTER STREAM getAverageSpeedByCar AS " + public static QueryContainer getAverageDataByCarAsStream() { + String query = "REGISTER STREAM getAverageSpeedByCar AS " + "PREFIX rdf: " + "PREFIX f: " + "PREFIX xsd: " @@ -227,13 +237,15 @@ public class RentACarSimulation implements Runnable { + " GROUP BY (?car) " + " }" + "} "; + QueryContainer queryContainer = new QueryContainer("getAverageSpeedByCar", query, true); + return queryContainer; } /** * IMPORTANT NOTE: STREAM gets matched everywhere, so don't call stuff "stream"! */ - public static String selectFromRegisteredStream() { - return "REGISTER Query askRegisteredStrm AS " + public static QueryContainer selectFromRegisteredStream() { + String query = "REGISTER Query askRegisteredStrm AS " + "PREFIX rdf: " + "PREFIX f: " + "PREFIX xsd: " @@ -243,5 +255,8 @@ public class RentACarSimulation implements Runnable { + "WHERE { " + " ?s ?p ?o . " + "} "; + QueryContainer queryContainer = new QueryContainer("askRegisteredStrm", query, false); + queryContainer.useObserverWindow(); + return queryContainer; } } diff --git a/src/main/java/lu/jpt/csparqlproject/util/CsparqlQueryHelper.java b/src/main/java/lu/jpt/csparqlproject/util/CsparqlQueryHelper.java deleted file mode 100644 index 102284b..0000000 --- a/src/main/java/lu/jpt/csparqlproject/util/CsparqlQueryHelper.java +++ /dev/null @@ -1,60 +0,0 @@ -package lu.jpt.csparqlproject.util; - -import java.util.StringTokenizer; - -/** - * Helper class that provides small methods to handle - * (strings containing) csparql queries. - */ -public class CsparqlQueryHelper { - - /** - * Small inner class containing query info - */ - public class CsparqlQueryInfo { - public boolean isStream; - public String name; - - public CsparqlQueryInfo() { - this.name = "[UNRESOLVED NAME]"; - this.isStream = false; - } - } - - /** - * Returns the csparql query name stated within the query. - * @param query given csparql query - * @return query info using class CsparqlQueryInfo - */ - public static CsparqlQueryInfo getQueryInfo(String query) { - CsparqlQueryHelper.CsparqlQueryInfo result = new CsparqlQueryHelper().new CsparqlQueryInfo(); - StringTokenizer tokenizer = new StringTokenizer(query); - boolean gotName = false; - int stateCounter = 0; - while(tokenizer.hasMoreElements() && gotName == false) { - String token = tokenizer.nextToken(); - String trimmedLowerToken = token.toLowerCase().trim(); - switch(stateCounter) { - case 0: - if(trimmedLowerToken.equals("register")) { - stateCounter++; - } - break; - case 1: - if(trimmedLowerToken.equals("query") || trimmedLowerToken.equals("stream")) { - if(trimmedLowerToken.equals("stream")) result.isStream = true; - stateCounter++; - } - break; - case 2: - result.name = token.trim(); - gotName = true; - break; - default: - break; - } - } - return result; - } - -}