1 | package felix.test; |
2 | |
3 | import static org.junit.Assert.*; |
4 | |
5 | import java.io.BufferedReader; |
6 | import java.io.FileReader; |
7 | import java.util.ArrayList; |
8 | import java.util.HashMap; |
9 | import java.util.HashSet; |
10 | import java.util.regex.Matcher; |
11 | import java.util.regex.Pattern; |
12 | |
13 | import org.junit.BeforeClass; |
14 | import org.junit.Test; |
15 | |
16 | import tuffy.util.UIMan; |
17 | |
18 | import felix.main.Felix; |
19 | import felix.parser.FelixCommandOptions; |
20 | import felix.util.FelixConfig; |
21 | import felix.util.FelixUIMan; |
22 | |
23 | |
24 | /** |
25 | * Test LR operator's quality. |
26 | * @author Ce Zhang |
27 | * |
28 | */ |
29 | public class LRTest{ |
30 | |
31 | public static ArrayList<Double> planCosts = new ArrayList<Double>(); |
32 | public static String exeTime = ""; |
33 | |
34 | |
35 | /** |
36 | * Test Marginal inference results of LR on a test case with manually-built ground truth. |
37 | */ |
38 | @Test |
39 | public final void testLRMarginal() { |
40 | try{ |
41 | String[] args = {"-e", "test/smoke/evidence.db", "-i", "test/smoke/prog.mln", |
42 | "-o", "test/testOutput.txt", "-queryFile", "test/smoke/query.db", "-marginal"}; |
43 | |
44 | for(int wwww = 0; wwww < 2; wwww++){ |
45 | |
46 | FelixConfig.overrideID(); |
47 | FelixCommandOptions options = FelixUIMan.parseCommand(args); |
48 | |
49 | if(wwww == 1){ |
50 | options.useDualDecomposition = true; |
51 | FelixConfig.nDDIT = 2; |
52 | } |
53 | |
54 | new Felix().run(options); |
55 | |
56 | String outfile = "test/testOutput.txt"; |
57 | |
58 | if(wwww == 1){ |
59 | outfile = outfile + "_snap_0"; |
60 | } |
61 | |
62 | //TODO: |
63 | |
64 | //Ground truth: |
65 | //Gray Smoke 0 => 0.59 |
66 | //Anna and Edward Smoke 1 => 0.52 |
67 | |
68 | /* |
69 | 0.52497918747894001 Smokes("Edward", "1") |
70 | 0.47502081252106004 Smokes("Edward", "0") |
71 | 0.401312339887548 Smokes("Gary", "1") |
72 | 0.598687660112452 Smokes("Gary", "0") |
73 | 0.52497918747894001 Smokes("Anna", "1") |
74 | 0.47502081252106004 Smokes("Anna", "0") |
75 | */ |
76 | |
77 | BufferedReader br = new BufferedReader(new FileReader("test/testOutput.txt")); |
78 | |
79 | Pattern p1 = Pattern.compile("0.52.*Smokes.*Edward.*1"); |
80 | Pattern p2 = Pattern.compile("0.47.*Smokes.*Edward.*0"); |
81 | Pattern p3 = Pattern.compile("0.40.*Smokes.*Gary.*1"); |
82 | Pattern p4 = Pattern.compile("0.59.*Smokes.*Gary.*0"); |
83 | Pattern p5 = Pattern.compile("0.52.*Smokes.*Anna.*1"); |
84 | Pattern p6 = Pattern.compile("0.47.*Smokes.*Anna.*0"); |
85 | |
86 | String line; |
87 | while( (line = br.readLine()) != null){ |
88 | int ct = 0; |
89 | |
90 | Matcher m = p1.matcher(line); |
91 | if(m.find()){ |
92 | ct++; |
93 | } |
94 | |
95 | m = p2.matcher(line); |
96 | if(m.find()){ |
97 | ct++; |
98 | } |
99 | |
100 | m = p3.matcher(line); |
101 | if(m.find()){ |
102 | ct++; |
103 | } |
104 | |
105 | m = p4.matcher(line); |
106 | if(m.find()){ |
107 | ct++; |
108 | } |
109 | |
110 | m = p5.matcher(line); |
111 | if(m.find()){ |
112 | ct++; |
113 | } |
114 | |
115 | m = p6.matcher(line); |
116 | if(m.find()){ |
117 | ct++; |
118 | } |
119 | |
120 | UIMan.warn(""+ct); |
121 | assertTrue(ct == 1); |
122 | |
123 | |
124 | } |
125 | } |
126 | |
127 | }catch(Exception e){ |
128 | e.printStackTrace(); |
129 | } |
130 | } |
131 | |
132 | /** |
133 | * Test MAP inference results of LR on a test case with manually-built ground truth. |
134 | */ |
135 | @Test |
136 | public final void testLRMAP() { |
137 | try{ |
138 | String[] args = {"-e", "test/smoke/evidence.db", "-i", "test/smoke/prog.mln", |
139 | "-o", "test/testOutput.txt", "-queryFile", "test/smoke/query.db"}; |
140 | |
141 | |
142 | for(int wwww = 0; wwww < 2; wwww++){ |
143 | |
144 | FelixConfig.overrideID(); |
145 | FelixCommandOptions options = FelixUIMan.parseCommand(args); |
146 | |
147 | if(wwww == 1){ |
148 | options.useDualDecomposition = true; |
149 | FelixConfig.nDDIT = 2; |
150 | } |
151 | |
152 | String outfile = "test/testOutput.txt"; |
153 | |
154 | if(wwww == 1){ |
155 | outfile = outfile + "_snap_1"; |
156 | } |
157 | |
158 | new Felix().run(options); |
159 | |
160 | //Ground truth: |
161 | //Gray Smoke 0 |
162 | //Anna and Edward Smoke 1 |
163 | |
164 | /* |
165 | Smokes("Edward", "1") |
166 | Smokes("Anna", "1") |
167 | Smokes("Gary", "0") |
168 | */ |
169 | |
170 | BufferedReader br = new BufferedReader(new FileReader(outfile)); |
171 | |
172 | Pattern p1 = Pattern.compile("Smokes.*Edward.*1"); |
173 | Pattern p4 = Pattern.compile("Smokes.*Gary.*0"); |
174 | Pattern p5 = Pattern.compile("Smokes.*Anna.*1"); |
175 | |
176 | String line; |
177 | while( (line = br.readLine()) != null){ |
178 | int ct = 0; |
179 | |
180 | Matcher m = p1.matcher(line); |
181 | if(m.find()){ |
182 | ct++; |
183 | } |
184 | |
185 | m = p4.matcher(line); |
186 | if(m.find()){ |
187 | ct++; |
188 | } |
189 | |
190 | m = p5.matcher(line); |
191 | if(m.find()){ |
192 | ct++; |
193 | } |
194 | |
195 | assertTrue(ct == 1); |
196 | |
197 | } |
198 | |
199 | } |
200 | |
201 | }catch(Exception e){ |
202 | e.printStackTrace(); |
203 | assertTrue(false); |
204 | } |
205 | } |
206 | |
207 | |
208 | |
209 | } |
210 | |
211 | |
212 | |
213 | |
214 | |
215 | |