diff options
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/Functor.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/Functor.cs | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/Functor.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/Functor.cs new file mode 100644 index 0000000..8ef8de0 --- /dev/null +++ b/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/Functor.cs | |||
@@ -0,0 +1,188 @@ | |||
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 | |||
31 | using System; | ||
32 | using System.Collections.Generic; | ||
33 | |||
34 | namespace OpenSim.Region.ScriptEngine.Shared.YieldProlog | ||
35 | { | ||
36 | public class Functor : IUnifiable | ||
37 | { | ||
38 | public readonly Atom _name; | ||
39 | public readonly object[] _args; | ||
40 | |||
41 | public Functor(Atom name, object[] args) | ||
42 | { | ||
43 | if (args.Length <= 3) | ||
44 | { | ||
45 | if (args.Length == 0) | ||
46 | throw new Exception("For arity 0 functor, just use name as an Atom"); | ||
47 | else if (args.Length == 1) | ||
48 | throw new Exception("For arity 1 functor, use Functor1"); | ||
49 | else if (args.Length == 2) | ||
50 | throw new Exception("For arity 2 functor, use Functor2"); | ||
51 | else if (args.Length == 3) | ||
52 | throw new Exception("For arity 3 functor, use Functor3"); | ||
53 | else | ||
54 | // (This shouldn't happen, but include it for completeness. | ||
55 | throw new Exception("Cannot create a Functor of arity " + args.Length); | ||
56 | } | ||
57 | |||
58 | _name = name; | ||
59 | _args = args; | ||
60 | } | ||
61 | |||
62 | public Functor(string name, object[] args) | ||
63 | : this(Atom.a(name), args) | ||
64 | { | ||
65 | } | ||
66 | |||
67 | /// <summary> | ||
68 | /// Return an Atom, Functor1, Functor2, Functor3 or Functor depending on the | ||
69 | /// length of args. | ||
70 | /// Note that this is different than the Functor constructor which requires | ||
71 | /// the length of args to be greater than 3. | ||
72 | /// </summary> | ||
73 | /// <param name="name"></param> | ||
74 | /// <param name="args"></param> | ||
75 | /// <returns></returns> | ||
76 | public static object make(Atom name, object[] args) | ||
77 | { | ||
78 | if (args.Length <= 0) | ||
79 | return name; | ||
80 | else if (args.Length == 1) | ||
81 | return new Functor1(name, args[0]); | ||
82 | else if (args.Length == 2) | ||
83 | return new Functor2(name, args[0], args[1]); | ||
84 | else if (args.Length == 3) | ||
85 | return new Functor3(name, args[0], args[1], args[2]); | ||
86 | else | ||
87 | return new Functor(name, args); | ||
88 | } | ||
89 | |||
90 | /// <summary> | ||
91 | /// Call the main make, first converting name to an Atom. | ||
92 | /// </summary> | ||
93 | /// <param name="name"></param> | ||
94 | /// <param name="args"></param> | ||
95 | /// <returns></returns> | ||
96 | public static object make(string name, object[] args) | ||
97 | { | ||
98 | return make(Atom.a(name), args); | ||
99 | } | ||
100 | |||
101 | public IEnumerable<bool> unify(object arg) | ||
102 | { | ||
103 | arg = YP.getValue(arg); | ||
104 | if (arg is Functor) | ||
105 | { | ||
106 | Functor argFunctor = (Functor)arg; | ||
107 | if (_name.Equals(argFunctor._name)) | ||
108 | return YP.unifyArrays(_args, argFunctor._args); | ||
109 | else | ||
110 | return YP.fail(); | ||
111 | } | ||
112 | else if (arg is Variable) | ||
113 | return ((Variable)arg).unify(this); | ||
114 | else | ||
115 | return YP.fail(); | ||
116 | } | ||
117 | |||
118 | public override string ToString() | ||
119 | { | ||
120 | string result = _name + "(" + YP.getValue(_args[0]); | ||
121 | for (int i = 1; i < _args.Length; ++i) | ||
122 | result += ", " + YP.getValue(_args[i]); | ||
123 | result += ")"; | ||
124 | return result; | ||
125 | } | ||
126 | |||
127 | public bool termEqual(object term) | ||
128 | { | ||
129 | term = YP.getValue(term); | ||
130 | if (term is Functor) | ||
131 | { | ||
132 | Functor termFunctor = (Functor)term; | ||
133 | if (_name.Equals(termFunctor._name) && _args.Length == termFunctor._args.Length) | ||
134 | { | ||
135 | for (int i = 0; i < _args.Length; ++i) | ||
136 | { | ||
137 | if (!YP.termEqual(_args[i], termFunctor._args[i])) | ||
138 | return false; | ||
139 | } | ||
140 | return true; | ||
141 | } | ||
142 | } | ||
143 | return false; | ||
144 | } | ||
145 | |||
146 | public bool lessThan(Functor functor) | ||
147 | { | ||
148 | // Do the equal check first since it is faster. | ||
149 | if (!_name.Equals(functor._name)) | ||
150 | return _name.lessThan(functor._name); | ||
151 | |||
152 | if (_args.Length != functor._args.Length) | ||
153 | return _args.Length < functor._args.Length; | ||
154 | |||
155 | for (int i = 0; i < _args.Length; ++i) | ||
156 | { | ||
157 | if (!YP.termEqual(_args[i], functor._args[i])) | ||
158 | return YP.termLessThan(_args[i], functor._args[i]); | ||
159 | } | ||
160 | |||
161 | return false; | ||
162 | } | ||
163 | |||
164 | public bool ground() | ||
165 | { | ||
166 | for (int i = 0; i < _args.Length; ++i) | ||
167 | { | ||
168 | if (!YP.ground(_args[i])) | ||
169 | return false; | ||
170 | } | ||
171 | return true; | ||
172 | } | ||
173 | |||
174 | public void addUniqueVariables(List<Variable> variableSet) | ||
175 | { | ||
176 | for (int i = 0; i < _args.Length; ++i) | ||
177 | YP.addUniqueVariables(_args[i], variableSet); | ||
178 | } | ||
179 | |||
180 | public object makeCopy(Variable.CopyStore copyStore) | ||
181 | { | ||
182 | object[] argsCopy = new object[_args.Length]; | ||
183 | for (int i = 0; i < _args.Length; ++i) | ||
184 | argsCopy[i] = YP.makeCopy(_args[i], copyStore); | ||
185 | return new Functor(_name, argsCopy); | ||
186 | } | ||
187 | } | ||
188 | } | ||