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

The following examples show how to use com.bigdata.rdf.model.BigdataValueFactory#asValue() . 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: BlazeGraph.java    From tinkerpop3 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Helper for {@link BlazeEdge#property(String, Object)} and 
 * {@link BlazeVertexProperty#property(String, Object)}.
 * 
 * @param element the BlazeEdge or BlazeVertexProperty
 * @param key the property key
 * @param val the property value
 * @return a BlazeProperty
 */
<V> BlazeProperty<V> property(final BlazeReifiedElement element, 
        final String key, final V val) {
    final BigdataValueFactory rdfvf = rdfValueFactory();
    final BigdataBNode s = element.rdfId();
    final URI p = rdfvf.asValue(vf.propertyURI(key));
    final Literal lit = rdfvf.asValue(vf.toLiteral(val));
    
    final RepositoryConnection cxn = cxn();
    Code.wrapThrow(() -> {
        final BigdataStatement stmt = rdfvf.createStatement(s, p, lit);
        if (!bulkLoad) {
            // remove (<<stmt>> <key> ?)
            cxn.remove(s, p, null);
        }
        // add (<<stmt>> <key> "val")
        cxn.add(stmt);
    });
    
    final BlazeProperty<V> prop = new BlazeProperty<V>(this, element, p, lit);
    return prop;
}
 
Example 2
Source File: RDRHistory.java    From database with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Helper method to resolve added and removed terms.
 */
protected IV<?,?>[] resolveTerms(final URI[] terms) throws Exception {
    
    final BigdataValueFactory vf = database.getValueFactory();
    
    final BigdataValue[] values = new BigdataValue[terms.length];
    for (int i = 0; i < terms.length; i++) {
        values[i] = vf.asValue(terms[i]);
    }
    
    database.addTerms(values);
    
    final IV<?,?>[] ivs = new IV[terms.length];
    for (int i = 0; i < values.length; i++) {
        ivs[i] = values[i].getIV();
    }
    
    return ivs;
    
}
 
Example 3
Source File: AbstractDistinctSolutionsTestCase.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return a (Mock) IV for a Value.
 * 
 * @param v
 *            The value.
 * 
 * @return The Mock IV.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private IV makeIV(final Value v) {
    final BigdataValueFactory valueFactory = BigdataValueFactoryImpl
            .getInstance(namespace);
    final BigdataValue bv = valueFactory.asValue(v);
    final IV iv = new TermId(VTE.valueOf(v), nextId++);
    iv.setValue(bv);
    return iv;
}
 
Example 4
Source File: AbstractHashJoinUtilityTestCase.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return a (Mock) IV for a Value.
 * 
 * @param v
 *            The value.
 * 
 * @return The Mock IV.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private IV makeIV(final Value v) {
    final BigdataValueFactory valueFactory = BigdataValueFactoryImpl
            .getInstance(namespace);
    final BigdataValue bv = valueFactory.asValue(v);
    final IV iv = new TermId(VTE.valueOf(v), nextId++);
    iv.setValue(bv);
    return iv;
}
 
Example 5
Source File: AbstractHashJoinUtilityTestCase.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return a (Mock) IV for a Value.
 * 
 * @param v
 *            The value.
 * 
 * @return The Mock IV.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private IV makeIV(final Value v) {
    final BigdataValueFactory valueFactory = BigdataValueFactoryImpl
            .getInstance(namespace);
    final BigdataValue bv = valueFactory.asValue(v);
    final IV iv = new TermId(VTE.valueOf(v), nextId++);
    iv.setValue(bv);
    return iv;
}
 
Example 6
Source File: AbstractHashJoinUtilityTestCase.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return a (Mock) IV for a Value.
 * 
 * @param v
 *            The value.
 * 
 * @return The Mock IV.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private IV makeIV(final Value v) {
    final BigdataValueFactory valueFactory = BigdataValueFactoryImpl
            .getInstance(namespace);
    final BigdataValue bv = valueFactory.asValue(v);
    final IV iv = new TermId(VTE.valueOf(v), nextId++);
    iv.setValue(bv);
    return iv;
}
 
Example 7
Source File: AbstractHashJoinUtilityTestCase.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return a (Mock) IV for a Value.
 * 
 * @param v
 *            The value.
 * 
 * @return The Mock IV.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private IV makeIV(final Value v) {
    final BigdataValueFactory valueFactory = BigdataValueFactoryImpl
            .getInstance(namespace);
    final BigdataValue bv = valueFactory.asValue(v);
    final IV iv = new TermId(VTE.valueOf(v), nextId++);
    iv.setValue(bv);
    return iv;
}
 
Example 8
Source File: AbstractHashJoinUtilityTestCase.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return a (Mock) IV for a Value.
 * 
 * @param v
 *            The value.
 * 
 * @return The Mock IV.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private IV makeIV(final Value v) {
    final BigdataValueFactory valueFactory = BigdataValueFactoryImpl
            .getInstance(namespace);
    final BigdataValue bv = valueFactory.asValue(v);
    final IV iv = new TermId(VTE.valueOf(v), nextId++);
    iv.setValue(bv);
    return iv;
}
 
Example 9
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 10
Source File: TestNativeDistinctFilter.java    From database with GNU General Public License v2.0 4 votes vote down vote up
public JoinSetup(final String kbNamespace) {

            if (kbNamespace == null)
                throw new IllegalArgumentException();
            
            final Properties properties = new Properties();

            properties.setProperty(Journal.Options.BUFFER_MODE,
                    BufferMode.Transient.toString());
            
            jnl = new Journal(properties);

            // create the kb.
            final AbstractTripleStore kb = new LocalTripleStore(jnl,
                    kbNamespace, ITx.UNISOLATED, properties);

            kb.create();

            this.spoNamespace = kb.getSPORelation().getNamespace();

            // Setup the vocabulary.
            {
                final BigdataValueFactory vf = kb.getValueFactory();
                final String uriString = "http://bigdata.com/";
                final BigdataURI _knows = vf.asValue(FOAFVocabularyDecl.knows);
                final BigdataURI _brad = vf.createURI(uriString+"brad");
                final BigdataURI _john = vf.createURI(uriString+"john");
                final BigdataURI _fred = vf.createURI(uriString+"fred");
                final BigdataURI _mary = vf.createURI(uriString+"mary");
                final BigdataURI _paul = vf.createURI(uriString+"paul");
                final BigdataURI _leon = vf.createURI(uriString+"leon");
                final BigdataURI _luke = vf.createURI(uriString+"luke");

                final BigdataValue[] a = new BigdataValue[] {
                      _knows,//
                      _brad,
                      _john,
                      _fred,
                      _mary,
                      _paul,
                      _leon,
                      _luke
                };

                kb.getLexiconRelation()
                        .addTerms(a, a.length, false/* readOnly */);

                knows = _knows.getIV();
                brad = _brad.getIV();
                john = _john.getIV();
                fred = _fred.getIV();
                mary = _mary.getIV();
                paul = _paul.getIV();
                leon = _leon.getIV();
                luke = _luke.getIV();

            }

//            // data to insert (in key order for convenience).
//            final SPO[] a = {//
//                    new SPO(paul, knows, mary, StatementEnum.Explicit),// [0]
//                    new SPO(paul, knows, brad, StatementEnum.Explicit),// [1]
//                    
//                    new SPO(john, knows, mary, StatementEnum.Explicit),// [2]
//                    new SPO(john, knows, brad, StatementEnum.Explicit),// [3]
//                    
//                    new SPO(mary, knows, brad, StatementEnum.Explicit),// [4]
//                    
//                    new SPO(brad, knows, fred, StatementEnum.Explicit),// [5]
//                    new SPO(brad, knows, leon, StatementEnum.Explicit),// [6]
//            };
//
//            // insert data (the records are not pre-sorted).
//            kb.addStatements(a, a.length);
//
//            // Do commit since not scale-out.
//            jnl.commit();

        }
 
Example 11
Source File: TestVocabulary.java    From database with GNU General Public License v2.0 4 votes vote down vote up
public void test_NoVocabulary() {

        final Properties properties = getProperties();
        
        // override the default.
        properties.setProperty(Options.VOCABULARY_CLASS, NoVocabulary.class
                .getName());

        properties.setProperty(Options.AXIOMS_CLASS, NoAxioms.class
                .getName());
        
        AbstractTripleStore store = getStore(properties);
        
        try {
            
            final Vocabulary vocab = store.getVocabulary();

            assertTrue(vocab instanceof NoVocabulary);
            
            final int nvalues = vocab.size();

            // the vocabulary should be empty.
            assertEquals(0, nvalues);

            // verify (de-)serialization.
            doRoundTripTest(vocab);

            // lookup/add some values.
            final BigdataValueFactory f = store.getValueFactory();

            // Must be using the same namespace.
            assertTrue(vocab.getNamespace()==f.getNamespace());
            
            final BigdataURI rdfType = f.asValue(RDF.TYPE);
            final BigdataURI rdfProperty = f.asValue(RDF.PROPERTY);
            final BigdataURI unknownURI = f.createURI("http://www.bigdata.com/unknown");
            
            // resolve term ids.
            store.addTerms(new BigdataValue[] { rdfType, rdfProperty, unknownURI });

            // point tests for unknown values (there are no known values).
            assertNull(vocab.get(RDF.TYPE));
//            try {
//                
//                vocab.get(RDF.TYPE);
//                
//                fail("Expecting: " + IllegalArgumentException.class);
//                
//            } catch (IllegalArgumentException ex) {
//                
//                log.info("Ignoring expected exception: " + ex);
//                
//            }

            assertNull(vocab.get(RDF.PROPERTY));

//            try {
//                
//                vocab.get(RDF.PROPERTY);
//                
//                fail("Expecting: " + IllegalArgumentException.class);
//                
//            } catch (IllegalArgumentException ex) {
//                
//                log.info("Ignoring expected exception: " + ex);
//                
//            }

            assertNull(vocab.get(unknownURI));
            
//            try {
//             
//                vocab.get(unknownURI);
//                
//                fail("Expecting: " + IllegalArgumentException.class);
//                
//            } catch (IllegalArgumentException ex) {
//                
//                log.info("Ignoring expected exception: " + ex);
//                
//            }

            if (store.isStable()) {

                store = reopenStore(store);

                final Vocabulary vocab2 = store.getVocabulary();

                assertSameVocabulary(vocab, vocab2);
                
            }

        } finally {
            
            store.__tearDownUnitTest();
            
        }

    }
 
Example 12
Source File: TestBlobsWriteTask.java    From database with GNU General Public License v2.0 3 votes vote down vote up
/**
     * Test helper similar to {@link LexiconRelation#getIV(Value)} (point
     * lookup).
     * 
     * @param value
     * @param h
     * @param vf
     * @param ndx
     * @return
     */
    private IV getTermIV(final Value value, final BlobsIndexHelper h,
            final BigdataValueFactory vf, final IIndex ndx) {

        final IKeyBuilder keyBuilder = h.newKeyBuilder();

        final BigdataValue asValue = vf.asValue(value);

        final byte[] baseKey = h.makePrefixKey(keyBuilder.reset(), asValue);

        final byte[] val = vf.getValueSerializer().serialize(asValue);

        final int counter = h.resolveOrAddValue(ndx, true/* readOnly */,
                keyBuilder, baseKey, val, null/* tmp */, null/* bucketSize */);

        if (counter == BlobsIndexHelper.NOT_FOUND) {

            // Not found.
            return null;

        }

//        final TermId<?> iv = (TermId<?>) IVUtility.decode(key);

		final BlobIV<?> iv = new BlobIV<BigdataValue>(VTE.valueOf(asValue),
				asValue.hashCode(), (short) counter);

        return iv;

    }
 
Example 13
Source File: TestTruthMaintenance.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/**
     * A simple test of {@link TruthMaintenance} in which some statements are
     * asserted and their closure is computed and aspects of that closure are
     * verified (this is based on rdfs11).
     */
    public void test_assertAll_01() {

        TempTripleStore tempStore = null;
        final AbstractTripleStore store = getStore();
        
        try {
            
            final TruthMaintenance tm = new TruthMaintenance(store.getInferenceEngine());
            
            final BigdataValueFactory f = store.getValueFactory();
            
            final BigdataURI U = f.createURI("http://www.bigdata.com/U");
            final BigdataURI V = f.createURI("http://www.bigdata.com/V");
            final BigdataURI X = f.createURI("http://www.bigdata.com/X");

            final BigdataURI rdfsSubClassOf = f.asValue(RDFS.SUBCLASSOF);

            tempStore = tm.newTempTripleStore();

            // buffer writes on the tempStore.
            {

                final StatementBuffer assertionBuffer = new StatementBuffer(
                        tempStore, store, 10/* capacity */, 10/*queueCapacity*/);

                assertTrue(tempStore == assertionBuffer.getStatementStore());
                
                assertionBuffer.add(U, rdfsSubClassOf, V);
                assertionBuffer.add(V, rdfsSubClassOf, X);

                // flush to the temp store.
                assertionBuffer.flush();
                
            }

            if (log.isInfoEnabled())
                log.info("\n\ntempStore:\n"
                        + tempStore.dumpStore(store,
                                true, true, false, true));

            // perform closure and write on the database.
            tm.assertAll(tempStore);

            if (log.isInfoEnabled())
                log.info("\n\ndatabase:\n"
                        + store.dumpStore(store, true, true, false, true));
            
            // explicit.
            assertTrue(store.hasStatement(U, rdfsSubClassOf, V));
            assertTrue(store.hasStatement(V, rdfsSubClassOf, X));

            // inferred.
            assertTrue(store.hasStatement(U, rdfsSubClassOf, X));
            
        } finally {
        // Note: This will be torn down with the [store].
//            if (tempStore != null)
//                tempStore.__tearDownUnitTest();
            
            store.__tearDownUnitTest();
            
        }
        
    }
 
Example 14
Source File: TestVocabulary.java    From database with GNU General Public License v2.0 2 votes vote down vote up
public void test_RdfsVocabulary() {
        
        final Properties properties = getProperties();
        
        // override the default.
        properties.setProperty(Options.VOCABULARY_CLASS, RDFSVocabulary.class
                .getName());

        properties.setProperty(Options.AXIOMS_CLASS, NoAxioms.class
                .getName());
        
        AbstractTripleStore store = getStore(properties);
        
        try {

            final Vocabulary vocab = store.getVocabulary();

            assertTrue(vocab instanceof RDFSVocabulary);

            // verify (de-)serialization.
            doRoundTripTest(vocab);

            // lookup/add some values.
            final BigdataValueFactory f = store.getValueFactory();

            final BigdataURI rdfType = f.asValue(RDF.TYPE);
            final BigdataURI rdfProperty = f.asValue(RDF.PROPERTY);
            final BigdataURI unknownURI = f.createURI("http://www.bigdata.com/unknown");
            
            // resolve term ids.
            store.addTerms(new BigdataValue[] { rdfType, rdfProperty, unknownURI });

            // point tests for known values.
            
            assertEquals(rdfType.getIV(), vocab.get(RDF.TYPE));
            
            assertEquals(rdfProperty.getIV(), vocab.get(RDF.PROPERTY));

            // point test for an unknown value.
            assertNull(vocab.get(unknownURI));
            
//            try {
//             
//                vocab.get(unknownURI);
//                
//                fail("Expecting: " + IllegalArgumentException.class);
//                
//            } catch (IllegalArgumentException ex) {
//                
//                log.info("Ignoring expected exception: " + ex);
//                
//            }

            if (store.isStable()) {

                store = reopenStore(store);

                final Vocabulary vocab2 = store.getVocabulary();

                assertSameVocabulary(vocab, vocab2);
                
            }
            
        } finally {
            
            store.__tearDownUnitTest();
            
        }
        
    }
 
Example 15
Source File: TestStatementIdentifiers.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Tests the correct rejection when the same blank node is used in the
 * context position of more than one statement.
 */
public void test_correctRejection_unificationError() {
    
    AbstractTripleStore store = getStore();

    try {

        if (!store.isStatementIdentifiers()) {

            log.warn("Statement identifiers are not enabled");

            return;

        }

        final BigdataValueFactory f = store.getValueFactory();

        final BigdataURI A = f.createURI("http://www.foo.org/A");
        final BigdataURI B = f.createURI("http://www.foo.org/B");
        final BigdataURI C = f.createURI("http://www.foo.org/C");
        final BigdataURI rdfType = f.asValue(RDF.TYPE);
        final BigdataBNode sid1 = f.createBNode("_S1");

        StatementBuffer buf = new StatementBuffer(store, 100/* capacity */);
        
        /*
         * Flush to the database, resolving statement identifiers as
         * necessary.
         */
        try {

         // same blank node in both two distinct statement is an error.
         buf.add(A, RDF.TYPE, C);
         buf.add(sid1, RDF.SUBJECT, A);
         buf.add(sid1, RDF.PREDICATE, RDF.TYPE);
         buf.add(sid1, RDF.OBJECT, C);
	
         buf.add(B, RDF.TYPE, C);
         buf.add(sid1, RDF.SUBJECT, B);
         buf.add(sid1, RDF.PREDICATE, RDF.TYPE);
         buf.add(sid1, RDF.OBJECT, C);
        
            buf.flush();

            fail("Expecting: "+UnificationException.class);
            
        } catch(UnificationException ex) {
            
            System.err.println("Ignoring expected exception: "+ex);
            
        }

    } finally {
        
        store.__tearDownUnitTest();
        
    }
    
}