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

edu.stanford.protege.webprotege.trigger.TriggerRunner Maven / Gradle / Ivy

The newest version!
package edu.stanford.protege.webprotege.trigger;

import com.google.common.collect.ImmutableList;
import edu.stanford.protege.webprotege.match.Matcher;
import org.semanticweb.owlapi.model.OWLEntity;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Matthew Horridge
 * Stanford Center for Biomedical Informatics Research
 * 7 Jun 2018
 */
public class TriggerRunner {

    @Nonnull
    private final ImmutableList triggers;

    @Nonnull
    private final List> contexts = new ArrayList<>();

    public TriggerRunner(@Nonnull ImmutableList triggers) {
        this.triggers = checkNotNull(triggers);
    }

    public void execute(@Nonnull Stream entities) {
        checkNotNull(entities);
        begin();
        entities.forEach(this::execute);
        end();
    }

    private void begin() {
        contexts.clear();
        triggers.forEach(trigger -> {
            List triggerContexts = new ArrayList<>();
            trigger.getTriggerActions().forEach(triggerAction -> {
                Object context = triggerAction.begin();
                triggerContexts.add(context);
            });
            contexts.add(triggerContexts);
        });
    }

    @SuppressWarnings("unchecked")
    private void execute(@Nonnull OWLEntity entity) {
        // Unchecked because we ask the trigger to create the context
        // and it handles the same type of context in the begin, execute,
        // and end methods.
        checkContextsSize();
        for (int i = 0; i < triggers.size(); i++) {
            Trigger trigger = triggers.get(i);
            Matcher matcher = trigger.getMatcher();
            if (matcher.matches(checkNotNull(entity))) {
                List triggerActions = trigger.getTriggerActions();
                List triggerContexts = contexts.get(i);
                for (int j = 0; j < triggerActions.size(); j++) {
                    TriggerAction triggerAction = triggerActions.get(j);
                    Object context = triggerContexts.get(j);
                    triggerAction.execute(entity, context);
                }
            }
        }
    }


    @SuppressWarnings("unchecked")
    private void end() {
        // Unchecked because we ask the trigger to create the context
        // and it handles the same type of context in the begin, execute,
        // and end methods.
        checkContextsSize();
        for (int i = 0; i < triggers.size(); i++) {
            Trigger trigger = triggers.get(i);
            List triggerActions = trigger.getTriggerActions();
            List triggerContexts = contexts.get(i);
            for (int j = 0; j < triggerActions.size(); j++) {
                TriggerAction triggerAction = triggerActions.get(j);
                Object context = triggerContexts.get(j);
                triggerAction.end(context);
            }
        }

    }

    private void checkContextsSize() {
        if (contexts.size() != triggers.size()) {
            throw new IllegalStateException("begin has not been called");
        }
    }
}