org.openrdf.model.impl.LiteralImpl Java Examples
The following examples show how to use
org.openrdf.model.impl.LiteralImpl.
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: TestBlobsWriteTask.java From database with GNU General Public License v2.0 | 6 votes |
public void test_add_various_toldBNodes() { // The values that we will be testing with. final Value[] valuesIn = new Value[] {// new LiteralImpl("abc"), // RDF.TYPE,// RDF.PROPERTY,// new LiteralImpl("test"),// new LiteralImpl("test", "en"),// new LiteralImpl("10", new URIImpl("http://www.w3.org/2001/XMLSchema#int")), new LiteralImpl("12", new URIImpl("http://www.w3.org/2001/XMLSchema#float")), new LiteralImpl("12.", new URIImpl("http://www.w3.org/2001/XMLSchema#float")), new LiteralImpl("12.0", new URIImpl("http://www.w3.org/2001/XMLSchema#float")), new LiteralImpl("12.00", new URIImpl("http://www.w3.org/2001/XMLSchema#float")), new BNodeImpl("a"),// new BNodeImpl("12"),// }; doAddTermsTest(valuesIn, true/* toldBNodes */); }
Example #2
Source File: Test_REST_HASSTMT.java From database with GNU General Public License v2.0 | 6 votes |
public void test_HASSTMT_po() throws Exception { doInsertbyURL("POST", packagePath + "test_estcard.ttl"); assertTrue( m_repo.hasStatement(null,// s, RDFS.LABEL,// p new LiteralImpl("Mike"),// o false // includeInferred // null // c )); assertFalse( m_repo.hasStatement(null,// s, RDFS.LABEL,// p new LiteralImpl("Fred"),// o false // includeInferred // null // c )); assertFalse( m_repo.hasStatement(null,// s, RDF.BAG,// p new LiteralImpl("Mike"),// o false // includeInferred // null // c )); }
Example #3
Source File: Test_REST_HASSTMT.java From database with GNU General Public License v2.0 | 6 votes |
public void test_HASSTMT_o() throws Exception { doInsertbyURL("POST", packagePath + "test_estcard.ttl"); assertTrue(m_repo.hasStatement(null,// s null,// p new LiteralImpl("Mike"),// o false // includeInferred // c )); assertFalse(m_repo.hasStatement(null,// s null,// p new LiteralImpl("Fred"),// o false // includeInferred // c )); }
Example #4
Source File: TestBigdataSailRemoteRepository.java From database with GNU General Public License v2.0 | 6 votes |
/** * Get everything with a specific predicate and object (a Literal). */ public void test_GET_accessPath_delete_p_o_Literal() throws Exception { doInsertbyURL("POST", packagePath + "test_delete_by_access_path.ttl"); final long result = countResults(doGetWithAccessPath(// // requestPath,// null,// s RDFS.LABEL,// p new LiteralImpl("Bryan")// o )); assertEquals(1, result); }
Example #5
Source File: AbstractTestNanoSparqlClient.java From database with GNU General Public License v2.0 | 6 votes |
/** * Sets up a simple data set on the server. * * @throws Exception */ protected void setupDataOnServer() throws Exception { final URI mike = new URIImpl(BD.NAMESPACE + "Mike"); final URI bryan = new URIImpl(BD.NAMESPACE + "Bryan"); final URI person = new URIImpl(BD.NAMESPACE + "Person"); final URI likes = new URIImpl(BD.NAMESPACE + "likes"); final URI rdf = new URIImpl(BD.NAMESPACE + "RDF"); final URI rdfs = new URIImpl(BD.NAMESPACE + "RDFS"); final Literal label1 = new LiteralImpl("Mike"); final Literal label2 = new LiteralImpl("Bryan"); { final Graph g = new LinkedHashModel(); g.add(mike, RDF.TYPE, person); g.add(mike, likes, rdf); g.add(mike, RDFS.LABEL, label1); g.add(bryan, RDF.TYPE, person); g.add(bryan, likes, rdfs); g.add(bryan, RDFS.LABEL, label2); m_repo.add(new AddOp(g)); } }
Example #6
Source File: TestBigdataSailRemoteRepository.java From database with GNU General Public License v2.0 | 6 votes |
/** * Delete everything with a specific predicate and object (a Literal). */ public void test_DELETE_accessPath_delete_p_o_Literal() throws Exception { doInsertbyURL("POST", packagePath + "test_delete_by_access_path.ttl"); final long mutationResult = doDeleteWithAccessPath(// // requestPath,// null,// s RDFS.LABEL,// p new LiteralImpl("Bryan")// o ); assertEquals(1, mutationResult); }
Example #7
Source File: TestEncodeDecodeValue.java From database with GNU General Public License v2.0 | 6 votes |
/** * Unit tests for a literal which uses single quotes in its encoded form. * For example <code>'abc'</code>. */ public void test_encodeDecode_Literal_singleQuotes() { assertEquals(new LiteralImpl("abc"), EncodeDecodeValue.decodeValue("'abc'")); assertEquals(new LiteralImpl(""), EncodeDecodeValue.decodeValue("''")); LiteralImpl l3 = new LiteralImpl("ab\"c"); Value v3 = EncodeDecodeValue.decodeValue("'ab\"c'"); assertEquals(l3, v3); assertEquals(new LiteralImpl("ab'c"), EncodeDecodeValue.decodeValue("'ab\'c'")); assertEquals(new LiteralImpl("ab'c"), EncodeDecodeValue.decodeValue("'ab'c'")); doTest("\"'ab'c'\"", new LiteralImpl("'ab'c'")); }
Example #8
Source File: TestCustomFunction.java From database with GNU General Public License v2.0 | 6 votes |
@Override public IV get(final IBindingSet bset) { // Evaluate a function argument. final IV arg = getAndCheckLiteral(0, bset); // Convert into an RDF Value. final Literal lit = asLiteral(arg); // Concat with self. final Literal lit2 = new LiteralImpl(lit.getLabel() + "-" + lit.getLabel()); // Convert into an IV. final IV ret = asIV(lit2, bset); // Return the function result. return ret; }
Example #9
Source File: SparqlParameters.java From anno4j with Apache License 2.0 | 6 votes |
private Object getDefaultValue(String value, Type type, ObjectConnection con) { Class<?> ctype = asClass(type); if (Set.class.equals(ctype)) { Object v = getDefaultValue(value, getComponentType(ctype, type), con); if (v == null) return null; return Collections.singleton(v); } ValueFactory vf = con.getValueFactory(); ObjectFactory of = con.getObjectFactory(); if (of.isDatatype(ctype)) { URIImpl datatype = new URIImpl("java:" + ctype.getName()); return of.createValue(of.createObject(new LiteralImpl(value, datatype))); } return vf.createURI(value); }
Example #10
Source File: CRUDTest.java From anno4j with Apache License 2.0 | 6 votes |
/** * Tests reading of resources that do not have a type assigned in the repository. */ @Test public void testReadNoType() throws Exception { // Create an Anno4j instance and get its repository connection for direct triple access: Anno4j anno4j = new Anno4j(new SailRepository(new MemoryStore()), null, false); RepositoryConnection repoConnection = anno4j.getRepository().getConnection(); // Resource urn:anno4j_test:p1 is present but does not have a rdf:type repoConnection.add(new StatementImpl(new URIImpl("urn:anno4j_test:p1"), new URIImpl(FOAF.MBOX), new LiteralImpl("[email protected]"))); // Try retrieving the resource with a specific type: Person p = anno4j.findByID(Person.class, "urn:anno4j_test:p1"); assertNull(p); }
Example #11
Source File: CRUDTest.java From anno4j with Apache License 2.0 | 6 votes |
/** * Tests reading objects and their property values from the repository. */ @Test public void testRead() throws Exception { // Create an Anno4j instance and get its repository connection for direct triple access: Anno4j anno4j = new Anno4j(new SailRepository(new MemoryStore()), null, false); RepositoryConnection repoConnection = anno4j.getRepository().getConnection(); // Add some triples to the repository: repoConnection.add(new StatementImpl(new URIImpl("urn:anno4j_test:p1"), new URIImpl(RDF.TYPE), new URIImpl(FOAF.PERSON))); repoConnection.add(new StatementImpl(new URIImpl("urn:anno4j_test:p1"), new URIImpl(FOAF.MBOX), new LiteralImpl("[email protected]"))); Person p = anno4j.findByID(Person.class, "urn:anno4j_test:p1"); assertNotNull(p); assertEquals("[email protected]", p.getMbox()); }
Example #12
Source File: TestBlobsWriteTask.java From database with GNU General Public License v2.0 | 6 votes |
/** * Note: Blank nodes are NOT included in this test since the test helper is * not smart enough to verify the blank nodes will not be unified when using * standard bnode semantics. However, {@link TestBlobsIndex} does verify * both standard and told bnode semantics. */ public void test_add_various_standardBNodes() { /* The values that we will be testing with. */ final Value[] valuesIn = new Value[] {// new LiteralImpl("abc"), // RDF.TYPE,// RDF.PROPERTY,// new LiteralImpl("test"),// new LiteralImpl("test", "en"),// new LiteralImpl("10", new URIImpl("http://www.w3.org/2001/XMLSchema#int")), new LiteralImpl("12", new URIImpl("http://www.w3.org/2001/XMLSchema#float")), new LiteralImpl("12.", new URIImpl("http://www.w3.org/2001/XMLSchema#float")), new LiteralImpl("12.0", new URIImpl("http://www.w3.org/2001/XMLSchema#float")), new LiteralImpl("12.00", new URIImpl("http://www.w3.org/2001/XMLSchema#float")), // new BNodeImpl("a"),// // new BNodeImpl("12"),// }; doAddTermsTest(valuesIn, false/* toldBNodes */); }
Example #13
Source File: AbstractRMLProcessor.java From GeoTriples with Apache License 2.0 | 5 votes |
/** * process an object map * * @param objectMap * @param node * @return */ public List<Value> processObjectMap(ObjectMap objectMap, Object node, TriplesMap triplesMap, Resource subject, URI predicate, SesameDataSet dataset) { // A Term map returns one or more values (in case expression matches // more) List<Object> values = processTermMap(objectMap, node, triplesMap, subject, predicate, dataset, false); List<Value> valueList = new ArrayList<>(); for (Object value : values) { switch (objectMap.getTermType()) { case IRI: log.trace("value as URI: "+value); if (value != null && !value.equals("")) { if (value.toString().startsWith("www.")) value = "http://" + value; valueList.add(new URIImpl(value.toString())); } break; case BLANK_NODE: valueList.add(new BNodeImpl(value.toString())); break; case LITERAL: if (objectMap.getLanguageTag() != null && !value.equals("")) { valueList.add(new LiteralImpl(value.toString(), objectMap .getLanguageTag())); } else if (value != null && !value.equals("") && objectMap.getDataType() != null) { valueList.add(new LiteralImpl(value.toString(), objectMap .getDataType())); } else if (value != null && !value.equals("")) { valueList.add(new LiteralImpl(value.toString().trim())); } } } return valueList; }
Example #14
Source File: TestSparqlUpdate.java From database with GNU General Public License v2.0 | 5 votes |
private Literal getReallyLongLiteral(final int length) { final StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { sb.append('a'); } return new LiteralImpl(sb.toString()); }
Example #15
Source File: JoinReferenceRMLPerformer.java From GeoTriples with Apache License 2.0 | 5 votes |
/** * Compare expressions from join to complete it * * @param node current object in parent iteration * @param dataset * @param map */ @Override public Collection<Statement> perform(Object node, SesameDataSet dataset, TriplesMap map) { List<Statement> statements=new LinkedList<>(); //Value object = processor.processSubjectMap(dataset, map.getSubjectMap(), node); List<Object> objects = processor.extractValueFromNode(node, expr); if (objects == null){ return statements; } log.debug("[JoinReferenceRMLPerformer:addTriples] Subject " + subject + " Predicate " + predicate + "Object " + objects.toString()); ValueFactory myFactory = ValueFactoryImpl.getInstance(); //add the join triple for(Object obj:objects){ Statement st = myFactory.createStatement((Resource) subject, predicate, (Value) new LiteralImpl(obj.toString().trim())); //dataset.add(subject, predicate, object); dataset.addStatement(st); statements.add(st); //dataset.add(subject, predicate, new LiteralImpl(obj.toString().trim())); } return statements; }
Example #16
Source File: SampleCode.java From database with GNU General Public License v2.0 | 5 votes |
/** * Demonstrate execution of a free-text query. * * @param repo * @throws Exception */ public boolean executeFreeTextQuery(Repository repo) throws Exception { if (((BigdataSailRepository) repo).getDatabase().getLexiconRelation() .getSearchEngine() == null) { /* * Only if the free text index exists. */ return false; } RepositoryConnection cxn = repo.getConnection(); cxn.setAutoCommit(false); try { cxn.add(new URIImpl("http://www.bigdata.com/A"), RDFS.LABEL, new LiteralImpl("Yellow Rose")); cxn.add(new URIImpl("http://www.bigdata.com/B"), RDFS.LABEL, new LiteralImpl("Red Rose")); cxn.add(new URIImpl("http://www.bigdata.com/C"), RDFS.LABEL, new LiteralImpl("Old Yellow House")); cxn.add(new URIImpl("http://www.bigdata.com/D"), RDFS.LABEL, new LiteralImpl("Loud Yell")); cxn.commit(); } catch (Exception ex) { cxn.rollback(); throw ex; } finally { // close the repository connection cxn.close(); } String query = "select ?x where { ?x <"+BDS.SEARCH+"> \"Yell\" . }"; executeSelectQuery(repo, query, QueryLanguage.SPARQL); // will match A, C, and D return true; }
Example #17
Source File: AbstractTestNanoSparqlClient.java From database with GNU General Public License v2.0 | 5 votes |
/** * Sets up a simple data set on the server. * @throws Exception */ protected void setupQuadsDataOnServer() throws Exception { final URI mike = new URIImpl(BD.NAMESPACE + "Mike"); final URI bryan = new URIImpl(BD.NAMESPACE + "Bryan"); final URI person = new URIImpl(BD.NAMESPACE + "Person"); final URI likes = new URIImpl(BD.NAMESPACE + "likes"); final URI rdf = new URIImpl(BD.NAMESPACE + "RDF"); final URI rdfs = new URIImpl(BD.NAMESPACE + "RDFS"); final URI c1 = new URIImpl(BD.NAMESPACE + "c1"); final URI c2 = new URIImpl(BD.NAMESPACE + "c2"); final URI c3 = new URIImpl(BD.NAMESPACE + "c3"); final Literal label1 = new LiteralImpl("Mike"); final Literal label2 = new LiteralImpl("Bryan"); { final Graph g = new LinkedHashModel(); g.add(mike, RDF.TYPE, person, c1, c2, c3); g.add(mike, likes, rdf, c1, c2, c3); g.add(mike, RDFS.LABEL, label1, c1, c2, c3); g.add(bryan, RDF.TYPE, person, c1, c2, c3); g.add(bryan, likes, rdfs, c1, c2, c3); g.add(bryan, RDFS.LABEL, label2, c1, c2, c3); m_repo.add(new AddOp(g)); } }
Example #18
Source File: CRUDTest.java From anno4j with Apache License 2.0 | 5 votes |
/** * Tests reading and modifying the same resource in different contexts. */ @Test public void testReadUpdateOtherContext() throws Exception { // Create an Anno4j instance and get its repository connection for direct triple access: Anno4j anno4j = new Anno4j(new SailRepository(new MemoryStore()), null, false); RepositoryConnection repoConnection = anno4j.getRepository().getConnection(); // Add some triples to the repository: repoConnection.add(new StatementImpl(new URIImpl("urn:anno4j_test:p1"), new URIImpl(RDF.TYPE), new URIImpl(FOAF.PERSON))); repoConnection.add(new StatementImpl(new URIImpl("urn:anno4j_test:p1"), new URIImpl(FOAF.MBOX), new LiteralImpl("[email protected]"))); repoConnection.add(new StatementImpl(new URIImpl("urn:anno4j_test:p1"), new URIImpl(RDF.TYPE), new URIImpl(FOAF.PERSON)), new URIImpl("urn:anno4j_test:context1")); repoConnection.add(new StatementImpl(new URIImpl("urn:anno4j_test:p1"), new URIImpl(FOAF.MBOX), new LiteralImpl("[email protected]")), new URIImpl("urn:anno4j_test:context1")); Person a = anno4j.findByID(Person.class, "urn:anno4j_test:p1"); // Use a transaction to access objects from the other context: Transaction transaction = anno4j.createTransaction(new URIImpl("urn:anno4j_test:context1")); transaction.begin(); Person b = transaction.findByID(Person.class, "urn:anno4j_test:p1"); assertEquals("[email protected]", a.getMbox()); assertEquals("[email protected]", b.getMbox()); a.setMbox("[email protected]"); assertEquals("[email protected]", a.getMbox()); assertEquals("[email protected]", b.getMbox()); }
Example #19
Source File: TestEncodeDecodeUnicodeIVs.java From database with GNU General Public License v2.0 | 5 votes |
/** * Unit test for the {@link XSDStringExtension} support for inlining * <code>xsd:string</code>. This approach is more efficient since the * datatypeURI is implicit in the {@link IExtension} handler than being * explicitly represented in the inline data. */ public void test_encodeDecode_extension_xsdString() { final BigdataValueFactory vf = BigdataValueFactoryImpl.getInstance("test"); final int maxInlineStringLength = 128; final XSDStringExtension<BigdataValue> ext = new XSDStringExtension<BigdataValue>( new IDatatypeURIResolver() { public BigdataURI resolve(final URI uri) { final BigdataURI buri = vf.createURI(uri.stringValue()); buri.setIV(newTermId(VTE.URI)); return buri; } }, maxInlineStringLength); final IV<?, ?>[] e = {// ext.createIV(new LiteralImpl("", XSD.STRING)), // ext.createIV(new LiteralImpl(" ", XSD.STRING)), // ext.createIV(new LiteralImpl(" ", XSD.STRING)), // ext.createIV(new LiteralImpl("1", XSD.STRING)), // ext.createIV(new LiteralImpl("12", XSD.STRING)), // ext.createIV(new LiteralImpl("123", XSD.STRING)), // ext.createIV(new LiteralImpl("234", XSD.STRING)), // ext.createIV(new LiteralImpl("34", XSD.STRING)), // ext.createIV(new LiteralImpl("4", XSD.STRING)), // ext.createIV(new LiteralImpl("a", XSD.STRING)), // ext.createIV(new LiteralImpl("ab", XSD.STRING)), // ext.createIV(new LiteralImpl("abc", XSD.STRING)), // }; doEncodeDecodeTest(e); doComparatorTest(e); }
Example #20
Source File: RDFStoreTest.java From database with GNU General Public License v2.0 | 5 votes |
@Test public void testValueRoundTrip3() throws Exception { URI subj = new URIImpl(EXAMPLE_NS + PICASSO); URI pred = new URIImpl(EXAMPLE_NS + PAINTS); Literal obj = new LiteralImpl("guernica"); testValueRoundTrip(subj, pred, obj); }
Example #21
Source File: RDFStoreTest.java From database with GNU General Public License v2.0 | 5 votes |
@Test public void testValueRoundTrip4() throws Exception { URI subj = new URIImpl(EXAMPLE_NS + PICASSO); URI pred = new URIImpl(EXAMPLE_NS + PAINTS); Literal obj = new LiteralImpl("guernica", "es"); testValueRoundTrip(subj, pred, obj); }
Example #22
Source File: Test_REST_DELETE_BY_ACCESS_PATH.java From database with GNU General Public License v2.0 | 5 votes |
/** * Delete everything with a specific predicate and object (a Literal). */ public void test_DELETE_accessPath_delete_p_o_Literal() throws Exception { doInsertbyURL("POST", packagePath + "test_delete_by_access_path.ttl"); final long mutationResult = doDeleteWithAccessPath(// // requestPath,// null,// s RDFS.LABEL,// p new LiteralImpl("Bryan")// o ); assertEquals(1, mutationResult); }
Example #23
Source File: RDFStoreTest.java From database with GNU General Public License v2.0 | 5 votes |
@Test public void testLongLangRoundTrip() throws Exception { StringBuffer sb = new StringBuffer(); for (int i = 0; i < 512; i++) { sb.append(Character.toChars('A' + (i % 26))); } URI subj = new URIImpl(EXAMPLE_NS + PICASSO); URI pred = new URIImpl(EXAMPLE_NS + PAINTS); Literal obj = new LiteralImpl("guernica" + sb.toString(), "es"); testValueRoundTrip(subj, pred, obj); }
Example #24
Source File: RDFStoreTest.java From database with GNU General Public License v2.0 | 5 votes |
@Test public void testLongLiteralRoundTrip() throws Exception { StringBuffer sb = new StringBuffer(); for (int i = 0; i < 512; i++) { sb.append(Character.toChars('A' + (i % 26))); } URI subj = new URIImpl(EXAMPLE_NS + PICASSO); URI pred = new URIImpl(EXAMPLE_NS + PAINTS); Literal obj = new LiteralImpl("guernica" + sb.toString()); testValueRoundTrip(subj, pred, obj); }
Example #25
Source File: RDFStoreTest.java From database with GNU General Public License v2.0 | 5 votes |
@Test public void testReallyLongLiteralRoundTrip() throws Exception { StringBuffer sb = new StringBuffer(); for (int i = 0; i < 1024000; i++) { sb.append(Character.toChars('A' + (i % 26))); } URI subj = new URIImpl(EXAMPLE_NS + PICASSO); URI pred = new URIImpl(EXAMPLE_NS + PAINTS); Literal obj = new LiteralImpl("guernica" + sb.toString()); testValueRoundTrip(subj, pred, obj); }
Example #26
Source File: TestBOps.java From database with GNU General Public License v2.0 | 4 votes |
public void testSimpleJoin() 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 mike = new URIImpl(ns+"Mike"); final URI bryan = new URIImpl(ns+"Bryan"); final URI person = new URIImpl(ns+"Person"); final URI likes = new URIImpl(ns+"likes"); final URI rdf = new URIImpl(ns+"RDF"); final Literal l1 = new LiteralImpl("Mike"); final Literal l2 = new LiteralImpl("Bryan"); /**/ cxn.setNamespace("ns", ns); cxn.add(mike, RDF.TYPE, person); cxn.add(mike, likes, rdf); cxn.add(mike, RDFS.LABEL, l1); cxn.add(bryan, RDF.TYPE, person); cxn.add(bryan, likes, rdf); 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.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:likes ?likes . " + " ?s rdfs:label ?label . " + "}"; 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", mike), new BindingImpl("likes", rdf), new BindingImpl("label", l1) })); solution.add(createBindingSet(new Binding[] { new BindingImpl("s", bryan), new BindingImpl("likes", rdf), new BindingImpl("label", l2) })); compare(result, solution); } } finally { cxn.close(); } } finally { sail.__tearDownUnitTest(); } }
Example #27
Source File: TestOptionals.java From database with GNU General Public License v2.0 | 4 votes |
/** * Tests mapping of left joins in SPARQL onto optionals in bigdata rules. * * @throws Exception */ public void testLeftJoins() 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 URI book1 = new URIImpl("http://www.bigdata.com/rdf#book1"); final URI book2 = new URIImpl("http://www.bigdata.com/rdf#book2"); final URI book3 = new URIImpl("http://www.bigdata.com/rdf#book3"); final URI price = new URIImpl("http://www.bigdata.com/rdf#price"); final URI XSD_INTEGER = new URIImpl("http://www.w3.org/2001/XMLSchema#integer"); /**/ cxn.add( book1, DC_TITLE, new LiteralImpl("TITLE 1") ); cxn.add( book1, price, new LiteralImpl("10", XSD_INTEGER) ); cxn.add( book2, DC_TITLE, new LiteralImpl("TITLE 2") ); cxn.add( book2, price, new LiteralImpl("20", XSD_INTEGER) ); cxn.add( book3, DC_TITLE, new LiteralImpl("TITLE 3") ); /**/ /* * 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(cxn.getTripleStore().dumpStore()); } String query = "SELECT ?title ?price " + "WHERE { " + "?book <"+DC_TITLE+"> ?title . " + "OPTIONAL { ?book <"+price+"> ?price . } . " + "}"; final TupleQuery tupleQuery = cxn.prepareTupleQuery(QueryLanguage.SPARQL, query); tupleQuery.setIncludeInferred(true /* includeInferred */); TupleQueryResult result = tupleQuery.evaluate(); Collection<BindingSet> answer = new LinkedList<BindingSet>(); answer.add(createBindingSet( new BindingImpl("title", new LiteralImpl("TITLE 1")), new BindingImpl("price", new LiteralImpl("10", XSD_INTEGER)))); answer.add(createBindingSet( new BindingImpl("title", new LiteralImpl("TITLE 2")), new BindingImpl("price", new LiteralImpl("20", XSD_INTEGER)))); answer.add(createBindingSet( new BindingImpl("title", new LiteralImpl("TITLE 3")))); compare(result, answer); } finally { cxn.close(); sail.__tearDownUnitTest(); } }
Example #28
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 #29
Source File: TestLexiconKeyBuilder.java From database with GNU General Public License v2.0 | 4 votes |
/** * Tests encode of a key and the decode of its "code" byte. * * @see ITermIndexCodes */ public void test_encodeDecodeCodeByte() { assertEquals(ITermIndexCodes.TERM_CODE_URI, fixture .value2Key(RDF.TYPE)[0]); assertEquals(ITermIndexCodes.TERM_CODE_BND, fixture .value2Key(new BNodeImpl("foo"))[0]); assertEquals(ITermIndexCodes.TERM_CODE_LIT, fixture .value2Key(new LiteralImpl("abc"))[0]); assertEquals(ITermIndexCodes.TERM_CODE_LCL, fixture .value2Key(new LiteralImpl("abc","en"))[0]); assertEquals(ITermIndexCodes.TERM_CODE_DTL, fixture .value2Key(new LiteralImpl("abc",XSD.BOOLEAN))[0]); }
Example #30
Source File: NamespacesHandler.java From cumulusrdf with Apache License 2.0 | 4 votes |
/** * * @param repository the Repository object * @param request the HttpServletRequest object * @param response the HttpServletResponse oject * @return QueryResultView object include the namespaces * @throws ClientHTTPException throws when errors in the request * @throws ServerHTTPException throws when errors in the Repository * @throws RepositoryException throws when errors in closing the RepositoryConnection */ private ModelAndView getExportNamespacesResult(final Repository repository, final HttpServletRequest request, final HttpServletResponse response) throws ClientHTTPException, ServerHTTPException, RepositoryException { final boolean headersOnly = METHOD_HEAD.equals(request.getMethod()); Map<String, Object> model = new HashMap<String, Object>(); if (!headersOnly) { List<String> columnNames = Arrays.asList("prefix", "namespace"); List<BindingSet> namespaces = new ArrayList<BindingSet>(); RepositoryConnection repositoryCon = repository.getConnection(); synchronized (repositoryCon) { try { CloseableIteration<? extends Namespace, RepositoryException> iter = repositoryCon.getNamespaces(); try { while (iter.hasNext()) { Namespace ns = iter.next(); Literal prefix = new LiteralImpl(ns.getPrefix()); Literal namespace = new LiteralImpl(ns.getName()); BindingSet bindingSet = new ListBindingSet(columnNames, prefix, namespace); namespaces.add(bindingSet); } } finally { iter.close(); } } catch (RepositoryException e) { throw new ServerHTTPException("Repository error: " + e.getMessage(), e); } } model.put(QueryResultView.QUERY_RESULT_KEY, new TupleQueryResultImpl(columnNames, namespaces)); repositoryCon.close(); } TupleQueryResultWriterFactory factory = ProtocolUtil.getAcceptableService(request, response, TupleQueryResultWriterRegistry.getInstance()); model.put(QueryResultView.FILENAME_HINT_KEY, "namespaces"); model.put(QueryResultView.HEADERS_ONLY, headersOnly); model.put(QueryResultView.FACTORY_KEY, factory); return new ModelAndView(TupleQueryResultView.getInstance(), model); }