[TASK] Rename project.

This commit is contained in:
JPT
2016-10-02 22:31:31 +02:00
parent fb37be3670
commit 664183b6ea
15 changed files with 29 additions and 29 deletions
@@ -0,0 +1,177 @@
package lu.jpt.csparqlproject;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import eu.larkc.csparql.cep.api.RdfStream;
import eu.larkc.csparql.core.engine.CsparqlEngine;
import eu.larkc.csparql.core.engine.CsparqlEngineImpl;
import eu.larkc.csparql.core.engine.CsparqlQueryResultProxy;
import lu.jpt.csparqlproject.gui.TextObserverWindow;
import lu.jpt.csparqlproject.rentacar.RentACarSimulation;
import lu.jpt.csparqlproject.util.CsparqlQueryHelper;
/**
* This class encapsulates the use of the C-SPARQL Engine. Here, the RentACarSimulation,
* which provides the needed event streams is being initialized, and its event streams
* are being registered with the engine.
*
* Also, the neccessary C-SPARQL queries are fetched from the RentACarSimulation and
* also getting registered with the engine. This way, everything you may need to know
* about how to use the engine itself lies within here.
*
* Take a look at the methods setup()
* and teardown(), since they contain the most important parts.
*/
public class SimulationContext {
public static Logger logger = LoggerFactory.getLogger(SimulationContext.class);
public enum SimulationState {UNINITIALIZED, INITIALIZED, RUNNING, PAUSED, SUSPENDED, TORNDOWN, ERROR};
// Internal simulation context
private SimulationState currentState;
// C-SPARQL related
private CsparqlEngine engine;
private Collection<RdfStream> registeredStreams;
private Collection<CsparqlQueryResultProxy> queryResultProxies;
// Custom simulation related
private RentACarSimulation simulation;
private Thread simulationThread;
/**
* Constructor
*/
public SimulationContext() {
this.currentState = SimulationState.UNINITIALIZED;
this.setup();
}
public void start() {
if(this.currentState != SimulationState.INITIALIZED) {
throw new RuntimeException("Action not allowed in this state!");
}
simulationThread = new Thread(simulation);
simulationThread.start();
SimulationContext.logger.info("Simulation started!");
this.currentState = SimulationState.RUNNING;
}
public void pause() {
if(this.currentState != SimulationState.RUNNING) {
throw new RuntimeException("Action not allowed in this state!");
}
this.simulation.pauseSimulation();
SimulationContext.logger.info("Simulation paused!");
this.currentState = SimulationState.PAUSED;
}
public void resume() {
if(this.currentState != SimulationState.PAUSED) {
throw new RuntimeException("Action not allowed in this state!");
}
this.simulation.resumeSimulation();
SimulationContext.logger.info("Simulation resumed!");
this.currentState = SimulationState.RUNNING;
}
public void suspend() {
if(this.currentState != SimulationState.RUNNING
&& this.currentState != SimulationState.PAUSED) {
throw new RuntimeException("Action not allowed in this state!");
}
this.simulation.pleaseStopSimulation();
this.simulationThread.interrupt();
SimulationContext.logger.info("Simulation suspended!");
this.currentState = SimulationState.SUSPENDED;
}
public void tearDown() {
if(this.currentState != SimulationState.SUSPENDED) {
throw new RuntimeException("Action not allowed in this state!");
}
// Unregister all queries from the engine
for(CsparqlQueryResultProxy resultProxy : this.queryResultProxies) {
this.engine.unregisterQuery(resultProxy.getId());
}
// Unregister all rdf streams from the engine
for(RdfStream eventStream : this.registeredStreams) {
this.engine.unregisterStream(eventStream.getIRI());
}
SimulationContext.logger.info("Simulation shut down!");
this.currentState = SimulationState.TORNDOWN;
System.exit(0);
}
public SimulationState getSimulationState() {
return this.currentState;
}
private void setup() {
// Prepare collections for registered streams and query result proxies
this.registeredStreams = new ArrayList<RdfStream>();
this.queryResultProxies = new ArrayList<CsparqlQueryResultProxy>();
// Instantiate and initialize engine
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: " + this.engine.getClass().getProtectionDomain().getCodeSource());
// Load local domain knowledge into its target graph
/*
try {
engine.putStaticNamedModel("http://example.org/carSimKnowledgeGraph", CsparqlUtils.serializeRDFFile("data/carSimulationABox.rdf"));
} catch (Exception e) {
SimulationContext.logger.error(e.toString());
SimulationContext.logger.error(e.getStackTrace().toString());
}
*/
// Note: This is how local domain knowledge can be updated during runtime:
/* Use a SPARQL Query to update the local knowledge from code instead of using CONSTRUCT within the engine.
String updateQuery = "PREFIX : <http://www.streamreasoning.org/ontologies/sr4ld2014-onto#> "
+ "INSERT DATA "
+ "{ GRAPH <http://streamreasoning.org/roomConnection> { :room :isConnectedTo :room2 } }";
this.engine.execUpdateQueryOverDatasource(updateQuery);
*/
// Spawn the whole simulation - this takes care of simulation-specific things
simulation = new RentACarSimulation();
// Register all the event streams
// Register car event stream
RdfStream carStream = simulation.getCarStream();
this.engine.registerStream(carStream);
this.registeredStreams.add(carStream);
// Register driver event stream
RdfStream driverStream = simulation.getDriverStream();
this.engine.registerStream(driverStream);
this.registeredStreams.add(driverStream);
// Register all the queries and add result observers!
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));
}
// Setup complete, ready to run.
SimulationContext.logger.info("Simulation set up and ready to go!");
this.currentState = SimulationState.INITIALIZED;
}
}