use of org.openrdf.model.URI in project blueprints by tinkerpop.
the class PropertyGraphSailConnection method getStatements_xxO.
private CloseableIteration<Statement, SailException> getStatements_xxO(final Value object) throws SailException {
if (object instanceof URI) {
Vertex v = vertexForURI((URI) object);
if (null == v) {
if (object.equals(PropertyGraphSail.VERTEX)) {
Source<Vertex> vertices = new Source<Vertex>(context.graph.getVertices().iterator(), vertexTypes);
return new StatementIteration(vertices);
} else if (object.equals(PropertyGraphSail.EDGE) && firstClassEdges) {
Source<Edge> edges = new Source<Edge>(context.graph.getEdges().iterator(), edgeTypes);
return new StatementIteration(edges);
} else {
return new StatementIteration();
}
} else {
if (firstClassEdges) {
Source<Edge> ins = new Source<Edge>(v.getEdges(Direction.IN).iterator(), heads);
Source<Edge> outs = new Source<Edge>(v.getEdges(Direction.OUT).iterator(), tails);
return new StatementIteration(ins, outs);
} else {
Source<Vertex> s = new Source<Vertex>(new SingleItemIterator<Vertex>(v), allInRelations);
return new StatementIteration(s);
}
}
} else {
Object val = literalToObject(object);
if (null == val) {
return new StatementIteration();
} else {
Collection<Source> sources = new LinkedList<Source>();
// id
{
Vertex v = context.graph.getVertex(val);
if (null != v) {
sources.add(new Source<Vertex>(new SingleItemIterator<Vertex>(v), vertexIds));
}
if (firstClassEdges) {
Edge e = context.graph.getEdge(val);
if (null != e) {
sources.add(new Source<Edge>(new SingleItemIterator<Edge>(e), edgeIds));
}
}
}
// label
if (firstClassEdges) {
// TODO: find matching edges faster using indices
if (val instanceof String) {
Source<Edge> s = new Source<Edge>(context.graph.getEdges().iterator(), matchingLabels((String) val, object));
sources.add(s);
}
}
// properties
{
// TODO: find matching vertices and edges faster using indices
Source<Vertex> vertices = new Source<Vertex>(context.graph.getVertices().iterator(), vertexPropertiesWithValue(val, (Literal) object));
sources.add(vertices);
if (firstClassEdges) {
Source<Edge> edges = new Source<Edge>(context.graph.getEdges().iterator(), edgePropertiesWithValue(val, (Literal) object));
sources.add(edges);
}
}
if (sources.size() > 0) {
Source[] s = new Source[sources.size()];
sources.toArray(s);
return new StatementIteration(s);
} else {
return new StatementIteration();
}
}
}
}
use of org.openrdf.model.URI in project blueprints by tinkerpop.
the class PropertyGraphSailConnection method getStatements_SxO.
private CloseableIteration<Statement, SailException> getStatements_SxO(final Resource subject, final Value object) throws SailException {
if (subject instanceof URI) {
Collection<Source> sources = new LinkedList<Source>();
Vertex v = vertexForURI((URI) subject);
Edge e = edgeForURI((URI) subject);
Object val = literalToObject(object);
Vertex vObj = object instanceof URI ? vertexForURI((URI) object) : null;
// vertex id
if (null != val && null != v) {
if (v.getId().equals(val)) {
Source<Vertex> s = new Source<Vertex>(new SingleItemIterator<Vertex>(v), vertexIds);
sources.add(s);
}
}
// vertex type
if (null != v && object instanceof URI && ((URI) object).equals(PropertyGraphSail.VERTEX)) {
Source<Vertex> s = new Source<Vertex>(new SingleItemIterator<Vertex>(v), vertexTypes);
sources.add(s);
}
// vertex properties
if (null != val && null != v) {
Source<Vertex> vertices = new Source<Vertex>(new SingleItemIterator<Vertex>(v), vertexPropertiesWithValue(val, (Literal) object));
sources.add(vertices);
}
if (firstClassEdges) {
// edge id
if (null != val && null != e) {
if (e.getId().equals(val)) {
Source<Edge> s = new Source<Edge>(new SingleItemIterator<Edge>(e), edgeIds);
sources.add(s);
}
}
// label
if (null != val && (val instanceof String)) {
if (null != e && e.getLabel().equals(val)) {
Source<Edge> s = new Source<Edge>(new SingleItemIterator<Edge>(e), labels);
sources.add(s);
}
}
// head
if (null != e && null != vObj && e.getVertex(Direction.IN).equals(vObj)) {
Source<Edge> s = new Source<Edge>(new SingleItemIterator<Edge>(e), heads);
sources.add(s);
}
// tail
if (null != e && null != vObj && e.getVertex(Direction.OUT).equals(vObj)) {
Source<Edge> s = new Source<Edge>(new SingleItemIterator<Edge>(e), tails);
sources.add(s);
}
// edge type
if (null != e && object instanceof URI && ((URI) object).equals(PropertyGraphSail.VERTEX)) {
Source<Edge> s = new Source<Edge>(new SingleItemIterator<Edge>(e), edgeTypes);
sources.add(s);
}
// edge properties
if (null != val && null != e) {
Source<Edge> edges = new Source<Edge>(new SingleItemIterator<Edge>(e), edgePropertiesWithValue(val, (Literal) object));
sources.add(edges);
}
} else {
if (null != v && null != vObj) {
Collection<Edge> edges = new LinkedList<Edge>();
for (Edge ev : v.getEdges(Direction.OUT)) {
if (ev.getVertex(Direction.IN).equals(vObj)) {
edges.add(ev);
}
}
if (edges.size() > 0) {
sources.add(new Source<Edge>(edges.iterator(), allEdgeStatements));
}
}
}
if (sources.size() > 0) {
Source[] s = new Source[sources.size()];
sources.toArray(s);
return new StatementIteration(s);
} else {
return new StatementIteration();
}
} else {
return new StatementIteration();
}
}
use of org.openrdf.model.URI in project frames by tinkerpop.
the class SailFramesTest method testAll.
@Test
public void testAll() throws Exception {
URI planet = new URIImpl("http://example.org/terms/planet");
URI gasGiant = new URIImpl("http://example.org/terms/gasGiant");
URI narrower = new URIImpl("http://www.w3.org/2004/02/skos/core#narrower");
SailConnection sc = sail.getConnection();
try {
sc.begin();
sc.addStatement(planet, RDFS.LABEL, new LiteralImpl("planet", "en"));
sc.addStatement(gasGiant, RDFS.LABEL, new LiteralImpl("gas giant", "en"));
sc.addStatement(planet, narrower, gasGiant);
sc.commit();
} finally {
sc.close();
}
Vertex p = sailGraph.getVertex(planet.stringValue());
FramedGraph<SailGraph> framedGraph = new FramedGraphFactory().create(sailGraph);
Concept planetFrame = framedGraph.frame(p, Concept.class);
assertNotNull(planetFrame);
assertEquals("uri", planetFrame.getKind());
//assertEquals("...", planetFrame.getValue());
RDFFrame label = planetFrame.getLabel();
assertNotNull(label);
assertEquals("literal", label.getKind());
assertEquals("en", label.getLang());
assertEquals("planet", label.getValue());
Iterable<Concept> narrowerConcepts = planetFrame.getNarrower();
int counter = 0;
for (Concept c : narrowerConcepts) {
counter++;
}
assertEquals(counter, 1);
Concept gasGiantFrame = narrowerConcepts.iterator().next();
label = gasGiantFrame.getLabel();
assertEquals("literal", label.getKind());
assertEquals("en", label.getLang());
assertEquals("gas giant", label.getValue());
}
use of org.openrdf.model.URI in project blueprints by tinkerpop.
the class SailTest method testBlankNodes.
// blank nodes /////////////////////////////////////////////////////////////
@Test
public void testBlankNodes() throws Throwable {
URI uriA = sail.getValueFactory().createURI("http://example.org/test/S_POG#a");
URI uriB = sail.getValueFactory().createURI("http://example.org/test/S_POG#b");
SailConnection sc = sail.getConnection();
try {
sc.begin();
ValueFactory factory = sail.getValueFactory();
BNode bNode = factory.createBNode();
try {
sc.addStatement(uriA, uriA, bNode);
} catch (SailException se) {
// FIXME: not supporting blank nodes ATM
assertTrue(se.getCause() instanceof UnsupportedOperationException);
}
sc.commit();
} finally {
sc.rollback();
sc.close();
}
}
use of org.openrdf.model.URI in project blueprints by tinkerpop.
the class SailTest method testGetLiteralsFromTripleStore.
@Test
public void testGetLiteralsFromTripleStore() throws Exception {
Literal l;
String prefix = "urn:com.tinkerpop.blueprints.pgm.oupls.sail.test/";
XMLGregorianCalendar calendar;
ValueFactory vf = sail.getValueFactory();
SailConnection sc = sail.getConnection();
// Get an actual plain literal from the triple store.
URI ford = vf.createURI(prefix + "ford");
l = (Literal) toSet(sc.getStatements(ford, RDFS.COMMENT, null, false)).iterator().next().getObject();
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("he really knows where his towel is", l.getLabel());
assertNull(l.getDatatype());
URI thor = vf.createURI(prefix + "thor");
// Get an actual language-tagged literal from the triple store.
URI foafName = vf.createURI("http://xmlns.com/foaf/0.1/name");
Iterator<Statement> iter = toSet(sc.getStatements(thor, foafName, null, false)).iterator();
boolean found = false;
while (iter.hasNext()) {
l = (Literal) iter.next().getObject();
if (l.getLanguage().equals("en")) {
found = true;
assertEquals("Thor", l.getLabel());
assertNull(l.getDatatype());
}
// if (l.getLanguage().equals("is")) {
// found = true;
// assertEquals("?�r", l.getLabel());
// }
}
assertTrue(found);
// Get an actual data-typed literal from the triple-store.
URI msnChatID = vf.createURI("http://xmlns.com/foaf/0.1/msnChatID");
l = (Literal) toSet(sc.getStatements(thor, msnChatID, null, false)).iterator().next().getObject();
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("Thorster123", l.getLabel());
assertEquals(XMLSchema.STRING, l.getDatatype());
// Test Literal.xxxValue() methods for Literals read from the triple
// store
URI valueUri, hasValueUri;
hasValueUri = vf.createURI(prefix + "hasValue");
valueUri = vf.createURI(prefix + "stringValue");
l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("foo", l.getLabel());
assertEquals(XMLSchema.STRING, l.getDatatype());
valueUri = vf.createURI(prefix + "byteValue");
l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("99", l.getLabel());
assertEquals(XMLSchema.BYTE, l.getDatatype());
assertEquals((byte) 'c', l.byteValue());
valueUri = vf.createURI(prefix + "booleanValue");
l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("false", l.getLabel());
assertEquals(XMLSchema.BOOLEAN, l.getDatatype());
assertEquals(false, l.booleanValue());
valueUri = vf.createURI(prefix + "intValue");
l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("42", l.getLabel());
assertEquals(XMLSchema.INT, l.getDatatype());
assertEquals(42, l.intValue());
valueUri = vf.createURI(prefix + "shortValue");
l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("42", l.getLabel());
assertEquals(XMLSchema.SHORT, l.getDatatype());
assertEquals((short) 42, l.shortValue());
valueUri = vf.createURI(prefix + "longValue");
l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("42", l.getLabel());
assertEquals(XMLSchema.LONG, l.getDatatype());
assertEquals(42l, l.longValue());
valueUri = vf.createURI(prefix + "floatValue");
l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("3.1415926", l.getLabel());
assertEquals(XMLSchema.FLOAT, l.getDatatype());
assertEquals((float) 3.1415926, l.floatValue());
valueUri = vf.createURI(prefix + "doubleValue");
l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("3.1415926", l.getLabel());
assertEquals(XMLSchema.DOUBLE, l.getDatatype());
assertEquals(3.1415926, l.doubleValue());
valueUri = vf.createURI(prefix + "dateTimeValue");
calendar = XMLDatatypeUtil.parseCalendar("2002-10-10T12:00:00-05:00");
l = (Literal) toSet(sc.getStatements(valueUri, hasValueUri, null, false)).iterator().next().getObject();
assertNotNull(l);
assertNull(l.getLanguage());
assertEquals("2002-10-10T12:00:00-05:00", l.getLabel());
assertEquals(XMLSchema.DATETIME, l.getDatatype());
assertEquals(calendar, l.calendarValue());
sc.rollback();
sc.close();
}
Aggregations