Java Code Examples for com.bigdata.rdf.model.BigdataValueFactory#createStatement()

The following examples show how to use com.bigdata.rdf.model.BigdataValueFactory#createStatement() . 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: TestTicket1086.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * When loading quads into a triple store, the context is striped away by
 * default.
 */
public void testQuadStripping() throws Exception {

    BigdataSailRepositoryConnection cxn = null;

    final BigdataSail sail = getSail(getTriplesNoInference());

    try {

        sail.initialize();
        final BigdataSailRepository repo = new BigdataSailRepository(sail);
        cxn = (BigdataSailRepositoryConnection) repo.getConnection();

        final BigdataValueFactory vf = (BigdataValueFactory) sail
              .getValueFactory();
        final URI s = vf.createURI("http://test/s");
        final URI p = vf.createURI("http://test/p");
        final URI o = vf.createURI("http://test/o");
        final URI c = vf.createURI("http://test/c");

        BigdataStatement stmt = vf.createStatement(s, p, o, c);
        cxn.add(stmt); 

        RepositoryResult<Statement> stmts = cxn.getStatements(null, null,
              null, false);
        Statement res = stmts.next();
        assertEquals(s, res.getSubject());
        assertEquals(p, res.getPredicate());
        assertEquals(o, res.getObject());
        assertEquals(null, res.getContext());

    } finally {
        if (cxn != null)
            cxn.close();
        sail.__tearDownUnitTest();
    }
}
 
Example 2
Source File: TestTicket1086.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * When loading quads into a triple store and the BigdataSail option
 * REJECT_QUADS_IN_TRIPLE_MODE is set to true, an exception will be thrown.
 */
public void testQuadStrippingRejected() throws Exception {

   BigdataSailRepositoryConnection cxn = null;

   final BigdataSail sail = getSail(getTriplesNoInferenceNoQuadsStripping());

   boolean exceptionEncountered = false;
   try {

      sail.initialize();
      final BigdataSailRepository repo = new BigdataSailRepository(sail);
      cxn = (BigdataSailRepositoryConnection) repo.getConnection();

      final BigdataValueFactory vf = (BigdataValueFactory) sail
            .getValueFactory();
      final URI s = vf.createURI("http://test/s");
      final URI p = vf.createURI("http://test/p");
      final URI o = vf.createURI("http://test/o");
      final URI c = vf.createURI("http://test/c");

      BigdataStatement stmt = vf.createStatement(s, p, o, c);
      cxn.add(stmt);

   } catch (RepositoryException e) {
      
      exceptionEncountered = true; // expected !
      
   } finally {
      
      if (cxn != null)
         cxn.close();
      sail.__tearDownUnitTest();
   }
   
   assertTrue(exceptionEncountered);
}
 
Example 3
Source File: TestRDRHistory.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test whether the RDRHistory can handle statements that are added
 * and removed in the same commit.
 */
public void testFullyRedundantEvents() throws Exception {

    BigdataSailRepositoryConnection cxn = null;

    final BigdataSail sail = getSail(getProperties());

    try {

        sail.initialize();
        final BigdataSailRepository repo = new BigdataSailRepository(sail);
        cxn = (BigdataSailRepositoryConnection) repo.getConnection();

        final BigdataValueFactory vf = (BigdataValueFactory) sail
                .getValueFactory();
        final URI s = vf.createURI(":s");
        final URI p = vf.createURI(":p");
        final Literal o = vf.createLiteral("foo");
        final BigdataStatement stmt = vf.createStatement(s, p, o);
        final BigdataBNode sid = vf.createBNode(stmt);

        cxn.add(stmt);
        cxn.commit();

        assertEquals(1, cxn.getTripleStore().getAccessPath(sid, null, null).rangeCount(false));
        
        cxn.remove(stmt);
        cxn.add(stmt);
        cxn.commit();

        assertEquals(1, cxn.getTripleStore().getAccessPath(sid, null, null).rangeCount(false));
                    
    } finally {
        if (cxn != null)
            cxn.close();
        
        sail.__tearDownUnitTest();
    }
}
 
Example 4
Source File: BlazeGraph.java    From tinkerpop3 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Helper for {@link BlazeVertex#property(Cardinality, String, Object, Object...)}.
 * 
 * @param vertex the BlazeVertex
 * @param cardinality the property cardinality
 * @param key the property key
 * @param val the property value
 * @param kvs the properties to attach to the BlazeVertexProperty
 * @return a BlazeVertexProperty
 */
<V> BlazeVertexProperty<V> vertexProperty(final BlazeVertex vertex, 
        final Cardinality cardinality, final String key, final V val,
        final Object... kvs) {
    final BigdataValueFactory rdfvf = rdfValueFactory();
    final URI s = vertex.rdfId();
    final URI p = rdfvf.asValue(vf.propertyURI(key));
    final Literal lit = rdfvf.asValue(vf.toLiteral(val));
    
    final BigdataStatement stmt;
    if (cardinality == Cardinality.list) {
        final Literal timestamp = rdfvf.createLiteral(
                String.valueOf(vpIdFactory.getAndIncrement()), 
                LI_DATATYPE);
        stmt = rdfvf.createStatement(s, p, timestamp);
    } else {
        stmt = rdfvf.createStatement(s, p, lit);
    }
    final BigdataBNode sid = rdfvf.createBNode(stmt);
    final String vpId = vertexPropertyId(stmt);
    
    final RepositoryConnection cxn = cxn();
    Code.wrapThrow(() -> {
        if (cardinality == Cardinality.list) {
            // <s> <key> timestamp .
            cxn.add(stmt);
            // <<<s> <key> timestamp>> rdf:value "val" .
            cxn.add(sid, VALUE, lit);
        } else {
            if (cardinality == Cardinality.single && !bulkLoad) {
                final String queryStr = sparql.cleanVertexProps(s, p, lit);
                update(queryStr);
            }
            // << stmt >> <key> "val" .
            cxn.add(stmt);
        }
    });
    
    final BlazeProperty<V> prop = 
            new BlazeProperty<>(this, vertex, p, lit);
    final BlazeVertexProperty<V> bvp =
            new BlazeVertexProperty<>(prop, vpId, sid);
    ElementHelper.attachProperties(bvp, kvs);
    return bvp;
}
 
Example 5
Source File: TestReificationDoneRightEval.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
	 * Bootstrap test. The data are explicitly entered into the KB by hand. This
	 * makes it possible to test evaluation without having to fix the RDF data
	 * loader. The query is based on <code>rdf-02</code>.
	 */
    public void test_reificationDoneRight_00() throws Exception {

		final BigdataValueFactory vf = store.getValueFactory();

		final BigdataURI SAP = vf.createURI("http://example.com/SAP");
		final BigdataURI bought = vf.createURI("http://example.com/bought");
		final BigdataURI sybase = vf.createURI("http://example.com/sybase");
		final BigdataURI dcSource = vf.createURI(DCTermsVocabularyDecl.NAMESPACE+"source");
		final BigdataURI dcCreated = vf.createURI(DCTermsVocabularyDecl.NAMESPACE+"created");
		final BigdataURI newsSybase = vf.createURI("http://example.com/news/us-sybase");
		final BigdataLiteral createdDate = vf.createLiteral("2011-04-05T12:00:00Z",XSD.DATETIME);
		final BigdataURI g1 = vf.createURI("http://example.com/g1");

		// Add/resolve the terms against the lexicon.
		final BigdataValue[] terms = new BigdataValue[] { SAP, bought, sybase,
				dcSource, dcCreated, newsSybase, createdDate, g1 };

		final BigdataURI context = store.isQuads() ? g1 : null;
		
		store.addTerms(terms);
		
		// ground statement.
		final BigdataStatement s0 = vf.createStatement(SAP, bought, sybase, 
				context, StatementEnum.Explicit);
		
		// Setup blank node with SidIV for that Statement.
		final BigdataBNode s1 = vf.createBNode("s1");
		s1.setStatementIdentifier(true);
		final ISPO spo = new SPO(s0);//SAP.getIV(), bought.getIV(), sybase.getIV(),
//				null/* NO CONTEXT */, StatementEnum.Explicit);
		s1.setIV(new SidIV<BigdataBNode>(spo));

		// metadata statements.

		final BigdataStatement mds1 = vf.createStatement(s1, dcSource,
				newsSybase, context, StatementEnum.Explicit);

		final BigdataStatement mds2 = vf.createStatement(s1, dcCreated,
				createdDate, context, StatementEnum.Explicit);

		final ISPO[] stmts = new ISPO[] { new SPO(s0), new SPO(mds1), new SPO(mds2) };

		store.addStatements(stmts, stmts.length);

		/*
		 * Now that we have populated the database, we can go ahead and compile
		 * the query. (If we compile the query first then it will not find any
		 * matching lexical items.)
		 */

		final TestHelper h = new TestHelper(TEST_RESOURCE_PREFIX + "rdr-00", // testURI,
				TEST_RESOURCE_PREFIX + "rdr-02.rq",// queryFileURL
				TEST_RESOURCE_PREFIX + "empty.ttl",// dataFileURL
				TEST_RESOURCE_PREFIX + "rdr-02.srx"// resultFileURL
		);

		h.runTest();

    }
 
Example 6
Source File: TestReificationDoneRightEval.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
* Bootstrap test. The data are explicitly entered into the KB by hand. This
* makes it possible to test evaluation without having to fix the RDF data
* loader. The query is based on <code>rdf-02a</code>.
*/
  public void test_reificationDoneRight_00a() throws Exception {

final BigdataValueFactory vf = store.getValueFactory();

final BigdataURI SAP = vf.createURI("http://example.com/SAP");
final BigdataURI bought = vf.createURI("http://example.com/bought");
final BigdataURI sybase = vf.createURI("http://example.com/sybase");
final BigdataURI dcSource = vf.createURI(DCTermsVocabularyDecl.NAMESPACE+"source");
final BigdataURI dcCreated = vf.createURI(DCTermsVocabularyDecl.NAMESPACE+"created");
final BigdataURI newsSybase = vf.createURI("http://example.com/news/us-sybase");
final BigdataLiteral createdDate = vf.createLiteral("2011-04-05T12:00:00Z",XSD.DATETIME);
final BigdataURI g1 = vf.createURI("http://example.com/g1");

// Add/resolve the terms against the lexicon.
final BigdataValue[] terms = new BigdataValue[] { SAP, bought, sybase,
		dcSource, dcCreated, newsSybase, createdDate, g1 };

final BigdataURI context = store.isQuads() ? g1 : null;

store.addTerms(terms);

// ground statement.
final BigdataStatement s0 = vf.createStatement(SAP, bought, sybase,
		context, StatementEnum.Explicit);

// Setup blank node with SidIV for that Statement.
final BigdataBNode s1 = vf.createBNode("s1");
s1.setStatementIdentifier(true);
final ISPO spo = new SPO(SAP.getIV(), bought.getIV(), sybase.getIV(),
		null/* NO CONTEXT */, StatementEnum.Explicit);
s1.setIV(new SidIV<BigdataBNode>(spo));

// metadata statements.

final BigdataStatement mds1 = vf.createStatement(s1, dcSource,
		newsSybase, context, StatementEnum.Explicit);

final BigdataStatement mds2 = vf.createStatement(s1, dcCreated,
		createdDate, context, StatementEnum.Explicit);

final ISPO[] stmts = new ISPO[] { new SPO(s0), new SPO(mds1), new SPO(mds2) };

store.addStatements(stmts, stmts.length);

/*
 * Now that we have populated the database, we can go ahead and compile
 * the query. (If we compile the query first then it will not find any
 * matching lexical items.)
 */

new TestHelper(TEST_RESOURCE_PREFIX + "rdr-00a", // testURI,
		TEST_RESOURCE_PREFIX + "rdr-02a.rq",// queryFileURL
		TEST_RESOURCE_PREFIX + "empty.ttl",// dataFileURL
		TEST_RESOURCE_PREFIX + "rdr-02a.srx"// resultFileURL
).runTest();

  }
 
Example 7
Source File: TestRDRHistory.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Test whether the RDRHistory can handle statements that are added
 * and removed in the same commit.
 */
public void testPartiallyRedundantEvents() throws Exception {

    BigdataSailRepositoryConnection cxn = null;

    final BigdataSail sail = getSail(getProperties());

    try {

        sail.initialize();
        final BigdataSailRepository repo = new BigdataSailRepository(sail);
        cxn = (BigdataSailRepositoryConnection) repo.getConnection();

        final BigdataValueFactory vf = (BigdataValueFactory) sail
                .getValueFactory();
        final URI s = vf.createURI(":s");
        final URI p = vf.createURI(":p");
        final Literal o = vf.createLiteral("foo");
        final Literal bar = vf.createLiteral("bar");
        final BigdataStatement stmt = vf.createStatement(s, p, o);
        final BigdataStatement stmt2 = vf.createStatement(s, p, bar);
        final BigdataBNode sid = vf.createBNode(stmt);
        final BigdataBNode sid2 = vf.createBNode(stmt2);

        cxn.add(stmt);
        cxn.commit();

        assertEquals(1, cxn.getTripleStore().getAccessPath(sid, null, null).rangeCount(false));
        
        cxn.remove(stmt);
        cxn.add(stmt);
        cxn.add(stmt2);
        cxn.commit();

        assertEquals(1, cxn.getTripleStore().getAccessPath(sid, null, null).rangeCount(false));
        assertEquals(1, cxn.getTripleStore().getAccessPath(sid2, null, null).rangeCount(false));
        
        
    } finally {
        if (cxn != null)
            cxn.close();
        
        sail.__tearDownUnitTest();
    }
}
 
Example 8
Source File: BigdataSPARQLResultsJSONParser.java    From database with GNU General Public License v2.0 4 votes vote down vote up
protected Value parseValue(final String bindingStr, final JsonParser jp) 
        throws QueryResultParseException, JsonParseException, IOException {
    
    String lang = null;
    String type = null;
    String datatype = null;
    String value = null;
    
    // added for Sids support
    final Map<String, Value> sid = new LinkedHashMap<String, Value>();

    while (jp.nextToken() != JsonToken.END_OBJECT) {

        if (jp.getCurrentToken() != JsonToken.FIELD_NAME) {
            throw new QueryResultParseException("Did not find value attribute under "
                    + bindingStr + " field", jp.getCurrentLocation().getLineNr(),
                    jp.getCurrentLocation().getColumnNr());
        }
        String fieldName = jp.getCurrentName();

        // move to the value token
        jp.nextToken();
        
        // set the appropriate state variable
        if (TYPE.equals(fieldName)) {
            type = jp.getText();
        }
        else if (XMLLANG.equals(fieldName)) {
            lang = jp.getText();
        }
        else if (DATATYPE.equals(fieldName)) {
            datatype = jp.getText();
        }
        else if (VALUE.equals(fieldName)) {
            value = jp.getText();
        }
        // added for Sids support
        else if (jp.getCurrentToken() == JsonToken.START_OBJECT) {
            sid.put(fieldName, parseValue(bindingStr, jp));
        }
        else {
            throw new QueryResultParseException("Unexpected field name: " + fieldName,
                    jp.getCurrentLocation().getLineNr(),
                    jp.getCurrentLocation().getColumnNr());

        }
    }
    
    // added for Sids support
    if (type.equals(SID)) {
        
        final Resource s = (Resource) sid.get(SUBJECT);
        final URI p = (URI) sid.get(PREDICATE);
        final Value o = (Value) sid.get(OBJECT);
        final Resource c = (Resource) sid.get(CONTEXT);
        
        if (s == null) {
            throw new QueryResultParseException("Missing subject for statement: " + bindingStr,
                    jp.getCurrentLocation().getLineNr(),
                    jp.getCurrentLocation().getColumnNr());
        }
        
        if (p == null) {
            throw new QueryResultParseException("Missing predicate for statement: " + bindingStr,
                    jp.getCurrentLocation().getLineNr(),
                    jp.getCurrentLocation().getColumnNr());
        }
        
        if (o == null) {
            throw new QueryResultParseException("Missing object for statement: " + bindingStr,
                    jp.getCurrentLocation().getLineNr(),
                    jp.getCurrentLocation().getColumnNr());
        }
        
        final BigdataValueFactory valueFactory = 
                (BigdataValueFactory) super.valueFactory;
        
        final BigdataStatement stmt = valueFactory.createStatement(s, p, o, c);
        
        return valueFactory.createBNode(stmt);
        
    }

    return parseValue(type, value, lang, datatype);
    
}
 
Example 9
Source File: StatementBuffer.java    From database with GNU General Public License v2.0 2 votes vote down vote up
public BigdataStatement toStatement(final BigdataValueFactory vf) {
	
	return vf.createStatement(s, p, o, c);
	
}