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

The following examples show how to use org.apache.jena.sparql.syntax.ElementFilter. 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: labelSearch.java    From xcurator with Apache License 2.0 6 votes vote down vote up
private QueryIterator buildSyntax(QueryIterator input, Node nodeVar, String pattern, ExecutionContext execCxt)
{
    Var var2 = createNewVar() ; 
    // Triple patterns for   ?x rdfs:label ?hiddenVar
    ElementTriplesBlock elementBGP = new ElementTriplesBlock();
    Triple t = new Triple(nodeVar, RDFS.label.asNode(), var2) ;
    elementBGP.addTriple(t) ;
    
    // Regular expression for  regex(?hiddenVar, "pattern", "i") 
    Expr regex = new E_Regex(new ExprVar(var2.getName()), pattern, "i") ;
    
    ElementGroup elementGroup = new ElementGroup() ;
    elementGroup.addElement(elementBGP) ;
    elementGroup.addElement(new ElementFilter(regex)) ;
    // Compile it.
    // The better design is to build the Op structure programmatically,
    Op op = Algebra.compile(elementGroup) ;
    op = Algebra.optimize(op, execCxt.getContext()) ;
    return QC.execute(op, input, execCxt) ;
}
 
Example #2
Source File: ConjunctiveQuery.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean equals(Object obj) {
	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (getClass() != obj.getClass())
		return false;
	ConjunctiveQuery other = (ConjunctiveQuery) obj;
	if (!getResultVars().equals(other.getResultVars())) {
		return false;
	}
	if (!new HashSet<Triple>(getTriples()).equals(new HashSet<Triple>(other.getTriples()))) {
		return false;
	}
	if (!new HashSet<ElementFilter>(getFilters()).equals(new HashSet<ElementFilter>(other.getFilters()))) {
		return false;
	}	
	return true;
}
 
Example #3
Source File: SPARQLExtFormatterElement.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(ElementFilter el) {
    out.print("FILTER ");
    Expr expr = el.getExpr();
    SPARQLExtFmtExprSPARQL v = new SPARQLExtFmtExprSPARQL(out, context);

    // This assumes that complex expressions are bracketted
    // (parens) as necessary except for some cases:
    // Plain variable or constant
    boolean addParens = false;
    if (expr.isVariable()) {
        addParens = true;
    }
    if (expr.isConstant()) {
        addParens = true;
    }

    if (addParens) {
        out.print("( ");
    }
    v.format(expr);
    if (addParens) {
        out.print(" )");
    }
}
 
Example #4
Source File: OWLQLCompiler.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
protected ConjunctiveQuery replace(ConjunctiveQuery q, Triple oldT, Triple newT) {
	ElementTriplesBlock newPattern = new ElementTriplesBlock();
	for (Triple t:q.getTriples()) {
		if (t.equals(oldT)) {
			newPattern.addTriple(newT);
		} else {
			newPattern.addTriple(t);
		}
	}
	ConjunctiveQuery ret = q.cloneConjQuery();
	ret.setQueryPattern(newPattern);
	for (ElementFilter ef: q.getFilters()) {
		ret.addFilter(ef.getExpr());
	}
	return ret;
}
 
Example #5
Source File: ConjunctiveQuery.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
public ConjunctiveQuery cloneConjQuery(boolean copyResultVars) {
	//return new ConjunctiveQuery(query.cloneQuery());
	
	ConjunctiveQuery ret = new ConjunctiveQuery();
	ElementTriplesBlock pattern = new ElementTriplesBlock();
	for (Triple t: getTriples()) {
		pattern.addTriple(new Triple(t.getSubject(), t.getPredicate(), t.getObject()));
	}
	ret.setQueryPattern(pattern);
	for (ElementFilter f: getFilters()) {
		ret.addFilter(f.getExpr());
	}
	if (copyResultVars) {
		for (String v: getResultVars()) {
			ret.query.addResultVar(v);
		}
	}
	return ret;
}
 
Example #6
Source File: ConjunctiveQuery.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
public void setQueryPattern(Element graphPattern) {
	if (graphPattern instanceof ElementTriplesBlock) {
		query.setQueryPattern((ElementTriplesBlock)graphPattern);
	} else if (graphPattern instanceof ElementGroup) {
		ElementGroup group = (ElementGroup) graphPattern;
		for (Element e: flatten(group)) {
			if (e instanceof ElementPathBlock) {
				ElementPathBlock epb = (ElementPathBlock) e;
				for (TriplePath p:epb.getPattern().getList()) {
					if (!p.isTriple()) {
						throw new IllegalArgumentException("Conjunctive query only accepts a basic graph patterns. No triple path:  "+ p+"\n"+graphPattern );	
					}
				}
			} else 	if (!(e instanceof ElementTriplesBlock)
			&&  !(e instanceof ElementFilter)) {
				throw new IllegalArgumentException("Conjunctive query only accepts a basic graph patterns: "+ e+"\n"+graphPattern );	
			}
		}
		query.setQueryPattern(graphPattern);
	}
	else {
		throw new IllegalArgumentException("Conjunctive query only accepts a basic graph patterns: "+ graphPattern);				
	}
	
}
 
