Commit fd89d16d authored by acoburn's avatar acoburn
Browse files

Simplify entailment code

parent 73cb8992
......@@ -15,8 +15,8 @@
*/
package edu.amherst.acdc.services.entailment;
import static java.util.Arrays.stream;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.toMap;
import static org.apache.http.impl.client.HttpClientBuilder.create;
import static org.apache.jena.rdf.model.ModelFactory.createDefaultModel;
import static org.apache.jena.rdf.model.ModelFactory.createRDFSModel;
......@@ -30,19 +30,15 @@ import static org.slf4j.LoggerFactory.getLogger;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.jena.rdf.model.InfModel;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.riot.Lang;
import org.apache.jena.riot.RDFLanguages;
import org.apache.jena.vocabulary.RDF;
import org.apache.jena.vocabulary.RDFS;
import org.slf4j.Logger;
/**
......@@ -71,15 +67,13 @@ public class EntailmentServiceImpl implements EntailmentService {
public EntailmentServiceImpl(final String ontologies) {
LOGGER.info("EntailmentServiceImpl: ontologies: {}", ontologies);
Arrays.stream(ontologies.split(","))
.peek(ont -> LOGGER.info("adding to rdfs model: {}", ont))
.map(String::trim)
.forEach(ont -> read(rdfsModel, ont));
stream(ontologies.split(",")).peek(ont -> LOGGER.info("adding to rdfs model: {}", ont))
.map(String::trim).forEach(ont -> read(rdfsModel, ont));
}
@Override
public InputStream getEntailedTriples(final String subject, final InputStream input,
final String contentType, final String acceptType) {
final String contentType, final String acceptType) {
final Model model = createDefaultModel();
......@@ -91,51 +85,21 @@ public class EntailmentServiceImpl implements EntailmentService {
// add the requested ontologies to the model, which does RDFS entailment over them.
fullModel.add(rdfsModel);
final Model entailed = fullModel.difference(rdfsModel);
final Model entailed = createDefaultModel();
entailed.setNsPrefixes(model);
final Resource subjectResource = entailed.getResource(subject);
final List<Statement> props = subjectResource.listProperties().toList();
fullModel.listStatements().filterDrop(t -> rdfsModel.contains(t.getSubject(), null))
// We don't care about rdfs:Class and rdfs:Resource types
.filterDrop(t -> t.matches(null, RDF.type.asNode(), RDFS.Class.asNode()) ||
t.matches(null, RDF.type.asNode(), RDFS.Resource.asNode()))
.forEachRemaining(entailed::add);
final Model newResourceModel = createDefaultModel();
newResourceModel.add(subjectResource.listProperties().toList());
final Map<String,String> prefixMap = getPrefixMap(newResourceModel, entailed.getNsPrefixMap());
newResourceModel.setNsPrefixes(prefixMap);
final ByteArrayOutputStream nbs = new ByteArrayOutputStream();
write(nbs, newResourceModel, getRdfLanguage(acceptType.split(";")[0]).orElse(DEFAULT_LANG));
return new ByteArrayInputStream(nbs.toByteArray());
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
write(baos, entailed, getRdfLanguage(acceptType.split(";")[0]).orElse(DEFAULT_LANG));
return new ByteArrayInputStream(baos.toByteArray());
}
private Optional<Lang> getRdfLanguage(final String contentType) {
return ofNullable(contentType).map(RDFLanguages::contentTypeToLang);
}
private Map<String,String> getPrefixMap(final Model model, final Map<String,String> prefixes) {
final List<Statement> props = model.listStatements().toList();
final Map<String,String> modelPrefixes = new HashMap<>();
final Map<String,String> flip_prefixes =
prefixes.entrySet().stream().collect(toMap(Map.Entry::getValue, Map.Entry::getKey, (key1, key2) -> key1));
LOGGER.debug("There are {} properties on resource", props.size());
props.forEach(statement -> {
final String pred_prefix = statement.getPredicate().getNameSpace();
if (flip_prefixes.containsKey(pred_prefix)) {
modelPrefixes.put(flip_prefixes.get(pred_prefix), pred_prefix);
}
if (statement.getObject().isResource()) {
final String obj_prefix = statement.getResource().getNameSpace();
if (flip_prefixes.containsKey(obj_prefix)) {
modelPrefixes.put(flip_prefixes.get(obj_prefix), obj_prefix);
}
}
});
return modelPrefixes;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment