gpt4 book ai didi

rdf - 如何仅从 Jena Ontology 模型中获取推断数据

转载 作者:行者123 更新时间:2023-12-01 00:59:11 25 4
gpt4 key购买 nike

尝试通过 Jena 界面使用 PelletReasoner 仅列出知识(新三元组)。我只想要应用 后生成的推断数据托盘推理机 信息模型 来自本体( OWL )。目前,我列出了所有个人 - 其中包含断言和推断的知识。

那么我怎样才能得到只有从猫头鹰推断知识。提前谢谢..

//Inference Logic//

com.hp.hpl.jena.reasoner.Reasoner reasoner = org.mindswap.pellet.jena.PelletReasonerFactory.theInstance().create();

Model infModel = ModelFactory.createInfModel(reasoner, ModelFactory.createDefaultModel());

OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, infModel);

try
{
inputStream = new FileInputStream(OWL_File);
}
catch (FileNotFoundException p)
{
p.printStackTrace();
}
model.read(inputStream, null, "RDF/XML");

最佳答案

下面的代码创建了一个简单的 OntModel 类 B 和 A,其中 B ⊑ A,而 i 是 B 类型的个体。我们可以推断,例如,i 是 A 的一个实例,我们将看到在结果中。

import org.mindswap.pellet.jena.PelletReasonerFactory;

import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.impl.StmtIteratorImpl;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.util.iterator.Filter;

public class JustTheDeductionsPlease {
public static void main(String[] args) {
// Create the base model. B is a subclass of A, and i is an instance of B.
String NS = "http://example.org/";
final OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM );
OntClass a = model.createClass( NS+"A" );
OntClass b = model.createClass( NS+"B" );
a.addSubClass( b );
model.createIndividual( NS+"i", b );

// Create the inference model.
InfModel pModel = ModelFactory.createInfModel( PelletReasonerFactory.theInstance().create(), model);

// An iterator over the statements of pModel that *aren't* in the base model.
ExtendedIterator<Statement> stmts = pModel.listStatements().filterDrop( new Filter<Statement>() {
@Override
public boolean accept(Statement o) {
return model.contains( o );
}
});

// For convenient printing, we create a model for the deductions.
// (If stmts were a StmtIterator, we could add it directly. It's not,
// so we end up creating a new StmtIteratorImpl, which is kludgey, but
// it's more efficient than converting the thing to a list.)
Model deductions = ModelFactory.createDefaultModel().add( new StmtIteratorImpl( stmts ));
deductions.write( System.out, "TTL" );
}
}

整个输出有点大,因为有很多纯粹基于 OWL 的结论。我们最清楚地看到纯粹推导结果的地方是在我们定义的类和个人中。请注意,例如,B 被列为其自身和 owl:Thing 的子类,但不是 A(因为它在原始模型中),并且 i 具有类型 owl:Thing 和 A,但不是 B(因为那也是在原始模型中):

<http://example.org/A>
<http://www.w3.org/2000/01/rdf-schema#subClassOf>
<http://www.w3.org/2002/07/owl#Thing> , <http://example.org/A> ;
<http://www.w3.org/2002/07/owl#disjointWith>
<http://www.w3.org/2002/07/owl#Nothing> ;
<http://www.w3.org/2002/07/owl#equivalentClass>
<http://example.org/A> .

<http://example.org/B>
<http://www.w3.org/2000/01/rdf-schema#subClassOf>
<http://www.w3.org/2002/07/owl#Thing> , <http://example.org/B> ;
<http://www.w3.org/2002/07/owl#disjointWith>
<http://www.w3.org/2002/07/owl#Nothing> ;
<http://www.w3.org/2002/07/owl#equivalentClass>
<http://example.org/B> .

<http://example.org/i>
a <http://www.w3.org/2002/07/owl#Thing> , <http://example.org/A> ;
<http://www.w3.org/2002/07/owl#sameAs>
<http://example.org/i> .

关于rdf - 如何仅从 Jena Ontology 模型中获取推断数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25055263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com