Commit 0132166f authored by bseeger's avatar bseeger
Browse files

Fixes a few minor issues. Adds more tests.

parent 7d00d653
......@@ -15,18 +15,13 @@
*/
package edu.amherst.acdc.exts.entailment;
//import static java.util.Optional.ofNullable;
import static org.apache.camel.Exchange.CONTENT_TYPE;
import static org.apache.camel.Exchange.HTTP_METHOD;
//import static org.apache.camel.Exchange.HTTP_RESPONSE_CODE;
import static org.apache.camel.Exchange.HTTP_RESPONSE_CODE;
import static org.fcrepo.camel.FcrepoHeaders.FCREPO_BASE_URL;
import static org.fcrepo.camel.FcrepoHeaders.FCREPO_URI;
//import java.util.Optional;
import org.apache.camel.builder.RouteBuilder;
//import org.apache.jena.riot.Lang;
//import org.apache.jena.riot.RDFLanguages;
/**
* A content router for handling ORE extension requests
......@@ -59,12 +54,11 @@ public class EntailmentRouter extends RouteBuilder {
.when(header(HTTP_QUERY_CONTEXT).startsWith(header(FCREPO_BASE_URL)))
.to("direct:get");
// first get resource
// then pass it to getTriples... which is defined in the blueprint script.
//
from("direct:get").routeId("EntailementGetTriples")
.log("Building RDFS Entailment Info ${body}")
.log("Building RDFS Entailment Info")
.to("fcrepo:{{fcrepo.baseUrl}}?throwExceptionOnFailure=false")
.setHeader(CONTENT_TYPE).constant("text/turtle")
.setHeader(HTTP_RESPONSE_CODE).constant(200)
.to("direct:getTriples");
}
}
......@@ -29,6 +29,7 @@ public interface EntailmentService {
* @param subject Subject of the triples
* @param input Input stream containing the information for the resource
* @param contentType The content type of the stream
* @return stream containing a serialization of the entailed triples
*/
InputStream getEntailedTriples(final String subject, final InputStream input, final String contentType);
......
......@@ -37,8 +37,6 @@ import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.io.StringWriter;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.jena.rdf.model.InfModel;
import org.apache.jena.rdf.model.Model;
......@@ -69,27 +67,28 @@ public class EntailmentServiceImpl implements EntailmentService {
/**
* Implementation of an Entailment Service to return the
* RDFS entailed triples for a resource.
*
* @param ontologies A string containing a list of comma separated ontologies
* to include in RDFS entailment
*/
public EntailmentServiceImpl(final String ontologies) {
LOGGER.info("EntailmentServiceImpl: ontologies: {}", ontologies);
rdfsOntologies = new ArrayList<>(Arrays.asList(ontologies.split(",")));
rdfsOntologies.forEach(x -> {
LOGGER.info("adding to rdfs model: {}", x);
read(rdfsModel, x);
});
rdfsOntologies.stream()
.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) {
// read the passed in resource into a model
final Model model = createDefaultModel();
//model.read(input, subject, contentTypeToLang(contentType.split(";")[0]).getName());
read(model, input, contentTypeToLang(contentType.split(";")[0]));
//read(model, input, getRdfLanguage(contentType.split(";")[0]).orElse(DEFAULT_LANG));
// perform RDFS entailment over it by creating a RDFSModel
final InfModel fullModel = createRDFSModel(model);
......@@ -100,28 +99,9 @@ public class EntailmentServiceImpl implements EntailmentService {
final Model entailed = fullModel.difference(rdfsModel).difference(model);
entailed.setNsPrefixes(model);
final StringWriter orig = new StringWriter();
write(orig, model, contentTypeToLang(contentType.split(";")[0]));
final StringWriter diff = new StringWriter();
write(diff, entailed, contentTypeToLang(contentType.split(";")[0]));
final StringWriter full = new StringWriter();
write(full, fullModel, contentTypeToLang(contentType.split(";")[0]));
LOGGER.info("EntailmentServiceImpl PLAIN model before entailment:\n{}", orig.toString());
LOGGER.info("EntailmentServiceImpl DIFF entailed triples:\n{}", diff.toString());
//LOGGER.info("EntailmentServiceImpl FULL model:\n{}", full.toString());
final Resource subjectResource = entailed.getResource(subject);
final List<Statement> props = subjectResource.listProperties().toList();
/*
LOGGER.info("List empty? : {}", props.isEmpty());
props.stream().peek(s -> {
LOGGER.info("FOO {} : {}", s.getPredicate().getLocalName(),
s.getObject().toString());
}).forEach(s -> s.getPredicate());
*/
final Model newResourceModel = createDefaultModel();
newResourceModel.add(subjectResource.listProperties().toList());
......@@ -143,26 +123,26 @@ public class EntailmentServiceImpl implements EntailmentService {
final Map<String,String> modelPrefixes = new HashMap<String,String>();
final Map<String,String> flip_prefixes =
prefixes.entrySet().stream().peek(e -> {
LOGGER.info(" Entry: {} : {} ", e.getKey(), e.getValue());
}).collect(Collectors.toMap(
prefixes.entrySet().stream().collect(Collectors.toMap(
Map.Entry::getValue,
Map.Entry::getKey,
(key1, key2) -> {
LOGGER.info("Dup key found!");
return key1;
}
));
LOGGER.info("There are {} properties on resource", props.size());
LOGGER.debug("There are {} properties on resource", props.size());
props.forEach(statement -> {
final String pred_key = statement.getPredicate().getNameSpace();
LOGGER.info("looking at statement pred : {}", pred_key);
if (flip_prefixes.containsKey(pred_key)) {
LOGGER.info("adding to map: {} : {} ", flip_prefixes.get(pred_key), pred_key);
modelPrefixes.put(flip_prefixes.get(pred_key), pred_key);
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);
}
}
// TODO check the object as well!!
});
return modelPrefixes;
......
......@@ -15,19 +15,17 @@
*/
package edu.amherst.acdc.services.entailment;
//import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.slf4j.LoggerFactory.getLogger;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.slf4j.LoggerFactory.getLogger;
import org.junit.Test;
import org.slf4j.Logger;
import java.io.InputStream;
import java.io.IOException;
import java.io.StringWriter;
import org.junit.Test;
import org.slf4j.Logger;
import org.apache.commons.io.IOUtils;
/**
* @author bseeger
* @since 3/24/2017
......@@ -52,87 +50,62 @@ public class EntailmentServiceTest {
@Test
public void testSimpleEntailment() throws IOException {
final InputStream bs = svc.getEntailedTriples(subject, getClass().getResourceAsStream("/simple.ttl"), TURTLE);
final StringWriter writer = new StringWriter();
IOUtils.copy(bs, writer, UTF_8);
LOGGER.info("Got triples: {}", writer.toString());
assertNotNull(bs);
final StringWriter sw = new StringWriter();
IOUtils.copy(bs, sw, UTF_8);
final String str = sw.toString();
LOGGER.debug("string is : '{}'", str);
assertFalse(str.equals(""));
assertTrue(str.contains("rdfs:Resource") && str.contains("skos:Concept"));
assertTrue(str.contains("skos:broaderTransitive") && str.contains("skos:semanticRelation"));
}
@Test
public void testEntailmentResource() throws IOException {
final InputStream bs = svc.getEntailedTriples(subject, getClass().getResourceAsStream("/resource.ttl"), TURTLE);
final StringWriter writer = new StringWriter();
final StringWriter sw = new StringWriter();
IOUtils.copy(bs, sw, UTF_8);
final String str = sw.toString();
IOUtils.copy(bs, writer, UTF_8);
LOGGER.info("Got triples: {}", writer.toString());
assertNotNull(bs);
LOGGER.debug("string is : '{}'", str);
assertFalse(str.equals(""));
assertTrue(str.contains("rdfs:Resource") && str.contains("rdfs:label"));
}
@Test
public void testDublinCore() throws IOException {
final InputStream bs = svc.getEntailedTriples(subject, getClass().getResourceAsStream("/dc.ttl"), TURTLE);
final StringWriter writer = new StringWriter();
IOUtils.copy(bs, writer, UTF_8);
LOGGER.info("Got triples: {}", writer.toString());
assertNotNull(bs);
}
/*
@Test
public void testIsEquivalentClass() {
assertTrue(svc.hasType(subject, appType,
getClass().getResourceAsStream("/resource.ttl"), TURTLE,
getClass().getResourceAsStream("/isResource.ttl"), TURTLE));
}
@Test
public void testIsNotEquivalentClass() {
assertFalse(svc.hasType(subject, appType,
getClass().getResourceAsStream("/resource.ttl"), TURTLE,
getClass().getResourceAsStream("/isBinary.ttl"), TURTLE));
}
@Test
public void testHasValue() {
assertTrue(svc.hasType(subject, appType,
getClass().getResourceAsStream("/resource.ttl"), TURTLE,
getClass().getResourceAsStream("/hasValue.ttl"), TURTLE));
}
@Test
public void testCardinality() {
assertTrue(svc.hasType(subject, appType,
getClass().getResourceAsStream("/resource.ttl"), TURTLE,
getClass().getResourceAsStream("/hasPrefLabel.ttl"), TURTLE));
}
@Test
public void testResourceSubClass() {
assertTrue(svc.hasType(subject, appType,
getClass().getResourceAsStream("/resource.ttl"), TURTLE,
getClass().getResourceAsStream("/isResourceSubClass.ttl"), TURTLE));
}
@Test
public void testIntersectionOf() {
assertTrue(svc.hasType(subject, appType,
getClass().getResourceAsStream("/resource.ttl"), TURTLE,
getClass().getResourceAsStream("/intersectionOf.ttl"), TURTLE));
final StringWriter sw = new StringWriter();
IOUtils.copy(bs, sw, UTF_8);
final String str = sw.toString();
LOGGER.debug("string is : '{}'", str);
assertFalse(str.equals(""));
assertTrue(str.contains("dc:rights") && str.contains("dc:title") && str.contains("dc:description"));
assertFalse(str.contains("dcterms:rights") && str.contains("dcterms:title"));
}
@Test
public void testUnionOf() {
assertTrue(svc.hasType(subject, appType,
getClass().getResourceAsStream("/resource.ttl"), TURTLE,
getClass().getResourceAsStream("/unionOf.ttl"), TURTLE));
public void testNoEntailedTriples() throws IOException {
final InputStream bs = svc.getEntailedTriples(subject,
getClass().getResourceAsStream("/noentailment.ttl"), TURTLE);
final StringWriter sw = new StringWriter();
IOUtils.copy(bs, sw, UTF_8);
final String str = sw.toString();
LOGGER.debug("string is : '{}'", str);
assertTrue(str.equals(""));
}
@Test
public void testApplicationProfile() {
assertTrue(svc.hasType(subject, appType,
getClass().getResourceAsStream("/resource.ttl"), TURTLE,
getClass().getResourceAsStream("/applicationProfile.ttl"), TURTLE));
public void testObjectWithNamespace() throws IOException {
final InputStream bs = svc.getEntailedTriples(subject,
getClass().getResourceAsStream("/objectnamespace.ttl"), TURTLE);
final StringWriter sw = new StringWriter();
IOUtils.copy(bs, sw, UTF_8);
final String str = sw.toString();
LOGGER.debug("string is : '{}'", str);
assertFalse(str.equals(""));
assertTrue(str.contains("@prefix ex:"));
}
*/
}
@prefix dc: <http://purl.org/dc/elements/1.1/>
@prefix ldp: <http://www.w3.org/ns/ldp#> .
<http://localhost:8080/fcrepo/rest/test> a ldp:RDFSource ;
dc:title "DC Test Title" ;
dc:rights "no rights, just wrong" .
@prefix dcterms: <http://purl.org/dc/terms/> .
@prefix ldp: <http://www.w3.org/ns/ldp#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix ex: <http://example.org/>
<http://localhost:8080/fcrepo/rest/test> a ldp:RDFSource ;
rdfs:label "DC Test Label" ;
dcterms:title "DC Test Title" ;
dcterms:accessRights ex:testRights .
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