public static String projectionOnTargetValuesFromCorrespondences( List<VariableCorrespondence> correspondences, String fromClause, String whereClause) { StringBuilder result = new StringBuilder(); result.append(INDENT).append("select distinct \n"); for (int i = 0; i < correspondences.size(); i++) { VariableCorrespondence correspondence = correspondences.get(i); if (correspondence.getSourcePaths() != null) { result .append(DOUBLE_INDENT) .append(GenerateSQL.attributeNameWithVariable(correspondence.getFirstSourcePath())); } else { String sourcePathName = correspondence.getSourceValue().toString(); sourcePathName = sourcePathName.replaceAll("\"", "\'"); result.append(DOUBLE_INDENT).append(sourcePathName); } result .append(" as ") .append(GenerateSQL.attributeNameInVariable(correspondence.getFirstSourcePath())); if (i != correspondences.size() - 1) result.append(",\n"); } result.append("\n"); result.append(INDENT).append(fromClause); if (!whereClause.equals("")) { result.append(INDENT).append(whereClause); } return result.toString(); }
private static List<VariablePathExpression> generateTargetPaths( List<VariableCorrespondence> correspondences) { List<VariablePathExpression> result = new ArrayList<VariablePathExpression>(); for (VariableCorrespondence correspondence : correspondences) { result.add(correspondence.getTargetPath()); } return result; }
private static VariablePathExpression findSourcePathWithEqualsId( List<VariableCorrespondence> correspondences, VariablePathExpression targetPath) { for (VariableCorrespondence variableCorrespondence : correspondences) { if (variableCorrespondence.getTargetPath().equalsAndHasSameVariableId(targetPath)) { return variableCorrespondence.getFirstSourcePath(); } } return null; }
private static String generateWhereClauseFromEqualities( NegatedComplexQuery negation, String positiveViewName, String negationViewName) { StringBuilder result = new StringBuilder(); result.append("where\n"); if (negation.isTargetDifference()) { for (int i = 0; i < negation.getTargetEqualities().getLeftCorrespondences().size(); i++) { VariableCorrespondence leftCorrespondence = negation.getTargetEqualities().getLeftCorrespondences().get(i); VariableCorrespondence rightCorrespondence = negation.getTargetEqualities().getRightCorrespondences().get(i); VariablePathExpression leftPath = leftCorrespondence.getFirstSourcePath(); VariablePathExpression rightPath = rightCorrespondence.getFirstSourcePath(); result .append(DOUBLE_INDENT) .append(positiveViewName) .append(".") .append(attributeNameInVariable(leftPath)); result.append(" = "); result.append(negationViewName).append(".").append(attributeNameInVariable(rightPath)); if (i != negation.getTargetEqualities().getLeftCorrespondences().size() - 1) result.append(" AND\n"); } } else { for (int i = 0; i < negation.getSourceEqualities().getLeftPaths().size(); i++) { VariablePathExpression leftPath = negation.getSourceEqualities().getLeftPaths().get(i); VariablePathExpression rightPath = negation.getSourceEqualities().getRightPaths().get(i); result .append(DOUBLE_INDENT) .append(positiveViewName) .append(".") .append(attributeNameInVariable(leftPath)); result.append(" = "); result.append(negationViewName).append(".").append(attributeNameInVariable(rightPath)); if (i != negation.getSourceEqualities().getLeftPaths().size() - 1) result.append(" AND\n"); } } result.append("\n"); return result.toString(); }
//////////////////////////////// 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(); }