diff options
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/Variable.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/Variable.cs | 196 |
1 files changed, 196 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/Variable.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/Variable.cs new file mode 100644 index 0000000..2b5b0f1 --- /dev/null +++ b/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/Variable.cs | |||
@@ -0,0 +1,196 @@ | |||
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 | |||
31 | using System; | ||
32 | using System.Collections; | ||
33 | using System.Collections.Generic; | ||
34 | |||
35 | namespace OpenSim.Region.ScriptEngine.Shared.YieldProlog | ||
36 | { | ||
37 | public interface IUnifiable | ||
38 | { | ||
39 | IEnumerable<bool> unify(object arg); | ||
40 | void addUniqueVariables(List<Variable> variableSet); | ||
41 | object makeCopy(Variable.CopyStore copyStore); | ||
42 | bool termEqual(object term); | ||
43 | bool ground(); | ||
44 | } | ||
45 | |||
46 | public class Variable : IUnifiable | ||
47 | { | ||
48 | // Use _isBound separate from _value so that it can be bound to any value, | ||
49 | // including null. | ||
50 | private bool _isBound = false; | ||
51 | private object _value; | ||
52 | |||
53 | public object getValue() | ||
54 | { | ||
55 | if (!_isBound) | ||
56 | return this; | ||
57 | |||
58 | object result = _value; | ||
59 | while (result is Variable) | ||
60 | { | ||
61 | if (!((Variable)result)._isBound) | ||
62 | return result; | ||
63 | |||
64 | // Keep following the Variable chain. | ||
65 | result = ((Variable)result)._value; | ||
66 | } | ||
67 | |||
68 | return result; | ||
69 | } | ||
70 | |||
71 | public IEnumerable<bool> unify(object arg) | ||
72 | { | ||
73 | if (!_isBound) | ||
74 | { | ||
75 | _value = YP.getValue(arg); | ||
76 | if (_value == this) | ||
77 | // We are unifying this unbound variable with itself, so leave it unbound. | ||
78 | yield return false; | ||
79 | else | ||
80 | { | ||
81 | _isBound = true; | ||
82 | try | ||
83 | { | ||
84 | yield return false; | ||
85 | } | ||
86 | finally | ||
87 | { | ||
88 | // Remove the binding. | ||
89 | _isBound = false; | ||
90 | } | ||
91 | } | ||
92 | } | ||
93 | else | ||
94 | { | ||
95 | foreach (bool l1 in YP.unify(this, arg)) | ||
96 | yield return false; | ||
97 | } | ||
98 | } | ||
99 | |||
100 | public override string ToString() | ||
101 | { | ||
102 | object value = getValue(); | ||
103 | if (value == this) | ||
104 | return "Variable"; | ||
105 | else | ||
106 | return getValue().ToString(); | ||
107 | } | ||
108 | |||
109 | /// <summary> | ||
110 | /// If bound, call YP.addUniqueVariables on the value. Otherwise, if this unbound | ||
111 | /// variable is not already in variableSet, add it. | ||
112 | /// </summary> | ||
113 | /// <param name="variableSet"></param> | ||
114 | public void addUniqueVariables(List<Variable> variableSet) | ||
115 | { | ||
116 | if (_isBound) | ||
117 | YP.addUniqueVariables(getValue(), variableSet); | ||
118 | else | ||
119 | { | ||
120 | if (variableSet.IndexOf(this) < 0) | ||
121 | variableSet.Add(this); | ||
122 | } | ||
123 | } | ||
124 | |||
125 | /// <summary> | ||
126 | /// If bound, return YP.makeCopy for the value, else return copyStore.getCopy(this). | ||
127 | /// However, if copyStore is null, just return this. | ||
128 | /// </summary> | ||
129 | /// <param name="copyStore"></param> | ||
130 | /// <returns></returns> | ||
131 | public object makeCopy(Variable.CopyStore copyStore) | ||
132 | { | ||
133 | if (_isBound) | ||
134 | return YP.makeCopy(getValue(), copyStore); | ||
135 | else | ||
136 | return copyStore == null ? this : copyStore.getCopy(this); | ||
137 | } | ||
138 | |||
139 | public bool termEqual(object term) | ||
140 | { | ||
141 | if (_isBound) | ||
142 | return YP.termEqual(getValue(), term); | ||
143 | else | ||
144 | return this == YP.getValue(term); | ||
145 | } | ||
146 | |||
147 | public bool ground() | ||
148 | { | ||
149 | if (_isBound) | ||
150 | // This is usually called by YP.ground which already did getValue, so this | ||
151 | // should never be reached, but check anyway. | ||
152 | return YP.ground(getValue()); | ||
153 | else | ||
154 | return false; | ||
155 | } | ||
156 | |||
157 | /// <summary> | ||
158 | /// A CopyStore is used by makeCopy to track which Variable objects have | ||
159 | /// been copied. | ||
160 | /// </summary> | ||
161 | public class CopyStore | ||
162 | { | ||
163 | private List<Variable> _inVariableSet = new List<Variable>(); | ||
164 | private List<Variable> _outVariableSet = new List<Variable>(); | ||
165 | |||
166 | /// <summary> | ||
167 | /// If inVariable has already been copied, return its copy. Otherwise, | ||
168 | /// return a fresh Variable associated with inVariable. | ||
169 | /// </summary> | ||
170 | /// <param name="inVariable"></param> | ||
171 | /// <returns></returns> | ||
172 | public Variable getCopy(Variable inVariable) | ||
173 | { | ||
174 | int index = _inVariableSet.IndexOf(inVariable); | ||
175 | if (index >= 0) | ||
176 | return _outVariableSet[index]; | ||
177 | else | ||
178 | { | ||
179 | Variable outVariable = new Variable(); | ||
180 | _inVariableSet.Add(inVariable); | ||
181 | _outVariableSet.Add(outVariable); | ||
182 | return outVariable; | ||
183 | } | ||
184 | } | ||
185 | |||
186 | /// <summary> | ||
187 | /// Return the number of unique variables that have been copied. | ||
188 | /// </summary> | ||
189 | /// <returns></returns> | ||
190 | public int getNUniqueVariables() | ||
191 | { | ||
192 | return _inVariableSet.Count; | ||
193 | } | ||
194 | } | ||
195 | } | ||
196 | } | ||