org.openrdf.query.BooleanQuery Java Examples

The following examples show how to use org.openrdf.query.BooleanQuery. 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: ClazzBuildingSupport.java    From anno4j with Apache License 2.0 6 votes vote down vote up
@Override
public boolean hasPropertyTransitive(RDFSProperty property) throws RepositoryException {
    try {
        BooleanQuery query = getObjectConnection().prepareBooleanQuery(QueryLanguage.SPARQL,
                "ASK {" +
                        "   {" +
                        "       <" + property.getResourceAsString() + "> rdfs:domain <" + getResourceAsString() + "> . " +
                        "   }" +
                        "   UNION" +
                        "   {" +
                        "       <" + getResourceAsString() + "> rdfs:subClassOf+ ?s ." +
                        "       <" + property.getResourceAsString() + "> rdfs:domain ?s ." +
                        "   }" +
                        "}"
        );

        return query.evaluate();
    } catch (QueryEvaluationException | MalformedQueryException e) {
        throw new RepositoryException(e);
    }
}
 
Example #2
Source File: ClazzBuildingSupport.java    From anno4j with Apache License 2.0 6 votes vote down vote up
/**
 * Whether the mapping for the given property needs to be redefined in this class.
 * This is the case if the class has a (direct) {@code owl:Restriction} on the property
 * for this class.
 * @param property The property to check for.
 * @return Returns true if the property mapping needs a redefinition in the Java class generated
 * for this class.
 * @throws RepositoryException Thrown if an error occurs while querying the repository.
 */
 boolean needsRedefinition(RDFSProperty property) throws RepositoryException {
    try {
        BooleanQuery query = getObjectConnection().prepareBooleanQuery(QueryLanguage.SPARQL,
                "ASK {" +
                        "   ?r a owl:Restriction . " +
                        "   <" + getResourceAsString() + "> rdfs:subClassOf ?r . " +
                        "   ?r owl:onProperty <" + property.getResourceAsString() + "> . " +
                        "}"
        );

        return query.evaluate();

    } catch (MalformedQueryException | QueryEvaluationException e) {
        throw new RepositoryException(e);
    }
}
 
Example #3
Source File: RepositoryConnectionTest.java    From database with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testPreparedBooleanQuery()
	throws Exception
{
	testCon.add(alice, name, nameAlice, context2);
	testCon.add(alice, mbox, mboxAlice, context2);
	testCon.add(context2, publisher, nameAlice);

	testCon.add(bob, name, nameBob, context1);
	testCon.add(bob, mbox, mboxBob, context1);
	testCon.add(context1, publisher, nameBob);

	StringBuilder queryBuilder = new StringBuilder();
	queryBuilder.append(PREFIX_FOAF + FOAF_NS + "> ");
	queryBuilder.append(ASK);
	queryBuilder.append("{ ?p foaf:name ?name }");

	BooleanQuery query = testCon.prepareBooleanQuery(QueryLanguage.SPARQL, queryBuilder.toString());
	query.setBinding(NAME, nameBob);

	assertThat(query.evaluate(), is(equalTo(true)));
}
 
Example #4
Source File: SesameDriver.java    From trainbenchmark with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected boolean ask(final String askQuery)
		throws RepositoryException, MalformedQueryException, QueryEvaluationException {
	final BooleanQuery q = connection.prepareBooleanQuery(QueryLanguage.SPARQL, askQuery);
	final boolean result = q.evaluate();
	return result;
}
 
Example #5
Source File: BuildableRDFSClazzSupport.java    From anno4j with Apache License 2.0 5 votes vote down vote up
@Override
public boolean hasParent(String resource) throws RepositoryException {
    try {
        BooleanQuery query = getObjectConnection().prepareBooleanQuery(QueryLanguage.SPARQL,
                "ASK {" +
                        "   <" + getResourceAsString() + "> rdfs:subClassOf+ <" + resource + "> . " +
                        "}"
        );
        return query.evaluate();
    } catch (MalformedQueryException | QueryEvaluationException e) {
        throw new RepositoryException(e);
    }
}
 
