Java Code Examples for info.aduna.iteration.CloseableIteration#next()

The following examples show how to use info.aduna.iteration.CloseableIteration#next() . 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: RDFList.java    From anno4j with Apache License 2.0 6 votes vote down vote up
Resource getRest(Resource list) {
	if (list == null)
		return null;
	try {
		CloseableIteration<Value, RepositoryException> stmts;
		stmts = getValues(list, RDF.REST, null);
		try {
			if (stmts.hasNext())
				return (Resource) stmts.next();
			return null;
		} finally {
			stmts.close();
		}
	} catch (RepositoryException e) {
		throw new ObjectStoreException(e);
	}
}
 
Example 2
Source File: RDFStoreTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testGetNamespaces()
	throws Exception
{
	con.begin();
	con.setNamespace("rdf", RDF.NAMESPACE);
	con.commit();

	CloseableIteration<? extends Namespace, SailException> namespaces = con.getNamespaces();
	try {
		assertTrue(namespaces.hasNext());
		Namespace rdf = namespaces.next();
		assertEquals("rdf", rdf.getPrefix());
		assertEquals(RDF.NAMESPACE, rdf.getName());
		assertTrue(!namespaces.hasNext());
	}
	finally {
		namespaces.close();
	}
}
 
Example 3
Source File: RepositoryConnectionTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
private int getTotalStatementCount(RepositoryConnection connection)
	throws RepositoryException
{
	CloseableIteration<? extends Statement, RepositoryException> iter = connection.getStatements(null,
			null, null, true);

	try {
		int size = 0;

		while (iter.hasNext()) {
			iter.next();
			++size;
		}

		return size;
	}
	finally {
		iter.close();
	}
}
 
Example 4
Source File: ForwardChainingRDFSInferencerConnection.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private int applyRuleRdfs3_2()
	throws SailException
{
	int nofInferred = 0;

	Iterator<Statement> ntIter = this.newThisIteration.match(null, RDFS.RANGE, null);

	while (ntIter.hasNext()) {
		Statement nt = ntIter.next();

		Resource aaa = nt.getSubject();
		Value zzz = nt.getObject();

		if (aaa instanceof URI && zzz instanceof Resource) {
			CloseableIteration<? extends Statement, SailException> t1Iter;
			t1Iter = getWrappedConnection().getStatements(null, (URI)aaa, null, true);

			while (t1Iter.hasNext()) {
				Statement t1 = t1Iter.next();

				Value uuu = t1.getObject();
				if (uuu instanceof Resource) {
					boolean added = addInferredStatement((Resource)uuu, RDF.TYPE, zzz);
					if (added) {
						nofInferred++;
					}
				}
			}
			t1Iter.close();
		}
	}

	return nofInferred;

}
 
Example 5
Source File: ForwardChainingRDFSInferencerConnection.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
	 * xxx nrl:inverseProperty yyy
	 * aaa xxx bbb 
	 * -->
	 * bbb yyy aaa
	 * @return
	 * @throws SailException
	 */
	private int applyRuleN1a()
	throws SailException
{
	int nofInferred = 0;
	
	Iterator<Statement> ntIter = this.newThisIteration.match(null, NRL_InverseProperty, null);

	while (ntIter.hasNext()) {
		Statement nt = ntIter.next();

		Resource xxx = nt.getSubject();
		Value yyy = nt.getObject();

		if (xxx instanceof URI && yyy instanceof URI) {
			// apply to triples using the property
			CloseableIteration<? extends Statement, SailException> t1Iter;
			t1Iter = getWrappedConnection().getStatements(null, (URI)xxx, null, true);

			while (t1Iter.hasNext()) {
				Statement t1 = t1Iter.next();

				Value aaa = t1.getSubject();
				Value bbb = t1.getObject();
				if (bbb instanceof Resource) {
					boolean added = addInferredStatement((Resource)bbb, (URI) yyy, aaa);
					if (added) {
						nofInferred++;
					}
				}
			}
			t1Iter.close();
		}
	}

	return nofInferred;
}
 
