PageRenderTime 82ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/YieldProlog/Modules/Functor1.cs

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