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

org.drools.verifier.dao.VerifierDataMaps Maven / Gradle / Ivy

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

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.drools.verifier.components.Consequence;
import org.drools.verifier.components.Constraint;
import org.drools.verifier.components.Field;
import org.drools.verifier.components.FieldObjectTypeLink;
import org.drools.verifier.components.ObjectType;
import org.drools.verifier.components.OperatorDescr;
import org.drools.verifier.components.Pattern;
import org.drools.verifier.components.PatternPossibility;
import org.drools.verifier.components.Restriction;
import org.drools.verifier.components.RulePackage;
import org.drools.verifier.components.RulePossibility;
import org.drools.verifier.components.Variable;
import org.drools.verifier.components.VerifierEvalDescr;
import org.drools.verifier.components.VerifierPredicateDescr;
import org.drools.verifier.components.VerifierRule;

/**
 * 
 * @author Toni Rikkola
 */
class VerifierDataMaps implements VerifierData {

	private Map packagesById = new TreeMap();
	private Map packagesByName = new TreeMap();

	private Map objectTypesById = new TreeMap();
	private Map objectTypesByName = new TreeMap();
	private Map fieldsByObjectTypeAndFieldName = new TreeMap();
	private Map fieldsById = new TreeMap();
	private DataTree fieldsByObjectTypeId = new DataTree();
	private Map fieldObjectTypeLinkByIds = new TreeMap();

	private Map rulesById = new TreeMap();
	private Map patternsById = new TreeMap();
	private DataTree patternsByObjectTypeId = new DataTree();
	private DataTree patternsByRuleName = new DataTree();
	private Map constraintsById = new TreeMap();
	private Map restrictionsById = new TreeMap();
	private DataTree restrictionsByFieldId = new DataTree();
	private Map operatorsById = new TreeMap();
	private Map evalsById = new TreeMap();
	private Map predicatesById = new TreeMap();
	private Map consiquencesById = new TreeMap();

	private Map variablesByRuleAndVariableName = new TreeMap();

	private Map patternPossibilitiesById = new TreeMap();
	private Map rulePossibilitiesById = new TreeMap();

	public void add(ObjectType objectType) {
		objectTypesById.put(Integer.valueOf(objectType.getId()), objectType);
		objectTypesByName.put(objectType.getName(), objectType);
	}

	public void add(Field field) {
		ObjectType objectType = objectTypesById.get(Integer.valueOf(field
				.getObjectTypeId()));
		fieldsByObjectTypeAndFieldName.put(objectType.getName() + "."
				+ field.getName(), field);

		fieldsById.put(field.getId(), field);

		fieldsByObjectTypeId.put(field.getObjectTypeId(), field);
	}

	public void add(Variable variable) {
		variablesByRuleAndVariableName.put(variable.getRuleName() + "."
				+ variable.getName(), variable);
	}

	public void add(VerifierRule rule) {
		rulesById.put(Integer.valueOf(rule.getId()), rule);
	}

	public void add(Pattern pattern) {
		patternsById.put(Integer.valueOf(pattern.getId()), pattern);

		patternsByObjectTypeId.put(pattern.getObjectTypeId(), pattern);
		patternsByRuleName.put(pattern.getRuleName(), pattern);
	}

	public void add(Constraint constraint) {
		constraintsById.put(Integer.valueOf(constraint.getId()), constraint);
	}

	public void add(Restriction restriction) {
		restrictionsById.put(restriction.getId(), restriction);

		restrictionsByFieldId.put(restriction.getFieldId(), restriction);
	}

	public void add(FieldObjectTypeLink link) {
		fieldObjectTypeLinkByIds.put(link.getFieldId() + "."
				+ link.getObjectTypeId(), link);
	}

	public ObjectType getObjectTypeByName(String name) {
		return objectTypesByName.get(name);
	}

	public Field getFieldByObjectTypeAndFieldName(String objectTypeName,
			String fieldName) {
		return fieldsByObjectTypeAndFieldName.get(objectTypeName + "."
				+ fieldName);
	}

	public Variable getVariableByRuleAndVariableName(String ruleName,
			String variableName) {
		return variablesByRuleAndVariableName
				.get(ruleName + "." + variableName);
	}

	public FieldObjectTypeLink getFieldObjectTypeLink(int id, int id2) {
		return fieldObjectTypeLinkByIds.get(id + "." + id2);
	}

	public Collection getAllRules() {
		return rulesById.values();
	}

	public void add(PatternPossibility possibility) {
		patternPossibilitiesById.put(possibility.getId(), possibility);
	}

	public void add(RulePossibility possibility) {
		rulePossibilitiesById.put(possibility.getId(), possibility);
	}

	public Collection getObjectTypesByRuleName(String ruleName) {
		Set set = new HashSet();

		for (Pattern pattern : patternsByRuleName.getBranch(ruleName)) {
			ObjectType objectType = getObjectTypeById(pattern.getObjectTypeId());
			set.add(objectType);
		}

		return set;
	}

	public ObjectType getObjectTypeById(int id) {
		return objectTypesById.get(id);
	}

	public Collection getAll() {
		List objects = new ArrayList();

		objects.addAll(packagesById.values());

		objects.addAll(rulesById.values());
		objects.addAll(patternsById.values());
		objects.addAll(constraintsById.values());
		objects.addAll(restrictionsById.values());
		objects.addAll(operatorsById.values());
		objects.addAll(evalsById.values());
		objects.addAll(predicatesById.values());
		objects.addAll(consiquencesById.values());

		objects.addAll(patternPossibilitiesById.values());
		objects.addAll(rulePossibilitiesById.values());

		objects.addAll(objectTypesByName.values());
		objects.addAll(fieldsByObjectTypeAndFieldName.values());
		objects.addAll(variablesByRuleAndVariableName.values());

		return objects;
	}

	public Collection getAllObjectTypes() {
		return objectTypesById.values();
	}

	public Collection getFieldsByObjectTypeId(int id) {
		return fieldsByObjectTypeId.getBranch(id);
	}

	public Collection getRulesByObjectTypeId(int id) {
		Set rules = new HashSet();

		for (Pattern pattern : patternsByObjectTypeId.getBranch(id)) {
			rules.add(rulesById.get(pattern.getRuleId()));
		}

		return rules;
	}

	public Collection getAllFields() {
		return fieldsById.values();
	}

	public Collection getRulesByFieldId(int id) {

		Set rules = new HashSet();

		for (Restriction restriction : restrictionsByFieldId.getBranch(id)) {
			rules.add(rulesById.get(restriction.getRuleId()));
		}

		return rules;
	}

	public Collection getAllRulePackages() {
		return packagesById.values();
	}

	public void add(RulePackage rulePackage) {
		packagesById.put(rulePackage.getId(), rulePackage);
		packagesByName.put(rulePackage.getName(), rulePackage);
	}

	public RulePackage getRulePackageByName(String name) {
		return packagesByName.get(name);
	}

	public Collection getRestrictionsByFieldId(int id) {
		return restrictionsByFieldId.getBranch(id);
	}

	public void add(OperatorDescr operatorDescr) {
		operatorsById.put(operatorDescr.getId(), operatorDescr);
	}

	public void add(VerifierEvalDescr eval) {
		evalsById.put(eval.getId(), eval);
	}

	public void add(VerifierPredicateDescr predicate) {
		predicatesById.put(predicate.getId(), predicate);
	}

	public void add(Consequence consequence) {
		consiquencesById.put(consequence.getId(), consequence);
	}
}