Skip to content

Commit

Permalink
Include <> a ldp:RDFSource assertions in the generated RDF
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Oct 20, 2014
1 parent 58e32d1 commit 53e2b1a
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 16 deletions.
Expand Up @@ -36,7 +36,7 @@
import org.fcrepo.kernel.impl.rdf.ManagedRdf;
import org.fcrepo.kernel.impl.rdf.impl.AclRdfContext;
import org.fcrepo.kernel.impl.rdf.impl.ChildrenRdfContext;
import org.fcrepo.kernel.impl.rdf.impl.ContainerRdfContext;
import org.fcrepo.kernel.impl.rdf.impl.LdpRdfContext;
import org.fcrepo.kernel.impl.rdf.impl.ContentRdfContext;
import org.fcrepo.kernel.impl.rdf.impl.HashRdfContext;
import org.fcrepo.kernel.impl.rdf.impl.LdpContainerRdfContext;
Expand Down Expand Up @@ -254,7 +254,7 @@ public RdfStream apply(final FedoraResource child) {
}

if (ldpPreferences.prefersServerManaged()) {
rdfStream.concat(getTriples(ContainerRdfContext.class));
rdfStream.concat(getTriples(LdpRdfContext.class));
}
}

Expand Down
Expand Up @@ -307,7 +307,7 @@ public String apply(final RDFNode input) {
"class org.fcrepo.kernel.impl.rdf.impl.LdpIsMemberOfRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.ReferencesRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.TypeRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.ContainerRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.LdpRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.PropertiesRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.ChildrenRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.AclRdfContext",
Expand Down Expand Up @@ -340,7 +340,7 @@ public String apply(final RDFNode input) {
"class org.fcrepo.kernel.impl.rdf.impl.LdpIsMemberOfRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.ReferencesRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.TypeRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.ContainerRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.LdpRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.PropertiesRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.ChildrenRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.AclRdfContext",
Expand Down Expand Up @@ -503,7 +503,7 @@ public String apply(final RDFNode input) {
"class org.fcrepo.kernel.impl.rdf.impl.LdpIsMemberOfRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.ReferencesRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.TypeRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.ContainerRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.LdpRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.PropertiesRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.ChildrenRdfContext",
"class org.fcrepo.kernel.impl.rdf.impl.AclRdfContext",
Expand Down
Expand Up @@ -17,6 +17,7 @@

import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.Resource;
import org.fcrepo.kernel.FedoraObject;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.identifiers.IdentifierConverter;
import org.slf4j.Logger;
Expand All @@ -36,15 +37,16 @@
import static org.fcrepo.kernel.RdfLexicon.HAS_MEMBER_RELATION;
import static org.fcrepo.kernel.RdfLexicon.LDP_MEMBER;
import static org.fcrepo.kernel.RdfLexicon.MEMBERSHIP_RESOURCE;
import static org.fcrepo.kernel.RdfLexicon.RDF_SOURCE;
import static org.slf4j.LoggerFactory.getLogger;

/**
* @author cabeer
* @since 9/16/14
*/
public class ContainerRdfContext extends NodeRdfContext {
public class LdpRdfContext extends NodeRdfContext {

private static final Logger LOGGER = getLogger(ContainerRdfContext.class);
private static final Logger LOGGER = getLogger(LdpRdfContext.class);

/**
* Default constructor.
Expand All @@ -53,16 +55,23 @@ public class ContainerRdfContext extends NodeRdfContext {
* @param graphSubjects
* @throws javax.jcr.RepositoryException
*/
public ContainerRdfContext(final FedoraResource resource,
final IdentifierConverter<Resource, FedoraResource> graphSubjects)
public LdpRdfContext(final FedoraResource resource,
final IdentifierConverter<Resource, FedoraResource> graphSubjects)
throws RepositoryException {
super(resource, graphSubjects);

concat(containerContext());
concat(membershipRelationContext());
concat(typeContext());

if (isContainer()) {
concat(membershipRelationContext());
}

}

private boolean isContainer() {
return resource() instanceof FedoraObject;
}

private Collection<Triple> membershipRelationContext() throws RepositoryException {

final Set<Triple> triples1 = new HashSet<>();
Expand All @@ -78,11 +87,18 @@ private Collection<Triple> membershipRelationContext() throws RepositoryExceptio
return triples1;
}

private Triple[] containerContext() {
return new Triple[] {
create(subject(), type.asNode(), CONTAINER.asNode()),
create(subject(), type.asNode(), DIRECT_CONTAINER.asNode())
};
private Triple[] typeContext() {
final Triple rdfSource = create(subject(), type.asNode(), RDF_SOURCE.asNode());

if (isContainer()) {
return new Triple[]{
create(subject(), type.asNode(), CONTAINER.asNode()),
create(subject(), type.asNode(), DIRECT_CONTAINER.asNode()),
rdfSource
};
} else {
return new Triple[] { rdfSource };
}
}

}
@@ -0,0 +1,114 @@
/**
* Copyright 2014 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.impl.rdf.impl;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDF;
import org.fcrepo.kernel.FedoraObject;
import org.fcrepo.kernel.FedoraResource;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;

import javax.jcr.RepositoryException;
import javax.jcr.Session;

import static org.fcrepo.jcr.FedoraJcrTypes.LDP_HAS_MEMBER_RELATION;
import static org.fcrepo.jcr.FedoraJcrTypes.LDP_MEMBER_RESOURCE;
import static org.fcrepo.kernel.RdfLexicon.CONTAINER;
import static org.fcrepo.kernel.RdfLexicon.DIRECT_CONTAINER;
import static org.fcrepo.kernel.RdfLexicon.HAS_MEMBER_RELATION;
import static org.fcrepo.kernel.RdfLexicon.LDP_MEMBER;
import static org.fcrepo.kernel.RdfLexicon.MEMBERSHIP_RESOURCE;
import static org.fcrepo.kernel.RdfLexicon.RDF_SOURCE;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

/**
* @author cabeer
*/
public class LdpRdfContextTest {

@Mock
private FedoraResource mockResource;

@Mock
private FedoraObject mockContainer;
@Mock
private Session mockSession;

private DefaultIdentifierTranslator subjects;

private LdpRdfContext testObj;


@Before
public void setUp() throws RepositoryException {
initMocks(this);
when(mockResource.getPath()).thenReturn("/a");

when(mockContainer.getPath()).thenReturn("/a");

subjects = new DefaultIdentifierTranslator(mockSession);
}

@Test
public void shouldIncludeRdfTypeAssertions() throws RepositoryException {
testObj = new LdpRdfContext(mockResource, subjects);
final Model model = testObj.asModel();

assertTrue(model.contains(subject(), RDF.type, RDF_SOURCE));
}

@Test
public void shouldIncludeRdfContainerAssertions() throws RepositoryException {
testObj = new LdpRdfContext(mockContainer, subjects);
final Model model = testObj.asModel();

assertTrue(model.contains(subject(), RDF.type, RDF_SOURCE));
assertTrue(model.contains(subject(), RDF.type, CONTAINER));
assertTrue(model.contains(subject(), RDF.type, DIRECT_CONTAINER));
}

@Test
public void shouldIncludeDefaultContainerProperties() throws RepositoryException {
testObj = new LdpRdfContext(mockContainer, subjects);
final Model model = testObj.asModel();

assertTrue(model.contains(subject(), HAS_MEMBER_RELATION, LDP_MEMBER));
assertTrue(model.contains(subject(), MEMBERSHIP_RESOURCE, subject()));
}

@Test
public void shouldNotIncludeDefaultContainerPropertiesWhenSet() throws RepositoryException {
when(mockContainer.hasProperty(LDP_HAS_MEMBER_RELATION)).thenReturn(true);
when(mockContainer.hasProperty(LDP_MEMBER_RESOURCE)).thenReturn(true);

testObj = new LdpRdfContext(mockContainer, subjects);
final Model model = testObj.asModel();

assertFalse(model.contains(subject(), HAS_MEMBER_RELATION, LDP_MEMBER));
assertFalse(model.contains(subject(), MEMBERSHIP_RESOURCE, subject()));
}

private Resource subject() {
return subjects.reverse().convert(mockResource);
}

}
2 changes: 2 additions & 0 deletions fcrepo-kernel/src/main/java/org/fcrepo/kernel/RdfLexicon.java
Expand Up @@ -178,6 +178,8 @@ public final class RdfLexicon {
createProperty(LDP_NAMESPACE + "contains");
public static final Property LDP_MEMBER =
createProperty(LDP_NAMESPACE + "member");
public static final Property RDF_SOURCE =
createProperty(LDP_NAMESPACE + "RDFSource");
public static final Property NON_RDF_SOURCE =
createProperty(LDP_NAMESPACE + "NonRDFSource");

Expand Down

0 comments on commit 53e2b1a

Please sign in to comment.