-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathProgram.cs
More file actions
67 lines (61 loc) · 3.49 KB
/
Program.cs
File metadata and controls
67 lines (61 loc) · 3.49 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
using Guan.Logic;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace GuanExamples
{
class Program
{
static async Task Main()
{
// External predicate types (as object instances or singletons (static single instances)) must be specified in a Guan FunctorTable object
// which is used to create the Module, which is a collection of predicate types and is required by the Query executor (see RunQueryAsync impl in GuanQueryDispatcher.cs).
var functorTable = new FunctorTable();
functorTable.Add(TestAddPredicateType.Singleton("addresult"));
functorTable.Add(TestDivPredicateType.Singleton("divresult"));
functorTable.Add(TestSubPredicateType.Singleton("subresult"));
// Create a List<string> containing logic rules (these could also be housed in a text file). These rules are very simple by design, as are the external predicates used in this sample console app.
// The rules below are simple examples of using logic in their sub rule parts and calling an external predicate.
var logicsRules = new List<string>
{
"test(?x, ?y) :- ?x == ?y, addresult(?x, ?y)",
"test(?x, ?y) :- ?y > 0 && ?y < ?x, divresult(?x, ?y)",
"test(?x, ?y) :- ?x > ?y, subresult(?x, ?y)",
"test1(1)",
"test1(2)",
"test2(2)",
"test2(3)",
"test3(q(1, ?x))",
"test4(?x, ?y) :- test3(q(?x, ?y)), test1(?y)",
"test5(?x, ?y) :- ?x > 1 && ?y < 3, test1(?x), test2(?y)",
"test6(?x) :- test1(?x), not(test2(?x))",
"test7(?x) :- not(?x < 2), test1(?x)",
"test8(?x, ?y, ?z) :- showtype(?x, ?y), ?x = 5, showtype(?x, ?z)",
"test9(?x, ?y, ?z) :- b_setval(v1, 0), test1(?x), getval(v1, ?y), b_setval(v1, 5), getval(v1, ?z)",
"test10(?x, ?y, ?z) :- b_setval(v1, 0), test1(?x), getval(v1, ?y), setval(v1, 5), getval(v1, ?z)",
"showtype(?x, 'var') :- var(?x)",
"showtype(?x, 'nonvar') :- nonvar(?x)",
"showtype(?x, 'atom') :- atom(?x)",
"showtype(?x, 'compound') :- compound(?x)",
"f1(?a, ?b, ?b, ?a)"
};
// A Module is a collection of predicate types.
Module module = Module.Parse("test", logicsRules, functorTable);
var queryDispatcher = new GuanQueryDispatcher(module);
/* Execute queries via GuanQueryDispatcher helper class */
// test goal with external predicate impls.
await queryDispatcher.RunQueryAsync("test(3, 3)");
await queryDispatcher.RunQueryAsync("test(0, 0)");
await queryDispatcher.RunQueryAsync("test(5, 2)");
await queryDispatcher.RunQueryAsync("test(3, 2)");
await queryDispatcher.RunQueryAsync("test(4, 2)");
await queryDispatcher.RunQueryAsync("test(2, 5)");
await queryDispatcher.RunQueryAsync("test(6, 2)");
await queryDispatcher.RunQueryAsync("test(8, 2)");
await queryDispatcher.RunQueryAsync("test(25, 5)");
await queryDispatcher.RunQueryAsync("test(1, 0)");
// testx goals with internal predicate impls.
// the answer/result for the below query would be (x=1,y=2) given the rules.
await queryDispatcher.RunQueryAsync("test4(?x, ?y), test2(?y)", true);
}
}
}