package org.spbu.bdd.samples.assign;

import java.util.LinkedList;
import java.util.Set;

import org.spbu.bdd.core.Bdd;
import org.spbu.bdd.core.Bdd.Filter;
//import org.spbu.bdd.core.Bdd.Path;

public class AssignBdd {
	Timetable timetable;

	class ContextStorage {
		final int stopVal = -1;
		class TTFilter implements Filter<Integer> {

			@Override
			public Integer getStopVal() {
				return stopVal;
			}

			@Override
			public boolean isSolution(LinkedList<Integer> lst) {
				return true;
			}

			@Override
			public boolean canBeInSolution(Integer value, int index) {
				return !(stopVal==value);
			}

			@Override
			public void checkFinished() {
			}
		}
		
		Bdd<Integer>[] consumer;
		TTFilter filter = new TTFilter();

		public ContextStorage(Timetable t) {
			Bdd.Init(1000000, 1009, filter, 2000000, 20000);
			consumer = new Bdd[t.getSize()];
			for (int i = 0; i < consumer.length; i++) {
				consumer[i] = Bdd.makeConst(stopVal);
				consumer[i] = consumer[i].insertNewList(t
						.getConsumerResources(i));
			}
		}

		public void addIteration(int index) {
			for (int i = 0; i < index; i++)
				consumer[index] = consumer[index].condNE(consumer[i]);
		}

		public Set<LinkedList<Integer>> getResult() {
			//LinkedList<Integer> tmp=Bdd.getSolution(consumer);
			return Bdd.getResult(consumer);
		}
	}

	Set<LinkedList<Integer>> solutions;

	public Set<LinkedList<Integer>> solve(Timetable t) {
		timetable = t;

		ContextStorage s = new ContextStorage(timetable);

		for (int i = 0; i < t.getSize(); i++)
			s.addIteration(i);
		
		solutions = s.getResult();

		return solutions;
	}
}