Example 6
Source File: ForwardChainingRDFSInferencerConnection.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private int applyRuleRdfs2_2()
	throws SailException
{
	int nofInferred = 0;

	Iterator<Statement> ntIter = this.newThisIteration.match(null, RDFS.DOMAIN, null);

	while (ntIter.hasNext()) {
		Statement nt = ntIter.next();

		Resource aaa = nt.getSubject();
		Value zzz = nt.getObject();

		if (aaa instanceof URI && zzz instanceof Resource) {
			CloseableIteration<? extends Statement, SailException> t1Iter;
			t1Iter = getWrappedConnection().getStatements(null, (URI)aaa, null, true);

			while (t1Iter.hasNext()) {
				Statement t1 = t1Iter.next();

				Resource xxx = t1.getSubject();
				boolean added = addInferredStatement(xxx, RDF.TYPE, zzz);
				if (added) {
					nofInferred++;
				}
			}
			t1Iter.close();
		}
	}

	return nofInferred;
}
 
Example 7
Source File: ForwardChainingRDFSInferencerConnection.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private int applyRuleRdfs2_1()
	throws SailException
{
	int nofInferred = 0;

	Iterator<Statement> ntIter = this.newThisIteration.match(null, null, null);

	while (ntIter.hasNext()) {
		Statement nt = ntIter.next();

		Resource xxx = nt.getSubject();
		URI aaa = nt.getPredicate();

		CloseableIteration<? extends Statement, SailException> t1Iter;
		t1Iter = getWrappedConnection().getStatements(aaa, RDFS.DOMAIN, null, true);

		while (t1Iter.hasNext()) {
			Statement t1 = t1Iter.next();

			Value zzz = t1.getObject();
			if (zzz instanceof Resource) {
				boolean added = addInferredStatement(xxx, RDF.TYPE, zzz);
				if (added) {
					nofInferred++;
				}
			}
		}
		t1Iter.close();
	}

	return nofInferred;
}
 
Example 8
Source File: ContextsHandler.java    From cumulusrdf with Apache License 2.0 5 votes vote down vote up
@Override
public ModelAndView serve(final Repository repository, final HttpServletRequest request, final HttpServletResponse response)
		throws Exception {
	Map<String, Object> model = new HashMap<String, Object>();
	TupleQueryResultWriterFactory factory = ProtocolUtil.getAcceptableService(request, response,
			TupleQueryResultWriterRegistry.getInstance());

	if (METHOD_GET.equals(request.getMethod())) {
		List<String> columnNames = Arrays.asList("contextID");
		List<BindingSet> contexts = new ArrayList<BindingSet>();
		RepositoryConnection repositoryCon = repository.getConnection();
		synchronized (repositoryCon) {
			try {
				CloseableIteration<? extends Resource, RepositoryException> contextIter = repositoryCon.getContextIDs();

				try {
					while (contextIter.hasNext()) {
						BindingSet bindingSet = new ListBindingSet(columnNames, contextIter.next());
						contexts.add(bindingSet);
					}
				} finally {
					contextIter.close();
				}
			} catch (RepositoryException e) {
				throw new ServerHTTPException("Repository error: " + e.getMessage(), e);
			}
		}
		model.put(QueryResultView.QUERY_RESULT_KEY, new TupleQueryResultImpl(columnNames, contexts));
	}

	model.put(QueryResultView.FILENAME_HINT_KEY, "contexts");
	model.put(QueryResultView.FACTORY_KEY, factory);
	model.put(QueryResultView.HEADERS_ONLY, METHOD_HEAD.equals(request.getMethod()));
	return new ModelAndView(TupleQueryResultView.getInstance(), model);
}
 
Example 9
Source File: ForwardChainingRDFSInferencerConnection.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private int applyRuleRdfs9_2()
	throws SailException
{
	int nofInferred = 0;

	Iterator<Statement> ntIter = this.newThisIteration.match(null, RDF.TYPE, null);

	while (ntIter.hasNext()) {
		Statement nt = ntIter.next();

		Resource aaa = nt.getSubject();
		Value xxx = nt.getObject();

		if (xxx instanceof Resource) {
			CloseableIteration<? extends Statement, SailException> t1Iter;
			t1Iter = getWrappedConnection().getStatements((Resource)xxx, RDFS.SUBCLASSOF, null, true);

			while (t1Iter.hasNext()) {
				Statement t1 = t1Iter.next();

				Value yyy = t1.getObject();

				if (yyy instanceof Resource) {
					boolean added = addInferredStatement(aaa, RDF.TYPE, yyy);
					if (added) {
						nofInferred++;
					}
				}
			}
			t1Iter.close();
		}
	}

	return nofInferred;
}
 
