甜味RDF / simple-rdf
尽可能简单的 rdfInterface 实现方案
2.0.0
2024-02-13 12:23 UTC
Requires
- php: >=8.0
- sweetrdf/rdf-helpers: ^2
- sweetrdf/rdf-interface: ^2
- zozlak/rdf-constants: ^1.1
Requires (Dev)
Suggests
README
一个PHP实现的RDF库,实现了https://github.com/sweetrdf/rdfInterface接口。
目标是最简单、最简洁、最清晰的实现。性能并不是最重要的(请参考下面的性能章节)。
它可以作为测试其他库性能的基准,也可以用于测试rdfInterface实现的互操作性(例如,确保它们可以正确处理由其他库创建的rdfInterface\Term
对象)。
安装
- 获取Composer
- 运行
composer require sweetrdf/simple-rdf
- 运行
composer require sweetrdf/quick-rdf-io
以安装解析器和序列化器。
自动生成的文档
https://sweetrdf.github.io/simpleRdf/namespaces/simplerdf.html
它很不完整,但总比没有好。
包括RdfInterface文档,它解释了最重要的设计决策。
用法
(您也可以查看通用的rdfInterface 示例)
include 'vendor/autoload.php'; use simpleRdf\DataFactory as DF; $graph = new simpleRdf\Dataset(); $parser = new quickRdfIo\TriGParser(new simpleRdf\DataFactory()); $stream = fopen('pathToTurtleFile', 'r'); $graph->add($parser->parseStream($stream)); fclose($stream); // count edges in the graph echo count($graph); // go trough all edges in the graph foreach ($graph as $i) { echo "$i\n"; } // find all graph edges with a given subject echo $graph->copy(DF::quadTemplate(DF::namedNode('http://mySubject'))); // find all graph edges with a given predicate echo $graph->copy(DF::quadTemplate(null, DF::namedNode('http://myPredicate'))); // find all graph edges with a given object echo $graph->copy(DF::quadTemplate(null, null, DF::literal('value', 'en'))); // replace an edge in the graph $edge = DF::quad(DF::namedNode('http://edgeSubject'), DF::namedNode('http://edgePredicate'), DF::namedNode('http://edgeObject')); $graph[$edge] = $edge->withObject(DF::namedNode('http://anotherObject')); // find intersection with other graph $graph->copy($otherGraph); // immutable $graph->delete($otherGraph); // in-place // compute union with other graph $graph->union($otherGraph); // immutable $graph->add($otherGraph); // in-place // compute set difference with other graph $graph->copyExcept($otherGraph); // immutable $graph->delete($otherGraph); // in-place $serializer = new quickRdfIo\TurtleSerializer(); $stream = fopen('pathToOutputTurtleFile', 'w'); $serializer->serializeStream($stream, $graph); fclose($stream);
性能
simpleRdf\Dataset
类不应该用于处理大量的四元组。
添加四元组的计算复杂度为O(N)
,其中N
是数据集中的四元组数量。这意味着添加n
个四元组的计算复杂度为O(n!)
(即,随着n
的增加,它会迅速失控)。
仅展示一些结果
您应该明白这个意思,对吧?
其他操作的性能也不佳,例如,所有搜索和在位删除的复杂度均为O(N)
,而所有返回数据集新副本的方法的复杂度均为O(N) + O(n!)
(其中N
是数据集中的四元组数量,n
是返回数据集中的四元组数量)。
如果您正在寻找一个高性能的实现,请查看quickRdf。