org.openrdf.query.algebra.QueryModelNode Java Examples

The following examples show how to use org.openrdf.query.algebra.QueryModelNode. 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: RuleTransformationVisitor.java    From neo4j-sparql-extension with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the rules that may be applicable for a given node.
 * 
 * @param node a node
 * @return rules that may be applicable to the node
 */
private ArrayList<Rule> getRules(QueryModelNode node) {
	// check if there is an entry in the map
	if (applied.containsKey(node)) {
		return applied.get(node);
	} else {
		// if there is no entry in the map,
		// traverse branch up until root to find a reduced rule set
		QueryModelNode parent = node.getParentNode();
		while (parent != null) {
			if (applied.containsKey(parent)) {
				return applied.get(parent);
			} else {
				parent = parent.getParentNode();
			}
		}
		// if there are no reduced rule sets return the full set
		return rules;
	}
}
 
Example #2
Source File: RuleTransformationVisitor.java    From neo4j-sparql-extension with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Visits a statement pattern and tries to transform using all rules
 * in the given rule set.
 * 
 * Tries to apply a rule from the rule set. If it is applicable
 * it will recurse using the visitor pattern to the nodes returned
 * from the rule after application.
 * @param node
 * @throws RuntimeException 
 */
@Override
public void meet(StatementPattern node) throws RuntimeException {
	// get a list of rules that may be applicable
	ArrayList<Rule> toApply = new ArrayList<>(getRules(node));
	for (Rule r : toApply) {
		// if a rule can be applied
		if (r.canApply(node)) {
			// apply the rule
			List<QueryModelNode> next = r.apply(node);
			// visit all nodes that the rule returned
			for (QueryModelNode toVisit : next) {
				removeRule(toApply, toVisit, r);
				toVisit.visit(this);
			}
			// halt execution, because the rule transformed the original
			// node and this visit can't transform further. Remaining rules
			// are applied in the recursion (see above).
			break;
		}
	}
}
 
Example #3
Source File: InverseObjectProperties.java    From neo4j-sparql-extension with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Transform a statement pattern according to OWL-2 inverse properties
 * axiom.
 *
 * @param node the node to transform
 * @return list of nodes to visit next
 */
@Override
public List<QueryModelNode> apply(StatementPattern node) {
	List<QueryModelNode> next = newNextList();
	Var s = node.getSubjectVar();
	Var p = node.getPredicateVar();
	Var o = node.getObjectVar();
	Var c = node.getContextVar();
	URI uri = (URI) p.getValue();
	String op = uri.stringValue();
	Var p2;
	// check if need to replace with op1 or op2
	if (op.equals(op1)) {
		p2 = new ConstVar(vf.createURI(op2));
	} else {
		p2 = new ConstVar(vf.createURI(op1));
	}
	StatementPattern left = node.clone();
	// switch subject and object and replace predicate
	StatementPattern right = new StatementPattern(o, p2, s, c);
	node.replaceWith(new Union(left, right));
	next.add(left);
	next.add(right);
	return next;
}
 
Example #4
Source File: SubPropertyOf.java    From neo4j-sparql-extension with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Transform a statement pattern according to OWL-2 subproperty axiom.
 *
 * @param node the node to transform
 * @return list of nodes to visit next
 */
@Override
public List<QueryModelNode> apply(StatementPattern node) {
	List<QueryModelNode> next = newNextList();
	StatementPattern left = node.clone();
	// replace the predicate with the subproperty
	StatementPattern right
			= new StatementPattern(
					node.getSubjectVar(),
					new ConstVar(vf.createURI(op1)),
					node.getObjectVar(),
					node.getContextVar());
	node.replaceWith(
			new Union(left, right));
	next.add(left);
	next.add(right);
	return next;
}
 
Example #5
Source File: SubClassOf.java    From neo4j-sparql-extension with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Transform a statement pattern according to OWL-2 subclass axiom.
 *
 * @param node the node to transform
 * @return list of nodes to visit next
 */
