Skip to content

Commit

Permalink
Factoring delegation out of RDF generation, minor refactoring to use …
Browse files Browse the repository at this point in the history
…Java 8 lambda-idioms
  • Loading branch information
ajs6f committed Feb 5, 2015
1 parent 91d20d3 commit 8f6d0e2
Show file tree
Hide file tree
Showing 10 changed files with 438 additions and 442 deletions.
Expand Up @@ -20,13 +20,15 @@
import static com.hp.hpl.jena.graph.Triple.create;
import static com.hp.hpl.jena.vocabulary.RDF.type;
import static java.util.Arrays.asList;
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toMap;
import static org.fcrepo.kernel.RdfLexicon.HAS_NAMESPACE_PREFIX;
import static org.fcrepo.kernel.RdfLexicon.HAS_NAMESPACE_URI;
import static org.fcrepo.kernel.RdfLexicon.VOAF_VOCABULARY;
import static org.fcrepo.kernel.impl.rdf.JcrRdfTools.getRDFNamespaceForJcrNamespace;
import static org.fcrepo.kernel.impl.utils.UncheckedFunction.uncheck;

import java.util.Arrays;

import javax.jcr.NamespaceRegistry;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
Expand Down Expand Up @@ -54,7 +56,7 @@ public NamespaceRdfContext(final Session session) throws RepositoryException {
super();
final NamespaceRegistry namespaceRegistry = session.getWorkspace().getNamespaceRegistry();

namespaces(stream(namespaceRegistry.getPrefixes()).filter(p -> !p.isEmpty() && !p.equals("jcr")).collect(
namespaces(Arrays.stream(namespaceRegistry.getPrefixes()).filter(p -> !p.isEmpty() && !p.equals("jcr")).collect(
toMap(p -> p, uncheck(p -> getRDFNamespaceForJcrNamespace(namespaceRegistry.getURI(p))))));

concat(namespaces().entrySet().stream().<Triple>flatMap(
Expand Down
Expand Up @@ -21,12 +21,10 @@
import static com.hp.hpl.jena.vocabulary.RDFS.Class;
import static com.hp.hpl.jena.vocabulary.RDFS.label;
import static com.hp.hpl.jena.vocabulary.RDFS.subClassOf;
import static java.util.Arrays.stream;
import static org.fcrepo.kernel.impl.rdf.impl.mappings.ItemDefinitionToTriples.getResource;
import static org.fcrepo.kernel.impl.utils.Streams.fromIterator;
import static org.fcrepo.kernel.impl.utils.UncheckedFunction.uncheck;
import static org.slf4j.LoggerFactory.getLogger;

import java.util.Arrays;
import java.util.Iterator;
import java.util.function.Predicate;

Expand All @@ -35,7 +33,6 @@

import org.fcrepo.kernel.impl.rdf.impl.mappings.NodeDefinitionToTriples;
import org.fcrepo.kernel.impl.rdf.impl.mappings.PropertyDefinitionToTriples;
import org.fcrepo.kernel.impl.utils.UncheckedConsumer;
import org.fcrepo.kernel.utils.iterators.RdfStream;

import org.slf4j.Logger;
Expand Down Expand Up @@ -79,29 +76,26 @@ public NodeTypeRdfContext(final NodeTypeManager nodeTypeManager)
*/
public NodeTypeRdfContext(final Iterator<NodeType> nodeTypeIterator) {
super();
fromIterator(nodeTypeIterator).forEach(UncheckedConsumer.uncheck(nt ->concat(new NodeTypeRdfContext(nt))));
nodeTypeIterator.forEachRemaining(nt -> concat(new NodeTypeRdfContext(nt)));
}

/**
* Convert a NodeType into an RDF stream by capturing the supertypes, node
* definitions, and property definitions of the type as RDFS triples.
*
* @param nodeType
* @throws RepositoryException
*/
public NodeTypeRdfContext(final NodeType nodeType)
throws RepositoryException {
public NodeTypeRdfContext(final NodeType nodeType) {
super();

final Node nodeTypeResource = getResource(nodeType).asNode();
final String nodeTypeName = nodeType.getName();

LOGGER.trace("Adding triples for nodeType: {} with URI: {}", nodeTypeName, nodeTypeResource.getURI());
concat(stream(nodeType.getDeclaredSupertypes()).map(uncheck(n ->
create(nodeTypeResource, subClassOf.asNode(), getResource(n).asNode()))));
concat(stream(nodeType.getDeclaredChildNodeDefinitions()).filter(isWildcardResidualDefinition.negate())
concat(Arrays.stream(nodeType.getDeclaredSupertypes()).map(n ->
create(nodeTypeResource, subClassOf.asNode(), getResource(n).asNode())));
concat(Arrays.stream(nodeType.getDeclaredChildNodeDefinitions()).filter(isWildcardResidualDefinition.negate())
.flatMap(new NodeDefinitionToTriples(nodeTypeResource)));
concat(stream(nodeType.getDeclaredPropertyDefinitions()).filter(isWildcardResidualDefinition.negate())
concat(Arrays.stream(nodeType.getDeclaredPropertyDefinitions()).filter(isWildcardResidualDefinition.negate())
.flatMap(new PropertyDefinitionToTriples(nodeTypeResource)));
concat(create(nodeTypeResource, type.asNode(), Class.asNode()), create(
nodeTypeResource, label.asNode(), createLiteral(nodeTypeName)));
Expand Down
Expand Up @@ -40,8 +40,10 @@
import org.fcrepo.kernel.identifiers.IdentifierConverter;
import org.fcrepo.metrics.RegistryService;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import javax.jcr.Repository;
import javax.jcr.RepositoryException;
Expand Down Expand Up @@ -90,7 +92,7 @@ public RootRdfContext(final FedoraResource resource,
LOGGER.trace("Creating RDF triples for repository description");
final Repository repository = resource().getNode().getSession().getRepository();

final Map<String, String> descriptors = stream(repository.getDescriptorKeys())
final Map<String, String> descriptors = Arrays.stream(repository.getDescriptorKeys())
.filter(key -> nonNull(repository.getDescriptor(key)))
.collect(toMap((key) -> REPOSITORY_NAMESPACE + "repository." + key,
key -> repository.getDescriptor(key)));
Expand All @@ -103,7 +105,7 @@ public RootRdfContext(final FedoraResource resource,
resource().getNode().getSession().getWorkspace().getNodeTypeManager();
@SuppressWarnings("unchecked")
final Stream<NodeType> nodeTypes =
stream(spliteratorUnknownSize(nodeTypeManager.getAllNodeTypes(), IMMUTABLE), true);
StreamSupport.stream(spliteratorUnknownSize(nodeTypeManager.getAllNodeTypes(), IMMUTABLE), true);
concat(nodeTypes.map(type -> create(subject(), HAS_NODE_TYPE.asNode(), createLiteral(type.getName()))));


Expand Down
Expand Up @@ -25,8 +25,10 @@
import static org.fcrepo.kernel.RdfLexicon.HAS_VERSION;
import static org.fcrepo.kernel.RdfLexicon.HAS_VERSION_LABEL;
import static org.fcrepo.kernel.impl.identifiers.NodeResourceConverter.nodeToResource;
import static org.fcrepo.kernel.impl.utils.Streams.fromIterator;
import static org.slf4j.LoggerFactory.getLogger;

import java.util.Arrays;
import java.util.Iterator;
import java.util.function.Function;
import java.util.stream.Stream;
Expand All @@ -41,6 +43,7 @@
import org.fcrepo.kernel.models.FedoraResource;
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.identifiers.IdentifierConverter;
import org.fcrepo.kernel.impl.utils.Streams;
import org.fcrepo.kernel.utils.iterators.RdfStream;

import com.hp.hpl.jena.graph.Triple;
Expand Down Expand Up @@ -73,7 +76,7 @@ public VersionsRdfContext(final FedoraResource resource,
super(resource, idTranslator);
this.versionHistory = resource.getVersionHistory();
final Iterator<Version> allVersions = versionHistory.getAllVersions();
final Stream<Version> versionsStream = stream(spliteratorUnknownSize(allVersions, 0), true);
final Stream<Version> versionsStream = fromIterator(allVersions);
concat(versionsStream.flatMap(version2triples));
}

Expand All @@ -98,7 +101,7 @@ public Stream<Triple> apply(final Version version) {
new RdfStream(create(subject(), HAS_VERSION.asNode(), versionSubject),
create(versionSubject, CREATED_DATE.asNode(),
createTypedLiteral(version.getCreated()).asNode()));
results.concat(stream(versionHistory.getVersionLabels(version)).map(
results.concat(Arrays.stream(versionHistory.getVersionLabels(version)).map(
label -> create(versionSubject, HAS_VERSION_LABEL.asNode(), createLiteral(label))));
return results;

Expand Down
Expand Up @@ -29,6 +29,7 @@
import javax.jcr.nodetype.NodeType;

import java.util.function.Function;

import static com.hp.hpl.jena.graph.NodeFactory.createLiteral;
import static com.hp.hpl.jena.graph.Triple.create;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createProperty;
Expand Down Expand Up @@ -91,17 +92,17 @@ public RdfStream apply(final T input) {
* @return a resource for the given Namespaced JCR object
* @throws javax.jcr.RepositoryException
*/
public static Resource getResource(final Namespaced namespacedObject)
throws RepositoryException {
public static Resource getResource(final Namespaced namespacedObject) {
// TODO find a better way to create an explicitly-namespaced resource
// if Jena offers one, since this isn't actually a Property
LOGGER.trace("Creating RDF resource for {}:{}",
namespacedObject.getNamespaceURI(),
namespacedObject.getLocalName());
return createProperty(
getRDFNamespaceForJcrNamespace(namespacedObject
.getNamespaceURI()), namespacedObject.getLocalName())
.asResource();
try {
final String namespaceURI = namespacedObject.getNamespaceURI();
final String localName = namespacedObject.getLocalName();
LOGGER.trace("Creating RDF resource for {}:{}", namespaceURI, localName);
return createProperty(getRDFNamespaceForJcrNamespace(namespaceURI), localName).asResource();
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
}
}

/**
Expand All @@ -111,9 +112,8 @@ public static Resource getResource(final Namespaced namespacedObject)
*
* @param nodeType
* @return a Resource for the given NodeType
* @throws javax.jcr.RepositoryException
*/
public static Resource getResource(final NodeType nodeType) throws RepositoryException {
public static Resource getResource(final NodeType nodeType) {
return getResource((Namespaced) nodeType);
}

Expand Down
Expand Up @@ -31,7 +31,7 @@
public class Streams {

public static <T> Stream<T> fromIterator(final Iterator<T> i) {
return stream(spliteratorUnknownSize(i, 0), true);
return stream(spliteratorUnknownSize(i, 0), false);
}

}
Expand Up @@ -16,7 +16,6 @@
package org.fcrepo.kernel.impl.rdf.impl;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
Expand All @@ -34,6 +33,7 @@
import java.io.StringWriter;
import java.io.Writer;
import java.util.List;
import java.util.function.Consumer;

import static com.hp.hpl.jena.datatypes.xsd.XSDDatatype.XSDanyURI;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
Expand All @@ -49,13 +49,16 @@
import static org.fcrepo.kernel.impl.rdf.impl.mappings.ItemDefinitionToTriples.getResource;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.slf4j.LoggerFactory.getLogger;

/**
* @author cbeer
* @author ajs6f
*/
public class NodeTypeRdfContextTest {

Expand Down Expand Up @@ -112,7 +115,7 @@ public void setUp() throws RepositoryException {
}

@Test
public void testShouldMapASimpleNodeTypeToRdf() throws RepositoryException {
public void testShouldMapASimpleNodeTypeToRdf() {
final Model actual = new NodeTypeRdfContext(mockNodeType).asModel();
assertTrue(actual.contains(createResource(REPOSITORY_NAMESPACE
+ mockNodeTypeName), type, Class));
Expand All @@ -133,24 +136,18 @@ public void testShouldMapNodeTypeIteratorToRdf() {
public void testShouldMapNodeTypeManagerToRdf() throws RepositoryException {
final NodeTypeManager mockNodeTypeManager = mock(NodeTypeManager.class);
final NodeTypeIterator mockNodeTypeIterator = mock(NodeTypeIterator.class);
when(mockNodeTypeIterator.next()).thenReturn(mockNodeType);
when(mockNodeTypeIterator.hasNext()).thenReturn(true, false);
when(mockNodeTypeManager.getPrimaryNodeTypes()).thenReturn(mockNodeTypeIterator);

final NodeTypeIterator mockMixinTypeIterator = mock(NodeTypeIterator.class);

when(mockMixinTypeIterator.next()).thenReturn(mockNodeTypeB);
when(mockMixinTypeIterator.hasNext()).thenReturn(true, false);
when(mockNodeTypeManager.getMixinNodeTypes()).thenReturn(mockMixinTypeIterator);

final Model actual = new NodeTypeRdfContext(mockNodeTypeManager).asModel();
assertTrue(actual.contains(
ResourceFactory.createResource(REPOSITORY_NAMESPACE + mockNodeTypeName), type, Class));
assertTrue(actual.contains(ResourceFactory.createResource("b#b"), type, Class));
new NodeTypeRdfContext(mockNodeTypeManager).asModel();
verify(mockNodeTypeIterator).forEachRemaining(any(Consumer.class));
verify(mockMixinTypeIterator).forEachRemaining(any(Consumer.class));
}

@Test
public void testShouldIncludeSupertypeInformation() throws RepositoryException {
public void testShouldIncludeSupertypeInformation() {

when(mockNodeType.getDeclaredSupertypes()).thenReturn(new NodeType[] { mockNodeTypeA, mockNodeTypeB });
final Model actual = new NodeTypeRdfContext(mockNodeType).asModel();
Expand Down Expand Up @@ -194,7 +191,7 @@ public void testShouldIncludeChildNodeRangeWhenTheChildNodeDeclaresRequiredType(
}

@Test
public void testShouldSkipChildNodesAsResidualSet() throws RepositoryException, IOException {
public void testShouldSkipChildNodesAsResidualSet() throws IOException {

when(mockNodeDefinitionA.getName()).thenReturn("*");
when(mockNodeType.getDeclaredChildNodeDefinitions()).thenReturn(
Expand Down Expand Up @@ -234,7 +231,7 @@ public void testShouldIncludePropertyDefinitionsRequiredTypeAsRange() throws Rep
}

@Test
public void testShouldExcludePropertyDefinitionsForResidualSets() throws RepositoryException {
public void testShouldExcludePropertyDefinitionsForResidualSets() {
when(mockProperty.getName()).thenReturn("*");
when(mockNodeType.getDeclaredPropertyDefinitions()).thenReturn(new PropertyDefinition[] { mockProperty });

Expand Down

0 comments on commit 8f6d0e2

Please sign in to comment.