-
Notifications
You must be signed in to change notification settings - Fork 0
/
hyphenate.go
213 lines (186 loc) · 6.77 KB
/
hyphenate.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
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
package main
// Hyphenate - is a built-in predicate function which joins two words with
// a hyphen. Its purpose is to test built-in predicate functionality.
//
// Hyphenate is instantiated from bip_test.go, as follows:
//
// c2 := Hyphenate(In, H, T, InErr, Err2)
//
// The arguments above are logic variables (type Variable). If this predicate
// were written in a Suiron source file, it would appear as follows:
//
// ..., hyphenate($In, $H, $T, $InErr, $Err2), ...
//
// Hyphenate takes the first two words from an input word list, and joins
// them with a hyphen. The new hyphenated word is bound to the second argument
// (head word). The remainder of the word list is bound to the third argument
// (tail of list). For example, if the input were:
//
// $In = [one, two, three, four]
//
// The output would be:
//
// $H = one-two
// $T = [three, four]
//
// The predicate also creates an error message, which is bound to the last
// argument. For the following input error list:
//
// [first error]
//
// The output ($Err2) will be:
//
// [another error, first error]
//
// Cleve Lendon
import (
. "github.com/indrikoterio/suiron/suiron"
//"fmt"
)
type HyphenateStruct BuiltInPredicateStruct
// Hyphenate - creates a Hyphenate predicate.
func Hyphenate(arguments ...Unifiable) HyphenateStruct {
if len(arguments) != 5 {
panic("Hyphenate - This predicate requires 5 arguments.")
}
return HyphenateStruct {
Name: "hyphenate",
Arguments: arguments,
}
}
// GetSolver - gets a solution node for this predicate.
// This function satisfies the Goal interface.
func (s HyphenateStruct) GetSolver(kb KnowledgeBase,
parentSolution SubstitutionSet,
parentNode SolutionNode) SolutionNode {
return makeHyphenateSolutionNode(s, kb, parentSolution, parentNode)
}
//----------------------------------------------------------------
// RecreateVariables(), ReplaceVariables(), and String() satisfy
// the Expression interface.
//----------------------------------------------------------------
// RecreateVariables - Refer to comments in expression.go.
func (jws HyphenateStruct) RecreateVariables(vars VarMap) Expression {
bip := BuiltInPredicateStruct(jws).RecreateVariables(vars)
return Expression(HyphenateStruct(*bip))
}
// ReplaceVariables - Refer to comments in expression.go.
func (jws HyphenateStruct) ReplaceVariables(ss SubstitutionSet) Expression {
return BuiltInPredicateStruct(jws).ReplaceVariables(ss)
} // ReplaceVariables
// String - creates a string representation.
// Returns: predicate_name(arg1, arg2, arg3)
func (jws HyphenateStruct) String() string {
return BuiltInPredicateStruct(jws).String()
}
//----------------------------------------------------------------
// Solution Node functions.
// makeHyphenateSolutionNode()
// NextSolution()
// SetNoBackTracking()
//----------------------------------------------------------------
// A solution node holds the current state of the search for a solution.
// It contains the current goal, the number of the last rule fetched
// from the knowledge base, and a substitution set (which represents the
// solution so far).
// Built-in predicates produce only one solution for a given set of
// arguments. The boolean flag 'moreSolutions' is set to false after
// the first solution is returned.
type HyphenateSolutionNodeStruct struct {
SolutionNodeStruct
moreSolutions bool
}
// makeHyphenateSolutionNode - creates a solution node for this predicate.
func makeHyphenateSolutionNode(goal Goal, kb KnowledgeBase,
parentSolution SubstitutionSet,
parentNode SolutionNode) SolutionNode {
node := HyphenateSolutionNodeStruct{
SolutionNodeStruct: MakeSolutionNode(goal, kb,
parentSolution, parentNode),
moreSolutions: true,
}
return &node
}
// NextSolution - calls a function (in this case, joinWithHyphen)
// to evaluate the current goal, based on its arguments and the
// substitution set.
// Returns:
// updated substitution set
// success/failure flag
// This function satisfies the SolutionNode interface.
func (sn *HyphenateSolutionNodeStruct) NextSolution() (SubstitutionSet, bool) {
if sn.NoBackTracking || !sn.moreSolutions { return nil, false }
sn.moreSolutions = false // Only one solution.
goal := sn.Goal.(HyphenateStruct)
return joinWithHyphen(goal.Arguments, sn.ParentSolution)
}
// SetNoBackTracking - set the NoBackTracking flag,
// which is used to implement Cuts.
// This function satisfies the SolutionNode interface.
func (sn *HyphenateSolutionNodeStruct) SetNoBackTracking() {
sn.NoBackTracking = true
}
// GetParentNode
func (n *HyphenateSolutionNodeStruct) GetParentNode() SolutionNode {
return n.ParentNode
}
// joinWithHyphen - joins the first two words in a word list with
// a hyphen, and generates an error message. For example,
//
// $In = [one, two, three, four]
//
// becomes,
//
// $H = one-two
// $T = [three, four]
//
// The function also takes an error list and adds a new error message, eg:
//
// $InErr = [first error]
// $OutErr = [another error, first error]
//
// The 5 arguments are:
//
// word list - in
// head word - out
// tail of list - out
// error list - in
// error list - out
//
// Params:
// list of 5 arguments
// substitution set (= solution so far)
// Return:
// updated substitution set
// success/failure flag
//
func joinWithHyphen(arguments []Unifiable, ss SubstitutionSet) (SubstitutionSet, bool) {
// First argument must be a linked list.
inList, ok := ss.CastLinkedList(arguments[0])
if !ok { return ss, false }
// The fourth argument must be a linked list.
inErrors, ok := ss.CastLinkedList(arguments[3])
if !ok { return ss, false }
err := Atom("another error")
// Add an error message to the error list
newErrorList := MakeLinkedList(false, err, inErrors)
// Flatten gets the first two items of a list and the
// rest of the list. Thus, Flatten(2, ss) returns a slice
// of 3 items: term1, term2, list of remaining terms
terms, ok := inList.Flatten(2, ss)
if !ok { return ss, false }
term1, _ := ss.CastAtom(terms[0])
term2, _ := ss.CastAtom(terms[1])
// Join the two terms.
str := term1.String() + "-" + term2.String()
newHead := Atom(str)
newTail := terms[2].(LinkedListStruct)
// Unify output terms.
ss, ok = newErrorList.Unify(arguments[4], ss)
if !ok { return ss, false }
ss, ok = newHead.Unify(arguments[1], ss)
if !ok { return ss, false }
ss, ok = newTail.Unify(arguments[2], ss)
if !ok { return ss, false }
return ss, true
} // joinWithHyphen