| 1 | package felix.test; |
| 2 | |
| 3 | import static org.junit.Assert.*; |
| 4 | |
| 5 | import java.io.BufferedReader; |
| 6 | import java.io.BufferedWriter; |
| 7 | import java.io.FileReader; |
| 8 | import java.io.FileWriter; |
| 9 | import java.util.ArrayList; |
| 10 | |
| 11 | import org.junit.BeforeClass; |
| 12 | import org.junit.Test; |
| 13 | |
| 14 | import tuffy.db.RDB; |
| 15 | import tuffy.util.Config; |
| 16 | import tuffy.util.FileMan; |
| 17 | import tuffy.util.UIMan; |
| 18 | |
| 19 | import felix.main.Felix; |
| 20 | import felix.parser.FelixCommandOptions; |
| 21 | import felix.util.FelixConfig; |
| 22 | import felix.util.FelixUIMan; |
| 23 | |
| 24 | /** |
| 25 | * Test using evidence supplied in existing database relations. |
| 26 | * @author Josh Slauson |
| 27 | * |
| 28 | */ |
| 29 | public class IODBTest { |
| 30 | |
| 31 | static RDB db; |
| 32 | |
| 33 | /** |
| 34 | * Perform required setup for this test. |
| 35 | */ |
| 36 | @BeforeClass |
| 37 | public static final void setUp() { |
| 38 | // delete existing output files |
| 39 | FileMan.removeFile("test/felix/output/out.txt_IODBTest_simpleTest"); |
| 40 | FileMan.removeFile("test/felix/output/out.txt_IODBTest_comboTest"); |
| 41 | FileMan.removeFile("test/felix/output/out.txt_IODBTest_emptyTable"); |
| 42 | FileMan.removeFile("test/felix/output/out.txt_IODBTest_incorrectMissingTable"); |
| 43 | FileMan.removeFile("test/felix/output/out.txt_IODBTest_incorrectColumnName"); |
| 44 | FileMan.removeFile("test/felix/output/out.txt_IODBTest_incorrectMissingIndices"); |
| 45 | FileMan.removeFile("test/felix/output/out.txt_IODBTest_incorrectMissingTruthPrior"); |
| 46 | |
| 47 | FelixConfig.overrideID(); |
| 48 | |
| 49 | UIMan.parseConfigFile(Config.path_conf); |
| 50 | |
| 51 | if(FelixConfig.evidDBSchema != null){ |
| 52 | FelixConfig.db_schema = FelixConfig.evidDBSchema; |
| 53 | } |
| 54 | |
| 55 | db = RDB.getRDBbyConfig(); |
| 56 | db.schema = FelixConfig.db_schema; |
| 57 | db.resetSchema(FelixConfig.db_schema); |
| 58 | |
| 59 | if(!db.isSchemaExists("felix_test")) { |
| 60 | String query = "CREATE SCHEMA felix_test"; |
| 61 | db.execute(query); |
| 62 | } |
| 63 | |
| 64 | db.dropTable("felix_test.evid"); |
| 65 | String query = "CREATE TABLE felix_test.evid (truth boolean, prior integer, a1 text, b2 text)"; |
| 66 | db.execute(query); |
| 67 | |
| 68 | query = "INSERT INTO felix_test.evid VALUES (true, 0, 1, 2), (true, 0, 4, 5)"; |
| 69 | db.execute(query); |
| 70 | |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Checks if output file contains expected results. |
| 75 | * @param output |
| 76 | * @param expect |
| 77 | */ |
| 78 | public void checkOutput(String output, String[] expect) { |
| 79 | ArrayList<String> lines = FileMan.getLines(output); |
| 80 | System.out.println("Checking output size: " + lines.size() + " = " + expect.length/2 + "?"); |
| 81 | assertTrue(lines.size() == expect.length/2); |
| 82 | |
| 83 | for(int i = 0; i < lines.size(); i++) { |
| 84 | System.out.println("Checking output: " + lines.get(i)); |
| 85 | |
| 86 | boolean match = false; |
| 87 | for(int j = 0; j + 1 < expect.length; j+=2) { |
| 88 | if(lines.get(i).equals("fact(\"" + expect[j] + "\", \"" + expect[j+1] + "\")")) { |
| 89 | match = true; |
| 90 | } |
| 91 | } |
| 92 | if(!match) { |
| 93 | System.out.println("\tNo match"); |
| 94 | } |
| 95 | assertTrue(match); |
| 96 | } |
| 97 | |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Simple smoke test. |
| 102 | */ |
| 103 | @Test |
| 104 | public void simpleTest() { |
| 105 | try { |
| 106 | |
| 107 | String output = "test/felix/output/out.txt_IODBTest_simpleTest"; |
| 108 | String[] args = {"-i", "test/felix/io_test/prog.mln-db", "-o", |
| 109 | "test/felix/output/out.txt_IODBTest_simpleTest", "-queryFile", "test/felix/io_test/query.db", "-keepData"}; |
| 110 | |
| 111 | FelixCommandOptions options = FelixUIMan.parseCommand(args); |
| 112 | new Felix().run(options); |
| 113 | |
| 114 | String[] expect = {"1", "2", "4", "5"}; |
| 115 | checkOutput(output, expect); |
| 116 | |
| 117 | } catch(Exception e) { |
| 118 | e.printStackTrace(); |
| 119 | assertTrue(false); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Combo test using evidence from a file and evidence from a database table. |
| 125 | */ |
| 126 | @Test |
| 127 | public void comboTest() { |
| 128 | try { |
| 129 | |
| 130 | String output = "test/felix/output/out.txt_IODBTest_comboTest"; |
| 131 | String[] args = {"-e", "test/felix/io_test/evid2.db", "-i", "test/felix/io_test/prog2.mln-db", "-o", |
| 132 | output, "-queryFile", "test/felix/io_test/query.db", "-keepData"}; |
| 133 | |
| 134 | FelixCommandOptions options = FelixUIMan.parseCommand(args); |
| 135 | new Felix().run(options); |
| 136 | |
| 137 | String[] expect = {"1", "2", "4", "5", "Hello", "World", "UW", "Madison"}; |
| 138 | checkOutput(output, expect); |
| 139 | |
| 140 | } catch(Exception e) { |
| 141 | e.printStackTrace(); |
| 142 | assertTrue(false); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // @Test |
| 147 | // public void emptyTable() { |
| 148 | // try { |
| 149 | // |
| 150 | // // Create evidence table |
| 151 | // db.dropTable("felix_test.evid"); |
| 152 | // String query = "CREATE TABLE felix_test.evid (truth boolean, prior integer, a1 text, b2 text)"; |
| 153 | // db.execute(query); |
| 154 | // |
| 155 | // String output = "test/felix/output/out.txt_IODBTest_emptyTable"; |
| 156 | // String[] args = {"-i", "test/felix/io_test/prog.mln-db", "-o", output, |
| 157 | // "-queryFile", "test/felix/io_test/query.db", "-keepData"}; |
| 158 | // |
| 159 | // FelixCommandOptions options = FelixUIMan.parseCommand(args); |
| 160 | // new Felix().run(options); |
| 161 | // |
| 162 | // checkOutput(output); |
| 163 | // |
| 164 | // } catch(Exception e) { |
| 165 | // e.printStackTrace(); |
| 166 | // assertTrue(false); |
| 167 | // } |
| 168 | // } |
| 169 | |
| 170 | // TODO: fix issue, then uncomment |
| 171 | // @Test |
| 172 | // public void incorrectMissingTable() { |
| 173 | // try { |
| 174 | // |
| 175 | // db.dropTable("felix_test.evid"); |
| 176 | // |
| 177 | // String output = "test/felix/output/out.txt_IODBTest_incorrectMissingTable"; |
| 178 | // String[] args = {"-i", "test/felix/io_test/prog.mln-db", "-o", output, |
| 179 | // "-queryFile", "test/felix/io_test/query.db", "-keepData"}; |
| 180 | // |
| 181 | // FelixCommandOptions options = FelixUIMan.parseCommand(args); |
| 182 | // new Felix().run(options); |
| 183 | // |
| 184 | // checkOutput(output); |
| 185 | // |
| 186 | // } catch(Exception e) { |
| 187 | // e.printStackTrace(); |
| 188 | // assertTrue(false); |
| 189 | // } |
| 190 | // } |
| 191 | |
| 192 | // TODO: fix issue, then uncomment |
| 193 | // @Test |
| 194 | // public void incorrectColumnName() { |
| 195 | // try { |
| 196 | // |
| 197 | // // Create evidence table |
| 198 | // db.dropTable("felix_test.evid"); |
| 199 | // String query = "CREATE TABLE felix_test.evid (truth boolean, prior integer, a1 text, a2 text)"; |
| 200 | // db.execute(query); |
| 201 | // |
| 202 | // query = "INSERT INTO felix_test.evid VALUES (true, 0, 1, 2), (true, 0, 4, 5)"; |
| 203 | // db.execute(query); |
| 204 | // |
| 205 | // String output = "test/felix/output/out.txt_IODBTest_incorrectColumnName"; |
| 206 | // String[] args = {"-i", "test/felix/io_test/prog.mln-db", "-o", output, |
| 207 | // "-queryFile", "test/felix/io_test/query.db", "-keepData"}; |
| 208 | // |
| 209 | // FelixCommandOptions options = FelixUIMan.parseCommand(args); |
| 210 | // new Felix().run(options); |
| 211 | // |
| 212 | // checkOutput(output); |
| 213 | // |
| 214 | // } catch(Exception e) { |
| 215 | // e.printStackTrace(); |
| 216 | // assertTrue(false); |
| 217 | // } |
| 218 | // } |
| 219 | |
| 220 | |
| 221 | // TODO: fix issue, then uncomment |
| 222 | // @Test |
| 223 | // public void incorrectMissingIndices() { |
| 224 | // try { |
| 225 | // |
| 226 | // // Create evidence table |
| 227 | // db.dropTable("felix_test.evid.evid"); |
| 228 | // String query = "CREATE TABLE felix_test.evid (truth boolean, prior integer, a text, b text)"; |
| 229 | // db.execute(query); |
| 230 | // |
| 231 | // query = "INSERT INTO felix_test.evid VALUES (true, 0, 1, 2), (true, 0, 4, 5)"; |
| 232 | // db.execute(query); |
| 233 | // |
| 234 | // String output = "test/felix/output/out.txt_IODBTest_incorrectMissingIndices"; |
| 235 | // String[] args = {"-i", "test/felix/io_test/prog.mln-db", "-o", output, |
| 236 | // "-queryFile", "test/felix/io_test/query.db", "-keepData"}; |
| 237 | // |
| 238 | // FelixCommandOptions options = FelixUIMan.parseCommand(args); |
| 239 | // new Felix().run(options); |
| 240 | // |
| 241 | // checkOutput(output); |
| 242 | // |
| 243 | // } catch(Exception e) { |
| 244 | // e.printStackTrace(); |
| 245 | // assertTrue(false); |
| 246 | // } |
| 247 | // } |
| 248 | |
| 249 | // TODO: fix issue, then uncomment |
| 250 | // @Test |
| 251 | // public void incorrectMissingTruthPrior() { |
| 252 | // try { |
| 253 | // |
| 254 | // // Create evidence table |
| 255 | // db.dropTable("felix_test.evid"); |
| 256 | // String query = "CREATE TABLE felix_test.evid (a1 text, b2 text)"; |
| 257 | // db.execute(query); |
| 258 | // |
| 259 | // query = "INSERT INTO felix_test.evid VALUES (1, 2), (4, 5)"; |
| 260 | // db.execute(query); |
| 261 | // |
| 262 | // String output = "test/felix/output/out.txt_IODBTest_incorrectMissingTruthPrior"; |
| 263 | // String[] args = {"-i", "test/felix/io_test/prog.mln-db", "-o", output, |
| 264 | // "-queryFile", "test/felix/io_test/query.db", "-keepData"}; |
| 265 | // |
| 266 | // FelixCommandOptions options = FelixUIMan.parseCommand(args); |
| 267 | // new Felix().run(options); |
| 268 | // |
| 269 | // checkOutput(output); |
| 270 | // |
| 271 | // } catch(Exception e) { |
| 272 | // e.printStackTrace(); |
| 273 | // assertTrue(false); |
| 274 | // } |
| 275 | // } |
| 276 | |
| 277 | } |