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

The following examples show how to use org.apache.jena.sparql.syntax.ElementTriplesBlock. 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: OCUtils.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
public static Set<ConjunctiveQuery> getMembershipQuery(OWLOntology ont) {
	Set<ConjunctiveQuery> ret = new HashSet<ConjunctiveQuery>();
	for (OWLClass owlclass: ont.getClassesInSignature(true)) {
		ElementTriplesBlock p = new ElementTriplesBlock();
		String x = "x";
		Node sub = NodeFactory.createVariable(x);
		Node pred = NodeFactory.createURI(RDFConstants.RDF_TYPE);
		Node obj = NodeFactory.createURI(owlclass.getIRI().toString());
		Triple qt = new Triple(sub, pred, obj);
		p.getPattern().add(qt);
		ConjunctiveQuery cq = new ConjunctiveQuery();
		cq.setQueryPattern(p);
		cq.addResultVar(x);
		cq.setDistinct(true);
		ret.add(cq);
	}
	return ret;
}
 
Example #3
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 #4
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 #5
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 #6
Source File: OWLQLSPARQLCompiler.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Element transform(ElementTriplesBlock tb) {
	List<Triple> triples = tb.getPattern().getList();
	// convert set of triples to a rule system
	// here is where all variables in triples are assumed to be distinguished variables
	logger.debug("Triples:\n{}", triples);
	RuleSystem rs = new TriplesToRuleSystem(tbox).toRules(triples, allVars, true);
	logger.debug("Triples as rule system:\n{}", rs);
	//
	RuleSystem outrs = compileToNonRecursiveDatalog(rs);
	logger.debug("RuleSystem:\n{}", outrs );
	if (outrs!=null) {
		Query q = RuleSystemToUnionQuery.toUnionQuery(outrs);
		return q.getQueryPattern();		
	} else {
		/*ElementTriplesBlock ts = new ElementTriplesBlock();
		Node owlNothing =  Node.createURI(OWL.Nothing.getURI());
		Node rdfType  = Node.createURI(RDF.type.getURI());
		ts.addTriple(new Triple(owlNothing, rdfType, owlNothing));
		result = ts;*/
		return tb;
	}
}
 
Example #7
Source File: SHACLPaths.java    From shacl with Apache License 2.0 6 votes vote down vote up
/**
 * Attempts to parse a given string into a Jena Path.
 * Throws an Exception if the string cannot be parsed.
 * @param string  the string to parse
 * @param model  the Model to operate on (for prefixes)
 * @return a Path or a Resource if this is a URI
 */
public static Object getJenaPath(String string, Model model) throws QueryParseException {
	Query query = ARQFactory.get().createQuery(model, "ASK { ?a \n" + string + "\n ?b }");
	Element element = query.getQueryPattern();
	if(element instanceof ElementGroup) {
		Element e = ((ElementGroup)element).getElements().get(0);
		if(e instanceof ElementPathBlock) {
			Path path = ((ElementPathBlock) e).getPattern().get(0).getPath();
			if(path instanceof P_Link && ((P_Link)path).isForward()) {
				return model.asRDFNode(((P_Link)path).getNode());
			}
			else {
				return path;
			}
		}
		else if(e instanceof ElementTriplesBlock) {
			return model.asRDFNode(((ElementTriplesBlock) e).getPattern().get(0).getPredicate());
		}
	}
	throw new QueryParseException("Not a SPARQL 1.1 Path expression", 2, 1);
}
 
Example #8
Source File: QueryPatternNormalizer.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(ElementTriplesBlock el) {
    final BasicPattern bgp = el.getPattern();
    final ElementTriplesBlock nzed = new ElementTriplesBlock();
    final NodeExprNormalizer nenzer = new NodeExprNormalizer();
    bgp.forEach((t) -> {
        t.getSubject().visitWith(nenzer);
        Node s = nenzer.getResult();
        t.getPredicate().visitWith(nenzer);
        Node p = nenzer.getResult();
        t.getObject().visitWith(nenzer);
        Node o = nenzer.getResult();
        nzed.addTriple(new Triple(s, p, o));
    });
    endVisit(nzed, nenzer);
}
 
Example #9
Source File: FindAllVariables.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void visit(ElementTriplesBlock e) {
	for (Triple t: e.getPattern().getList()) {
		addVars(t);
	}
	
}
 
