org.aksw.jenax.sparql.fragment.api.MappedFragment Maven / Gradle / Ivy
package org.aksw.jenax.sparql.fragment.api;
import java.util.AbstractMap.SimpleEntry;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import org.apache.jena.graph.Node;
import org.apache.jena.sparql.core.Var;
import org.apache.jena.sparql.graph.NodeTransform;
/** A relation with an additional mapping of variables to custom objects */
public class MappedFragment
extends FragmentWrapperBase
{
protected Map mapping;
protected MappedFragment(Fragment delegate, Map mapping) {
super(delegate);
this.mapping = mapping;
}
public static MappedFragment of(Fragment delegate, Map mapping) {
return new MappedFragment<>(delegate, mapping);
}
public Map getMapping() {
return mapping;
}
@Override
public Fragment applyNodeTransform(NodeTransform nodeTransform) {
Fragment newDelegate = getDelegate().applyNodeTransform(nodeTransform);
Map newMapping = mapping.entrySet().stream()
.map(e -> {
Node newVar = nodeTransform.apply(e.getKey());
return new SimpleEntry<>((Var)newVar, e.getValue());
})
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
return new MappedFragment<>(newDelegate, newMapping);
}
}