001 /* 002 * Created on Aug 2, 2005 003 * 004 */ 005 package aima.test.learningtest; 006 007 import junit.framework.TestCase; 008 import aima.learning.framework.DataSet; 009 import aima.learning.framework.DataSetFactory; 010 import aima.learning.inductive.DLTest; 011 import aima.learning.inductive.DecisionList; 012 013 /** 014 * @author Ravi Mohan 015 * 016 */ 017 public class DecisionListTest extends TestCase { 018 public void testDecisonListWithNoTestsReturnsDefaultValue() 019 throws Exception { 020 DecisionList dlist = new DecisionList("Yes", "No"); 021 DataSet ds = DataSetFactory.getRestaurantDataSet(); 022 assertEquals("No", dlist.predict(ds.getExample(0))); 023 } 024 025 public void testDecisionListWithSingleTestReturnsTestValueIfTestSuccessful() 026 throws Exception { 027 DecisionList dlist = new DecisionList("Yes", "No"); 028 DataSet ds = DataSetFactory.getRestaurantDataSet(); 029 030 DLTest test = new DLTest(); 031 test.add("type", "French"); 032 033 dlist.add(test, "test1success"); 034 035 assertEquals("test1success", dlist.predict(ds.getExample(0))); 036 } 037 038 public void testDecisionListFallsThruToNextTestIfOneDoesntMatch() 039 throws Exception { 040 DecisionList dlist = new DecisionList("Yes", "No"); 041 DataSet ds = DataSetFactory.getRestaurantDataSet(); 042 043 DLTest test1 = new DLTest(); 044 test1.add("type", "Thai"); // doesn't match first example 045 dlist.add(test1, "test1success"); 046 047 DLTest test2 = new DLTest(); 048 test2.add("type", "French"); 049 dlist.add(test2, "test2success");// matches first example 050 051 assertEquals("test2success", dlist.predict(ds.getExample(0))); 052 } 053 054 public void testDecisionListFallsThruToDefaultIfNoTestMatches() 055 throws Exception { 056 DecisionList dlist = new DecisionList("Yes", "No"); 057 DataSet ds = DataSetFactory.getRestaurantDataSet(); 058 059 DLTest test1 = new DLTest(); 060 test1.add("type", "Thai"); // doesn't match first example 061 dlist.add(test1, "test1success"); 062 063 DLTest test2 = new DLTest(); 064 test2.add("type", "Burger"); 065 dlist.add(test2, "test2success");// doesn't match first example 066 067 assertEquals("No", dlist.predict(ds.getExample(0))); 068 } 069 070 public void testDecisionListHandlesEmptyDataSet() throws Exception { 071 // tests first base case of recursion 072 DecisionList dlist = new DecisionList("Yes", "No"); 073 DataSet ds = DataSetFactory.getRestaurantDataSet(); 074 075 DLTest test1 = new DLTest(); 076 test1.add("type", "Thai"); // doesn't match first example 077 dlist.add(test1, "test1success"); 078 } 079 080 public void testDecisionListMerge() throws Exception { 081 DecisionList dlist1 = new DecisionList("Yes", "No"); 082 DecisionList dlist2 = new DecisionList("Yes", "No"); 083 DataSet ds = DataSetFactory.getRestaurantDataSet(); 084 085 DLTest test1 = new DLTest(); 086 test1.add("type", "Thai"); // doesn't match first example 087 dlist1.add(test1, "test1success"); 088 089 DLTest test2 = new DLTest(); 090 test2.add("type", "French"); 091 dlist2.add(test2, "test2success");// matches first example 092 093 DecisionList dlist3 = dlist1.mergeWith(dlist2); 094 assertEquals("test2success", dlist3.predict(ds.getExample(0))); 095 } 096 097 }