[TASK] Cleanups.

This commit is contained in:
Jan Philipp Timme 2016-10-02 20:28:51 +02:00
parent 1a47cc094e
commit 36a14510f3
4 changed files with 38 additions and 10 deletions

View File

@ -4,6 +4,9 @@ import java.text.ParseException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import eu.larkc.csparql.cep.api.RdfStream; import eu.larkc.csparql.cep.api.RdfStream;
import eu.larkc.csparql.core.engine.CsparqlEngine; import eu.larkc.csparql.core.engine.CsparqlEngine;
import eu.larkc.csparql.core.engine.CsparqlEngineImpl; import eu.larkc.csparql.core.engine.CsparqlEngineImpl;
@ -13,6 +16,8 @@ import lu.jpt.csparqltest.rentacar.RentACarSimulation;
public class SimulationContext { public class SimulationContext {
public static Logger logger = LoggerFactory.getLogger(SimulationContext.class);
public enum SimulationState { UNINITIALIZED, INITIALIZED, RUNNING, PAUSED, SUSPENDED, TORNDOWN, ERROR}; public enum SimulationState { UNINITIALIZED, INITIALIZED, RUNNING, PAUSED, SUSPENDED, TORNDOWN, ERROR};
// Internal simulation context // Internal simulation context
@ -42,6 +47,7 @@ public class SimulationContext {
} }
simulationThread = new Thread(simulation); simulationThread = new Thread(simulation);
simulationThread.start(); simulationThread.start();
SimulationContext.logger.info("Simulation started!");
this.currentState = SimulationState.RUNNING; this.currentState = SimulationState.RUNNING;
} }
@ -50,6 +56,7 @@ public class SimulationContext {
throw new RuntimeException("Action not allowed in this state!"); throw new RuntimeException("Action not allowed in this state!");
} }
this.simulation.pauseSimulation(); this.simulation.pauseSimulation();
SimulationContext.logger.info("Simulation paused!");
this.currentState = SimulationState.PAUSED; this.currentState = SimulationState.PAUSED;
} }
@ -58,6 +65,7 @@ public class SimulationContext {
throw new RuntimeException("Action not allowed in this state!"); throw new RuntimeException("Action not allowed in this state!");
} }
this.simulation.resumeSimulation(); this.simulation.resumeSimulation();
SimulationContext.logger.info("Simulation resumed!");
this.currentState = SimulationState.RUNNING; this.currentState = SimulationState.RUNNING;
} }
@ -69,6 +77,7 @@ public class SimulationContext {
} }
this.simulation.pleaseStopSimulation(); this.simulation.pleaseStopSimulation();
this.simulationThread.interrupt(); this.simulationThread.interrupt();
SimulationContext.logger.info("Simulation suspended!");
this.currentState = SimulationState.SUSPENDED; this.currentState = SimulationState.SUSPENDED;
} }
@ -84,7 +93,9 @@ public class SimulationContext {
for(RdfStream eventStream : this.registeredStreams) { for(RdfStream eventStream : this.registeredStreams) {
this.engine.unregisterStream(eventStream.getIRI()); this.engine.unregisterStream(eventStream.getIRI());
} }
SimulationContext.logger.info("Simulation shut down!");
this.currentState = SimulationState.TORNDOWN; this.currentState = SimulationState.TORNDOWN;
System.exit(0);
} }
public SimulationState getSimulationState() { public SimulationState getSimulationState() {
@ -100,15 +111,15 @@ public class SimulationContext {
// Initialize with true to allow use of timestamp function // Initialize with true to allow use of timestamp function
this.engine.initialize(true); this.engine.initialize(true);
// Debugging output // Debugging output
Main.logger.debug("CWD: " + System.getProperty("user.dir")); SimulationContext.logger.debug("CWD: " + System.getProperty("user.dir"));
Main.logger.debug("Engine from: " + engine.getClass().getProtectionDomain().getCodeSource()); SimulationContext.logger.debug("Engine from: " + engine.getClass().getProtectionDomain().getCodeSource());
// Load local domain knowledge into its target graph // Load local domain knowledge into its target graph
/* /*
try { try {
engine.putStaticNamedModel("http://example.org/carSimKnowledgeGraph", CsparqlUtils.serializeRDFFile("data/carSimulationABox.rdf")); engine.putStaticNamedModel("http://example.org/carSimKnowledgeGraph", CsparqlUtils.serializeRDFFile("data/carSimulationABox.rdf"));
} catch (Exception e) { } catch (Exception e) {
Main.logger.error(e.toString()); SimulationContext.logger.error(e.toString());
Main.logger.error(e.getStackTrace().toString()); SimulationContext.logger.error(e.getStackTrace().toString());
} }
*/ */
// Note: This is how local domain knowledge can be updated during runtime: // Note: This is how local domain knowledge can be updated during runtime:
@ -135,12 +146,13 @@ public class SimulationContext {
try { try {
resultProxy = engine.registerQuery(query, true); resultProxy = engine.registerQuery(query, true);
} catch (ParseException e) { } catch (ParseException e) {
Main.logger.error(e.toString()); SimulationContext.logger.error(e.toString());
Main.logger.error(e.getStackTrace().toString()); SimulationContext.logger.error(e.getStackTrace().toString());
} }
this.queryResultProxies.add(resultProxy); this.queryResultProxies.add(resultProxy);
resultProxy.addObserver(new TextObserverWindow("ResultProxy Observer Window")); resultProxy.addObserver(new TextObserverWindow("ResultProxy Observer Window"));
// Setup complete, ready to run. // Setup complete, ready to run.
SimulationContext.logger.info("Simulation set up and ready to go!");
this.currentState = SimulationState.INITIALIZED; this.currentState = SimulationState.INITIALIZED;
} }

View File

@ -3,10 +3,16 @@ package lu.jpt.csparqltest.rentacar;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import eu.larkc.csparql.cep.api.RdfQuadruple; import eu.larkc.csparql.cep.api.RdfQuadruple;
import lu.jpt.csparqltest.util.RandomHelper; import lu.jpt.csparqltest.util.RandomHelper;
public class Car { public class Car {
public static Logger logger = LoggerFactory.getLogger(Car.class);
// State-change related enums // State-change related enums
private enum CarAction {NONE, UNLOCKING, STARTING, ACCELERATING, BRAKING, CRASHING, STOPPING, LOCKING, HALTING}; private enum CarAction {NONE, UNLOCKING, STARTING, ACCELERATING, BRAKING, CRASHING, STOPPING, LOCKING, HALTING};
public enum CarState {LOCKED, OFF, IDLE, DRIVE, WRECKED}; public enum CarState {LOCKED, OFF, IDLE, DRIVE, WRECKED};
@ -132,7 +138,7 @@ public class Car {
*/ */
public void tick() { public void tick() {
int rpmBonus = 0; int rpmBonus = 0;
System.err.println("Old state: " + this.currentState + ", Action: " + this.currentAction); Car.logger.debug("Old state: " + this.currentState + ", Action: " + this.currentAction);
switch(this.currentAction) { switch(this.currentAction) {
case STARTING: case STARTING:
this.motorOn = true; this.motorOn = true;
@ -198,7 +204,7 @@ public class Car {
this.currentAction = CarAction.NONE; this.currentAction = CarAction.NONE;
} }
this.generateContinousReportQuads(); this.generateContinousReportQuads();
System.err.println("New state: " + this.currentState); Car.logger.debug("New state: " + this.currentState);
} }
private void fireAirbagEvent() { private void fireAirbagEvent() {

View File

@ -3,11 +3,16 @@ package lu.jpt.csparqltest.rentacar;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import eu.larkc.csparql.cep.api.RdfQuadruple; import eu.larkc.csparql.cep.api.RdfQuadruple;
import lu.jpt.csparqltest.util.RandomHelper; import lu.jpt.csparqltest.util.RandomHelper;
public class Driver { public class Driver {
public static Logger logger = LoggerFactory.getLogger(Driver.class);
// Traits with corresponding behaviour for certain situations // Traits with corresponding behaviour for certain situations
public enum Trait {CAREFUL, REGULAR, FAST, INSANE}; public enum Trait {CAREFUL, REGULAR, FAST, INSANE};
private static final double[] chanceToEngageHandbrakeOnLock = {1.00, 0.85, 0.60, 0.05}; private static final double[] chanceToEngageHandbrakeOnLock = {1.00, 0.85, 0.60, 0.05};
@ -139,7 +144,7 @@ public class Driver {
} }
break; break;
default: default:
System.err.println("Driver cannot handle car state: " + carState); Driver.logger.error("Driver cannot handle car state: " + carState);
break; break;
} }
} }

View File

@ -3,12 +3,17 @@ package lu.jpt.csparqltest.rentacar;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import eu.larkc.csparql.cep.api.RdfQuadruple; import eu.larkc.csparql.cep.api.RdfQuadruple;
import eu.larkc.csparql.cep.api.RdfStream; import eu.larkc.csparql.cep.api.RdfStream;
import lu.jpt.csparqltest.util.WindowLoggingRdfStream; import lu.jpt.csparqltest.util.WindowLoggingRdfStream;
public class RentACarSimulation implements Runnable { public class RentACarSimulation implements Runnable {
public static Logger logger = LoggerFactory.getLogger(RentACarSimulation.class);
public static final String BASE_URI = "http://example.org/carSim"; public static final String BASE_URI = "http://example.org/carSim";
public static final String BASE_ONTOLOGY_IRI = BASE_URI + "/carSimulationOntology#"; public static final String BASE_ONTOLOGY_IRI = BASE_URI + "/carSimulationOntology#";
public static final String BASE_STREAM_IRI = BASE_URI + "/stream"; public static final String BASE_STREAM_IRI = BASE_URI + "/stream";
@ -34,7 +39,7 @@ public class RentACarSimulation implements Runnable {
this.drivers = new ArrayList<Driver>(); this.drivers = new ArrayList<Driver>();
for(int i = 0; i < numberOfCustomers; i++) { for(int i = 0; i < numberOfCustomers; i++) {
Driver driver = new Driver(i, this.carPool); Driver driver = new Driver(i, this.carPool);
System.out.println(driver); RentACarSimulation.logger.debug(driver.toString());
this.drivers.add(driver); this.drivers.add(driver);
} }
// Create streams // Create streams