package ua.ansymo.hintco

import org.eclipse.core.runtime.Assert

class ConstraintChecker implements IConstraintChecker {
	
	def private constraintsConsistent(ConstraintsStack constraints, HintConfiguration cs){
		try {
			constraints.forEach[m |
				m.forEach[srcN, trgNs|
					trgNs.forEach[trgN |
						if (! srcN.precedes.contains(trgN)){
							throw new InterruptedException()
						}
					]
				]
			]
		} catch (InterruptedException e) {
			return false
		}
		return true
	}
	
	override constraintIsFeasible(PrecendenceNode src, PrecendenceNode trg, VariantDiagram n, ConstraintsStack constraints, HintConfiguration cs) {
		/*
		  * A constraint is not feasible adding it to the precedence graph causes a new cycle to be formed.
		  */
		 val found = ModelQuery.pathExists(trg, src, constraints, cs)
		 Assert.isTrue(constraintsConsistent(constraints, cs))
		 return !found
	}
	
}