package rapanui.core;

import java.util.ArrayList;
import java.util.List;

import rapanui.dsl.Formula;
import rapanui.dsl.RuleSystemCollection;
import rapanui.dsl.Term;

import static rapanui.core.Patterns.*;

/**
 * The basic environment in which proofs are made. Contains a list of premises
 * and the resulting conclusions.
 */
public class ProofEnvironment {
	private final RuleSystemCollection ruleSystems;
	private final List<Formula> premises;
	private final List<ConclusionProcess> conclusions;
	private final List<Observer> observers;

	/**
	 * Create a new environment.
	 *
	 * @param ruleSystems The rule systems upon which any conclusions in the environment are based. Mmust not be null.
	 */
	public ProofEnvironment(RuleSystemCollection ruleSystems) {
		assert ruleSystems != null;

		this.ruleSystems = ruleSystems;
		this.premises = new ArrayList<Formula>();
		this.conclusions = new ArrayList<ConclusionProcess>();
		this.observers = new ArrayList<Observer>();
	}

	public RuleSystemCollection getRuleSystems() {
		return ruleSystems;
	}

	/**
	 * Add a new premise to the environment so it can be used in conclusions.
	 *
	 * @param premise The formula to add as premise. Must not be null.
	 *
	 * If the premise already exists in the environment, it will not be added again.
	 */
	public void addPremise(Formula premise) {
		assert premise != null;
		if (addToSet(premises, premise))
			notifyObservers(observers, Observer::premiseAdded, premise);
	}

	/**
	 * @return An array of premises. Guaranteed to be non-null.
	 */
	public Formula[] getPremises() {
		return listToArray(premises, Formula[]::new);
	}

	/**
	 * Starts a new conclusion process in the environment.
	 *
	 * @param startTerm The initial term of the conclusion. Must not be null.
	 */
	public void addConclusion(Term startTerm) {
		assert startTerm != null;
		ConclusionProcess conclusion = new ConclusionProcess(this, startTerm);
		conclusions.add(conclusion);
		notifyObservers(observers, Observer::conclusionStarted, conclusion);
	}

	public ConclusionProcess[] getConclusions() {
		return listToArray(conclusions, ConclusionProcess[]::new);
	}

	public interface Observer {
		void premiseAdded(Formula premise);
		void premiseRemoved(Formula premise); // unused for now

		void conclusionStarted(ConclusionProcess conclusion);
		void conclusionRemoved(ConclusionProcess conclusion); // unused for now
		void conclusionMoved(ConclusionProcess conclusion); // unused for now
	}

	/**
	 * Adds an observer to the environment. Observers are notified upon changes.
	 *
	 * @param observer The new observer
	 */
	public void addObserver(Observer observer) {
		observers.add(observer);
	}

	/**
	 * Unregisters an observer from the environment, so it is no longer notified of changes.
	 *
	 * @param observer The observer to remove
	 */
	public void deleteObserver(Observer observer) {
		observers.remove(observer);
	}
}

@@ -66,16 +66,52 @@
 il.List;
+%0Aimport java.util.stream.Collectors;
 %0A%0Aimport
@@ -1653,24 +1653,419 @@
 ::new);%0A%09%7D%0A%0A
+%09/**%0A%09 * @return An array of resolved premises (contains only @see Equation and @see Inclusion instances). Guaranteed to be non-null.%0A%09 */%0A%09public Formula%5B%5D getResolvedPremises() %7B%0A%09%09return listToArray(%0A%09%09%09premises.stream().flatMap(premise -%3E premise.resolve().stream())%0A%09%09%09%09.collect(Collectors.toMap(Formula::serialize, x -%3E x)).values(), // remove syntactic duplicates%0A%09%09%09Formula%5B%5D::new);%0A%09%7D%0A%0A
 %09/**%0A%09 * Sta

Implement ProofEnvironment.getResolvedPremises() (#27)