Java Code Examples for org.eclipse.rdf4j.query.impl.EmptyBindingSet#getInstance()

The following examples show how to use org.eclipse.rdf4j.query.impl.EmptyBindingSet#getInstance() . 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: RDFQueryLogParser.java    From semagrow with Apache License 2.0 6 votes vote down vote up
private QueryLogRecord parseQueryRecord(Resource qr, Model model) {

        Optional<IRI> optionalEndpoint = Models.objectIRI(model.filter(qr, QFR.ENDPOINT, null));
        Optional<IRI> optionalResults  = Models.objectIRI(model.filter(qr, QFR.RESULTFILE, null));

        Date startTime = parseDate(Models.objectLiteral(model.filter(qr, QFR.START, null)).get(), model);
        Date endTime = parseDate(Models.objectLiteral(model.filter(qr, QFR.END, null)).get(), model);

        long cardinality = parseCardinality(Models.objectLiteral(model.filter(qr, QFR.CARDINALITY, null)).get(), model);
        String expr = parseQuery(Models.object(model.filter(qr, QFR.QUERY, null)).get(), model).toString();

        QueryLogRecord r = new QueryLogRecordImpl(null, optionalEndpoint.get(), expr , EmptyBindingSet.getInstance(), Collections.<String>emptyList());

        //r.setDuration(startTime, endTime);

        r.setCardinality(cardinality);
        r.setDuration(startTime.getTime(), endTime.getTime());
        r.setResults(optionalResults.get());
        return r;
    }
 
