当前位置:网站首页>Jena default inference query based on OWL
Jena default inference query based on OWL
2022-07-01 06:40:00 【Cold tea ice】
Catalog
Some background
adopt Jena Of API, We can RDF Of .nt Import the file into the corresponding TDB In storage , Then use the relevant API Make a triple query , But at this time, the query can only query the triple data in the storage , There is no inference .
This article About Jena My reasoning is quite clear .
This article It's the foundation , This paper mainly introduces RDF/RDFS/OWL The relationship between the past life and this life , Only by understanding these , In order to better understand Jena The reasoning of .
Simply speaking ,RDF Is a specification for the storage of actual triple data , But this can only be the specification of data organization , Cannot describe data , So there's... Again RDFS,RDFS We can simply understand it as a description language ; In short , It can be simply understood as :RDF It is the actual data in the database ,RDFS Is a description of the database table structure . however RDFS The ability to express relationships is still insufficient , And then it came OWL,OWL yes RDFS To complement and perfect .
OWL File is also what we often call ontology description file , The ontology of triples 、 A document that describes a relationship . This ontology describes , combination API We can carry out reasoning query .
The first article on reasoning is actually a demonstration of reasoning , But it's based on Fuseki To achieve , adopt Fuseki Configure related configuration files , Turn on the reasoner , Appoint TDB Storage location , And then query , We can infer and query the actual nonexistent triple data .
My key record is that I passed API An example of a code based query .
Data import
public static void loadRdfData(){
String directory = "D:\\jena\\test" ;
String ntFile = "D:\\kg_demo_movie.nt";
Dataset dataset = TDBFactory.createDataset(directory);
Model model = dataset.getNamedModel("kgMovie");
RDFDataMgr.read(model,ntFile);
checkModel(dataset);
dataset.close();
}
public static void checkModel(Dataset dataset){
Iterator<String> names = dataset.listNames();
String name = null;
while (names.hasNext())
{
name = names.next();
System.out.println(name);
}
}
public static void exampleQuery(Model model){
String sparqlQueryString = "PREFIX : <http://www.kgdemo.com#> \n" + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n" + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" + "SELECT ?x ?p ?o WHERE {\n" + "?x :movieTitle ' Kung fu '.\n" + "?x ?p ?o.\n" + "}" ;
Query query = QueryFactory.create(sparqlQueryString) ;
QueryExecution qexec = QueryExecutionFactory.create(query, model) ;
try {
ResultSet results = qexec.execSelect() ;
for ( ; results.hasNext() ; )
{
QuerySolution soln = results.nextSolution() ;
System.out.println(soln.get("x")+" "+soln.get("p")+" "+soln.get("o"));
}
} finally { qexec.close() ; }
}among exampleQuery It's a public method , Encapsulates a query Inquire about , The following general query and inference query will use . Compared with ordinary query , The main thing is to start the inference engine .
Common query
public static void sampleSearch(){
String directory = "D:\\jena\\test" ;
Dataset dataset = TDBFactory.createDataset(directory);
Model model = dataset.getNamedModel("kgMovie");
exampleQuery(model);
dataset.close();
}The query results are as follows :

That's exactly what happened RDF The data in corresponds to each other .
Inference query
public static void reasonersSearch(){
String directory = "D:\\jena\\test" ;
String ttlFile = "D:\\movie_owl.ttl";
Dataset dataset = TDBFactory.createDataset(directory);
//RDF data Model
Model dataModel = dataset.getNamedModel("kgMovie");
//owl Ontology description schema
Model schema = RDFDataMgr.loadModel(ttlFile, Lang.TTL);
// Add inference engine
Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
reasoner = reasoner.bindSchema(schema);
// Creating models
InfModel infmodel = ModelFactory.createInfModel(reasoner, dataModel);
// Inquire about
exampleQuery(infmodel);
dataset.close();
}give the result as follows :

among hasActor It is found by reasoning , Because of existence Person--hasActedIn-Movie Such triple data , then OWL In the description rule of hasActedIn and hasActor The relationship between inverseOf, as follows :
### http://www.kgdemo.com#hasActedIn
:hasActedIn rdf:type owl:ObjectProperty ;
owl:inverseOf :hasActor ;
rdfs:domain :Person ;
rdfs:range :Movie .
### http://www.kgdemo.com#hasActor
:hasActor rdf:type owl:ObjectProperty ;
rdfs:domain :Movie ;
rdfs:range :Person .
### http://www.kgdemo.com#hasGenre
:hasGenre rdf:type owl:ObjectProperty ;
rdfs:domain :Movie ;
rdfs:range :Genre .therefore , Through reasoning query, you can also query the actor information owned by the film .
边栏推荐
猜你喜欢

脏读、幻读和不可重复读

Redis安装到Windows系统上的详细步骤

How to use SCI hub

C language course is provided with employee information management system (large operation)

Esp32 monitors the battery voltage with ULP when the battery is powered

关于变量是否线程安全的问题

清除过期缓存条目后可用空间仍不足 - 请考虑增加缓存的最大空间

Spark入门(一篇就够了)

【微信小程序】视图容器和基本内容组件

Student attendance system for C language course (big homework)
随机推荐
Requests module (requests)
Esp32 - ULP coprocessor reading Hall sensor in low power mode
Dirty reading, unreal reading and unrepeatable reading
C语言课设学生考勤系统(大作业)
Grain Mall - environment (p1-p27)
SQL learning notes nine connections 2
Postgraduate entrance examination directory link
C语言课设物业费管理系统(大作业)
C language course set up student elective course system (big homework)
[unity shader ablation effect _ case sharing]
数据库笔记
Student attendance system for C language course (big homework)
MySQL learning
Which securities company does qiniu school cooperate with? Is it safe to open an account?
TDB中多个model情况下使用fuseki查询
SQL学习笔记2
如何通过cdn方式使用阿里巴巴矢量图字体文件
[wechat applet] view container and basic content components
SQL statement
What are the functions of LAN monitoring software