org.apache.jena.sparql.expr.ExprAggregator Java Examples

The following examples show how to use org.apache.jena.sparql.expr.ExprAggregator. 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: ExprNormalizer.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
/**
 * normalizes an expression, substituting every instance of NodeValueNode
 * whose node is a Node_Extended with the associated expression of that
 * Node_Extended.
 *
 * @param expr expression to normalize
 * @return
 */
public Expr normalize(Expr expr) {
    if (expr instanceof ExprFunction1) {
        return normalize((ExprFunction1) expr);
    } else if (expr instanceof ExprFunction2) {
        return normalize((ExprFunction2) expr);
    } else if (expr instanceof ExprFunction3) {
        return normalize((ExprFunction3) expr);
    } else if (expr instanceof ExprFunctionN) {
        return normalize((ExprFunctionN) expr);
    } else if (expr instanceof ExprFunctionOp) {
        return normalize((ExprFunctionOp) expr);
    } else if (expr instanceof NodeValueNode) {
        return normalize((NodeValueNode) expr);
    } else if (expr instanceof ExprAggregator) {
        return normalize((ExprAggregator) expr);
    }
    return expr;
}
 
Example #2
Source File: ExprNormalizer.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
/**
 * normalizes an expression, substituting every instance of NodeValueNode
 * whose node is a Node_Extended with the associated expression of that
 * Node_Extended.
 *
 * @param expr expression to normalize
 * @return
 */
public Expr normalize(Expr expr) {
    if (expr instanceof ExprFunction1) {
        return normalize((ExprFunction1) expr);
    } else if (expr instanceof ExprFunction2) {
        return normalize((ExprFunction2) expr);
    } else if (expr instanceof ExprFunction3) {
        return normalize((ExprFunction3) expr);
    } else if (expr instanceof ExprFunctionN) {
        return normalize((ExprFunctionN) expr);
    } else if (expr instanceof ExprFunctionOp) {
        return normalize((ExprFunctionOp) expr);
    } else if (expr instanceof NodeValueNode) {
        return normalize((NodeValueNode) expr);
    } else if (expr instanceof ExprAggregator) {
        return normalize((ExprAggregator) expr);
    }
    return expr;
}
 
Example #3
Source File: JenaTranslator.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
private Formula handleAggregator(ExprAggregator e, Expression table) {
	Expression lv = toTerm(e.getAggVar().asVar());
	Aggregator agg = e.getAggregator();
	if (agg instanceof AggSample) {
		return lv.in(table);
	} else if (agg instanceof AggMax) {
		Variable elt = Variable.unary("elt");
		return isNumericType(lv.join(QuadTableRelations.literalDatatypes)).and(lv.in(table).and(lv.eq(elt).not().and(less_test(lv, elt)).forSome(elt.oneOf(table)).not()));		
	} else if (agg instanceof AggMin) {
		Variable elt = Variable.unary("elt");
		return isNumericType(lv.join(QuadTableRelations.literalDatatypes)).and(lv.in(table).and(lv.eq(elt).not().and(greater_test(lv, elt)).forSome(elt.oneOf(table)).not()));		
	} else if (agg instanceof AggCount || agg instanceof AggCountVar || agg instanceof AggCountVarDistinct) {
		return intValue(lv).eq(table.count());
	} else {
		assert false : agg;
		return null;
	}
}
 
Example #4
Source File: ExprNormalizer.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
private Expr normalize(ExprAggregator eAgg) {
    final ExprList exprList = eAgg.getAggregator().getExprList();
    if (exprList == null) {
        return eAgg;
    } else {
        final Var v = eAgg.getVar();
        final ExprList x = new ExprList();
        for (Expr e : exprList) {
            x.add(normalize(e));
        }
        Aggregator agg = eAgg.getAggregator().copy(x);
        return new ExprAggregator(v, agg);
    }
}
 