Example 2
Source File: FederationEvalStrategy.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Evaluate a SERVICE using vectored evaluation, taking the provided bindings as input.
 *
 * See {@link ControlledWorkerBoundJoin} and {@link FedXConfig#getEnableServiceAsBoundJoin()}
 *
 * @param service
 * @param bindings
 * @return the result iteration
 * @throws QueryEvaluationException
 */
public CloseableIteration<BindingSet, QueryEvaluationException> evaluateService(FedXService service,
		final List<BindingSet> bindings) throws QueryEvaluationException {

	Var serviceRef = service.getService().getServiceRef();
	String serviceUri;
	if (serviceRef.hasValue()) {
		serviceUri = serviceRef.getValue().stringValue();
	} else {
		return new ServiceJoinIterator(new CollectionIteration<>(bindings),
				service.getService(), EmptyBindingSet.getInstance(), this);
	}

	// use vectored evaluation
	FederatedService fs = getService(serviceUri);
	if (fs instanceof RepositoryFederatedService) {
		// set the bound join block size to 0 => leave block size up to FedX engine
		((RepositoryFederatedService) fs).setBoundJoinBlockSize(0);
	}
	return fs.evaluate(service.getService(), new CollectionIteration<>(bindings),
			service.getService().getBaseURI());
}
 
Example 3
Source File: HashJoinIterationTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testCartesianJoin() throws QueryEvaluationException {
	BindingSetAssignment left = new BindingSetAssignment();
	{
		QueryBindingSet leftb = new QueryBindingSet();
		leftb.addBinding("a", vf.createLiteral("1"));
		left.setBindingSets(Arrays.<BindingSet>asList(leftb));
	}

	BindingSetAssignment right = new BindingSetAssignment();
	{
		QueryBindingSet rightb = new QueryBindingSet();
		rightb.addBinding("b", vf.createLiteral("2"));
		right.setBindingSets(Arrays.<BindingSet>asList(rightb));
	}

	HashJoinIteration iter = new HashJoinIteration(evaluator, left, right, EmptyBindingSet.getInstance(), false);
	BindingSet actual = iter.next();

	assertEquals("1", actual.getValue("a").stringValue());
	assertEquals("2", actual.getValue("b").stringValue());
}
 
Example 4
Source File: HashJoinIterationTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testInnerJoin() throws QueryEvaluationException {
	BindingSetAssignment left = new BindingSetAssignment();
	{
		QueryBindingSet leftb = new QueryBindingSet();
		leftb.addBinding("a", vf.createLiteral("1"));
		leftb.addBinding("i", vf.createLiteral("x"));
		left.setBindingSets(Arrays.<BindingSet>asList(leftb));
	}

	BindingSetAssignment right = new BindingSetAssignment();
	{
		QueryBindingSet rightb = new QueryBindingSet();
		rightb.addBinding("b", vf.createLiteral("2"));
		rightb.addBinding("i", vf.createLiteral("x"));
		right.setBindingSets(Arrays.<BindingSet>asList(rightb));
	}

	HashJoinIteration iter = new HashJoinIteration(evaluator, left, right, EmptyBindingSet.getInstance(), false);
	BindingSet actual = iter.next();

	assertEquals("1", actual.getValue("a").stringValue());
	assertEquals("2", actual.getValue("b").stringValue());
	assertEquals("x", actual.getValue("i").stringValue());
}
 
Example 5
Source File: HashJoinIterationTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testLeftJoin() throws QueryEvaluationException {
	BindingSetAssignment left = new BindingSetAssignment();
	{
		QueryBindingSet leftb = new QueryBindingSet();
		leftb.addBinding("a", vf.createLiteral("1"));
		leftb.addBinding("i", vf.createLiteral("x"));
		left.setBindingSets(Arrays.<BindingSet>asList(leftb));
	}

	BindingSetAssignment right = new BindingSetAssignment();
	{
		QueryBindingSet rightb = new QueryBindingSet();
		rightb.addBinding("b", vf.createLiteral("2"));
		rightb.addBinding("i", vf.createLiteral("y"));
		right.setBindingSets(Arrays.<BindingSet>asList(rightb));
	}

	HashJoinIteration iter = new HashJoinIteration(evaluator, left, right, EmptyBindingSet.getInstance(), true);
	BindingSet actual = iter.next();

	assertEquals("1", actual.getValue("a").stringValue());
	assertEquals("x", actual.getValue("i").stringValue());
	assertFalse(actual.hasBinding("b"));
}
 
Example 6
Source File: GroupIteratorTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testMaxEmptySet_DefaultGroup() throws QueryEvaluationException {
	Group group = new Group(EMPTY_ASSIGNMENT);
	group.addGroupElement(new GroupElem("max", new Max(new Var("a"))));
	GroupIterator gi = new GroupIterator(evaluator, group, EmptyBindingSet.getInstance());

	assertThat(gi.hasNext()).isTrue();
	assertThat(gi.next().size()).isEqualTo(0);
}
 
Example 7
Source File: RepositoryFederatedService.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param inputBindings
 * @throws QueryEvaluationException
 */
public BatchingServiceIteration(CloseableIteration<BindingSet, QueryEvaluationException> inputBindings,
		int blockSize, Service service) throws QueryEvaluationException {
	super(inputBindings, null, EmptyBindingSet.getInstance());
	this.blockSize = blockSize;
	this.service = service;
	run();
}
 
Example 8
Source File: GroupIteratorTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testSumNotZero() throws QueryEvaluationException {
	Group group = new Group(NONEMPTY_ASSIGNMENT);
	group.addGroupElement(new GroupElem("sum", new Sum(new Var("a"))));
	GroupIterator gi = new GroupIterator(evaluator, group, EmptyBindingSet.getInstance());

	assertThat(gi.next().getBinding("sum").getValue()).isEqualTo(vf.createLiteral("45", XMLSchema.INTEGER));
}
 
Example 9
Source File: GroupIteratorTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testCountNotZero() throws QueryEvaluationException {
	Group group = new Group(NONEMPTY_ASSIGNMENT);
	group.addGroupElement(new GroupElem("count", new Count(new Var("a"))));
	GroupIterator gi = new GroupIterator(evaluator, group, EmptyBindingSet.getInstance());

	assertThat(gi.next().getBinding("count").getValue()).isEqualTo(vf.createLiteral("9", XMLSchema.INTEGER));
}
 
Example 10
Source File: GroupIteratorTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testAvgNotZero() throws QueryEvaluationException {
	Group group = new Group(NONEMPTY_ASSIGNMENT);
	group.addGroupElement(new GroupElem("avg", new Avg(new Var("a"))));
	GroupIterator gi = new GroupIterator(evaluator, group, EmptyBindingSet.getInstance());

	assertThat(gi.next().getBinding("avg").getValue()).isEqualTo(vf.createLiteral("5", XMLSchema.DECIMAL));
}
 
Example 11
Source File: GroupIteratorTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testGroupConcatEmptySet() throws QueryEvaluationException {
	Group group = new Group(EMPTY_ASSIGNMENT);
	group.addGroupElement(new GroupElem("groupconcat", new GroupConcat(new Var("a"))));
	GroupIterator gi = new GroupIterator(evaluator, group, EmptyBindingSet.getInstance());

	assertThat(gi.next().getBinding("groupconcat").getValue())
			.describedAs("GROUP_CONCAT on empty set should result in empty string")
			.isEqualTo(vf.createLiteral(""));
}
 
Example 12
Source File: GroupIteratorTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testSampleEmptySet() throws QueryEvaluationException {
	Group group = new Group(EMPTY_ASSIGNMENT);
	group.addGroupElement(new GroupElem("sample", new Sample(new Var("a"))));
	GroupIterator gi = new GroupIterator(evaluator, group, EmptyBindingSet.getInstance());

	assertThat(gi.hasNext()).isTrue();
	assertThat(gi.next().size()).isEqualTo(0);
}
 
Example 13
Source File: GroupIteratorTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testMinEmptySet() throws QueryEvaluationException {
	Group group = new Group(EMPTY_ASSIGNMENT);
	group.addGroupElement(new GroupElem("min", new Min(new Var("a"))));
	GroupIterator gi = new GroupIterator(evaluator, group, EmptyBindingSet.getInstance());

	assertThat(gi.hasNext()).isTrue();
	assertThat(gi.next().size()).isEqualTo(0);
}
 
Example 14
Source File: GroupIteratorTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testMaxEmptySet_Grouped() throws QueryEvaluationException {
	Group group = new Group(EMPTY_ASSIGNMENT);
	group.addGroupElement(new GroupElem("max", new Max(new Var("a"))));
	group.addGroupBindingName("x"); // we are grouping by variable x

	GroupIterator gi = new GroupIterator(evaluator, group, EmptyBindingSet.getInstance());

	assertThat(gi.hasNext()).isFalse();
}
 
Example 15
Source File: GroupIteratorTest.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testAvgEmptySet() throws QueryEvaluationException {
	Group group = new Group(EMPTY_ASSIGNMENT);
	group.addGroupElement(new GroupElem("avg", new Avg(new Var("a"))));
	GroupIterator gi = new GroupIterator(evaluator, group, EmptyBindingSet.getInstance());

	assertThat(gi.next().getBinding("avg").getValue())
			.describedAs("AVG on empty set should result in 0")
			.isEqualTo(vf.createLiteral("0", XMLSchema.INTEGER));
}
 
Example 16
Source File: HashJoinIteration.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected BindingSet getNextElement() throws QueryEvaluationException {
	Map<BindingSetHashKey, List<BindingSet>> nextHashTable = hashTable;
	if (nextHashTable == null) {
		synchronized (this) {
			nextHashTable = hashTable;
			if (nextHashTable == null) {
				nextHashTable = hashTable = setupHashTable();
			}
		}
	}
	Iterator<BindingSet> nextHashTableValues = hashTableValues;

	while (currentScanElem == null) {
		if (scanList.hasNext()) {
			currentScanElem = nextFromCache(scanList);
		} else {
			disposeCache(scanList); // exhausted so can free

			if (restIter.hasNext()) {
				currentScanElem = restIter.next();
			} else {
				// no more elements available
				return null;
			}
		}

		if (currentScanElem != null) {
			if (currentScanElem instanceof EmptyBindingSet) {
				// the empty bindingset should be merged with all bindingset in the
				// hash table
				Collection<List<BindingSet>> values = nextHashTable.values();
				boolean empty = values.isEmpty() || values.size() == 1 && values.contains(null);
				nextHashTableValues = hashTableValues = empty ? new EmptyIterator<>() : new UnionIterator<>(values);
				if (!nextHashTableValues.hasNext()) {
					currentScanElem = null;
					closeHashValue(nextHashTableValues);
					nextHashTableValues = hashTableValues = null;
				}
			} else {
				BindingSetHashKey key = BindingSetHashKey.create(joinAttributes, currentScanElem);
				List<BindingSet> hashValue = nextHashTable.get(key);
				if (hashValue != null && !hashValue.isEmpty()) {
					nextHashTableValues = hashTableValues = hashValue.iterator();
				} else if (leftJoin) {
					nextHashTableValues = hashTableValues = Collections.singletonList(EmptyBindingSet.getInstance())
							.iterator();
				} else {
					currentScanElem = null;
					closeHashValue(nextHashTableValues);
					nextHashTableValues = hashTableValues = null;
				}
			}
		}
	}

	if (nextHashTableValues != null) {
		BindingSet nextHashTableValue = nextHashTableValues.next();

		QueryBindingSet result = new QueryBindingSet(currentScanElem);

		for (String name : nextHashTableValue.getBindingNames()) {
			if (!result.hasBinding(name)) {
				Value v = nextHashTableValue.getValue(name);
				if (v != null) {
					result.addBinding(name, v);
				}
			}
		}

		if (!nextHashTableValues.hasNext()) {
			// we've exhausted the current scanlist entry
			currentScanElem = null;
			closeHashValue(nextHashTableValues);
			nextHashTableValues = hashTableValues = null;
		}
		return result;
	}

	return EmptyBindingSet.getInstance();
}
 
Example 17
Source File: TripleSourceBase.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private CloseableIteration<BindingSet, QueryEvaluationException> booleanToBindingSetIteration(boolean hasResult) {
	if (hasResult) {
		return new SingleBindingSetIteration(EmptyBindingSet.getInstance());
	}
	return new EmptyIteration<>();
}
 
Example 18
Source File: TripleSourceBase.java    From CostFed with GNU Affero General Public License v3.0 4 votes vote down vote up
private CloseableIteration<BindingSet, QueryEvaluationException> booleanToBindingSetIteration(boolean hasResult) {
	if (hasResult)
		return new SingleBindingSetIteration(EmptyBindingSet.getInstance());
	return new EmptyIteration<BindingSet, QueryEvaluationException>();
}
 
Example 19
Source File: FederationEvalStrategy.java    From CostFed with GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * Evaluate a SERVICE using vectored evaluation, taking the provided bindings as input.
 * 
 * See {@link ControlledWorkerBoundJoin} and {@link Config#getEnableServiceAsBoundJoin()}
 * 
 * @param service
 * @param bindings
 * @return
 * @throws QueryEvaluationException
 */
public CloseableIteration<BindingSet, QueryEvaluationException> evaluateService(FedXService service, final List<BindingSet> bindings) {
	
	return new ServiceJoinIterator(new CollectionIteration<BindingSet, QueryEvaluationException>(bindings), service.getService(), EmptyBindingSet.getInstance(), this);
}