Semweb4j/filesQueries/step1

From semanticweb.org

Jump to: navigation, search

Up | Next step

Contents

[edit] Step 1: Simple serialization

We will use simple serialization and prove it's working.

[edit] writing a model to a file

The Method Model.writeTo(Writer writer) is sufficent:

model.writeTo( new FileWriter("model.rdf") );

Note that any Writer instance can be used here, not only FileWriter.

[edit] reading a model from file

As above, use Model.readFrom(Reader reader):

model.readFrom( new FileReader("model.rdf") );

[edit] proving the serialization works

With

model.removeAll();

the model is empty. The assertion

assert model.size() == 0 : "model is empty after removal of all statements"; 

should always pass after this operation.

Assume we have done this and read a new model from file.

assert model.size() > 0 : "model contains statements after reading a serialized model";

will only throw an AssertionFailedException if the model inside our file was empty.

[edit] the complete program

Now we're putting the parts together and get a runnable Java application:

package org.ontoware.semweb4j.lessons.lesson2;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.ontoware.rdf2go.RDF2Go;
import org.ontoware.rdf2go.exception.ModelRuntimeException;
import org.ontoware.rdf2go.model.Model;
import org.ontoware.rdf2go.model.node.URI;

public class Step1 {

	private static Model model;
	private static final String FOAFNS = "http://xmlns.com/foaf/0.1/";
	
	private static void init() throws ModelRuntimeException {
		model = RDF2Go.getModelFactory().createModel();
		model.open();
	}
	
	public static void main(String[] args) throws ModelRuntimeException, IOException {

		init();

		// creating URIs
		URI max = model.createURI("http://xam.de/foaf.rdf.xml#i");
		URI currentProject = model.createURI(FOAFNS+"#term_currentProject");
		URI name = model.createURI(FOAFNS+"#term_name");
		URI semweb4j = model.createURI("http://semweb4j.org");

		// adding statements
		model.addStatement(max, currentProject, semweb4j);
		model.addStatement(max, name, "Max Völkel");
		
		// serializing model to model.rdf
		model.writeTo( new FileWriter("model.rdf") );
		
		// removing statements
		model.removeAll();
		
		// proving the model has no more statements
		assert model.size() == 0 : "model is empty after removal of all statements";
		model.dump();
		
		// reading model from model.rdf
		model.readFrom( new FileReader("model.rdf") );
		
		// proving the model has been read
		assert model.size() > 0 : "model contains statements after reading a serialized model";
		model.dump();

	}

}

You can get the code by downloading semweb4j also.

Personal tools