aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/Atom.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/Atom.cs')
-rw-r--r--OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/Atom.cs218
1 files changed, 218 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/Atom.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/Atom.cs
new file mode 100644
index 0000000..27ab04e
--- /dev/null
+++ b/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/Atom.cs
@@ -0,0 +1,218 @@
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
31using System;
32using System.Collections.Generic;
33using System.Text;
34
35namespace OpenSim.Region.ScriptEngine.Shared.YieldProlog
36{
37 public class Atom : IUnifiable
38 {
39 private static Dictionary<string, Atom> _atomStore = new Dictionary<string, Atom>();
40 public readonly string _name;
41 public readonly Atom _module;
42
43 /// <summary>
44 /// You should not call this constructor, but use Atom.a instead.
45 /// </summary>
46 /// <param name="name"></param>
47 /// <param name="module"></param>
48 private Atom(string name, Atom module)
49 {
50 _name = name;
51 _module = module;
52 }
53
54 /// <summary>
55 /// Return the unique Atom object for name where module is null. You should use this to create
56 /// an Atom instead of calling the Atom constructor.
57 /// </summary>
58 /// <param name="name"></param>
59 /// <returns></returns>
60 public static Atom a(string name)
61 {
62 Atom atom;
63 if (!_atomStore.TryGetValue(name, out atom))
64 {
65 atom = new Atom(name, null);
66 _atomStore[name] = atom;
67 }
68 return atom;
69 }
70
71 /// <summary>
72 /// Return an Atom object with the name and module. If module is null or Atom.NIL,
73 /// this behaves like Atom.a(name) and returns the unique object where the module is null.
74 /// If module is not null or Atom.NIL, this may or may not be the same object as another Atom
75 /// with the same name and module.
76 /// </summary>
77 /// <param name="name"></param>
78 /// <param name="module"></param>
79 /// <returns></returns>
80 public static Atom a(string name, Atom module)
81 {
82 if (module == null || module == Atom.NIL)
83 return a(name);
84 return new Atom(name, module);
85 }
86
87 /// <summary>
88 /// If Obj is an Atom unify its _module with Module. If the Atom's _module is null, use Atom.NIL.
89 /// </summary>
90 /// <param name="Atom"></param>
91 /// <param name="Module"></param>
92 /// <returns></returns>
93 public static IEnumerable<bool> module(object Obj, object Module)
94 {
95 Obj = YP.getValue(Obj);
96 if (Obj is Atom)
97 {
98 if (((Atom)Obj)._module == null)
99 return YP.unify(Module, Atom.NIL);
100 else
101 return YP.unify(Module, ((Atom)Obj)._module);
102 }
103 return YP.fail();
104 }
105
106 public static readonly Atom NIL = Atom.a("[]");
107 public static readonly Atom DOT = Atom.a(".");
108 public static readonly Atom F = Atom.a("f");
109 public static readonly Atom SLASH = Atom.a("/");
110 public static readonly Atom HAT = Atom.a("^");
111 public static readonly Atom RULE = Atom.a(":-");
112
113 public IEnumerable<bool> unify(object arg)
114 {
115 arg = YP.getValue(arg);
116 if (arg is Atom)
117 return Equals(arg) ? YP.succeed() : YP.fail();
118 else if (arg is Variable)
119 return ((Variable)arg).unify(this);
120 else
121 return YP.fail();
122 }
123
124 public void addUniqueVariables(List<Variable> variableSet)
125 {
126 // Atom does not contain variables.
127 }
128
129 public object makeCopy(Variable.CopyStore copyStore)
130 {
131 // Atom does not contain variables that need to be copied.
132 return this;
133 }
134
135 public bool termEqual(object term)
136 {
137 return Equals(YP.getValue(term));
138 }
139
140 public bool ground()
141 {
142 // Atom is always ground.
143 return true;
144 }
145
146 public override bool Equals(object obj)
147 {
148 if (obj is Atom)
149 {
150 if (_module == null && ((Atom)obj)._module == null)
151 // When _declaringClass is null, we always use an identical object from _atomStore.
152 return this == obj;
153 // Otherwise, ignore _declaringClass and do a normal string compare on the _name.
154 return _name == ((Atom)obj)._name;
155 }
156 return false;
157 }
158
159 public override string ToString()
160 {
161 return _name;
162 }
163
164 public override int GetHashCode()
165 {
166 // Debug: need to check _declaringClass.
167 return _name.GetHashCode();
168 }
169
170 public string toQuotedString()
171 {
172 if (_name.Length == 0)
173 return "''";
174 else if (this == Atom.NIL)
175 return "[]";
176
177 StringBuilder result = new StringBuilder(_name.Length);
178 bool useQuotes = false;
179 foreach (char c in _name)
180 {
181 int cInt = (int)c;
182 if (c == '\'')
183 {
184 result.Append("''");
185 useQuotes = true;
186 }
187 else if (c == '_' || cInt >= (int)'a' && cInt <= (int)'z' ||
188 cInt >= (int)'A' && cInt <= (int)'Z' || cInt >= (int)'0' && cInt <= (int)'9')
189 result.Append(c);
190 else
191 {
192 // Debug: Need to handle non-printable chars.
193 result.Append(c);
194 useQuotes = true;
195 }
196 }
197
198 if (!useQuotes && (int)_name[0] >= (int)'a' && (int)_name[0] <= (int)'z')
199 return result.ToString();
200 else
201 {
202 // Surround in single quotes.
203 result.Append('\'');
204 return "'" + result;
205 }
206 }
207
208 /// <summary>
209 /// Return true if _name is lexicographically less than atom._name.
210 /// </summary>
211 /// <param name="atom"></param>
212 /// <returns></returns>
213 public bool lessThan(Atom atom)
214 {
215 return _name.CompareTo(atom._name) < 0;
216 }
217 }
218}