Skip to content

Commit

Permalink
[Enhancement #73] Refactor Sesame driver to support the new plural co…
Browse files Browse the repository at this point in the history
…ntexts supporting axiom descriptors.
  • Loading branch information
ledsoft committed Sep 24, 2020
1 parent 78975d1 commit 3753749
Show file tree
Hide file tree
Showing 25 changed files with 436 additions and 482 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import org.eclipse.rdf4j.model.ValueFactory;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

abstract class AbstractSesameIterator implements SesameIterator {

Expand All @@ -50,6 +52,10 @@ public AbstractSesameIterator(ListDescriptor listDescriptor, Connector connector
this.vf = vf;
}

protected Set<IRI> contexts() {
return context != null ? Collections.singleton(context) : Collections.emptySet();
}

protected void checkSuccessorMax(Collection<Statement> stmts, IRI property) {
// We don't mind the same statement multiple times, it could have been added during transaction
if (new HashSet<>(stmts).size() > 1) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
/**
* Copyright (C) 2020 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.ontodriver.sesame;

Expand Down Expand Up @@ -99,12 +97,14 @@ private Assertion processAssertions(AxiomDescriptor descriptor) {

Collection<Axiom<?>> loadAxioms(NamedResource individual, boolean includeInferred, java.net.URI context)
throws SesameDriverException {
final IRI sesameContext = SesameUtils.toSesameIri(context, valueFactory);
final Set<IRI> contextIris =
context != null ? Collections.singleton(SesameUtils.toSesameIri(context, valueFactory)) :
Collections.emptySet();
final IRI subject = SesameUtils.toSesameIri(individual.getIdentifier(), valueFactory);
final AxiomBuilder axiomBuilder = new AxiomBuilder(individual, Collections.emptyMap(),
Assertion.createUnspecifiedPropertyAssertion(includeInferred));
final Collection<Statement> statements = connector
.findStatements(subject, null, null, includeInferred, sesameContext);
.findStatements(subject, null, null, includeInferred, contextIris);
return statements.stream().map(axiomBuilder::statementToAxiom).collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,16 @@
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.ValueFactory;

import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;

/**
* This class performs an epistemic remove of axioms described by the axiom descriptor.
* <p/>
* Epistemic remove means that only information known to the application is
* deleted. The assertions in the descriptor represent this information. Thus,
* only these assertions are removed from the ontology. Note that if the
* descriptor contains an unspecified property assertion, all property
* assertions related to the subject individual are removed from the property's
* context.
* Epistemic remove means that only information known to the application is deleted. The assertions in the descriptor
* represent this information. Thus, only these assertions are removed from the ontology. Note that if the descriptor
* contains an unspecified property assertion, all property assertions related to the subject individual are removed
* from the property's context.
*/
class EpistemicAxiomRemover {

Expand All @@ -54,11 +52,11 @@ void remove(AbstractAxiomDescriptor axiomDescriptor) throws SesameDriverExceptio
if (a.isInferred()) {
continue;
}
for (URI ctx : axiomDescriptor.getAssertionContexts(a)) {
final IRI contextUri = SesameUtils.toSesameIri(ctx, valueFactory);
toRemove.addAll(connector.findStatements(individual,
SesameUtils.toSesameIri(a.getIdentifier(), valueFactory), null, a.isInferred(), contextUri));
}
final Set<IRI> contexts = axiomDescriptor.getAssertionContexts(a).stream()
.map(uri -> SesameUtils.toSesameIri(uri, valueFactory))
.collect(Collectors.toSet());
toRemove.addAll(connector.findStatements(individual,
SesameUtils.toSesameIri(a.getIdentifier(), valueFactory), null, a.isInferred(), contexts));
}
connector.removeStatements(toRemove);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
/**
* Copyright (C) 2020 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.ontodriver.sesame;

Expand All @@ -23,9 +21,7 @@
import cz.cvut.kbss.ontodriver.sesame.util.SesameUtils;
import org.eclipse.rdf4j.model.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.*;

/**
* Base class for list handlers.
Expand All @@ -46,8 +42,7 @@ abstract class ListHandler<T extends ListDescriptor, V extends ListValueDescript
}

/**
* Loads axioms representing list described by the specified list
* descriptor.
* Loads axioms representing list described by the specified list descriptor.
*
* @return Collection of axioms representing sequence values
* @throws SesameDriverException When storage access error occurs
Expand Down Expand Up @@ -96,17 +91,17 @@ void updateList(V listValueDescriptor) throws SesameDriverException {
if (listValueDescriptor.getValues().isEmpty()) {
clearList(listValueDescriptor);
} else if (isOldListEmpty(owner(listValueDescriptor), hasList(listValueDescriptor),
listValueDescriptor.getListProperty().isInferred(), context(listValueDescriptor))) {
listValueDescriptor.getListProperty().isInferred(), contexts(listValueDescriptor))) {
persistList(listValueDescriptor);
} else {
mergeList(listValueDescriptor);
}
}

private boolean isOldListEmpty(Resource owner, IRI hasListProperty, boolean includeInferred,
IRI context) throws SesameDriverException {
Set<IRI> contexts) throws SesameDriverException {
final Collection<Statement> stmts = connector.findStatements(owner, hasListProperty, null,
includeInferred, context);
includeInferred, contexts);
return stmts.isEmpty();
}

Expand Down Expand Up @@ -150,6 +145,11 @@ Resource extractListNode(Collection<Statement> stmts, IRI nodeAssertion) {
return (Resource) val;
}

Set<IRI> contexts(ListDescriptor listDescriptor) {
final IRI ctx = sesameIri(listDescriptor.getContext());
return ctx != null ? Collections.singleton(ctx) : Collections.emptySet();
}

IRI context(ListDescriptor listDescriptor) {
return sesameIri(listDescriptor.getContext());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
/**
* Copyright (C) 2020 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.ontodriver.sesame;

import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
import cz.cvut.kbss.ontodriver.descriptor.ReferencedListDescriptor;
import cz.cvut.kbss.ontodriver.descriptor.ReferencedListValueDescriptor;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.ValueFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.*;

public class ReferencedListHandler extends
ListHandler<ReferencedListDescriptor, ReferencedListValueDescriptor> {
Expand Down Expand Up @@ -68,7 +63,7 @@ private IRI generateSequenceNode(IRI owner, IRI context) throws SesameDriverExce
do {
node = vf.createIRI(uriBase + "-SEQ_" + sequenceCounter++);
final Collection<Statement> stmts = connector.findStatements(node, null, null, false,
context);
context != null ? Collections.singleton(context) : Collections.emptySet());
unique = stmts.isEmpty();
} while (!unique);
return node;
Expand Down Expand Up @@ -107,7 +102,7 @@ void clearList(ReferencedListValueDescriptor listDescriptor) throws SesameDriver
final IRI hasNext = hasNext(listDescriptor);
final IRI hasContent = hasContent(listDescriptor);
final boolean includeInferred = listDescriptor.getListProperty().isInferred();
final IRI context = context(listDescriptor);
final Set<IRI> context = contexts(listDescriptor);
Resource previous = owner(listDescriptor);
IRI currentProperty = hasList(listDescriptor);
final Collection<Statement> toRemove = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
/**
* Copyright (C) 2020 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.ontodriver.sesame;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import cz.cvut.kbss.ontodriver.sesame.util.SesameUtils;

import cz.cvut.kbss.ontodriver.exception.IntegrityConstraintViolatedException;
import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
import cz.cvut.kbss.ontodriver.descriptor.ReferencedListDescriptor;
import cz.cvut.kbss.ontodriver.exception.IntegrityConstraintViolatedException;
import cz.cvut.kbss.ontodriver.model.Axiom;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
import cz.cvut.kbss.ontodriver.sesame.util.SesameUtils;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.ValueFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

class ReferencedListIterator extends AbstractSesameIterator {

private final ReferencedListDescriptor listDescriptor;
Expand All @@ -53,11 +50,11 @@ public ReferencedListIterator(ReferencedListDescriptor listDescriptor, Connector
}

private void init() throws SesameDriverException {
this.next = connector.findStatements(listOwner, hasListProperty, null, includeInferred, context);
this.next = connector.findStatements(listOwner, hasListProperty, null, includeInferred, contexts());
}

@Override
public boolean hasNext() throws SesameDriverException {
public boolean hasNext() {
return !next.isEmpty();
}

Expand All @@ -77,17 +74,17 @@ private void nextInternal() throws SesameDriverException {
checkNodeIsResource(currentNode);
final Resource elem = (Resource) currentNode.getObject();
this.currentContent = getNodeContent(elem);
this.next = connector.findStatements(elem, hasNextProperty, null, includeInferred, context);
this.next = connector.findStatements(elem, hasNextProperty, null, includeInferred, contexts());
}

private Statement getNodeContent(Resource node) throws SesameDriverException {
final Collection<Statement> elems = connector.findStatements(node, hasContentProperty,
null, includeInferred, context);
checkSuccessorMax(elems, hasContentProperty);
if (elems.isEmpty()) {
final Collection<Statement> elements = connector.findStatements(node, hasContentProperty,
null, includeInferred, contexts());
checkSuccessorMax(elements, hasContentProperty);
if (elements.isEmpty()) {
throw new IntegrityConstraintViolatedException("Node " + node + " has no content.");
}
final Statement elem = elems.iterator().next();
final Statement elem = elements.iterator().next();
checkNodeIsResource(elem);
return elem;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.model.ValueFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.*;

class SimpleListHandler extends ListHandler<SimpleListDescriptor, SimpleListValueDescriptor> {

Expand Down Expand Up @@ -71,15 +68,15 @@ List<Statement> createListRest(IRI head, SimpleListValueDescriptor listValueDesc
*/
@Override
void clearList(SimpleListValueDescriptor listValueDescriptor) throws SesameDriverException {
final IRI context = context(listValueDescriptor);
final Set<IRI> contexts = contexts(listValueDescriptor);
final Collection<Statement> toRemove = new ArrayList<>();
IRI currentProperty = hasList(listValueDescriptor);
final IRI hasNext = hasNext(listValueDescriptor);
final boolean includeInferred = listValueDescriptor.getNextNode().isInferred();
Collection<Statement> stmts;
Resource subject = owner(listValueDescriptor);
do {
stmts = connector.findStatements(subject, currentProperty, null, includeInferred, context);
stmts = connector.findStatements(subject, currentProperty, null, includeInferred, contexts);
if (!stmts.isEmpty()) {
subject = extractListNode(stmts, hasNext);
toRemove.addAll(stmts);
Expand Down
Loading

0 comments on commit 3753749

Please sign in to comment.