[TASK] Add helper to get csparql query names.

This commit is contained in:
Jan Philipp Timme 2016-10-02 22:14:54 +02:00
parent a8d3b0932f
commit 56957a0911
3 changed files with 74 additions and 13 deletions

View File

@ -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<RdfStream>();
this.queryResultProxies = new ArrayList<CsparqlQueryResultProxy>();
// 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<String> queriesToRegister = new ArrayList<String>();
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;

View File

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

View File

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