io.carml.engine.rdf.RdfSubjectMapper Maven / Gradle / Ivy
package io.carml.engine.rdf;
import static io.carml.util.LogUtil.exception;
import static io.carml.util.LogUtil.log;
import io.carml.engine.ExpressionEvaluation;
import io.carml.engine.TermGenerator;
import io.carml.engine.TriplesMapperException;
import io.carml.model.SubjectMap;
import io.carml.model.TriplesMap;
import io.carml.util.Models;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.vocabulary.RDF;
import reactor.core.publisher.Flux;
@Slf4j
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class RdfSubjectMapper {
@NonNull
private final TermGenerator subjectGenerator;
private final Set> graphGenerators;
private final Set classes;
@NonNull
private final ValueFactory valueFactory;
public static RdfSubjectMapper of(@NonNull SubjectMap subjectMap, @NonNull TriplesMap triplesMap,
@NonNull RdfMapperConfig rdfMapperConfig) {
if (LOG.isDebugEnabled()) {
LOG.debug("Creating mapper for SubjectMap {}", log(triplesMap, subjectMap));
}
RdfTermGeneratorFactory rdfTermGeneratorFactory =
(RdfTermGeneratorFactory) rdfMapperConfig.getTermGeneratorFactory();
TermGenerator subjectGenerator;
try {
subjectGenerator = rdfTermGeneratorFactory.getSubjectGenerator(subjectMap);
} catch (RuntimeException ex) {
throw new TriplesMapperException(String.format("Exception occurred while creating subject generator for %s",
exception(triplesMap, subjectMap)), ex);
}
return new RdfSubjectMapper(subjectGenerator,
RdfTriplesMapper.createGraphGenerators(subjectMap.getGraphMaps(), rdfTermGeneratorFactory),
subjectMap.getClasses(), rdfMapperConfig.getValueFactorySupplier()
.get());
}
public Result map(ExpressionEvaluation expressionEvaluation) {
LOG.debug("Determining subjects ...");
Set subjects = subjectGenerator.apply(expressionEvaluation)
.stream()
.collect(Collectors.toUnmodifiableSet());
LOG.debug("Determined subjects {}", subjects);
if (subjects.isEmpty()) {
return resultOf(subjects, Set.of(), Flux.empty());
}
// graphs to be used when generating statements in predicate object mapper
Set graphs = graphGenerators.stream()
.flatMap(graph -> graph.apply(expressionEvaluation)
.stream())
.collect(Collectors.toUnmodifiableSet());
Flux typeStatements = classes.isEmpty() ? Flux.empty() : mapTypeStatements(subjects, graphs);
return resultOf(subjects, graphs, typeStatements);
}
private Flux mapTypeStatements(Set subjects, Set graphs) {
LOG.debug("Generating triples for subjects: {}", subjects);
Stream typeStatementStream = Models.streamCartesianProductStatements(subjects, Set.of(RDF.TYPE), classes,
graphs, RdfTriplesMapper.defaultGraphModifier, valueFactory, RdfTriplesMapper.logAddStatements);
return Flux.fromStream(typeStatementStream);
}
private static Result resultOf(Set subjects, Set graphs, Flux typeStatements) {
return new Result(subjects, graphs, typeStatements);
}
@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
static class Result {
Set subjects;
Set graphs;
Flux typeStatements;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy