PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/YieldProlog/Modules/Functor3.cs

https://bitbucket.org/VirtualReality/optional-modules
C# | 138 lines | 91 code | 14 blank | 33 comment | 13 complexity | 5b4861f54812d2a9cf911ce4ee0957d3 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 Functor3 : IUnifiable
  32. {
  33. public readonly Atom _name;
  34. public readonly object _arg1;
  35. public readonly object _arg2;
  36. public readonly object _arg3;
  37. public Functor3(Atom name, object arg1, object arg2, object arg3)
  38. {
  39. _name = name;
  40. _arg1 = arg1;
  41. _arg2 = arg2;
  42. _arg3 = arg3;
  43. }
  44. public Functor3(string name, object arg1, object arg2, object arg3)
  45. : this(Atom.a(name), arg1, arg2, arg3)
  46. {
  47. }
  48. // disable warning on l1, don't see how we can
  49. // code this differently
  50. #pragma warning disable 0168, 0219
  51. /// If arg is another Functor3, then succeed (yield once) if this and arg have the
  52. /// same name and all functor args unify, otherwise fail (don't yield).
  53. /// If arg is a Variable, then call its unify to unify with this.
  54. /// Otherwise fail (don't yield).
  55. public IEnumerable<bool> unify(object arg)
  56. {
  57. arg = YP.getValue(arg);
  58. if (arg is Functor3)
  59. {
  60. Functor3 argFunctor = (Functor3)arg;
  61. if (_name.Equals(argFunctor._name))
  62. {
  63. foreach (bool l1 in YP.unify(_arg1, argFunctor._arg1))
  64. {
  65. foreach (bool l2 in YP.unify(_arg2, argFunctor._arg2))
  66. {
  67. foreach (bool l3 in YP.unify(_arg3, argFunctor._arg3))
  68. yield return false;
  69. }
  70. }
  71. }
  72. }
  73. else if (arg is Variable)
  74. {
  75. foreach (bool l1 in ((Variable)arg).unify(this))
  76. yield return false;
  77. }
  78. }
  79. #pragma warning restore 0168, 0219
  80. public override string ToString()
  81. {
  82. return _name + "(" + YP.getValue(_arg1) + ", " + YP.getValue(_arg2) + ", " +
  83. YP.getValue(_arg3) + ")";
  84. }
  85. public bool termEqual(object term)
  86. {
  87. term = YP.getValue(term);
  88. if (term is Functor3)
  89. {
  90. Functor3 termFunctor = (Functor3)term;
  91. return _name.Equals(termFunctor._name) && YP.termEqual(_arg1, termFunctor._arg1)
  92. && YP.termEqual(_arg2, termFunctor._arg2)
  93. && YP.termEqual(_arg3, termFunctor._arg3);
  94. }
  95. return false;
  96. }
  97. public bool lessThan(Functor3 functor)
  98. {
  99. // Do the equal check first since it is faster.
  100. if (!_name.Equals(functor._name))
  101. return _name.lessThan(functor._name);
  102. if (!YP.termEqual(_arg1, functor._arg1))
  103. return YP.termLessThan(_arg1, functor._arg1);
  104. if (!YP.termEqual(_arg2, functor._arg2))
  105. return YP.termLessThan(_arg2, functor._arg2);
  106. return YP.termLessThan(_arg3, functor._arg3);
  107. }
  108. public bool ground()
  109. {
  110. return YP.ground(_arg1) && YP.ground(_arg2) && YP.ground(_arg3);
  111. }
  112. public void addUniqueVariables(List<Variable> variableSet)
  113. {
  114. YP.addUniqueVariables(_arg1, variableSet);
  115. YP.addUniqueVariables(_arg2, variableSet);
  116. YP.addUniqueVariables(_arg3, variableSet);
  117. }
  118. public object makeCopy(Variable.CopyStore copyStore)
  119. {
  120. return new Functor3(_name, YP.makeCopy(_arg1, copyStore),
  121. YP.makeCopy(_arg2, copyStore), YP.makeCopy(_arg3, copyStore));
  122. }
  123. }
  124. }