如何从使用sparqllib完成的查询中获得SPARQL的ASK答案?

时间:2014-06-18 23:25:36

标签: php sparql

当我在Virtuoso SPARQL Query Editor执行时,SPARQL ASK对以下代码执行的true查询会返回<?php require_once('./sparqllib.php'); $db = sparql_connect('http://dbpedia.org/sparql'); $query = " ASK { ?book a dbpedia-owl:Book . ?book dbpprop:author ?author . ?book dbpprop:name ?bookname . ?author dbpprop:name ?authorname . FILTER regex(?bookname, 'A Tale of Two Cities', 'i') . FILTER regex(?authorname, 'Dickens', 'i') }"; $result = sparql_query($query); var_dump($result); 。同样的结果是我想要返回到我的脚本。我如何调整 sparqllib 使用?

var_dump()

object(sparql_result)[3] public 'rows' => array (size=0) empty public 'fields' => array (size=0) empty public 'db' => object(sparql_connection)[1] public 'db' => null public 'debug' => boolean false public 'errno' => null public 'error' => null public 'ns' => array (size=0) empty public 'params' => null public 'caps' => array (size=0) empty public 'caps_desc' => array (size=7) 'select' => string 'Basic SELECT' (length=12) 'constant_as' => string 'SELECT ("foo" AS ?bar)' (length=22) 'math_as' => string 'SELECT (2+3 AS ?bar)' (length=20) 'count' => string 'SELECT (COUNT(?a) AS ?n) ?b ... GROUP BY ?b' (length=43) 'max' => string 'SELECT (MAX(?a) AS ?n) ?b ... GROUP BY ?b' (length=41) 'sample' => string 'SELECT (SAMPLE(?a) AS ?n) ?b ... GROUP BY ?b' (length=44) 'load' => string 'LOAD <...>' (length=10) public 'caps_cache' => null public 'caps_anysubject' => null public 'endpoint' => string 'http://dbpedia.org/sparql' (length=25) public 'i' => int 0 的结果:

{{1}}

1 个答案:

答案 0 :(得分:3)

简答题(解决方法)

sparqllib.php似乎不支持处理ask查询的结果,所以你应该只使用

select * where {
    ?book a dbpedia-owl:Book .
    ?book dbpprop:author ?author .
    ?book dbpprop:name ?bookname .
    ?author dbpprop:name ?authorname .
    FILTER regex(?bookname, 'A Tale of Two Cities', 'i') .
    FILTER regex(?authorname, 'Dickens', 'i')
}
limit 1

并检查您是否得到了结果。

长答案(sparqllib不支持ask结果)

我认为sparqllib不支持select查询以外的查询。如果您查看sparqllib.php,可以看到查询的执行方式:

function query( $query, $timeout=null )
{   
    $prefixes = "";
    foreach( $this->ns as $k=>$v )
    {
        $prefixes .= "PREFIX $k: <$v>\n";
    }
    $output = $this->dispatchQuery( $prefixes.$query, $timeout );
    if( $this->errno ) { return; }
    $parser = new xx_xml($output, 'contents');
    if( $parser->error() ) 
    { 
        $this->errno = -1; # to not clash with CURLOPT return; }
        $this->error = $parser->error();
        return;
    }
    return new sparql_result( $this, $parser->rows, $parser->fields );
}

dispatchQuery负责发送查询并返回结果。它在application/sparql-results+xml中询问它们,这就是XML解析器参与的原因。如果您查看XML解析代码,可以使用以下内容:

function startXML($parser, $name, $attr)    
{
    if( $name == "sparql" ) { $this->looks_legit = true; }
    if( $name == "result" )
    {
        $this->result = array();
    }
    if( $name == "binding" )
    {
        $this->part = $attr["name"];
    }
    if( $name == "uri" || $name == "bnode" )
    {
        $this->part_type = $name;
        $this->chars = "";
    }
    if( $name == "literal" )
    {
        $this->part_type = "literal";
        if( isset( $attr["datatype"] ) )
        {
            $this->part_datatype = $attr["datatype"];
        }
        if( isset( $attr["xml:lang"] ) )
        {
            $this->part_lang = $attr["xml:lang"];
        }
        $this->chars = "";
    }
    if( $name == "variable" )
    {
        $this->fields[] = $attr["name"];
    }
}

现在,如果您查看定义结果应该是什么的SPARQL Query Results XML Format (Second Edition),尤其是2.3.2. Boolean Results,我们会看到ASK查询的结果应该是

<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
  ...  head ...

  <boolean>true</boolean>

</sparql>

sparqllib.php不包含任何要查找名称为“boolean”的元素的内容。它只是为了处理select结果而设计的。也就是说,代码是根据LGPL许可的,并且根据文件头可以在GitHub上获得,所以如果你愿意,你应该能够分叉并添加ask支持:

###############################
# Christopher Gutteridge 2010
#  cjg@ecs.soton.ac.uk
#  LGPL License 
#  http://graphite.ecs.soton.ac.uk/sparqllib/
#  https://github.com/cgutteridge/PHP-SPARQL-Lib
###############################