org.apache.jena.sparql.syntax.ElementData Java Examples

The following examples show how to use org.apache.jena.sparql.syntax.ElementData. 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: QueryPatternNormalizer.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(ElementData el) {
    final NodeExprNormalizer nenzer = new NodeExprNormalizer();
    final ElementData nzed = new ElementData();
    final List<Var> vars = el.getVars();
    vars.forEach((v) -> {
        nzed.add(v);
    });
    el.getRows().forEach((b) -> {
        final BindingHashMap binding = new BindingHashMap();
        vars.forEach((v) -> {
            final Node n = b.get(v);
            if (n != null) {
                n.visitWith(nenzer);
                binding.add(v, nenzer.getResult());
            }
        });
        nzed.add(binding);
    });
    endVisit(nzed, nenzer);
}
 
Example #2
Source File: SelectPlan.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
private ElementData mergeValues(final ElementData qData, final List<Var> variables, final List<Binding> values) {
	if (values.isEmpty()) {
		return qData;
	}
	List<Var> vars = qData.getVars();
	if (!Collections.disjoint(vars, variables)) {
		throw new SPARQLExtException("Variables " + vars.retainAll(variables) + "were already bound.");
	}
	ElementData data = new ElementData();
	qData.getVars().forEach(data::add);
	variables.forEach(data::add);
	qData.getRows().forEach((qbinding) -> {
		values.forEach((binding) -> {
			BindingHashMap newb = new BindingHashMap(qbinding);
			variables.forEach((v) -> newb.add(v, binding.get(v)));
			data.add(newb);
		});
	});
	return data;
}
 
Example #3
Source File: LogUtils.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
private static ElementData compress(List<Binding> input) {
    ElementData el = new ElementData();

    if (input.size() < 10) {
        input.forEach((b) -> {
            addCompressedToElementData(el, b);
        });
        return el;
    }
    for (int i = 0; i < 5; i++) {
        addCompressedToElementData(el, input.get(i));
    }
    BindingMap binding = BindingFactory.create();
    Node n = NodeFactory.createLiteral("[ " + (input.size() - 10) + " more ]");
    el.getVars().forEach((v) -> binding.add(v, n));
    el.add(binding);
    for (int i = input.size() - 5; i < input.size(); i++) {
        addCompressedToElementData(el, input.get(i));
    }
    return el;
}
 
Example #4
Source File: LogUtils.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
private static ElementData compress(List<Var> variables, List<Binding> input) {
    ElementData el = new ElementData();
    variables.forEach(el::add);
    if (input.size() < 10) {
        input.forEach((b) -> el.add(compress(variables, b)));
        return el;
    }
    for (int i = 0; i < 5; i++) {
        el.add(compress(variables, input.get(i)));
    }
    BindingMap binding = BindingFactory.create();
    Node n = NodeFactory.createLiteral("[ " + (input.size() - 10) + " more ]");
    variables.forEach((v) -> binding.add(v, n));
    el.add(binding);
    for (int i = input.size() - 5; i < input.size(); i++) {
        el.add(compress(variables, input.get(i)));
    }
    return el;
}
 
Example #5
Source File: ARQTest.java    From tarql with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testQueryAddValuesInQueryPattern() {
	List<Var> header = vars("a", "b");
	Binding b1 = binding(header, "1", "2");
	Binding b2 = binding(header, "3", "4");

	Query q = QueryFactory.create("SELECT * {}");
	ElementGroup group = new ElementGroup();
	ElementData table = new ElementData();
	table.add(Var.alloc("a"));
	table.add(Var.alloc("b"));
	table.add(b1);
	table.add(b2);
	group.addElement(q.getQueryPattern());
	group.addElement(table);
	q.setQueryPattern(group);
	ResultSet rs = QueryExecutionFactory.create(q, 
			ModelFactory.createDefaultModel()).execSelect();

	assertEquals(Arrays.asList(new String[]{"a","b"}), rs.getResultVars());
	assertTrue(rs.hasNext());
	assertEquals(b1, rs.nextBinding());
	assertTrue(rs.hasNext());
	assertEquals(b2, rs.nextBinding());
	assertFalse(rs.hasNext());
}
 
