private IVmdSceneGenerator getGenerator(Dependency dependency) {
   if (dependency.getType().equals(LunaticConstants.EGD)
       || dependency.getType().equals(LunaticConstants.ExtEGD)) {
     return new VmdEgdSceneGenerator(dependency);
   }
   return new VmdTgdDepSceneGenerator(dependency);
 }
 public static Dependency findDependency(String dependencyId, List<Dependency> dependencies) {
   for (Dependency dependency : dependencies) {
     if (dependency.getId().equalsIgnoreCase(dependencyId)) {
       return dependency;
     }
   }
   throw new IllegalArgumentException(
       "Unable to find dependency with id " + dependencyId + " in " + dependencies);
 }
 public static List<AttributeRef> extractRequestedAttributes(Dependency dependency) {
   List<AttributeRef> result = new ArrayList<AttributeRef>();
   result.addAll(dependency.getQueriedAttributes());
   for (AttributeRef attributeRef : dependency.getAdditionalAttributes()) {
     if (!result.contains(attributeRef)) {
       result.add(attributeRef);
     }
   }
   return result;
 }
 private void addAdditionalAttributes(
     CellGroup cellGroup, TupleOID originalOIDForConclusionValue, Tuple tuple, Dependency egd) {
   for (AttributeRef additionalAttribute : egd.getAdditionalAttributes()) {
     for (Cell additionalCell : tuple.getCells()) {
       AttributeRef unaliasedAttribute = ChaseUtility.unAlias(additionalCell.getAttributeRef());
       if (!unaliasedAttribute.equals(additionalAttribute)) {
         continue;
       }
       TupleOID originalOIDForCell =
           new TupleOID(ChaseUtility.getOriginalOid(tuple, additionalCell.getAttributeRef()));
       if (!originalOIDForCell.equals(originalOIDForConclusionValue)) {
         continue;
       }
       CellGroupCell additionalCellGroupCell =
           new CellGroupCell(
               originalOIDForCell,
               unaliasedAttribute,
               additionalCell.getValue(),
               null,
               LunaticConstants.TYPE_ADDITIONAL,
               null);
       cellGroup.addAdditionalCell(additionalAttribute, additionalCellGroupCell);
     }
   }
 }
 public static List<AttributeRef> findTargetQueriedAttributesForExtTGD(Dependency dependency) {
   List<AttributeRef> queriedAttributes = new ArrayList<AttributeRef>();
   LunaticUtility.addAllIfNotContained(
       queriedAttributes, findTargetQueriedAttributesInPremise(dependency));
   for (FormulaVariable formulaVariable : dependency.getPremise().getLocalVariables()) {
     if (formulaVariable.getConclusionRelationalOccurrences().isEmpty()) {
       continue;
     }
     AttributeRef firstPremiseOccurrenceInTarget =
         getFirstPremiseOccurrenceInTarget(formulaVariable.getPremiseRelationalOccurrences());
     if (firstPremiseOccurrenceInTarget != null) {
       AttributeRef unaliasedPremiseAttribute =
           ChaseUtility.unAlias(firstPremiseOccurrenceInTarget);
       LunaticUtility.addIfNotContained(queriedAttributes, unaliasedPremiseAttribute);
     }
     for (FormulaVariableOccurrence conclusionOccurrence :
         formulaVariable.getConclusionRelationalOccurrences()) {
       AttributeRef attribute = conclusionOccurrence.getAttributeRef();
       if (attribute.getTableAlias().isSource()) {
         continue;
       }
       AttributeRef unaliasedConclusionAttribute =
           ChaseUtility.unAlias(conclusionOccurrence.getAttributeRef());
       LunaticUtility.addIfNotContained(queriedAttributes, unaliasedConclusionAttribute);
     }
   }
   return queriedAttributes;
 }
 public static List<FormulaVariable> findUniversalVariablesInConclusion(Dependency dependency) {
   List<FormulaVariable> result = new ArrayList<FormulaVariable>();
   for (FormulaVariable formulaVariable : dependency.getPremise().getLocalVariables()) {
     if (formulaVariable.getConclusionRelationalOccurrences().size() > 0) {
       result.add(formulaVariable);
     }
   }
   return result;
 }
 private EquivalenceClassForSymmetricEGD createEquivalenceClass(Dependency egd) {
   FormulaVariable v1 =
       ((ComparisonAtom) egd.getConclusion().getAtoms().get(0)).getVariables().get(0);
   AttributeRef variableOccurrence = v1.getPremiseRelationalOccurrences().get(0).getAttributeRef();
   AttributeRef conclusionAttribute =
       EquivalenceClassUtility.correctAttributeForSymmetricEGDs(variableOccurrence, egd);
   List<BackwardAttribute> attributesForBackwardChasing = findAttributesForBackwardChasing(egd);
   return new EquivalenceClassForSymmetricEGD(
       egd, conclusionAttribute, attributesForBackwardChasing);
 }
 private NewChaseSteps applyRepairs(
     DeltaChaseStep currentNode, List<Repair> repairs, Dependency egd, Scenario scenario) {
   if (logger.isDebugEnabled()) logger.debug("---Applying repairs...");
   NewChaseSteps newChaseSteps = new NewChaseSteps(egd);
   for (int i = 0; i < repairs.size(); i++) {
     Repair repair = repairs.get(i);
     boolean consistentRepair =
         purgeOverlappingContexts(egd, repair, currentNode.getAffectedAttributesInAncestors());
     try {
       CellGroupUtility.checkCellGroupConsistency(repair);
     } catch (ChaseException ex) { // TODO++
       IExportSolution solutionExporter =
           OperatorFactory.getInstance().getSolutionExporter(scenario);
       solutionExporter.export(currentNode, "error", scenario);
       throw ex;
     }
     String egdId = egd.getId();
     String localId = ChaseUtility.generateChaseStepIdForEGDs(egdId, i, repair);
     DeltaChaseStep newStep =
         new DeltaChaseStep(scenario, currentNode, localId, egd, repair, repair.getChaseModes());
     for (ChangeDescription changeSet : repair.getChangeDescriptions()) {
       this.cellChanger.changeCells(
           changeSet.getCellGroup(), newStep.getDeltaDB(), newStep.getId(), scenario);
     }
     if (consistentRepair) {
       if (logger.isDebugEnabled())
         logger.debug("EGD " + egd.getId() + " is satisfied in this step...");
       newStep.addSatisfiedEGD(egd);
     }
     newStep.setAffectedAttributesInNode(ChaseUtility.extractAffectedAttributes(repair));
     newStep.setAffectedAttributesInAncestors(
         ChaseUtility.findChangedAttributesInAncestors(newStep));
     if (logger.isDebugEnabled())
       logger.debug("Generated step " + newStep.getId() + " for repair: " + repair);
     newChaseSteps.addChaseStep(newStep);
   }
   this.cellChanger.flush(currentNode.getDeltaDB());
   if (repairs.isEmpty()) {
     newChaseSteps.setNoRepairsNeeded(true);
   }
   return newChaseSteps;
 }
 public static List<FormulaVariable> getUniversalVariablesInConclusion(Dependency dependency) {
   List<FormulaVariable> result = new ArrayList<FormulaVariable>();
   for (FormulaVariable formulaVariable : dependency.getPremise().getLocalVariables()) {
     List<FormulaVariableOccurrence> variablesOccurrence =
         formulaVariable.getConclusionRelationalOccurrences();
     if (!variablesOccurrence.isEmpty()) {
       LunaticUtility.addIfNotContained(result, formulaVariable);
     }
   }
   return result;
 }
 public static List<AttributeRef> extractRequestedAttributesWithExistential(
     Dependency dependency) {
   List<AttributeRef> result = new ArrayList<AttributeRef>();
   result.addAll(dependency.getQueriedAttributes());
   for (FormulaVariable variable : dependency.getConclusion().getLocalVariables()) {
     if (variable.getConclusionRelationalOccurrences().size() == 1) {
       continue;
     }
     for (FormulaVariableOccurrence occurrence : variable.getConclusionRelationalOccurrences()) {
       if (logger.isTraceEnabled()) logger.trace("Inspecting occurrence: " + occurrence);
       AttributeRef attribute = occurrence.getAttributeRef();
       AttributeRef unaliasedAttribute = ChaseUtility.unAlias(attribute);
       LunaticUtility.addIfNotContained(result, unaliasedAttribute);
     }
   }
   for (AttributeRef attributeRef : dependency.getAdditionalAttributes()) {
     if (!result.contains(attributeRef)) {
       result.add(attributeRef);
     }
   }
   return result;
 }
 public static List<AttributeRef> getFirstAttributesOfUniversalVariablesInConclusion(
     Dependency dependency) {
   List<AttributeRef> result = new ArrayList<AttributeRef>();
   List<FormulaVariable> universalVariable =
       DependencyUtility.getUniversalVariablesInConclusion(dependency);
   for (FormulaVariable formulaVariable : universalVariable) {
     AttributeRef firstAttribute =
         DependencyUtility.findFirstOccurrenceInFormula(
             dependency.getPremise(), formulaVariable.getPremiseRelationalOccurrences());
     //            LunaticUtility.addIfNotContained(result, firstAttribute);
     result.add(firstAttribute);
   }
   return result;
 }
 private List<BackwardAttribute> findAttributesForBackwardChasing(Dependency egd) {
   List<BackwardAttribute> attributesForBackwardChasing = new ArrayList<BackwardAttribute>();
   for (FormulaVariableOccurrence backwardAttributeOccurrence : egd.getBackwardAttributes()) {
     AttributeRef occurrenceAttribute =
         EquivalenceClassUtility.correctAttributeForSymmetricEGDs(
             backwardAttributeOccurrence.getAttributeRef(), egd);
     FormulaVariable variable =
         LunaticUtility.findPremiseVariableInDepedency(backwardAttributeOccurrence, egd);
     BackwardAttribute backwardAttribute = new BackwardAttribute(occurrenceAttribute, variable);
     if (attributesForBackwardChasing.contains(backwardAttribute)) {
       continue;
     }
     attributesForBackwardChasing.add(backwardAttribute);
   }
   return attributesForBackwardChasing;
 }
 public static List<String> findSourceAtoms(Dependency egd, Scenario scenario) {
   List<String> result = new ArrayList<String>();
   if (!DependencyUtility.hasSourceSymbols(egd)) {
     return result;
   }
   for (IFormulaAtom atom : egd.getPremise().getPositiveFormula().getAtoms()) {
     if (!(atom instanceof RelationalAtom)) {
       continue;
     }
     RelationalAtom relationalAtom = (RelationalAtom) atom;
     if (!relationalAtom.isSource()) {
       continue;
     }
     result.add(relationalAtom.getTableName());
   }
   return result;
 }
 public static List<AttributeRef> findTargetJoinAttributesInPositiveFormula(
     Dependency dependency) {
   List<VariableEquivalenceClass> relevantVariableClasses =
       ChaseUtility.findJoinVariablesInTarget(dependency);
   List<AttributeRef> targetJoinAttributes = new ArrayList<AttributeRef>();
   for (VariableEquivalenceClass variableEquivalenceClass : relevantVariableClasses) {
     List<FormulaVariableOccurrence> targetOccurrencesForEquivalenceClass =
         ChaseUtility.findTargetOccurrences(variableEquivalenceClass);
     List<FormulaVariableOccurrence> positiveTargetOccurrencesForEquivalenceClass =
         ChaseUtility.findPositiveOccurrences(
             dependency.getPremise().getPositiveFormula(), targetOccurrencesForEquivalenceClass);
     for (FormulaVariableOccurrence occurrence : positiveTargetOccurrencesForEquivalenceClass) {
       targetJoinAttributes.add(occurrence.getAttributeRef());
     }
   }
   return targetJoinAttributes;
 }
 public static List<AttributeRef> findTargetQueriedAttributesInPremise(Dependency dependency) {
   if (logger.isTraceEnabled())
     logger.trace("Searching query attributes for dependency: \n" + dependency);
   List<AttributeRef> queriedAttributes = new ArrayList<AttributeRef>();
   for (FormulaVariable variable : dependency.getPremise().getLocalVariables()) {
     if (logger.isTraceEnabled()) logger.trace("Inspecting variable: " + variable);
     if (hasSingleOccurrenceInPremise(variable)) {
       continue;
     }
     for (FormulaVariableOccurrence occurrence : variable.getPremiseRelationalOccurrences()) {
       if (logger.isTraceEnabled()) logger.trace("Inspecting occurrence: " + occurrence);
       AttributeRef attribute = occurrence.getAttributeRef();
       if (attribute.getTableAlias().isSource()) {
         continue;
       }
       AttributeRef unaliasedAttribute = ChaseUtility.unAlias(attribute);
       LunaticUtility.addIfNotContained(queriedAttributes, unaliasedAttribute);
     }
   }
   if (logger.isTraceEnabled()) logger.trace("Result: " + queriedAttributes);
   return queriedAttributes;
 }
 public static List<FormulaVariable> getExistentialVariables(Dependency dependency) {
   return dependency.getConclusion().getLocalVariables();
 }
 public static String buildTableNameForConstants(Dependency dependency, boolean premise) {
   String suffix = (premise ? "prem" : "conc");
   return "c_in_" + dependency.getId() + "_" + suffix;
 }