File: Statement.cs

package info (click to toggle)
beagle 0.2.12-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 12,204 kB
  • ctags: 16,188
  • sloc: cs: 91,628; sh: 27,627; ansic: 10,646; makefile: 2,248; xml: 15
file content (129 lines) | stat: -rw-r--r-- 4,054 bytes parent folder | download
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
using System;

namespace SemWeb {
	public struct Statement {
		private Entity s;
		private Entity p;
		private Resource o;
		private Entity m;
		
		public static Entity DefaultMeta = new Entity(null);
		
		public Statement(Entity subject, Entity predicate, Resource @object)
		: this(subject, predicate, @object, DefaultMeta) {
		}
		
		public Statement(Entity subject, Entity predicate, Resource @object, Entity meta) {
		  s = subject;
		  p = predicate;
		  o = @object;
		  m = meta;
		}
		
		public Entity Subject { get { return s; } }
		public Entity Predicate { get { return p; } }
		public Resource Object { get { return o; } }
		
		public Entity Meta { get { return m; } }
		
		internal 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) return false;
			if (Predicate != null && Predicate != statement.Predicate) return false;
			if (Object != null && Object != statement.Object) return false;
			if (Meta != null && Meta != statement.Meta) return false;
			return true;
		}
		
		public override string ToString() {
			string ret = "";
			if (Subject != null) ret += "<" + Subject + "> "; else ret += "? ";
			if (Predicate != null) ret += "<" + Predicate + "> "; else ret += "? ";
			if (Object != null) {
				if (Object is Literal)
					ret += Object;
				else
					ret += "<" + Object + ">";
			} else {
				ret += "?";
			}
			if (Meta != null && Meta != DefaultMeta) ret += " meta=<" + Meta + ">";
			return ret + ".";
		}
		
		public override bool Equals(object other) {
			return (Statement)other == this;
		}
		
		public override int GetHashCode() {
			int ret = 0;
			if (s != null) ret = unchecked(ret + s.GetHashCode());
			if (p != null) ret = unchecked(ret + p.GetHashCode());
			if (o != null) ret = unchecked(ret + o.GetHashCode());
			if (m != null) ret = unchecked(ret + m.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);
		}
	}
	
	public struct SelectPartialFilter {
		bool s, p, o, m;
		bool first;
		
		public static readonly SelectPartialFilter All = new SelectPartialFilter(true, true, true, true);
		
		public SelectPartialFilter(bool subject, bool predicate, bool @object, bool meta) {
			s = subject;
			p = predicate;
			o = @object;
			m = meta;
			
			first = false;
		}
		
		public bool Subject { get { return s; } }
		public bool Predicate { get { return p; } }
		public bool Object { get { return o; } }
		public bool Meta { get { return m; } }
		
		public bool SelectAll { get { return s && p && o && m; } }
		public bool SelectNone { get { return !s && !p && !o && !m; } }
		
		public bool SelectFirst { get { return first; } set { first = value; } }
		
		public override string ToString() {
			if (SelectAll) return "All";
			if (SelectNone) return "None";
			string ret = "";
			if (Subject) ret += "S";
			if (Predicate) ret += "P";
			if (Object) ret += "O";
			if (Meta) ret += "M";
			return ret;
		}
	}
}