package blang.engines.internals.factories;

import org.eclipse.xtext.xbase.lib.Pair;

import bayonet.smc.ParticlePopulation;
import blang.engines.AdaptiveJarzynski;
import blang.engines.internals.PosteriorInferenceEngine;
import blang.inits.GlobalArg;
import blang.inits.experiments.ExperimentResults;
import blang.io.BlangTidySerializer;
import blang.runtime.SampledModel;
import blang.runtime.internals.model2kernel.ChangeOfMeasureKernel;
import blang.runtime.internals.objectgraph.GraphAnalysis;

/**
 * Sequential Change of Measure implementation.
 */
public class SCM extends AdaptiveJarzynski<SampledModel> implements PosteriorInferenceEngine
{
  @GlobalArg ExperimentResults results = new ExperimentResults();
  
  ChangeOfMeasureKernel kernel;
  
  @Override
  public void setSampledModel(SampledModel model) 
  { 
    this.kernel = new ChangeOfMeasureKernel(model);
  }

  @SuppressWarnings("unchecked")
  @Override
  public void performInference() 
  {
    // create approx
    ParticlePopulation<SampledModel> approximation = getApproximation(kernel);
    
    // resample for now the last iteration to simplify processing downstream
    approximation = approximation.resample(random, resamplingScheme);
    
    // write samples
    BlangTidySerializer tidySerializer = new BlangTidySerializer(results.child("samples")); 
    int particleIndex = 0;
    for (SampledModel model : approximation.particles)  
      model.getSampleWriter(tidySerializer).write(Pair.of("sample", particleIndex++)); 
  }

  @Override
  public void check(GraphAnalysis analysis) 
  {
    // TODO: may want to check forward simulators ok
    return;
  }
}

@@ -491,16 +491,39 @@
 nalysis;
+%0Aimport briefj.BriefIO;
 %0A%0A/**%0A *
@@ -1232,24 +1232,289 @@
 heme);%0A    %0A
+    // write Z estimate%0A    double logNormEstimate = approximation.logNormEstimate();%0A    System.out.println(%22Normalization constant estimate: %22 + logNormEstimate);%0A    BriefIO.write(results.getFileInResultFolder(%22logNormEstimate.txt%22), %22%22 + logNormEstimate);%0A    %0A
     // write

Fix #69