Skip to content

Commit

Permalink
Use Jena's notion of graph containment instead of our strict equality
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Oct 17, 2014
1 parent 16eac6b commit 2ade9b8
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 154 deletions.
Expand Up @@ -16,7 +16,6 @@
package org.fcrepo.kernel.impl;

import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.ImmutableSet.copyOf;
import static com.google.common.collect.Iterators.singletonIterator;
import static com.google.common.collect.Lists.newArrayList;
import static com.hp.hpl.jena.update.UpdateAction.execute;
Expand All @@ -35,7 +34,6 @@
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;


import javax.jcr.ItemNotFoundException;
Expand All @@ -58,7 +56,7 @@
import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.identifiers.IdentifierConverter;
import org.fcrepo.kernel.impl.utils.JcrPropertyStatementListener;
import org.fcrepo.kernel.utils.iterators.DifferencingIterator;
import org.fcrepo.kernel.utils.iterators.GraphDifferencingIterator;
import org.fcrepo.kernel.impl.utils.iterators.RdfAdder;
import org.fcrepo.kernel.impl.utils.iterators.RdfRemover;
import org.fcrepo.kernel.utils.iterators.NodeIterator;
Expand All @@ -67,9 +65,7 @@
import org.modeshape.jcr.api.JcrTools;
import org.slf4j.Logger;

import com.google.common.collect.Iterables;
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.update.UpdateRequest;

Expand Down Expand Up @@ -417,28 +413,23 @@ public Boolean isNew() {
* (org.fcrepo.kernel.identifiers.IdentifierConverter, com.hp.hpl.jena.rdf.model.Model)
*/
@Override
public RdfStream replaceProperties(final IdentifierConverter<Resource, FedoraResource> graphSubjects,
public void replaceProperties(final IdentifierConverter<Resource, FedoraResource> graphSubjects,

This comment has been minimized.

Copy link
@ajs6f

ajs6f Oct 17, 2014

Contributor

I prefer to return the result for chaining, but it's not a big thing.

final Model inputModel, final RdfStream originalTriples) {

final RdfStream replacementStream = RdfStream.fromModel(inputModel);
final RdfStream replacementStream = new RdfStream().namespaces(inputModel.getNsPrefixMap());

final Set<Triple> replacementTriples = copyOf(replacementStream);

final DifferencingIterator<Triple> differencer =
new DifferencingIterator<>(replacementTriples, originalTriples);
final GraphDifferencingIterator differencer =
new GraphDifferencingIterator(inputModel, originalTriples);

try {
new RdfRemover(graphSubjects, getSession(), replacementStream
.withThisContext(differencer)).consume();

new RdfAdder(graphSubjects, getSession(), replacementStream
.withThisContext(differencer.notCommon())).consume();
.withThisContext(differencer.notCommon().find(null, null, null))).consume();

This comment has been minimized.

Copy link
@ajs6f

ajs6f Oct 17, 2014

Contributor

Why find(null, null, null)? Can we clean up GraphDifferencingIterator to avoid this?

} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
}

return replacementStream.withThisContext(Iterables.concat(differencer
.common(), differencer.notCommon()));
}

