Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions Official_jena_examples/ExampleAPI_01.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

}
38 changes: 38 additions & 0 deletions Official_jena_examples/ExampleAPI_02.java
Original file line number Diff line number Diff line change
@@ -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");
}

}
44 changes: 44 additions & 0 deletions Official_jena_examples/ExampleAPI_03.java
Original file line number Diff line number Diff line change
@@ -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");
}

}
51 changes: 51 additions & 0 deletions Official_jena_examples/ExampleAPI_04.java
Original file line number Diff line number Diff line change
@@ -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");
}

}
70 changes: 70 additions & 0 deletions Official_jena_examples/ExampleDataTypes_01.java
Original file line number Diff line number Diff line change
@@ -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 );
}

}
Loading