Example #6
Source File: BigdataSailRemoteRepositoryConnection.java    From database with GNU General Public License v2.0 5 votes vote down vote up
@Override
   public BooleanQuery prepareBooleanQuery(final QueryLanguage ql,
           final String query)
           throws RepositoryException, MalformedQueryException {

       return prepareBooleanQuery(ql, query, null);
       
}
 
Example #7
Source File: BigdataGraph.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
     * Select results using a SPARQL query.
     */
	public boolean ask(final String queryStr, String externalQueryId)
			throws Exception {
        
        final RepositoryConnection cxn = cxn();
                
        UUID queryId = null;
        
        try {
            
            final BooleanQuery query = (BooleanQuery) 
                    cxn.prepareBooleanQuery(QueryLanguage.SPARQL, queryStr);

            setMaxQueryTime(query);
            
            if (query instanceof BigdataSailBooleanQuery
					&& cxn instanceof BigdataSailRepositoryConnection) {

				final BigdataSailBooleanQuery bdtq = (BigdataSailBooleanQuery) query;
				queryId = setupQuery((BigdataSailRepositoryConnection) cxn,
						bdtq.getASTContainer(), QueryType.ASK,
						externalQueryId);
			}
            
            final boolean result = query.evaluate();
            
//            finalizeQuery(queryId);
            
            return result;
            
        } finally {
            if (queryId != null) {
                /*
                 * In case the exception happens during evaluate().
                 */
                finalizeQuery(queryId);
            }
        }
            
    }
 
Example #8
Source File: IntegrationTestSupertypeLayer.java    From cumulusrdf with Apache License 2.0 5 votes vote down vote up
/**
 * Executes a given ASK query against a given dataset.
 * 
 * @param data the mistery guest containing test data (query and dataset)
 * @throws Exception never, otherwise the test fails.
 */
protected void askTest(final MisteryGuest data) throws Exception {
	load(data);
	
	final String queryString = queryString(data.query);
	final BooleanQuery localQuery = localConnection.prepareBooleanQuery(QueryLanguage.SPARQL, queryString);
	final BooleanQuery cumulusQuery = cumulusConnection.prepareBooleanQuery(QueryLanguage.SPARQL, queryString);
	
	assertEquals(localQuery.evaluate(), cumulusQuery.evaluate());
}
 
Example #9
Source File: RepositoryModelSet.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public boolean sparqlAsk(String queryString) throws ModelRuntimeException {
	this.assertModel();
	BooleanQuery booleanQuery;
	try {
		booleanQuery = this.connection.prepareBooleanQuery(QueryLanguage.SPARQL, queryString);
		boolean result = booleanQuery.evaluate();
		return result;
	} catch(OpenRDFException e) {
		throw new ModelRuntimeException(e);
	}
}
 
Example #10
Source File: SPARQLBooleanStreamingOutput.java    From neo4j-sparql-extension with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create a new boolean streaming output that executes the given
 * query and returns the answer.
 * 
 * @param query the ASK query
 * @param writerFactory a writer factory that creates boolean results
 * @param conn the connection to use for query execution
 */
public SPARQLBooleanStreamingOutput(
	BooleanQuery query,
	BooleanQueryResultWriterFactory writerFactory,
	RepositoryConnection conn) {
	super(conn);
	this.query = query;
	this.factory = writerFactory;
}
 