Example 10
Source File: ForwardChainingRDFSInferencerConnection.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private int applyRuleRdfs11_2()
	throws SailException
{
	int nofInferred = 0;

	Iterator<Statement> ntIter = this.newThisIteration.match(null, RDFS.SUBCLASSOF, null);

	while (ntIter.hasNext()) {
		Statement nt = ntIter.next();

		Resource yyy = nt.getSubject();
		Value zzz = nt.getObject();

		if (zzz instanceof Resource) {
			CloseableIteration<? extends Statement, SailException> t1Iter;
			t1Iter = getWrappedConnection().getStatements(null, RDFS.SUBCLASSOF, yyy, true);

			while (t1Iter.hasNext()) {
				Statement t1 = t1Iter.next();

				Resource xxx = t1.getSubject();

				boolean added = addInferredStatement(xxx, RDFS.SUBCLASSOF, zzz);
				if (added) {
					nofInferred++;
				}
			}
			t1Iter.close();
		}
	}

	return nofInferred;
}
 
Example 11
Source File: BigdataStoreTest.java    From database with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void testValueRoundTrip(Resource subj, URI pred, Value obj)
        throws Exception {
    con.begin();
    con.addStatement(subj, pred, obj);
    con.commit();

    CloseableIteration<? extends Statement, SailException> stIter = con
            .getStatements(null, null, null, false);

    try {
        assertTrue(stIter.hasNext());

        Statement st = stIter.next();
        assertEquals(subj, st.getSubject());
        assertEquals(pred, st.getPredicate());
        assertEquals(obj, st.getObject());
        assertTrue(!stIter.hasNext());
    } finally {
        stIter.close();
    }

    final String query = "SELECT ?S ?P ?O WHERE { ?S ?P ?O filter(?P = <"
            + pred.stringValue() + ">) }";

    CloseableIteration<? extends BindingSet, QueryEvaluationException> iter;
    iter = evaluate(query, con);

    try {
        assertTrue(iter.hasNext());

        BindingSet bindings = iter.next();
        assertEquals(subj, bindings.getValue("S"));
        assertEquals(pred, bindings.getValue("P"));
        assertEquals(obj, bindings.getValue("O"));
        assertTrue(!iter.hasNext());
    } finally {
        iter.close();
    }
}
 
Example 12
Source File: ForwardChainingRDFSInferencerConnection.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private int applyRuleRdfs5_1()
	throws SailException
{
	int nofInferred = 0;

	Iterator<Statement> ntIter = this.newThisIteration.match(null, RDFS.SUBPROPERTYOF, null);

	while (ntIter.hasNext()) {
		Statement nt = ntIter.next();

		Resource aaa = nt.getSubject();
		Value bbb = nt.getObject();

		if (bbb instanceof Resource) {
			CloseableIteration<? extends Statement, SailException> t1Iter;
			t1Iter = getWrappedConnection().getStatements((Resource)bbb, RDFS.SUBPROPERTYOF, null, true);

			while (t1Iter.hasNext()) {
				Statement t1 = t1Iter.next();

				Value ccc = t1.getObject();
				if (ccc instanceof Resource) {
					boolean added = addInferredStatement(aaa, RDFS.SUBPROPERTYOF, ccc);
					if (added) {
						nofInferred++;
					}
				}
			}
			t1Iter.close();

		}
	}

	return nofInferred;
}
 
Example 13
Source File: ForwardChainingRDFSInferencerConnection.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
	 * aaa xxx bbb 
	 * xxx nrl:inverseProperty yyy
	 * -->
	 * bbb yyy aaa
	 * @return
	 * @throws SailException
	 */
	private int applyRuleN1b()
	throws SailException
{
	int nofInferred = 0;
	
	Iterator<Statement> ntIter = this.newThisIteration.match(null, null, null);

	while (ntIter.hasNext()) {
		Statement nt = ntIter.next();

		Resource xxx = nt.getPredicate();

		CloseableIteration<? extends Statement, SailException> t1Iter;
		t1Iter = getWrappedConnection().getStatements(xxx,NRL_InverseProperty, null, true);

		while (t1Iter.hasNext()) {
			Statement t1 = t1Iter.next();

			Value yyy = t1.getObject();
			if (yyy instanceof URI) {
				Resource aaa = 	nt.getSubject();
				Value bbb = nt.getObject();
				boolean added = addInferredStatement((Resource)bbb, (URI) yyy, aaa);
				if (added) {
					nofInferred++;
				}
			}
		}
		t1Iter.close();
	}

	return nofInferred;
}
 
