package urban.transformers;
import urban.model.Model;
import urban.model.Rule;
import urban.model.Statement;
import urban.model.info.ModelInfo;
import urban.shapes.RateCalculator;
import urban.shapes.ShapeParameters;
/**
* Uses shapes to calculate rates for bidirectional rules with rates that are zero.
*
* @see RateCalculator
*/
public class AddRatesFromShapesTransformer implements ModelTransformer {
@Override
public Model transform(Model in) {
ShapeParameters sp = new ModelInfo(in.getLines()).getShapeParameters();
RateCalculator rc = new RateCalculator(sp);
Model out = new Model();
for(Statement line : in.getLines())
if (line instanceof Rule)
out.addStatement(addRate(rc, (Rule)line));
else
out.addStatement(line);
return out;
}
private Statement addRate(RateCalculator rc, Rule line) {
if (!line.isBidirectional())
return line;
if (line.getForwardRate() != 0.0 || line.getBackwardRate() != 0.0)
return line;
return rc.calculateRates(new RuleToGeneratorTransformer().transform(line), line);
}
}