/* (non-Javadoc)
Expand Down
Expand Up @@ -565,6 +565,25 @@ public boolean apply(final javax.jcr.Property property) {

}

@Test
public void testReplacePropertyWithSemanticEquivalent() throws RepositoryException {

This comment has been minimized.

Copy link
@ajs6f

ajs6f Oct 17, 2014

Contributor

Can we say testReplacePropertyWithRDFSpecEquality? I am really chary of the words "semantic equivalent".

final String pid = UUID.randomUUID().toString();
final FedoraObject object = objectService.findOrCreateObject(session, pid);
object.getNode().setProperty("dc:title", "xyz");
session.save();

final RdfStream triples = object.getTriples(subjects, PropertiesRdfContext.class);
final Model model = triples.asModel();

final Resource subject = subjects.reverse().convert(object);
final Property predicate = model.createProperty("http://purl.org/dc/elements/1.1/title");

This comment has been minimized.

Copy link
@ajs6f

model.removeAll(subject, predicate, null);

model.add(subject, predicate, createPlainLiteral("xyz"));

object.replaceProperties(subjects, model, object.getTriples(subjects, PropertiesRdfContext.class));

This comment has been minimized.

Copy link
@ajs6f

ajs6f Oct 17, 2014

Contributor

Is the test here just to see that no exception is thrown? I'm not sure what the assertion is?

This comment has been minimized.

Copy link
@cbeer

cbeer Oct 17, 2014

Author Contributor

I'm still trying to find out what the assertion is. I guess it's that the object it's just that the object isn't changed (not that we can assert that...)?

The real test will be down in the GraphDifferencingIterator itself.

This comment has been minimized.

Copy link
@ajs6f

ajs6f Oct 17, 2014

Contributor

Yeah, I think that would work. You could also add dc:title "foo" and subtract dc:title "foo"^string and see that it really went away. Maybe both.

}

@Test
public void testDeleteObject() throws RepositoryException {
Expand Down
Expand Up @@ -312,11 +312,10 @@ public void testReplacePropertiesDataset() throws Exception {
replacementModel.createProperty("j"),
"k");

final Model replacements = testObj.replaceProperties(defaultGraphSubjects,
testObj.replaceProperties(defaultGraphSubjects,
replacementModel,
propertiesStream).asModel();

assertTrue(replacements.containsAll(replacementModel));
propertiesStream);
// TODO
}
@Test
public void shouldGetEtagForAnObject() throws RepositoryException {
Expand Down
Expand Up @@ -19,7 +19,8 @@
import java.util.Iterator;

import javax.jcr.Node;
import javax.jcr.Property;import javax.jcr.version.Version;
import javax.jcr.Property;
import javax.jcr.version.Version;
import javax.jcr.version.VersionHistory;

import com.hp.hpl.jena.rdf.model.Resource;
Expand Down Expand Up @@ -175,7 +176,7 @@ RdfStream getTriples(IdentifierConverter<Resource, FedoraResource> graphSubjects
* @param inputModel
* @return RDFStream
*/
RdfStream replaceProperties(final IdentifierConverter<Resource, FedoraResource> graphSubjects,
void replaceProperties(final IdentifierConverter<Resource, FedoraResource> graphSubjects,

This comment has been minimized.

Copy link
@ajs6f

ajs6f Oct 17, 2014

Contributor

Might as well start changing out graphSubjects param name here...

final Model inputModel,
final RdfStream originalTriples);

Expand Down
Expand Up @@ -15,12 +15,14 @@
*/
package org.fcrepo.kernel.utils.iterators;

import static com.google.common.collect.Sets.newHashSet;

import java.util.Iterator;
import java.util.Set;

import com.google.common.collect.AbstractIterator;
import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

/**
* A wrapping {@link Iterator} that calculates two differences between a
Expand All @@ -30,39 +32,39 @@
*
* @author ajs6f
* @since Oct 24, 2013
* @param <E> The type of element common to both source and set.
*/
public class DifferencingIterator<E> extends AbstractIterator<E> {
public class GraphDifferencingIterator extends AbstractIterator<Triple> {

This comment has been minimized.

Copy link
@ajs6f

ajs6f Oct 17, 2014

Contributor

I would prefer to keep a generic form available. I think that amongst other things, that makes unit tests easier to understand.

This comment has been minimized.

Copy link
@cbeer

cbeer Oct 17, 2014

Author Contributor

How? Graph isn't a Set<Triple> or anything, is it?


Set<? extends E> notCommon;
Graph notCommon;

private Set<E> common;
private Graph common;

private Iterator<E> source;
private Iterator<Triple> source;

/**
* Ordinary constructor.
*
* @param original
* @param source
*/
public DifferencingIterator(final Set<? extends E> original,
final Iterator<E> source) {
public GraphDifferencingIterator(final Model original,
final Iterator<Triple> source) {
super();
this.notCommon = newHashSet(original);
this.common = newHashSet();
this.notCommon = original.getGraph();
this.common = ModelFactory.createDefaultModel().getGraph();
this.source = source;
}

@Override
protected E computeNext() {
protected Triple computeNext() {
if (source.hasNext()) {
E next = source.next();
Triple next = source.next();
// we only want to return this element if it is not common
// to the two inputs
while (common.contains(next) || notCommon.contains(next)) {
// it was common, so shift it to common
if (notCommon.remove(next)) {
if (notCommon.contains(next)) {
notCommon.delete(next);
common.add(next);
}
// move onto the next candidate
Expand All @@ -82,7 +84,7 @@ protected E computeNext() {
*
* @return The elements that turned out to be common to the two inputs.
*/
public Set<E> common() {
public Graph common() {
return source.hasNext() ? null : common;
}

Expand All @@ -91,7 +93,7 @@ public Set<E> common() {
*
* @return The elements that turned out not to be common to the two inputs.
*/
public Set<? extends E> notCommon() {
public Graph notCommon() {
return source.hasNext() ? null : notCommon;
}

Expand Down

This file was deleted.

@@ -0,0 +1,31 @@
/**
* 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.utils.iterators;

import org.junit.Test;

/**
* <p>DifferencingIteratorTest class.</p>
*
* @author ksclarke
*/
public class GraphDifferencingIteratorTest {

@Test
public void yepWeShouldHaveSome() {

This comment has been minimized.

Copy link
@ajs6f

ajs6f Oct 17, 2014

Contributor

This is one reason to leave the generic in. The original test should still work.


}
}

0 comments on commit 2ade9b8

Please sign in to comment.