Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Add tutorial module (#23)
CLEREZZA-1071: Add tutorial module
  • Loading branch information
miador authored and Hasan committed Feb 23, 2022
1 parent 713c422 commit 78c7bba06cc104c7690db00354655da45f756622
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 0 deletions.
@@ -61,6 +61,7 @@
<module>dataset</module>
<module>api.utils</module>
<module>jaxrs.rdf.providers</module>
<module>tutorial</module>
</modules>

<profiles>
@@ -0,0 +1,6 @@
## Run an Example Program
To run an example program, e.g., Example01, invoke the exec-maven-plugin.

$ mvn compile

$ mvn exec:java -Dexec.mainClass="org.apache.clerezza.tutorial.Example01"
@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed
with this work for additional information regarding copyright
ownership. The ASF licenses this file to you under the Apache
License, Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of
the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the specific
language governing permissions and limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.clerezza.tutorial</groupId>
<artifactId>tutorial</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Clerezza - Tutorial</name>
<description>A set of tutorials to show Apache Clerezza functionalities</description>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<!--
<executions>
<execution>
<id>first-example</id>
<configuration>
<mainClass>org.apache.clerezza.tutorial.Example01</mainClass>
</configuration>
</execution>
<execution>
<id>second-example</id>
<configuration>
<mainClass>org.apache.clerezza.tutorial.Example02</mainClass>
</configuration>
</execution>
<execution>
<id>third-example</id>
<configuration>
<mainClass>org.apache.clerezza.tutorial.Example03</mainClass>
</configuration>
</execution>
</executions>
-->
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.clerezza</groupId>
<artifactId>api</artifactId>
<version>2.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.clerezza</groupId>
<artifactId>api-implementation</artifactId>
<version>2.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.clerezza</groupId>
<artifactId>representation</artifactId>
<version>2.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.wymiwyg.clerezza</groupId>
<artifactId>rdf.jena.parser</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.wymiwyg.clerezza</groupId>
<artifactId>rdf.jena.serializer</artifactId>
<version>2.0.3</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,41 @@
package org.apache.clerezza.tutorial;

import org.apache.clerezza.BlankNode;
import org.apache.clerezza.Graph;
import org.apache.clerezza.IRI;
import org.apache.clerezza.Triple;
import org.apache.clerezza.implementation.literal.PlainLiteralImpl;
import org.apache.clerezza.implementation.TripleImpl;
import org.apache.clerezza.implementation.in_memory.SimpleGraph;

import java.util.Iterator;

public class Example01 {

public static void main( String[] args ) {
BlankNode subject = new BlankNode();

IRI isA = new IRI( "http://clerezza.apache.org/2017/01/example#isA" );
IRI clerezzaUser = new IRI( "http://clerezza.apache.org/2017/01/example#ClerezzaUser" );

IRI hasFirstName = new IRI( "http://clerezza.apache.org/2017/01/example#hasFirstName" );
PlainLiteralImpl firstName = new PlainLiteralImpl( "Hasan" );

TripleImpl subjectType = new TripleImpl( subject, isA, clerezzaUser );
TripleImpl subjectFirstName = new TripleImpl( subject, hasFirstName, firstName );

Graph myGraph = new SimpleGraph();
myGraph.add( subjectType );
myGraph.add( subjectFirstName );

Iterator<Triple> iterator = myGraph.filter( null, null, null );
Triple triple;
while ( iterator.hasNext() ) {
triple = iterator.next();
System.out.println( triple.getSubject().toString() );
System.out.println( triple.getPredicate().toString() );
System.out.println( triple.getObject().toString() );
}
}

}
@@ -0,0 +1,42 @@
package org.apache.clerezza.tutorial;

import org.apache.clerezza.Graph;
import org.apache.clerezza.Triple;
import org.apache.clerezza.representation.Parser;
import org.apache.clerezza.representation.SupportedFormat;
import org.apache.clerezza.representation.UnsupportedFormatException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.Iterator;

public class Example02 {

private static final Logger logger = LoggerFactory.getLogger( Example02.class );

public static void main( String[] args ) {

InputStream inputStream = Example02.class.getResourceAsStream( "example02.ttl" );
Parser parser = Parser.getInstance();

try {
Graph graph = parser.parse( inputStream, SupportedFormat.TURTLE );

Iterator<Triple> iterator = graph.filter( null, null, null );
Triple triple;

while ( iterator.hasNext() ) {
triple = iterator.next();
logger.info( String.format( "%s %s %s",
triple.getSubject().toString(),
triple.getPredicate().toString(),
triple.getObject().toString()
) );
}
} catch ( UnsupportedFormatException ex ) {
logger.warn( String.format( "%s is not supported by the used parser", SupportedFormat.TURTLE ) );
}
}

}
@@ -0,0 +1,41 @@
package org.apache.clerezza.tutorial;

import org.apache.clerezza.Graph;
import org.apache.clerezza.representation.Parser;
import org.apache.clerezza.representation.Serializer;
import org.apache.clerezza.representation.SupportedFormat;
import org.apache.clerezza.representation.UnsupportedFormatException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;

public class Example03 {

private static final Logger logger = LoggerFactory.getLogger( Example03.class );

public static void main( String[] args ) {
InputStream inputStream = Example03.class.getResourceAsStream( "example03.ttl" );
Parser parser = Parser.getInstance();
Graph graph;
try {
graph = parser.parse( inputStream, SupportedFormat.TURTLE );
} catch ( UnsupportedFormatException ex ) {
logger.warn( String.format( "%s is not supported by the used parser", SupportedFormat.TURTLE ) );
return;
}

Serializer serializer = Serializer.getInstance();
try {
FileOutputStream outputStream = new FileOutputStream( "/tmp/example03.rdf" );
serializer.serialize( outputStream, graph, SupportedFormat.RDF_XML );
} catch ( FileNotFoundException ex ) {
logger.warn( ex.getMessage() );
} catch ( UnsupportedFormatException ex ) {
logger.warn( String.format( "%s is not supported by the used serializer", SupportedFormat.RDF_XML ) );
}
}

}
@@ -0,0 +1,3 @@
@prefix ex: <http://clerezza.apache.org/2017/01/example#> .
_:a ex:hasFirstName "Hasan" .
_:a ex:isA ex:ClerezzaUser .
@@ -0,0 +1,3 @@
@prefix ex: <http://clerezza.apache.org/2017/01/example#> .
_:a ex:hasFirstName "Hasan" .
_:a ex:isA ex:ClerezzaUser .

0 comments on commit 78c7bba

Please sign in to comment.