package urban.shapes;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.antlr.runtime.RecognitionException;
import org.apache.commons.collections15.CollectionUtils;
import org.junit.Test;
import urban.model.Agent;
import urban.model.Rule;
import urban.parser.UrbanParser;
import urban.transformers.RuleGraphToRuleTransformer;
public class ShapeTest {
@Test
public void testGetMatchPermutations1() throws RecognitionException{
doTest(gen("P(f~0) <-> P(f~1)"),
new Shape(parse("P(f~1, r!1), P(f~1, l!1)")),
"P(f~0, l!1),P(f~1, r!1) <-> P(f~1, l!1),P(f~1, r!1)",
"P(f~0, r!1),P(f~1, l!1) <-> P(f~1, r!1),P(f~1, l!1)"
);
}
@Test
public void testGetMatchPermutations2() throws RecognitionException{
doTest(gen("P(f~0) <-> P(f~1)"),
new Shape(parse("P(f~0, r!1), P(f~1, l!1)")),
"P(f~0, r!1),P(f~1, l!1) <-> P(f~1, r!1),P(f~1, l!1)",
"P(f~0, l!1),P(f~0, r!1) <-> P(f~1, l!1),P(f~0, r!1)");
}
@Test
public void testGetMatchPermutations3() throws RecognitionException {
doTest( gen("A(a),B(b) <-> A(a!1),B(b!1)"),
new Shape(parse("A(a,b~0)")),
"A(a, b~0),B(b) <-> A(a!1, b~0),B(b!1)");
}
@Test
public void testGetMatchPermutations4() throws RecognitionException {
doTest( gen("A(a),B(b) <-> A(a!1),B(b!1)"),
new Shape(parse("A(a!1,b~0),B(b!1)")),
"A(a, b~0),B(b) <-> A(a!1, b~0),B(b!1)");
}
@Test
public void testGetMatchPermutations6() throws RecognitionException {
doTest( gen("A(b),B(a) <-> A(b!1),B(a!1)"),
new Shape(parse("B(a,c)")),
"A(b),B(a, c) <-> A(b!1),B(a!1, c)");
}
@Test
public void testGetMatchPermutations7() throws RecognitionException {
doTest( gen("A(b),B(a) <-> A(b!1),B(a!1)"),
new Shape(parse("B(a,c!1),C(b!1)")),
"A(b),B(a, c!1),C(b!1) <-> A(b!1),B(a!1, c!2),C(b!2)");
}
@Test
public void testGetMatchPermutations5() throws RecognitionException {
doTest( gen("A(b),B(a) <-> A(b!1),B(a!1)"),
new Shape(parse("A(b!1,c!2),B(a!1,c!3),C(a!2,b!3)")),
"A(b, c!1),B(a, c!2),C(a!1, b!2) <-> A(b!1, c!2),B(a!1, c!3),C(a!2, b!3)");
}
private void doTest(Generator generator, Shape s, String... expected) {
Collection<RuleGraph> rules = s.getMatchPermutations(generator.getRuleGraph());
assertEquals(expected.length, rules.size());
int i=0;
for(Rule rule : CollectionUtils.collect(rules, new RuleGraphToRuleTransformer())){
assertEquals(expected[i++]+" @ 0, 0\n", rule.toString());
}
}
private List<Agent> parse(String string) throws RecognitionException {
return new ArrayList<Agent>(UrbanParser.parse(string).expression());
}
private static Generator gen(String text){
return Generator.createGeneratorFromString(text);
}
}