Functor2.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.Generic;
  32. namespace OpenSim.Region.ScriptEngine.Shared.YieldProlog
  33. {
  34. public class Functor2 : IUnifiable
  35. {
  36. public readonly Atom _name;
  37. public readonly object _arg1;
  38. public readonly object _arg2;
  39. public Functor2(Atom name, object arg1, object arg2)
  40. {
  41. _name = name;
  42. _arg1 = arg1;
  43. _arg2 = arg2;
  44. }
  45. public Functor2(string name, object arg1, object arg2)
  46. : this(Atom.a(name), arg1, arg2)
  47. {
  48. }
  49. // disable warning on l1, don't see how we can
  50. // code this differently
  51. #pragma warning disable 0168, 0219
  52. /// If arg is another Functor2, then succeed (yield once) if this and arg have the
  53. /// same name and all functor args unify, otherwise fail (don't yield).
  54. /// If arg is a Variable, then call its unify to unify with this.
  55. /// Otherwise fail (don't yield).
  56. public IEnumerable<bool> unify(object arg)
  57. {
  58. arg = YP.getValue(arg);
  59. if (arg is Functor2)
  60. {
  61. Functor2 argFunctor = (Functor2)arg;
  62. if (_name.Equals(argFunctor._name))
  63. {
  64. foreach (bool l1 in YP.unify(_arg1, argFunctor._arg1))
  65. {
  66. foreach (bool l2 in YP.unify(_arg2, argFunctor._arg2))
  67. yield return false;
  68. }
  69. }
  70. }
  71. else if (arg is Variable)
  72. {
  73. foreach (bool l1 in ((Variable)arg).unify(this))
  74. yield return false;
  75. }
  76. }
  77. #pragma warning restore 0168, 0219
  78. public override string ToString()
  79. {
  80. if (_name == Atom.DOT)
  81. return listPairToString(this);
  82. else
  83. return _name + "(" + YP.getValue(_arg1) + ", " + YP.getValue(_arg2) + ")";
  84. }
  85. public bool termEqual(object term)
  86. {
  87. term = YP.getValue(term);
  88. if (term is Functor2)
  89. {
  90. Functor2 termFunctor = (Functor2)term;
  91. return _name.Equals(termFunctor._name) && YP.termEqual(_arg1, termFunctor._arg1)
  92. && YP.termEqual(_arg2, termFunctor._arg2);
  93. }
  94. return false;
  95. }
  96. public bool lessThan(Functor2 functor)
  97. {
  98. // Do the equal check first since it is faster.
  99. if (!_name.Equals(functor._name))
  100. return _name.lessThan(functor._name);
  101. if (!YP.termEqual(_arg1, functor._arg1))
  102. return YP.termLessThan(_arg1, functor._arg1);
  103. return YP.termLessThan(_arg2, functor._arg2);
  104. }
  105. public bool ground()
  106. {
  107. return YP.ground(_arg1) && YP.ground(_arg2);
  108. }
  109. public void addUniqueVariables(List<Variable> variableSet)
  110. {
  111. YP.addUniqueVariables(_arg1, variableSet);
  112. YP.addUniqueVariables(_arg2, variableSet);
  113. }
  114. public object makeCopy(Variable.CopyStore copyStore)
  115. {
  116. return new Functor2(_name, YP.makeCopy(_arg1, copyStore),
  117. YP.makeCopy(_arg2, copyStore));
  118. }
  119. private static string listPairToString(Functor2 listPair)
  120. {
  121. string result = "[";
  122. while (true)
  123. {
  124. object head = YP.getValue(listPair._arg1);
  125. object tail = YP.getValue(listPair._arg2);
  126. if (tail == (object)Atom.NIL)
  127. {
  128. result += head;
  129. break;
  130. }
  131. else if (tail is Functor2 && ((Functor2)tail)._name == Atom.DOT)
  132. {
  133. result += head + ", ";
  134. listPair = (Functor2)tail;
  135. // Loop again.
  136. }
  137. else
  138. {
  139. // The list is not terminated with NIL.
  140. result += head + "|" + tail;
  141. break;
  142. }
  143. }
  144. result += "]";
  145. return result;
  146. }
  147. }
  148. }