org.aksw.jenax.path.util.PathNodeUtils Maven / Gradle / Ivy
The newest version!
package org.aksw.jenax.path.util;
import java.util.LinkedHashSet;
import java.util.Set;
import org.aksw.commons.path.core.Path;
import org.aksw.jenax.sparql.path.PathUtils;
import org.apache.jena.graph.Node;
/** Utils for working with {@code Path} */
public class PathNodeUtils {
/**
* Collects all nodes in a {@code Path} object.
* Descends into literals that represenot property paths.
* TODO Descend into literals representing queries
*
*/
public static Set collectNodes(Path path) {
Set result = new LinkedHashSet<>();
for (Path item : path) {
Node node = item.toSegment();
Object obj = node.isLiteral() ? node.getLiteralValue() : null;
if (obj instanceof org.apache.jena.sparql.path.Path) {
org.apache.jena.sparql.path.Path p = (org.apache.jena.sparql.path.Path)obj;
Set contrib = PathUtils.collectNodes(p);
result.addAll(contrib);
} else {
result.add(node);
}
}
return result;
}
}