Difference between revisions of "Java Examples"

From Info216
Line 169: Line 169:
 
===Creating an RDFS model===
 
===Creating an RDFS model===
 
  <nowiki>
 
  <nowiki>
 
+
Model rdfModel = ModelFactory.createDefaultModel();
</nowiki>
+
InfModel rdfsModel = ModelFactory.createRDFSModel(rdfModel);</nowiki>
  
 
===RDFS entailment: subClassOf===
 
===RDFS entailment: subClassOf===
 
  <nowiki>
 
  <nowiki>
 +
String iriBase = "http://no.uib.infomedia.info216/";
 +
 +
Resource resUCB = rdfsModel.createResource(iriBase + "UCB");
 +
Resource resUniversity = rdfsModel.createResource(iriBase + "University");
 +
resUCB.addProperty(RDF.type, resUniversity);
  
 +
Resource resHEI = rdfsModel.createResource(iriBase + "HEI");
 +
resUniversity.addProperty(RDFS.subClassOf, resHEI);
 +
 +
rdfsModel.write(System.out, "TURTLE");
 
</nowiki>
 
</nowiki>
  
Line 181: Line 190:
 
===Adding namespace prefixes===
 
===Adding namespace prefixes===
 
  <nowiki>
 
  <nowiki>
 +
rdfsModel.setNsPrefix("", iriBase);
 +
