Skip to content

Commit

Permalink
Distinguishing between managed and unmanaged RDF
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Oct 24, 2013
1 parent d08914b commit dd2ae9a
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 10 deletions.
67 changes: 61 additions & 6 deletions fcrepo-kernel/src/main/java/org/fcrepo/kernel/RdfLexicon.java
Expand Up @@ -15,8 +15,14 @@
*/
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 @@ -58,6 +64,9 @@ public final class RdfLexicon {
public static final Property HAS_CHILD_COUNT =
createProperty(REPOSITORY_NAMESPACE + "numberOfChildren");

public static final Set<Property> membershipProperties = of(
HAS_MEMBER_OF_RESULT, HAS_PARENT, HAS_CHILD, HAS_CHILD_COUNT);

// FIXITY
public static final Property IS_FIXITY_RESULT_OF =
createProperty(REPOSITORY_NAMESPACE + "isFixityResultOf");
Expand All @@ -77,6 +86,10 @@ public final class RdfLexicon {
public static final Property HAS_FIXITY_REPAIRED_COUNT =
createProperty(REPOSITORY_NAMESPACE + "numFixityRepaired");

public static final Set<Property> fixityProperties = of(
IS_FIXITY_RESULT_OF, HAS_FIXITY_RESULT, HAS_FIXITY_STATE,
HAS_COMPUTED_CHECKSUM, HAS_COMPUTED_SIZE, HAS_FIXITY_CHECK_COUNT,
HAS_FIXITY_ERROR_COUNT, HAS_FIXITY_REPAIRED_COUNT);

// SEARCH
public static final Property SEARCH_PAGE = createProperty("http://sindice.com/vocab/search#Page");
Expand All @@ -91,16 +104,19 @@ public final class RdfLexicon {
public static final Property SEARCH_HAS_MORE =
createProperty(RESTAPI_NAMESPACE + "hasMoreResults");

public static final Set<Property> searchProperties = of(SEARCH_PAGE,
SEARCH_HAS_TOTAL_RESULTS, SEARCH_ITEMS_PER_PAGE, SEARCH_OFFSET,
SEARCH_OFFSET, SEARCH_TERMS, SEARCH_HAS_MORE);

// Linked Data Platform
public static final Property PAGE =
createProperty("http://www.w3.org/ns/ldp#Page");
public static final Property PAGE_OF =
createProperty("http://www.w3.org/ns/ldp#pageOf");

public static final Property FIRST_PAGE =
createProperty("http://www.w3.org/ns/ldp#firstPage");
public static final Property NEXT_PAGE =
createProperty("http://www.w3.org/ns/ldp#nextPage");

public static final Property MEMBERS_INLINED =
createProperty("http://www.w3.org/ns/ldp#membersInlined");
public static final Property CONTAINER =
Expand All @@ -114,8 +130,12 @@ public final class RdfLexicon {
public static final Property MEMBER_SUBJECT =
createProperty("http://www.w3.org/ns/ldp#MemberSubject");
public static final Property INLINED_RESOURCE =
createProperty("http://www.w3.org/ns/ldp#inlinedResource");
createProperty("http://www.w3.org/ns/ldp#inlinedResource");

public static final Set<Property> ldpProperties = of(PAGE, PAGE_OF,
FIRST_PAGE, NEXT_PAGE, MEMBERS_INLINED, CONTAINER,
MEMBERSHIP_SUBJECT, MEMBERSHIP_PREDICATE, MEMBERSHIP_OBJECT,
MEMBER_SUBJECT, INLINED_RESOURCE);

// REPOSITORY INFORMATION
public static final Property HAS_OBJECT_COUNT =
Expand All @@ -134,13 +154,20 @@ public final class RdfLexicon {
public static final Property HAS_SITEMAP =
createProperty("http://microformats.org/wiki/rel-sitemap");

public static final Set<Property> repositoryProperties = of(
HAS_OBJECT_COUNT, HAS_OBJECT_SIZE, HAS_TRANSACTION_SERVICE,
HAS_NAMESPACE_SERVICE, HAS_WORKSPACE_SERVICE, HAS_SEARCH_SERVICE,
HAS_SITEMAP);

// NAMESPACES
public static final Property HAS_NAMESPACE_PREFIX =
createProperty("http://purl.org/vocab/vann/preferredNamespacePrefix");
public static final Property HAS_NAMESPACE_URI =
createProperty("http://purl.org/vocab/vann/preferredNamespaceUri");
public static final Property VOAF_VOCABULARY = createProperty("http://purl.org/vocommons/voaf#Vocabulary");

public static final Set<Property> namespaceProperties = of(
HAS_NAMESPACE_PREFIX, HAS_NAMESPACE_URI, VOAF_VOCABULARY);

// OTHER SERVICES
public static final Property HAS_SERIALIZATION =
Expand All @@ -158,6 +185,11 @@ public final class RdfLexicon {
public static final Property NOT_IMPLEMENTED =
createProperty(REPOSITORY_NAMESPACE + "notImplemented");

public static final Set<Property> otherServiceProperties = of(
HAS_SERIALIZATION, HAS_VERSION_HISTORY, HAS_FIXITY_SERVICE,
HAS_FEED, HAS_SUBSCRIPTION_SERVICE, NOT_IMPLEMENTED);


// CONTENT
public static final Property HAS_CONTENT =
createProperty(REPOSITORY_NAMESPACE + "hasContent");
Expand All @@ -170,12 +202,19 @@ public final class RdfLexicon {
public static final Property HAS_SIZE =
createProperty(REPOSITORY_NAMESPACE + "hasSize");

public static final Set<Property> contentProperties = of(HAS_CONTENT,
IS_CONTENT_OF, HAS_LOCATION, HAS_MIME_TYPE, HAS_SIZE);


// VERSIONING
public static final Property HAS_VERSION =
createProperty(REPOSITORY_NAMESPACE + "hasVersion");
public static final Property HAS_VERSION_LABEL =
createProperty(REPOSITORY_NAMESPACE + "hasVersionLabel");

public static final Set<Property> versioningProperties = of(HAS_VERSION,
HAS_VERSION_LABEL);

// RDF EXTRACTION
public static final Property COULD_NOT_STORE_PROPERTY =
createProperty(REPOSITORY_NAMESPACE + "couldNotStoreProperty");
Expand All @@ -189,24 +228,40 @@ public final class RdfLexicon {
createProperty(REPOSITORY_NAMESPACE + "hasNodeType");
public static final Property HAS_MIXIN_TYPE =
createProperty(REPOSITORY_NAMESPACE + "mixinTypes");

public static final Property CREATED_DATE =
createProperty(REPOSITORY_NAMESPACE + "created");
public static final Property CREATED_BY =
createProperty(REPOSITORY_NAMESPACE + "createdBy");


public static final Property LAST_MODIFIED_DATE =
createProperty(REPOSITORY_NAMESPACE + "lastModified");
public static final Property LAST_MODIFIED_BY =
createProperty(REPOSITORY_NAMESPACE + "lastModifiedBy");

public static final Set<Property> jcrProperties = of(
HAS_PRIMARY_IDENTIFIER, HAS_PRIMARY_TYPE, HAS_NODE_TYPE,
HAS_MIXIN_TYPE, CREATED_DATE, CREATED_BY, LAST_MODIFIED_DATE,
LAST_MODIFIED_BY);

public static final Property RDFS_LABEL =
createProperty("http://www.w3.org/2000/01/rdf-schema#label");
public static final Property DC_TITLE =
createProperty("http://purl.org/dc/terms/title");

public static final Set<Property> managedProperties;

static {
final ImmutableSet.Builder<Property> b = ImmutableSet.builder();
b.addAll(membershipProperties).addAll(fixityProperties).addAll(
searchProperties).addAll(ldpProperties).addAll(
repositoryProperties).addAll(namespaceProperties).addAll(
otherServiceProperties).addAll(contentProperties).addAll(
versioningProperties).addAll(jcrProperties);
managedProperties = b.build();
}

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

private RdfLexicon() {

}
Expand Down
Expand Up @@ -17,6 +17,7 @@
package org.fcrepo.kernel.utils.iterators;

import static com.google.common.collect.Iterators.singletonIterator;
import static com.google.common.collect.Iterators.transform;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static java.util.Collections.emptySet;

Expand All @@ -26,10 +27,12 @@
import java.util.Map;
import java.util.Set;

import com.google.common.base.Function;
import com.google.common.collect.ForwardingIterator;
import com.google.common.collect.Iterators;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Statement;

/**
* @author ajs6f
Expand All @@ -40,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 Expand Up @@ -156,4 +159,23 @@ public Model asModel() {
return model;
}

/**
* @param model A {@link Model} containing the prefix mappings and triples to be put into
* this stream of RDF
* @return
*/
public static RdfStream fromModel(final Model model) {
final Iterator<Triple> triples = transform(model.listStatements(), statement2triple);
return new RdfStream(triples).addNamespaces(model.getNsPrefixMap());
}

public static Function<Statement, Triple> statement2triple = new Function<Statement, Triple>() {

@Override
public Triple apply(final Statement s) {
return s.asTriple();
}

};

}
@@ -0,0 +1,67 @@
/**
* Copyright 2013 DuraSpace, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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());
}

};

/**
* Ordinary constructor.
*
* @param triples
*/
public UnmanagedRdfStream(final Iterator<Triple> triples) {
super(triples);
}

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

}
Expand Up @@ -18,7 +18,10 @@

import static com.hp.hpl.jena.graph.NodeFactory.createAnon;
import static com.hp.hpl.jena.graph.Triple.create;
import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static org.fcrepo.kernel.utils.iterators.RdfStream.fromModel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -49,6 +52,9 @@ public class RdfStreamTest {
@Mock
private Triple triple1, triple2, triple3;

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

private final static String prefix1 = "testNS";

private final static String uri1 = "http://testNS";
Expand Down Expand Up @@ -155,13 +161,23 @@ public void testAddNamespaces() {

@Test
public void testAsModel() throws RepositoryException {
final Triple t = create(createAnon(), createAnon(), createAnon());
testStream = new RdfStream(singletonList(t));
testStream = new RdfStream(singletonList(triple));
testStream.addNamespaces(testNamespaces);

final Model testModel = testStream.asModel();
assertEquals(testModel.getNsPrefixMap(), testNamespaces);
assertTrue(testModel.contains(testModel.asStatement(t)));
assertTrue(testModel.contains(testModel.asStatement(triple)));
}

@Test
public void testFromModel() {
final Model model = createDefaultModel();
model.setNsPrefix(prefix1, uri1);
testStream = fromModel(model.add(model.asStatement(triple)));
assertEquals("Didn't find triple in stream from Model!", triple,
testStream.next());
assertEquals("Didn't find namespace mapping in stream from Model!",
singletonMap(prefix1, uri1), testStream.namespaces());
}

@Test
Expand Down

0 comments on commit dd2ae9a

Please sign in to comment.