NoiseFilter: Dont drop last word of apparent hostnames. Too many non-hostnames can...
[beagle.git] / Util / SemWeb / Statement.cs
blob8671bcab7a1dfdd750cefb65e261849b47128e4d
1 using System;
3 namespace SemWeb {
4 public struct Statement {
5 private Entity s;
6 private Entity p;
7 private Resource o;
8 private Entity m;
10 public static Entity DefaultMeta = new Entity(null);
12 public Statement(Entity subject, Entity predicate, Resource @object)
13 : this(subject, predicate, @object, DefaultMeta) {
16 public Statement(Entity subject, Entity predicate, Resource @object, Entity meta) {
17 s = subject;
18 p = predicate;
19 o = @object;
20 m = meta;
23 public Entity Subject { get { return s; } }
24 public Entity Predicate { get { return p; } }
25 public Resource Object { get { return o; } }
27 public Entity Meta { get { return m; } }
29 internal bool AnyNull {
30 get {
31 return Subject == null || Predicate == null || Object == null || Meta == null;
35 public Statement Invert() {
36 if (!(Object is Entity)) throw new ArgumentException("The object of the statement must be an entity.");
37 return new Statement((Entity)Object, Predicate, Subject, Meta);
40 public bool Matches(Statement statement) {
41 if (Subject != null && Subject != statement.Subject) return false;
42 if (Predicate != null && Predicate != statement.Predicate) return false;
43 if (Object != null && Object != statement.Object) return false;
44 if (Meta != null && Meta != statement.Meta) return false;
45 return true;
48 public override string ToString() {
49 string ret = "";
50 if (Subject != null) ret += "<" + Subject + "> "; else ret += "? ";
51 if (Predicate != null) ret += "<" + Predicate + "> "; else ret += "? ";
52 if (Object != null) {
53 if (Object is Literal)
54 ret += Object;
55 else
56 ret += "<" + Object + ">";
57 } else {
58 ret += "?";
60 if (Meta != null && Meta != DefaultMeta) ret += " meta=<" + Meta + ">";
61 return ret + ".";
64 public override bool Equals(object other) {
65 return (Statement)other == this;
68 public override int GetHashCode() {
69 int ret = 0;
70 if (s != null) ret = unchecked(ret + s.GetHashCode());
71 if (p != null) ret = unchecked(ret + p.GetHashCode());
72 if (o != null) ret = unchecked(ret + o.GetHashCode());
73 if (m != null) ret = unchecked(ret + m.GetHashCode());
74 return ret;
77 public static bool operator ==(Statement a, Statement b) {
78 if ((a.Subject == null) != (b.Subject == null)) return false;
79 if ((a.Predicate == null) != (b.Predicate == null)) return false;
80 if ((a.Object == null) != (b.Object == null)) return false;
81 if ((a.Meta == null) != (b.Meta == null)) return false;
82 if (a.Subject != null && !a.Subject.Equals(b.Subject)) return false;
83 if (a.Predicate != null && !a.Predicate.Equals(b.Predicate)) return false;
84 if (a.Object != null && !a.Object.Equals(b.Object)) return false;
85 if (a.Meta != null && !a.Meta.Equals(b.Meta)) return false;
86 return true;
88 public static bool operator !=(Statement a, Statement b) {
89 return !(a == b);
93 public struct SelectPartialFilter {
94 bool s, p, o, m;
95 bool first;
97 public static readonly SelectPartialFilter All = new SelectPartialFilter(true, true, true, true);
99 public SelectPartialFilter(bool subject, bool predicate, bool @object, bool meta) {
100 s = subject;
101 p = predicate;
102 o = @object;
103 m = meta;
105 first = false;
108 public bool Subject { get { return s; } }
109 public bool Predicate { get { return p; } }
110 public bool Object { get { return o; } }
111 public bool Meta { get { return m; } }
113 public bool SelectAll { get { return s && p && o && m; } }
114 public bool SelectNone { get { return !s && !p && !o && !m; } }
116 public bool SelectFirst { get { return first; } set { first = value; } }
118 public override string ToString() {
119 if (SelectAll) return "All";
120 if (SelectNone) return "None";
121 string ret = "";
122 if (Subject) ret += "S";
123 if (Predicate) ret += "P";
124 if (Object) ret += "O";
125 if (Meta) ret += "M";
126 return ret;