Skip to content
johan-montagnat edited this page Feb 23, 2016 · 3 revisions

Table of Contents generated with DocToc

First Program with Corese (command-line)

Below is an illustration of a simple application using the CORESE library to load an RDF graph and compute a SPARQL query. The example follows these steps:

  1. Creation of a maven project skeleton;
  2. Configuration of the project;
  3. Application compilation;
  4. Execution.

This example can also be found in the https://github.com/Wimmics/coreseLibTutorial project.

1. Creation of the maven Tutorial project

The following line creates a basic maven project.

> mvn archetype:generate -DgroupId=fr.inria.wimmics.corese.tutorial -DartifactId=coreseTutorial -DarchetypeArtifactId=maven-archetype-quickstart

In what follows, $TUTORIAL_ROOT stands for the directory where the project skeleton has been created. For example, in a Unix environment, one could set this variable as follows: TUTORIAL_ROOT=$PWD/coreseTutorial

2. Maven Dependency Configuration

In order to add the Corese engine to the application, add a dependency to kgtool to $TUTORIAL_ROOT/pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project ...>
...
    <dependency>
            <groupId>fr.inria.wimmics</groupId>
            <artifactId>kgtool</artifactId>
            <version>3.2.0</version>
            <exclusions>
                <exclusion>
                    <artifactId>jws</artifactId>
                    <groupId>edu.sussex.nlp</groupId>
                </exclusion>
            </exclusions>
        </dependency>
...
</project>

3. Application compilation

Application code

Edit the file coreseTutorial/src/main/java/fr/inria/coreseTutorial/App.java as follows:

package fr.inria.wimmics.corese.tutorial;
import fr.inria.acacia.corese.exceptions.EngineException;
import fr.inria.edelweiss.kgram.core.Mappings;
import fr.inria.edelweiss.kgraph.core.Graph;
import fr.inria.edelweiss.kgraph.query.QueryProcess;
import fr.inria.edelweiss.kgtool.load.Load;
import fr.inria.edelweiss.kgtool.print.ResultFormat;
import fr.inria.edelweiss.kgtool.print.TripleFormat;
/**
 * Hello world!
 *
 */
public class App
{
    public static void main( String[] args ) throws EngineException
    {
                Graph graph = Graph.create();
                Load ld = Load.create(graph);
                ld.load("human_2007_09_11.rdf");
                QueryProcess exec = QueryProcess.create(graph);
                String query = "select * where {?x ?p ?y}";
                Mappings map = exec.query(query);
                ResultFormat f1 = ResultFormat.create(map);
                System.out.println(f1);
                TripleFormat f2 = TripleFormat.create(graph);
                System.out.println(f2);
    }
}

Project build

In TUTORIAL_ROOT, run mvn install.

4. Execution

In TUTORIAL_ROOT:

  1. download the file http://corese.inria.fr/data/tutorial/human1.rdf.
  2. run mvn exec:java -Dexec.mainClass="fr.inria.wimmics.corese.tutorial.App".

You should obtain the list of all the triples contained in the human1.rdf file:

$ mvn exec:java -Dexec.mainClass="fr.inria.wimmics.corese.tutorial.App"
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Corese Library Tutorial 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ corese-library-tutorial ---
<?xml version="1.0" ?>
<sparql xmlns='http://www.w3.org/2005/sparql-results#'>
<head>
<variable name='x'/>
<variable name='y'/>
<variable name='p'/>
</head>
<results>
<result>
<binding name='x'><uri>http://www.inria.fr/2015/humans-instances#John</uri></binding>
<binding name='y'><literal datatype='http://www.w3.org/2001/XMLSchema#integer'>37</literal></binding>
<binding name='p'><uri>http://www.inria.fr/2015/humans#age</uri></binding>
</result>

...

<result>
<binding name='x'><uri>http://www.inria.fr/2015/humans-instances#Jennifer</uri></binding>
<binding name='y'><uri>http://www.inria.fr/2015/humans#Woman</uri></binding>
<binding name='p'><uri>http://www.w3.org/1999/02/22-rdf-syntax-ns#type</uri></binding>
</result>
</results>
</sparql>

@prefix rq: <http://ns.inria.fr/sparql-function/> .
@prefix dt: <http://ns.inria.fr/sparql-datatype/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix sp: <http://spinrdf.org/sp#> .
@prefix cos: <http://www.inria.fr/acacia/corese#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix us: <http://ns.inria.fr/sparql-extension/user/> .
@prefix cw: <http://ns.inria.fr/edelweiss/2010/kgram/construct/> .
@prefix xt: <http://ns.inria.fr/sparql-extension/> .
@prefix cs: <http://ns.inria.fr/sparql-custom/> .
@prefix ns1: <http://www.inria.fr/2015/humans#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix kg: <http://ns.inria.fr/edelweiss/2010/kgram/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix eng: <http://ns.inria.fr/edelweiss/2010/kgram/extension/> .
@prefix st: <http://ns.inria.fr/sparql-template/> .
@prefix pp: <http://ns.inria.fr/edelweiss/2010/kgram/pprinter/> .
@prefix fp: <ftp://ftp-sop.inria.fr/wimmics/soft/pprint/> .

<http://www.inria.fr/2015/humans-instances#Jack> ns1:hasChild <http://www.inria.fr/2015/humans-instances#Harry> ;
ns1:hasFriend <http://www.inria.fr/2015/humans-instances#Alice> ;
ns1:name "Jack" ;
rdf:type ns1:Man .

...

<http://www.inria.fr/2015/humans-instances#David> ns1:hasFriend <http://www.inria.fr/2015/humans-instances#Gaston> ;
ns1:name "David" ;
rdf:type ns1:Person ;
rdf:type ns1:Researcher .

<http://www.inria.fr/2015/humans-instances#Jennifer> ns1:hasSpouse <http://www.inria.fr/2015/humans-instances#John> ;
ns1:name "Jennifer" ;
rdf:type ns1:Woman .


[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.051 s
[INFO] Finished at: 2016-02-19T14:50:13+01:00
[INFO] Final Memory: 14M/156M
[INFO] ------------------------------------------------------------------------