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

The following examples show how to use info.aduna.iteration.CloseableIteration#close() . 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: RDFSContainer.java    From anno4j with Apache License 2.0 6 votes vote down vote up
private int findSize() throws RepositoryException {
	CloseableIteration<? extends Statement, RepositoryException> iter;
	HashSet<URI> set = new HashSet<URI>();
	ObjectConnection conn = getObjectConnection();
	iter = conn.getStatements(getResource(), null, null);
	try {
		while (iter.hasNext()) {
			set.add(iter.next().getPredicate());
		}
	} finally {
		iter.close();
	}
	int index = 0;
	while (set.contains(getMemberPredicate(index)))
		index++;
	return index;
}
 
Example 3
Source File: ForwardChainingRDFSInferencerConnection.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private int applyRuleRdfs7_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();
		Value yyy = nt.getObject();

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

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

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

	return nofInferred;
}
 
Example 4
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 5
Source File: ForwardChainingRDFSInferencerConnection.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private int applyRuleRdfs9_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(null, RDF.TYPE, xxx, true);

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

				Resource aaa = t1.getSubject();

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

	return nofInferred;
}
 
Example 6
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 7
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 8
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 9
Source File: ForwardChainingRDFSInferencerConnection.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * rrr rdfs:subPropertyOf  ppp 
 * rrr nrl:inverseProperty sss 
 * ppp nrl:inverseProperty qqq 
 * -->
 * sss rdfs:subPropertyOf  qqq
 * @return
 * @throws SailException
 */