Example #10
Source File: SPARQLExtFormatterElement.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(ElementTriplesBlock el) {
    if (el.isEmpty()) {
        out.println("# Empty BGP");
        return;
    }
    formatTriples(el.getPattern());
}
 
Example #11
Source File: QueryPatternSimplification.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void visit(ElementGroup group) {
	//remove nested groups
	ElementGroup ret = new ElementGroup();
	ElementPathBlock pathBlock = new ElementPathBlock();
	for (Element elt: group.getElements()) {
		elt.visit(this);
		if (result instanceof ElementGroup) {
			ElementGroup subgroup = (ElementGroup) result;
			// add the subgroup content directly
			add(ret, pathBlock, subgroup);
		} else if (result instanceof ElementPathBlock) {
			if (pathBlock.isEmpty()) {
				ret.addElement(pathBlock);
			}
			add(pathBlock, (ElementPathBlock) result);
		} else if (result instanceof ElementTriplesBlock) {
			if (pathBlock.isEmpty()) {
				ret.addElement(pathBlock);
			}
			add(pathBlock, (ElementTriplesBlock) result);
			
		} else {
			ret.addElement(result);
		}
		
	}
	if (ret.getElements().size() == 1) {
		result = ret.getElements().get(0);
	} else {
		result = ret;
	}
}
 
Example #12
Source File: OCUtils.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public static ElementTriplesBlock toTriples(ElementPathBlock pb) {
	ElementTriplesBlock ret = new ElementTriplesBlock(); 
	for (TriplePath tp :pb.getPattern().getList()) {
		if (tp.isTriple()) {
			ret.addTriple(tp.asTriple());
		} else {
			throw new RuntimeException("Path query not supported");
		}
	} 
	return ret;
}
 
Example #13
Source File: TriplePatternExtractor.java    From NLIWOD with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void visit(final ElementTriplesBlock el) {
	for (Iterator<Triple> iterator = el.patternElts(); iterator.hasNext();) {
		Triple t = iterator.next();
		if (inOptionalClause) {
			optionalTriplePattern.add(t);
		} else {
			triplePattern.add(t);
		}
	}
}
 
Example #14
Source File: RuleSystemToUnionQuery.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void visit(ElementTriplesBlock tb) {
	ElementTriplesBlock ret = new ElementTriplesBlock();
	for (Triple t:tb.getPattern().getList()) {
		ret.addTriple(copySubstitute(t));
	}
	result = ret;
}
 
Example #15
Source File: PropertyPathRewrite.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void visit(P_Link p) {
	Triple t = new Triple(subject, p.getNode(), object);
	ElementTriplesBlock e = new ElementTriplesBlock();
	e.addTriple(t);
	result = e;
	
}
 
Example #16
Source File: NormalizedOWLQLTbox.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
protected void addOnlyAboxTriples(ElementUnion patterns, List<Triple> triples){
	boolean isAbsentFromAbox = false;
	ElementTriplesBlock sp = new ElementTriplesBlock();
	for (Triple t : triples) {
		if (isTripleAbsentFromAbox(t)) {
			isAbsentFromAbox = true;
			break;
		}
		sp.addTriple(t);
	}
	if (!isAbsentFromAbox)
		patterns.addElement(sp);
}
 
Example #17
Source File: PropertyPathRewrite.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void visit(P_ReverseLink p) {
	Triple t = new Triple(object, p.getNode(), subject);
	ElementTriplesBlock e = new ElementTriplesBlock();
	e.addTriple(t);
	result = e;
}
 
Example #18
Source File: QueryPatternSimplification.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
protected void add(ElementPathBlock targetPathBlock, ElementTriplesBlock newTriples) {
	for (Triple t:newTriples.getPattern().getList()) {
		targetPathBlock.addTriple(t);
	}
}
 
Example #19
Source File: QueryPatternSimplification.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void visit(ElementTriplesBlock e) {
	result = e;
	
}
 
Example #20
Source File: SPARQLExtFormatterElement.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
private static boolean needsDotSeparator(Element el) {
    return (el instanceof ElementTriplesBlock) || (el instanceof ElementPathBlock);
}
 
Example #21
Source File: SPARQLExtElementVisitorBase.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(ElementTriplesBlock el) {
}
 
Example #22
Source File: OutputClauseNormalizer.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(ElementTriplesBlock el) {
    LOG.warn("Should not reach this point");
}
 
Example #23
Source File: OutputClauseNormalizer.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(ElementTriplesBlock el) {
    LOG.warn("Should not reach this point");
}