diff --git a/Official_jena_examples/ExampleAPI_01.java b/Official_jena_examples/ExampleAPI_01.java new file mode 100644 index 0000000..9fdcc6e --- /dev/null +++ b/Official_jena_examples/ExampleAPI_01.java @@ -0,0 +1,67 @@ +/** + * 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. + */ + +package Official_jena_examples; + +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.RDFNode; +import com.hp.hpl.jena.rdf.model.Resource; +import com.hp.hpl.jena.rdf.model.Statement; +import com.hp.hpl.jena.rdf.model.StmtIterator; +import com.hp.hpl.jena.util.FileManager; + +public class ExampleAPI_01 { + + public static void main(String[] args) { + FileManager.get().addLocatorClassLoader(ExampleAPI_01.class.getClassLoader()); + Model model = FileManager.get().loadModel("data/data.ttl", null, "TURTLE"); + + StmtIterator iter = model.listStatements(); + try { + while ( iter.hasNext() ) { + Statement stmt = iter.next(); + + Resource s = stmt.getSubject(); + Resource p = stmt.getPredicate(); + RDFNode o = stmt.getObject(); + + if ( s.isURIResource() ) { + System.out.print("URI"); + } else if ( s.isAnon() ) { + System.out.print("blank"); + } + + if ( p.isURIResource() ) + System.out.print(" URI "); + + if ( o.isURIResource() ) { + System.out.print("URI"); + } else if ( o.isAnon() ) { + System.out.print("blank"); + } else if ( o.isLiteral() ) { + System.out.print("literal"); + } + + System.out.println(); + } + } finally { + if ( iter != null ) iter.close(); + } + } + +} diff --git a/Official_jena_examples/ExampleAPI_02.java b/Official_jena_examples/ExampleAPI_02.java new file mode 100644 index 0000000..330e65f --- /dev/null +++ b/Official_jena_examples/ExampleAPI_02.java @@ -0,0 +1,38 @@ +/** + * 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. + */ + +package Official_jena_examples; + +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.sparql.vocabulary.FOAF; + +public class ExampleAPI_02 { + + public static void main(String[] args) { + Model model = ModelFactory.createDefaultModel(); + + model.createResource("http://example.org/alice", FOAF.Person) + .addProperty(FOAF.name, "Alice") + .addProperty(FOAF.mbox, model.createResource("mailto:alice@example.org")) + .addProperty(FOAF.knows, model.createResource("http://example.org/bob")); + + model.write(System.out, "TURTLE"); + } + +} diff --git a/Official_jena_examples/ExampleAPI_03.java b/Official_jena_examples/ExampleAPI_03.java new file mode 100644 index 0000000..4bb6149 --- /dev/null +++ b/Official_jena_examples/ExampleAPI_03.java @@ -0,0 +1,44 @@ +/** + * 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. + */ + +package Official_jena_examples; + +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.rdf.model.Resource; +import com.hp.hpl.jena.rdf.model.ResourceFactory; +import com.hp.hpl.jena.sparql.vocabulary.FOAF; +import com.hp.hpl.jena.vocabulary.RDF; + +public class ExampleAPI_03 { + + public static void main(String[] args) { + Model model = ModelFactory.createDefaultModel(); + + Resource alice = ResourceFactory.createResource("http://example.org/alice"); + + Resource bob = ResourceFactory.createResource("http://example.org/bob"); + model.add (alice, RDF.type, FOAF.Person); + model.add (alice, FOAF.name, "Alice"); + model.add (alice, FOAF.mbox, ResourceFactory.createResource("mailto:alice@example.org")); + model.add (alice, FOAF.knows, bob); + + model.write(System.out, "TURTLE"); + } + +} diff --git a/Official_jena_examples/ExampleAPI_04.java b/Official_jena_examples/ExampleAPI_04.java new file mode 100644 index 0000000..14cba41 --- /dev/null +++ b/Official_jena_examples/ExampleAPI_04.java @@ -0,0 +1,51 @@ +/** + * 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. + */ + +package Official_jena_examples; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.util.FileManager; + +public class ExampleAPI_04 { + + public static void main(String[] args) throws IOException { + FileManager.get().addLocatorClassLoader(ExampleAPI_04.class.getClassLoader()); + InputStream in = FileManager.get().open("data/data2.ttl"); + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + String line = null; + Model all = ModelFactory.createDefaultModel(); + while ( ( line = reader.readLine() ) != null ) { + ByteArrayInputStream bais = new ByteArrayInputStream(line.getBytes()); + Model model = ModelFactory.createDefaultModel(); + model.read(bais, null, "TURTLE"); + model.write(System.out, "TURTLE"); + all.add(model); + all.setNsPrefixes(model.getNsPrefixMap()); + System.out.println("----------------"); + } + all.write(System.out, "TURTLE"); + } + +} diff --git a/Official_jena_examples/ExampleDataTypes_01.java b/Official_jena_examples/ExampleDataTypes_01.java new file mode 100644 index 0000000..5a2fffd --- /dev/null +++ b/Official_jena_examples/ExampleDataTypes_01.java @@ -0,0 +1,70 @@ +/** + * 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. + */ + +package Official_jena_examples; + +import com.hp.hpl.jena.datatypes.RDFDatatype; +import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; +import com.hp.hpl.jena.rdf.model.Literal; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.rdf.model.Property; +import com.hp.hpl.jena.rdf.model.Resource; +import com.hp.hpl.jena.rdf.model.ResourceFactory; + +public class ExampleDataTypes_01 { + + private static final String BASE = "http://example.org/"; + + public static void main(String[] args) { + Model model = ModelFactory.createDefaultModel(); + + Resource subject = r("s"); + + model.addLiteral (subject, p("p1"), 10); + model.addLiteral (subject, p("p2"), 0.5); + model.addLiteral (subject, p("p3"), (float)0.5); + model.addLiteral (subject, p("p4"), l(20)); + model.addLiteral (subject, p("p5"), l(0.99)); + model.addLiteral (subject, p("p6"), true); + model.add (subject, p("p7"), l("2012-03-11", XSDDatatype.XSDdate)); + model.add (subject, p("p8"), l("P2Y", XSDDatatype.XSDduration)); + + model.setNsPrefix("example", BASE); + model.setNsPrefix("xsd", "http://www.w3.org/2001/XMLSchema#"); + + model.write(System.out, "TURTLE"); + } + + private static Resource r ( String localname ) { + return ResourceFactory.createResource ( BASE + localname ); + } + + private static Property p ( String localname ) { + return ResourceFactory.createProperty ( BASE, localname ); + } + + private static Literal l ( Object value ) { + return ResourceFactory.createTypedLiteral ( value ); + } + + private static Literal l ( String lexicalform, RDFDatatype datatype ) { + return ResourceFactory.createTypedLiteral ( lexicalform, datatype ); + } + +} diff --git a/Official_jena_examples/ExampleDataTypes_02.java b/Official_jena_examples/ExampleDataTypes_02.java new file mode 100644 index 0000000..fd90b71 --- /dev/null +++ b/Official_jena_examples/ExampleDataTypes_02.java @@ -0,0 +1,177 @@ +/** + * 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. + */ + +package Official_jena_examples; + +import com.hp.hpl.jena.datatypes.BaseDatatype; +import com.hp.hpl.jena.datatypes.DatatypeFormatException; +import com.hp.hpl.jena.datatypes.RDFDatatype; +import com.hp.hpl.jena.datatypes.TypeMapper; +import com.hp.hpl.jena.query.Query; +import com.hp.hpl.jena.query.QueryExecution; +import com.hp.hpl.jena.query.QueryExecutionFactory; +import com.hp.hpl.jena.query.QueryFactory; +import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.query.ResultSetFormatter; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.rdf.model.ResourceFactory; +import com.hp.hpl.jena.vocabulary.RDF; + +public class ExampleDataTypes_02 { + + public static void main(String[] args) { + // Register custom datatypes + RDFDatatype celsius = TemperatureCelsius.get(); + RDFDatatype fahrenheit = TemperatureFahrenheit.get(); + RDFDatatype kelvin = TemperatureKelvin.get(); + RDFDatatype rankine = TemperatureRankine.get(); + TypeMapper.getInstance().registerDatatype(celsius); + TypeMapper.getInstance().registerDatatype(fahrenheit); + TypeMapper.getInstance().registerDatatype(kelvin); + TypeMapper.getInstance().registerDatatype(rankine); + + // Data + Model model = ModelFactory.createDefaultModel(); + model.add(ResourceFactory.createResource("x1"), RDF.value, model.createTypedLiteral("25", celsius)); + model.add(ResourceFactory.createResource("x2"), RDF.value, model.createTypedLiteral("15", celsius)); + model.add(ResourceFactory.createResource("x3"), RDF.value, model.createTypedLiteral("25", fahrenheit)); + model.add(ResourceFactory.createResource("x4"), RDF.value, model.createTypedLiteral("25", kelvin)); + model.add(ResourceFactory.createResource("x5"), RDF.value, model.createTypedLiteral("25", rankine)); + System.out.println("\n---- Data ----"); + model.write(System.out, "N-TRIPLES"); + + // Query and ORDER BY temperature + String queryString = + "PREFIX java: \n" + + "PREFIX rdf: \n" + + "SELECT * WHERE {\n" + + " ?s rdf:value ?temperature .\n" + + "}\n" + + "ORDER BY java:temperature( ?temperature )"; + System.out.println("\n---- Query ----"); + System.out.println(queryString); + + Query query = QueryFactory.create(queryString); + QueryExecution qexec = QueryExecutionFactory.create(query, model); + try { + ResultSet results = qexec.execSelect(); + System.out.println("\n---- Results ----"); + System.out.println(ResultSetFormatter.asText(results)); + } finally { + qexec.close(); + } + } + +} + +abstract class AbstractTemperatureType extends BaseDatatype { + + public AbstractTemperatureType(String uri) { + super(uri); + } + + @Override + public Class getJavaClass() { + return Double.class; + } + + @Override + public String unparse(Object value) { + return value.toString(); + } + + @Override + public Object parse(String lexicalForm) throws DatatypeFormatException { + try { + return new Double(lexicalForm); + } catch (NumberFormatException ex) { + throw new DatatypeFormatException(lexicalForm, this, ex.getMessage()); + } + } + + @Override public abstract String toString(); + +} + +class TemperatureCelsius extends AbstractTemperatureType { + + private static TemperatureCelsius datatype = new TemperatureCelsius(); + public static TemperatureCelsius get() { return datatype; } + + public TemperatureCelsius() { + super("http://jena.apache.org/datatypes/temperature/celsius"); + } + + @Override + public String toString() { + return "Temperature in °C"; + } + +} + +class TemperatureFahrenheit extends AbstractTemperatureType { + + private static TemperatureFahrenheit datatype = new TemperatureFahrenheit(); + public static TemperatureFahrenheit get() { return datatype; } + + + public TemperatureFahrenheit() { + super("http://jena.apache.org/datatypes/temperature/fahrenheit"); + } + + @Override + public String toString() { + return "Temperature in °F"; + } + +} + +class TemperatureKelvin extends AbstractTemperatureType { + + private static TemperatureKelvin datatype = new TemperatureKelvin(); + public static TemperatureKelvin get() { return datatype; } + + + public TemperatureKelvin() { + super("http://jena.apache.org/datatypes/temperature/kelvin"); + } + + @Override + public String toString() { + return "Temperature in K"; + } + +} + +class TemperatureRankine extends AbstractTemperatureType { + + private static TemperatureRankine datatype = new TemperatureRankine(); + public static TemperatureRankine get() { return datatype; } + + + public TemperatureRankine() { + super("http://jena.apache.org/datatypes/temperature/rankine"); + } + + @Override + public String toString() { + return "Temperature in °R"; + } + +}