@Override
public List<QueryModelNode> apply(StatementPattern node) {
	List<QueryModelNode> next = newNextList();
	StatementPattern left = node.clone();
	// replace the object with the subclass
	StatementPattern right
			= new StatementPattern(
					node.getSubjectVar(),
					node.getPredicateVar(),
					new ConstVar(vf.createURI(ce1)),
					node.getContextVar());
	node.replaceWith(
			new Union(left, right));
	next.add(left);
	next.add(right);
	return next;
}
 
Example #6
Source File: RuleTransformationVisitor.java    From neo4j-sparql-extension with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Removes a rule from a rule set for a node.
 * 
 * If there is no entry in the map use the base rule set instead and
 * save it in the map.
 * @param base the rule set to use if there is no reduced set in the map
 * @param node a node
 * @param r the rule to remove
 */
private void removeRule(List<Rule> base, QueryModelNode node, Rule r) {
	if (applied.containsKey(node)) {
		applied.get(node).remove(r);
	} else {
		ArrayList<Rule> reduced = new ArrayList<>(base);
		reduced.remove(r);
		applied.put(node, reduced);
	}
}
 
Example #7
Source File: QueryModelNodeTree.java    From neo4j-sparql-extension with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns the root of a node in a query model tree.
 * 
 * @param node the node to traverse up from
 * @return the root of the tree
 */
public static QueryModelNode getRoot(QueryModelNode node) {
	QueryModelNode parent;
	parent = node;
	while(parent.getParentNode() != null) {
		parent = parent.getParentNode();
	}
	return parent;
}
 
Example #8
Source File: QueryModelNodeTree.java    From neo4j-sparql-extension with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns the distance to the root for a node in a query model tree.
 * 
 * @param node the node to traverse up from
 * @return the distance to the root node
 */
public static int getRootDistance(QueryModelNode node) {
	int i = 0;
	QueryModelNode parent;
	parent = node;
	while(parent.getParentNode() != null) {
		parent = parent.getParentNode();
		i++;
	}
	return i;
}
 
Example #9
Source File: ObjectPropertyChain.java    From neo4j-sparql-extension with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Transform a statement pattern according to OWL-2 property chain
 * axiom.
 * 
 * @param node the node to transform
 * @return list of nodes to visit next
 */
@Override
public List<QueryModelNode> apply(StatementPattern node) {
	List<QueryModelNode> next = newNextList();
	Var s = node.getSubjectVar();
	Var o = node.getObjectVar();
	Var c = node.getContextVar();
	TupleExpr left  = node.clone();
	TupleExpr right = getChain(s, o, c);
	node.replaceWith(new Union(left, right));
	next.add(left);
	next.add(right);
	return next;
}
 
Example #10
Source File: ConsistencyVisitor.java    From neo4j-sparql-extension with GNU General Public License v3.0 5 votes vote down vote up
private void checkParentChild(QueryModelNode node) {
	QueryModelNode parent = node.getParentNode();
	SeenVisitor seen = new SeenVisitor();
	if (parent != null) {
		parent.visitChildren(seen);
		if (!seen.getSeen().contains(node)) {
			throw new IllegalStateException("Node is not a child node! " + node);
		}
	}
}
 
Example #11
Source File: PredicateVariable.java    From neo4j-sparql-extension with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Transform a statement pattern to infer triples for a predicate variable.
 * 
 * @param node the node to transform
 * @return list of nodes to visit next
 */
@Override
public List<QueryModelNode> apply(StatementPattern node) {
	List<QueryModelNode> next = newNextList();
	StatementPattern left = node.clone();
	next.add(left);
	TupleExpr right = assignPredicates(predicates, node.clone(), next);
	node.replaceWith(new Union(left, right));
	return next;
}
 
Example #12
Source File: ConsistencyVisitor.java    From neo4j-sparql-extension with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void meetOther(QueryModelNode node) throws RuntimeException {
	check(node);
	super.meetOther(node);
}
 
Example #13
Source File: ResolutionEngine.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
/****
 * If the visited pattern unifies with rule, and the node has not been
 * visited, it will replace the node (in the nodes parent) with a union
 * consisting of the node unified with the body of all the rules that
 * unified with the node.
 */
