private FormulaVariable findUniversalVariableForTargetPath(
     VariablePathExpression attributePath, List<FormulaVariable> universalFormulaVariables) {
   for (FormulaVariable formulaVariable : universalFormulaVariables) {
     if (SpicyEngineUtility.containsPathWithSameVariableId(
         formulaVariable.getTargetOccurrencePaths(), attributePath)) {
       return formulaVariable;
     }
   }
   return null;
 }
 private FormulaVariable findExistentialVariable(
     VariablePathExpression attributePath, List<FormulaVariable> existentialFormulaVariables) {
   for (FormulaVariable formulaVariable : existentialFormulaVariables) {
     if (SpicyEngineUtility.containsPathWithSameVariableId(
         formulaVariable.getTargetOccurrencePaths(), attributePath)) {
       return formulaVariable;
     }
   }
   throw new IllegalArgumentException(
       "Unable to find variable for path "
           + attributePath
           + " in "
           + SpicyEngineUtility.printVariableList(existentialFormulaVariables));
 }
 ///////////////////////////    SKOLEMS   ///////////////////////////////
 private String printSkolems(FORule rule, MappingTask mappingTask) {
   StringBuilder result = new StringBuilder();
   List<FormulaVariable> existentialVariables = rule.getExistentialFormulaVariables(mappingTask);
   for (FormulaVariable variable : existentialVariables) {
     String generatorString = findGeneratorForVariable(variable, rule, mappingTask);
     result.append(variable.toShortString()).append(": ").append(generatorString).append("\n");
   }
   if (mappingTask.getTargetProxy().isNested()) {
     for (SetAlias alias : rule.getTargetView().getGenerators()) {
       VariablePathExpression bindingPath = alias.getBindingPathExpression();
       String generatorString = findGeneratorForPath(bindingPath, rule, mappingTask);
       result.append(alias.toShortString()).append(": ").append(generatorString).append("\n");
     }
   }
   return result.toString();
 }
 private FormulaVariable findVariableForPath(
     VariablePathExpression attributePath, List<List<FormulaVariable>> variables) {
   //        for (int i = variables.size() - 1; i >= 0; i--) {
   for (int i = 0; i < variables.size(); i++) {
     List<FormulaVariable> variableList = variables.get(i);
     for (FormulaVariable formulaVariable : variableList) {
       if (SpicyEngineUtility.containsPathWithSameVariableId(
           formulaVariable.getOriginalSourceOccurrencePaths(), attributePath)) {
         return formulaVariable;
       }
     }
     for (FormulaVariable formulaVariable : variableList) {
       if (SpicyEngineUtility.containsPathWithSameVariableId(
           formulaVariable.getSourceOccurrencePaths(), attributePath)) {
         return formulaVariable;
       }
     }
   }
   throw new IllegalArgumentException(
       "Unable to find variable for path "
           + attributePath
           + " in "
           + SpicyEngineUtility.printListOfVariableLists(variables));
 }
 private String generateConjunctionString(
     List<SetAlias> setVariables, List<List<FormulaVariable>> variables, INode schema) {
   StringBuilder localResult = new StringBuilder();
   List<SetAlias> sortedAliases = new ArrayList<SetAlias>(setVariables);
   Collections.sort(sortedAliases);
   for (int i = 0; i < sortedAliases.size(); i++) {
     SetAlias sourceVariable = sortedAliases.get(i);
     localResult.append(generateIndent());
     if (!useSaveFormat) {
       localResult.append(
           utility.printAtomName(sourceVariable, mappingTask.getSourceProxy().isNested()));
     } else {
       localResult.append(
           utility.printAtomNameForSaveFormat(
               sourceVariable, mappingTask.getSourceProxy().isNested()));
     }
     List<VariablePathExpression> attributePaths = sourceVariable.getFirstLevelAttributes(schema);
     for (int j = 0; j < attributePaths.size(); j++) {
       VariablePathExpression attributePath = attributePaths.get(j);
       FormulaVariable attributeVariable = findVariableForPath(attributePath, variables);
       localResult.append(attributePath.getLastStep()).append(": ");
       if (useSaveFormat) {
         localResult.append("$");
       }
       localResult.append(attributeVariable.toShortString());
       if (j != attributePaths.size() - 1) {
         localResult.append(", ");
       }
     }
     localResult.append(")");
     if (i != sortedAliases.size() - 1) {
       localResult.append(", \n");
     }
   }
   return localResult.toString();
 }
 private String findGeneratorForVariable(
     FormulaVariable variable, FORule rule, MappingTask mappingTask) {
   List<FormulaVariable> universalVariables =
       rule.getUniversalFormulaVariablesInTarget(mappingTask);
   TGDGeneratorsMap generatorsMap = rule.getGenerators(mappingTask);
   for (VariablePathExpression targetPath : variable.getTargetOccurrencePaths()) {
     IValueGenerator generator =
         generatorsMap.getGeneratorForLeaf(
             targetPath, mappingTask.getTargetProxy().getIntermediateSchema());
     if (generator != null && generator instanceof SkolemFunctionGenerator) {
       SkolemFunctionGenerator skolemGenerator = (SkolemFunctionGenerator) generator;
       return skolemGenerator.toStringWithVariableArguments(universalVariables);
     }
   }
   return NullValueGenerator.getInstance().toString();
   // throw new IllegalArgumentException("Unable to find generator for variable: " +
   // variable.toLongString() + "\nin rule: " + rule + "\n" +
   // SpicyEngineUtility.printVariableList(rule.getExistentialFormulaVariables(mappingTask)) +
   // "\nGenerators: " + generatorsMap);
 }
  ////////////////////////////////   CONCLUSION   ////////////////////////////////////////////////
  public String conclusionString(FORule tgd, MappingTask mappingTask, String indent) {
    // giannisk
    // find target paths that have a constant value correspondence
    List<VariablePathExpression> constantTargetPaths = new ArrayList<VariablePathExpression>();
    HashMap<VariablePathExpression, ISourceValue> constantValueMap =
        new HashMap<VariablePathExpression, ISourceValue>();
    for (VariableCorrespondence varCor : tgd.getCoveredCorrespondences()) {
      if (varCor.isConstant()) {
        constantTargetPaths.add(varCor.getTargetPath());
        constantValueMap.put(varCor.getTargetPath(), varCor.getSourceValue());
      }
    }

    StringBuilder result = new StringBuilder();
    List<FormulaVariable> universalVariables = tgd.getUniversalFormulaVariables(mappingTask);
    List<FormulaVariable> existentialVariables = tgd.getExistentialFormulaVariables(mappingTask);
    if (!useSaveFormat
        && (!existentialVariables.isEmpty() || mappingTask.getTargetProxy().isNested())) {
      result.append("exist ");
      if (mappingTask.getTargetProxy().isNested()) {
        utility.printOIDVariables(tgd.getTargetView());
      }
      if (!existentialVariables.isEmpty()) {
        result.append(utility.printVariables(existentialVariables));
      }
    }
    result.append("\n");
    for (int i = 0; i < tgd.getTargetView().getGenerators().size(); i++) {
      SetAlias targetVariable = tgd.getTargetView().getGenerators().get(i);
      result.append(indent).append(utility.INDENT);
      if (!useSaveFormat) {
        result.append(
            utility.printAtomName(targetVariable, mappingTask.getTargetProxy().isNested()));
      } else {
        result.append(
            utility.printAtomNameForSaveFormat(
                targetVariable, mappingTask.getTargetProxy().isNested()));
      }
      List<VariablePathExpression> attributePaths =
          targetVariable.getFirstLevelAttributes(
              mappingTask.getTargetProxy().getIntermediateSchema());
      for (int j = 0; j < attributePaths.size(); j++) {
        VariablePathExpression attributePath = attributePaths.get(j);
        result.append(attributePath.getLastStep()).append(": ");
        FormulaVariable attributeVariable =
            findUniversalVariableForTargetPath(attributePath, universalVariables);
        Expression transformation = null;
        if (!constantTargetPaths.contains(attributePath)) {
          if (attributeVariable == null) {
            attributeVariable =
                findExistentialVariable(
                    attributePath, tgd.getExistentialFormulaVariables(mappingTask));
          }
          transformation = attributeVariable.getTransformationFunction(attributePath);
        }
        if (transformation == null) {
          if (!constantTargetPaths.contains(attributePath)) {
            if (useSaveFormat) {
              result.append("$");
            }
            result.append(attributeVariable.toShortString());
          } else { // constant
            result.append(constantValueMap.get(attributePath));
          }
        } else { // function
          List<List<FormulaVariable>> variables = new ArrayList<List<FormulaVariable>>();
          variables.add(tgd.getUniversalFormulaVariables(mappingTask));
          utility.setVariableDescriptions(transformation, variables);
          result.append(transformation);
        }
        if (j != attributePaths.size() - 1) {
          result.append(", ");
        }
      }
      result.append(")");
      if (i != tgd.getTargetView().getGenerators().size() - 1) {
        result.append(", \n");
      }
    }
    return result.toString();
  }