Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access simulation parameters through probes #4

Open
yoannkubera opened this issue Jan 28, 2019 · 3 comments
Open

Access simulation parameters through probes #4

yoannkubera opened this issue Jan 28, 2019 · 3 comments

Comments

@yoannkubera
Copy link
Collaborator

yoannkubera commented Jan 28, 2019

Issue witnessed in commit: 52c5843

Issue description

Probes cannot access neither the simulation parameters, nor the simulation model.

Issue location

In the interface ISimulationEngine (no such methods can be found).

Possible solutions

Add a wrapper for simulation engines, adding such methods :

public class ExtendedEngineWrapper implements ISimulationEngine {
   private AbstractExtendedSimulationModel model;
   private ISimulationEngine engine;
   public ExtendedEngineWrapper( ISimulationEngine engine ){
      this.engine = engine;
   }
   public runNewSimulation( AbstractExtendedSimulationModel model ){
      this.model = (AbstractExtendedSimulationModel) model:
      this.engine.runNewSimulation( model );
   }
   public runNewSimulation( ISimulationModel model ){
      if( ! ( model instanceof AbstractExtendedSimulationModel ) )
          throw IllegalArgumentException( "The model is not an instance of AbstractExtendedSimulationModel" );
      this.runNewSimulation( (AbstractExtendedSimulationModel) model ):
   }
   public ISimulationParameters getParameters() {
      if( this.model == null ) {
         throw new IllegalStateException( "The engine has no model" );
      }
      return this.model.getParameters();
   }
  //... Plus an implementation of all other methods of ISimulationEngine, calling the corresponding
  // method of this.engine
}

Edit: A proper implementation can be found here. Note that it does not work appropriately (see the comments below).

@yoannkubera
Copy link
Collaborator Author

Then, a probe has to cast the engine to ExtendedEngineWrapper to access the simulation parameters.

This raises another question : should be add a type parameter to probes (the type of engine) ?

@yoannkubera
Copy link
Collaborator Author

yoannkubera commented Jan 28, 2019

Nevermind, it is more complicated than that to implement.

Problem: When the methods of the probe are called, the engine provided as a parameter is the wrapped engine, and not the wrapper : the call to probes in the simulation process is made by the wrapped engine, with probe.observeAtInitialTime( time, this ).

To cope with this problem, a reference to the engine should be stored inside the probe when it is added to an engine. The methods of the probe should be call with that value instead of the engine.

Example:

public interface IProbe {
   ...
   ISimulationEngine getHost();
   void setHost( ISimulationEngine e );
}
public EngineImplementation implements ISimulationEngine {
   ...
   public void addProbe(String k, IProbe p) {
      this.probes.put( k, p );
      p.setHost( this );
   }
   public runNewSimulation( ISimulationModel model ) {
      ...
     // After the initialization of the simulation, in the loop over the probes
     probe.observeAtInitialTimes( initialTimestamp, probe.getHost() );
      ...
   }
}
public class ExtendedEngineWrapper implements ISimulationEngine {
   ...
   public void addProbe(String arg0, IProbe arg1) {
      this.engine.addProbe(arg0, arg1);
      arg1.setHost( this );
   }
}

@yoannkubera
Copy link
Collaborator Author

In the end, very complicated thing for very little benefits. In simple cases (when the engines runs with the same instance of the model), it is easier to provide the parameters as an attribute of the probe.

It still raises one philosophical question : are parameters part of the model, or only a means to initialize the content of the model ? In the latter, providing the parameters as an attribute of the probe is not a breach of the similar philosophy. Else, it introduces an unwanted hard link between model and view (probes).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant