Variable.cs 6.8 KB

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