-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfEng.cs
More file actions
688 lines (584 loc) · 24.9 KB
/
InfEng.cs
File metadata and controls
688 lines (584 loc) · 24.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;
using System.IO;
namespace InferenceEngine
{
class InfEng
{
private SQLiteConnection m_dbConnection;
private string DB_filename;
private List<string> undo_chain;
private bool isUndoOpen;
//CONSTRUCTOR: accepts an explicit filename for the DB, or uses a default value
//RESULT: The constructor will create the DB connection, then attempt to create
// the DB tables if they do not already exist.
public InfEng(string filename = "MyDatabase.sqlite")
{
DB_filename = filename;
undo_chain = new List<string>();
isUndoOpen = false;
//Lets see if we need to create the DB
if (!File.Exists(DB_filename))
{
SQLiteConnection.CreateFile(DB_filename);
}
//Create connection to the DB
m_dbConnection = new SQLiteConnection("Data Source=" + DB_filename + ";Version=3;");
m_dbConnection.Open();
//Time to create the tables if necessary
string create_ALL = "CREATE TABLE IF NOT EXISTS rules_all(noun1 TEXT NOT NULL, noun2 TEXT NOT NULL, UNIQUE(noun1, noun2))";
string create_NO = "CREATE TABLE IF NOT EXISTS rules_no(noun1 TEXT NOT NULL, noun2 TEXT NOT NULL, UNIQUE(noun1, noun2))";
string create_SOME = "CREATE TABLE IF NOT EXISTS rules_some(noun1 TEXT NOT NULL, noun2 TEXT NOT NULL, UNIQUE(noun1, noun2))";
//Execute the SQL commands
SQLiteCommand command = new SQLiteCommand(create_ALL, m_dbConnection);
command.ExecuteNonQuery();
command = new SQLiteCommand(create_NO, m_dbConnection);
command.ExecuteNonQuery();
command = new SQLiteCommand(create_SOME, m_dbConnection);
command.ExecuteNonQuery();
//All done with the DB for now
m_dbConnection.Close();
}
private bool addToTable(string table, string noun1, string noun2)
{
bool result = true;
string sql = string.Format("INSERT INTO rules_{0}(noun1, noun2) VALUES (\"{1}\", \"{2}\")", table, noun1, noun2);
m_dbConnection.Open();
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
try
{
command.ExecuteNonQuery();
}
catch (SQLiteException exeption)
{
//Console.WriteLine(exeption.Message);
result = false;
}
m_dbConnection.Close();
if (!result)
Console.WriteLine(string.Format("Can't Add {0} and {1} to {2}", noun1, noun2, table));
return result;
}
private bool removeFromTable(string table, string noun1, string noun2)
{
bool result = true;
m_dbConnection.Open();
string sql = string.Format("DELETE FROM rules_{0} where noun1 = \"{1}\" and noun2 = \"{2}\"", table, noun1, noun2);
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
try
{
command.ExecuteNonQuery();
}
catch (SQLiteException exeption)
{
Console.WriteLine(exeption.Message);
result = false;
}
m_dbConnection.Close();
return result;
}
//Returns 0 if a contradiction is found in table other than targetTable
//Returns 1 if a contradiction found in targetTable
//Returns 2 if no contradiction is found
private int checkContradictions(string targetTable, string noun1, string noun2)
{
//Attempt to add the values to all tables. addToTable will return
// false if the set can't be added due to a unique violation.
// In which case there is a contradiction, then we need to check if
// we are trying to add to the target table in which case its not really
// a contradiction, just already known information
bool addToAllTable = addToTable("ALL", noun1, noun2);
if (addToAllTable)
removeFromTable("ALL", noun1, noun2);
else if (!addToAllTable && targetTable == "ALL")
return 1;
else if (!addToAllTable)
return 0;
bool addToNoTable = addToTable("NO", noun1, noun2);
if (addToNoTable)
removeFromTable("NO", noun1, noun2);
else if (!addToNoTable && targetTable == "NO")
return 1;
else if (!addToNoTable)
return 0;
bool addToSomeTable = addToTable("SOME", noun1, noun2);
if (addToSomeTable)
removeFromTable("SOME", noun1, noun2);
else if (!addToSomeTable && targetTable == "SOME")
return 1;
else if (!addToSomeTable)
return 0;
return 2;
}
public bool insertInTable(string table, string noun1, string noun2)
{
//This is used only on the first call of insertInTable, these variables insure that
// recursively called insertInTable's don't damage the undoChain which is necessary
// when a contradiction is found.
bool needToCloseUndo = false;
if (!isUndoOpen)
{
isUndoOpen = true;
needToCloseUndo = true;
undo_chain.Add(table); //The first element of the chain will always be the targetTable
}
int contradictionType = checkContradictions(table, noun1, noun2);
if (contradictionType == 0) //True contradiction with another table
{
Console.WriteLine("Contradiction with another table");
if (needToCloseUndo)
{
undo_chain.Clear();
isUndoOpen = false;
failure();
}
return false;
}
else if (contradictionType == 1) //Already known knowledge
{
Console.WriteLine("Already Known Information");
}
else if (contradictionType == 2) //No contradictions found
{
addToTable(table, noun1, noun2);
undo_chain.Add(noun1);
undo_chain.Add(noun2);
}
if (!makeInferences(table, noun1, noun2))
{
if(needToCloseUndo) //if there is a contradiction anywhere recursively and this is the first instance of insertIntoTable
{
revert(); //Run through the undoChain and revert all the changes made
undo_chain.Clear();
isUndoOpen = false;
}
failure();
return false;
}
else
{
if (needToCloseUndo)
{
undo_chain.Clear();
isUndoOpen = false;
success();
}
return true;
}
}
private bool makeInferences(string table, string noun1, string noun2)
{
//Equvalence Case: X==Y Y==X
// If we don't account for this the other checks will result in permanent loop
m_dbConnection.Open();
string sql = String.Format("select noun2 from rules_{0} where noun1 = \"{1}\" and noun2 = \"{2}\"", table, noun2, noun1);
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
List<string> other_nouns = new List<string>();
if (reader.Read())
{
m_dbConnection.Close();
removeFromTable(table, noun1, noun2);
Console.WriteLine("Error: Already known, {0} {1} are {2}",table, noun2, noun1);
return false;
}
m_dbConnection.Close();
//Barbara Syllogism: Y==Z X==Y :: X==Z
// We are adding noun1 == noun2
// If there exists a rule noun2 == other_noun
// Then we can infer that noun1 == other_noun
if (table == "ALL")
{
m_dbConnection.Open();
sql = String.Format("select noun2 from rules_{0} where noun1 = \"{1}\"", table, noun2);
command = new SQLiteCommand(sql, m_dbConnection);
reader = command.ExecuteReader();
other_nouns = new List<string>();
while (reader.Read())
{
string other_noun = (string)reader["noun2"];
Console.WriteLine(string.Format("Infering that {0} {1} are {2}", table, noun1, other_noun));
other_nouns.Add(other_noun);
}
m_dbConnection.Close();
for (int i = 0; i < other_nouns.Count; i++)
{
if (!insertInTable(table, noun1, other_nouns[i]))
{
return false;
}
}
}
//Calarent Syllogism: X!=Y Z==X :: Z!=Y
// Adding noun1 == noun2
// if there exists a rule noun2 != other_noun
// Then we can infer that noun1 != other_noun
if (table == "ALL")
{
m_dbConnection.Open();
sql = String.Format("select noun2 from rules_{0} where noun1 = \"{1}\"", "NO", noun2);
command = new SQLiteCommand(sql, m_dbConnection);
reader = command.ExecuteReader();
other_nouns = new List<string>();
while (reader.Read())
{
string other_noun = (string)reader["noun2"];
Console.WriteLine(string.Format("Infering that {0} {1} are {2}", "NO", noun1, other_noun));
other_nouns.Add(other_noun);
}
m_dbConnection.Close();
for (int i = 0; i < other_nouns.Count; i++)
{
if (!insertInTable("NO", noun1, other_nouns[i]))
{
return false;
}
}
}
//Datisi Syllogism: X==Y Z~=X :: Z~=Y
// Adding noun1 ~= noun2
// if there exists a rule noun2 == other_noun
// Then we can infer that noun1 ~= other_noun
if (table == "SOME")
{
m_dbConnection.Open();
sql = String.Format("select noun2 from rules_{0} where noun1 = \"{1}\"", "ALL", noun2);
command = new SQLiteCommand(sql, m_dbConnection);
reader = command.ExecuteReader();
other_nouns = new List<string>();
while (reader.Read())
{
string other_noun = (string)reader["noun2"];
Console.WriteLine(string.Format("Infering that {0} {1} are {2}", "SOME", noun1, other_noun));
other_nouns.Add(other_noun);
}
m_dbConnection.Close();
for (int i = 0; i < other_nouns.Count; i++)
{
if (!insertInTable("SOME", noun1, other_nouns[i]))
{
return false;
}
}
}
//There are many other Syllogisms but I haven't implemented a SOME_NOT
// table so were not going to be able to do it.. :(
return true;
}
public void reset()
{
m_dbConnection.Open();
string sql = "DELETE FROM rules_all";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
sql = "DELETE FROM rules_no";
command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
sql = "DELETE FROM rules_some";
command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
m_dbConnection.Close();
Console.WriteLine("Database is now empty");
return;
}
//Returns all known information about a given noun
//Results returned in the form of a string with : delimeters
// - First entry is the table, second is noun1, third is noun2
public List<string> query(string noun)
{
List<string> results = new List<string>();
m_dbConnection.Open();
//Whats in the All table?????
string sql = string.Format("SELECT * FROM rules_all WHERE noun1 = \"{0}\"", noun);
//Get all info from table if noun is empty
if (noun.Length < 1)
sql = string.Format("SELECT * FROM rules_all ORDER BY noun1");
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
results.Add(String.Format("{0}:{1}:{2}","ALL", reader["noun1"], reader["noun2"]));
}
//Whats in the No table?????
sql = string.Format("SELECT * FROM rules_no WHERE noun1 = \"{0}\"", noun);
//All info again
if (noun.Length < 1)
sql = string.Format("SELECT * FROM rules_no ORDER BY noun1");
command = new SQLiteCommand(sql, m_dbConnection);
reader = command.ExecuteReader();
while (reader.Read())
{
results.Add(String.Format("{0}:{1}:{2}", "NO", reader["noun1"], reader["noun2"]));
}
//Whats in the Some table??????
sql = string.Format("SELECT * FROM rules_some WHERE noun1 = \"{0}\"", noun);
//all info again
if (noun.Length < 1)
sql = string.Format("SELECT * FROM rules_some ORDER BY noun1");
command = new SQLiteCommand(sql, m_dbConnection);
reader = command.ExecuteReader();
while (reader.Read())
{
results.Add(String.Format("{0}:{1}:{2}", "SOME", reader["noun1"], reader["noun2"]));
}
m_dbConnection.Close();
return results;
}
public bool parse(string input)
{
string[] nouns = input.ToUpper().Split(' ');
if (nouns.Length != 4)
{
Console.WriteLine("Syntax Error: Too Many Words");
return false;
}
if (nouns[0] != "ALL" && nouns[0] != "NO" && nouns[0] != "SOME")
{
Console.WriteLine("Syntax Error: Doesn't contain Keyword");
return false;
}
else
insertInTable(nouns[0], nouns[1], nouns[3]);
return true;
}
private void revert()
{
for (int i = 1; i < undo_chain.Count; i += 2 )
{
removeFromTable(undo_chain[0], undo_chain[i], undo_chain[i + 1]);
}
return;
}
//WARNING: Calling any test_* functions will completely wipe the InfEng DB
public void test_addToTable()
{
Console.WriteLine("\n\n Testing addToTable() \n__________________________");
// We need a clean DB for consistant results
reset();
//Test: Inserting values to empty InfEng
//Assert: True
Console.WriteLine("Test 1: Adding Dog and Mammal");
if (!addToTable("ALL", "DOG", "Mammal"))
failure();
else
success();
//Test: non-unique noun1
//Assert: True
Console.WriteLine("Test 2: Adding Dog and Wet");
if (!addToTable("ALL", "DOG", "Wet"))
failure();
else
success();
//Test: non-unique noun2
//Assert: True
Console.WriteLine("Test 3: Adding Cat and Mammal");
if (!addToTable("ALL", "Cat", "Mammal"))
failure();
else
success();
//Test: non-unique noun1 and noun2
//Assert: False
Console.WriteLine("Test 4: Adding Cat and Mammal");
if (addToTable("ALL", "Cat", "Mammal"))
failure();
else
success();
return;
}
//TODO: Desparately need to clean this up with some sort of a query function
//Tests are performed by quering the tables for values that we expect to not be there
public void test_removeFromTable()
{
Console.WriteLine("\n\n Testing removeFromTable() \n_______________________________");
// We need a clean DB for consistant results
reset();
//Test: Delete values from empty table
//Assert: False
Console.WriteLine("Test 1: Removing Dog and Mammal");
removeFromTable("ALL", "DOG", "Mammal");
m_dbConnection.Open();
string sql = String.Format("select * from rules_all where noun1 = \"Dog\" and noun2 = \"Mammal\"");
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
int iterations = 0;
while (reader.Read())
{
iterations++;
}
if (iterations == 0)
success();
else
Console.WriteLine(string.Format("Test failed: {0} iterations\n", iterations));
m_dbConnection.Close();
//Test: Delete existing values from table
//Assert: True
Console.WriteLine("Test 2: Removing Dog and Mammal");
addToTable("ALL", "DOG", "Mammal");
removeFromTable("ALL", "DOG", "Mammal");
m_dbConnection.Open();
sql = String.Format("select * from rules_all where noun1 = \"Dog\" and noun2 = \"Mammal\"");
command = new SQLiteCommand(sql, m_dbConnection);
reader = command.ExecuteReader();
iterations = 0;
while (reader.Read())
{
iterations++;
}
if (iterations == 0)
success();
else
Console.WriteLine(string.Format("Test failed: {0} iterations\n", iterations));
m_dbConnection.Close();
//Test: Delete non-existing values from non-empty table
//Assert: False
Console.WriteLine("Test 3: Removing Alligator and Mammal");
addToTable("ALL", "Cat", "Mammal");
addToTable("ALL", "DOG", "Mammal");
removeFromTable("ALL", "Alligator", "Mammal");
m_dbConnection.Open();
sql = String.Format("select * from rules_all where noun1 = \"Alligator\" and noun2 = \"Mammal\"");
command = new SQLiteCommand(sql, m_dbConnection);
reader = command.ExecuteReader();
iterations = 0;
while (reader.Read())
{
iterations++;
}
if (iterations == 0)
success();
else
Console.WriteLine(string.Format("Test failed: {0} iterations\n", iterations));
m_dbConnection.Close();
}
public void test_checkContradictions()
{
//Returns 0 if a contradiction is found in table other than targetTable
//Returns 1 if a contradiction found in targetTable
//Returns 2 if no contradiction is found
// We need a clean DB for consistant results
reset();
Console.WriteLine("\n\n Testing checkContradictions() " +
"\n___________________________________");
//Test: No contradictions in an empty DB
//Assert: True
Console.WriteLine("Test 1: Contradictions in an empty DB?");
if (checkContradictions("ALL", "DOG", "mammals") != 2)
failure();
else
success();
//Test: Contradiction in all table
//Assert: False
Console.WriteLine("Test 2: Contradiction in All table");
addToTable("ALL", "DOG", "mammals");
if (checkContradictions("NO","DOG", "mammals") != 0)
failure();
else
success();
removeFromTable("ALL", "DOG", "mammals");
//Test: Contradiction in no table
//Assert: False
Console.WriteLine("Test 3: Contradiction in No table");
addToTable("NO", "DOG", "mammals");
if (checkContradictions("ALL", "DOG", "mammals") != 0)
failure();
else
success();
removeFromTable("NO", "DOG", "mammals");
//Test: Contradiction in some table
//Assert: False
Console.WriteLine("Test 4: Contradiction in Some table");
addToTable("SOME", "DOG", "mammals");
if (checkContradictions("ALL", "DOG", "mammals") != 0)
failure();
else
success();
removeFromTable("SOME", "DOG", "mammals");
return;
}
public void test_insertIntoTable()
{
reset();
//Test: insert to empty table
//Assert: true
Console.WriteLine("Test 1: Insert into empty DB");
if (insertInTable("ALL", "DOG", "MAMMAL"))
success();
else
failure();
//Test: insert contradictory info
//Assert: false
Console.WriteLine("Test 2: Contradictory information");
if (!insertInTable("NO", "DOG", "MAMMAL"))
success();
else
failure();
//Test: insert and make recursive X==Y Z==X Z==Y inference
//Assert: False
Console.WriteLine("Test 3: recursive inference Z==Y");
insertInTable("ALL", "cat", "mammal");
insertInTable("ALL", "furry", "has_hair");
if (!insertInTable("NO", "DOG", "has_hair"))
success();
else
failure();
//Test: revert changes from a shallow recursive inference Error
//Assert: False
Console.WriteLine("Test 4: shallow recursive error");
reset();
insertInTable("ALL", "thing1", "thing2");
insertInTable("ALL", "thing2", "thing3");
insertInTable("ALL", "thing3", "thing4");
insertInTable("SOME", "thing4", "thing5");
if (!insertInTable("ALL", "thing4", "thing5"))
success();
else
failure();
//Test: revert changes from deep recursive X==Z inference Error
//Assert: True
Console.WriteLine("Test 5: deep recursive error X==Z");
reset();
insertInTable("NO", "thing10", "thing11");
insertInTable("NO", "thing20", "thing21");
insertInTable("NO", "thing29", "thing30");
for (int i = 0; i < 30; i++)
{
insertInTable("ALL", "thing" + i, "thing" + (i + 1));
}
if (insertInTable("NO", "thing1", "thing11")) //I should be able to add this because this inference was never made in the all table
success();
else
failure();
//Test: revert changes from deep recursive Z==Y inference Error
//Assert: True
Console.WriteLine("Test 6: deep recursive error Z==Y");
reset();
insertInTable("NO", "thing10", "thing11");
insertInTable("NO", "thing20", "thing21");
insertInTable("NO", "thing29", "thing30");
for (int i = 0; i < 30; i++)
{
insertInTable("ALL", "thing" + (i + 1), "thing" + (i - 1));
}
if (insertInTable("NO", "thing1", "thing11")) //I should be able to add this because this inference was never made in the all table
success();
else
failure();
}
private void success()
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("** Success **\n");
Console.ForegroundColor = ConsoleColor.White;
}
private void failure()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("** Failure **\n");
Console.ForegroundColor = ConsoleColor.White;
}
}
}