opensim – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | eva | 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 | /// <summary> |
||
102 | /// If arg is another Functor, then succeed (yield once) if this and arg have the |
||
103 | /// same name and all functor args unify, otherwise fail (don't yield). |
||
104 | /// If arg is a Variable, then call its unify to unify with this. |
||
105 | /// Otherwise fail (don't yield). |
||
106 | /// </summary> |
||
107 | /// <param name="arg"></param> |
||
108 | /// <returns></returns> |
||
109 | public IEnumerable<bool> unify(object arg) |
||
110 | { |
||
111 | arg = YP.getValue(arg); |
||
112 | if (arg is Functor) |
||
113 | { |
||
114 | Functor argFunctor = (Functor)arg; |
||
115 | if (_name.Equals(argFunctor._name)) |
||
116 | return YP.unifyArrays(_args, argFunctor._args); |
||
117 | else |
||
118 | return YP.fail(); |
||
119 | } |
||
120 | else if (arg is Variable) |
||
121 | return ((Variable)arg).unify(this); |
||
122 | else |
||
123 | return YP.fail(); |
||
124 | } |
||
125 | |||
126 | public override string ToString() |
||
127 | { |
||
128 | string result = _name + "(" + YP.getValue(_args[0]); |
||
129 | for (int i = 1; i < _args.Length; ++i) |
||
130 | result += ", " + YP.getValue(_args[i]); |
||
131 | result += ")"; |
||
132 | return result; |
||
133 | } |
||
134 | |||
135 | public bool termEqual(object term) |
||
136 | { |
||
137 | term = YP.getValue(term); |
||
138 | if (term is Functor) |
||
139 | { |
||
140 | Functor termFunctor = (Functor)term; |
||
141 | if (_name.Equals(termFunctor._name) && _args.Length == termFunctor._args.Length) |
||
142 | { |
||
143 | for (int i = 0; i < _args.Length; ++i) |
||
144 | { |
||
145 | if (!YP.termEqual(_args[i], termFunctor._args[i])) |
||
146 | return false; |
||
147 | } |
||
148 | return true; |
||
149 | } |
||
150 | } |
||
151 | return false; |
||
152 | } |
||
153 | |||
154 | public bool lessThan(Functor functor) |
||
155 | { |
||
156 | // Do the equal check first since it is faster. |
||
157 | if (!_name.Equals(functor._name)) |
||
158 | return _name.lessThan(functor._name); |
||
159 | |||
160 | if (_args.Length != functor._args.Length) |
||
161 | return _args.Length < functor._args.Length; |
||
162 | |||
163 | for (int i = 0; i < _args.Length; ++i) |
||
164 | { |
||
165 | if (!YP.termEqual(_args[i], functor._args[i])) |
||
166 | return YP.termLessThan(_args[i], functor._args[i]); |
||
167 | } |
||
168 | |||
169 | return false; |
||
170 | } |
||
171 | |||
172 | public bool ground() |
||
173 | { |
||
174 | for (int i = 0; i < _args.Length; ++i) |
||
175 | { |
||
176 | if (!YP.ground(_args[i])) |
||
177 | return false; |
||
178 | } |
||
179 | return true; |
||
180 | } |
||
181 | |||
182 | public void addUniqueVariables(List<Variable> variableSet) |
||
183 | { |
||
184 | for (int i = 0; i < _args.Length; ++i) |
||
185 | YP.addUniqueVariables(_args[i], variableSet); |
||
186 | } |
||
187 | |||
188 | public object makeCopy(Variable.CopyStore copyStore) |
||
189 | { |
||
190 | object[] argsCopy = new object[_args.Length]; |
||
191 | for (int i = 0; i < _args.Length; ++i) |
||
192 | argsCopy[i] = YP.makeCopy(_args[i], copyStore); |
||
193 | return new Functor(_name, argsCopy); |
||
194 | } |
||
195 | } |
||
196 | } |