public void Replace(Entity a, Entity b) { MemoryStore removals = new MemoryStore(); MemoryStore additions = new MemoryStore(); foreach (Statement statement in statements) { if ((statement.Subject != null && statement.Subject == a) || (statement.Predicate != null && statement.Predicate == a) || (statement.Object != null && statement.Object == a) || (statement.Meta != null && statement.Meta == a)) { removals.Add(statement); additions.Add(statement.Replace(a, b)); } } RemoveAll(removals.ToArray()); Import(additions); }
public static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("Usage: euler.exe axioms.n3 axioms... {questions.n3 | -sparql query.sparql}"); return; } // Load Axioms bool sparql = false; MemoryStore axioms = new MemoryStore(); for (int i = 0; i < args.Length-1; i++) { if (i > 0 && i == args.Length-2 && args[i] == "-sparql") { sparql = true; break; } N3Reader axiomsreader = new N3Reader(args[i]); axiomsreader.BaseUri = "http://www.example.org/arbitrary/base#"; axioms.Import(axiomsreader); } Euler engine = new Euler(axioms); // Load question if (!sparql) { MemoryStore question = new MemoryStore(); question.Import(new N3Reader(args[args.Length-1])); Proof[] proofs = engine.Prove(null, question.ToArray()); foreach (Proof p in proofs) { Console.WriteLine(p.ToString()); } } else { using (StreamReader fs = new StreamReader(args[args.Length-1])) { string q = fs.ReadToEnd(); Store store = new Store(); store.AddReasoner(engine); SparqlEngine s = new SparqlEngine(q); s.Run(store, Console.Out); } } }
public bool hasNext() { if (statements == null) { System.DateTime start = System.DateTime.Now; MemoryStore results = new MemoryStore(); StatementSink sink = results; if (!source.Distinct) sink = new SemWeb.Util.DistinctStatementsSink(results, !wantMetas); source.Select(filter, sink); wrapper.Log("SELECT: " + filter + " => " + results.StatementCount + " statements [" + (System.DateTime.Now-start) + "s]"); statements = results.ToArray(); } return curindex + 1 < statements.Length; }
private StatementIterator GetIterator(Entity[] subjects, Entity[] predicates, Resource[] objects, Entity[] metas, java.util.List litFilters, bool defaultGraph) { DateTime start = DateTime.Now; if (subjects == null && predicates == null && objects == null) throw new QueryExecutionException("Query would select all statements in the store."); if (subjects != null) Depersist(subjects); if (predicates != null) Depersist(predicates); if (objects != null) Depersist(objects); if (metas != null) Depersist(metas); if (subjects != null && subjects.Length == 0) return new StatementIterator(null); if (predicates != null && predicates.Length == 0) return new StatementIterator(null); if (objects != null && objects.Length == 0) return new StatementIterator(null); if (metas != null && metas.Length == 0) return new StatementIterator(null); MemoryStore results = new MemoryStore(); StatementSink sink = results; if (!source.Distinct) sink = new SemWeb.Util.DistinctStatementsSink(results, defaultGraph && metas == null); SelectFilter filter = new SelectFilter(subjects, predicates, objects, metas); if (litFilters != null) { filter.LiteralFilters = new LiteralFilter[litFilters.size()]; for (int i = 0; i < litFilters.size(); i++) filter.LiteralFilters[i] = (LiteralFilter)litFilters.get(i); } source.Select(filter, sink); Log("SELECT: " + filter + " => " + results.StatementCount + " statements [" + (DateTime.Now-start) + "s]"); return new StatementIterator(results.ToArray()); }
public static void MakeLean(Store store, SelectableSource relativeTo, StatementSink removed) { // Break the data source into MSGs. Make each MSG // lean first (in isolation). Then check each lean MSG // to see if it's already entailed by the whole store, // or by relativeTo if it's provided (not null). MSG.Graph[] msgs = MSG.FindMSGs(store, true); foreach (MSG.Graph msgg in msgs) { // Load the MSG into memory. MemoryStore msg = new MemoryStore(msgg); // unnecessary duplication... // Make this MSG lean. The "right" thing to do is // to consider all of the 'connected' subgraphs of MSG // against the whole store, rather than the MSG in // isolation. But that gets much too expensive. MemoryStore msgremoved = new MemoryStore(); MakeLeanMSG(new Store(msg), msgg.GetBNodes(), msgremoved); // Whatever was removed from msg, remove it from the main graph. store.RemoveAll(msgremoved.ToArray()); // And track what was removed. if (removed != null) msgremoved.Select(removed); // If this MSG is now (somehow) empty (shouldn't happen, // but one never knows), don't test for entailment. if (msg.StatementCount == 0) continue; // Remove this MSG if it is already entailed. // The GraphMatch will treat all blank nodes in // msg as variables. GraphMatch match = new GraphMatch(msg); QueryResultBuffer sink = new QueryResultBuffer(); match.Run(new SubtractionSource(store, msg), sink); if (sink.Bindings.Count > 0) { // This MSG can be removed. store.RemoveAll(msg.ToArray()); if (removed != null) msg.Select(removed); } else if (relativeTo != null) { match.Run(relativeTo, sink); if (sink.Bindings.Count > 0) { // This MSG can be removed. store.RemoveAll(msg.ToArray()); if (removed != null) msg.Select(removed); } } } }
// The next few routines convert a set of axioms from a StatementSource // into a data structure of use for the algorithm, with Sequents and things. private static Hashtable RulesToCases(StatementSource rules) { Hashtable cases = new Hashtable(); MemoryStore rules_store = new MemoryStore(rules); foreach (Statement p in rules_store) { if (p.Meta == Statement.DefaultMeta) { if (p.Predicate == entLOGIMPLIES && p.Object is Entity) { MemoryStore body = new MemoryStore(); MemoryStore head = new MemoryStore(); rules_store.Select(new Statement(null, null, null, (Entity)p.Subject), new RemoveMeta(body)); rules_store.Select(new Statement(null, null, null, (Entity)p.Object), new RemoveMeta(head)); // Any variables in the head not bound in the body represent existentially closed bnodes. // (Euler's OWL test case does this. Wish they had used bnodes instead of vars...) ResSet bodyvars = new ResSet(); foreach (Statement b in body) { if (b.Subject is Variable) bodyvars.Add(b.Subject); if (b.Predicate is Variable) bodyvars.Add(b.Predicate); if (b.Object is Variable) bodyvars.Add(b.Object); } foreach (Entity v in head.GetEntities()) { if (v is Variable && !bodyvars.Contains(v)) head.Replace(v, new BNode(((Variable)v).LocalName)); } // Replace (...) lists in the body that are tied to the subjects // of user predicates with callArgs objects. Hashtable callArgs = new Hashtable(); CollectCallArgs(body, callArgs); // Rules can't have more than one statement in their // consequent. The best we can do is break up // the consequent into multiple rules. (Since all head // variables are bound in body, it's equivalent...?) foreach (Statement h in head) AddSequent(cases, new Sequent(h, body.ToArray(), callArgs)); } else { AddSequent(cases, new Sequent(p, new Statement[0], null)); } } } return cases; }
public override void Replace(Entity a, Entity b) { MemoryStore removals = new MemoryStore(); MemoryStore additions = new MemoryStore(); foreach (Statement statement in statements) { if ((statement.Subject != null && statement.Subject == a) || (statement.Predicate != null && statement.Predicate == a) || (statement.Object != null && statement.Object == a) || (statement.Meta != null && statement.Meta == a)) { removals.Add(statement); additions.Add(statement.Replace(a, b)); } } RemoveAll(removals.ToArray()); Import(additions); }