Example #11
Source File: OWLSchemaPersistingManagerTest.java    From anno4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testAnnotationPersisting() throws Exception {
    Anno4j anno4j = new Anno4j();

    Reflections types = new Reflections(
            new ConfigurationBuilder()
            .setUrls(
                    ClasspathHelper.forClass(Person.class, ClasspathHelper.staticClassLoader()),
                    ClasspathHelper.forClass(PersonSupport.class, ClasspathHelper.staticClassLoader())
            )
            .setScanners(new MethodAnnotationsScanner(), new FieldAnnotationsScanner(), new TypeAnnotationsScanner(), new SubTypesScanner())
    );

    Transaction transaction = anno4j.createTransaction();
    transaction.begin();
    SchemaPersistingManager persistingManager = new OWLSchemaPersistingManager(transaction.getConnection());

    persistingManager.persistSchema(types);

    // Query property characteristics:
    String q = QUERY_PREFIX + "ASK { " +
            "   <http://example.de/#id> a owl:FunctionalProperty . " +
            "   <http://example.de/#id> a owl:InverseFunctionalProperty . " +
            "   <http://example.de/#partner> a owl:SymmetricProperty . " +
            "   <http://example.de/#has_subordinate> a owl:TransitiveProperty . " +
            "   <http://example.de/#has_boss> a owl:TransitiveProperty . " +
            "   <http://example.de/#has_boss> owl:inverseOf <http://example.de/#has_subordinate> . " +
            "   <http://example.de/#has_subordinate> owl:inverseOf <http://example.de/#has_boss> . " +
            "}";
    BooleanQuery characteristicsQuery = transaction.getConnection().prepareBooleanQuery(QueryLanguage.SPARQL, q);
    assertTrue(characteristicsQuery.evaluate());

    // Query property restrictions:
    q = QUERY_PREFIX + "ASK { " +
            "   ?r1 a owl:Restriction . " +
            "   <http://example.de/#validly_annotated_person> rdfs:subClassOf ?r1 . " +
            "   ?r1 owl:onProperty <http://example.de/#id> . " +
            "   ?r1 owl:minCardinality ?v1 . " +
            "   FILTER ( ?v1 = 1 )" +

            "   ?r2 a owl:Restriction . " +
            "   <http://example.de/#validly_annotated_person> rdfs:subClassOf ?r2 . " +
            "   ?r2 owl:onProperty <http://example.de/#id> . " +
            "   ?r2 owl:maxCardinality ?v2 . " +
            "   FILTER ( ?v2 = 1 )" +

            "   ?r3 a owl:Restriction . " +
            "   <http://example.de/#validly_annotated_person> rdfs:subClassOf ?r3 . " +
            "   ?r3 owl:onProperty <http://example.de/#partner> . " +
            "   ?r3 owl:onClass <http://example.de/#validly_annotated_person> . " +
            "   ?r3 owl:minQualifiedCardinality ?v3 . " +
            "   FILTER ( ?v3 = 0 )" +

            "   ?r4 a owl:Restriction . " +
            "   <http://example.de/#validly_annotated_person> rdfs:subClassOf ?r4 . " +
            "   ?r4 owl:onProperty <http://example.de/#has_boss> . " +
            "   ?r4 owl:allValuesFrom <http://example.de/#validly_annotated_person> . " +

            "   ?r5 a owl:Restriction . " +
            "   <http://example.de/#validly_annotated_person> rdfs:subClassOf ?r5 . " +
            "   ?r5 owl:onProperty <http://example.de/#has_activity> . " +
            "   ?r5 owl:minCardinality ?v5 . " +
            "   FILTER( ?v5 = 2 )" +

            "   ?r6 a owl:Restriction . " +
            "   <http://example.de/#validly_annotated_person> rdfs:subClassOf ?r6 . " +
            "   ?r6 owl:onProperty <http://example.de/#has_activity> . " +
            "   ?r6 owl:minQualifiedCardinality ?v6 . " +
            "   ?r6 owl:onClass <http://example.de/#job> . " +
            "   FILTER( ?v6 = 1 )" +
            "}";

    BooleanQuery restrictionQuery = transaction.getConnection().prepareBooleanQuery(QueryLanguage.SPARQL, q);
    assertTrue(restrictionQuery.evaluate());
}
 
