import java.io.File;
import java.io.IOException;
import java.util.List;
import org.openrdf.model.Resource;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import org.openrdf.model.impl.URIImpl;
import org.openrdf.query.BindingSet;
import org.openrdf.query.MalformedQueryException;
import org.openrdf.query.QueryEvaluationException;
import org.openrdf.query.QueryLanguage;
import org.openrdf.query.TupleQuery;
import org.openrdf.query.TupleQueryResult;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.sail.SailRepository;
import org.openrdf.repository.sail.SailRepositoryConnection;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFParseException;
import org.openrdf.sail.inferencer.fc.ForwardChainingRDFSInferencer;
import org.openrdf.sail.memory.MemoryStore;
public class Sesame_Test {
/**
* @param args
* @throws RepositoryException
* @throws IOException
* @throws RDFParseException
* @throws QueryEvaluationException
* @throws MalformedQueryException
*/
public static void main(String[] args) throws RepositoryException,
RDFParseException, IOException, MalformedQueryException,
QueryEvaluationException {
MemoryStore ms = new MemoryStore();
ForwardChainingRDFSInferencer fci = new ForwardChainingRDFSInferencer(
ms);
SailRepository myRepository = new SailRepository(fci);
myRepository.initialize();
SailRepositoryConnection con = myRepository.getConnection();
if (true) {
{
Resource s = new URIImpl("http://xmlns.com/foaf/0.1/#Person");
URI p = new URIImpl(
"http://www.w3.org/2000/01/rdf-schema#subClassOf");
Value o = new URIImpl("http://xmlns.com/foaf/0.1/#Agent");
con.add(s, p, o);
}
{
Resource s = new URIImpl(
"http://knowledgebooks.com/ontology#KnowledgeEngineer");
URI p = new URIImpl(
"http://www.w3.org/2000/01/rdf-schema#subClassOf");
Value o = new URIImpl("http://xmlns.com/foaf/0.1/#Person");
con.add(s, p, o);
}
{
Resource s = new URIImpl("http://www.markwatson.com/index.rdf");
URI p = new URIImpl(
"http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
// The 'a' in Notation3 format means the
// 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' in URI;
Value o = new URIImpl(
"http://knowledgebooks.com/ontology#KnowledgeEngineer");
con.add(s, p, o);
}
} else {
File file = new File("rdf_files/class_example.n3");
con.add(file, null, RDFFormat.N3);
}
String sparql_query = "PREFIX foaf: <http://xmlns.com/foaf/0.1/#>";
sparql_query += " SELECT ?subject ?predicate WHERE { ?subject ?predicate foaf:Agent . }";
TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL,
sparql_query);
TupleQueryResult result = tupleQuery.evaluate();
try {
List<String> bindingNames = result.getBindingNames();
while (result.hasNext()) {
BindingSet bindingSet = result.next();
int size2 = bindingSet.size();
for (int i = 0; i < size2; i++) {
System.out.print('\t' + bindingSet.getValue(
bindingNames.get(i)).stringValue());
}
System.out.print('\n');
}
} finally {
result.close();
}
}
}
jarは前々回を参照してもらうか,めんどくさければopenrdf-sesame-2.6.9-sdk.zip展開後のlibフォルダにあるの全部.
0 件のコメント:
コメントを投稿