简体   繁体   中英

Use log system to store time simulation data instead of data structure?

I am building simulators which simulates the evolution of a body through time. I need to store its position / velocity coordinates for every simulated time steps and sometimes some other data which could be for example the orientation of the body, or any other physical related timestamped data based on simulation time.

I run into a problem which is that the available data that I need to store is often only known by one component of my system, let us say a sensor.

Currently I have implemented a singleton to store the data in order to avoid passing a data structure down from the body down to the sensor. This singleton is used for the body and the sensor (and some other parts of the system):

class Body {
   HistoricalData historicalData = new HistoricalData();  // SINGLETON !

   Body() {
       this.sensor = new Sensor();
   }
   [...]

   public void compute() {
   [...]
   this.sensor.compute();
   this.historicalData.append(currentTime, "variableName", currentVariableValue);
   }
}

class Sensor {
   HistoricalData historicalData = new HistoricalData();  // SINGLETON !

   [...]

   public void compute() {
       [...]
       this.historicalData.append(currentTime, "sensorVariableName", currentSensorVariableValue);
   }
}

The problem is that I don't really like singletons (problems if run multiple times, concurrency problems, etc.).

The other approach using a data structure I was thinking is as follow:

class Body {
   HistoricalData historicalData = null;

   Body(HistoricalData historicalData) {
       this.sensor = new Sensor(historicalData);
       this.historicalData = historicalData; 
   }
   [...]

   public void compute() {
       [...]
       this.historicalData.append(currentTime, "variableName", currentVariableValue);
   }
}

The problem with this last approach is that when it is a sub sub component that has information to be stored, well, I need to pass the HistoricalData object from the master component down to the sub sub component, going through all sub components...complexifying constructors signatures.

I was thinking of using a logger system, and then parse logs information to reconstruct my data structure. For example in Java, it would look like:

public class Main {
    private static Logger logger = LogManager.getLogger(Main.class);

    public static void main(String[] args) throws ParseException {
        [...]
        body.compute();
        logger.info("simlation_time: " + time + " ; subsubsystemvalue: " + currentValue);
        [...]
        // reconstruction of data from logger
        var historicalData = new HistoricalData(logger);
    }

Is this a bad practice ? I am wondering if you have another elegant solution...

Why would it be better to abuse a logging system and parse text lines instead of using real data structures?

I don't really understand your use case but I still have some suggestions:

  • Don't use this for no reason: this.sensor.compute();
  • Don't set fields to null for no reason, it's already null by default
  • Take a look on what is Dependency Injection and Inversion of Control

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM