package urban.shapes;
import static org.junit.Assert.*;
import static urban.util.Pair.pairOf;
import static urban.parser.UrbanParser.parse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.antlr.runtime.RecognitionException;
import org.junit.Test;
import urban.model.Agent;
import urban.model.Rule;
import urban.model.Site;
import urban.parser.UrbanParser;
public class RuleFactoryTest {
private static final Generator p01 = gen("P(f~0) <-> P(f~1)");
private static final Generator py = gen("P(s),Y(s) <-> P(s!1),Y(s!1)");
private static final Generator aa = gen("A(a),A(a) <-> A(a!1),A(a!1)");
private static final Shape ab = shape("A(a,b!1),B(b!1)");
private static final Shape p0xp0y = shape("P(f~0,x!1),P(f~0,y!1)");
private static final Shape p1xp1y = shape("P(f~1,x!1),P(f~1,y!1)");
private static final Shape p0y = shape("P(f~0,s!1),Y(s!1)");
private static final Shape p1y = shape("P(f~1,s!1),Y(s!1)");
private static final Shape S1 = new Shape(A(f("0")));
private static final Shape S2 = new Shape(A(f("0"),x(null)));
private static final Shape S3 = new Shape(A(f("0"),x("_")));
private static Set<Agent> A(Site... all) {
return Collections.singleton(new Agent("A",Arrays.asList(all)));
}
private static Site f(String string) {
return new Site("A","f",string,null);
}
private static final Site x(String string){
return new Site("A","x",null,string);
}
private static final Generator G = gen("A(f~0) <-> A(f~1)");
private static final Generator G2 = gen("B(f~0) <-> B(f~1)");
@Test
public void testGenerateRules_SimpleFlip() {
ShapeParameters p = new ShapeParameters();
p.constants.put(G,0.5);
p.epsilons.put(S1, -1.0);
p.lambda.put(pairOf(G,S1), -0.5);
doTest(p,"A(f~0) <-> A(f~1) @ 0, 0\n");
}
@Test
public void testGenerateRules_SimpleBond() {
ShapeParameters p = new ShapeParameters();
p.constants.put(py,0.0);
p.epsilons.put(p0y, -1.0);
p.epsilons.put(p1y, -1.0);
doTest(p,
"P(f~0, s),Y(s) <-> P(f~0, s!1),Y(s!1) @ 0, 0\n",
"P(f~1, s),Y(s) <-> P(f~1, s!1),Y(s!1) @ 0, 0\n"
);
}
@Test
public void testGenerateRules_SimpleFlip_NonMatchingShapes() {
ShapeParameters p = new ShapeParameters();
p.constants.put(G2,0.0);
p.epsilons.put(S1, -1.0);
p.lambda.put(pairOf(G,S1), -0.5);
doTest(p,"B(f~0) <-> B(f~1) @ 0, 0\n");
}
@Test
public void testGenerateRules_Flip2() {
ShapeParameters p = new ShapeParameters();
p.constants.put(G,0.0);
p.epsilons.put(S2, -2.0);
p.epsilons.put(S3, 0.0);
p.lambda.put(pairOf(G,S2), -0.5);
doTest(p,
"A(f~0, x!_) <-> A(f~1, x!_) @ 0, 0\n",
"A(f~0, x) <-> A(f~1, x) @ 0, 0\n"
);
}
@Test
public void testGenerateRules_Flip3() {
ShapeParameters p = new ShapeParameters();
p.constants.put(G,0.0);
p.epsilons.put(S3, 0.0);
p.lambda.put(pairOf(G,S2), -0.5);
doTest(p,
"A(f~0, x!_) <-> A(f~1, x!_) @ 0, 0\n"
);
}
private void doTest(ShapeParameters p, String... expected) {
RuleFactory factory = new RuleFactory(p);
List<Rule> rules = factory.generateRules();
assertEquals(rules.toString(),expected.length, rules.size());
for(int i=0; i<expected.length; i++)
assertEquals(expected[i], rules.get(i).toString());
}
@Test
public void testGenerateRules_CS1() {
ShapeParameters p = new ShapeParameters();
p.constants.put(p01, -0.349485);
p.epsilons.put(p0xp0y, -1.0);
doTest(p, "P(f~0, x!1, y!2),P(f~0, y!1),P(f~0, x!2) <-> P(f~1, x!1, y!2),P(f~0, y!1),P(f~0, x!2) @ 0, 0\n");
}
@Test
public void testGenerateRules_CS2() {
ShapeParameters p = new ShapeParameters();
p.constants.put(p01, -0.349485);
p.epsilons.put(p1xp1y, -1.0);
doTest(p, "P(f~0, x!1, y!2),P(f~1, y!1),P(f~1, x!2) <-> P(f~1, x!1, y!2),P(f~1, y!1),P(f~1, x!2) @ 0, 0\n");
}
@Test
public void testGenerateRules_MultipleMatch() {
ShapeParameters p = new ShapeParameters();
p.constants.put(aa, 1.0);
p.epsilons.put(ab, 1.0);
doTest(p,
"A(a, b!1),A(a, b!2),B(b!1),B(b!2) <-> A(a!1, b!2),A(a!1, b!3),B(b!2),B(b!3) @ 0, 0\n");
}
@Test
public void testGenerateRules_MultipleMatch2() {
ShapeParameters p = new ShapeParameters();
p.constants.put(aa, 1.0);
p.epsilons.put(ab, 1.0);
p.epsilons.put(shape("A(a,b)"), 1.0);
doTest(p, "A(a, b!1),A(a, b!2),B(b!1),B(b!2) <-> A(a!1, b!2),A(a!1, b!3),B(b!2),B(b!3) @ 0, 0\n",
"A(a, b!1),A(a, b),B(b!1) <-> A(a!1, b!2),A(a!1, b),B(b!2) @ 0, 0\n",
"A(a, b),A(a, b) <-> A(a!1, b),A(a!1, b) @ 0, 0\n"
);
}
private static Shape shape(String string) {
try {
return new Shape(parse(string).expression());
} catch (RecognitionException e) {
e.printStackTrace();
}
return null;
}
private static Generator gen(String text){
return Generator.createGeneratorFromString(text);
}
}