package urban.transformers;
import static org.junit.Assert.*;
import org.antlr.runtime.RecognitionException;
import org.junit.Test;
import urban.model.Model;
import static urban.parser.UrbanParser.parse;
public class AddRatesFromShapesTransformerTest {
@Test
public void testTransform_FlipGenerator() throws RecognitionException {
String example =
"A(f~0) <-> A(f~1) @ 0, 0 \n"+
"%gen: 'G' A(f~0) <-> A(f~1) := 10\n ";
doTest(example, "A(f~0) <-> A(f~1) @ 10, 10\n");
}
@Test
public void testTransform_FlipGenerator_WithShape() throws RecognitionException {
String example =
"A(f~0) <-> A(f~1) @ 0, 0 \n"+
"%gen: 'G' A(f~0) <-> A(f~1) := 3.16227766016838\n "+
"%shape: 'S' A(f~0) := ln(10)\n ";
doTest(example, "A(f~0) <-> A(f~1) @ 10, 1\n");
}
@Test
public void testTransform_FlipGenerator_WithMultipleShape() throws RecognitionException {
String rule = "P(f~0, r!1),P(f~0, l!1, r!2, y),P(f~0, l!2) <-> P(f~0, r!1),P(f~1, l!1, r!2, y),P(f~0, l!2) @ ";
String example =
rule + "0, 0 \n"+
"%gen: 'G' P(f~0) <-> P(f~1) := 1.41421356237\n "+
"%shape: 'S' P(f~1) := ln(2)\n "+
"%shape: 'S2' P(f~0,r!1),P(f~0,l!1) := ln(0.1)\n "+
"%shape: 'S3' P(f~1,r!1),P(f~1,l!1) := ln(0.1)\n ";
doTest(example, rule + "0.1, 20\n");
}
@Test
public void testTransform_BindingGenerator() throws RecognitionException {
String rule = "P(a!1, f~0, y),Y(s~p, y),A(a!1) <-> P(a!1, f~0, y!2),Y(s~p, y!2),A(a!1) @ ";
String example =
rule + "0, 0 \n"+
"%gen: 'G' P(y), Y(y) <-> P(y!1), Y(y!1) := 31.6227766016838\n "+
"%shape: 'S4' P(f~0, y!1), Y(y!1) := ln(0.1)\n ";
doTest(example, rule + "100, 10\n");
}
private void doTest(String example, String expected) throws RecognitionException {
Model m = parse(example).model();
AddRatesFromShapesTransformer t = new AddRatesFromShapesTransformer();
m = t.transform(m);
assertEquals(expected, removeComments(m.toString()));
}
private String removeComments(String string) {
return string.replaceAll("#.*\n", "");
}
}