1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
using System;
using System.Collections;
using SemWeb.Util;
namespace SemWeb {
public struct Statement :
#if DOTNET2
IEquatable<Statement>, IComparable<Statement>
#else
IComparable
#endif
{
public Entity Subject;
public Entity Predicate;
public Resource Object;
public Entity Meta;
public static Entity DefaultMeta = new BNode();
public static Statement All = new Statement(null, null, null, null);
public Statement(Entity subject, Entity predicate, Resource @object)
: this(subject, predicate, @object, DefaultMeta) {
}
public Statement(Entity subject, Entity predicate, Resource @object, Entity meta) {
Subject = subject;
Predicate = predicate;
Object = @object;
Meta = meta;
}
public bool AnyNull {
get {
return Subject == null || Predicate == null || Object == null || Meta == null;
}
}
public Statement Invert() {
if (!(Object is Entity)) throw new ArgumentException("The object of the statement must be an entity.");
return new Statement((Entity)Object, Predicate, Subject, Meta);
}
public bool Matches(Statement statement) {
if (Subject != null && Subject != statement.Subject && statement.Subject != null) return false;
if (Predicate != null && Predicate != statement.Predicate && statement.Predicate != null) return false;
if (Object != null && Object != statement.Object && statement.Object != null) return false;
if (Meta != null && Meta != statement.Meta && statement.Meta != null) return false;
return true;
}
public override string ToString() {
string ret = "";
if (Subject != null) ret += Subject.ToString(); else ret += "?"; ret += " ";
if (Predicate != null) ret += Predicate.ToString(); else ret += "?"; ret += " ";
if (Object != null) ret += Object.ToString(); else ret += "?"; ret += " ";
if (Meta != null && Meta != DefaultMeta) ret += "meta=" + Meta.ToString();
return ret + ".";
}
public Statement Replace(Resource find, Resource replacement) {
if (replacement is Literal) {
if (find == Object)
return new Statement(Subject, Predicate, replacement, Meta);
return this;
} else {
Entity ent = (Entity)replacement;
return new Statement(
Subject == find ? ent : Subject,
Predicate == find ? ent : Predicate,
Object == find ? ent : Object,
Meta == find ? ent : Meta
);
}
}
public Statement Replace(Hashtable resourceMap) {
return new Statement(
!resourceMap.ContainsKey(Subject) ? Subject : (Entity)resourceMap[Subject],
!resourceMap.ContainsKey(Predicate) ? Predicate : (Entity)resourceMap[Predicate],
!resourceMap.ContainsKey(Object) ? Object : (Resource)resourceMap[Object],
!resourceMap.ContainsKey(Meta) ? Meta : (Entity)resourceMap[Meta]
);
}
public override bool Equals(object other) {
return (Statement)other == this;
}
#if DOTNET2
bool IEquatable<Statement>.Equals(Statement other) {
return other == this;
}
#endif
public override int GetHashCode() {
int ret = 0;
if (Subject != null) ret = unchecked(ret + Subject.GetHashCode());
if (Predicate != null) ret = unchecked(ret + Predicate.GetHashCode());
if (Object != null) ret = unchecked(ret + Object.GetHashCode());
if (Meta != null) ret = unchecked(ret + Meta.GetHashCode());
return ret;
}
public static bool operator ==(Statement a, Statement b) {
if ((a.Subject == null) != (b.Subject == null)) return false;
if ((a.Predicate == null) != (b.Predicate == null)) return false;
if ((a.Object == null) != (b.Object == null)) return false;
if ((a.Meta == null) != (b.Meta == null)) return false;
if (a.Subject != null && !a.Subject.Equals(b.Subject)) return false;
if (a.Predicate != null && !a.Predicate.Equals(b.Predicate)) return false;
if (a.Object != null && !a.Object.Equals(b.Object)) return false;
if (a.Meta != null && !a.Meta.Equals(b.Meta)) return false;
return true;
}
public static bool operator !=(Statement a, Statement b) {
return !(a == b);
}
#if !DOTNET2
int IComparable.CompareTo(object other) {
return CompareTo((Statement)other);
}
#endif
public int CompareTo(Statement s) {
int x;
x = cmp(Subject, s.Subject); if (x != 0) return x;
x = cmp(Predicate, s.Predicate); if (x != 0) return x;
x = cmp(Object, s.Object); if (x != 0) return x;
x = cmp(Meta, s.Meta); if (x != 0) return x;
return 0;
}
int cmp(Resource a, Resource b) {
if (a == null && b == null) return 0;
if (a == null) return -1;
if (b == null) return 1;
return ((IComparable)a).CompareTo(b);
}
internal Resource GetComponent(int index) {
switch (index) {
case 0: return Subject;
case 1: return Predicate;
case 2: return Object;
case 3: return Meta;
}
throw new ArgumentException("index");
}
internal void SetComponent(int index, Resource r) {
switch (index) {
case 0: Subject = (Entity)r; break;
case 1: Predicate = (Entity)r; break;
case 2: Object = r; break;
case 3: Meta = (Entity)r; break;
default: throw new ArgumentException("index");
}
}
}
public struct SelectFilter : IEnumerable {
public Entity[] Subjects;
public Entity[] Predicates;
public Resource[] Objects;
public Entity[] Metas;
public LiteralFilter[] LiteralFilters;
public int Limit;
public static SelectFilter All = new SelectFilter(null, null, null, null);
public SelectFilter(Statement statement) {
Subjects = null; Predicates = null; Objects = null; Metas = null; LiteralFilters = null; Limit = 0;
if (statement.Subject != null) Subjects = new Entity[] { statement.Subject };
if (statement.Predicate != null) Predicates = new Entity[] { statement.Predicate };
if (statement.Object != null) Objects = new Resource[] { statement.Object };
if (statement.Meta != null) Metas = new Entity[] { statement.Meta };
}
public SelectFilter(Entity[] subjects, Entity[] predicates, Resource[] objects, Entity[] metas) {
Subjects = null; Predicates = null; Objects = null; Metas = null; LiteralFilters = null; Limit = 0;
Subjects = subjects;
Predicates = predicates;
Objects = objects;
Metas = metas;
}
internal Resource[] GetComponent(int index) {
switch (index) {
case 0: return Subjects;
case 1: return Predicates;
case 2: return Objects;
case 3: return Metas;
}
throw new ArgumentException("index");
}
internal void SetComponent(int index, Resource[] res) {
switch (index) {
case 0: Subjects = (Entity[])res; break;
case 1: Predicates = (Entity[])res; break;
case 2: Objects = res; break;
case 3: Metas = (Entity[])res; break;
default: throw new ArgumentException("index");
}
}
public override string ToString() {
string ret =
ToString(Subjects) + " " +
ToString(Predicates) + " " +
ToString(Objects);
if (Metas == null || Metas.Length > 1 || Metas[0] != Statement.DefaultMeta)
ret += " meta=" + ToString(Metas);
return ret;
}
private string ToString(Resource[] res) {
if (res == null) return "?";
if (res.Length == 1) return res[0].ToString();
System.Text.StringBuilder b = new System.Text.StringBuilder();
b.Append("{ ");
bool first = true;
bool cutoff = false;
foreach (Resource r in res) {
if (!first) b.Append(", "); first = false;
if (b.Length > 50) { b.Append("..."); cutoff = true; break; }
b.Append(r.ToString());
}
b.Append(" }");
if (cutoff) b.Insert(2, "(" + res.Length +") ");
return b.ToString();
}
public override bool Equals(object other) {
return this == (SelectFilter)other;
}
public override int GetHashCode() {
int hc = 0;
if (Subjects != null) hc ^= Subjects[0].GetHashCode();
if (Predicates != null) hc ^= Predicates[0].GetHashCode();
if (Objects != null) hc ^= Objects[0].GetHashCode();
if (Metas != null) hc ^= Metas[0].GetHashCode();
return hc;
}
public static bool operator ==(SelectFilter a, SelectFilter b) {
return eq(a.Subjects, b.Subjects)
&& eq(a.Predicates, b.Predicates)
&& eq(a.Objects, b.Objects)
&& eq(a.Metas, b.Metas)
&& a.LiteralFilters == b.LiteralFilters
&& a.Limit == b.Limit;
}
public static bool operator !=(SelectFilter a, SelectFilter b) {
return !(a == b);
}
static bool eq(Resource[] a, Resource[] b) {
if (a == b) return true;
if (a == null || b == null) return false;
if (a.Length != b.Length) return false;
bool alleq = true;
for (int i = 0; i < a.Length; i++)
if (!a[i].Equals(b[i]))
alleq = false;
if (alleq) return true;
ResSet xa = new ResSet(a);
ResSet xb = new ResSet(b);
xa.RetainAll(xb);
return xa.Count == xb.Count;
}
public static SelectFilter[] FromGraph(Statement[] graph) {
SelectFilter[] ret = new SelectFilter[graph.Length];
for (int i = 0; i < ret.Length; i++)
ret[i] = new SelectFilter(graph[i]);
return ret;
}
public IEnumerator GetEnumerator() {
return new StatementIterator(this);
}
class StatementIterator : IEnumerator {
SelectFilter f;
SemWeb.Util.Permutation p;
int[] cur;
public StatementIterator(SelectFilter filter) {
f = filter;
p = new SemWeb.Util.Permutation(new int[] {
f.Subjects == null ? 1 : f.Subjects.Length,
f.Predicates == null ? 1 : f.Predicates.Length,
f.Objects == null ? 1 : f.Objects.Length,
f.Metas == null ? 1 : f.Metas.Length,
} );
}
public object Current {
get {
if (cur == null) throw new InvalidOperationException("Call MoveNext!");
return new Statement(
f.Subjects == null ? null : f.Subjects[cur[0]],
f.Predicates == null ? null : f.Predicates[cur[1]],
f.Objects == null ? null : f.Objects[cur[2]],
f.Metas == null ? null : f.Metas[cur[3]]
);
}
}
public bool MoveNext() {
cur = p.Next();
return cur != null;
}
public void Reset() { cur = null; p.Reset(); }
}
}
}
|