-
Notifications
You must be signed in to change notification settings - Fork 0
/
goal.go
executable file
·73 lines (61 loc) · 2.56 KB
/
goal.go
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
package suiron
// Goal - is the base interface of all goal objects. Complex terms
// and operators such as And and Or etc. implement this interface.
//
// The method GetSolver() provides a solution node.
//
// Cleve Lendon
type Goal interface {
Expression
// GetSolver - gets a solution node for the current goal.
GetSolver(kb KnowledgeBase, parentSolution SubstitutionSet,
parentNode SolutionNode) SolutionNode
}
func MakeQuery(terms ...Unifiable) Complex {
// The main bottleneck in Suiron is the time it takes
// to copy the substitution set. The substitution set
// is as large as the highest variable ID. Therefore
// variableId should be set to 0 for every query.
variableId = 0
newTerms := makeLogicVariablesUnique(terms...)
return Complex(newTerms)
} // MakeQuery
// ParseQuery - creates a query (Complex term) from a text string,
// and ensures that all logic variables have unique IDs.
//
// Params: string representation of query
// Return: query (Complex term)
// error
func ParseQuery(str string) (Complex, error) {
// The main bottleneck in Suiron is the time it takes
// to copy the substitution set. The substitution set
// is as large as the highest variable ID. Therefore
// variableId should be set to 0 for every query.
variableId = 0
c, err := ParseComplex(str)
if err != nil { return c, err }
terms := []Unifiable(c) // get terms
newTerms := makeLogicVariablesUnique(terms...)
return Complex(newTerms), nil
} // ParseQuery
// makeLogicVariablesUnique - Long explanation.
// A substitution set keeps track of the bindings of logic variables.
// In order to avoid the overhead of hashing, the substitution set is
// indexed by the ID numbers of these variables. If two logic vars had
// the same ID, this would cause the search for a solution to fail.
// The function LogicVar() creates logic variables with a name and an
// ID number, which is always 0. This is OK, because whenever a rule
// is fetched from the knowledge base, its variables are recreated,
// by calling RecreateVariables().
// However, queries are not fetched from the knowledge base. If a query
// is created, it is necessary to ensure that any logic variables it
// contains do not have an index of 0.
func makeLogicVariablesUnique(terms ...Unifiable) []Unifiable {
newTerms := []Unifiable{}
vars := make(VarMap)
for _, term := range terms {
newTerm := term.RecreateVariables(vars).(Unifiable)
newTerms = append(newTerms, newTerm)
}
return newTerms
} // makeLogicVariablesUnique