aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/BagofAnswers.cs
diff options
context:
space:
mode:
authorlbsa712008-06-24 21:09:49 +0000
committerlbsa712008-06-24 21:09:49 +0000
commit6b7930104bdb845d3b9c085dc04f52b6446f23b1 (patch)
tree05ee45781a455817fa400bb99f30f4d19d4eb1f8 /OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/BagofAnswers.cs
parentbased on positive feedback on performance of making keys fixed length (diff)
downloadopensim-SC-6b7930104bdb845d3b9c085dc04f52b6446f23b1.zip
opensim-SC-6b7930104bdb845d3b9c085dc04f52b6446f23b1.tar.gz
opensim-SC-6b7930104bdb845d3b9c085dc04f52b6446f23b1.tar.bz2
opensim-SC-6b7930104bdb845d3b9c085dc04f52b6446f23b1.tar.xz
* Applied patch from Melanie, mantis issue #1581 - "Refactor LSL language, api and compiler out of XEngine"
"First stage in a major Script Engine refactor, that will result in the LSL implementaions ebing reconverged. Not there yet, but one major part is done." Thank you, Melanie!
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/BagofAnswers.cs')
-rw-r--r--OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/BagofAnswers.cs234
1 files changed, 234 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/BagofAnswers.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/BagofAnswers.cs
new file mode 100644
index 0000000..70c1b5a
--- /dev/null
+++ b/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/BagofAnswers.cs
@@ -0,0 +1,234 @@
1/*
2 * Copyright (C) 2007-2008, Jeff Thompson
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of the copyright holder nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31using System;
32using System.Collections;
33using System.Collections.Generic;
34
35namespace OpenSim.Region.ScriptEngine.Shared.YieldProlog
36{
37 /// <summary>
38 /// A BagofAnswers holds answers for bagof and setof.
39 /// </summary>
40 public class BagofAnswers
41 {
42 private object _template;
43 private Variable[] _freeVariables;
44 private Dictionary<object[], List<object>> _bagForFreeVariables;
45 private List<object> _findallBagArray;
46 private static TermArrayEqualityComparer _termArrayEqualityComparer =
47 new TermArrayEqualityComparer();
48
49 /// <summary>
50 /// To get the free variables, split off any existential qualifiers from Goal such as the X in
51 /// "X ^ f(Y)", get the set of unbound variables in Goal that are not qualifiers, then remove
52 /// the unbound variables that are qualifiers as well as the unbound variables in Template.
53 /// </summary>
54 /// <param name="Template"></param>
55 /// <param name="Goal"></param>
56 public BagofAnswers(object Template, object Goal)
57 {
58 _template = Template;
59
60 // First get the set of variables that are not free variables.
61 List<Variable> variableSet = new List<Variable>();
62 YP.addUniqueVariables(Template, variableSet);
63 object UnqualifiedGoal = YP.getValue(Goal);
64 while (UnqualifiedGoal is Functor2 && ((Functor2)UnqualifiedGoal)._name == Atom.HAT)
65 {
66 YP.addUniqueVariables(((Functor2)UnqualifiedGoal)._arg1, variableSet);
67 UnqualifiedGoal = YP.getValue(((Functor2)UnqualifiedGoal)._arg2);
68 }
69
70 // Remember how many non-free variables there are so we can find the unique free variables
71 // that are added.
72 int nNonFreeVariables = variableSet.Count;
73 YP.addUniqueVariables(UnqualifiedGoal, variableSet);
74 int nFreeVariables = variableSet.Count - nNonFreeVariables;
75 if (nFreeVariables == 0)
76 {
77 // There were no free variables added, so we won't waste time with _bagForFreeVariables.
78 _freeVariables = null;
79 _findallBagArray = new List<object>();
80 }
81 else
82 {
83 // Copy the free variables.
84 _freeVariables = new Variable[nFreeVariables];
85 for (int i = 0; i < nFreeVariables; ++i)
86 _freeVariables[i] = variableSet[i + nNonFreeVariables];
87
88 _bagForFreeVariables = new Dictionary<object[], List<object>>(_termArrayEqualityComparer);
89 }
90 }
91
92 public void add()
93 {
94 if (_freeVariables == null)
95 // The goal has bound the values in _template but we don't bother with _freeVariables.
96 _findallBagArray.Add(YP.makeCopy(_template, new Variable.CopyStore()));
97 else
98 {
99 // The goal has bound the values in _template and _freeVariables.
100 // Find the entry for this set of _freeVariables values.
101 object[] freeVariableValues = new object[_freeVariables.Length];
102 for (int i = 0; i < _freeVariables.Length; ++i)
103 freeVariableValues[i] = YP.getValue(_freeVariables[i]);
104 List<object> bagArray;
105 if (!_bagForFreeVariables.TryGetValue(freeVariableValues, out bagArray))
106 {
107 bagArray = new List<object>();
108 _bagForFreeVariables[freeVariableValues] = bagArray;
109 }
110
111 // Now copy the template and add to the bag for the freeVariables values.
112 bagArray.Add(YP.makeCopy(_template, new Variable.CopyStore()));
113 }
114 }
115
116 /// <summary>
117 /// For each result, unify the _freeVariables and unify bagArrayVariable with the associated bag.
118 /// </summary>
119 /// <param name="bagArrayVariable">this is unified with the List<object> of matches for template that
120 /// corresponds to the bindings for freeVariables. Be very careful: this does not unify with a Prolog
121 /// list.</param>
122 /// <returns></returns>
123 public IEnumerable<bool> resultArray(Variable bagArrayVariable)
124 {
125 if (_freeVariables == null)
126 {
127 // No unbound free variables, so we only filled one bag. If empty, bagof fails.
128 if (_findallBagArray.Count > 0)
129 {
130 foreach (bool l1 in bagArrayVariable.unify(_findallBagArray))
131 yield return false;
132 }
133 }
134 else
135 {
136 foreach (KeyValuePair<object[], List<object>> valuesAndBag in _bagForFreeVariables)
137 {
138 foreach (bool l1 in YP.unifyArrays(_freeVariables, valuesAndBag.Key))
139 {
140 foreach (bool l2 in bagArrayVariable.unify(valuesAndBag.Value))
141 yield return false;
142 }
143 // Debug: Should we free memory of the answers already returned?
144 }
145 }
146 }
147
148 /// <summary>
149 /// For each result, unify the _freeVariables and unify Bag with the associated bag.
150 /// </summary>
151 /// <param name="Bag"></param>
152 /// <returns></returns>
153 public IEnumerable<bool> result(object Bag)
154 {
155 Variable bagArrayVariable = new Variable();
156 foreach (bool l1 in resultArray(bagArrayVariable))
157 {
158 foreach (bool l2 in YP.unify(Bag, ListPair.make((List<object>)bagArrayVariable.getValue())))
159 yield return false;
160 }
161 }
162
163 /// <summary>
164 /// For each result, unify the _freeVariables and unify Bag with the associated bag which is sorted
165 /// with duplicates removed, as in setof.
166 /// </summary>
167 /// <param name="Bag"></param>
168 /// <returns></returns>
169 public IEnumerable<bool> resultSet(object Bag)
170 {
171 Variable bagArrayVariable = new Variable();
172 foreach (bool l1 in resultArray(bagArrayVariable))
173 {
174 List<object> bagArray = (List<object>)bagArrayVariable.getValue();
175 YP.sortArray(bagArray);
176 foreach (bool l2 in YP.unify(Bag, ListPair.makeWithoutRepeatedTerms(bagArray)))
177 yield return false;
178 }
179 }
180
181 public static IEnumerable<bool> bagofArray
182 (object Template, object Goal, IEnumerable<bool> goalIterator, Variable bagArrayVariable)
183 {
184 BagofAnswers bagOfAnswers = new BagofAnswers(Template, Goal);
185 foreach (bool l1 in goalIterator)
186 bagOfAnswers.add();
187 return bagOfAnswers.resultArray(bagArrayVariable);
188 }
189
190 public static IEnumerable<bool> bagof
191 (object Template, object Goal, IEnumerable<bool> goalIterator, object Bag)
192 {
193 BagofAnswers bagOfAnswers = new BagofAnswers(Template, Goal);
194 foreach (bool l1 in goalIterator)
195 bagOfAnswers.add();
196 return bagOfAnswers.result(Bag);
197 }
198
199 public static IEnumerable<bool> setof
200 (object Template, object Goal, IEnumerable<bool> goalIterator, object Bag)
201 {
202 BagofAnswers bagOfAnswers = new BagofAnswers(Template, Goal);
203 foreach (bool l1 in goalIterator)
204 bagOfAnswers.add();
205 return bagOfAnswers.resultSet(Bag);
206 }
207
208 /// <summary>
209 /// A TermArrayEqualityComparer implements IEqualityComparer to compare two object arrays using YP.termEqual.
210 /// </summary>
211 private class TermArrayEqualityComparer : IEqualityComparer<object[]>
212 {
213 public bool Equals(object[] array1, object[] array2)
214 {
215 if (array1.Length != array2.Length)
216 return false;
217 for (int i = 0; i < array1.Length; ++i)
218 {
219 if (!YP.termEqual(array1[i], array2[i]))
220 return false;
221 }
222 return true;
223 }
224
225 public int GetHashCode(object[] array)
226 {
227 int hashCode = 0;
228 for (int i = 0; i < array.Length; ++i)
229 hashCode ^= array[i].GetHashCode();
230 return hashCode;
231 }
232 }
233 }
234}