From 56957a09113304e8ffc064fdb517d9dd804a29d0 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Sun, 2 Oct 2016 22:14:54 +0200 Subject: [PATCH] [TASK] Add helper to get csparql query names. --- .../lu/jpt/csparqltest/SimulationContext.java | 31 +++++++------ .../gui/SimulationControlWindow.java | 10 ++++ .../csparqltest/util/CsparqlQueryHelper.java | 46 +++++++++++++++++++ 3 files changed, 74 insertions(+), 13 deletions(-) create mode 100644 src/main/java/lu/jpt/csparqltest/util/CsparqlQueryHelper.java diff --git a/src/main/java/lu/jpt/csparqltest/SimulationContext.java b/src/main/java/lu/jpt/csparqltest/SimulationContext.java index d84e7f3..8573706 100644 --- a/src/main/java/lu/jpt/csparqltest/SimulationContext.java +++ b/src/main/java/lu/jpt/csparqltest/SimulationContext.java @@ -13,6 +13,7 @@ import eu.larkc.csparql.core.engine.CsparqlEngineImpl; import eu.larkc.csparql.core.engine.CsparqlQueryResultProxy; import lu.jpt.csparqltest.gui.TextObserverWindow; import lu.jpt.csparqltest.rentacar.RentACarSimulation; +import lu.jpt.csparqltest.util.CsparqlQueryHelper; /** * This class encapsulates the use of the C-SPARQL Engine. Here, the RentACarSimulation, @@ -119,12 +120,12 @@ public class SimulationContext { this.registeredStreams = new ArrayList(); this.queryResultProxies = new ArrayList(); // Instantiate and initialize engine - engine = new CsparqlEngineImpl(); + this.engine = new CsparqlEngineImpl(); // Initialize with true to allow use of timestamp function this.engine.initialize(true); // Debugging output SimulationContext.logger.debug("CWD: " + System.getProperty("user.dir")); - SimulationContext.logger.debug("Engine from: " + engine.getClass().getProtectionDomain().getCodeSource()); + SimulationContext.logger.debug("Engine from: " + this.engine.getClass().getProtectionDomain().getCodeSource()); // Load local domain knowledge into its target graph /* try { @@ -146,23 +147,27 @@ public class SimulationContext { // Register all the event streams // Register car event stream RdfStream carStream = simulation.getCarStream(); - engine.registerStream(carStream); + this.engine.registerStream(carStream); this.registeredStreams.add(carStream); // Register driver event stream RdfStream driverStream = simulation.getDriverStream(); - engine.registerStream(driverStream); + this.engine.registerStream(driverStream); this.registeredStreams.add(driverStream); // Register all the queries and add result observers! - String query = RentACarSimulation.getEventsQuery(); - CsparqlQueryResultProxy resultProxy = null; - try { - resultProxy = engine.registerQuery(query, true); - } catch (ParseException e) { - SimulationContext.logger.error(e.toString()); - SimulationContext.logger.error(e.getStackTrace().toString()); + Collection queriesToRegister = new ArrayList(); + queriesToRegister.add(RentACarSimulation.getEventsQuery()); + for(String query : queriesToRegister) { + String queryName = CsparqlQueryHelper.getQueryName(query); + CsparqlQueryResultProxy resultProxy = null; + try { + resultProxy = this.engine.registerQuery(query, true); + } catch (ParseException e) { + SimulationContext.logger.error(e.toString()); + SimulationContext.logger.error(e.getStackTrace().toString()); + } + this.queryResultProxies.add(resultProxy); + resultProxy.addObserver(new TextObserverWindow("[ResultProxy] " + queryName)); } - this.queryResultProxies.add(resultProxy); - resultProxy.addObserver(new TextObserverWindow("ResultProxy Observer Window")); // Setup complete, ready to run. SimulationContext.logger.info("Simulation set up and ready to go!"); this.currentState = SimulationState.INITIALIZED; diff --git a/src/main/java/lu/jpt/csparqltest/gui/SimulationControlWindow.java b/src/main/java/lu/jpt/csparqltest/gui/SimulationControlWindow.java index ee164ef..35821b7 100644 --- a/src/main/java/lu/jpt/csparqltest/gui/SimulationControlWindow.java +++ b/src/main/java/lu/jpt/csparqltest/gui/SimulationControlWindow.java @@ -83,6 +83,16 @@ public class SimulationControlWindow extends JFrame { }); buttonPanel.add(shutdownButton); + // Create quit button + JButton quitButton = new JButton("Quit"); + quitButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + System.exit(0); + } + }); + buttonPanel.add(quitButton); + // Add buttonPanel to contentPane contentPane.add(buttonPanel, BorderLayout.CENTER); diff --git a/src/main/java/lu/jpt/csparqltest/util/CsparqlQueryHelper.java b/src/main/java/lu/jpt/csparqltest/util/CsparqlQueryHelper.java new file mode 100644 index 0000000..6f209da --- /dev/null +++ b/src/main/java/lu/jpt/csparqltest/util/CsparqlQueryHelper.java @@ -0,0 +1,46 @@ +package lu.jpt.csparqltest.util; + +import java.util.StringTokenizer; + +/** + * Helper class that provides small methods to handle + * (strings containing) csparql queries. + */ +public class CsparqlQueryHelper { + + /** + * Returns the csparql query name stated within the query. + * @param query given csparql query + * @return name of query + */ + public static String getQueryName(String query) { + String queryName = "[UNRESOLVED NAME]"; + 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")) { + stateCounter++; + } + break; + case 2: + queryName = token.trim(); + gotName = true; + break; + default: + break; + } + } + return queryName; + } + +}