private int applyRuleN2b()
throws SailException
{
	int nofInferred = 0;
	Iterator<Statement> it1 = this.newThisIteration.match(null, RDFS.SUBPROPERTYOF, null);
	while (it1.hasNext()) {
		Statement stmt1 = it1.next();
		Resource rrr = stmt1.getSubject();
		Value ppp = stmt1.getObject();
		if(ppp instanceof Resource) {
			CloseableIteration<? extends Statement, SailException> it2;
			it2 = getWrappedConnection().getStatements(rrr,NRL_InverseProperty, null, true);
			while (it2.hasNext()) {
				Statement stmt2 = it2.next();
				Value sss = stmt2.getObject();
				if(sss instanceof Resource) {
					CloseableIteration<? extends Statement, SailException> it3;
					it3 = getWrappedConnection().getStatements( (Resource) ppp,NRL_InverseProperty, null, true);
					while (it3.hasNext()) {
						Statement stmt3 = it3.next();
						Value qqq = stmt3.getObject();
						if( qqq instanceof Resource) {
							boolean added = addInferredStatement((Resource)sss, RDFS.SUBPROPERTYOF, qqq);
							if (added) {
								nofInferred++;
							}
						}
					}
					it3.close();
				}
			}
			it2.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 applyRuleRdfs5_2()
	throws SailException
{
	int nofInferred = 0;

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

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

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

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

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

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

	return nofInferred;
}
 
Example 11
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 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
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 14
Source File: TestMROWTransactions.java    From database with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Long call() throws Exception {
    BigdataSailConnection con = null;
    try {
        con = sail.getReadOnlyConnection();
        /*
         * Note: This sleep makes it much easier to hit the bug
         * documented here. However, the sleep can also cause the test
         * to really stretch out. So the sleep is only used until the
         * writers are done.
         * 
         * https://sourceforge.net/apps/trac/bigdata/ticket/467
         */
        if (commits.get() < Math.max(nwriters, 5))
            Thread.sleep(2000/* millis */);

        for (int i = 0; i < nreads; i++) {
            final CloseableIteration<? extends Statement, SailException> stats = con
                    .getStatements(subs[r.nextInt(nuris)], (URI) null,
                            (Value) null, (Resource) null);
            try {
                while (stats.hasNext()) {
                    stats.next();
                }
            } finally {
                stats.close();
            }
        }
    } catch (Throwable ise) {
        if (InnerCause.isInnerCause(ise, InterruptedException.class)) {
            // Ignore.
        } else {
            if (failex
                    .compareAndSet(null/* expected */, ise/* newValue */)) {
                log.error("firstCause:" + ise, ise);
            } else {
                if (log.isInfoEnabled())
                    log.info("Other error: " + ise, ise);
            }
        }
    } finally {
        if (con != null) {
            con.rollback();
            con.close();
        }
        nreadersDone.increment();
    }
    return null;
}
 
Example 15
Source File: TestSingleTailRule.java    From database with GNU General Public License v2.0 4 votes vote down vote up
private void testValueRoundTrip(final SailConnection con, 
            final Resource subj, final URI pred, final Value obj)
        throws Exception
    {
        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);
        ParsedTupleQuery tupleQuery = QueryParserUtil.parseTupleQuery(QueryLanguage.SPARQL,
                "SELECT ?S ?P ?O " +
                "WHERE { " +
                "  ?S ?P ?O . " +
                "  FILTER( ?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 16
Source File: ExportKB.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
     * Exports all told statements associated with the last commit point for the
     * KB.
     * 
     * @throws IOException
     * @throws SailException
     * @throws RDFHandlerException
     */
    public void exportData() throws IOException, SailException,
            RDFHandlerException {
        prepare();
//        final BigdataSail sail = new BigdataSail(kb);
//        try {
//            sail.initialize();
//            final SailConnection conn = sail.getReadOnlyConnection();
//            try {
                final CloseableIteration<? extends Statement, SailException> itr = conn
                        .getStatements(null/* s */, null/* p */, null/* o */,
                                includeInferred, new Resource[] {}/* contexts */);
                try {
                    final File file = new File(kbdir, "data."
                            + format.getDefaultFileExtension()+".gz");
                    System.out.println("Writing " + file);
                    final OutputStream os = new GZIPOutputStream(
                            new FileOutputStream(file));
                    try {
                        final RDFWriter writer = RDFWriterRegistry
                                .getInstance().get(format).getWriter(os);
                        writer.startRDF();
                        while (itr.hasNext()) {
                            final Statement stmt = itr.next();
                            writer.handleStatement(stmt);
                        }
                        writer.endRDF();
                    } finally {
                        os.close();
                    }
                } finally {
                    itr.close();
                }
//            } finally {
//                conn.close();
//            }
//        } finally {
//            sail.shutDown();
//        }

    }
 
Example 17
Source File: ASTEvalHelper.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Evaluate a boolean query.
 * 
 * @param store
 *            The {@link AbstractTripleStore} having the data.
 * @param astContainer
 *            The {@link ASTContainer}.
 * @param globallyScopedBS
 *            The initial solution to kick things off.
 *            
 * @return <code>true</code> if there are any solutions to the query.
 * 
 * @throws QueryEvaluationException
 */
static public boolean evaluateBooleanQuery(
        final AbstractTripleStore store,
        final ASTContainer astContainer,
        final BindingSet globallyScopedBS,
        final Dataset dataset)
        throws QueryEvaluationException {

    final AST2BOpContext context = new AST2BOpContext(astContainer, store);

    final DeferredResolutionResult resolved;
    try {
    	// @see https://jira.blazegraph.com/browse/BLZG-1176
        resolved = ASTDeferredIVResolution.resolveQuery(
            store, astContainer, globallyScopedBS, dataset, context);
    } catch (MalformedQueryException e) {
        throw new QueryEvaluationException(e.getMessage(), e);
    }

    if (resolved.dataset != null) {
        astContainer.getOriginalAST().setDataset(
            new DatasetNode(resolved.dataset, false/* update */));
    }


    // Clear the optimized AST.
    astContainer.clearOptimizedAST();

    // Batch resolve Values to IVs and convert to bigdata binding set.
    final IBindingSet[] globallyScopedBSAsList = toBindingSet(resolved.bindingSet) ;

    // Convert the query (generates an optimized AST as a side-effect).
    AST2BOpUtility.convert(context, globallyScopedBSAsList);

    // The optimized AST.
    final QueryRoot optimizedQuery = astContainer.getOptimizedAST();

    // Note: We do not need to materialize anything for ASK.
    final boolean materializeProjectionInQuery = context.materializeProjectionInQuery
            && !optimizedQuery.hasSlice();

    CloseableIteration<BindingSet, QueryEvaluationException> itr = null;
    try {
        itr = ASTEvalHelper.evaluateQuery(
                astContainer,
                context,
                materializeProjectionInQuery,
                new IVariable[0]// required
                );
        return itr.hasNext();
    } finally {
        if (itr != null) {
            /**
             * Ensure query is terminated. An interrupt during hasNext()
             * should cause the query to terminate through itr.close().
             * 
             * @see <a
             *      href="https://sourceforge.net/apps/trac/bigdata/ticket/707">
             *      BlockingBuffer.close() does not unblock threads </a>
             */
            itr.close();
        }
    }
}
 
Example 18
Source File: NamespacesHandler.java    From cumulusrdf with Apache License 2.0 4 votes vote down vote up
/**
 * 
 * @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);
}
 
Example 19
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 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);
        
    }
    
}