예제 #1
0
 public boolean isEquivalent(Formula formula1, Formula formula2) {
   if (formula1.equals(formula2)) return true;
   if (hasOpposite(formula1)) {
     Formula equiv1 = equivalentFormula(formula1);
     return equiv1.equals(formula2);
   }
   return false;
 }
예제 #2
0
 private boolean isLegalCvt(Formula inner, Formula outer) {
   if (FreebaseInfo.isReverseProperty(inner.toString())
       && !FreebaseInfo.isReverseProperty(outer.toString())) return false;
   if (!FreebaseInfo.isReverseProperty(inner.toString())
       && FreebaseInfo.isReverseProperty(outer.toString())) return false;
   // if (isEquivalent(reverseFormula(outer), inner)) //don't expand if reverse is equivalent
   // return false;
   return true;
 }
예제 #3
0
 public void validate(Formula formula) {
   final Formula resolved = (Formula) resolvedNodes.get(formula);
   if (resolved != null) {
     return; // already resolved
   }
   try {
     stack.push(formula);
     resolvedNodes.put(formula, placeHolder);
     formula.accept(this);
     resolvedNodes.put(formula, formula);
   } finally {
     stack.pop();
   }
 }
예제 #4
0
 public void writeFormulas(FormulaResolver formulaResolver) {
   Set sheetNames = sheetFormulasMap.keySet();
   for (Iterator iterator = sheetNames.iterator(); iterator.hasNext(); ) {
     String sheetName = (String) iterator.next();
     List formulas = (List) sheetFormulasMap.get(sheetName);
     for (int i = 0; i < formulas.size(); i++) {
       Formula formula = (Formula) formulas.get(i);
       String formulaString = formulaResolver.resolve(formula, null);
       if (formulaString != null) {
         Cell hssfCell =
             Util.getOrCreateCell(
                 formula.getSheet().getPoiSheet(), formula.getRowNum(), formula.getCellNum());
         try {
           hssfCell.setCellFormula(formulaString);
         } catch (RuntimeException e) {
           log.error("Can't set formula: " + formulaString, e);
           //                        hssfCell.setCellType( Cell.CELL_TYPE_BLANK );
           throw new RuntimeException("Can't set formula: " + formulaString, e);
         }
       }
     }
   }
 }
예제 #5
0
 public String toString() {
   return formula.toString()
       + "\t"
       + popularity
       + "\t"
       + expectedType1
       + "\t"
       + expectedType2
       + "\t"
       + unitId
       + "\t"
       + unitDesc
       + "\t"
       + Joiner.on("###").join(descriptions);
 }
예제 #6
0
 /** supports chains only */
 public boolean hasOpposite(Formula formula) {
   LispTree tree = formula.toLispTree();
   if (tree.isLeaf()) {
     String fbProperty =
         FreebaseInfo.isReverseProperty(tree.value) ? tree.value.substring(1) : tree.value;
     return freebaseInfo.fbPropertyHasOpposite(fbProperty);
   } else {
     // Un-reverse everything.
     String binary1 = tree.child(2).child(0).value;
     binary1 = FreebaseInfo.isReverseProperty(binary1) ? binary1.substring(1) : binary1;
     String binary2 = tree.child(2).child(1).child(0).value;
     binary2 = FreebaseInfo.isReverseProperty(binary2) ? binary2.substring(1) : binary2;
     return freebaseInfo.fbPropertyHasOpposite(binary1)
         && freebaseInfo.fbPropertyHasOpposite(binary2);
   }
 }
예제 #7
0
  public Formula equivalentFormula(Formula formula) {

    LispTree tree = formula.toLispTree();
    return equivalentFormula(tree);
  }
예제 #8
0
 /** supports chains only */
 public boolean isReversed(Formula formula) {
   LispTree tree = formula.toLispTree();
   if (tree.isLeaf()) return FreebaseInfo.isReverseProperty(tree.value);
   else return FreebaseInfo.isReverseProperty(tree.child(2).child(0).value);
 }
