Reputation: 3466
I like to list all drugs that start with some letter to fill autocomplete text box.Here is the code
public string[] GetCompletionList(string prefixText)
{
string rdfDat = AppDomain.CurrentDomain.BaseDirectory + "DrugRDF.rdf";
List<string> list = new List<string>();
TripleStore store = new TripleStore();
Graph rdf = new Graph();
FileLoader.Load(rdf, rdfDat, new RdfXmlParser());
store.Add(rdf);
string tmp = "^" + prefixText;
string sparqlQuery = "PREFIX mojLek: <http://www.example.org/mojLek#>"
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
+ "SELECT ?x"
+ "WHERE {?h mojLek:ime ?x ."
+ "FILTER regex(str(?x),"+tmp+") }";
SparqlQueryParser sparqlParser = new SparqlQueryParser();
SparqlQuery query = sparqlParser.ParseFromString(sparqlQuery);
Object results = store.ExecuteQuery(query);
if (results is SparqlResultSet)
{
SparqlResultSet r = results as SparqlResultSet;
foreach (SparqlResult res in r)
{
list.Add(res["x"].ToString().ToLower());
}
}
return list.ToArray();
}
However if I try it with for example A there are already couples that starts with A I got this error
VDS.RDF.Parsing.RdfParseException: [Line 1 Column 263] The value 'A' is not valid as a QName
at VDS.RDF.Parsing.Tokens.SparqlTokeniser.TryGetQNameOrKeyword()
at VDS.RDF.Parsing.Tokens.SparqlTokeniser.GetNextToken() at VDS.RDF.Parsing.Tokens.TokenQueue.InitialiseBuffer() at VDS.RDF.Parsing.SparqlQueryParser.ParseInternal(SparqlQueryParserContext context) at VDS.RDF.Parsing.SparqlQueryParser.ParseInternal(TextReader input) at VDS.RDF.Parsing.SparqlQueryParser.ParseFromString(String queryString) at SuggestWebService.GetCompletionList(String prefixText) in d:\Suggest\App_Code\SuggestWebService.cs:line 57
Upvotes: 0
Views: 3527
Reputation: 16700
Put newlines in the query string to make the error messages better.
There are no SPARQL quotes at
regex(str(?x),"+tmp+")
Try:
regex(str(?x),'"+tmp+"')
which puts single quotes into the SPARQL. Be careful of any quotes in tmp
.
Upvotes: 3
Reputation: 3466
I have changed my code in this way so it worked for me
string tmp="^"+prefixText;
var query = "PREFIX mojLek: <http://www.example.org/mojLek#>"
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
+ "PREFIX fn: <http://www.w3.org/2005/xpath-functions#>"
+ "SELECT ?x ?h"
+ "WHERE {?h mojLek:ime ?x ."
+ "FILTER regex(?x,\""+tmp+"\")"
+"}";
Upvotes: 0