I am extracting the subjects,predicates and objects in queries in log by
using this handy snippet here [1]. My goal is to compare them with other
sets of Nodes coming from other sources.
However, the .equals between the Node coming from the snippet and a Node
created with NodeFactory is not working as expected. Roughly:

Node var = NodeFactory.createVariable("movie");
QueryStr = "SELECT ?movie WHERE {?movie .......}" ;
Got the subjects of the query with [1], that is, a set containing the
variable '?movie' (let's call it 'sub')

My issue is that sub.equals(var) is false when I expected the opposite.

What I'm missing here?

I'm attaching a minimal test case for those keen to execute it.


P.S. ARQ 3.1.0 from maven repo on Java 8, if it matters.


Ing. Luis Daniel Ibáñez G.
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
package soton.wais.queryanalyzer;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;
import org.apache.jena.graph.Node;
import org.apache.jena.graph.NodeFactory;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.sparql.core.TriplePath;
import org.apache.jena.sparql.syntax.ElementPathBlock;
import org.apache.jena.sparql.syntax.ElementVisitorBase;
import org.apache.jena.sparql.syntax.ElementWalker;

 * @author ldig
public class MUTCNodeEquality {

	public static Set<Node> getSubjects(Query query){
	  final Set<Node> subjects = new HashSet<>();

  // This will walk through all parts of the query
			  // For each element...
			  new ElementVisitorBase() {
				  // ...when it's a block of triples...
				  public void visit(ElementPathBlock el) {
					  // ...go through all the triples...
					  Iterator<TriplePath> triples = el.patternElts();
					  while (triples.hasNext()) {
						  // ...and grab the subject
		 return subjects;

	public static void main(String[] args){
		Query q = QueryFactory.create("PREFIX dbo:<http://dbpedia.org/ontology/#>" +
			 " SELECT ?movie (avg(?runtime) as ?avgrt) WHERE {" +
			 "?movie dbo:runtime ?runtime ." +
			 "?movie dbo:starring <http://dbpedia.org/resource/Jason_Statham>" +
			 "} GROUP BY ?movie");
		Set<Node> subjects = getSubjects(q); 
		// result is {?movie}
		Node s = NodeFactory.createVariable("movie");
		Iterator<Node> iter = subjects.iterator();
			Node node = iter.next();
			System.out.println("Nodes are equal, same label");
			System.out.println("Same label, but not equal :(");

Reply via email to