public boolean compareCASes()

in uimaj-core/src/main/java/org/apache/uima/cas/impl/CasCompare.java [762:1061]


  public boolean compareCASes() {
    boolean allOk = true;
    // final List<TOP> c1FoundFSs;
    // final List<TOP> c2FoundFSs;
    final boolean savedIsTypeMapping = isTypeMapping;
    mismatchSb.setLength(0);

    try {

      // processIndexedFeatureStructures(c1, false);
      Predicate<TOP> includeFilter = isTypeMapping ? (fs -> isTypeInTgt(fs)) : null;
      if (IS_SHOW_PROGRESS) {
        System.out.println("Finding all FSs in cas 1");
      }
      // this next call doesn't get just the indexed ones, it includes the "reachable" ones too
      c1FoundFSs = new AllFSs(c1, null, includeFilter, isTypeMapping ? typeMapper : null)
              .getAllFSsAllViews_sofas_reachable().getAllFSs();

      // c1FoundFSs = fssToSerialize; // all reachable FSs, filtered by CAS1 -> CAS2 type systems.

      // processIndexedFeatureStructures(c2, false);
      if (IS_SHOW_PROGRESS) {
        System.out.println("Finding all FSs in cas 2");
      }
      c2FoundFSs = new AllFSs(c2, null, null, null).getAllFSsAllViews_sofas_reachable().getAllFSs(); // get
                                                                                                     // just
                                                                                                     // the
                                                                                                     // indexed
                                                                                                     // ones.

      // filter out items only of interest when reached via ref
      if (!excludedRootNames.isEmpty()) {
        System.out.println("Excluding Root Names with: "
                + Misc.ppList(Arrays.asList(excludedRootNames.toArray())));
        c1FoundFSs = c1FoundFSs.stream()
                .filter(fs -> !excludedRootNames.contains(fs.getType().getName()))
                .collect(Collectors.toCollection(ArrayList::new));
        c2FoundFSs = c2FoundFSs.stream()
                .filter(fs -> !excludedRootNames.contains(fs.getType().getName()))
                .collect(Collectors.toCollection(ArrayList::new));
      } else if (!includedTypeNames.isEmpty()) {
        System.out.println("Including only Root Names: "
                + Misc.ppList(Arrays.asList(includedTypeNames.toArray())));
        c1FoundFSs = c1FoundFSs.stream()
                .filter(fs -> includedTypeNames.contains(fs.getType().getName()))
                .collect(Collectors.toCollection(ArrayList::new));
        c2FoundFSs = c2FoundFSs.stream()
                .filter(fs -> includedTypeNames.contains(fs.getType().getName()))
                .collect(Collectors.toCollection(ArrayList::new));

      }

      // if type systems are "isEqual()" still need to map because of feature validation testing

      int i1 = 0;
      int i2 = 0;

      maxId1 = c1.peekNextFsId();
      maxId2 = c2.peekNextFsId();

      // convert_linear_lists_to_arrays may add more items

      convert_linear_lists_to_arrays(c1FoundFSs);
      convert_linear_lists_to_arrays(c2FoundFSs);

      final int sz1 = c1FoundFSs.size(); // max size for comparing, includes added arrays converted
                                         // from lists
      final int sz2 = c2FoundFSs.size();

      isSrcCas = true; // avoids sorting on types/features not present in ts2
      if (IS_SHOW_PROGRESS) {
        System.out.println("Sorting FSs in cas 1");
      }
      sort(c1FoundFSs);

      isSrcCas = false; // avoids sorting on types/features not present in ts1
      if (IS_SHOW_PROGRESS) {
        System.out.println("Sorting FSs in cas 2");
      }
      sort(c2FoundFSs);

      // miscompares.clear();
      prevReport.clear();

      int fsz = Math.max(sz1, sz2);
      int fsz100 = Math.max(1, fsz / 100);
      int prev_done = 0;
      if (IS_SHOW_PROGRESS) {
        System.out.format("Starting compare loop, for %,d FSs%n", Math.max(sz1, sz2));
      }

      while (i1 < sz1 && i2 < sz2) {
        if (IS_SHOW_PROGRESS) {
          int done = Math.max(i1, i2);
          if (done - prev_done >= fsz100) {
            System.out.format("percent done: %d%n", Math.round((done * 100F) / fsz));
            prev_done = done;
          }
        }
        TOP fs1 = c1FoundFSs.get(i1); // assumes the elements are in same order??
        TOP fs2 = c2FoundFSs.get(i2);

        leafErrorReported = null;

        if (null == fs1) { // nulls at end indicate list elements converted to arrays
          if (null != fs2) {
            System.err.format("%,d Feature Structures in CAS2 with no matches in CAS2, e.g. %s%n",
                    sz2 - i2, fs2.toString(2));
            return !allOk;
          } else {
            return allOk;
          }
        }
        if (null == fs2) { // nulls at end indicate list elements converted to arrays
          System.err.format("%,d Feature Structures in CAS1 with no matches in CAS2, e.g. %s%n",
                  sz1 - i1, fs1.toString(2));
          return !allOk;
        }

        if (IS_CANONICAL_EMPTY_LISTS) {
          if (fs1 instanceof EmptyList && !(fs2 instanceof EmptyList)) {
            int start = i1;
            while (true) {
              i1++;
              if (i1 >= sz1) {
                break;
              }
              fs1 = c1FoundFSs.get(i1);
              if (!(fs1 instanceof EmptyList)) {
                break;
              }
            }
            System.out.println("CasCompare skipping " + (i1 - start)
                    + " emptylist FSs in 1st CAS to realign.");
          } else if (fs2 instanceof EmptyList && !(fs1 instanceof EmptyList)) {
            int start = i2;
            while (true) {
              i2++;
              if (i2 >= sz2) {
                break;
              }
              fs2 = c2FoundFSs.get(i2);
              if (!(fs2 instanceof EmptyList)) {
                break;
              }
            }
            System.out.println("CasCompare skipping " + (i2 - start)
                    + " emptylist FSs in 2nd CAS to realign.");
          }
        }

        clearPrevFss();
        prev1.prevCompareTop = fs1;
        prev2.prevCompareTop = fs2;

        if (isTypeMapping) {
          // skip compares for types that are missing in the other type system
          final boolean typeMissingIn1 = typeMapper.mapTypeTgt2Src(fs2._getTypeImpl()) == null;
          final boolean typeMissingIn2 = typeMapper.mapTypeSrc2Tgt(fs1._getTypeImpl()) == null;
          if (!typeMissingIn1 && !typeMissingIn2) {
            if (0 != compareFss(fs1, fs2, null, null)) {
              mismatchFsDisplay();
              if (!isCompareAll) {
                return false;
              }
              allOk = false;
              int tc = fs1._getTypeImpl().compareTo(fs2._getTypeImpl());
              if (tc < 0) {
                System.out.print("skiping first to align types ");
                while (tc < 0 && i1 < sz1) {
                  i1++;
                  tc = c1FoundFSs.get(i1)._getTypeImpl().compareTo(fs2._getTypeImpl());
                  System.out.print(".");
                }
                System.out.println("");
              } else if (tc > 0) {
                System.out.print("skiping second to align types ");
                while (tc > 0 && i2 < sz2) {
                  i2++;
                  tc = fs1._getTypeImpl().compareTo(c2FoundFSs.get(i2)._getTypeImpl());
                  System.out.print(".");
                }
                System.out.println("");
              }
            }
            i1++;
            i2++;
            continue;
          }
          if (typeMissingIn1 && typeMissingIn2) {
            Misc.internalError();
            i1++;
            i2++;
            continue;
          }
          if (typeMissingIn1) {
            System.out.println("debug - type missing in 1, but test fails for refs");
            i2++;
            continue;
          }
          if (typeMissingIn2) {
            Misc.internalError();
            i1++;
            continue;
          }
        } else { // not type mapping
          int rr = -1;
          try {
            rr = compareFss(fs1, fs2, null, null);
          } catch (Throwable e) {
            System.out.println("debug caught throwable");
            e.printStackTrace();
          }
          if (0 != rr) {

            mismatchFsDisplay();
            if (!isCompareAll) {
              return false;
            }
            allOk = false;
            int tc = fs1._getTypeImpl().compareTo(fs2._getTypeImpl());
            if (tc < 0) {
              System.out.print("skiping first to align types ");
              while (tc < 0 && i1 < sz1) {
                i1++;
                tc = c1FoundFSs.get(i1)._getTypeImpl().compareTo(fs2._getTypeImpl());
                System.out.print(".");
              }
              System.out.println("");
            } else if (tc > 0) {
              System.out.print("skiping second to align types ");
              while (tc > 0 && i2 < sz2) {
                i2++;
                tc = fs1._getTypeImpl().compareTo(c2FoundFSs.get(i2)._getTypeImpl());
                System.out.print(".");
              }
              System.out.println("");
            }
            // debug
            else {
              // debug - realign for case where c1 has 1 extra fs
              if (i1 + 1 < sz1) {
                fs1 = c1FoundFSs.get(i1 + 1);
                clearPrevFss();
                prev1.prevCompareTop = fs1;
                prev2.prevCompareTop = fs2;
                if (0 == compareFss(fs1, fs2, null, null)) {
                  // realign
                  System.out.println(
                          "Skipping 1 to realign within same type " + fs1._getTypeImpl().getName());
                  i1++;
                }
              }
            }
          }
          i1++;
          i2++;
          continue;
        }
      }

      if (i1 == sz1 && i2 == sz2) {
        return allOk; // end
      }

      if (isTypeMapping) {
        if (i1 < sz1) {
          System.err.format("%,d Feature Structures in CAS1 with no matches in CAS2, e.g. %s%n",
                  sz1 - i1, c1FoundFSs.get(i1));
          return false;
        }

        while (i2 < sz2) {
          TOP fs = c2FoundFSs.get(i2);
          if (isTypeMapping && typeMapper.mapTypeTgt2Src(fs._getTypeImpl()) != null) { // not a
                                                                                       // complete
                                                                                       // test,
                                                                                       // misses
                                                                                       // refs
            return false; // have more FSs in c2 than in c1
          }
          i2++;
        }
        return true;
      }

      // not type mapping, and number of FS didn't match
      if (i1 < sz1) {
        System.err.format("CAS1 had %,d additional Feature Structures, e.g.: %s%n", sz1 - i1,
                c1FoundFSs.get(i1));
      } else {
        System.err.format("CAS2 had %,d additional Feature Structures, e.g.: %s%n", sz2 - i2,
                c2FoundFSs.get(i2));
      }
      return false;
    } finally {
      isTypeMapping = savedIsTypeMapping;
      clearPrevFss();
    }
  }