Knowledge Reasoning Machine Reproduction Based on jena

Keywords: MySQL Apache Java github

Knowledge inference machine reappearing

Because csdn grammar support is different from github, you are welcome to visit the GitHub version of this article: https://github.com/JimXiongGM/KnowledgeBasedSearch/blob/master/ knowledge reasoning machine reappears.md

Catalog

This paper reproduces the knowledge inference machine based on jena and collate the detailed process. The main problem encountered is that there are some configuration changes after the update of the jena version.

The main reference materials are by simmerchan and data

Put the following file in / root/xiazai/. Click to enter the file download page.

  1. KG-demo-for-movie-master.zip
  2. d2rq-0.8.1.tar.gz
  3. mysql-connector-java-5.1.47.tar.gz
  4. apache-jena-3.12.0.tar.gz
  5. apache-jena-fuseki-3.12.0.tar.gz

mysql 8.0 preparation

Installation section see MySQL 8.0 Environment Construction.

cd /root/xiazai;
unzip KG-demo-for-movie-master.zip;
# Move to this path, otherwise The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
cp ./KG-demo-for-movie-master/data/kg_demo_movie.sql /var/lib/mysql-files/;
mysql -u root -p
//Input password

Enter mysql shell

create database kg_demo_movie;
use kg_demo_movie;
source /var/lib/mysql-files/kg_demo_movie.sql;
-- View results
show databases;
exit

The data is ready.

Installation and Use of d2rq Tools

This is a tool that automatically converts relational databases into triples, just download the installation package and mysql connector. Run the following command.

Note that MySQL 5.0 + jar packages are used instead of 8.0 + ones, otherwise errors will be reported.

In addition, the connection password in kg_demo_movie_mapping.ttl needs to be modified by itself. This article directly uses the TTL file provided by the original author.

# Require java version >= 1.5
java -version;
cd /root/xiazai;
tar -zxvf d2rq-0.8.1.tar.gz -C /opt;
tar -zvxf mysql-connector-java-5.1.47.tar.gz;
cp mysql-connector-java-5.1.47/mysql-connector-java-5.1.47-bin.jar /opt/d2rq-0.8.1/lib/;

cd /opt/d2rq-0.8.1;
# Be careful! You need to modify your mysql connection password by yourself
cp /root/xiazai/KG-demo-for-movie-master/kg_demo_movie_mapping.ttl ./
# Memorandum: Generate. ttl files using the tools that come with them
# ./generate-mapping -u root -p root -o test.ttl --verbose jdbc:mysql:///fzzk_use?useUnicode=true&characterEncoding=utf8&useSSL=false
./dump-rdf -o kg_demo_movie.nt kg_demo_movie_mapping.ttl;
# test
nohup ./d2r-server kg_demo_movie_mapping.ttl &
./d2r-query kg_demo_movie_mapping.ttl "SELECT * { ?s <http://www.kgdemo.com#hasActedIn> ?o } LIMIT 10";

Interestingly, the tool provides a web UI for SPARQL queries and can be used by opening http://master:2020/snorql.

Apache jena + jena-fuseki

cd /root/xiazai;
tar -zxvf apache-jena-3.12.0.tar.gz -C /opt/;
tar -zxvf apache-jena-fuseki-3.12.0.tar.gz -C /opt/;

# Load nt files using jena
cd /opt/apache-jena-3.12.0;
mkdir -p /data/jena/kg_demo_movie_database/;
./bin/tdbloader --loc=/data/jena/kg_demo_movie_database /opt/d2rq-0.8.1/kg_demo_movie.nt;

# Initialize jena-fuseki
cd /opt/apache-jena-fuseki-3.12.0/;
./fuseki-server
# ctrl+z automatically creates run folders after running
# Open remote connection privileges
sed -i 's/= localhostFilter/= anon/g' ./run/shiro.ini;

# A key. Update the original author's configuration file
# Notably, there is no need for owl files exported from protege by the original author
echo '@prefix fuseki:  <http://jena.apache.org/fuseki#> .
@prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
@prefix tdb:     <http://jena.hpl.hp.com/2008/tdb#> .
@prefix ja:      <http://jena.hpl.hp.com/2005/11/Assembler#> .
@prefix :        <#> .

<#service1>  rdf:type fuseki:Service ;
    fuseki:name                       "kg_demo" ;       # http://host:port/tdb
    fuseki:serviceQuery               "sparql" ;    # SPARQL query service
    fuseki:serviceQuery               "query" ;    # SPARQL query service (alt name)
    fuseki:serviceUpdate              "update" ;   # SPARQL update service
    fuseki:serviceUpload              "upload" ;   # Non-SPARQL upload service
    fuseki:serviceReadWriteGraphStore "data" ;     # SPARQL Graph store protocol (read and write)
    # A separate read-only graph store endpoint:
    fuseki:serviceReadGraphStore      "get" ;      # SPARQL Graph store protocol (read only)
    fuseki:dataset           <#dataset> ;
    .

<#dataset> rdf:type ja:RDFDataset ;
    ja:defaultGraph <#modelInf> ;
    .

<#modelInf> rdf:type ja:InfModel ;
    ja:reasoner [
        ja:reasonerURL <http://jena.hpl.hp.com/2003/GenericRuleReasoner>;
        # ubuntu
        ja:rulesFrom <file:///opt/apache-jena-fuseki-3.12.0/run/databases/rules.ttl>;
    ];
    ja:baseModel <#g> ;
    .

<#g> rdf:type tdb:GraphTDB ;
    # ubuntu
    tdb:location "/data/jena/kg_demo_movie_database" ;
    tdb:unionDefaultGraph true ;' > ./run/configuration/fuseki_conf.ttl

# A key. Update the original author's reasoning file
echo '@prefix : <http://www.kgdemo.com#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

[ruleComedian: (?p :hasActedIn ?m), (?m :hasGenre ?g), (?g :genreName "comedy") -> (?p rdf:type :Comedian)]
[ruleInverse: (?p :hasActedIn ?m) -> (?m :hasActor ?p)]
' > ./run/databases/rules.ttl;

# Start directly without parameters.
./fuseki-server
# Background boot
# nohup ./fuseki-server >> /logs/jena_fuseki.log &

Open http://localhost:3030 and enter a command to query the comedian:

PREFIX : <http://www.kgdemo.com#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT ?n WHERE {
?x rdf:type :Comedian.
?x :personName ?n.
}
limit 10

Posted by cowfish on Sat, 05 Oct 2019 17:32:53 -0700