Example #5
Source File: SPARQLExtFmtExprSPARQL.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(ExprAggregator eAgg) {
	final Aggregator agg = eAgg.getAggregator();
	if (agg instanceof AggCount) {
		out.print("COUNT(*)");
		return;
	}
	if (agg instanceof AggCountDistinct) {
		out.print("COUNT(DISTINCT *)");
		return;
	}
	out.print(agg.getName());
	out.print("(");
	String clazz = agg.getClass().getSimpleName();
	if (clazz.endsWith("Distinct")) {
		out.print("DISTINCT ");
	}
	if (agg.getExprList() != null) {
		SPARQLExtFmtUtils.fmtSPARQL(out, agg.getExprList(), context);
	}
	String separator = null;
	if (agg instanceof AggGroupConcat) {
		separator = ((AggGroupConcat) agg).getSeparator();
	}
	if (agg instanceof AggGroupConcatDistinct) {
		separator = ((AggGroupConcatDistinct) agg).getSeparator();
	}
	if (separator != null) {
		out.print(" ; SEPARATOR=");
		String y = StrUtils.escapeString(separator);
		out.print("'");
		out.print(y);
		out.print("'");
	}
	out.print(")");
}
 
Example #6
Source File: ExprNormalizer.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
private Expr normalize(ExprAggregator eAgg) {
	Var var = VarUtils.allocVar(UUID.randomUUID().toString().substring(0, 8));
	query.addPostSelect(var, eAgg);
	return new ExprVar(var);
}
 
Example #7
Source File: ResolutionEngineForJena.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
/***
 * Takes a SELECT query and unroll it into a more complex SPARQL query under
 * rules
 * 
 * @param query
 */
public Query unfold(Query query) {

	ResolutionVisitor visitor = new ResolutionVisitor(rules);
	Op body = Algebra.compile(query);

	OpVariableVistor<String> collector = new OpVariableVistor<String>(body,
			true) {
		@Override
		protected String processVar(Node v) {
			return v.getName();
		}
	};
	OpWalker.walk(body, collector);

	Set<String> bindingNames = new HashSet<String>(collector.getResult());

	visitor.setBindingNames(bindingNames);

	Op newQuery = body;
	newQuery = Transformer.transform(visitor, body);

	
	VarExprList expr = new VarExprList();	
//	E_StrConcat trace = getRuleTracingConcat(newQuery);
	E_StrConcat trace = null;
	
	if (trace!=null) {
		expr.add(Var.alloc("trace"),trace);		
		newQuery = OpExtend.extend(newQuery, expr);
		newQuery = OpJoin.create(newQuery, OpTable.unit());
		
		if (SPARQLRewriterForJena.GENERATE_TRACE_SUMMARY) {
			
			VarExprList groupVars = new VarExprList();
			groupVars.add(Var.alloc("trace"));
			AggCount count = new AggCount();
			ExprAggregator aggregators = new ExprAggregator(Var.alloc("trace.sum"), count);
			List<ExprAggregator> list = new LinkedList<ExprAggregator>();
			list.add(aggregators);
			
			Op groupBy = new OpGroup(newQuery, groupVars, list);
			
			VarExprList expr2 = new VarExprList();	
			expr2.add(Var.alloc("sum"), new ExprVar("trace.sum"));			
			groupBy = OpExtend.extend(groupBy, expr2);
			List<Var> vars = new LinkedList<Var>();
			vars.add(Var.alloc("sum"));
			vars.add(Var.alloc("trace"));
			OpProject project = new OpProject(groupBy, vars);
			newQuery = project;
			
			
		}

	}
	
	
	
	
	
	Query q = OpAsQuery.asQuery(newQuery);
	q.setDistinct(true);	// KAVITHA: Not setting distinct on the consequent leaves duplicates
	return q;

}
 
Example #8
Source File: Query.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public List<ExprAggregator> getAggregators() {
	throw new RdfStoreException("Not implemented");
}