PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/YieldProlog/Modules/Functor2.cs

https://bitbucket.org/VirtualReality/optional-modules
C# | 160 lines | 110 code | 15 blank | 35 comment | 19 complexity | 6d69960f76977806d0834faa6e5f4cae MD5 | raw file
  1. /*
  2. * Copyright (c) Contributors, http://aurora-sim.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the Aurora-Sim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. namespace OpenSim.Region.ScriptEngine.Shared.YieldProlog
  30. {
  31. public class Functor2 : IUnifiable
  32. {
  33. public readonly Atom _name;
  34. public readonly object _arg1;
  35. public readonly object _arg2;
  36. public Functor2(Atom name, object arg1, object arg2)
  37. {
  38. _name = name;
  39. _arg1 = arg1;
  40. _arg2 = arg2;
  41. }
  42. public Functor2(string name, object arg1, object arg2)
  43. : this(Atom.a(name), arg1, arg2)
  44. {
  45. }
  46. // disable warning on l1, don't see how we can
  47. // code this differently
  48. #pragma warning disable 0168, 0219
  49. /// If arg is another Functor2, then succeed (yield once) if this and arg have the
  50. /// same name and all functor args unify, otherwise fail (don't yield).
  51. /// If arg is a Variable, then call its unify to unify with this.
  52. /// Otherwise fail (don't yield).
  53. public IEnumerable<bool> unify(object arg)
  54. {
  55. arg = YP.getValue(arg);
  56. if (arg is Functor2)
  57. {
  58. Functor2 argFunctor = (Functor2)arg;
  59. if (_name.Equals(argFunctor._name))
  60. {
  61. foreach (bool l1 in YP.unify(_arg1, argFunctor._arg1))
  62. {
  63. foreach (bool l2 in YP.unify(_arg2, argFunctor._arg2))
  64. yield return false;
  65. }
  66. }
  67. }
  68. else if (arg is Variable)
  69. {
  70. foreach (bool l1 in ((Variable)arg).unify(this))
  71. yield return false;
  72. }
  73. }
  74. #pragma warning restore 0168, 0219
  75. public override string ToString()
  76. {
  77. if (_name == Atom.DOT)
  78. return listPairToString(this);
  79. else
  80. return _name + "(" + YP.getValue(_arg1) + ", " + YP.getValue(_arg2) + ")";
  81. }
  82. public bool termEqual(object term)
  83. {
  84. term = YP.getValue(term);
  85. if (term is Functor2)
  86. {
  87. Functor2 termFunctor = (Functor2)term;
  88. return _name.Equals(termFunctor._name) && YP.termEqual(_arg1, termFunctor._arg1)
  89. && YP.termEqual(_arg2, termFunctor._arg2);
  90. }
  91. return false;
  92. }
  93. public bool lessThan(Functor2 functor)
  94. {
  95. // Do the equal check first since it is faster.
  96. if (!_name.Equals(functor._name))
  97. return _name.lessThan(functor._name);
  98. if (!YP.termEqual(_arg1, functor._arg1))
  99. return YP.termLessThan(_arg1, functor._arg1);
  100. return YP.termLessThan(_arg2, functor._arg2);
  101. }
  102. public bool ground()
  103. {
  104. return YP.ground(_arg1) && YP.ground(_arg2);
  105. }
  106. public void addUniqueVariables(List<Variable> variableSet)
  107. {
  108. YP.addUniqueVariables(_arg1, variableSet);
  109. YP.addUniqueVariables(_arg2, variableSet);
  110. }
  111. public object makeCopy(Variable.CopyStore copyStore)
  112. {
  113. return new Functor2(_name, YP.makeCopy(_arg1, copyStore),
  114. YP.makeCopy(_arg2, copyStore));
  115. }
  116. private static string listPairToString(Functor2 listPair)
  117. {
  118. string result = "[";
  119. while (true)
  120. {
  121. object head = YP.getValue(listPair._arg1);
  122. object tail = YP.getValue(listPair._arg2);
  123. if (tail == (object)Atom.NIL)
  124. {
  125. result += head;
  126. break;
  127. }
  128. else if (tail is Functor2 && ((Functor2)tail)._name == Atom.DOT)
  129. {
  130. result += head + ", ";
  131. listPair = (Functor2)tail;
  132. // Loop again.
  133. }
  134. else
  135. {
  136. // The list is not terminated with NIL.
  137. result += head + "|" + tail;
  138. break;
  139. }
  140. }
  141. result += "]";
  142. return result;
  143. }
  144. }
  145. }