Example #6
Source File: SelectPlan.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
private void augmentQuery(final Query q, final List<Var> variables, final List<Binding> values) {
	if (variables.isEmpty()) {
		return;
	}
	ElementGroup old = (ElementGroup) q.getQueryPattern();
	ElementGroup newQueryPattern = new ElementGroup();
	q.setQueryPattern(newQueryPattern);
	if (old.size() >= 1 && old.get(0) instanceof ElementData) {
		ElementData qData = (ElementData) old.get(0);
		int oldSize = qData.getRows().size();
		qData = mergeValues(qData, variables, values);
		newQueryPattern.addElement(qData);
		for (int i = 1; i < old.size(); i++) {
			newQueryPattern.addElement(old.get(i));
		}
		LOG.debug("New query has " + qData.getRows().size() + " initial values. It had " + oldSize
				+ " values before");
	} else {
		ElementData data = new ElementData();
		variables.forEach(data::add);
		values.forEach(data::add);
		newQueryPattern.addElement(data);
		old.getElements().forEach(newQueryPattern::addElement);
		// unexplainable, but did happen
		check(data, values);
	}
}
 
Example #7
Source File: SelectPlan.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
private void check(ElementData data, Collection<Binding> values) {
	if (data.getRows().size() != values.size()) {
		LOG.warn("Different size for the values block here.\n Was " + values.size() + ": \n" + values + "\n now is "
				+ data.getRows().size() + ": \n" + data.getRows());
		StringBuilder sb = new StringBuilder(
				"Different size for the values block here.\n Was " + values.size() + ": \n" + values + "\n\n");
		int i = 0;
		for (Binding b : values) {
			sb.append("\nbinding ").append(i++).append(" is ").append(b);
		}
		LOG.warn(sb.toString());
	}
}
 
Example #8
Source File: LogUtils.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
private static void addCompressedToElementData(ElementData el, Binding b) {
    final Binding compressed = compress(b);
    final Iterator<Var> varsIterator = compressed.vars();
    while (varsIterator.hasNext()) {
        el.add(varsIterator.next());
    }
    el.add(compressed);
}
 
Example #9
Source File: OutputClauseNormalizer.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(ElementData el) {
    LOG.warn("Should not reach this point");
}
 
Example #10
Source File: OutputClauseNormalizer.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(ElementData el) {
    LOG.warn("Should not reach this point");
}
 
Example #11
Source File: SPARQLExtElementVisitorBase.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(ElementData el) {
}
 
Example #12
Source File: SPARQLExtFormatterElement.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(ElementData el) {
    QuerySerializer.outputDataBlock(out, el.getVars(), el.getRows(), new SerializationContext(context.getPrologue()));
}
 
Example #13
Source File: QueryPatternSimplification.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void visit(ElementData e) {
	result = e;
	
}
 
Example #14
Source File: RuleSystemToUnionQuery.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void visit(ElementData e) {
	result = e;
}
 
Example #15
Source File: FindAllVariables.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void visit(ElementData ed) {
	vars.addAll(ed.getVars());
	
}
 
Example #16
Source File: TarqlQueryExecution.java    From tarql with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Modifies a query so that it operates onto a table. This is achieved
 * by appending the table as a VALUES block to the end of the main
 * query pattern.
 * 
 * @param query Original query; will be modified in place
 * @param table Data table to be added into the query
 */
private void modifyQuery(Query query, final Table table) {
	ElementData tableElement = new ElementData() {
		@Override
		public Table getTable() {
			return table;
		}
	};
	for (Var var: table.getVars()) {
		// Skip ?ROWNUM for "SELECT *" queries -- see further below
		if (query.isSelectType() && query.isQueryResultStar() 
				&& var.equals(TarqlQuery.ROWNUM)) continue;
		tableElement.add(var);
	}
	ElementGroup groupElement = new ElementGroup();
	groupElement.addElement(tableElement);
	if (query.getQueryPattern() instanceof ElementGroup) {
		for (Element element: ((ElementGroup) query.getQueryPattern()).getElements()) {
			groupElement.addElement(element);
		}
	} else {
		groupElement.addElement(query.getQueryPattern());
	}
	query.setQueryPattern(groupElement);
	
	// For SELECT * queries, we don't want to include pseudo
	// columns such as ?ROWNUM that may exist in the table.
	// That's why we skipped ?ROWNUM further up.
	if (query.isSelectType() && query.isQueryResultStar()) {
		// Force expansion of "SELECT *" to actual projection list
		query.setResultVars();
		// Tell ARQ that it actually needs to pay attention to
		// the projection list
		query.setQueryResultStar(false);
		// And now we can add ?ROWNUM to the table, as the "*"
		// has already been expanded.
		tableElement.add(TarqlQuery.ROWNUM);
	}
	// Data can only be added to table after we've finished the
	// ?ROWNUM shenangians
	/*for (Binding binding: table.getRows()) {
		tableElement.add(binding);
	}*/
}