Difference between revisions of "Java Examples"

From Info216
Line 24: Line 24:
 
}
 
}
 
}
 
}
</nowiki>
+
</nowiki>
  
 
==Lecture 2: RDF==
 
==Lecture 2: RDF==
Line 49: Line 49:
 
===Writing to file===
 
===Writing to file===
 
  <nowiki>
 
  <nowiki>
...
+
...
  
 
try {
 
try {
Line 57: Line 57:
 
}
 
}
  
...
+
...
</nowiki>
+
</nowiki>
  
 
===Contents of '''test.ttl'''===
 
===Contents of '''test.ttl'''===
Line 73: Line 73:
 
         <http://dbpedia.org/resource/ontology/populationEstimate>
 
         <http://dbpedia.org/resource/ontology/populationEstimate>
 
                 66644000.0 .
 
                 66644000.0 .
</nowiki>
+
</nowiki>
  
 
===Reading from file===
 
===Reading from file===
Line 98: Line 98:
 
}
 
}
 
}
 
}
</nowiki>
+
</nowiki>
  
 
===Reading from web resource===
 
===Reading from web resource===
Line 128: Line 128:
 
}
 
}
 
}
 
}
</nowiki>
+
</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 13:41, 19 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

HelloJena.java

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

 

Language-tagged literals

 

Typed literals

 

Looping through statements

 

Selecting statements

 

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.0 .

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);
	}
}

INFO216, UiB, Spring 2017, Prof. Andreas L. Opdahl (c)