Java Code Examples for org.openrdf.model.ValueFactory#createLiteral()
The following examples show how to use
org.openrdf.model.ValueFactory#createLiteral() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomSesameDataset.java From GeoTriples with Apache License 2.0 | 6 votes |
/** * Literal factory * * @param s * the literal value * @param typeuri * uri representing the type (generally xsd) * @return */ public org.openrdf.model.Literal Literal(String s, URI typeuri) { try { RepositoryConnection con = currentRepository.getConnection(); try { ValueFactory vf = con.getValueFactory(); if (typeuri == null) { return vf.createLiteral(s); } else { return vf.createLiteral(s, typeuri); } } finally { con.close(); } } catch (Exception e) { e.printStackTrace(); return null; } }
Example 2
Source File: SesameTransformationInjectPosLength.java From trainbenchmark with Eclipse Public License 1.0 | 6 votes |
@Override public void activate(final Collection<SesamePosLengthInjectMatch> matches) throws RepositoryException { final RepositoryConnection con = driver.getConnection(); final ValueFactory vf = driver.getValueFactory(); final URI typeURI = vf.createURI(BASE_PREFIX + ModelConstants.LENGTH); final Literal zeroLiteral = vf.createLiteral(0); for (final SesamePosLengthInjectMatch match : matches) { final URI segment = match.getSegment(); final RepositoryResult<Statement> statementsToRemove = con.getStatements(segment, typeURI, null, true); con.remove(statementsToRemove); con.add(segment, typeURI, zeroLiteral); } }
Example 3
Source File: WriteStatementsKnowledgeStore.java From EventCoreference with Apache License 2.0 | 6 votes |
static public org.openrdf.model.Statement castJenaOpenRdf(com.hp.hpl.jena.rdf.model.Statement jenaStatement, String modelName) { org.openrdf.model.Statement statement = null; try { ValueFactory valueFactory = ValueFactoryImpl.getInstance(); URI modelURI = valueFactory.createURI(modelName); URI subject = valueFactory.createURI(jenaStatement.getSubject().getURI()); URI sem = valueFactory.createURI(jenaStatement.getPredicate().getURI()); if (jenaStatement.getObject().isLiteral()) { Literal objectLiteral = valueFactory.createLiteral(jenaStatement.getObject().toString()); statement = valueFactory.createStatement(subject, sem, objectLiteral, modelURI); } else { URI objectUri = valueFactory.createURI(jenaStatement.getObject().asResource().getURI()); statement = valueFactory.createStatement(subject, sem, objectUri, modelURI); } } catch (Exception e) { System.out.println("jenaStatement.toString() = " + jenaStatement.toString()); e.printStackTrace(); } return statement; }
Example 4
Source File: SesameTransformationInjectConnectedSegments.java From trainbenchmark with Eclipse Public License 1.0 | 5 votes |
@Override public void activate(final Collection<SesameConnectedSegmentsInjectMatch> matches) throws Exception { final RepositoryConnection connection = driver.getConnection(); final ValueFactory vf = driver.getValueFactory(); final URI length = vf.createURI(BASE_PREFIX + LENGTH); final URI connectsTo = vf.createURI(BASE_PREFIX + CONNECTS_TO); final URI monitoredBy = vf.createURI(BASE_PREFIX + MONITORED_BY); final URI segmentType = vf.createURI(BASE_PREFIX + SEGMENT); final Literal lengthLiteral = vf.createLiteral(TrainBenchmarkConstants.DEFAULT_SEGMENT_LENGTH); for (final SesameConnectedSegmentsInjectMatch match : matches) { // create (segment2) node final Long newVertexId = driver.generateNewVertexId(); final URI segment2 = vf.createURI(BASE_PREFIX + ID_PREFIX + newVertexId); connection.add(segment2, RDF.TYPE, segmentType); connection.add(segment2, length, lengthLiteral); // (segment1)-[:connectsTo]->(segment2) connection.add(match.getSegment1(), connectsTo, segment2); // (segment2)-[:connectsTo]->(segment3) connection.add(segment2, connectsTo, match.getSegment3()); // (segment2)-[:monitoredBy]->(sensor) connection.add(segment2, monitoredBy, match.getSensor()); // remove (segment1)-[:connectsTo]->(segment3) connection.remove(match.getSegment1(), connectsTo, match.getSegment3()); } }
Example 5
Source File: InterceptTest.java From anno4j with Apache License 2.0 | 5 votes |
public void testIllegalArgument() throws Exception { IConcept concept = con.addDesignation( con.getObject("urn:test:concept"), IConcept.class); ValueFactory vf = con.getValueFactory(); Resource subj = concept.getResource(); URI pred = vf.createURI("urn:test:date"); Literal lit = vf.createLiteral("noon", XMLSchema.DATETIME); con.add(subj, pred, lit); try { concept.getDate(); fail(); } catch (IllegalArgumentException e) { assertTrue(e.getMessage().contains("noon")); } }
Example 6
Source File: InterceptTest.java From anno4j with Apache License 2.0 | 5 votes |
public void testCatchIllegalArgument() throws Exception { IConcept concept = con.addDesignation( con.getObject("urn:test:concept"), IConcept.class); ValueFactory vf = con.getValueFactory(); Resource subj = concept.getResource(); URI pred = vf.createURI("urn:test:time"); Literal lit = vf.createLiteral("noon", XMLSchema.DATETIME); con.add(subj, pred, lit); XMLGregorianCalendar zero = DatatypeFactory.newInstance().newXMLGregorianCalendar(); assertEquals(zero, concept.getTime()); }
Example 7
Source File: EntityStateSerializer.java From attic-polygene-java with Apache License 2.0 | 5 votes |
private void serializeProperty( PropertyDescriptor persistentProperty, Object property, Resource subject, Graph graph, boolean includeNonQueryable ) { if( !( includeNonQueryable || persistentProperty.queryable() ) ) { return; // Skip non-queryable } ValueType valueType = persistentProperty.valueType(); final ValueFactory valueFactory = graph.getValueFactory(); String propertyURI = persistentProperty.qualifiedName().toURI(); URI predicate = valueFactory.createURI( propertyURI ); String baseURI = propertyURI.substring( 0, propertyURI.indexOf( '#' ) ) + "/"; if( valueType instanceof ValueCompositeType ) { serializeValueComposite( subject, predicate, (ValueComposite) property, valueType, graph, baseURI, includeNonQueryable ); } else { String stringProperty = serializer.serialize( Serializer.Options.NO_TYPE_INFO, property ); final Literal object = valueFactory.createLiteral( stringProperty ); graph.add( subject, predicate, object ); } }
Example 8
Source File: UUIDLiteralIV.java From database with GNU General Public License v2.0 | 4 votes |
@SuppressWarnings("unchecked") public V asValue(final LexiconRelation lex) { V v = getValueCache(); if (v == null) { final ValueFactory f = lex.getValueFactory(); v = (V) f.createLiteral(value.toString(), DTE.UUID.getDatatypeURI()); v.setIV(this); setValue(v); } return v; }
Example 9
Source File: ConversionUtil.java From semweb4j with BSD 2-Clause "Simplified" License | 4 votes |
public static org.openrdf.model.Literal toOpenRDF(String string, ValueFactory factory) { return string == null ? null : factory.createLiteral(string); }
Example 10
Source File: TestMaterialization.java From database with GNU General Public License v2.0 | 4 votes |
public void testXsdStr() throws Exception { final BigdataSail sail = getSail(); try { sail.initialize(); final BigdataSailRepository repo = new BigdataSailRepository(sail); final RepositoryConnection cxn = repo.getConnection(); try { cxn.setAutoCommit(false); final ValueFactory vf = sail.getValueFactory(); /* * Create some terms. */ final URI X = vf.createURI(BD.NAMESPACE + "X"); final Literal label = vf.createLiteral("John"); /* * Create some statements. */ cxn.add(X, RDF.TYPE, RDFS.RESOURCE); cxn.add(X, RDFS.LABEL, label); /* * Note: The either flush() or commit() is required to flush the * statement buffers to the database before executing any operations * that go around the sail. */ cxn.commit(); if (log.isInfoEnabled()) { log.info(((BigdataSailRepositoryConnection) cxn).getTripleStore().dumpStore()); } { String query = "select * where { ?s ?p ?o . FILTER (xsd:string(?o) = \""+RDF.PROPERTY+"\"^^xsd:string) }"; final SailTupleQuery tupleQuery = (SailTupleQuery) cxn.prepareTupleQuery(QueryLanguage.SPARQL, query); tupleQuery.setIncludeInferred(true /* includeInferred */); if (log.isInfoEnabled()) { log.info(query); final TupleQueryResult result = tupleQuery.evaluate(); if (result.hasNext()) { while (result.hasNext()) log.info(result.next()); } else { fail("expecting result from the vocab"); } } // final Collection<BindingSet> answer = new LinkedList<BindingSet>(); // answer.add(createBindingSet( // new BindingImpl("a", paul), // new BindingImpl("b", mary) // )); // answer.add(createBindingSet( // new BindingImpl("a", brad), // new BindingImpl("b", john) // )); // // final TupleQueryResult result = tupleQuery.evaluate(); // compare(result, answer); } } finally { cxn.close(); } } finally { if (sail instanceof BigdataSail) ((BigdataSail)sail).__tearDownUnitTest();//shutDown(); } }
Example 11
Source File: ConversionUtil.java From semweb4j with BSD 2-Clause "Simplified" License | 4 votes |
public static org.openrdf.model.Literal toOpenRDF(PlainLiteral literal, ValueFactory factory) { return literal == null ? null : factory.createLiteral(literal.getValue()); }
Example 12
Source File: TestMaterialization.java From database with GNU General Public License v2.0 | 4 votes |
public void testRegex() throws Exception { final BigdataSail sail = getSail(); try { sail.initialize(); final BigdataSailRepository repo = new BigdataSailRepository(sail); final RepositoryConnection cxn = repo.getConnection(); try { cxn.setAutoCommit(false); final ValueFactory vf = sail.getValueFactory(); /* * Create some terms. */ final URI X = vf.createURI(BD.NAMESPACE + "X"); final Literal label = vf.createLiteral("John"); /* * Create some statements. */ cxn.add(X, RDF.TYPE, RDFS.RESOURCE); cxn.add(X, RDFS.LABEL, label); /* * Note: The either flush() or commit() is required to flush the * statement buffers to the database before executing any operations * that go around the sail. */ cxn.commit(); if (log.isInfoEnabled()) { log.info(((BigdataSailRepositoryConnection) cxn).getTripleStore().dumpStore()); } { final String query = "select * where { ?s ?p ?o . FILTER (regex(?o,\"John\",\"i\")) }"; final SailTupleQuery tupleQuery = (SailTupleQuery) cxn.prepareTupleQuery(QueryLanguage.SPARQL, query); tupleQuery.setIncludeInferred(true /* includeInferred */); if (log.isInfoEnabled()) { log.info(query); final TupleQueryResult result = tupleQuery.evaluate(); while (result.hasNext()) { log.info(result.next()); } } // final Collection<BindingSet> answer = new LinkedList<BindingSet>(); // answer.add(createBindingSet( // new BindingImpl("a", paul), // new BindingImpl("b", mary) // )); // answer.add(createBindingSet( // new BindingImpl("a", brad), // new BindingImpl("b", john) // )); // // final TupleQueryResult result = tupleQuery.evaluate(); // compare(result, answer); } } finally { cxn.close(); } } finally { if (sail instanceof BigdataSail) ((BigdataSail)sail).__tearDownUnitTest();//shutDown(); } }
Example 13
Source File: TestBOps.java From database with GNU General Public License v2.0 | 4 votes |
public void testSimpleConstraint() throws Exception { final BigdataSail sail = getSail(); try { sail.initialize(); final BigdataSailRepository repo = new BigdataSailRepository(sail); final BigdataSailRepositoryConnection cxn = (BigdataSailRepositoryConnection) repo.getConnection(); cxn.setAutoCommit(false); try { final ValueFactory vf = sail.getValueFactory(); final String ns = BD.NAMESPACE; final URI jill = new URIImpl(ns+"Jill"); final URI jane = new URIImpl(ns+"Jane"); final URI person = new URIImpl(ns+"Person"); final URI age = new URIImpl(ns+"age"); final URI IQ = new URIImpl(ns+"IQ"); final Literal l1 = new LiteralImpl("Jill"); final Literal l2 = new LiteralImpl("Jane"); final Literal age1 = vf.createLiteral(20); final Literal age2 = vf.createLiteral(30); final Literal IQ1 = vf.createLiteral(130); final Literal IQ2 = vf.createLiteral(140); /**/ cxn.setNamespace("ns", ns); cxn.add(jill, RDF.TYPE, person); cxn.add(jill, RDFS.LABEL, l1); cxn.add(jill, age, age1); cxn.add(jill, IQ, IQ1); cxn.add(jane, RDF.TYPE, person); cxn.add(jane, RDFS.LABEL, l2); cxn.add(jane, age, age2); cxn.add(jane, IQ, IQ2); /* * Note: The either flush() or commit() is required to flush the * statement buffers to the database before executing any operations * that go around the sail. */ cxn.flush();//commit(); cxn.commit();// if (log.isInfoEnabled()) { log.info("\n" + cxn.getTripleStore().dumpStore()); } { final String query = "PREFIX rdf: <"+RDF.NAMESPACE+"> " + "PREFIX rdfs: <"+RDFS.NAMESPACE+"> " + "PREFIX ns: <"+ns+"> " + "select * " + "WHERE { " + " ?s rdf:type ns:Person . " + " ?s ns:age ?age . " + " ?s ns:IQ ?iq . " + " ?s rdfs:label ?label . " + " FILTER( ?age < 25 && ?iq > 125 ) . " + "}"; final TupleQuery tupleQuery = cxn.prepareTupleQuery(QueryLanguage.SPARQL, query); final TupleQueryResult result = tupleQuery.evaluate(); // while (result.hasNext()) { // System.err.println(result.next()); // } final Collection<BindingSet> solution = new LinkedList<BindingSet>(); solution.add(createBindingSet(new Binding[] { new BindingImpl("s", jill), new BindingImpl("age", age1), new BindingImpl("iq", IQ1), new BindingImpl("label", l1) })); compare(result, solution); } } finally { cxn.close(); } } finally { sail.__tearDownUnitTest(); } }
Example 14
Source File: TestInlineValues.java From database with GNU General Public License v2.0 | 4 votes |
public void testInlineValuesLT() throws Exception { final BigdataSail sail = getSail(); sail.initialize(); final BigdataSailRepository repo = new BigdataSailRepository(sail); final BigdataSailRepositoryConnection cxn = (BigdataSailRepositoryConnection) repo.getConnection(); cxn.setAutoCommit(false); try { final ValueFactory vf = sail.getValueFactory(); URI A = vf.createURI("_:A"); URI B = vf.createURI("_:B"); URI X = vf.createURI("_:X"); URI AGE = vf.createURI("_:AGE"); Literal _25 = vf.createLiteral(25); Literal _45 = vf.createLiteral(45); cxn.add(A, RDF.TYPE, X); cxn.add(B, RDF.TYPE, X); cxn.add(A, AGE, _25); cxn.add(B, AGE, _45); /* * Note: The either flush() or commit() is required to flush the * statement buffers to the database before executing any operations * that go around the sail. */ cxn.flush();//commit(); if (log.isInfoEnabled()) { log.info(cxn.getTripleStore().dumpStore()); } { String query = "select ?s ?age " + "WHERE { " + " ?s <"+RDF.TYPE+"> <"+X+"> . " + " ?s <"+AGE+"> ?age . " + " FILTER( ?age < 35 ) . " + "}"; final TupleQuery tupleQuery = cxn.prepareTupleQuery(QueryLanguage.SPARQL, query); TupleQueryResult result = tupleQuery.evaluate(); Collection<BindingSet> solution = new LinkedList<BindingSet>(); solution.add(createBindingSet(new Binding[] { new BindingImpl("s", A), new BindingImpl("age", _25) })); compare(result, solution); } } finally { cxn.close(); sail.__tearDownUnitTest(); } }
Example 15
Source File: TestSesameFilters.java From database with GNU General Public License v2.0 | 4 votes |
public void testRegex() throws Exception { // final Sail sail = new MemoryStore(); // sail.initialize(); // final Repository repo = new SailRepository(sail); final BigdataSail sail = getSail(); sail.initialize(); final BigdataSailRepository repo = new BigdataSailRepository(sail); final RepositoryConnection cxn = repo.getConnection(); cxn.setAutoCommit(false); try { final ValueFactory vf = sail.getValueFactory(); /* * Create some terms. */ final URI mike = vf.createURI(BD.NAMESPACE + "mike"); final URI bryan = vf.createURI(BD.NAMESPACE + "bryan"); final URI person = vf.createURI(BD.NAMESPACE + "Person"); final Literal l1 = vf.createLiteral("mike personick"); final Literal l2 = vf.createLiteral("bryan thompson"); /* * Create some statements. */ cxn.add(mike, RDF.TYPE, person); cxn.add(mike, RDFS.LABEL, l1); cxn.add(bryan, RDF.TYPE, person); cxn.add(bryan, RDFS.LABEL, l2); /* * Note: The either flush() or commit() is required to flush the * statement buffers to the database before executing any operations * that go around the sail. */ cxn.commit(); { String query = "prefix bd: <"+BD.NAMESPACE+"> " + "prefix rdf: <"+RDF.NAMESPACE+"> " + "prefix rdfs: <"+RDFS.NAMESPACE+"> " + "select * " + "where { " + " ?s rdf:type bd:Person . " + " ?s rdfs:label ?label . " + " FILTER regex(?label, \"mike\") . " + "}"; final SailTupleQuery tupleQuery = (SailTupleQuery) cxn.prepareTupleQuery(QueryLanguage.SPARQL, query); tupleQuery.setIncludeInferred(false /* includeInferred */); final Collection<BindingSet> answer = new LinkedList<BindingSet>(); answer.add(createBindingSet( new BindingImpl("s", mike), new BindingImpl("label", l1) )); final TupleQueryResult result = tupleQuery.evaluate(); compare(result, answer); } } finally { cxn.close(); sail.shutDown(); } }
Example 16
Source File: ConversionUtil.java From semweb4j with BSD 2-Clause "Simplified" License | 4 votes |
public static org.openrdf.model.Literal toOpenRDF(DatatypeLiteral literal, ValueFactory factory) { return literal == null ? null : factory.createLiteral(literal.getValue(), toOpenRDF(literal.getDatatype(), factory)); }
Example 17
Source File: TestSingleTailRule.java From database with GNU General Public License v2.0 | 4 votes |
public void testOptionalFilter() throws Exception { final BigdataSail sail = getSail(); final BigdataSailRepository repo = new BigdataSailRepository(sail); // final Sail sail = new MemoryStore(); // final Repository repo = new SailRepository(sail); repo.initialize(); final RepositoryConnection cxn = repo.getConnection(); cxn.setAutoCommit(false); try { final ValueFactory vf = sail.getValueFactory(); URI s = vf.createURI("urn:test:s"); URI p1 = vf.createURI("urn:test:p1"); URI p2 = vf.createURI("urn:test:p2"); Literal v1 = vf.createLiteral(1); Literal v2 = vf.createLiteral(2); Literal v3 = vf.createLiteral(3); cxn.add(s, p1, v1); cxn.add(s, p2, v2); cxn.add(s, p1, v3); cxn.commit(); String qry = "PREFIX :<urn:test:> " + "SELECT ?s ?v1 ?v2 " + "WHERE { " + " ?s :p1 ?v1 . " + " OPTIONAL {?s :p2 ?v2 FILTER(?v1 < 3) } " + "}"; TupleQuery query = cxn.prepareTupleQuery(QueryLanguage.SPARQL, qry); TupleQueryResult result = query.evaluate(); // while (result.hasNext()) { // System.err.println(result.next()); // } Collection<BindingSet> solution = new LinkedList<BindingSet>(); solution.add(createBindingSet(new Binding[] { new BindingImpl("s", s), new BindingImpl("v1", v1), new BindingImpl("v2", v2), })); solution.add(createBindingSet(new Binding[] { new BindingImpl("s", s), new BindingImpl("v1", v3), })); compare(result, solution); } finally { cxn.close(); if (sail instanceof BigdataSail) ((BigdataSail)sail).__tearDownUnitTest(); } }
Example 18
Source File: TestBOpUtility.java From database with GNU General Public License v2.0 | 4 votes |
public void testOpenWorldEq() throws Exception { final Sail sail = new MemoryStore(); final Repository repo = new SailRepository(sail); repo.initialize(); final RepositoryConnection cxn = repo.getConnection(); try { final ValueFactory vf = sail.getValueFactory(); final URI mike = vf.createURI(BD.NAMESPACE + "mike"); final URI age = vf.createURI(BD.NAMESPACE + "age"); final Literal mikeAge = vf.createLiteral(34); cxn.add(vf.createStatement(mike, RDF.TYPE, RDFS.RESOURCE)); cxn.add(vf.createStatement(mike, age, mikeAge)); final String query = "select * " + "where { " + " ?s ?p ?o . " + " filter (?o < 40) " + "}"; final TupleQuery tupleQuery = cxn.prepareTupleQuery(QueryLanguage.SPARQL, query); final TupleQueryResult result = tupleQuery.evaluate(); while (result.hasNext()) { final BindingSet tmp = result.next(); if(log.isInfoEnabled()) log.info(tmp.toString()); } } finally { cxn.close(); repo.shutDown(); } }
Example 19
Source File: ConversionUtil.java From semweb4j with BSD 2-Clause "Simplified" License | 4 votes |
public static org.openrdf.model.Literal toOpenRDF(LanguageTagLiteral literal, ValueFactory factory) { return literal == null ? null : factory.createLiteral(literal.getValue(), literal.getLanguageTag()); }
Example 20
Source File: TestEquals.java From database with GNU General Public License v2.0 | 3 votes |
private Literal createLiteral(ValueFactory f, final String label, final URI datatype, final String languageCode) { if (datatype == null && languageCode == null) return f.createLiteral(label); if (datatype == null) return f.createLiteral(label, languageCode); return f.createLiteral(label, datatype); }