rdfsModel.setNsPrefix("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
 +
rdfsModel.setNsPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");</nowiki>
  
</nowiki>
+
We can do this in a single call too, by first creating a map:
 +
<nowiki>
 +
rdfsModel.setNsPrefixes(new HashMap() {{
 +
put("", iriBase);
 +
put("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
 +
put("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
 +
}});</nowiki>
 +
(The "double-curly notation" '''{{ ... }}''' lets us add code to initialise the new HashMap object at construction, before we pass it as a parameter to setNsPrefixes.)
 +
 
 +
Instead of
 +
<nowiki>
 +
Resource resUCB = rdfsModel.createResource(iriBase + "UCB");</nowiki>
 +
we can now write
 +
<nowiki>
 +
Resource resUCB = rdfsModel.createResource(rdfsModel.getNsPrefix("") + "UCB");</nowiki>
  
 
===Outputting the RDFS axioms===
 
===Outputting the RDFS axioms===
 
  <nowiki>
 
  <nowiki>
 
+
ModelFactory
 +
.createRDFSModel(ModelFactory.createDefaultModel())
 +
.write(System.out, "TURTLE");
 
</nowiki>
 
</nowiki>
  
 
===Removing axioms from RDFS outputs===
 
===Removing axioms from RDFS outputs===
 +
Here, we write the triples in rdfsModel to the console, after eliminating all tripes that are axioms:
 
  <nowiki>
 
  <nowiki>
 
+
InfModel axiomModel = ModelFactory.createRDFSModel(ModelFactory.createDefaultModel());
</nowiki>
+
ModelFactory
 +
.createDefaultModel()
 +
.add(rdfsModel.listStatements().toList())
 +
.remove(axiomModel.listStatements().toList())
 +
.write(System.out, "TURTLE");</nowiki>
  
 
===RDFS entailment: subPropertyOf===
 
===RDFS entailment: subPropertyOf===
 
  <nowiki>
 
  <nowiki>
 
</nowiki>
 
 
===Chained RDFS entailment: subPropertyOf and domain===
 
<nowiki>
 
String iriBase = "http://no.uib.infomedia.info216/";
 
String iriDbpedia = "http://dbpedia.org/resource/";
 
 
Model rdfModel = ModelFactory.createDefaultModel();
 
InfModel rdfsModel = ModelFactory.createRDFSModel(rdfModel);
 
 
rdfsModel.setNsPrefix("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
 
rdfsModel.setNsPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
 
rdfsModel.setNsPrefix("", iriBase);
 
rdfsModel.setNsPrefix("dbpedia", iriDbpedia);
 
 
 
Resource resCadeTracy = rdfsModel.createResource(rdfsModel.getNsPrefixURI("") + "Cade_Tracy");
 
Resource resCadeTracy = rdfsModel.createResource(rdfsModel.getNsPrefixURI("") + "Cade_Tracy");
resCadeTracy.addLiteral(FOAF.name, "Cade Tracy");
 
 
Resource resUCB = rdfsModel.createResource(iriBase + "UCB");
 
 
Property propHasBScFrom = rdfsModel.createProperty(iriBase + "hasBScFrom");
 
Property propHasBScFrom = rdfsModel.createProperty(iriBase + "hasBScFrom");
 
 
resCadeTracy.addProperty(propHasBScFrom, resUCB);
 
resCadeTracy.addProperty(propHasBScFrom, resUCB);
 
Resource resUniversity = rdfsModel.createResource(iriBase + "University");
 
resUCB.addProperty(RDF.type, resUniversity);
 
 
Resource resHEI = rdfsModel.createResource(iriBase + "HEI");
 
resUniversity.addProperty(RDFS.subClassOf, resHEI);
 
  
 
Property propGraduatedFrom = rdfsModel.createProperty(iriBase + "graduatedFrom");
 
Property propGraduatedFrom = rdfsModel.createProperty(iriBase + "graduatedFrom");
 
propHasBScFrom.addProperty(RDFS.subPropertyOf, propGraduatedFrom);
 
propHasBScFrom.addProperty(RDFS.subPropertyOf, propGraduatedFrom);
  
 +
rdfsModel.write(System.out, "TURTLE");</nowiki>
 +
The output will show that Cade graduated from University of Califoria, Berkeley (UCB), even though we did not assert that explicitly.
 +
 +
===Chained RDFS entailment: subPropertyOf and domain===
 +
<nowiki>
 
propGraduatedFrom.addProperty(RDFS.domain, FOAF.Person);
 
propGraduatedFrom.addProperty(RDFS.domain, FOAF.Person);
 
 
InfModel axiomModel = ModelFactory.createRDFSModel(ModelFactory.createDefaultModel());
+
rdfsModel.write(System.out, "TURTLE");</nowiki>
axiomModel.write(System.out, "TURTLE");
+
The output will show that Cade is a FOAF person, even though we did not assert that explicitly.
+
 
System.out.println(rdfsModel.getReasoner().getClass().toString());
+
===The Reasoner object===
+
This outputs the name of the Reasoner's class:
ModelFactory
+
<nowiki>
.createDefaultModel()
+
System.out.println(rdfsModel.getReasoner().getClass().toString());</nowiki>
.add(rdfsModel.listStatements().toList())
 
.remove(axiomModel.listStatements().toList())
 
.write(System.out, "TURTLE");</nowiki>
 
  
 
<div class="credits" style="text-align: right; direction: ltr; margin-left: 1em;">''INFO216, UiB, Spring 2017, Prof. Andreas L. Opdahl (c)''</div>
 
<div class="credits" style="text-align: right; direction: ltr; margin-left: 1em;">''INFO216, UiB, Spring 2017, Prof. Andreas L. Opdahl (c)''</div>

Revision as of 14:17, 26 January 2017

Here are the code examples we have used in the live sessions during the lectures - along with a few additional ones.

Lecture 1: Java, Jena, and Eclipse

Hello Jena

package no.uib.sinoa.info216;

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.vocabulary.FOAF;

public class HelloJena {

	public static void main(String[] args) {
		
		Model model = ModelFactory.createDefaultModel();
		
		Resource andreas = model.createResource("http://no.uib.infomedia.info216.ex/CadeTracy");
		andreas.addLiteral(FOAF.name, "Cade Tracy");
		
		model.write(System.out, "TURTLE");
	}	
}

Lecture 2: RDF

Resource objects

package no.uib.infomedia.info216;

...

public class HelloJena {
	public static void main(String[] args) {
		String iriBase = "http://no.uib.infomedia.info216/";
		String iriDbpedia = "http://dbpedia.org/resource/";
		
		Model model = ModelFactory.createDefaultModel();
		
		Resource resCadeTracy = model.createResource(iriBase + "Cade_Tracy");
		resCadeTracy.addLiteral(FOAF.name, "Cade Tracy");
		
		Resource resCanada = model.createResource(iriDbpedia + "Canada");
		Resource resFrance = model.createResource(iriDbpedia + "France");
		Property propVisited = model.createProperty(iriBase + "visited");
		resCadeTracy.addProperty(propVisited, resCanada);
		resCadeTracy.addProperty(propVisited, resFrance);

		model.write(System.out, "TURTLE");
	}
}

Language-tagged literals

		resFrance.addProperty(RDFS.label, "Frankrike", "no");
		resFrance.addProperty(RDFS.label, "France", "en");
		resFrance.addProperty(RDFS.label, "Francia", "es");

Typed literals

		Property propPopEst = model.createProperty(iriDbpedia + "ontology/populationEstimate");
		resFrance.addProperty(propPopEst, "66644000", XSDDatatype.XSDinteger);

Looping through statements

		for (Statement stmt : model.listStatements().toList()) {
			System.out.println(stmt.toString());
		}

Selecting statements

		for (Statement stmt : model
				.listStatements((Resource)null, RDFS.label, (RDFNode)null)
				.toList()) {
			System.out.println("Subject:   " + stmt.getSubject().toString());
			System.out.println("Predicate: " + stmt.getPredicate().toString());
			System.out.println("Object:    " + stmt.getObject().toString());
		}

Using a selector

		for (Statement stmt : model
				.listStatements(new SimpleSelector() {
					public boolean test(Statement s) { 
						return (s.getPredicate().equals(FOAF.name)); 
					}
				})
				.toList()) {
			System.out.println(stmt.getObject().toString());
		}

Writing to file

		try {
			model.write(new FileOutputStream("test.ttl"), "TURTLE");
		} catch (Exception e) {
			// TODO: handle exception
		}

Contents of test.ttl

<http://no.uib.infomedia.info216/Cade_Tracy>
        <http://no.uib.infomedia.info216/visited>
                <http://dbpedia.org/resource/France> , <http://dbpedia.org/resource/Canada> ;
        <http://xmlns.com/foaf/0.1/name>
                "Cade Tracy" .

<http://dbpedia.org/resource/France>
        <http://www.w3.org/2000/01/rdf-schema#label>
                "Francia"@es , "France"@en , "Frankrike"@no ;
        <http://dbpedia.org/resource/ontology/populationEstimate>
                66644000 .

Reading from file

package no.uib.infomedia.sinoa.info216;

import java.io.FileInputStream;

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;

public class ReadJena {

	public static void main(String[] args) {
		Model model = ModelFactory.createDefaultModel();
		
		try {
			model.read(new FileInputStream("test.ttl"), "http://ex.org/", "TURTLE");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		model.write(System.out);
	}
}

Reading from web resource

package no.uib.infomedia.sinoa.info216;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;

public class HttpTest {

	public static void main(String[] args) {
		Model model = ModelFactory.createDefaultModel();
		
		try {
			URL url = new URL("http://people.uib.no/sinoa/test.ttl");   
			HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();  
			InputStream is = urlConnection.getInputStream();
			model.read(is, "http://ex.org/", "TURTLE");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		model.write(System.out);
	}
}

Lecture 3: RDFS

Creating an RDFS model

		Model rdfModel = ModelFactory.createDefaultModel();
		InfModel rdfsModel = ModelFactory.createRDFSModel(rdfModel);

RDFS entailment: subClassOf

		String iriBase = "http://no.uib.infomedia.info216/";

		Resource resUCB = rdfsModel.createResource(iriBase + "UCB");
		Resource resUniversity = rdfsModel.createResource(iriBase + "University");
		resUCB.addProperty(RDF.type, resUniversity);

		Resource resHEI = rdfsModel.createResource(iriBase + "HEI");
		resUniversity.addProperty(RDFS.subClassOf, resHEI);

		rdfsModel.write(System.out, "TURTLE");

The output will show that University of Califoria, Berkeley (UCB) is a Higher-Education Institution (HEI), even though we did not assert that explicitly.

Adding namespace prefixes

		rdfsModel.setNsPrefix("", iriBase);
		rdfsModel.setNsPrefix("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
		rdfsModel.setNsPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");

We can do this in a single call too, by first creating a map: rdfsModel.setNsPrefixes(new HashMap() {{ put("", iriBase); put("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); put("rdfs", "http://www.w3.org/2000/01/rdf-schema#"); }}); (The "double-curly notation" Template:... lets us add code to initialise the new HashMap object at construction, before we pass it as a parameter to setNsPrefixes.)

Instead of Resource resUCB = rdfsModel.createResource(iriBase + "UCB"); we can now write Resource resUCB = rdfsModel.createResource(rdfsModel.getNsPrefix("") + "UCB");

Outputting the RDFS axioms

		ModelFactory
			.createRDFSModel(ModelFactory.createDefaultModel())
			.write(System.out, "TURTLE");

Removing axioms from RDFS outputs

Here, we write the triples in rdfsModel to the console, after eliminating all tripes that are axioms:

		InfModel axiomModel = ModelFactory.createRDFSModel(ModelFactory.createDefaultModel());
		ModelFactory
			.createDefaultModel()
			.add(rdfsModel.listStatements().toList())
			.remove(axiomModel.listStatements().toList())
			.write(System.out, "TURTLE");

RDFS entailment: subPropertyOf

		Resource resCadeTracy = rdfsModel.createResource(rdfsModel.getNsPrefixURI("") + "Cade_Tracy");
		Property propHasBScFrom = rdfsModel.createProperty(iriBase + "hasBScFrom");
		resCadeTracy.addProperty(propHasBScFrom, resUCB);

		Property propGraduatedFrom = rdfsModel.createProperty(iriBase + "graduatedFrom");
		propHasBScFrom.addProperty(RDFS.subPropertyOf, propGraduatedFrom);

		rdfsModel.write(System.out, "TURTLE");

The output will show that Cade graduated from University of Califoria, Berkeley (UCB), even though we did not assert that explicitly.

Chained RDFS entailment: subPropertyOf and domain

		propGraduatedFrom.addProperty(RDFS.domain, FOAF.Person);
		
		rdfsModel.write(System.out, "TURTLE");

The output will show that Cade is a FOAF person, even though we did not assert that explicitly.

The Reasoner object

This outputs the name of the Reasoner's class:

		System.out.println(rdfsModel.getReasoner().getClass().toString());
INFO216, UiB, Spring 2017, Prof. Andreas L. Opdahl (c)