@Override
public void meet(StatementPattern node) {

	if (visited.contains(node))
		return;

	visited.add(node);
	QueryModelNode parent = node.getParentNode();

	List<TupleExpr> nodeAlternatives = new LinkedList<TupleExpr>();
	nodeAlternatives.add(node);

	for (Rule rule : rules) {
		
		try {
			rule = rule.getFreshRule();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} 
		
		Substitution s = StatementUnifier.getMGU(node, rule.consequent);
		if (s == null)
			continue;

		// The rule matches the node, replacing (executing a resolution
		// step)

		// Preparing the body of the rule
		TupleExpr body = rule.antecedent;
		SubstitutionApplier app = new SubstitutionApplier(s);
		body.visit(app);
		
		/* If the substitution affects any variables in the domain of the original query
		then we need to add a BIND(...) element, using extensions, for example. If x is in 
		the original query, and we have the substition x/<example.org> we need the 
		extension BIND(<example.org> as ?x)
		*/
		Extension ex = new Extension();
		for (Var var: s.getMap().keySet()) {
			String name = var.getName();
			if (!queryBindingNames.contains(name)) {
				continue;
			}
			
			Var expr = s.get(var);
			ex.addElement(new ExtensionElem(expr, name));
		}
		
		TupleExpr newExpression;
		
		if (!ex.getElements().isEmpty()) {
			ex.setArg(body);
			newExpression = ex;
		} else {
			newExpression = body;
		}
		
		nodeAlternatives.add(newExpression);
	}

	if (nodeAlternatives.size() == 1) {
		// There was no resolution, no change
		return;
	}

	// constructing UNION operator, a binary tree, removing 2 at a time
	Union union = null;
	while (!nodeAlternatives.isEmpty()) {
		Union newunion = null;
		if (union == null) {
			newunion = new Union(nodeAlternatives.remove(0), nodeAlternatives.remove(0));

		} else {
			newunion = new Union(nodeAlternatives.remove(0), union);
		}
		union = newunion;
	}

	// replacing the node with the constructed union
	parent.replaceChildNode(node, union);
	
	
	producedChange = true;

}
 
Example #14
Source File: ResolutionEngine.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
public ResolutionVisitor(List<Rule> rules) {
	this.rules = rules;
	this.visited = new HashSet<QueryModelNode>();
}
 
Example #15
Source File: BasicGraphPatternExtractor.java    From GeoTriples with Apache License 2.0 4 votes vote down vote up
public static List<TupleExpr> process(QueryModelNode node) {
	BasicGraphPatternExtractor ex = new BasicGraphPatternExtractor();
	node.visit(ex);
	return ex.bgpList;
}
 
Example #16
Source File: ConsistencyVisitor.java    From neo4j-sparql-extension with GNU General Public License v3.0 4 votes vote down vote up
private void check(QueryModelNode node) {
	checkParentChild(node);
}
 
Example #17
Source File: SeenVisitor.java    From neo4j-sparql-extension with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void meetOther(QueryModelNode node) throws RuntimeException {
	setSeen(node);
	super.meetOther(node);
}
 
Example #18
Source File: SeenVisitor.java    From neo4j-sparql-extension with GNU General Public License v3.0 4 votes vote down vote up
public Set<QueryModelNode> getSeen() {
	return seen.keySet();
}
 
Example #19
Source File: SeenVisitor.java    From neo4j-sparql-extension with GNU General Public License v3.0 4 votes vote down vote up
private void setSeen(QueryModelNode node) {
	seen.put(node, null);
}
 
Example #20
Source File: Rule.java    From neo4j-sparql-extension with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Transforms a statement pattern to include inference.
 *
 * @param node the statement pattern to transform
 * @return a list of expressions that still need to be transformed
 */
public List<QueryModelNode> apply(StatementPattern node);
 
Example #21
Source File: AbstractRule.java    From neo4j-sparql-extension with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Creates a new list that can be used as a reference to nodes that need to
 * be visited after rule application.
 *
 * @return new query model node list
 */
protected List<QueryModelNode> newNextList() {
	return new LinkedList<>();
}
 
Example #22
Source File: StatementUnifier.java    From quetzal with Eclipse Public License 2.0 2 votes vote down vote up
private static void applySubstitution(Substitution s, QueryModelNode node) {
	
		SubstitutionApplier visitor = new SubstitutionApplier(s);
		node.visit(visitor);
	
}