private static INode MakeRdfList(IRdfHandler handler, JArray list) { var rdfFirst = handler.CreateUriNode(new Uri(RdfNs + "first")); var rdfRest = handler.CreateUriNode(new Uri(RdfNs + "rest")); var rdfNil = handler.CreateUriNode(new Uri(RdfNs + "nil")); if (list == null || list.Count == 0) { return(rdfNil); } var bNodes = list.Select(x => handler.CreateBlankNode()).ToList(); for (int ix = 0; ix < list.Count; ix++) { var subject = bNodes[ix]; var obj = MakeNode(handler, list[ix]); if (obj != null) { handler.HandleTriple(new Triple(subject, rdfFirst, obj)); } var rest = (ix + 1 < list.Count) ? bNodes[ix + 1] : (INode)rdfNil; handler.HandleTriple(new Triple(subject, rdfRest, rest)); } return(bNodes[0]); }
/// <summary> /// Generates the Description for each of the Nodes to be described /// </summary> /// <param name="handler">RDF Handler</param> /// <param name="context">SPARQL Evaluation Context</param> /// <param name="nodes">Nodes to be described</param> protected override void DescribeInternal(IRdfHandler handler, SparqlEvaluationContext context, IEnumerable <INode> nodes) { //Rewrite Blank Node IDs for DESCRIBE Results Dictionary <String, INode> bnodeMapping = new Dictionary <string, INode>(); //Get Triples for this Subject foreach (INode n in nodes) { //Get Triples where the Node is the Subject foreach (Triple t in context.Data.GetTriplesWithSubject(n)) { if (!handler.HandleTriple((this.RewriteDescribeBNodes(t, bnodeMapping, handler)))) { ParserHelper.Stop(); } } //Get Triples where the Node is the Object foreach (Triple t in context.Data.GetTriplesWithObject(n)) { if (!handler.HandleTriple((this.RewriteDescribeBNodes(t, bnodeMapping, handler)))) { ParserHelper.Stop(); } } } }
/// <summary> /// Generates the Description for each of the Nodes to be described /// </summary> /// <param name="handler">RDF Handler</param> /// <param name="context">SPARQL Evaluation Context</param> /// <param name="nodes">Nodes to be described</param> protected override void DescribeInternal(IRdfHandler handler, SparqlEvaluationContext context, IEnumerable<INode> nodes) { //Rewrite Blank Node IDs for DESCRIBE Results Dictionary<String, INode> bnodeMapping = new Dictionary<string, INode>(); //Get Triples for this Subject Queue<INode> bnodes = new Queue<INode>(); HashSet<INode> expandedBNodes = new HashSet<INode>(); foreach (INode n in nodes) { //Get Triples where the Node is the Subject foreach (Triple t in context.Data.GetTriplesWithSubject(n)) { if (t.Object.NodeType == NodeType.Blank) { if (!expandedBNodes.Contains(t.Object)) bnodes.Enqueue(t.Object); } if (!handler.HandleTriple(this.RewriteDescribeBNodes(t, bnodeMapping, handler))) ParserHelper.Stop(); } //Get Triples where the Node is the Object foreach (Triple t in context.Data.GetTriplesWithObject(n)) { if (t.Subject.NodeType == NodeType.Blank) { if (!expandedBNodes.Contains(t.Subject)) bnodes.Enqueue(t.Subject); } if (!handler.HandleTriple(this.RewriteDescribeBNodes(t, bnodeMapping, handler))) ParserHelper.Stop(); } //Compute the Blank Node Closure for this Subject while (bnodes.Count > 0) { INode bsubj = bnodes.Dequeue(); if (expandedBNodes.Contains(bsubj)) continue; expandedBNodes.Add(bsubj); foreach (Triple t2 in context.Data.GetTriplesWithSubject(bsubj)) { if (t2.Object.NodeType == NodeType.Blank) { if (!expandedBNodes.Contains(t2.Object)) bnodes.Enqueue(t2.Object); } if (!handler.HandleTriple(this.RewriteDescribeBNodes(t2, bnodeMapping, handler))) ParserHelper.Stop(); } foreach (Triple t2 in context.Data.GetTriplesWithObject(bsubj)) { if (t2.Subject.NodeType == NodeType.Blank) { if (!expandedBNodes.Contains(t2.Subject)) bnodes.Enqueue(t2.Subject); } if (!handler.HandleTriple(this.RewriteDescribeBNodes(t2, bnodeMapping, handler))) ParserHelper.Stop(); } } } }
/// <summary> /// Generates the Description for each of the Nodes to be described. /// </summary> /// <param name="handler">RDF Handler.</param> /// <param name="context">SPARQL Evaluation Context.</param> /// <param name="nodes">Nodes to be described.</param> protected override void DescribeInternal(IRdfHandler handler, SparqlEvaluationContext context, IEnumerable <INode> nodes) { // Rewrite Blank Node IDs for DESCRIBE Results Dictionary <String, INode> bnodeMapping = new Dictionary <string, INode>(); // Get Triples for this Subject Queue <INode> bnodes = new Queue <INode>(); HashSet <INode> expandedBNodes = new HashSet <INode>(); foreach (INode n in nodes) { // Get Triples where the Node is the Subject foreach (Triple t in context.Data.GetTriplesWithSubject(n).ToList()) { if (t.Object.NodeType == NodeType.Blank) { if (!expandedBNodes.Contains(t.Object)) { bnodes.Enqueue(t.Object); } } if (!handler.HandleTriple(RewriteDescribeBNodes(t, bnodeMapping, handler))) { ParserHelper.Stop(); } } // Compute the Blank Node Closure for this Subject while (bnodes.Count > 0) { INode bsubj = bnodes.Dequeue(); if (expandedBNodes.Contains(bsubj)) { continue; } expandedBNodes.Add(bsubj); foreach (Triple t2 in context.Data.GetTriplesWithSubject(bsubj).ToList()) { if (t2.Object.NodeType == NodeType.Blank) { if (!expandedBNodes.Contains(t2.Object)) { bnodes.Enqueue(t2.Object); } } if (!handler.HandleTriple(RewriteDescribeBNodes(t2, bnodeMapping, handler))) { ParserHelper.Stop(); } } } } }
private void TryParseTriple(XmlReader reader, IRdfHandler handler, Uri graphUri) { //Verify Node Name if (!reader.Name.Equals("triple")) { throw Error("Unexpected Element <" + reader.Name + "> encountered, only an optional <id>/<uri> element followed by zero/more <triple> elements are permitted within a <graph> element", reader); } //Parse XML Nodes into RDF Nodes INode subj, pred, obj; subj = this.TryParseNode(reader, handler, TripleSegment.Subject); pred = this.TryParseNode(reader, handler, TripleSegment.Predicate); obj = this.TryParseNode(reader, handler, TripleSegment.Object); if (reader.NodeType != XmlNodeType.EndElement) { throw Error("Unexpected element type " + reader.NodeType.ToString() + " encountered, expected the </triple> element", reader); } if (!reader.Name.Equals("triple")) { throw Error("Unexpected </" + reader.Name + "> encountered, expected a </triple> element", reader); } //Assert the resulting Triple if (!handler.HandleTriple(new Triple(subj, pred, obj, graphUri))) { ParserHelper.Stop(); } }
private void TryParseTriple(IRdfHandler handler, IToken s, IToken p, IToken o, Uri graphUri) { INode subj, pred, obj; switch (s.TokenType) { case Token.BLANKNODEWITHID: subj = handler.CreateBlankNode(s.Value.Substring(2)); break; case Token.URI: subj = ParserHelper.TryResolveUri(handler, s); break; default: throw ParserHelper.Error("Unexpected Token '" + s.GetType().ToString() + "' encountered, expected a Blank Node/URI as the Subject of a Triple", s); } switch (p.TokenType) { case Token.URI: pred = ParserHelper.TryResolveUri(handler, p); break; default: throw ParserHelper.Error("Unexpected Token '" + p.GetType().ToString() + "' encountered, expected a URI as the Predicate of a Triple", p); } switch (o.TokenType) { case Token.BLANKNODEWITHID: obj = handler.CreateBlankNode(o.Value.Substring(2)); break; case Token.LITERAL: obj = handler.CreateLiteralNode(o.Value); break; case Token.LITERALWITHDT: String dtUri = ((LiteralWithDataTypeToken)o).DataType; obj = handler.CreateLiteralNode(o.Value, new Uri(dtUri.Substring(1, dtUri.Length - 2))); break; case Token.LITERALWITHLANG: obj = handler.CreateLiteralNode(o.Value, ((LiteralWithLanguageSpecifierToken)o).Language); break; case Token.URI: obj = ParserHelper.TryResolveUri(handler, o); break; default: throw ParserHelper.Error("Unexpected Token '" + o.GetType().ToString() + "' encountered, expected a Blank Node/Literal/URI as the Object of a Triple", o); } if (!handler.HandleTriple(new Triple(subj, pred, obj, graphUri))) { ParserHelper.Stop(); } }
private static void HandleTriple(IRdfHandler handler, string subject, string predicate, string obj, string datatype, bool isLiteral) { INode subjectNode = (subject.StartsWith("_") ? (INode)handler.CreateBlankNode(subject.Substring(2)) : handler.CreateUriNode(new Uri(subject))); INode predicateNode = handler.CreateUriNode(new Uri(predicate)); INode objNode; if (isLiteral) { if (datatype == "http://www.w3.org/2001/XMLSchema#boolean") { obj = obj.ToLowerInvariant(); } objNode = (datatype == null ? handler.CreateLiteralNode(obj) : handler.CreateLiteralNode(obj, new Uri(datatype))); } else { objNode = (obj.StartsWith("_") ? (INode)handler.CreateBlankNode(obj.Substring(2)) : handler.CreateUriNode(new Uri(obj))); } if (!handler.HandleTriple(new Triple(subjectNode, predicateNode, objNode))) { throw new InvalidOperationException(String.Format("Could not add triple {0} {1} {2} .", subjectNode, predicateNode, objNode)); } }
/// <summary> /// Handles Triples by passing them to the inner handler and cancelling handling if it has been requested. /// </summary> /// <param name="t">Triple.</param> /// <returns></returns> protected override bool HandleTripleInternal(Triple t) { if (_cancelled) { return(false); } return(_handler.HandleTriple(t)); }
/// <summary> /// Handles triples by stripping explicit xsd:string datatype on object literals before delegating to inner handler /// </summary> protected override bool HandleTripleInternal(Triple t) { if (t.Object is ILiteralNode literal && literal.DataType == DataTypeString) { t = new Triple(t.Subject, t.Predicate, CreateLiteralNode(literal.Value)); } return(_handler.HandleTriple(t)); }
/// <summary> /// Handles triples by stripping explicit xsd:string datatype on object literals before delegating to inner handler. /// </summary> protected override bool HandleTripleInternal(Triple t) { if (t.Object is ILiteralNode literal && EqualityHelper.AreUrisEqual(literal.DataType, DataTypeString)) { t = new Triple(t.Subject, t.Predicate, t.Graph.CreateLiteralNode(literal.Value)); } return(_handler.HandleTriple(t)); }
/// <summary> /// Generates the Description for each of the Nodes to be described /// </summary> /// <param name="handler">RDF Handler</param> /// <param name="context">SPARQL Evaluation Context</param> /// <param name="nodes">Nodes to be described</param> protected override void DescribeInternal(IRdfHandler handler, SparqlEvaluationContext context, IEnumerable<INode> nodes) { //Rewrite Blank Node IDs for DESCRIBE Results Dictionary<String, INode> bnodeMapping = new Dictionary<string, INode>(); //Get Triples for this Subject foreach (INode n in nodes) { //Get Triples where the Node is the Subject foreach (Triple t in context.Data.GetTriplesWithSubject(n)) { if (!handler.HandleTriple((this.RewriteDescribeBNodes(t, bnodeMapping, handler)))) ParserHelper.Stop(); } //Get Triples where the Node is the Object foreach (Triple t in context.Data.GetTriplesWithObject(n)) { if (!handler.HandleTriple((this.RewriteDescribeBNodes(t, bnodeMapping, handler)))) ParserHelper.Stop(); } } }
/// <summary> /// Handles triples by stripping explicit xsd:string datatype on object literals before delegating to inner handler /// </summary> protected override bool HandleTripleInternal(Triple t) { if (t.Object is ILiteralNode literalNode && literalNode.AsValuedNode() is DateTimeNode dateTimeNode) { t = new Triple( t.Subject, t.Predicate, t.Graph.CreateLiteralNode(dateTimeNode.AsDateTimeOffset().ToString("u"))); } return(_handler.HandleTriple(t)); }
/// <summary> /// Handles a Triple by passing it to the Inner Handler only if the Offset has been passed and the Limit has yet to be reached. /// </summary> /// <param name="t">Triple.</param> /// <returns></returns> /// <remarks> /// Terminates handling immediately upon the reaching of the limit. /// </remarks> protected override bool HandleTripleInternal(Triple t) { // If the Limit is zero stop parsing immediately if (_limit == 0) { return(false); } _counter++; if (_limit > 0) { // Limit greater than zero means get a maximum of limit triples after the offset if (_counter > _offset && _counter <= _limit + _offset) { return(_handler.HandleTriple(t)); } else if (_counter > _limit + _offset) { // Stop parsing when we've reached the limit return(false); } else { return(true); } } else { // Limit less than zero means get all triples after the offset if (_counter > _offset) { return(_handler.HandleTriple(t)); } else { return(true); } } }
protected override void DescribeInternal(IRdfHandler handler, SparqlEvaluationContext context, IEnumerable <INode> nodes) { var bnodeMapping = new Dictionary <string, INode>(); var map = new Dictionary <INode, INode>(); var outstanding = new Queue <INode>(); var done = new HashSet <INode>(); void process(INode originalSubject, INode mappedSubject = null) { foreach (var t in context.Data.GetTriplesWithSubject(originalSubject)) { var @object = t.Object; if (@object.NodeType == NodeType.Blank && !done.Contains(@object)) { if (Vocabulary.PredicatesToExpandInReport.Contains(t.Predicate)) { @object = @object.Graph.CreateBlankNode(); map.Add(@object, t.Object); outstanding.Enqueue(@object); } } if (!handler.HandleTriple(RewriteDescribeBNodes(new Triple(mappedSubject ?? originalSubject, t.Predicate, @object, t.Graph), bnodeMapping, handler))) { ParserHelper.Stop(); } } } foreach (var node in nodes) { process(node); while (outstanding.Any()) { var mappedSubject = outstanding.Dequeue(); if (done.Add(mappedSubject)) { if (map.TryGetValue(mappedSubject, out var originalSubject)) { process(originalSubject, mappedSubject); } else { process(mappedSubject); } } } } }
/// <summary> /// Generates the Description for each of the Nodes to be described /// </summary> /// <param name="handler">RDF Handler</param> /// <param name="context">SPARQL Evaluation Context</param> /// <param name="nodes">Nodes to be described</param> protected override void DescribeInternal(IRdfHandler handler, SparqlEvaluationContext context, IEnumerable<INode> nodes) { //Rewrite Blank Node IDs for DESCRIBE Results Dictionary<String, INode> bnodeMapping = new Dictionary<string, INode>(); foreach (INode n in nodes) { if (n.NodeType == NodeType.Uri) { IGraph g = context.Data[((IUriNode)n).Uri]; foreach (Triple t in g.Triples) { if (!handler.HandleTriple(this.RewriteDescribeBNodes(t, bnodeMapping, handler))) ParserHelper.Stop(); } } } }
/// <summary> /// Applies the triples of a Graph to an RDF Handler. /// </summary> /// <param name="handler">RDF Handler.</param> /// <param name="g">Graph.</param> public static void Apply(this IRdfHandler handler, IGraph g) { try { handler.StartRdf(); if (g != null) { // Handle the Base URI if present if (g.BaseUri != null) { if (!handler.HandleBaseUri(g.BaseUri)) { ParserHelper.Stop(); } } // Handle any namespaces foreach (String prefix in g.NamespaceMap.Prefixes) { if (!handler.HandleNamespace(prefix, g.NamespaceMap.GetNamespaceUri(prefix))) { ParserHelper.Stop(); } } // Finally handle triples foreach (Triple t in g.Triples) { if (!handler.HandleTriple(t)) { ParserHelper.Stop(); } } } handler.EndRdf(true); } catch (RdfParsingTerminatedException) { handler.EndRdf(true); } catch { handler.EndRdf(false); throw; } }
/// <summary> /// Creates and handles a triple /// </summary> /// <param name="handler">Handler</param> /// <param name="subject">Subject</param> /// <param name="predicate">Predicate</param> /// <param name="obj">Object</param> /// <param name="datatype">Object Datatype</param> /// <param name="isLiteral">isLiteral Object</param> /// <returns>True if parsing should continue, false otherwise</returns> bool HandleTriple(IRdfHandler handler, string subject, string predicate, string obj, string datatype, bool isLiteral) { INode subjectNode; if (subject.StartsWith("_")) { string nodeId = subject.Substring(subject.IndexOf(":") + 1); subjectNode = handler.CreateBlankNode(nodeId); } else { subjectNode = handler.CreateUriNode(new Uri(subject)); } INode predicateNode = handler.CreateUriNode(new Uri(predicate)); INode objNode; if (isLiteral) { if (datatype == "http://www.w3.org/2001/XMLSchema#boolean") { // sometimes newtonsoft.json appears to return boolean as string True and dotNetRdf doesn't appear to recognize that obj = ((string)obj).ToLowerInvariant(); } objNode = (datatype == null) ? handler.CreateLiteralNode((string)obj) : handler.CreateLiteralNode((string)obj, new Uri(datatype)); } else { if (obj.StartsWith("_")) { string nodeId = obj.Substring(obj.IndexOf(":") + 1); objNode = handler.CreateBlankNode(nodeId); } else { objNode = handler.CreateUriNode(new Uri(obj)); } } return(handler.HandleTriple(new Triple(subjectNode, predicateNode, objNode))); }
/// <summary> /// Generates the Description for each of the Nodes to be described. /// </summary> /// <param name="handler">RDF Handler.</param> /// <param name="context">SPARQL Evaluation Context.</param> /// <param name="nodes">Nodes to be described.</param> protected override void DescribeInternal(IRdfHandler handler, SparqlEvaluationContext context, IEnumerable <INode> nodes) { // Rewrite Blank Node IDs for DESCRIBE Results Dictionary <String, INode> bnodeMapping = new Dictionary <string, INode>(); foreach (INode n in nodes) { if (n.NodeType == NodeType.Uri) { IGraph g = context.Data[((IUriNode)n).Uri]; foreach (Triple t in g.Triples.ToList()) { if (!handler.HandleTriple(RewriteDescribeBNodes(t, bnodeMapping, handler))) { ParserHelper.Stop(); } } } } }
/// <summary> /// Applies the triples to an RDF Handler. /// </summary> /// <param name="handler">RDF Handler.</param> /// <param name="ts">Triples.</param> public static void Apply(this IRdfHandler handler, IEnumerable <Triple> ts) { try { handler.StartRdf(); foreach (Triple t in ts) { if (!handler.HandleTriple(t)) { ParserHelper.Stop(); } } handler.EndRdf(false); } catch (RdfParsingTerminatedException) { handler.EndRdf(true); } catch { handler.EndRdf(false); throw; } }
/// <summary> /// Creates and handles a triple /// </summary> /// <param name="handler">Handler</param> /// <param name="subject">Subject</param> /// <param name="predicate">Predicate</param> /// <param name="obj">Object</param> /// <param name="datatype">Object Datatype</param> /// <returns>True if parsing should continue, false otherwise</returns> bool HandleTriple(IRdfHandler handler, string subject, string predicate, string obj, string datatype, bool isLiteral) { INode subjectNode; if (subject.StartsWith("_")) { string nodeId = subject.Substring(subject.IndexOf(":") + 1); subjectNode = handler.CreateBlankNode(nodeId); } else { subjectNode = handler.CreateUriNode(new Uri(subject)); } INode predicateNode = handler.CreateUriNode(new Uri(predicate)); INode objNode; if (isLiteral) { objNode = (datatype == null) ? handler.CreateLiteralNode((string)obj) : handler.CreateLiteralNode((string)obj, new Uri(datatype)); } else { if (obj.StartsWith("_")) { string nodeId = obj.Substring(obj.IndexOf(":") + 1); objNode = handler.CreateBlankNode(nodeId); } else { objNode = handler.CreateUriNode(new Uri(obj)); } } return handler.HandleTriple(new Triple(subjectNode, predicateNode, objNode)); }
/// <summary> /// Creates and handles a triple /// </summary> /// <param name="handler">Handler</param> /// <param name="subject">Subject</param> /// <param name="predicate">Predicate</param> /// <param name="obj">Object</param> /// <param name="datatype">Object Datatype</param> /// <param name="isLiteral">isLiteral Object</param> /// <returns>True if parsing should continue, false otherwise</returns> bool HandleTriple(IRdfHandler handler, string subject, string predicate, string obj, string datatype, bool isLiteral) { INode subjectNode; if (subject.StartsWith("_")) { string nodeId = subject.Substring(subject.IndexOf(":") + 1); subjectNode = handler.CreateBlankNode(nodeId); } else { subjectNode = handler.CreateUriNode(new Uri(subject)); } INode predicateNode = handler.CreateUriNode(new Uri(predicate)); INode objNode; if (isLiteral) { if (datatype == "http://www.w3.org/2001/XMLSchema#boolean") { // sometimes newtonsoft.json appears to return boolean as string True and dotNetRdf doesn't appear to recognize that obj = ((string)obj).ToLowerInvariant(); } objNode = (datatype == null) ? handler.CreateLiteralNode((string)obj) : handler.CreateLiteralNode((string)obj, new Uri(datatype)); } else { if (obj.StartsWith("_")) { string nodeId = obj.Substring(obj.IndexOf(":") + 1); objNode = handler.CreateBlankNode(nodeId); } else { objNode = handler.CreateUriNode(new Uri(obj)); } } return handler.HandleTriple(new Triple(subjectNode, predicateNode, objNode)); }
INode IXmlNodeProcessor <INode> .ProcessDocument <TProvider>(TProvider provider, ContentIterator <INode> content) { var baseNode = MakeUriNode(provider.BaseUri); rdf.HandleTriple(baseNode, value, MakeList(content(baseNode)) ?? nil); return(baseNode); }
/// <summary> /// Processes a SPARQL Query sending the results to a RDF/SPARQL Results handler as appropriate /// </summary> /// <param name="rdfHandler">RDF Handler</param> /// <param name="resultsHandler">Results Handler</param> /// <param name="query">SPARQL Query</param> public void ProcessQuery(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, SparqlQuery query) { //Do Handler null checks before evaluating the query if (query == null) { throw new ArgumentNullException("query", "Cannot evaluate a null query"); } if (rdfHandler == null && (query.QueryType == SparqlQueryType.Construct || query.QueryType == SparqlQueryType.Describe || query.QueryType == SparqlQueryType.DescribeAll)) { throw new ArgumentNullException("rdfHandler", "Cannot use a null RDF Handler when the Query is a CONSTRUCT/DESCRIBE"); } if (resultsHandler == null && (query.QueryType == SparqlQueryType.Ask || SparqlSpecsHelper.IsSelectQuery(query.QueryType))) { throw new ArgumentNullException("resultsHandler", "Cannot use a null resultsHandler when the Query is an ASK/SELECT"); } //Handle the Thread Safety of the Query Evaluation #if !NO_RWLOCK ReaderWriterLockSlim currLock = (this._dataset is IThreadSafeDataset) ? ((IThreadSafeDataset)this._dataset).Lock : this._lock; try { currLock.EnterReadLock(); #endif //Reset Query Timers query.QueryExecutionTime = null; bool datasetOk = false, defGraphOk = false; try { //Set up the Default and Active Graphs if (query.DefaultGraphs.Any()) { //Call HasGraph() on each Default Graph but ignore the results, we just do this //in case a dataset has any kind of load on demand behaviour foreach (Uri defGraphUri in query.DefaultGraphs) { this._dataset.HasGraph(defGraphUri); } this._dataset.SetDefaultGraph(query.DefaultGraphs); defGraphOk = true; } else if (query.NamedGraphs.Any()) { //No FROM Clauses but one/more FROM NAMED means the Default Graph is the empty graph this._dataset.SetDefaultGraph(Enumerable.Empty <Uri>()); } this._dataset.SetActiveGraph(this._dataset.DefaultGraphUris); datasetOk = true; //Convert to Algebra and execute the Query SparqlEvaluationContext context = this.GetContext(query); BaseMultiset result; try { context.StartExecution(); ISparqlAlgebra algebra = query.ToAlgebra(); result = context.Evaluate(algebra); context.EndExecution(); query.QueryExecutionTime = new TimeSpan(context.QueryTimeTicks); } catch (RdfQueryException) { context.EndExecution(); query.QueryExecutionTime = new TimeSpan(context.QueryTimeTicks); throw; } catch { context.EndExecution(); query.QueryExecutionTime = new TimeSpan(context.QueryTimeTicks); throw; } //Return the Results switch (query.QueryType) { case SparqlQueryType.Ask: case SparqlQueryType.Select: case SparqlQueryType.SelectAll: case SparqlQueryType.SelectAllDistinct: case SparqlQueryType.SelectAllReduced: case SparqlQueryType.SelectDistinct: case SparqlQueryType.SelectReduced: //For SELECT and ASK can populate a Result Set directly from the Evaluation Context //return new SparqlResultSet(context); resultsHandler.Apply(context); break; case SparqlQueryType.Construct: //Create a new Empty Graph for the Results try { rdfHandler.StartRdf(); foreach (String prefix in query.NamespaceMap.Prefixes) { if (!rdfHandler.HandleNamespace(prefix, query.NamespaceMap.GetNamespaceUri(prefix))) { ParserHelper.Stop(); } } //Construct the Triples for each Solution foreach (ISet s in context.OutputMultiset.Sets) { //List<Triple> constructedTriples = new List<Triple>(); try { ConstructContext constructContext = new ConstructContext(rdfHandler, s, false); foreach (IConstructTriplePattern p in query.ConstructTemplate.TriplePatterns.OfType <IConstructTriplePattern>()) { try { if (!rdfHandler.HandleTriple(p.Construct(constructContext))) { ParserHelper.Stop(); } //constructedTriples.Add(((IConstructTriplePattern)p).Construct(constructContext)); } catch (RdfQueryException) { //If we get an error here then we could not construct a specific triple //so we continue anyway } } } catch (RdfQueryException) { //If we get an error here this means we couldn't construct for this solution so the //entire solution is discarded continue; } //h.Assert(constructedTriples); } rdfHandler.EndRdf(true); } catch (RdfParsingTerminatedException) { rdfHandler.EndRdf(true); } catch { rdfHandler.EndRdf(false); throw; } break; case SparqlQueryType.Describe: case SparqlQueryType.DescribeAll: //For DESCRIBE we retrieve the Describe algorithm and apply it ISparqlDescribe describer = query.Describer; describer.Describe(rdfHandler, context); break; default: throw new RdfQueryException("Unknown query types cannot be processed by Leviathan"); } } finally { if (defGraphOk) { this._dataset.ResetDefaultGraph(); } if (datasetOk) { this._dataset.ResetActiveGraph(); } } #if !NO_RWLOCK } finally { currLock.ExitReadLock(); } #endif }
private void TryParseTriple(XmlReader reader, IRdfHandler handler, Uri graphUri) { //Verify Node Name if (!reader.Name.Equals("triple")) { throw Error("Unexpected Element <" + reader.Name + "> encountered, only an optional <id>/<uri> element followed by zero/more <triple> elements are permitted within a <graph> element", reader); } //Parse XML Nodes into RDF Nodes INode subj, pred, obj; subj = this.TryParseNode(reader, handler, TripleSegment.Subject); pred = this.TryParseNode(reader, handler, TripleSegment.Predicate); obj = this.TryParseNode(reader, handler, TripleSegment.Object); if (reader.NodeType != XmlNodeType.EndElement) throw Error("Unexpected element type " + reader.NodeType.ToString() + " encountered, expected the </triple> element", reader); if (!reader.Name.Equals("triple")) throw Error("Unexpected </" + reader.Name + "> encountered, expected a </triple> element", reader); //Assert the resulting Triple if (!handler.HandleTriple(new Triple(subj, pred, obj, graphUri))) throw ParserHelper.Stop(); }
/// <summary> /// Makes a Query where the expected result is a Graph i.e. a CONSTRUCT or DESCRIBE query /// </summary> /// <param name="handler">RDF Handler to process the results</param> /// <param name="sparqlQuery">SPARQL Query</param> public override void QueryWithResultGraph(IRdfHandler handler, string sparqlQuery) { //If no endpoints do nothing if (this._endpoints.Count == 0) { return; } //Fire off all the Asychronous Requests List <AsyncQueryWithResultGraph> asyncCalls = new List <AsyncQueryWithResultGraph>(); List <IAsyncResult> asyncResults = new List <IAsyncResult>(); int count = 0; foreach (SparqlRemoteEndpoint endpoint in this._endpoints) { //Limit the number of simultaneous requests we make to the user defined level (default 4) //We do this limiting check before trying to issue a request so that when the last request //is issued we'll always drop out of the loop and move onto our WaitAll() while (count >= this._maxSimultaneousRequests) { //First check that the count of active requests is accurate int active = asyncResults.Count(r => !r.IsCompleted); if (active < count) { //Some of the requests have already completed so we don't need to wait count = active; break; } else if (active > count) { //There are more active requests then we thought count = active; } //While the number of requests is at/above the maximum we'll wait for any of the requests to finish //Then we can decrement the count and if this drops back below our maximum then we'll go back into the //main loop and fire off our next request WaitHandle.WaitAny(asyncResults.Select(r => r.AsyncWaitHandle).ToArray()); count--; } //Make an asynchronous query to the next endpoint AsyncQueryWithResultGraph d = new AsyncQueryWithResultGraph(endpoint.QueryWithResultGraph); asyncCalls.Add(d); IAsyncResult asyncResult = d.BeginInvoke(sparqlQuery, null, null); asyncResults.Add(asyncResult); count++; } //Wait for all our requests to finish int waitTimeout = (base.Timeout > 0) ? base.Timeout : System.Threading.Timeout.Infinite; WaitHandle.WaitAll(asyncResults.Select(r => r.AsyncWaitHandle).ToArray(), waitTimeout); //Check for and handle timeouts if (!this._ignoreFailedRequests && !asyncResults.All(r => r.IsCompleted)) { for (int i = 0; i < asyncCalls.Count; i++) { try { asyncCalls[i].EndInvoke(asyncResults[i]); } catch { //Exceptions don't matter as we're just ensuring all the EndInvoke() calls are made } } throw new RdfQueryTimeoutException("Federated Querying failed due to one/more endpoints failing to return results within the Timeout specified which is currently " + (base.Timeout / 1000) + " seconds"); } //Now merge all the results together HashSet <String> varsSeen = new HashSet <string>(); bool cont = true; for (int i = 0; i < asyncCalls.Count; i++) { //Retrieve the result for this call AsyncQueryWithResultGraph call = asyncCalls[i]; IGraph g; try { g = call.EndInvoke(asyncResults[i]); } catch (Exception ex) { if (!this._ignoreFailedRequests) { //Clean up in the event of an error for (int j = i + 1; j < asyncCalls.Count; j++) { try { asyncCalls[j].EndInvoke(asyncResults[j]); } catch { //Exceptions don't matter as we're just ensuring all the EndInvoke() calls are made } } //If a single request fails then the entire query fails throw new RdfQueryException("Federated Querying failed due to the query against the endpoint '" + this._endpoints[i] + "' failing", ex); } else { //If we're ignoring failed requests we continue here continue; } } //Merge the result into the final results //If the handler has previously told us to stop we skip this step if (cont) { handler.StartRdf(); foreach (Triple t in g.Triples) { cont = handler.HandleTriple(t); //Stop if the Handler tells us to if (!cont) { break; } } handler.EndRdf(true); } } }
/// <inheritdoc/> public void Load(IRdfHandler handler, TextReader input) { handler.StartRdf(); var rdfTypeNode = handler.CreateUriNode(new Uri(RdfNs + "type")); try { JToken element; using (var reader = new JsonTextReader(input) { DateParseHandling = DateParseHandling.None }) { element = JToken.ReadFrom(reader); } var expandedElement = JsonLdProcessor.Expand(element, ParserOptions); var nodeMap = JsonLdProcessor.GenerateNodeMap(expandedElement); foreach (var p in nodeMap.Properties()) { var graphName = p.Name; var graph = p.Value as JObject; if (graph == null) { continue; } Uri graphIri; if (graphName == "@default") { graphIri = null; } else { if (!Uri.TryCreate(graphName, UriKind.Absolute, out graphIri)) { continue; } } foreach (var gp in graph.Properties()) { var subject = gp.Name; var node = gp.Value as JObject; INode subjectNode; if (IsBlankNodeIdentifier(subject)) { subjectNode = handler.CreateBlankNode(subject.Substring(2)); } else { Uri subjectIri; if (!Uri.TryCreate(subject, UriKind.Absolute, out subjectIri)) { continue; } subjectNode = handler.CreateUriNode(subjectIri); } foreach (var np in node.Properties()) { var property = np.Name; var values = np.Value as JArray; if (property.Equals("@type")) { foreach (var type in values) { var typeNode = MakeNode(handler, type); handler.HandleTriple(new Triple(subjectNode, rdfTypeNode, typeNode, graphIri)); } } else if (JsonLdProcessor.IsKeyword(property)) { continue; } else if (JsonLdProcessor.IsBlankNodeIdentifier(property) && !ParserOptions.ProduceGeneralizedRdf) { continue; } else if (JsonLdProcessor.IsRelativeIri(property)) { continue; } else { foreach (var item in values) { var predicateNode = MakeNode(handler, property); var objectNode = MakeNode(handler, item); if (objectNode != null) { handler.HandleTriple(new Triple(subjectNode, predicateNode, objectNode, graphIri)); } } } } } } } catch (Exception) { handler.EndRdf(false); throw; } handler.EndRdf(true); }
/// <summary> /// Handles a Triple /// </summary> /// <param name="t">Triple</param> /// <returns></returns> protected override bool HandleTripleInternal(Triple t) { return(_handler.HandleTriple(t)); }
/// <summary> /// Generates the Description for each of the Nodes to be described /// </summary> /// <param name="handler">RDF Handler</param> /// <param name="context">SPARQL Evaluation Context</param> /// <param name="nodes">Nodes to be described</param> protected override void DescribeInternal(IRdfHandler handler, SparqlEvaluationContext context, IEnumerable <INode> nodes) { //Rewrite Blank Node IDs for DESCRIBE Results Dictionary <String, INode> bnodeMapping = new Dictionary <string, INode>(); //Get Triples for this Subject Queue <INode> bnodes = new Queue <INode>(); HashSet <INode> expandedBNodes = new HashSet <INode>(); foreach (INode n in nodes) { foreach (Triple t in context.Data.GetTriplesWithSubject(n)) { if (t.Object.NodeType == NodeType.Blank) { if (!expandedBNodes.Contains(t.Object)) { bnodes.Enqueue(t.Object); } } if (!handler.HandleTriple((this.RewriteDescribeBNodes(t, bnodeMapping, handler)))) { ParserHelper.Stop(); } } if (n.NodeType == NodeType.Blank) { foreach (Triple t in context.Data.GetTriplesWithPredicate(n)) { if (t.Subject.NodeType == NodeType.Blank) { if (!expandedBNodes.Contains(t.Subject)) { bnodes.Enqueue(t.Subject); } } if (t.Object.NodeType == NodeType.Blank) { if (!expandedBNodes.Contains(t.Object)) { bnodes.Enqueue(t.Object); } } if (!handler.HandleTriple((this.RewriteDescribeBNodes(t, bnodeMapping, handler)))) { ParserHelper.Stop(); } } } foreach (Triple t in context.Data.GetTriplesWithObject(n)) { if (t.Subject.NodeType == NodeType.Blank) { if (!expandedBNodes.Contains(t.Object)) { bnodes.Enqueue(t.Subject); } } if (!handler.HandleTriple((this.RewriteDescribeBNodes(t, bnodeMapping, handler)))) { ParserHelper.Stop(); } } } //Expand BNodes while (bnodes.Count > 0) { INode n = bnodes.Dequeue(); if (expandedBNodes.Contains(n)) { continue; } expandedBNodes.Add(n); foreach (Triple t in context.Data.GetTriplesWithSubject(n)) { if (t.Object.NodeType == NodeType.Blank) { if (!expandedBNodes.Contains(t.Object)) { bnodes.Enqueue(t.Object); } } if (!handler.HandleTriple((this.RewriteDescribeBNodes(t, bnodeMapping, handler)))) { ParserHelper.Stop(); } } if (n.NodeType == NodeType.Blank) { foreach (Triple t in context.Data.GetTriplesWithPredicate(n)) { if (t.Subject.NodeType == NodeType.Blank) { if (!expandedBNodes.Contains(t.Subject)) { bnodes.Enqueue(t.Subject); } } if (t.Object.NodeType == NodeType.Blank) { if (!expandedBNodes.Contains(t.Object)) { bnodes.Enqueue(t.Object); } } if (!handler.HandleTriple((this.RewriteDescribeBNodes(t, bnodeMapping, handler)))) { ParserHelper.Stop(); } } } foreach (Triple t in context.Data.GetTriplesWithObject(n)) { if (t.Subject.NodeType == NodeType.Blank) { if (!expandedBNodes.Contains(t.Object)) { bnodes.Enqueue(t.Subject); } } if (!handler.HandleTriple((this.RewriteDescribeBNodes(t, bnodeMapping, handler)))) { ParserHelper.Stop(); } } } }
/// <summary> /// Makes a Query where the expected result is a Graph i.e. a CONSTRUCT or DESCRIBE query /// </summary> /// <param name="handler">RDF Handler to process the results</param> /// <param name="sparqlQuery">SPARQL Query</param> public override void QueryWithResultGraph(IRdfHandler handler, string sparqlQuery) { //If no endpoints do nothing if (this._endpoints.Count == 0) return; //Fire off all the Asychronous Requests List<AsyncQueryWithResultGraph> asyncCalls = new List<AsyncQueryWithResultGraph>(); List<IAsyncResult> asyncResults = new List<IAsyncResult>(); int count = 0; foreach (SparqlRemoteEndpoint endpoint in this._endpoints) { //Limit the number of simultaneous requests we make to the user defined level (default 4) //We do this limiting check before trying to issue a request so that when the last request //is issued we'll always drop out of the loop and move onto our WaitAll() while (count >= this._maxSimultaneousRequests) { //First check that the count of active requests is accurate int active = asyncResults.Count(r => !r.IsCompleted); if (active < count) { //Some of the requests have already completed so we don't need to wait count = active; break; } else if (active > count) { //There are more active requests then we thought count = active; } //While the number of requests is at/above the maximum we'll wait for any of the requests to finish //Then we can decrement the count and if this drops back below our maximum then we'll go back into the //main loop and fire off our next request WaitHandle.WaitAny(asyncResults.Select(r => r.AsyncWaitHandle).ToArray()); count--; } //Make an asynchronous query to the next endpoint AsyncQueryWithResultGraph d = new AsyncQueryWithResultGraph(endpoint.QueryWithResultGraph); asyncCalls.Add(d); IAsyncResult asyncResult = d.BeginInvoke(sparqlQuery, null, null); asyncResults.Add(asyncResult); count++; } //Wait for all our requests to finish int waitTimeout = (base.Timeout > 0) ? base.Timeout : System.Threading.Timeout.Infinite; WaitHandle.WaitAll(asyncResults.Select(r => r.AsyncWaitHandle).ToArray(), waitTimeout); //Check for and handle timeouts if (!this._ignoreFailedRequests && !asyncResults.All(r => r.IsCompleted)) { for (int i = 0; i < asyncCalls.Count; i++) { try { asyncCalls[i].EndInvoke(asyncResults[i]); } catch { //Exceptions don't matter as we're just ensuring all the EndInvoke() calls are made } } throw new RdfQueryTimeoutException("Federated Querying failed due to one/more endpoints failing to return results within the Timeout specified which is currently " + (base.Timeout / 1000) + " seconds"); } //Now merge all the results together HashSet<String> varsSeen = new HashSet<string>(); bool cont = true; for (int i = 0; i < asyncCalls.Count; i++) { //Retrieve the result for this call AsyncQueryWithResultGraph call = asyncCalls[i]; IGraph g; try { g = call.EndInvoke(asyncResults[i]); } catch (Exception ex) { if (!this._ignoreFailedRequests) { //Clean up in the event of an error for (int j = i + 1; j < asyncCalls.Count; j++) { try { asyncCalls[j].EndInvoke(asyncResults[j]); } catch { //Exceptions don't matter as we're just ensuring all the EndInvoke() calls are made } } //If a single request fails then the entire query fails throw new RdfQueryException("Federated Querying failed due to the query against the endpoint '" + this._endpoints[i] + "' failing", ex); } else { //If we're ignoring failed requests we continue here continue; } } //Merge the result into the final results //If the handler has previously told us to stop we skip this step if (cont) { handler.StartRdf(); foreach (Triple t in g.Triples) { cont = handler.HandleTriple(t); //Stop if the Handler tells us to if (!cont) break; } handler.EndRdf(true); } } }
/// <summary> /// Makes a Query where the expected result is a Graph i.e. a CONSTRUCT or DESCRIBE query. /// </summary> /// <param name="handler">RDF Handler to process the results.</param> /// <param name="sparqlQuery">SPARQL Query.</param> public override void QueryWithResultGraph(IRdfHandler handler, string sparqlQuery) { // If no endpoints do nothing if (_endpoints.Count == 0) { return; } // Fire off all the asynchronous requests var asyncCalls = new List <Task <IGraph> >(); var count = 0; var cts = new CancellationTokenSource(); foreach (var endpoint in _endpoints) { // Limit the number of simultaneous requests we make to the user defined level (default 4) // We do this limiting check before trying to issue a request so that when the last request // is issued we'll always drop out of the loop and move onto our WaitAll() while (count >= _maxSimultaneousRequests) { // First check that the count of active requests is accurate int active = asyncCalls.Count(r => !r.IsCompleted); if (active < count) { // Some of the requests have already completed so we don't need to wait count = active; break; } else if (active > count) { // There are more active requests then we thought count = active; } // While the number of requests is at/above the maximum we'll wait for any of the requests to finish // Then we can decrement the count and if this drops back below our maximum then we'll go back into the // main loop and fire off our next request try { Task.WaitAny(asyncCalls.ToArray()); } catch (AggregateException ex) { var faultedTaskIx = asyncCalls.FindIndex(x => x.IsFaulted); var faultedEndpoint = _endpoints[faultedTaskIx]; if (!_ignoreFailedRequests) { throw new RdfQueryException("Federated Querying failed due to the query against the endpoint '" + faultedEndpoint + "' failing", ex); } } count--; } // Make an asynchronous query to the next endpoint asyncCalls.Add(Task.Factory.StartNew(() => endpoint.QueryWithResultGraph(sparqlQuery), cts.Token)); count++; } // Wait for all our requests to finish var waitTimeout = (Timeout > 0) ? Timeout : System.Threading.Timeout.Infinite; // Check for and handle timeouts try { if (!Task.WaitAll(asyncCalls.ToArray(), waitTimeout, cts.Token)) { // Handle timeouts - cancel overrunning tasks and optionally throw an exception cts.Cancel(false); if (!_ignoreFailedRequests) { throw new RdfQueryTimeoutException( "Federated Querying failed due to one/more endpoints failing to return results within the Timeout specified which is currently " + (Timeout / 1000) + " seconds"); } } } catch (AggregateException ex) { if (!_ignoreFailedRequests) { // Try to determine which endpoint faulted var faultedTaskIndex = asyncCalls.FindIndex(t => t.IsFaulted); var faultedEndpointUri = _endpoints[faultedTaskIndex].Uri.AbsoluteUri; throw new RdfQueryException( "Federated querying failed due to the query against the endpoint '" + faultedEndpointUri + "' failing.", ex.InnerException); } } // Now merge all the results together var cont = true; for (var i = 0; i < asyncCalls.Count; i++) { if (!asyncCalls[i].IsCompleted) { // This is a timed out task that has not transitioned to cancelled state yet // We will only get here if _ignoreFailedRequests is true continue; } try { var g = asyncCalls[i].Result; // Merge the result into the final results // If the handler has previously told us to stop we skip this step if (cont) { handler.StartRdf(); foreach (var t in g.Triples) { cont = handler.HandleTriple(t); // Stop if the Handler tells us to if (!cont) { break; } } handler.EndRdf(true); } } catch (AggregateException ex) { if (!_ignoreFailedRequests) { throw new RdfQueryException("Federated Querying failed due to the query against the endpoint '" + _endpoints[i] + "' failing", ex); } } } }
/// <summary> /// Handles a Triple by rewriting the Graph URI and passing it to the inner handler. /// </summary> /// <param name="t">Triple.</param> /// <returns></returns> protected override bool HandleTripleInternal(Triple t) { t = new Triple(t.Subject, t.Predicate, t.Object, _graphUri); return(_handler.HandleTriple(t)); }
private void TryParseTriple(XmlNode tripleEl, IRdfHandler handler, Uri graphUri) { //Verify Node Name if (!tripleEl.Name.Equals("triple")) { throw new RdfParseException("Unexpected Element <" + tripleEl.Name + "> encountered, only an optional <uri> element followed by zero/more <triple> elements are permitted within a <graph> element"); } //Verify number of Child Nodes if (!tripleEl.HasChildNodes) throw new RdfParseException("<triple> element has no child nodes - 3 child nodes are expected"); if (tripleEl.ChildNodes.Count < 3) throw new RdfParseException("<triple> element has too few child nodes (" + tripleEl.ChildNodes.Count + ") - 3 child nodes are expected"); if (tripleEl.ChildNodes.Count > 3) throw new RdfParseException("<triple> element has too many child nodes (" + tripleEl.ChildNodes.Count + ") - 3 child nodes are expected"); //Get the 3 Child Nodes XmlNode subjEl, predEl, objEl; subjEl = tripleEl.ChildNodes[0]; predEl = tripleEl.ChildNodes[1]; objEl = tripleEl.ChildNodes[2]; //Parse XML Nodes into RDF Nodes INode subj, pred, obj; if (subjEl.Name.Equals("uri")) { subj = handler.CreateUriNode(new Uri(subjEl.InnerText)); } else if (subjEl.Name.Equals("id")) { subj = handler.CreateBlankNode(subjEl.InnerText); } else { throw Error("Unexpected element <" + subjEl.Name + "> encountered, expected a <id>/<uri> element as the Subject of a Triple", subjEl); } if (predEl.Name.Equals("uri")) { pred = handler.CreateUriNode(new Uri(predEl.InnerText)); } else { throw Error("Unexpected element <" + predEl.Name + "> encountered, expected a <uri> element as the Predicate of a Triple", subjEl); } if (objEl.Name.Equals("uri")) { obj = handler.CreateUriNode(new Uri(objEl.InnerText)); } else if (objEl.Name.Equals("id")) { obj = handler.CreateBlankNode(objEl.InnerText); } else if (objEl.Name.Equals("plainLiteral")) { if (objEl.Attributes.GetNamedItem("xml:lang") != null) { obj = handler.CreateLiteralNode(objEl.InnerText, objEl.Attributes["xml:lang"].Value); } else { obj = handler.CreateLiteralNode(objEl.InnerText); } } else if (objEl.Name.Equals("typedLiteral")) { if (objEl.Attributes.GetNamedItem("datatype") != null) { Uri dtUri = new Uri(objEl.Attributes["datatype"].Value); if (objEl.FirstChild.NodeType == XmlNodeType.Text) { obj = handler.CreateLiteralNode(objEl.InnerText, dtUri); } else if (objEl.FirstChild.NodeType == XmlNodeType.CDATA) { obj = handler.CreateLiteralNode(objEl.FirstChild.InnerXml, dtUri); } else { obj = handler.CreateLiteralNode(objEl.InnerText, dtUri); } } else { throw Error("<typedLiteral> element does not have the required datatype attribute", objEl); } } else { throw Error("Unexpected element <" + objEl.Name + "> encountered, expected a <id>/<uri>/<plainLiteral>/<typedLiteral> element as the Object of a Triple", subjEl); } //Assert the resulting Triple if (!handler.HandleTriple(new Triple(subj, pred, obj, graphUri))) throw ParserHelper.Stop(); ; }
private void TryParseTriple(XmlNode tripleEl, IRdfHandler handler, Uri graphUri) { //Verify Node Name if (!tripleEl.Name.Equals("triple")) { throw new RdfParseException("Unexpected Element <" + tripleEl.Name + "> encountered, only an optional <uri> element followed by zero/more <triple> elements are permitted within a <graph> element"); } //Verify number of Child Nodes if (!tripleEl.HasChildNodes) { throw new RdfParseException("<triple> element has no child nodes - 3 child nodes are expected"); } if (tripleEl.ChildNodes.Count < 3) { throw new RdfParseException("<triple> element has too few child nodes (" + tripleEl.ChildNodes.Count + ") - 3 child nodes are expected"); } if (tripleEl.ChildNodes.Count > 3) { throw new RdfParseException("<triple> element has too many child nodes (" + tripleEl.ChildNodes.Count + ") - 3 child nodes are expected"); } //Get the 3 Child Nodes XmlNode subjEl, predEl, objEl; subjEl = tripleEl.ChildNodes[0]; predEl = tripleEl.ChildNodes[1]; objEl = tripleEl.ChildNodes[2]; //Parse XML Nodes into RDF Nodes INode subj, pred, obj; if (subjEl.Name.Equals("uri")) { subj = handler.CreateUriNode(UriFactory.Create(subjEl.InnerText)); } else if (subjEl.Name.Equals("id")) { subj = handler.CreateBlankNode(subjEl.InnerText); } else { throw Error("Unexpected element <" + subjEl.Name + "> encountered, expected a <id>/<uri> element as the Subject of a Triple", subjEl); } if (predEl.Name.Equals("uri")) { pred = handler.CreateUriNode(UriFactory.Create(predEl.InnerText)); } else { throw Error("Unexpected element <" + predEl.Name + "> encountered, expected a <uri> element as the Predicate of a Triple", subjEl); } if (objEl.Name.Equals("uri")) { obj = handler.CreateUriNode(UriFactory.Create(objEl.InnerText)); } else if (objEl.Name.Equals("id")) { obj = handler.CreateBlankNode(objEl.InnerText); } else if (objEl.Name.Equals("plainLiteral")) { if (objEl.Attributes.GetNamedItem("xml:lang") != null) { obj = handler.CreateLiteralNode(objEl.InnerText, objEl.Attributes["xml:lang"].Value); } else { obj = handler.CreateLiteralNode(objEl.InnerText); } } else if (objEl.Name.Equals("typedLiteral")) { if (objEl.Attributes.GetNamedItem("datatype") != null) { Uri dtUri = UriFactory.Create(objEl.Attributes["datatype"].Value); if (objEl.FirstChild.NodeType == XmlNodeType.Text) { obj = handler.CreateLiteralNode(objEl.InnerText, dtUri); } else if (objEl.FirstChild.NodeType == XmlNodeType.CDATA) { obj = handler.CreateLiteralNode(objEl.FirstChild.InnerXml, dtUri); } else { obj = handler.CreateLiteralNode(objEl.InnerText, dtUri); } } else { throw Error("<typedLiteral> element does not have the required datatype attribute", objEl); } } else { throw Error("Unexpected element <" + objEl.Name + "> encountered, expected a <id>/<uri>/<plainLiteral>/<typedLiteral> element as the Object of a Triple", subjEl); } //Assert the resulting Triple if (!handler.HandleTriple(new Triple(subj, pred, obj, graphUri))) { ParserHelper.Stop(); } ; }
public static void HandleTriple(this IRdfHandler handler, INode subj, INode pred, INode obj) { handler.HandleTriple(new Triple(subj, pred, obj)); }
private void TryParseTriple(IRdfHandler handler, IToken s, IToken p, IToken o, Uri graphUri) { INode subj, pred, obj; switch (s.TokenType) { case Token.BLANKNODEWITHID: subj = handler.CreateBlankNode(s.Value.Substring(2)); break; case Token.URI: subj = ParserHelper.TryResolveUri(handler, s); break; default: throw ParserHelper.Error("Unexpected Token '" + s.GetType().ToString() + "' encountered, expected a Blank Node/URI as the Subject of a Triple", s); } switch (p.TokenType) { case Token.URI: pred = ParserHelper.TryResolveUri(handler, p); break; default: throw ParserHelper.Error("Unexpected Token '" + p.GetType().ToString() + "' encountered, expected a URI as the Predicate of a Triple", p); } switch (o.TokenType) { case Token.BLANKNODEWITHID: obj = handler.CreateBlankNode(o.Value.Substring(2)); break; case Token.LITERAL: obj = handler.CreateLiteralNode(o.Value); break; case Token.LITERALWITHDT: String dtUri = ((LiteralWithDataTypeToken)o).DataType; obj = handler.CreateLiteralNode(o.Value, new Uri(dtUri.Substring(1, dtUri.Length - 2))); break; case Token.LITERALWITHLANG: obj = handler.CreateLiteralNode(o.Value, ((LiteralWithLanguageSpecifierToken)o).Language); break; case Token.URI: obj = ParserHelper.TryResolveUri(handler, o); break; default: throw ParserHelper.Error("Unexpected Token '" + o.GetType().ToString() + "' encountered, expected a Blank Node/Literal/URI as the Object of a Triple", o); } if (!handler.HandleTriple(new Triple(subj, pred, obj, graphUri))) throw ParserHelper.Stop(); }
/// <summary> /// Delegates triple handling to inner handler /// </summary> protected override bool HandleTripleInternal(Triple t) => _handler.HandleTriple(t);
/// <summary> /// Loads a Graph from the Quad Store /// </summary> /// <param name="handler">RDF Handler</param> /// <param name="graphUri">URI of the Graph to Load</param> public void LoadGraph(IRdfHandler handler, Uri graphUri) { if (graphUri == null) throw new RdfStorageException("Cannot load an unnamed Graph from Virtuoso as this would require loading the entirety of the Virtuoso Quad Store into memory!"); try { handler.StartRdf(); //Need to keep Database Open as Literals require extra trips to the Database to get additional //information about Language and Type this.Open(false); DataTable data = this.LoadTriples(graphUri); foreach (DataRow row in data.Rows) { Object s, p, o; INode subj, pred, obj; //Get Data s = row["S"]; p = row["P"]; o = row["O"]; //Create Nodes subj = this.LoadNode(handler, s); pred = this.LoadNode(handler, p); obj = this.LoadNode(handler, o); //Assert Triple if (!handler.HandleTriple(new Triple(subj, pred, obj))) ParserHelper.Stop(); } handler.EndRdf(true); this.Close(false); } catch (RdfParsingTerminatedException) { handler.EndRdf(true); this.Close(false); } catch { handler.EndRdf(false); this.Close(true); throw; } }
private INode MakeNode(IRdfHandler handler, JToken token, Uri graphIri, bool allowRelativeIri = false) { if (token is JValue) { var stringValue = token.Value <string>(); if (JsonLdUtils.IsBlankNodeIdentifier(stringValue)) { return(handler.CreateBlankNode(stringValue.Substring(2))); } if (Uri.TryCreate(stringValue, allowRelativeIri ? UriKind.RelativeOrAbsolute : UriKind.Absolute, out Uri iri)) { if (!Uri.IsWellFormedUriString(stringValue, allowRelativeIri ? UriKind.RelativeOrAbsolute : UriKind.Absolute)) { return(null); } return(handler.CreateUriNode(iri)); } return(null); } if (JsonLdUtils.IsValueObject(token)) { string literalValue = null; var valueObject = token as JObject; var value = valueObject["@value"]; var datatype = valueObject.Property("@type")?.Value.Value <string>(); var language = valueObject.Property("@language")?.Value.Value <string>(); if (datatype == "@json") { datatype = RdfNs + "JSON"; var serializer = new JsonLiteralSerializer(); literalValue = serializer.Serialize(value); } else if (value.Type == JTokenType.Boolean) { literalValue = value.Value <bool>() ? "true" : "false"; if (datatype == null) { datatype = XsdNs + "boolean"; } } else if (value.Type == JTokenType.Float || value.Type == JTokenType.Integer && datatype != null && datatype.Equals(XsdNs + "double")) { var doubleValue = value.Value <double>(); var roundedValue = Math.Round(doubleValue); if (doubleValue.Equals(roundedValue) && doubleValue < 1000000000000000000000.0 && datatype == null) { // Integer values up to 10^21 should be rendered as a fixed-point integer literalValue = roundedValue.ToString("F0"); datatype = XsdNs + "integer"; } else { literalValue = value.Value <double>().ToString("E15", CultureInfo.InvariantCulture); literalValue = ExponentialFormatMatcher.Replace(literalValue, "$1E"); if (literalValue.EndsWith("E")) { literalValue = literalValue + "0"; } if (datatype == null) { datatype = XsdNs + "double"; } } } else if (value.Type == JTokenType.Integer || value.Type == JTokenType.Float && datatype != null && datatype.Equals(XsdNs + "integer")) { literalValue = value.Value <long>().ToString("D", CultureInfo.InvariantCulture); if (datatype == null) { datatype = XsdNs + "integer"; } } else if (valueObject.ContainsKey("@direction") && ParserOptions.RdfDirection.HasValue) { literalValue = value.Value <string>(); var direction = valueObject["@direction"].Value <string>(); language = valueObject.ContainsKey("@language") ? valueObject["@language"].Value <string>().ToLowerInvariant() : string.Empty; if (ParserOptions.RdfDirection == JsonLdRdfDirectionMode.I18NDatatype) { datatype = "https://www.w3.org/ns/i18n#" + language + "_" + direction; return(handler.CreateLiteralNode(literalValue, new Uri(datatype))); } // Otherwise direction mode is CompoundLiteral var literalNode = handler.CreateBlankNode(); var xsdString = UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeString); handler.HandleTriple(new Triple( literalNode, handler.CreateUriNode(UriFactory.Create(RdfSpecsHelper.RdfValue)), handler.CreateLiteralNode(literalValue, xsdString), graphIri)); if (!string.IsNullOrEmpty(language)) { handler.HandleTriple(new Triple( literalNode, handler.CreateUriNode(UriFactory.Create(RdfSpecsHelper.RdfLanguage)), handler.CreateLiteralNode(language, xsdString), graphIri)); } handler.HandleTriple(new Triple( literalNode, handler.CreateUriNode(UriFactory.Create(RdfSpecsHelper.RdfDirection)), handler.CreateLiteralNode(direction, xsdString), graphIri)); return(literalNode); } else { literalValue = value.Value <string>(); if (datatype == null && language == null) { datatype = XsdNs + "string"; } } if (language != null) { return(LanguageTag.IsWellFormed(language) ? handler.CreateLiteralNode(literalValue, language) : null); } return(handler.CreateLiteralNode(literalValue, new Uri(datatype))); } if (JsonLdUtils.IsListObject(token)) { var listArray = token["@list"] as JArray; return(MakeRdfList(handler, listArray, graphIri)); } if ((token as JObject)?.Property("@id") != null) { // Must be a node object var nodeObject = (JObject)token; return(MakeNode(handler, nodeObject["@id"], graphIri)); } return(null); }