예제 #9
0
 public void updateWorkbookFormulas(BlockTransformation transformation) {
   Set sheetNames = sheetFormulasMap.keySet();
   Formula formula, newFormula;
   Set cellRefs, newCellRefs;
   CellRef cellRef, newCellRef;
   List resultCells;
   String newCell;
   Point point, newPoint;
   Set cellRefsToRemove = new HashSet();
   Set formulasToRemove = new HashSet();
   for (Iterator iterator = sheetNames.iterator(); iterator.hasNext(); ) {
     String sheetName = (String) iterator.next();
     List formulas = (List) sheetFormulasMap.get(sheetName);
     formulasToRemove.clear();
     for (int i = 0, size = formulas.size(); i < size; i++) {
       formula = (Formula) formulas.get(i);
       List formulaPoints = null;
       Point formulaPoint = null;
       boolean transformFormula = false;
       if (formula
           .getSheet()
           .getSheetName()
           .equals(transformation.getBlock().getSheet().getSheetName())) {
         transformFormula = true;
         formulaPoint =
             new Point(formula.getRowNum().intValue(), formula.getCellNum().shortValue());
         formulaPoints = transformation.transformCell(formulaPoint);
       }
       if (!transformFormula || (formulaPoints != null && !formulaPoints.isEmpty())) {
         cellRefs = formula.getCellRefs();
         cellRefsToRemove.clear();
         for (Iterator iter = cellRefs.iterator(); iter.hasNext(); ) {
           cellRef = (CellRef) iter.next();
           if (!(transformation instanceof DuplicateTransformation
               && transformation.getBlock().contains(cellRef)
               && transformation.getBlock().contains(formula))) {
             resultCells = transformation.transformCell(sheetName, cellRef);
             if (resultCells != null) {
               int len = resultCells.size();
               if (len == 0) ;
               else if (len == 1) {
                 newCell = (String) resultCells.get(0);
                 cellRef.update(newCell);
               } else if (len > 1) {
                 cellRef.update(resultCells);
               }
             } else {
               cellRefsToRemove.add(cellRef);
             }
           }
         }
         //                cellRefs.removeAll( cellRefsToRemove );
         if (!cellRefsToRemove.isEmpty()) {
           formula.removeCellRefs(cellRefsToRemove);
         }
         if (formula.updateReplacedRefCellsCollection()) {
           formula.updateCellRefs();
         }
         if (formulaPoints != null && !formulaPoints.isEmpty()) {
           if (formulaPoints.size() == 1) {
             newPoint = (Point) formulaPoints.get(0);
             formula.setRowNum(newPoint.getRow());
             formula.setCellNum((int) newPoint.getCol());
           } else {
             List sheetFormulas = (List) sheetFormulasMap.get(formula.getSheet().getSheetName());
             for (int j = 1, num = formulaPoints.size(); j < num; j++) {
               point = (Point) formulaPoints.get(j);
               newFormula = new Formula(formula);
               newFormula.setRowNum(point.getRow());
               newFormula.setCellNum((int) point.getCol());
               newCellRefs = newFormula.getCellRefs();
               for (Iterator iterator1 = newCellRefs.iterator(); iterator1.hasNext(); ) {
                 newCellRef = (CellRef) iterator1.next();
                 if (transformation.getBlock().contains(newCellRef)
                     && transformation.getBlock().contains(formulaPoint)) {
                   newCellRef.update(
                       transformation.getDuplicatedCellRef(sheetName, newCellRef.toString(), j));
                 }
               }
               sheetFormulas.add(newFormula);
             }
           }
         }
       } else {
         if (formulaPoints == null) {
           // remove formula
           formulasToRemove.add(formula);
         }
       }
     }
     formulas.removeAll(formulasToRemove);
   }
 }