Example #7
Source File: ConjunctiveQuery.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public void addFilter(ElementFilter filter) {
	if (query.getQueryPattern() instanceof ElementTriplesBlock) {
		ElementGroup group = new ElementGroup();
		group.addElement(query.getQueryPattern());
		group.addElementFilter(filter);
		query.setQueryPattern(group);
	} else {
		assert query.getQueryPattern() instanceof ElementGroup;
		ElementGroup group = (ElementGroup) query.getQueryPattern();
		group.addElementFilter(filter);
	}
}
 
Example #8
Source File: ConjunctiveQuery.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public List<ElementFilter>  getFilters() {
	if (query.getQueryPattern() instanceof ElementTriplesBlock) {
		return new ArrayList<ElementFilter>();
	} else {
		assert query.getQueryPattern() instanceof ElementGroup;
		List<Element> es = flatten(query.getQueryPattern());
		List<ElementFilter> ret = new ArrayList<ElementFilter>();
		for (Element e: es) {
			if (e instanceof ElementFilter) {
				ret.add((ElementFilter) e);
			}
		}
		return ret;
	}
}
 
Example #9
Source File: ExProg2.java    From xcurator with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args)
{
    Model model = createModel() ;
    
    Query query = QueryFactory.make() ;
    query.setQuerySelectType() ;
    
    // See also ExProg1
    
    ElementGroup elg = new ElementGroup() ;
    
    Var varTitle = Var.alloc("title") ;
    Var varX = Var.alloc("x") ;
    
    Triple t1 = new Triple(varX, DC.title.asNode(),  varTitle) ;
    elg.addTriplePattern(t1) ;
    
    // Adds a filter.  Need to wrap variable in a NodeVar.
    Expr expr = new E_Regex(new ExprVar(varTitle), "sparql", "i") ;
    ElementFilter filter = new  ElementFilter(expr) ;
    elg.addElementFilter(filter) ;
    
    // Attach the group to query.  
    query.setQueryPattern(elg) ;
    
    // Choose what we want - SELECT ?title
    query.addResultVar(varTitle) ;
    
    // Print query with line numbers
    // Prefix mapping just helps serialization
    query.getPrefixMapping().setNsPrefix("dc" , DC.getURI()) ;
    query.serialize(new IndentedWriter(System.out,true)) ;
    System.out.println() ;
    
    try(QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
        // Assumption: it's a SELECT query.
        ResultSet rs = qexec.execSelect() ;
        
        // The order of results is undefined.
        System.out.println("Titles: ") ;
        for ( ; rs.hasNext() ; )
        {
            QuerySolution rb = rs.nextSolution() ;
            
            // Get title - variable names do not include the '?' (or '$')
            RDFNode x = rb.get("title") ;
            
            // Check the type of the result value
            if ( x instanceof Literal )
            {
                Literal titleStr = (Literal)x  ;
                System.out.println("    "+titleStr) ;
            }
            else
                System.out.println("Strange - not a literal: "+x) ;
                
        }
    }
}
 
Example #10
Source File: TriplePatternExtractor.java    From NLIWOD with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void visit(final ElementFilter el) {
	filterCount++;
}
 
Example #11
Source File: FindAllVariables.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void visit(ElementFilter e) {
	ExprVars.varsMentioned(vars, e.getExpr());
}
 
Example #12
Source File: ConjunctiveQuery.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
public void addFilter(Expr expr) {
	addFilter(new ElementFilter(expr));
}
 
Example #13
Source File: RuleSystemToUnionQuery.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void visit(ElementFilter filter) {
	org.apache.jena.sparql.expr.Expr expr = filter.getExpr();
	expr = expr.copySubstitute(this);
	result = new ElementFilter(expr);
}
 
Example #14
Source File: QueryPatternSimplification.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void visit(ElementFilter e) {
	result = e;
	
}
 
Example #15
Source File: SPARQLExtElementVisitorBase.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(ElementFilter el) {
}
 
Example #16
Source File: OutputClauseNormalizer.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(ElementFilter el) {
    LOG.warn("Should not reach this point");
}
 
Example #17
Source File: QueryPatternNormalizer.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(ElementFilter el) {
    final ExprNormalizer enzer = new ExprNormalizer();
    final Expr nzed = enzer.normalize(el.getExpr());
    result = new ElementFilter(nzed);
}
 
Example #18
Source File: OutputClauseNormalizer.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(ElementFilter el) {
    LOG.warn("Should not reach this point");
}
 
Example #19
Source File: StatisticsVisitor.java    From IGUANA with GNU Affero General Public License v3.0 votes vote down vote up
public void startElement(ElementFilter el) {this.filter=true;el.getExpr();}