001    package aima.test.coretest;
002    
003    import junit.framework.TestCase;
004    import aima.basic.Percept;
005    import aima.basic.simplerule.ANDCondition;
006    import aima.basic.simplerule.EQUALCondition;
007    import aima.basic.simplerule.NOTCondition;
008    import aima.basic.simplerule.ORCondition;
009    import aima.basic.simplerule.Rule;
010    
011    /**
012     * @author Ciaran O'Reilly
013     * 
014     */
015    public class RuleTest extends TestCase {
016    
017            public void testEQUALRule() {
018                    Rule r = new Rule(
019                                    new EQUALCondition("car-in-front-is-braking", "true"),
020                                    "initiate-braking");
021    
022                    assertEquals("initiate-braking", r.getAction());
023    
024                    assertEquals("if car-in-front-is-braking==true then initiate-braking.",
025                                    r.toString());
026    
027                    assertEquals(true, r.evaluate(new Percept("car-in-front-is-braking",
028                                    "true")));
029    
030                    assertEquals(false, r.evaluate(new Percept("car-in-front-is-braking",
031                                    "false")));
032    
033                    assertEquals(false, r.evaluate(new Percept(
034                                    "car-in-front-is-indicating", "true")));
035            }
036    
037            public void testNOTRule() {
038                    Rule r = new Rule(new NOTCondition(new EQUALCondition(
039                                    "car-in-front-is-braking", "true")), "initiate-braking");
040    
041                    assertEquals("initiate-braking", r.getAction());
042    
043                    assertEquals(
044                                    "if ![car-in-front-is-braking==true] then initiate-braking.", r
045                                                    .toString());
046    
047                    assertEquals(false, r.evaluate(new Percept("car-in-front-is-braking",
048                                    "true")));
049    
050                    assertEquals(true, r.evaluate(new Percept("car-in-front-is-braking",
051                                    "false")));
052    
053                    assertEquals(true, r.evaluate(new Percept("car-in-front-is-indicating",
054                                    "true")));
055            }
056    
057            public void testANDRule() {
058                    Rule r = new Rule(new ANDCondition(new EQUALCondition(
059                                    "car-in-front-is-braking", "true"), new EQUALCondition(
060                                    "car-in-front-tires-smoking", "true")), "emergency-braking");
061    
062                    assertEquals("emergency-braking", r.getAction());
063    
064                    assertEquals(
065                                    "if [car-in-front-is-braking==true && car-in-front-tires-smoking==true] then emergency-braking.",
066                                    r.toString());
067    
068                    assertEquals(false, r.evaluate(new Percept("car-in-front-is-braking",
069                                    "true")));
070    
071                    assertEquals(false, r.evaluate(new Percept(
072                                    "car-in-front-tires-smoking", "true")));
073    
074                    assertEquals(true, r.evaluate(new Percept("car-in-front-is-braking",
075                                    "true", "car-in-front-tires-smoking", "true")));
076    
077                    assertEquals(false, r.evaluate(new Percept("car-in-front-is-braking",
078                                    "false", "car-in-front-tires-smoking", "true")));
079    
080                    assertEquals(false, r.evaluate(new Percept("car-in-front-is-braking",
081                                    "true", "car-in-front-tires-smoking", "false")));
082            }
083    
084            public void testORRule() {
085                    Rule r = new Rule(new ORCondition(new EQUALCondition(
086                                    "car-in-front-is-braking", "true"), new EQUALCondition(
087                                    "car-in-front-tires-smoking", "true")), "emergency-braking");
088    
089                    assertEquals("emergency-braking", r.getAction());
090    
091                    assertEquals(
092                                    "if [car-in-front-is-braking==true || car-in-front-tires-smoking==true] then emergency-braking.",
093                                    r.toString());
094    
095                    assertEquals(true, r.evaluate(new Percept("car-in-front-is-braking",
096                                    "true")));
097    
098                    assertEquals(true, r.evaluate(new Percept("car-in-front-tires-smoking",
099                                    "true")));
100    
101                    assertEquals(true, r.evaluate(new Percept("car-in-front-is-braking",
102                                    "true", "car-in-front-tires-smoking", "true")));
103    
104                    assertEquals(true, r.evaluate(new Percept("car-in-front-is-braking",
105                                    "false", "car-in-front-tires-smoking", "true")));
106    
107                    assertEquals(true, r.evaluate(new Percept("car-in-front-is-braking",
108                                    "true", "car-in-front-tires-smoking", "false")));
109    
110                    assertEquals(false, r.evaluate(new Percept("car-in-front-is-braking",
111                                    "false", "car-in-front-tires-smoking", "false")));
112            }
113    }