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

org.drools.verifier.Solver Maven / Gradle / Ivy

There is a newer version: 9.44.0.Final
Show newest version
package org.drools.verifier;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.drools.verifier.components.OperatorDescr;
import org.drools.verifier.components.VerifierComponent;

/**
 * Takes a list of Constraints and makes possibilities from them.
 * 
 * @author Toni Rikkola
 */
class Solver {

	private List> possibilityLists = new ArrayList>();
	private Solver subSolver = null;
	private boolean isChildExists = false;
	private boolean isChildForall = false;
	private boolean isChildNot = false;

	private OperatorDescr.Type type;

	protected Solver(OperatorDescr.Type type) {
		this.type = type;
	}

	public void addOperator(OperatorDescr.Type type) {
		if (subSolver != null) {
			subSolver.addOperator(type);
		} else {
			subSolver = new Solver(type);
		}
	}

	/**
	 * Add new descr.
	 * 
	 * @param descr
	 */
	public void add(VerifierComponent descr) {

		if (descr instanceof OperatorDescr) {
			throw new UnsupportedOperationException(
					"Operator descrs are not supported.");
		}

		if (subSolver != null) {
			subSolver.add(descr);
		} else {
			if (type == OperatorDescr.Type.AND) {
				if (possibilityLists.isEmpty()) {
					possibilityLists.add(new HashSet());
				}
				for (Set set : possibilityLists) {
					set.add(descr);
				}
			} else if (type == OperatorDescr.Type.OR) {
				Set set = new HashSet();
				set.add(descr);
				possibilityLists.add(set);
			}
		}
	}

	/**
	 * Ends subSolvers data collection.
	 * 
	 */
	protected void end() {
		if (subSolver != null && subSolver.subSolver == null) {
			if (type == OperatorDescr.Type.AND) {
				if (possibilityLists.isEmpty()) {
					possibilityLists.add(new HashSet());
				}

				List> newPossibilities = new ArrayList>();

				List> sets = subSolver
						.getPossibilityLists();
				for (Set possibilityList : possibilityLists) {

					for (Set set : sets) {
						Set newSet = new HashSet();
						newSet.addAll(possibilityList);
						newSet.addAll(set);
						newPossibilities.add(newSet);
					}
				}
				possibilityLists = newPossibilities;

			} else if (type == OperatorDescr.Type.OR) {

				possibilityLists.addAll(subSolver.getPossibilityLists());

			}

			subSolver = null;

		} else if (subSolver != null && subSolver.subSolver != null) {

			subSolver.end();
		}

	}

	public void setChildForall(boolean b) {
		if (subSolver != null) {
			subSolver.setChildForall(b);
		} else {
			isChildForall = b;
		}
	}

	public void setChildExists(boolean b) {
		if (subSolver != null) {
			subSolver.setChildExists(b);
		} else {
			isChildExists = b;
		}
	}

	public void setChildNot(boolean b) {
		if (subSolver != null) {
			subSolver.setChildNot(b);
		} else {
			isChildNot = b;
		}
	}

	public boolean isForall() {
		if (subSolver != null) {
			return subSolver.isForall();
		} else {
			return isChildForall;
		}
	}

	public boolean isExists() {
		if (subSolver != null) {
			return subSolver.isExists();
		} else {
			return isChildExists;
		}
	}

	public boolean isChildNot() {
		if (subSolver != null) {
			return subSolver.isChildNot();
		} else {
			return isChildNot;
		}
	}

	public List> getPossibilityLists() {
		return possibilityLists;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy