From 36a14510f382f1708ef00b999c726386f5a939da Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Sun, 2 Oct 2016 20:28:51 +0200 Subject: [PATCH] [TASK] Cleanups. --- .../lu/jpt/csparqltest/SimulationContext.java | 24 ++++++++++++++----- .../java/lu/jpt/csparqltest/rentacar/Car.java | 10 ++++++-- .../lu/jpt/csparqltest/rentacar/Driver.java | 7 +++++- .../rentacar/RentACarSimulation.java | 7 +++++- 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/main/java/lu/jpt/csparqltest/SimulationContext.java b/src/main/java/lu/jpt/csparqltest/SimulationContext.java index bc849f4..0237c02 100644 --- a/src/main/java/lu/jpt/csparqltest/SimulationContext.java +++ b/src/main/java/lu/jpt/csparqltest/SimulationContext.java @@ -4,6 +4,9 @@ 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; @@ -13,6 +16,8 @@ import lu.jpt.csparqltest.rentacar.RentACarSimulation; public class SimulationContext { + public static Logger logger = LoggerFactory.getLogger(SimulationContext.class); + public enum SimulationState { UNINITIALIZED, INITIALIZED, RUNNING, PAUSED, SUSPENDED, TORNDOWN, ERROR}; // Internal simulation context @@ -42,6 +47,7 @@ public class SimulationContext { } simulationThread = new Thread(simulation); simulationThread.start(); + SimulationContext.logger.info("Simulation started!"); this.currentState = SimulationState.RUNNING; } @@ -50,6 +56,7 @@ public class SimulationContext { throw new RuntimeException("Action not allowed in this state!"); } this.simulation.pauseSimulation(); + SimulationContext.logger.info("Simulation paused!"); this.currentState = SimulationState.PAUSED; } @@ -58,6 +65,7 @@ public class SimulationContext { throw new RuntimeException("Action not allowed in this state!"); } this.simulation.resumeSimulation(); + SimulationContext.logger.info("Simulation resumed!"); this.currentState = SimulationState.RUNNING; } @@ -69,6 +77,7 @@ public class SimulationContext { } this.simulation.pleaseStopSimulation(); this.simulationThread.interrupt(); + SimulationContext.logger.info("Simulation suspended!"); this.currentState = SimulationState.SUSPENDED; } @@ -84,7 +93,9 @@ public class SimulationContext { 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() { @@ -100,15 +111,15 @@ public class SimulationContext { // Initialize with true to allow use of timestamp function this.engine.initialize(true); // Debugging output - Main.logger.debug("CWD: " + System.getProperty("user.dir")); - Main.logger.debug("Engine from: " + engine.getClass().getProtectionDomain().getCodeSource()); + SimulationContext.logger.debug("CWD: " + System.getProperty("user.dir")); + SimulationContext.logger.debug("Engine from: " + 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) { - Main.logger.error(e.toString()); - Main.logger.error(e.getStackTrace().toString()); + SimulationContext.logger.error(e.toString()); + SimulationContext.logger.error(e.getStackTrace().toString()); } */ // Note: This is how local domain knowledge can be updated during runtime: @@ -135,12 +146,13 @@ public class SimulationContext { try { resultProxy = engine.registerQuery(query, true); } catch (ParseException e) { - Main.logger.error(e.toString()); - Main.logger.error(e.getStackTrace().toString()); + SimulationContext.logger.error(e.toString()); + SimulationContext.logger.error(e.getStackTrace().toString()); } 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/rentacar/Car.java b/src/main/java/lu/jpt/csparqltest/rentacar/Car.java index 05bce13..d8c79be 100644 --- a/src/main/java/lu/jpt/csparqltest/rentacar/Car.java +++ b/src/main/java/lu/jpt/csparqltest/rentacar/Car.java @@ -3,10 +3,16 @@ package lu.jpt.csparqltest.rentacar; import java.util.ArrayList; import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import eu.larkc.csparql.cep.api.RdfQuadruple; import lu.jpt.csparqltest.util.RandomHelper; public class Car { + + public static Logger logger = LoggerFactory.getLogger(Car.class); + // State-change related enums private enum CarAction {NONE, UNLOCKING, STARTING, ACCELERATING, BRAKING, CRASHING, STOPPING, LOCKING, HALTING}; public enum CarState {LOCKED, OFF, IDLE, DRIVE, WRECKED}; @@ -132,7 +138,7 @@ public class Car { */ public void tick() { 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) { case STARTING: this.motorOn = true; @@ -198,7 +204,7 @@ public class Car { this.currentAction = CarAction.NONE; } this.generateContinousReportQuads(); - System.err.println("New state: " + this.currentState); + Car.logger.debug("New state: " + this.currentState); } private void fireAirbagEvent() { diff --git a/src/main/java/lu/jpt/csparqltest/rentacar/Driver.java b/src/main/java/lu/jpt/csparqltest/rentacar/Driver.java index 598a597..e81bde2 100644 --- a/src/main/java/lu/jpt/csparqltest/rentacar/Driver.java +++ b/src/main/java/lu/jpt/csparqltest/rentacar/Driver.java @@ -3,10 +3,15 @@ package lu.jpt.csparqltest.rentacar; import java.util.ArrayList; import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import eu.larkc.csparql.cep.api.RdfQuadruple; import lu.jpt.csparqltest.util.RandomHelper; public class Driver { + + public static Logger logger = LoggerFactory.getLogger(Driver.class); // Traits with corresponding behaviour for certain situations public enum Trait {CAREFUL, REGULAR, FAST, INSANE}; @@ -139,7 +144,7 @@ public class Driver { } break; default: - System.err.println("Driver cannot handle car state: " + carState); + Driver.logger.error("Driver cannot handle car state: " + carState); break; } } diff --git a/src/main/java/lu/jpt/csparqltest/rentacar/RentACarSimulation.java b/src/main/java/lu/jpt/csparqltest/rentacar/RentACarSimulation.java index bf8522b..4fa59ce 100644 --- a/src/main/java/lu/jpt/csparqltest/rentacar/RentACarSimulation.java +++ b/src/main/java/lu/jpt/csparqltest/rentacar/RentACarSimulation.java @@ -3,12 +3,17 @@ package lu.jpt.csparqltest.rentacar; import java.util.ArrayList; 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.RdfStream; import lu.jpt.csparqltest.util.WindowLoggingRdfStream; 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_ONTOLOGY_IRI = BASE_URI + "/carSimulationOntology#"; public static final String BASE_STREAM_IRI = BASE_URI + "/stream"; @@ -34,7 +39,7 @@ public class RentACarSimulation implements Runnable { this.drivers = new ArrayList(); for(int i = 0; i < numberOfCustomers; i++) { Driver driver = new Driver(i, this.carPool); - System.out.println(driver); + RentACarSimulation.logger.debug(driver.toString()); this.drivers.add(driver); } // Create streams