Java Code Examples for org.apache.jena.query.Query#setDistinct()

The following examples show how to use org.apache.jena.query.Query#setDistinct() . 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: RuleSystemToUnionQuery.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
public static Query  toUnionQuery(RuleSystem nonRecursiveSys) {
	 Map<Predicate, Query> headPred2Query = new HashMap<Predicate, Query>();
	 AbstractRDFStoreDBSchemaProcessor pr = new  AbstractRDFStoreDBSchemaProcessor() {
		protected Object getID(ConstantExpr exp) {
			return exp.getValue();
		}
		protected Object getID(Predicate p) {
			try {
				return new URI(p.getName());
			} catch (URISyntaxException e) {
				throw new RuntimeException(e);
			}
		}
	 };
	 pr.setRuleSystem(nonRecursiveSys);
	 RuleSystem newSys = pr.convertDLPredicateToDBTablePredicate();
	 logger.debug("Rulesystem after transformation of dl predicate: {}", newSys);
	 
	 String prefix =  OWLQLCompiler.UNBOUND_VARIABLE_PREFIX;
	 int suffixStart = OCUtils.nextAvailableSuffixVariable(newSys.getAllVariableNames(), prefix	);
	 NewVariableGenerator varGen = new NewVariableGenerator(prefix, suffixStart);
	 Query ret =  getUnionQuery(newSys,nonRecursiveSys.getMainHeadFormula().getPredicate() , headPred2Query, varGen);
	 ret.setDistinct(nonRecursiveSys.areResultsForMainHeadFormulaDistinct());
	 return ret;
}
 
Example 2
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;

}