public void rdfRemoteEndpointToKB(string endpointURI, string graphKBName, string query, string assertTemplate) { //Define a remote endpoint //Use the DBPedia SPARQL endpoint with the default Graph set to DBPedia SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri(endpointURI)); var focus = FindOrCreateKB(graphKBName); RdfRules ruleDefs = new RdfRules(focus.RdfStore.rdfGraph); StringWriter miniMt = new StringWriter(); //Use the extension method ExecuteQuery() to make the query against the Graph try { //Object results = g.ExecuteQuery(query); //Make a SELECT query against the Endpoint SparqlResultSet results = endpoint.QueryWithResultSet(query); GetMiniMt(results, assertTemplate, graphKBName, focus.RdfStore, show, null, miniMt, ruleDefs); } catch (RdfQueryException queryEx) { //There was an error executing the query so handle it here Warn("While endpointURI={0}\n\n{1}", endpointURI, queryEx); } insertKB(miniMt.ToString(), graphKBName); }
public void IncludeRules(RdfRules sourceRules) { AddNew(sourceRules.Producing, Producing); AddNew(sourceRules.Requirements, Requirements); AddNew(sourceRules.Consequences, Consequences); AddNew(sourceRules.Subjects, Subjects); }
static public INode TermToRdf(Part part, RdfRules triples) { Part car, cdr; if (GetCons(part, out car, out cdr)) { var definations = triples.def; var rdf = CreateBlankNode(definations, "CONS" + CONSP); triples.AddRequirement(rdf, "rdf:first", car); triples.AddRequirement(rdf, "rdf:rest", cdr); return(rdf); } if (part is Term) { RdfRules rules = PartToTriples(part, triples.def); rules.Requirements.ToList().ForEach(triples.AddRequirement); rules.Consequences.ToList().ForEach(triples.AddConsequent); rules.Producing.ToList().ForEach(triples.AddProducing); var ret = rules.RuleNode; if (ret == null) { Warn("No rule node for " + part); return(null); } else { return(ret); } } throw ErrorBadOp("ToRDF on " + part); }
internal void pushGraphToKB(bool clearPrologFirst) { SaveOffRDF(); var focus = PrologKB; WarnAndClear(focus, ContentBackingStore.RdfMemory, clearPrologFirst, focus.ClearPrologCache, ContentBackingStore.Prolog); var trips = Triples; if (trips.Count == 0) { return; } lock (trips) { focus.pdb.index.Clear(); foreach (Triple triple in trips) { RdfRules rules = new RdfRules(rdfGraph); var term = MakeTerm(TripleName, GraphWithDef.RdfToPart(triple.Subject, rules), GraphWithDef.RdfToPart(triple.Predicate, rules), GraphWithDef.RdfToPart(triple.Object, rules)); var rule = new Rule(term); rule.rdfRuleCache = rules; if (!focus.pdb.rules.Contains(rule)) { focus.pdb.rules.Add(rule); } } } }
public void refreshRDFGraphOLD() { var rdfGraphWithDefs = FindOrCreateKB("rdfMT"); var rdfGraph = rdfGraphWithDefs.RdfStore.rdfGraph; EnsureGraphPrefixes(rdfGraph); // Possibly called by the Sparql endpoint before servicing a query // Is there anything we want to update rdfGraph with ? var bingingsList = new ListOfBindings(); string mt = "spindleMT"; askQuery(ParseQuery("triple(S,P,O)", mt), mt, true, bingingsList, null); RdfRules newTriples = new RdfRules(rdfGraph); foreach (var bindings in bingingsList) { //foreach (string k in bindings.Keys) //{ rdfGraphAssert(rdfGraph, MakeTriple(GraphWithDef.PartToRdf(bindings["S"], newTriples), GraphWithDef.PartToRdf(bindings["P"], newTriples), GraphWithDef.PartToRdf(bindings["O"], newTriples))); string rdfLine = String.Format(@"<robokind:{0}> <robokind:{1}> <robokind:{2}> .", bindings["S"].ToString(), bindings["P"].ToString(), bindings["O"].ToString()); StringParser.Parse(rdfGraph, rdfLine); // } } newTriples.AssertTriples(rdfGraphWithDefs.definations, true, true); }
static public string PlReadble(INode subject0, RdfRules rules) { var subject1 = ToValueNode(subject0); Part part1 = RdfToPart(subject1, rules); string readable = part1.ToSource(SourceLanguage.Prolog); if (RdfDeveloperSanityChecks < 2) { return(readable); } INode subject2 = PartToRdf(part1, rules); if (!ReferenceEquals(subject2, subject1)) { if (!subject1.Equals(subject2)) { Warn("PlReadble not round tripping! INodes " + NodeDesc(subject1) + "->" + NodeDesc(subject2)); } } Tokeniser oldTokenizer = new Tokeniser(readable); string mt = rules.prologMt; Part part2 = ParsePart(oldTokenizer, mt); if (part2 == null || !part2.Equals(part1)) { if (part2 != null) { part2.Equals(part1); } readable = part1.ToSource(SourceLanguage.Prolog); string readable2 = null; if (part2 != null) { readable2 = part2.ToSource(SourceLanguage.Prolog); } tl_spy_prolog_reader = true; Tokeniser newTokeniser = new Tokeniser(readable); part2 = ParsePart(newTokeniser, mt); tl_spy_prolog_reader = false; Warn("PlReadble not round tripping! re-readablity Node=" + NodeDesc(subject1) + " part1=" + PartDesc(part1) + ".ToPLReadable()->" + readable); } if ((part2 is Variable && subject1.NodeType != NodeType.Variable)) { Warn("PlReadble not round tripping! Making a prolog variable? " + NodeDesc(subject1) + " part1= " + PartDesc(part1)); } return(readable); }
private static void Release(INode tripleInst, RdfRules ruleCache, Rule r) { if (tripleInst.NodeType != NodeType.Blank) { Warn("Removing non Bnode " + r); } //ConsoleWriteLine("Remove Rule: " + r); IGraph graph = ruleCache.ContainingGraph ?? tripleInst.Graph; IEnumerable <Triple> found = LockInfo.CopyOf(graph.GetTriples(tripleInst)); int fnd = 0; foreach (Triple triple in found) { // ConsoleWriteLine("Remove triple: " + triple); triple.Graph.Retract(triple); fnd++; } //ConsoleWriteLine("Removed triples: " + fnd); }
static public Part RdfToPart(INode node, RdfRules triples) { SIProlog.checkNode(node); if (node is IVariableNode) { var vnode = (IVariableNode)node; return(new Variable(vnode.VariableName)); } if (node is IGraphLiteralNode) { var vnode = (IGraphLiteralNode)node; throw ErrorBadOp("RDFToPart: on " + vnode); } if (node is IBlankNode) { var vnode = (IBlankNode)node; var atom = Atom.MakeNodeAtom(vnode); return(atom); node = triples.def.CreateUriNode(UriFactory.Create("_:" + vnode.InternalID)); } if (node is IUriNode) { var vnode = (IUriNode)node; var atom = Atom.MakeNodeAtom(vnode); return(atom); } // all the below are now Literal Nodes of some type (we divide into "strings", numbers and "strings with"^"meaning" and ILiteralNode litnode = node as ILiteralNode; if (litnode == null) { throw ErrorBadOp("Cant find the nodetype on " + node); } return(Atom.MakeNodeAtomFixme(litnode)); }
static public INode PartToRdf(Part part, RdfRules triples) { if (part is Atom) { Atom atom = ((Atom)part); var rdf = atom.AsRDFNode(); if (rdf != null) { return(rdf); } Warn("Atom.AsValuedNode returned NULL" + part); } if (part is Variable) { var definations = triples.def; return(definations.CreateVariableNode(((Variable)part).vname)); } if (part is Term) { return(TermToRdf(part, triples)); } throw ErrorBadOp("ToRDF on " + part); }
static private INode TermToRdf2OLD(Part part, RdfRules triples) { if (part is Term) { var def = triples.def; RdfRules antRules = new RdfRules(def); var subj = CreateAntecedantNode((Term)part, antRules); if (subj == null) { foreach (var ms in antRules.Subjects) { subj = subj ?? ms; } } if (subj != null) { triples.IncludeRules(antRules); return(subj); } ICollection <Triple> trips = antRules.Requirements; if (trips.Count == 0) { trips = antRules.ToTriples; if (trips.Count == 0) { Warn("No triples made from: " + part); } var gl1 = ToBracket(def, trips); return(gl1); } antRules.Requirements.Clear(); triples.IncludeRules(antRules); var gl = ToBracket(def, trips); return(gl); } throw ErrorBadOp("ToRDF on " + part); }
public void rdfImportToKB(IGraph g, string graphKBName, string query, string assertTemplate) { EnsureReaderNamespaces(g); StringWriter miniMt = new StringWriter(); var repo = FindOrCreateKB(graphKBName); RdfRules ruleDefs = new RdfRules(g); ruleDefs.prologMt = graphKBName; //Use the extension method ExecuteQuery() to make the query against the Graph try { Object results = g.ExecuteQuery(query); List <Rule> newTriples = new List <Rule>(); GetMiniMt(results, assertTemplate, graphKBName, repo.RdfStore, show, null, miniMt, ruleDefs); } catch (RdfQueryException queryEx) { //There was an error executing the query so handle it here Warn(queryEx); } string miniMtSources = miniMt.ToString(); insertKB(miniMtSources, graphKBName); }
private void GetMiniMt(object results, string assertTemplate, string graphKBName, PNode repo, bool show, ICollection <Rule> newRules, TextWriter ruleSources, RdfRules ruleDefs) { assertTemplate = assertTemplate ?? "triple($?s$,$?p$,$?o$).\n"; bool MakeRules = newRules != null && assertTemplate.Trim().EndsWith("."); var outMap = new Dictionary <string, string>(); outMap["s"] = "unknownSubject"; outMap["p"] = "unknownPredicate"; outMap["o"] = "unknownObject"; outMap["mt"] = Atom.aq(repo.Id); if (results is SparqlResultSet) { //SELECT/ASK queries give a SparqlResultSet SparqlResultSet rset = (SparqlResultSet)results; if (show) { ConsoleWriteLine("SparqlResultSet.Count = {0}", rset.Count); ConsoleWriteLine("SparqlResultSet:{0}", rset.ToString()); } foreach (SparqlResult r in rset) { //Do whatever you want with each Result if (show) { ConsoleWriteLine("SparqlResult.Count = {0}", r.Count); ConsoleWriteLine("SparqlResult:{0}", r.ToString()); } var assertIt = assertTemplate; //Do whatever you want with each Triple foreach (string vname in r.Variables) { INode value0 = r[vname]; SIProlog.checkNode(value0); //Graph into = FindGraph(baseURI.AbsoluteUri); INode value = value0.CopyWNode(repo.rdfGraph); string strVal = GraphWithDef.PlReadble(value, ruleDefs); assertIt = assertIt.Replace("$?" + vname + "$", strVal); if (show) { ConsoleWriteLine("BIND: {0} = {1}", vname, strVal); } } if (assertIt.Contains("$?s$")) { foreach (KeyValuePair <string, string> map in outMap) { assertIt = assertIt.Replace("$?" + map.Key + "$", map.Value); } } if (MakeRules) { Rule rule = ParseRule(new Tokeniser(assertIt), graphKBName); if (show) { ConsoleWriteLine("RULE_IG: {0}", rule); } newRules.Add(rule); } else { if (show) { ConsoleWriteLine("TRIPLE_IG: {0}", assertIt); } } if (ruleSources != null) { ruleSources.WriteLine(assertIt); } } } else if (results is IGraph) { //CONSTRUCT/DESCRIBE queries give a IGraph IGraph resGraph = (IGraph)results; var rset = resGraph.Triples; outMap["mt"] = "<" + resGraph.BaseUri.AbsoluteUri + ">"; if (show) { ConsoleWriteLine("IGraphResultSet.Count = {0}", rset.Count); ConsoleWriteLine("IGraphResultSet:{0}", rset.ToString()); } foreach (Triple t in rset) { var assertIt = assertTemplate; //Do whatever you want with each Triple outMap["s"] = GraphWithDef.PlReadble(t.Subject, ruleDefs); outMap["p"] = GraphWithDef.PlReadble(t.Predicate, ruleDefs); outMap["o"] = GraphWithDef.PlReadble(t.Object, ruleDefs); foreach (KeyValuePair <string, string> map in outMap) { assertIt = assertIt.Replace("$?" + map.Key + "$", map.Value); } if (MakeRules) { Rule rule = ParseRule(new Tokeniser(assertIt), graphKBName); if (show) { ConsoleWriteLine("RULE_IG: {0}", rule); } newRules.Add(rule); } else { if (show) { ConsoleWriteLine("TRIPLE_IG: {0}", assertIt); } } if (ruleSources != null) { ruleSources.WriteLine(assertIt); } } } else { //If you don't get a SparqlResutlSet or IGraph something went wrong //but didn't throw an exception so you should handle it here if (results == null) { throw ErrorBadOp("ERROR: no Results From NULL Query Object for " + graphKBName); } throw ErrorBadOp("ERROR: Cant how understand " + results.GetType() + " " + results + " to import to " + graphKBName); } }
public void pushRulesToGraph(string mt, PNode rdfGraphWithDefs, bool includeInherited) { // Possibly called by the Sparql endpoint before servicing a query // Is there anything we want to update rdfGraph with ? PNode focus = FindOrCreateKB(mt); var bingingsList = new ListOfBindings(); askQuery(ParseQuery("triple(S,P,O)", mt), mt, includeInherited, bingingsList, null); bool useTripeQuery = true; if (bingingsList == null || bingingsList.Count <= 0) { useTripeQuery = false; var triples = findVisibleKBRules(mt, new List <string>(), includeInherited); foreach (Rule rule in triples) { try { rdfGraphWithDefs.AddRuleToRDF(rule); } catch (Exception e) { Warn(e); } } return; } var rdfGraph = rdfGraphWithDefs.rdfGraph; if (!useTripeQuery) { return; } var newTriples = new RdfRules(rdfGraphWithDefs.definations); EnsureGraphPrefixes(rdfGraph); foreach (Dictionary <string, Part> bindings in bingingsList) { Part psubj = bindings["S"]; Part ppred = bindings["P"]; Part pobj = bindings["O"]; INode subj = GraphWithDef.PartToRdf(psubj, newTriples); INode pred = GraphWithDef.PartToRdf(ppred, newTriples); INode obj = GraphWithDef.PartToRdf(pobj, newTriples); if (subj is ILiteralNode) { Warn("Subj was a literal (not supported) [{0} {1} {2}]", subj, pred, obj); continue; } if (!(pred is IUriNode)) { Warn("Pred was not a uri (not supported) [{0} {1} {2}]", subj, pred, obj); continue; } //foreach (string k in bindings.Keys) //{ rdfGraphAssert(rdfGraph, MakeTriple(subj, pred, obj)); //string rdfLine = String.Format(@"<{0}> <{1}> <{2}> .", bindings["S"].ToString(), bindings["P"].ToString(), bindings["O"].ToString()); //StringParser.Parse(rdfGraph, rdfLine); // } } newTriples.RequirementsMet = true; newTriples.AssertTriples(rdfGraphWithDefs.definations, true, true); }