All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.uniroma2.art.semanticturkey.changetracking.sail.UndoStack Maven / Gradle / Ivy

There is a newer version: 13.1
Show newest version
package it.uniroma2.art.semanticturkey.changetracking.sail;

import it.uniroma2.art.semanticturkey.changetracking.vocabulary.CHANGETRACKER;
import it.uniroma2.art.semanticturkey.changetracking.vocabulary.PROV;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.util.Models;
import org.eclipse.rdf4j.model.util.Values;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Objects;
import java.util.Optional;

public class UndoStack implements Iterable {
    public static final int MAX_DEPTH = 10;

    private LinkedList storage;

    public UndoStack() {
        this.storage = new LinkedList<>();
    }

    public void push(StagingArea data) {

        if (storage.size() == MAX_DEPTH) {
            storage.clear();
        }

        Model commitMetadataModel = data.getCommitMetadataModel();
        Optional performer = getPerformer(commitMetadataModel);

        if (performer.isPresent()) {
            /* @Nullable */ StagingArea stackTip = storage.peek();
            if (stackTip != null) {
                Optional tipPerformer = getPerformer(stackTip.getCommitMetadataModel());
                if (!tipPerformer.filter(performer.get()::equals).isPresent()) {
                    storage.clear();
                }
            }

            storage.push(data);
        } else {
            storage.clear();
        }
    }

    public Optional peek() {
        return Optional.ofNullable(storage.peek());
    }

    public StagingArea pop() {
        return storage.pop();
    }

    public static Optional getPerformer(Model commitMetadataModel) {
        Optional performer = Models.getPropertyResources(commitMetadataModel, CHANGETRACKER.COMMIT_METADATA, PROV.QUALIFIED_ASSOCIATION)
                .stream()
                .filter(ass -> commitMetadataModel.contains(ass, PROV.HAD_ROLE, Values.iri("http://semanticturkey.uniroma2.it/ns/st-changelog#performer")))
                .map(ass -> Models.getPropertyIRI(commitMetadataModel, ass, PROV.HAS_AGENT).orElse(null))
                .filter(Objects::nonNull)
                .findAny();
        return performer;
    }

    @Override
    public Iterator iterator() {
        return storage.iterator();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy