本文整理汇总了C#中IRdfHandler.StartRdf方法的典型用法代码示例。如果您正苦于以下问题:C# IRdfHandler.StartRdf方法的具体用法?C# IRdfHandler.StartRdf怎么用?C# IRdfHandler.StartRdf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IRdfHandler
的用法示例。
在下文中一共展示了IRdfHandler.StartRdf方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Load
/// <inheritdoc />
public void Load(IRdfHandler handler, TextReader input)
{
bool finished = false;
try
{
handler.StartRdf();
using (JsonReader jsonReader = new JsonTextReader(input))
{
jsonReader.DateParseHandling = DateParseHandling.None;
JToken json = JsonLD.Core.JsonLdProcessor.Expand(JToken.Load(jsonReader));
foreach (JObject subjectJObject in json)
{
string subject = subjectJObject["@id"].ToString();
JToken type;
if (subjectJObject.TryGetValue("@type", out type))
{
HandleType(handler, subject, type);
}
foreach (var property in subjectJObject.Properties().Where(property => (property.Name != "@id") && (property.Name != "@type")))
{
HandleProperty(handler, subject, property);
}
}
}
finished = true;
handler.EndRdf(true);
}
catch
{
finished = true;
handler.EndRdf(false);
throw;
}
finally
{
if (!finished)
{
handler.EndRdf(true);
}
}
}
示例2: Describe
/// <summary>
/// Gets the Description Graph based on the Query Results from the given Evaluation Context passing the resulting Triples to the given RDF Handler
/// </summary>
/// <param name="handler">RDF Handler</param>
/// <param name="context">SPARQL Evaluation Context</param>
public void Describe(IRdfHandler handler, SparqlEvaluationContext context)
{
try
{
handler.StartRdf();
//Apply Base URI and Namespaces to the Handler
if (context.Query != null)
{
if (context.Query.BaseUri != null)
{
if (!handler.HandleBaseUri(context.Query.BaseUri)) ParserHelper.Stop();
}
foreach (String prefix in context.Query.NamespaceMap.Prefixes)
{
if (!handler.HandleNamespace(prefix, context.Query.NamespaceMap.GetNamespaceUri(prefix))) ParserHelper.Stop();
}
}
//Get the Nodes needing describing
List<INode> nodes = this.GetNodes(handler, context);
if (nodes.Count > 0)
{
//If there is at least 1 Node then start describing
this.DescribeInternal(handler, context, nodes);
}
handler.EndRdf(true);
}
catch (RdfParsingTerminatedException)
{
handler.EndRdf(true);
}
catch
{
handler.EndRdf(false);
throw;
}
}
示例3: Parse
private void Parse(IRdfHandler handler, ITokenQueue tokens)
{
IToken next;
IToken s, p, o;
try
{
handler.StartRdf();
//Expect a BOF token at start
next = tokens.Dequeue();
if (next.TokenType != Token.BOF)
{
throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a BOF token at the start of the input", next);
}
do
{
next = tokens.Peek();
if (next.TokenType == Token.EOF) return;
s = this.TryParseSubject(tokens);
p = this.TryParsePredicate(tokens);
o = this.TryParseObject(tokens);
Uri context = this.TryParseContext(tokens);
this.TryParseTriple(handler, s, p, o, context);
next = tokens.Peek();
} while (next.TokenType != Token.EOF);
handler.EndRdf(true);
}
catch (RdfParsingTerminatedException)
{
handler.EndRdf(true);
//Discard this - it justs means the Handler told us to stop
}
catch
{
handler.EndRdf(false);
throw;
}
}
示例4: Load
public void Load(IRdfHandler handler, TextReader input)
{
bool finished = false;
try
{
// Tell handler we starting parsing
handler.StartRdf();
// Perform actual parsing
using (JsonReader jsonReader = new JsonTextReader(input))
{
jsonReader.DateParseHandling = DateParseHandling.None;
JToken json = JToken.Load(jsonReader);
foreach (JObject subjectJObject in json)
{
string subject = subjectJObject["@id"].ToString();
JToken type;
if (subjectJObject.TryGetValue("@type", out type))
{
if (type is JArray)
{
foreach (JToken t in (JArray) type)
{
if (!HandleTriple(handler, subject, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", t.ToString(), null, false)) return;
}
}
else
{
if (!HandleTriple(handler, subject, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", type.ToString(), null, false)) return;
}
}
foreach (JProperty property in subjectJObject.Properties())
{
if (property.Name == "@id" || property.Name == "@type")
{
continue;
}
foreach (JObject objectJObject in property.Value)
{
JToken id;
JToken value;
if (objectJObject.TryGetValue("@id", out id))
{
if (!HandleTriple(handler, subject, property.Name, id.ToString(), null, false)) return;
}
else if (objectJObject.TryGetValue("@value", out value))
{
string datatype = null;
JToken datatypeJToken;
if (objectJObject.TryGetValue("@type", out datatypeJToken))
{
datatype = datatypeJToken.ToString();
}
else
{
switch (value.Type)
{
case JTokenType.Boolean:
datatype = "http://www.w3.org/2001/XMLSchema#boolean";
break;
case JTokenType.Float:
datatype = "http://www.w3.org/2001/XMLSchema#double";
break;
case JTokenType.Integer:
datatype = "http://www.w3.org/2001/XMLSchema#integer";
break;
}
}
if (!HandleTriple(handler, subject, property.Name, value.ToString(), datatype, true)) return;
}
}
}
}
}
// Tell handler we've finished parsing
finished = true;
handler.EndRdf(true);
}
catch
{
// Catch all block to fulfill the IRdfHandler contract of informing the handler when the parsing has ended with failure
finished = true;
handler.EndRdf(false);
throw;
}
finally
{
// Finally block handles the case where we exit the parsing loop early because the handler indicated it did not want
// to receive further triples. In this case finished will be set to false and we need to inform the handler we're are done
if (!finished)
{
handler.EndRdf(true);
}
//.........这里部分代码省略.........
示例5: TryParseGraphset
private void TryParseGraphset(XmlReader reader, IRdfHandler handler)
{
try
{
handler.StartRdf();
reader.Read();
//Skip XML Declaration if present
if (reader.NodeType == XmlNodeType.XmlDeclaration) reader.Read();
if (!reader.Name.Equals("TriX"))
{
throw new RdfParseException("Unexpected Document Element '" + reader.Name + "' encountered, expected the Document Element of a TriX Document to be the <TriX> element");
}
if (!reader.HasAttributes)
{
throw new RdfParseException("<TriX> fails to define any attributes, the element must define the xmlns attribute to be the TriX namespace");
}
else
{
bool trixNSDefined = false;
for (int i = 0; i < reader.AttributeCount; i++)
{
reader.MoveToNextAttribute();
if (reader.Name.Equals("xmlns"))
{
//Ensure that the xmlns attribute is defined and is the TriX namespace
if (trixNSDefined) throw new RdfParseException("The xmlns attribute can only occur once on the <TriX> element");
if (!reader.Value.Equals(TriXNamespaceURI)) throw new RdfParseException("The xmlns attribute of the <TriX> element must have it's value set to the TriX Namespace URI which is '" + TriXNamespaceURI + "'");
trixNSDefined = true;
}
else if (reader.LocalName.Equals("xmlns"))
{
//Don't think we need to do anything here
}
}
if (!trixNSDefined) throw new RdfParseException("The <TriX> element fails to define the required xmlns attribute defining the TriX Namespace");
}
//Process Child Nodes
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
//For elements we recurse to try and parse a Graph
this.TryParseGraph(reader, handler);
}
else if (reader.NodeType == XmlNodeType.EndElement)
{
//Stop when we hit an end element
break;
}
else
{
//Stop if we hit an unexpected element
break;
}
}
//Expect the </TriX> element
if (reader.NodeType == XmlNodeType.EndElement)
{
if (!reader.Name.Equals("TriX")) throw new RdfParseException("Expected </TriX> element was not found, encountered </" + reader.Name + "> instead");
}
else
{
throw new RdfParseException("Unexpected Note Type " + reader.NodeType.ToString() + " encountered, expected a </TriX> element");
}
handler.EndRdf(true);
}
catch (RdfParsingTerminatedException)
{
handler.EndRdf(true);
//Discard this - it justs means the Handler told us to stop
}
catch
{
handler.EndRdf(false);
throw;
}
}
示例6: LoadGraph
/// <summary>
/// Loads a Graph from the Store asynchronously
/// </summary>
/// <param name="handler">Handler to load with</param>
/// <param name="graphUri">URI of the Graph to load</param>
/// <param name="callback">Callback</param>
/// <param name="state">State to pass to the callback</param>
public override void LoadGraph(IRdfHandler handler, string graphUri, AsyncStorageCallback callback, object state)
{
try
{
HttpWebRequest request;
Dictionary<String, String> serviceParams = new Dictionary<string, string>();
Uri baseUri = null;
SparqlParameterizedString construct = new SparqlParameterizedString();
if (!graphUri.Equals(string.Empty))
{
construct.CommandText = "CONSTRUCT { ?s ?p ?o } WHERE { GRAPH @graph { ?s ?p ?o } }";
baseUri = UriFactory.Create(graphUri);
construct.SetUri("graph", baseUri);
}
else
{
construct.CommandText = "CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }";
}
serviceParams.Add("query", construct.ToString());
request = this.CreateRequest("/sparql", MimeTypesHelper.HttpAcceptHeader, "GET", serviceParams);
Tools.HttpDebugRequest(request);
request.BeginGetResponse(r =>
{
try
{
using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(r))
{
Tools.HttpDebugResponse(response);
IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType);
parser.Load(handler, new StreamReader(response.GetResponseStream()));
if (baseUri != null)
{
handler.StartRdf();
handler.HandleBaseUri(baseUri);
handler.EndRdf(true);
}
response.Close();
}
callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.LoadWithHandler, handler), state);
}
catch (WebException webEx)
{
callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.LoadWithHandler, StorageHelper.HandleHttpError(webEx, "loading a Graph from")), state);
}
catch (Exception ex)
{
callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.LoadWithHandler, StorageHelper.HandleError(ex, "loading a Graph from")), state);
}
}, state);
}
catch (WebException webEx)
{
callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.LoadWithHandler, StorageHelper.HandleHttpError(webEx, "loading a Graph from")), state);
}
catch (Exception ex)
{
callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.LoadWithHandler, StorageHelper.HandleError(ex, "loading a Graph from")), state);
}
}
示例7: QueryWithResultGraph
//.........这里部分代码省略.........
//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);
}
}
}
示例8: LoadGraph
/// <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;
}
}