Skip to content

Commit

Permalink
Providing RDF stream that distinguishes managed and unmanaged triples
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Oct 23, 2013
1 parent 013df44 commit 31380cb
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
5 changes: 5 additions & 0 deletions fcrepo-kernel/src/main/java/org/fcrepo/kernel/RdfLexicon.java
Expand Up @@ -15,11 +15,13 @@
*/
package org.fcrepo.kernel;

import static com.google.common.base.Predicates.in;
import static com.google.common.collect.ImmutableSet.of;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createProperty;

import java.util.Set;

import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.hp.hpl.jena.rdf.model.Property;

Expand Down Expand Up @@ -257,6 +259,9 @@ public final class RdfLexicon {
managedProperties = b.build();
}

public static final Predicate<Property> isManagedPredicate =
in(managedProperties);

private RdfLexicon() {

}
Expand Down
Expand Up @@ -43,7 +43,7 @@ public class RdfStream extends ForwardingIterator<Triple> implements

private Map<String, String> namespaces = new HashMap<String, String>();

private Iterator<Triple> triples;
protected Iterator<Triple> triples;

private final static Set<Triple> none = emptySet();

Expand Down
@@ -0,0 +1,47 @@

package org.fcrepo.kernel.utils.iterators;

import static com.google.common.base.Predicates.not;
import static com.google.common.collect.Iterators.filter;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static org.fcrepo.kernel.RdfLexicon.isManagedPredicate;

import java.util.Iterator;

import com.google.common.base.Predicate;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.Model;

/**
* This wraps an {@link Iterator} of {@link Triple}s and produces only those
* safe for persistence into the repo as properties. It does this by filtering
* all triples with managed predicates, as defined in {@link RdfLexicon}.
*
* @author ajs6f
* @date Oct 23, 2013
*/
public class UnmanagedRdfStream extends RdfStream {

private static final Model model = createDefaultModel();

private static final Predicate<Triple> isManagedTriple =
new Predicate<Triple>() {

@Override
public boolean apply(final Triple t) {
return isManagedPredicate.apply(model.asStatement(t)
.getPredicate());
}

};

public UnmanagedRdfStream(final Iterator<Triple> triples) {
super(triples);
}

@Override
protected Iterator<Triple> delegate() {
return filter(triples, not(isManagedTriple));
}

}
@@ -0,0 +1,48 @@

package org.fcrepo.kernel.utils.iterators;

import static com.hp.hpl.jena.graph.NodeFactory.createAnon;
import static com.hp.hpl.jena.graph.Triple.create;
import static org.fcrepo.kernel.RdfLexicon.HAS_CHILD;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

import java.util.Iterator;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;

import com.hp.hpl.jena.graph.Triple;

public class UnmanagedRdfStreamTest {

private final static Triple managedTriple = create(createAnon(), HAS_CHILD
.asNode(), createAnon());

private final static Triple unManagedTriple = create(createAnon(),
createAnon(), createAnon());

@Mock
private Iterator<Triple> mockIterator;

private UnmanagedRdfStream testStream;

@Before
public void setUp() {
initMocks(this);
testStream = new UnmanagedRdfStream(mockIterator);
}

@Test
public void testFiltering() {
when(mockIterator.hasNext()).thenReturn(true, true, false);
when(mockIterator.next()).thenReturn(managedTriple, unManagedTriple);
assertEquals("Didn't get unmanaged triple!", unManagedTriple,
testStream.next());
assertFalse("Failed to filter managed triple!", testStream.hasNext());
}

}

0 comments on commit 31380cb

Please sign in to comment.