本文整理汇总了C#中VDS.RDF.Parsing.SparqlQueryParser.ParseFromFile方法的典型用法代码示例。如果您正苦于以下问题:C# SparqlQueryParser.ParseFromFile方法的具体用法?C# SparqlQueryParser.ParseFromFile怎么用?C# SparqlQueryParser.ParseFromFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类VDS.RDF.Parsing.SparqlQueryParser
的用法示例。
在下文中一共展示了SparqlQueryParser.ParseFromFile方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SparqlParsingNestedGraphPatternFirstItem2
public void SparqlParsingNestedGraphPatternFirstItem2()
{
try
{
SparqlQueryParser parser = new SparqlQueryParser();
SparqlQuery q = parser.ParseFromFile("childgraphpattern2.rq");
Console.WriteLine(q.ToString());
Console.WriteLine();
Console.WriteLine(q.ToAlgebra().ToString());
}
catch (RdfParseException parseEx)
{
TestTools.ReportError("Parsing Error", parseEx, true);
}
}
示例2: SparqlFunctionsNow
public void SparqlFunctionsNow()
{
SparqlQueryParser parser = new SparqlQueryParser();
SparqlQuery q = parser.ParseFromFile("now01.rq");
Console.WriteLine("ToString Output:");
Console.WriteLine(q.ToString());
Console.WriteLine();
SparqlFormatter formatter = new SparqlFormatter();
Console.WriteLine("SparqlFormatter Output:");
Console.WriteLine(formatter.Format(q));
TripleStore store = new TripleStore();
SparqlResultSet results = q.Evaluate(store) as SparqlResultSet;
if (results != null)
{
Assert.IsTrue(results.Result, "Result should be true");
}
else
{
Assert.Fail("Expected a non-null result");
}
}
示例3: Main
//.........这里部分代码省略.........
{
reportError(output, "Parsing Exception", parseEx);
}
catch (RdfException rdfEx)
{
reportError(output, "RDF Exception", rdfEx);
}
catch (Exception ex)
{
reportError(output, "Other Exception", ex);
}
finally
{
timer.Stop();
//Write the Triples to the Output
foreach (Triple t in g.Triples)
{
Console.WriteLine(t.ToString());
}
//Now we run the Test SPARQL (if present)
if (File.Exists("rdfa_tests/" + Path.GetFileNameWithoutExtension(file) + ".sparql"))
{
if (skipCheck.Contains(Path.GetFileName(file)))
{
output.WriteLine("## Skipping Check of File " + Path.GetFileName(file));
output.WriteLine();
}
else
{
try
{
SparqlQuery q = queryparser.ParseFromFile("rdfa_tests/" + Path.GetFileNameWithoutExtension(file) + ".sparql");
Object results = g.ExecuteQuery(q);
if (results is SparqlResultSet)
{
//The Result is the result of the ASK Query
if (falseTests.Contains(Path.GetFileName(file)))
{
passed = !((SparqlResultSet)results).Result;
}
else
{
passed = ((SparqlResultSet)results).Result;
}
}
}
catch
{
passed = false;
}
}
}
if (passed && passDesired)
{
//Passed and we wanted to Pass
testsPassed++;
output.WriteLine("# Result = Test Passed");
totalTime += timer.ElapsedMilliseconds;
totalTriples += g.Triples.Count;
}
else if (!passed && passDesired)
{
//Failed when we should have Passed
示例4: DoQuery
static void DoQuery(Dictionary<String, String> arguments)
{
SparqlRemoteEndpoint endpoint;
bool verbose = arguments.ContainsKey("verbose") || arguments.ContainsKey("v");
if (verbose) Options.HttpDebugging = true;
//First get the Server to which we are going to connect
try
{
if (arguments.ContainsKey("server") && !arguments["server"].Equals(String.Empty))
{
endpoint = new SparqlRemoteEndpoint(new Uri(arguments["server"]));
}
else if (arguments.ContainsKey("service") && !arguments["service"].Equals(String.Empty))
{
endpoint = new SparqlRemoteEndpoint(new Uri(arguments["service"]));
}
else
{
Console.Error.WriteLine("soh: Error: Required --server/--service argument not present");
Environment.Exit(-1);
return;
}
}
catch (UriFormatException uriEx)
{
Console.Error.WriteLine("soh: Error: Malformed SPARQL Endpoint URI");
Console.Error.WriteLine(uriEx.Message);
Environment.Exit(-1);
return;
}
if (verbose) Console.Error.WriteLine("soh: SPARQL Endpoint for URI " + endpoint.Uri + " created OK");
//Then decide where to get the query to execute from
SparqlQueryParser parser = new SparqlQueryParser();
SparqlQuery query;
try
{
if (arguments.ContainsKey("query") && !arguments["query"].Equals(String.Empty))
{
query = parser.ParseFromFile(arguments["query"]);
}
else if (arguments.ContainsKey("file") && !arguments["file"].Equals(String.Empty))
{
query = parser.ParseFromFile(arguments["file"]);
}
else if (arguments.ContainsKey("$1") && !arguments["$1"].Equals(String.Empty))
{
query = parser.ParseFromString(arguments["$1"]);
}
else
{
Console.Error.WriteLine("soh: Error: Required SPARQL Query not found - may be specified as --file/--query FILE or as final argument");
Environment.Exit(-1);
return;
}
}
catch (Exception ex)
{
Console.Error.WriteLine("soh: Error: Error Parsing SPARQL Query");
Console.Error.WriteLine(ex.Message);
Environment.Exit(-1);
return;
}
if (verbose)
{
Console.Error.WriteLine("soh: Parsed Query OK");
Console.Error.WriteLine("soh: dotNetRDF's interpretation of the Query:");
SparqlFormatter formatter = new SparqlFormatter();
Console.Error.WriteLine(formatter.Format(query));
Console.Error.WriteLine("soh: Submitting Query");
}
try
{
using (HttpWebResponse response = endpoint.QueryRaw(query.ToString()))
{
MimeTypeDefinition definition = MimeTypesHelper.GetDefinitions(response.ContentType).FirstOrDefault();
Encoding enc;
if (definition != null)
{
enc = definition.Encoding;
}
else if (!response.ContentEncoding.Equals(String.Empty))
{
enc = Encoding.GetEncoding(response.ContentEncoding);
}
else if (response.ContentType.Contains("charset="))
{
enc = Encoding.GetEncoding(response.ContentType.Substring(response.ContentType.IndexOf('=') + 1));
}
else
{
enc = Console.OutputEncoding;
}
if (verbose)
{
Console.Error.WriteLine("soh: Got Response from SPARQL Endpoint OK");
//.........这里部分代码省略.........
示例5: ProcessEvaluationTest
private int ProcessEvaluationTest(SparqlQueryParser parser, Triple commentDef, String queryFile, String dataFile, List<String> dataFiles, String resultFile)
{
Console.WriteLine("# Processing Evaluation Test " + Path.GetFileName(queryFile));
if (commentDef != null)
{
Console.WriteLine(commentDef.Object.ToString());
Console.WriteLine();
}
if (dataFiles.Contains(dataFile)) dataFiles.Remove(dataFile);
if (queryFile.StartsWith("file:///")) queryFile = queryFile.Substring(8);
if (dataFile != null && dataFile.StartsWith("file:///")) dataFile = dataFile.Substring(8);
if (resultFile.StartsWith("file:///")) resultFile = resultFile.Substring(8);
Console.WriteLine("Query File is " + queryFile);
if (evaluationTestOverride.Any(x => queryFile.EndsWith(x)))
{
Console.WriteLine();
Console.WriteLine("# Test Result = Manually overridden to Pass (Test Passed)");
testsPassed++;
testsEvaluationPassed++;
return 1;
}
if (dataFile != null) Console.WriteLine("Default Graph File is " + dataFile);
foreach (String file in dataFiles)
{
Console.WriteLine("Uses Named Graph File " + file);
}
Console.WriteLine("Expected Result File is " + resultFile);
Console.WriteLine();
SparqlQuery query;
try
{
query = parser.ParseFromFile(queryFile);
Console.WriteLine(query.ToString());
Console.WriteLine();
Console.WriteLine("Formatted with SparqlFormatter");
SparqlFormatter formatter = new SparqlFormatter(query.NamespaceMap);
Console.WriteLine(formatter.Format(query));
Console.WriteLine();
try
{
Console.WriteLine(query.ToAlgebra().ToString());
Console.WriteLine();
}
catch
{
//Do Nothing
}
}
catch (RdfParseException parseEx)
{
this.ReportError("Query Parser Error", parseEx);
testsFailed++;
testsEvaluationFailed++;
Console.WriteLine("# Test Result = Unable to parse query (Test Failed)");
return -1;
}
IInMemoryQueryableStore store;
if (dataFile != null)
{
store = new TripleStore();
}
else
{
store = new WebDemandTripleStore();
}
//Load Default Graph
Graph defaultGraph = new Graph();
try
{
if (dataFile != null)
{
FileLoader.Load(defaultGraph, dataFile);
}
store.Add(defaultGraph);
}
catch (RdfParseException parseEx)
{
this.ReportError("Parser Error", parseEx);
testsFailed++;
testsEvaluationFailed++;
Console.WriteLine("# Test Result = Unable to parse Default Graph (Test Failed)");
return -1;
}
//Load Named Graphs
try
{
foreach (String graphFile in dataFiles)
{
Graph namedGraph = new Graph();
if (graphFile.StartsWith("file:///"))
{
//.........这里部分代码省略.........
示例6: ProcessSyntaxTest
private void ProcessSyntaxTest(SparqlQueryParser parser, String queryFile, bool shouldParse)
{
if (queryFile.StartsWith("file:///")) queryFile = queryFile.Substring(8);
bool error = false;
try
{
Console.WriteLine("# Processing Syntax Test " + Path.GetFileName(queryFile));
Console.Write("# Result Expected = ");
if (shouldParse)
{
Console.WriteLine("Parses OK");
}
else
{
Console.WriteLine("Parsing Fails");
}
SparqlQuery q = parser.ParseFromFile(queryFile);
Console.WriteLine(q.ToString());
Console.WriteLine();
Console.WriteLine("Formatted with SparqlFormatter");
SparqlFormatter formatter = new SparqlFormatter(q.NamespaceMap);
Console.WriteLine(formatter.Format(q));
Console.WriteLine();
}
catch (RdfParseException parseEx)
{
this.ReportError("Parser Error", parseEx);
error = true;
}
catch (Exception ex)
{
this.ReportError("Other Error", ex);
error = true;
}
finally
{
Console.Write("# Result = ");
if (error)
{
if (shouldParse)
{
Console.WriteLine(" Parsing Failed when should have parsed (Test Failed)");
testsFailed++;
testsSyntaxFailed++;
}
else
{
Console.WriteLine(" Parsing Failed as expected (Test Passed)");
testsPassed++;
testsSyntaxPassed++;
}
}
else
{
if (shouldParse)
{
Console.WriteLine(" Parsed OK as expected (Test Passed)");
testsPassed++;
testsSyntaxPassed++;
}
else
{
Console.WriteLine(" Parsed OK when should have failed (Test Failed)");
testsFailed++;
testsSyntaxFailed++;
}
}
Console.WriteLine(new String('-', 150));
}
}
示例7: ProcessSyntaxTest
private void ProcessSyntaxTest(SparqlQueryParser queryParser, SparqlUpdateParser updateParser, String inputFile, bool shouldParse)
{
if (inputFile.StartsWith("file:///")) inputFile = inputFile.Substring(8);
bool error = false;
bool skipFinally = false;
try
{
Console.WriteLine("# Processing Syntax Test " + Path.GetFileName(inputFile));
Console.Write("# Result Expected = ");
if (shouldParse)
{
Console.WriteLine("Parses OK");
}
else
{
Console.WriteLine("Parsing Fails");
}
if (evaluationTestOverride.Any(x => inputFile.EndsWith(x)))
{
Console.WriteLine();
Console.WriteLine("# Test Result = Manually overridden to Pass (Test Passed)");
skipFinally = true;
testsPassed++;
testsSyntaxPassed++;
return;
}
if (inputFile.EndsWith(".rq"))
{
SparqlQuery q = queryParser.ParseFromFile(inputFile);
Console.WriteLine("Formatted with SparqlFormatter");
SparqlFormatter formatter = new SparqlFormatter(q.NamespaceMap);
Console.WriteLine(formatter.Format(q));
Console.WriteLine();
}
else if (inputFile.EndsWith(".ru"))
{
SparqlUpdateCommandSet cmds = updateParser.ParseFromFile(inputFile);
Console.WriteLine(cmds.ToString());
Console.WriteLine();
}
else
{
Console.WriteLine("# Test Result - Unknown Input File for Syntax Test (Test Indeterminate)");
skipFinally = true;
testsIndeterminate++;
testsSyntaxIndeterminate++;
return;
}
}
catch (RdfParseException parseEx)
{
this.ReportError("Parser Error", parseEx);
error = true;
}
catch (Exception ex)
{
this.ReportError("Other Error", ex);
error = true;
}
finally
{
if (!skipFinally)
{
Console.Write("# Test Result = ");
if (error)
{
if (shouldParse)
{
Console.WriteLine(" Parsing Failed when should have parsed (Test Failed)");
testsFailed++;
testsSyntaxFailed++;
}
else
{
Console.WriteLine(" Parsing Failed as expected (Test Passed)");
testsPassed++;
testsSyntaxPassed++;
}
}
else
{
if (shouldParse)
{
Console.WriteLine(" Parsed OK as expected (Test Passed)");
testsPassed++;
testsSyntaxPassed++;
}
else
{
Console.WriteLine(" Parsed OK when should have failed (Test Failed)");
testsFailed++;
testsSyntaxFailed++;
}
}
}
//.........这里部分代码省略.........
示例8: SparqlEvaluationMultipleOptionals2
public void SparqlEvaluationMultipleOptionals2()
{
TripleStore store = new TripleStore();
store.LoadFromFile("multiple-options.trig");
SparqlQueryParser parser = new SparqlQueryParser();
SparqlQuery query = parser.ParseFromFile("multiple-optionals-alternate.rq");
Object results = query.Evaluate(store);
if (results is SparqlResultSet)
{
TestTools.ShowResults(results);
}
else
{
Assert.Fail("Expected a SPARQL Result Set");
}
}
示例9: SparqlEvaluationGraphUnion
public void SparqlEvaluationGraphUnion()
{
SparqlQueryParser parser = new SparqlQueryParser();
SparqlQuery q = parser.ParseFromFile("graph-11.rq");
q.BaseUri = new Uri("file:///" + Environment.CurrentDirectory.Replace('\\', '/') + "/");
TripleStore store = new TripleStore();
Graph g = new Graph();
FileLoader.Load(g, "data-g1.ttl");
store.Add(g);
Graph h = new Graph();
FileLoader.Load(h, "data-g2.ttl");
store.Add(h);
Graph i = new Graph();
FileLoader.Load(i, "data-g3.ttl");
store.Add(i);
Graph j = new Graph();
FileLoader.Load(j, "data-g4.ttl");
store.Add(j);
InMemoryDataset dataset = new InMemoryDataset(store);
q.AddDefaultGraph(g.BaseUri);
q.AddNamedGraph(g.BaseUri);
q.AddNamedGraph(h.BaseUri);
q.AddNamedGraph(i.BaseUri);
q.AddNamedGraph(j.BaseUri);
SparqlFormatter formatter = new SparqlFormatter(q.NamespaceMap);
Object results;
//Try the full Query
Console.WriteLine("Full Query");
Console.WriteLine(formatter.Format(q));
results = q.Evaluate(dataset);
if (results is SparqlResultSet)
{
SparqlResultSet rset = (SparqlResultSet)results;
TestTools.ShowResults(rset);
//SparqlRdfParser resultsParser = new SparqlRdfParser(new TurtleParser());
//SparqlResultSet expected = new SparqlResultSet();
//resultsParser.Load(expected, "graph-11.ttl");
//Console.WriteLine();
//Console.WriteLine("Expected Results");
//TestTools.ShowResults(expected);
//Assert.AreEqual(rset, expected, "Result Sets should be equal");
}
else
{
Assert.Fail("Didn't get a Result Set as expected");
}
}
示例10: SparqlEvaluationComplexOptionalGraphUnion
public void SparqlEvaluationComplexOptionalGraphUnion()
{
SparqlQueryParser parser = new SparqlQueryParser();
SparqlQuery q = parser.ParseFromFile("q-opt-complex-4.rq");
TripleStore store = new TripleStore();
Graph g = new Graph();
FileLoader.Load(g, "complex-data-2.ttl");
store.Add(g);
Graph h = new Graph();
FileLoader.Load(h, "complex-data-1.ttl");
store.Add(h);
InMemoryDataset dataset = new InMemoryDataset(store);
q.AddDefaultGraph(g.BaseUri);
q.AddNamedGraph(h.BaseUri);
SparqlFormatter formatter = new SparqlFormatter(q.NamespaceMap);
Object results;
//Examine limited parts of the Query to see why it doesn't work properly
SparqlParameterizedString unionClause = new SparqlParameterizedString();
unionClause.Namespaces = q.NamespaceMap;
unionClause.QueryText = "SELECT * WHERE { ?person foaf:name ?name . { ?person ex:healthplan ?plan . } UNION { ?person ex:department ?dept . } }";
SparqlQuery unionQuery = parser.ParseFromString(unionClause);
Console.WriteLine("UNION Clause Only");
Console.WriteLine(formatter.Format(unionQuery));
results = unionQuery.Evaluate(dataset);
if (results is SparqlResultSet)
{
SparqlResultSet rset = (SparqlResultSet)results;
TestTools.ShowResults(rset);
}
else
{
Assert.Fail("Didn't get a Result Set as expected");
}
Console.WriteLine();
//Try the Optional Clause
SparqlParameterizedString optionalClause = new SparqlParameterizedString();
optionalClause.Namespaces = q.NamespaceMap;
optionalClause.QueryText = "SELECT * WHERE { OPTIONAL { ?person a foaf:Person . GRAPH ?g { [] foaf:depiction ?img ; foaf:name ?name } } }";
SparqlQuery optionalQuery = parser.ParseFromString(optionalClause);
Console.WriteLine("OPTIONAL Clause Only");
Console.WriteLine(formatter.Format(optionalQuery));
results = optionalQuery.Evaluate(dataset);
if (results is SparqlResultSet)
{
SparqlResultSet rset = (SparqlResultSet)results;
TestTools.ShowResults(rset);
}
else
{
Assert.Fail("Didn't get a Result Set as expected");
}
Console.WriteLine();
//Try the full Query with a SELECT * to examine all the values
SparqlParameterizedString fullQuery = new SparqlParameterizedString();
fullQuery.Namespaces = q.NamespaceMap;
fullQuery.QueryText = "SELECT * WHERE { ?person foaf:name ?name . { ?person ex:healthplan ?plan . } UNION { ?person ex:department ?dept . } OPTIONAL { ?person a foaf:Person . GRAPH ?g { [] foaf:depiction ?img ; foaf:name ?name } } }";
SparqlQuery q2 = parser.ParseFromString(fullQuery);
Console.WriteLine("Full Query as a SELECT *");
Console.WriteLine(formatter.Format(q2));
results = q2.Evaluate(dataset);
if (results is SparqlResultSet)
{
SparqlResultSet rset = (SparqlResultSet)results;
TestTools.ShowResults(rset);
}
else
{
Assert.Fail("Didn't get a Result Set as expected");
}
Console.WriteLine();
//Try the full Query
Console.WriteLine("Full Query");
Console.WriteLine(formatter.Format(q));
results = q.Evaluate(dataset);
if (results is SparqlResultSet)
{
SparqlResultSet rset = (SparqlResultSet)results;
TestTools.ShowResults(rset);
SparqlRdfParser resultsParser = new SparqlRdfParser(new TurtleParser());
SparqlResultSet expected = new SparqlResultSet();
resultsParser.Load(expected, "result-opt-complex-4.ttl");
Console.WriteLine();
Console.WriteLine("Expected Results");
TestTools.ShowResults(expected);
//.........这里部分代码省略.........
示例11: SparqlAnton
public void SparqlAnton()
{
Graph g = new Graph();
FileLoader.Load(g, "anton.rdf");
SparqlQueryParser parser = new SparqlQueryParser();
SparqlQuery query = parser.ParseFromFile("anton.rq");
Object results = g.ExecuteQuery(query);
if (results is SparqlResultSet)
{
SparqlResultSet rset = (SparqlResultSet)results;
foreach (SparqlResult r in rset)
{
Console.WriteLine(r);
}
Assert.AreEqual(1, rset.Count, "Should be exactly 1 result");
}
else
{
Assert.Fail("Query should have returned a Result Set");
}
}
示例12: Main
public static void Main(string[] args)
{
StreamWriter output = new StreamWriter("SPARQLTestSuite.txt");
Console.SetOut(output);
try
{
int testsPassed = 0;
int testsFailed = 0;
bool passed, passDesired;
SparqlQueryParser parser = new SparqlQueryParser();
parser.TraceTokeniser = true;
parser.DefaultBaseUri = TestSuiteURI;
output.WriteLine("## SPARQL Test Suite");
output.WriteLine();
String[] dirs = Directory.GetDirectories("sparqlparser_tests");
foreach (String dir in dirs)
{
if (dir.Contains(".svn"))
{
continue;
}
String[] files = Directory.GetFiles(dir);
foreach (String file in files)
{
if (!Path.GetExtension(file).Equals(".rq"))
{
continue;
}
passed = false;
passDesired = true;
output.WriteLine("## Testing " + file);
output.WriteLine("# Test Started at " + DateTime.Now.ToString(TestSuite.TestSuiteTimeFormat));
if (Path.GetFileNameWithoutExtension(file).Contains("bad"))
{
passDesired = false;
output.WriteLine("# Desired Result = Parsing Failed");
}
else
{
output.WriteLine("# Desired Result = Parses OK");
}
try
{
Debug.WriteLine("Testing file " + file);
SparqlQuery q = parser.ParseFromFile(file);
passed = true;
}
catch (IOException ioEx)
{
reportError(output, "IO Exception", ioEx);
}
catch (RdfParseException parseEx)
{
reportError(output, "Parsing Exception", parseEx);
}
catch (RdfException rdfEx)
{
reportError(output, "RDF Exception", rdfEx);
}
catch (Exception ex)
{
reportError(output, "Other Exception", ex);
}
finally
{
if (passed && passDesired)
{
//Passed and we wanted to Pass
testsPassed++;
output.WriteLine("# Result = Test Passed");
}
else if (!passed && passDesired)
{
//Failed when we should have Passed
testsFailed++;
output.WriteLine("# Result = Test Failed");
}
else if (passed && !passDesired)
{
//Passed when we should have Failed
testsFailed++;
output.WriteLine("# Result = Test Failed");
}
else
{
//Failed and we wanted to Fail
testsPassed++;
output.WriteLine("# Result = Test Passed");
}
//.........这里部分代码省略.........