
org.aksw.jena_sparql_api.utils.CompareUtils Maven / Gradle / Ivy
package org.aksw.jena_sparql_api.utils;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.Sets;
import org.apache.jena.graph.Graph;
import org.apache.jena.graph.Node;
import org.apache.jena.graph.Triple;
import org.apache.jena.graph.impl.GraphMatcher;
import org.apache.jena.sparql.core.Quad;
import org.apache.jena.util.iterator.ExtendedIterator;
public class CompareUtils {
//public Set
public static Set alignActualQuads(Set expected, Set actual) {
Map e = QuadPatternUtils.indexAsGraphs(expected);
Map a = QuadPatternUtils.indexAsGraphs(actual);
Set result = alignActualQuads(e, a);
return result;
}
public static Set toQuads(Node g, Graph graph) {
Set result = new HashSet();
ExtendedIterator it = graph.find(null, null, null);
try {
while(it.hasNext()) {
Triple t = it.next();
Quad quad = new Quad(g, t);
result.add(quad);
}
} finally {
it.close();
}
return result;
}
public static Set toQuads(Node g, Graph graph, Map subst) {
Set result = new HashSet();
ExtendedIterator it = graph.find(null, null, null);
try {
while(it.hasNext()) {
Triple t = it.next();
Quad tmp = new Quad(g, t);
Quad quad = QuadUtils.copySubstitute(tmp, subst);
result.add(quad);
}
} finally {
it.close();
}
return result;
}
/**
* Per graph alignment of quads.
* Equivalent blank node objects in distinct graphs may be mapped differently.
* Result of this method is not necessarily deterministic; see GraphMacher.match()
*
* @param expected
* @param actual
* @return
*/
public static Set alignActualQuads(Map expected, Map actual) {
Set result = new HashSet();
// Set excessiveQuads = new HashSet();
// Set missingQuads = new HashSet();
Set expectedGs = expected.keySet();
Set actualGs = actual.keySet();
Set excessiveGs = Sets.difference(actualGs, expectedGs);
Set commonGs = Sets.intersection(expectedGs, actualGs);
// Set missingGs = Sets.difference(expectedGs, actualGs);
for(Node g : excessiveGs) {
Graph graph = actual.get(g);
Set tmp = toQuads(g, graph);
result.addAll(tmp);
}
for(Node g : commonGs) {
Graph expectedGraph = expected.get(g);
Graph actualGraph = actual.get(g);
Node[][] rawMapping = GraphMatcher.match(actualGraph, expectedGraph);
Map mapping = new HashMap();
if(rawMapping != null) {
for(int i = 0; i < rawMapping.length; ++i) {
Node source = rawMapping[i][0];
Node target = rawMapping[i][1];
mapping.put(source, target);
}
}
else {
//logger.warn("Could not establish a mapping between the graphs")
}
Set tmp = toQuads(g, actualGraph, mapping);
result.addAll(tmp);
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy