Changeset 1287 for trunk/sources/HeuristicLab.CEDMA.DB/Store.cs
- Timestamp:
- 03/08/09 12:48:18 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.CEDMA.DB/Store.cs
r557 r1287 28 28 using System.ServiceModel; 29 29 using System.Data; 30 using System.Data.SQLite;31 30 using System.Data.Common; 32 31 using System.Threading; … … 43 42 private object bigLock = new object(); 44 43 public Store(string connectionString) { 45 lock (bigLock) {44 lock (bigLock) { 46 45 this.connectionString = connectionString; 47 46 store = SemWeb.Store.Create(connectionString); 47 InitStore(); 48 } 49 } 50 51 private void InitStore() { 52 foreach (Statement s in Ontology.InitialStatements) { 53 Add(s); 48 54 } 49 55 } 50 56 51 57 public void Add(Statement statement) { 52 lock (bigLock) {58 lock (bigLock) { 53 59 store.Add(Translate(statement)); 54 60 } 55 61 } 56 62 57 public IList<Statement> Select(Statement template) { 58 SemWeb.SelectResult result; 59 lock(bigLock) { 60 result = store.Select(Translate(template)); 61 } 62 List<Statement> r = new List<Statement>(); 63 foreach(SemWeb.Statement resultStatement in result) { 64 r.Add(Translate(resultStatement)); 65 } 66 return r; 67 } 68 69 public IList<Statement> Select(SelectFilter filter) { 70 SemWeb.SelectResult result; 71 lock(bigLock) { 72 result = store.Select(Translate(filter)); 73 } 74 List<Statement> r = new List<Statement>(); 75 foreach(SemWeb.Statement resultStatement in result) { 76 r.Add(Translate(resultStatement)); 77 } 78 return r; 79 } 80 81 private SemWeb.SelectFilter Translate(SelectFilter filter) { 82 SemWeb.SelectFilter f = new SemWeb.SelectFilter(); 83 f.Subjects = Array.ConvertAll(filter.Subjects, s => Translate(s)); 84 f.Predicates = Array.ConvertAll(filter.Predicates, p => Translate(p)); 85 f.Objects = Array.ConvertAll(filter.Properties, prop => Translate(prop)); 86 return f; 87 } 88 89 private SemWeb.Entity Translate(Entity e) { 63 64 public ICollection<VariableBindings> Query(string query) { 65 MyQueryResultSink resultSink = new MyQueryResultSink(); 66 SemWeb.N3Reader n3Reader = new SemWeb.N3Reader(new StringReader(query)); 67 SemWeb.Query.GraphMatch matcher = new SemWeb.Query.GraphMatch(n3Reader); 68 matcher.Run(store, resultSink); 69 return resultSink.Bindings; 70 } 71 72 public ICollection<VariableBindings> Query(ICollection<Statement> query) { 73 MyQueryResultSink resultSink = new MyQueryResultSink(); 74 Translate(query).Run(store, resultSink); 75 return resultSink.Bindings; 76 } 77 78 private SemWeb.Query.Query Translate(ICollection<Statement> query) { 79 Dictionary<object, object> translatedObjects = new Dictionary<object, object>(); 80 SemWeb.MemoryStore queryStore = new SemWeb.MemoryStore(query.Select(st => Translate(st, translatedObjects)).ToArray()); 81 82 return new SemWeb.Query.GraphMatch(queryStore); 83 } 84 85 86 //public ICollection<Statement> Select(Statement template) { 87 // SemWeb.MemoryStore memStore = new SemWeb.MemoryStore(); 88 // lock (bigLock) { 89 // store.Select(Translate(template), memStore); 90 // } 91 // return memStore.Select(x=>Translate(x)).ToList(); 92 //} 93 94 //public ICollection<Statement> Select(SelectFilter filter) { 95 // SemWeb.MemoryStore memStore = new SemWeb.MemoryStore(); 96 // lock (bigLock) { 97 // store.Select(Translate(filter), memStore); 98 // } 99 // return memStore.Select(x => Translate(x)).ToList(); 100 //} 101 102 //private SemWeb.SelectFilter Translate(SelectFilter filter) { 103 // SemWeb.SelectFilter f = new SemWeb.SelectFilter(); 104 // f.Subjects = Array.ConvertAll(filter.Subjects, s => Translate(s)); 105 // f.Predicates = Array.ConvertAll(filter.Predicates, p => Translate(p)); 106 // f.Objects = Array.ConvertAll(filter.Properties, prop => Translate(prop)); 107 // return f; 108 //} 109 110 private static SemWeb.Entity Translate(Entity e) { 90 111 return e.Uri == null ? null : new SemWeb.Entity(e.Uri); 91 112 } 92 113 93 private SemWeb.Resource Translate(Resource prop) {94 if (prop is Literal) {114 private static SemWeb.Resource Translate(Resource prop) { 115 if (prop is Literal) { 95 116 return TranslateLiteral((Literal)prop); 96 } else if (prop is SerializedLiteral) {117 } else if (prop is SerializedLiteral) { 97 118 return TranslateLiteral((SerializedLiteral)prop); 98 119 } else { … … 101 122 } 102 123 103 private Statement Translate(SemWeb.Statement statement) { 104 if(statement.Object is SemWeb.Literal) { 105 return new Statement( 106 new Entity(statement.Subject.Uri), 107 new Entity(statement.Predicate.Uri), 108 TranslateLiteral((SemWeb.Literal)statement.Object)); 109 } else { 110 return new Statement( 111 new Entity(statement.Subject.Uri), 112 new Entity(statement.Predicate.Uri), 113 new Entity(((SemWeb.Entity)statement.Object).Uri)); 114 } 115 } 116 117 private SemWeb.Statement Translate(Statement statement) { 124 private static Statement Translate(SemWeb.Statement statement) { 125 return Translate(statement, new Dictionary<object, object>()); 126 } 127 128 private static Statement Translate(SemWeb.Statement statement, Dictionary<object, object> translatedObjects) { 129 if (!translatedObjects.ContainsKey(statement.Subject)) { 130 translatedObjects[statement.Subject] = new Entity(statement.Subject.Uri); 131 } 132 if (!translatedObjects.ContainsKey(statement.Predicate)) { 133 translatedObjects[statement.Predicate] = new Entity(statement.Predicate.Uri); 134 } 135 if (!translatedObjects.ContainsKey(statement.Object)) { 136 if (statement.Object is SemWeb.Literal) { 137 translatedObjects[statement.Object] = TranslateLiteral((SemWeb.Literal)statement.Object); 138 } else { 139 translatedObjects[statement.Object] = new Entity(((SemWeb.Entity)statement.Object).Uri); 140 } 141 } 142 143 Entity subjectEntity = (Entity)translatedObjects[statement.Subject]; 144 Entity predicateEntity = (Entity)translatedObjects[statement.Predicate]; 145 Resource property = (Resource)translatedObjects[statement.Object]; 146 147 return new Statement( 148 subjectEntity, 149 predicateEntity, 150 property); 151 } 152 153 private static SemWeb.Statement Translate(Statement statement) { 154 return Translate(statement, new Dictionary<object, object>()); 155 } 156 157 private static SemWeb.Statement Translate(Statement statement, Dictionary<object, object> translatedObjects) { 158 if (!translatedObjects.ContainsKey(statement.Subject)) { 159 translatedObjects[statement.Subject] = Translate(statement.Subject); 160 } 161 if (!translatedObjects.ContainsKey(statement.Predicate)) { 162 translatedObjects[statement.Predicate] = Translate(statement.Predicate); 163 } 164 if (!translatedObjects.ContainsKey(statement.Property)) { 165 translatedObjects[statement.Property] = Translate(statement.Property); 166 } 167 168 SemWeb.Entity subject = (SemWeb.Entity)translatedObjects[statement.Subject]; 169 SemWeb.Entity predicate = (SemWeb.Entity)translatedObjects[statement.Predicate]; 170 SemWeb.Resource property = (SemWeb.Resource)translatedObjects[statement.Property]; 171 118 172 return new SemWeb.Statement( 119 Translate(statement.Subject),120 Translate(statement.Predicate),121 Translate(statement.Property));122 } 123 124 private SemWeb.Literal TranslateLiteral(SerializedLiteral l) {125 if (l.RawData == null) return null;173 subject, 174 predicate, 175 property); 176 } 177 178 private static SemWeb.Literal TranslateLiteral(SerializedLiteral l) { 179 if (l.RawData == null) return null; 126 180 return new SemWeb.Literal(l.RawData, null, "serializedItem"); 127 181 } 128 182 129 private SemWeb.Literal TranslateLiteral(Literal l) {130 if (l.Value == null) return null;131 if (l.Value is double) return SemWeb.Literal.FromValue((double)l.Value);132 else if (l.Value is bool) return SemWeb.Literal.FromValue((bool)l.Value);133 else if (l.Value is int) return SemWeb.Literal.FromValue((int)l.Value);134 else if (l.Value is long) return SemWeb.Literal.FromValue((long)l.Value);135 else if (l.Value is string) return SemWeb.Literal.FromValue((string)l.Value);183 private static SemWeb.Literal TranslateLiteral(Literal l) { 184 if (l.Value == null) return null; 185 if (l.Value is double) return SemWeb.Literal.FromValue((double)l.Value); 186 else if (l.Value is bool) return SemWeb.Literal.FromValue((bool)l.Value); 187 else if (l.Value is int) return SemWeb.Literal.FromValue((int)l.Value); 188 else if (l.Value is long) return SemWeb.Literal.FromValue((long)l.Value); 189 else if (l.Value is string) return SemWeb.Literal.FromValue((string)l.Value); 136 190 else return new SemWeb.Literal(l.Value.ToString()); 137 191 } 138 192 139 private Resource TranslateLiteral(SemWeb.Literal l) {140 if (l.DataType == "serializedItem") {193 private static Resource TranslateLiteral(SemWeb.Literal l) { 194 if (l.DataType == "serializedItem") { 141 195 return new SerializedLiteral(l.Value); 142 } else if (l.DataType != null) {196 } else if (l.DataType != null) { 143 197 return new Literal(l.ParseValue()); 144 198 } else { … … 146 200 } 147 201 } 202 203 private class MyQueryResultSink : SemWeb.Query.QueryResultSink { 204 205 private List<VariableBindings> bindings = new List<VariableBindings>(); 206 public ICollection<VariableBindings> Bindings { 207 get { return bindings.AsReadOnly(); } 208 } 209 210 public override bool Add(SemWeb.Query.VariableBindings result) { 211 VariableBindings varBindings = new VariableBindings(); 212 foreach (SemWeb.Variable var in result.Variables) { 213 if (var.LocalName != null && result[var] != null) { 214 if (result[var] is SemWeb.Literal) { 215 varBindings.Add(var.LocalName, TranslateLiteral((SemWeb.Literal)result[var])); 216 } else { 217 varBindings.Add(var.LocalName, new Entity(((SemWeb.Entity)result[var]).Uri)); 218 } 219 } 220 bindings.Add(varBindings); 221 } 222 return true; 223 } 224 } 148 225 } 149 226 }
Note: See TracChangeset
for help on using the changeset viewer.