Created
June 4, 2011 19:19
-
-
Save AlBaker/1008232 to your computer and use it in GitHub Desktop.
Jena and DateTime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package testjena; | |
import static org.junit.Assert.*; | |
import org.junit.Test | |
import org.junit.Before; | |
import com.hp.hpl.jena.query.ResultSetFormatter; | |
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.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.query.QuerySolution; | |
import com.hp.hpl.jena.rdf.model.RDFNode; | |
class TestDateTime { | |
@Test | |
public void testJenaDateTime() { | |
Model model = ModelFactory.createDefaultModel(); | |
Resource resource = model.createResource("http://example.com/resource/test"); | |
Property property = model.createProperty("http://example.com/prop/test"); | |
Calendar cal = GregorianCalendar.getInstance(); | |
Literal value = model.createTypedLiteral(cal); | |
resource.addProperty(property, value); | |
/** | |
* Results in: | |
* | |
* Subject: <http://example.com/resource/test> | |
* Predicate: <http://example.com/prop/test> | |
* Object: "2011-06-04T19:16:58.869Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> | |
* | |
*/ | |
/** | |
* First, let's use ARQ to retrieve the date via a SPARQL query | |
*/ | |
String sparql = "SELECT ?s ?p ?o WHERE { ?s ?p ?o }"; | |
Query query = QueryFactory.create(sparql); | |
QueryExecution qe = QueryExecutionFactory.create(query, model); | |
Date result; | |
try { | |
ResultSet rs = qe.execSelect(); | |
for ( ; rs.hasNext(); ) { | |
QuerySolution sol = rs.nextSolution(); | |
RDFNode resultNode = sol.get("o"); | |
// traverse the to the date - must be careful you know what you want | |
// as if these aren't types as dateTime, you'll wind up with nulls to check | |
result = resultNode.asLiteral().getValue().asCalendar().getTime() | |
} | |
} | |
finally { | |
qe.close(); | |
} | |
System.out.println("Resulting date: " + result); | |
/** | |
* Now let's try that again, but with using the Jena Model API | |
*/ | |
Statement stmt = resource.getProperty(property); | |
Literal resultLiteral = stmt.getLiteral(); | |
Date resultDate = resultLiteral.getValue().asCalendar().getTime(); | |
assertEquals(resultDate, result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment