甜RDF / term-templates
一套用于rdfInterface的术语模板
2.0.2
2024-02-13 12:26 UTC
Requires
- php: >=8.0
- sweetrdf/rdf-interface: ^2
- zozlak/rdf-constants: ^1
Requires (Dev)
- phpstan/phpstan: ^1
- phpunit/phpunit: ^10
- sweetrdf/simple-rdf: ^2
README
提供
- 一套允许匹配所需的RDF命名节点、字面量和四元组的术语模板。主要用于作为
rdfInterface\Dataset
方法的$filter
参数。 - 方便地从
rdfInterface\Dataset
中提取单个术语及其值的方法。
四元组匹配类
termTemplates\QuadTemplate
termTemplates\PredicateTemplate
- 一个跳过主语的termTemplates\QuadTemplate
变体(方便用于过滤rdfInterface\DatasetNode
)
术语匹配类
(所有类都在termTemplate
命名空间中)
[1] 支持通过字面量的语言(使用==和any运算符)和数据类型(仅使用==运算符)进行过滤。
[2] 支持严格和非严格模式。在严格模式下,与非数字数据类型的字面量进行比较时,无论其值如何,都会返回false
。
其他类
termTemplates\NotTemplate
- 取消给定rdfInterface\TermCompare
对象上equals()
操作的结果。termTemplates\AnyOfTemplate
- 匹配与给定列表中的任何rdfInterface\TermCompare
对象相等的术语。可以是单个rdfInterface\Term
、单个值、rdfInterface\Term
数组或值数组。
安装
- 获取 Composer
- 运行
composer require sweetrdf/term-templates
自动生成的文档
https://sweetrdf.github.io/termTemplates/namespaces/termtemplates.html
它很不完整,但总比没有好。
还包括 RdfInterface 文档,其中解释了最重要的设计决策。
用法示例
备注
- 术语模板的比较不是对称的! 您应该始终调用
$termTemplate->equals($termToBeCompared)
而不是$termToBeCompared->equals($termTemplate)
。后者的结果总是返回 false。 - 有关使用术语模板与RDF数据集结合使用的示例,请参阅 此处。
- 要运行这些示例,需要一个RDF术语工厂。这里我们将使用来自 quickRdf 库的一个。您可以使用
composer require sweetrdf/quick-rdf
安装它。 - 在下面的示例中,比较运算符是通过指定相应的
termTemplates\ValueTemplate
类常量来指定的,但您也可以使用上面列出的相应常量值(例如>=
或regex
)。 - 如果您对冗长的代码感到厌烦,可以定义类别名,例如
use termTemplates\QuadTemplate as QT;
,use termTemplates\ValueTemplate as VT;
等。
字符串值比较
$df = new quickRdf\DataFactory(); $literals = [ $df::literal('Lorem ipsum', 'lat'), $df::namedNode('http://ipsum.dolor/sit#amet'), $df::literal('foo bar'), ]; // find all terms containing 'ipsum' // true, true, false $tmplt = new termTemplates\ValueTemplate('ipsum', termTemplates\ValueTemplate::CONTAINS); print_r(array_map(fn($x) => $tmplt->equals($x), $literals)); // find all literals containing 'ipsum' // true, false, false $tmplt = new termTemplates\LiteralTemplate('ipsum', termTemplates\ValueTemplate::CONTAINS); print_r(array_map(fn($x) => $tmplt->equals($x), $literals)); // find all named nodes containing 'ipsum' // false, true, false $tmplt = new termTemplates\NamedNodeTemplate('ipsum', termTemplates\ValueTemplate::CONTAINS); print_r(array_map(fn($x) => $tmplt->equals($x), $literals)); // find all literals in latin // true, false, false $tmplt = new termTemplates\LiteralTemplate(lang: 'lat'); print_r(array_map(fn($x) => $tmplt->equals($x), $literals)); // find all terms with string value lower than 'http' // true, false, true $tmplt = new termTemplates\LiteralTemplate('http', termTemplates\ValueTemplate::LOWER); print_r(array_map(fn($x) => $tmplt->equals($x), $literals)); // find all terms matching a 'Lorem|foo' regular expression // true, false, true $tmplt = new termTemplates\ValueTemplate('/Lorem|foo/', termTemplates\ValueTemplate::REGEX); print_r(array_map(fn($x) => $tmplt->equals($x), $literals)); // ValueTemplate, NamedNodeTemplate and LiteralTemplate can be passed multiple values // In such a case condition needs to be fulfilled on any value // true, true, false $tmplt = new termTemplates\ValueTemplate(['Lorem ipsum', 'http://ipsum.dolor/sit#amet']); print_r(array_map(fn($x) => $tmplt->equals($x), $literals));
数值比较
$df = new quickRdf\DataFactory(); $literals = [ $df->literal('2'), $df->literal(3, null, 'http://www.w3.org/2001/XMLSchema#int'), $df->literal('1foo'), $df->literal('2', null, 'http://www.w3.org/2001/XMLSchema#int'), $df->namedNode('http://ipsum.dolor/sit#amet'), ]; // find terms with a value of 2 // true, false, false, true, false $tmplt = new termTemplates\NumericTemplate(2); print_r(array_map(fn($x) => $tmplt->equals($x), $literals)); // find terms with a value of 2 and enforce an RDF numeric type // false, false, false, true, false $tmplt = new termTemplates\NumericTemplate(2, strict: true); print_r(array_map(fn($x) => $tmplt->equals($x), $literals)); // find terms with a value greate or equal than 3 // false, true, false, false, false $tmplt = new termTemplates\NumericTemplate(3, termTemplates\ValueTemplate::GREATER_EQUAL); print_r(array_map(fn($x) => $tmplt->equals($x), $literals)); // find all terms with numeric values // true, true, false, true, false $tmplt = new termTemplates\NumericTemplate(matchMode: termTemplates\ValueTemplate::ANY); print_r(array_map(fn($x) => $tmplt->equals($x), $literals)); // find all numeric terms with values not equal 2 // false, true, false, false, false $tmplt = new termTemplates\NumericTemplate(2, termTemplates\ValueTemplate::NOT_EQUALS); print_r(array_map(fn($x) => $tmplt->equals($x), $literals));
否定和聚合
$literals = [ $df->literal('2'), $df->literal(3, null, 'http://www.w3.org/2001/XMLSchema#int'), $df->literal('1foo'), $df->literal('2', null, 'http://www.w3.org/2001/XMLSchema#int'), $df->namedNode('http://ipsum.dolor/sit#amet'), ]; // find all terms which value is not equal 2 (also non-numeric and non-literal ones) // false, true, true, false, true $tmplt = new termTemplates\NumericTemplate(2); $tmplt = new termTemplates\NotTemplate($tmplt); print_r(array_map(fn($x) => $tmplt->equals($x), $literals)); // find all terms with numeric value of 2 and all named nodes // true, false, false, true, true $tmplt = new termTemplates\AnyOfTemplate([ new termTemplates\NumericTemplate(2), new termTemplates\NamedNodeTemplate() ]); print_r(array_map(fn($x) => $tmplt->equals($x), $literals));
四元组比较
$df = new quickRdf\DataFactory(); // find all quads with a literal value containing 'foo' $tmplt = new termTemplates\QuadTemplate(object: new termTemplates\LiteralTemplate('foo', termTemplates\ValueTemplate::CONTAINS)); // find all quads with a given subject within a given graph $tmplt = new termTemplates\QuadTemplate( subject: 'http://desired/subject', graph: 'http://desired/graph' ); // find all quads with a given predicate $tmplt = new termTemplates\QuadTemplate(predicate: 'http://desired/predicate'); //or $tmplt = new termTemplates\PredicateTemplate('http://desired/predicate'); // both QuadTemplate and PredicateTemplate support negation, e.g. // find all quads with subject different than 'http://unwanted/subject' $tmplt = new termTemplates\QuadTemplate('http://desired/predicate', negate: true);