Example 14
Source File: ForwardChainingRDFSInferencerConnection.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private int applyRuleRdfs7_2()
	throws SailException
{
	int nofInferred = 0;

	Iterator<Statement> ntIter = this.newThisIteration.match(null, RDFS.SUBPROPERTYOF, null);

	while (ntIter.hasNext()) {
		Statement nt = ntIter.next();

		Resource aaa = nt.getSubject();
		Value bbb = nt.getObject();

		if (aaa instanceof URI && bbb instanceof URI) {
			CloseableIteration<? extends Statement, SailException> t1Iter;
			t1Iter = getWrappedConnection().getStatements(null, (URI)aaa, null, true);

			while (t1Iter.hasNext()) {
				Statement t1 = t1Iter.next();

				Resource xxx = t1.getSubject();
				Value yyy = t1.getObject();

				boolean added = addInferredStatement(xxx, (URI)bbb, yyy);
				if (added) {
					nofInferred++;
				}
			}
			t1Iter.close();
		}
	}

	return nofInferred;
}
 
Example 15
Source File: ForwardChainingRDFSInferencerConnection.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private int applyRuleRdfs11_1()
	throws SailException
{
	int nofInferred = 0;

	Iterator<Statement> ntIter = this.newThisIteration.match(null, RDFS.SUBCLASSOF, null);

	while (ntIter.hasNext()) {
		Statement nt = ntIter.next();

		Resource xxx = nt.getSubject();
		Value yyy = nt.getObject();

		if (yyy instanceof Resource) {
			CloseableIteration<? extends Statement, SailException> t1Iter;
			t1Iter = getWrappedConnection().getStatements((Resource)yyy, RDFS.SUBCLASSOF, null, true);

			while (t1Iter.hasNext()) {
				Statement t1 = t1Iter.next();

				Value zzz = t1.getObject();

				if (zzz instanceof Resource) {
					boolean added = addInferredStatement(xxx, RDFS.SUBCLASSOF, zzz);
					if (added) {
						nofInferred++;
					}
				}
			}
			t1Iter.close();
		}
	}

	return nofInferred;
}
 
Example 16
Source File: ObjectCursor.java    From anno4j with Apache License 2.0 5 votes vote down vote up
public ObjectCursor(ObjectConnection manager, CloseableIteration<BindingSet, QueryEvaluationException> result,
		String binding) throws QueryEvaluationException {
	this.binding = binding;
	this.result = result;
	this.next = result.hasNext() ? result.next() : null;
	this.manager = manager;
	this.of = manager.getObjectFactory();
}
 
Example 17
Source File: CBD.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
     * Consume statements from the source iterator, adding new statements into a
     * collection and adding new blank node {@link IV}s into another collection.
     * 
     * @param src
     *            The statements to be consumed.
     * @param stmts
     *            The set of statements in the description of the resources from
     *            the previous round(s) (if any).
     * @param bnodes_tm1
     *            The blank node {@link IV}s already known on entry to the
     *            current round. This is empty for on entry to the first
     *            expansion round.
     * 
     * @return The set of blank node {@link IV}s not previous encountered in the
     *         CBD expansion.
     * 
     * @throws QueryEvaluationException
     */
    private static Set<IV<?, ?>> consumeStatements(
            final CloseableIteration<BigdataStatement, QueryEvaluationException> src,
            final Set<BigdataStatement> stmts, final Set<IV<?, ?>> bnodes_tm1)
            throws QueryEvaluationException {

        final Set<IV<?, ?>> newBnodes = new LinkedHashSet<IV<?, ?>>();
        try {

            while (src.hasNext()) {

                final BigdataStatement stmt = src.next();

//                /*
//                 * A statement of the form
//                 * 
//                 * ?stmtN rdf:subject <term>
//                 * 
//                 * where <term> is a blank node.
//                 */
//                final boolean foo = stmt.getPredicate().equals(RDF.SUBJECT)
//                        && bnodes_tm1.contains(stmt.getObject());
                
                if (stmts.add(stmt)) {

                    /*
                     * New blank node IVs can only be encountered for new
                     * statements.
                     * 
                     * TODO Consider using an ISPO => BigdataStatement map for
                     * the statements so we can avoid duplicate entries for
                     * BigdataStatements having different blank nodes but the
                     * same IVs for those blank nodes.
                     */
                    
                    collectBNodeIVs(bnodes_tm1, newBnodes,
                            getBNodeIV(stmt.getSubject()));

                    collectBNodeIVs(bnodes_tm1, newBnodes,
                            getBNodeIV(stmt.getObject()));

                    collectBNodeIVs(bnodes_tm1, newBnodes,
                            getBNodeIV(stmt.getContext()));

                }

            }
            
            return newBnodes;

        } finally {

            src.close();

        }

    }
 