Example #12
Source File: SparqlEvaluator.java    From anno4j with Apache License 2.0 4 votes vote down vote up
private BooleanQuery prepareBooleanQuery()
		throws MalformedQueryException, RepositoryException {
	String base = query.getBaseURI();
	String sparql = bindMultiples(query.toString());
	return bindSingles(con.prepareBooleanQuery(SPARQL, sparql, base));
}
 
Example #13
Source File: BigdataSailRemoteRepositoryConnection.java    From database with GNU General Public License v2.0 4 votes vote down vote up
@Override
public BooleanQuery prepareBooleanQuery(final QueryLanguage ql,
           final String query, final String baseURI) throws RepositoryException,
           MalformedQueryException {
	
	if (ql != QueryLanguage.SPARQL) {
		
		throw new UnsupportedOperationException("unsupported query language: " + ql);
		
	}
	
	try {
		
		return new BigdataRemoteBooleanQuery(repo.getRemoteRepository(), query, baseURI);
	
	} catch (Exception ex) {
		
		throw new RepositoryException(ex);
		
	}
	
}
 
Example #14
Source File: RepositoryConnectionTest.java    From database with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testDataset()
	throws Exception
{
	testCon.add(alice, name, nameAlice, context2);
	testCon.add(alice, mbox, mboxAlice, context2);
	testCon.add(context2, publisher, nameAlice);
	testCon.add(bob, name, nameBob, context1);
	testCon.add(bob, mbox, mboxBob, context1);
	testCon.add(context1, publisher, nameBob);
	StringBuilder queryBuilder = new StringBuilder();
	queryBuilder.append(PREFIX_FOAF + FOAF_NS + "> ");
	queryBuilder.append(ASK);
	queryBuilder.append("{ ?p foaf:name ?name }");
	BooleanQuery query = testCon.prepareBooleanQuery(QueryLanguage.SPARQL, queryBuilder.toString());
	query.setBinding(NAME, nameBob);
	assertThat(query.evaluate(), is(equalTo(true)));
	DatasetImpl dataset = new DatasetImpl();

	// default graph: {context1}
	dataset.addDefaultGraph(context1);
	query.setDataset(dataset);
	assertThat(query.evaluate(), is(equalTo(true)));

	// default graph: {context1, context2}
	dataset.addDefaultGraph(context2);
	query.setDataset(dataset);
	assertThat(query.evaluate(), is(equalTo(true)));

	// default graph: {context2}
	dataset.removeDefaultGraph(context1);
	query.setDataset(dataset);
	assertThat(query.evaluate(), is(equalTo(false)));
	queryBuilder.setLength(0);
	queryBuilder.append(PREFIX_FOAF + FOAF_NS + "> ");
	queryBuilder.append(ASK);
	queryBuilder.append("{ GRAPH ?g { ?p foaf:name ?name } }");
	query = testCon.prepareBooleanQuery(QueryLanguage.SPARQL, queryBuilder.toString());
	query.setBinding(NAME, nameBob);

	// default graph: {context2}; named graph: {}
	query.setDataset(dataset);
	assertThat(query.evaluate(), is(equalTo(false)));

	// default graph: {context1, context2}; named graph: {context2}
	dataset.addDefaultGraph(context1);
	dataset.addNamedGraph(context2);
	query.setDataset(dataset);
	assertThat(query.evaluate(), is(equalTo(false)));

	// default graph: {context1, context2}; named graph: {context1, context2}
	dataset.addNamedGraph(context1);
	query.setDataset(dataset);
	assertThat(query.evaluate(), is(equalTo(true)));
}
 
Example #15
Source File: TestBigdataSailRemoteRepository.java    From database with GNU General Public License v2.0 3 votes vote down vote up
/**
 * "ASK" query with an empty KB.
 */
public void test_ASK() throws Exception {
    
    final String queryStr = "ASK where {?s ?p ?o}";
    
    final BooleanQuery query = cxn.prepareBooleanQuery(QueryLanguage.SPARQL, queryStr);
    assertEquals(false, query.evaluate());
    
}