带过滤的SPARQL查询

时间:2012-08-02 21:37:27

标签: regex sparql dotnetrdf

我想列出所有以某些字母开头的药物来填写自动填充文本框。这是代码

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();


}

但是,如果我尝试使用例如A,那么已经有一对以A开头的情侣

VDS.RDF.Parsing.RdfParseException: [Line 1 Column 263] The value &#39;A&#39; is not valid as a QName


at VDS.RDF.Parsing.Tokens.SparqlTokeniser.TryGetQNameOrKeyword()

at VDS.RDF.Parsing.Tokens.SparqlTokeniser.GetNextToken()    在VDS.RDF.Parsing.Tokens.TokenQueue.InitialiseBuffer()    在VDS.RDF.Parsing.SparqlQueryParser.ParseInternal(SparqlQueryParserContext context)    在VDS.RDF.Parsing.SparqlQueryParser.ParseInternal(TextReader输入)    at VDS.RDF.Parsing.SparqlQueryParser.ParseFromString(String queryString)    位于d:\ Suggest \ App_Code \ SuggestWebService.cs中的SuggestWebService.GetCompletionList(String prefixText):第57行

2 个答案:

答案 0 :(得分:3)

在查询字符串中添加换行符以使错误消息更好。

没有SPARQL引号
regex(str(?x),"+tmp+")

尝试:

regex(str(?x),'"+tmp+"')

将单引号放入SPARQL。请注意tmp中的任何引号。

答案 1 :(得分:0)

我已经以这种方式更改了我的代码,因此它对我有用

        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+"\")"
                +"}";