Example 18
Source File: RDFStoreTest.java    From database with GNU General Public License v2.0 4 votes vote down vote up
protected void testValueRoundTrip(Resource subj, URI pred, Value obj)
	throws Exception
{
	con.begin();
	con.addStatement(subj, pred, obj);
	con.commit();

	CloseableIteration<? extends Statement, SailException> stIter = con.getStatements(null, null, null,
			false);

	try {
		assertTrue(stIter.hasNext());

		Statement st = stIter.next();
		assertEquals(subj, st.getSubject());
		assertEquals(pred, st.getPredicate());
		assertEquals(obj, st.getObject());
		assertTrue(!stIter.hasNext());
	}
	finally {
		stIter.close();
	}

	ParsedTupleQuery tupleQuery = QueryParserUtil.parseTupleQuery(QueryLanguage.SERQL,
			"SELECT S, P, O FROM {S} P {O} WHERE P = <" + pred.stringValue() + ">", null);

	CloseableIteration<? extends BindingSet, QueryEvaluationException> iter;
	iter = con.evaluate(tupleQuery.getTupleExpr(), null, EmptyBindingSet.getInstance(), false);

	try {
		assertTrue(iter.hasNext());

		BindingSet bindings = iter.next();
		assertEquals(subj, bindings.getValue("S"));
		assertEquals(pred, bindings.getValue("P"));
		assertEquals(obj, bindings.getValue("O"));
		assertTrue(!iter.hasNext());
	}
	finally {
		iter.close();
	}
}
 
Example 19
Source File: TestTicket422.java    From database with GNU General Public License v2.0 2 votes vote down vote up
public void test_wrapTempTripleStore() throws SailException, ExecutionException, InterruptedException {

        final BigdataSail sail = getSail();
        
        try {

            sail.initialize();
            
            final String namespace = sail.getNamespace();
            
            final BigdataSailConnection mainConn = sail.getUnisolatedConnection();
            
            try {
            
                final AbstractTripleStore mainTripleStore = mainConn.getTripleStore();
    
                if (mainTripleStore == null)
                    throw new UnsupportedOperationException();
                
                final TempTripleStore tempStore = new TempTripleStore(//
                        sail.getIndexManager().getTempStore(), //
                        mainConn.getProperties(), mainTripleStore);
    
            try {
    
                    // Note: The namespace of the tempSail MUST be distinct from the namespace of the main Sail.
                    final BigdataSail tempSail = new BigdataSail(namespace+"-"+UUID.randomUUID(), tempStore.getIndexManager(),
                            mainTripleStore.getIndexManager());
    
                try {
    
                    tempSail.initialize();
    
                        tempSail.create(new Properties());
                        
                    final BigdataSailConnection con = tempSail.getConnection();
    
                    try {
    
                        final CloseableIteration<? extends Statement, SailException> itr = con
                                .getStatements((Resource) null, (URI) null,
                                        (Value) null, (Resource) null);
    
                        try {
    
                            while (itr.hasNext()) {
    
                                itr.next();
    
                            }
                                
                        } finally {
    
                            itr.close();
    
                        }
    
                    } finally {
    
                        con.close();
    
                    }
    
                } finally {
    
                    tempSail.shutDown();
                    
                }
    
            } finally {
    
                tempStore.close();
                
            }
            
        } finally {
                
                mainConn.close();
                
            }
            
        } finally {

            sail.__tearDownUnitTest();
            
        }
        
    }
 
Example 20
Source File: SAILGASEngine.java    From database with GNU General Public License v2.0 2 votes vote down vote up
@Override
public VertexDistribution getDistribution(final Random r) {

    try {

        final VertexDistribution sample = new VertexDistribution(r);

        final CloseableIteration<? extends Statement, SailException> citr = cxn
                .getStatements(null/* s */, null/* p */, null /* o */,
                        includeInferred, defaultContext);

        try {

            while (citr.hasNext()) {

                final Statement st = citr.next();

                if (!(st.getObject() instanceof Resource)) {
                    
                    // This is a property value, not an edge.
                    continue;
                }

                if (st.getSubject().equals(st.getObject())) {

                    // ignore self-loops.
                    continue;

                }

                sample.addOutEdgeSample(st.getSubject());

                sample.addInEdgeSample((Resource) st.getObject());

            }

        } finally {

            citr.close();

        }

        return sample;

    } catch (SailException ex) {

        throw new RuntimeException(ex);
        
    }
    
}