Java Code Examples for org.apache.uima.resource.metadata.TypeDescription#getName()

The following examples show how to use org.apache.uima.resource.metadata.TypeDescription#getName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CasCreationUtils.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Utility method for populating the aOutputMergedTypes argument in the mergeTypeSystems method.
 * 
 * @param aOutputMergedTypes
 *                Map to populate
 * @param currentType
 *                TypeDescription currently being processed
 * @param existingType
 *                TypeDescription that already existed for the same name
 */
private static void reportMerge(Map<String, Set<String>> aOutputMergedTypes, TypeDescription currentType,
    TypeDescription existingType) {
  if (aOutputMergedTypes != null) {
    String typeName = currentType.getName();
    Set<String> descriptorUrls = aOutputMergedTypes.get(typeName);
    if (descriptorUrls == null) {
      descriptorUrls = new TreeSet<>();
      descriptorUrls.add(existingType.getSourceUrlString());
      descriptorUrls.add(currentType.getSourceUrlString());
      aOutputMergedTypes.put(typeName, descriptorUrls);
    } else {
      descriptorUrls.add(currentType.getSourceUrlString());
    }
  }
}
 
Example 2
Source File: TypeSection.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * Edits the type.
 *
 * @param item the item
 */
// type is always local; could be merging with import(s) or built-in
private void editType(TreeItem item) {

  boolean mergeAndRefreshNeeded = false;
  boolean refreshNeeded = false;
  TypeDescription td = getTypeDescriptionFromTableTreeItem(item);
  AddTypeDialog dialog = new AddTypeDialog(this, td);
  if (dialog.open() == Window.CANCEL)
    return;

  // dialog disallows supertype specs inconsistent with existing features or allowed values
  // dialog has already checked for dup type name
  String newTypeName = dialog.typeName;
  String oldTypeName = td.getName();
  String[] typesRequiringThisOne = stringArray0;
  if (!oldTypeName.equals(newTypeName)) {
    typesRequiringThisOne = showTypesRequiringThisOneMessage(oldTypeName, ALLOWED);
    if (null == typesRequiringThisOne) // null is cancel signal
      return;

    if (isImportedType(oldTypeName)
            && Window.CANCEL == Utility
                    .popOkCancel(
                            "Type define via Import",
                            "The type '"
                                    + oldTypeName
                                    + "' is also defined in 1 or more imports.  Changing the type name here will not change it in the imported type file, causing both types to be in the type system, together. Please confirm this is what you intend.",
                            MessageDialog.WARNING))
      return;
    if (isBuiltInType(oldTypeName)
            && Window.CANCEL == Utility
                    .popOkCancel(
                            "Type was extending a built-in",
                            "The type '"
                                    + oldTypeName
                                    + "' was extending a builtin type of the same name. Changing the type name here will not change the built-in type, causing both types to be in the type system, together. Please confirm this is what you intend.",
                            MessageDialog.WARNING))
      return;
    if (isImportedType(oldTypeName) || isImportedType(newTypeName) || isBuiltInType(oldTypeName)
            || isBuiltInType(newTypeName))
      mergeAndRefreshNeeded = true;
  }
  valueChanged = false;

  // guaranteed non-null because otherwise edit not allowed
  TypeDescription localTd = getLocalTypeDefinition(td);
  typeUpdate(localTd, dialog);
  if (!valueChanged)
    return;
  if (mergeAndRefreshNeeded) {
    rebuildMergedTypeSystem();
    td = getMergedTypeSystemDescription().getType(newTypeName);
  } else {
    typeUpdate(td, dialog);
    updateGuiType(item, td);
  }

  editor.removeDirtyTypeName(oldTypeName);
  editor.addDirtyTypeName(td.getName());
  // interesting case: renaming a type causes another type
  // which has a shadow to get a new super (should cause merge error)
  // or which has a shadow to get a new range (should cause merge error)
  // or which has a shadow of a built-in to get new range or super
  // (should cause error) (but only if that feature is defined in the
  // non-local version)

  refreshNeeded |= alterTypeMentionsInOtherTypes(oldTypeName, td.getName());
  if (refreshNeeded || mergeAndRefreshNeeded)
    refresh();
  alterTypeMentions(oldTypeName, td.getName());

  finishActionPack();
}
 
Example 3
Source File: TypeSection.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * Handle remove type.
 *
 * @param item the item
 */
private void handleRemoveType(final TreeItem item) {

  TypeDescription td = getTypeDescriptionFromTableTreeItem(item);

  String sTypeNameToRemove = td.getName();

  // pop a dialog mentioning typesRequiringThisOne, saying that others must be
  // deleted first....
  if (null == showTypesRequiringThisOneMessage(sTypeNameToRemove, !ALLOWED))
    return;

  boolean bTypeInUseElsewhere = isTypeInUseElsewhere(sTypeNameToRemove);
  if (bTypeInUseElsewhere) {
    String sCascadeDeleteTitle = CASCADE_DELETE_WARNING;
    String sCascadeDeleteMessage = CASCADE_MESSAGE;
    boolean bContinue = MessageDialog.openConfirm(getSection().getShell(), sCascadeDeleteTitle,
            sCascadeDeleteMessage);
    if (!bContinue) {
      return;
    }
  }

  TypeDescription localTd = getLocalTypeDefinition(td);
  removeType(localTd, getTypeSystemDescription());

  if (isImportedType(td)) {
    // although the type itself still remains in the merged type system,
    // features may be removed by this action, so
    // a remerge is needed
    rebuildMergedTypeSystem();
    refresh();
  } else {
    removeType(td, getMergedTypeSystemDescription());
    // update GUI
    setSelectionOneUp(tt, item);
    item.dispose();
  }

  TypeFeature[] featuresToRemove = computeFeaturesToRemove(localTd,
          getMergedTypeSystemDescription().getType(td.getName()));

  if (bTypeInUseElsewhere && !isImportedType(td) && !isBuiltInType(td)) {
    deleteTypeOrFeatureMentions(sTypeNameToRemove, TYPES, null);
  }

  // if removing a type which is also imported or built-in, which is a supertype of something
  // this action can change the feature set.

  if (null != featuresToRemove)
    for (int i = 0; i < featuresToRemove.length; i++) {
      deleteTypeOrFeatureMentions(featuresToRemove[i].featureName, FEATURES,
              featuresToRemove[i].typeName);
    }

  editor.removeDirtyTypeName(sTypeNameToRemove);
  finishAction();
}
 
Example 4
Source File: TypeSection.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * return true if refresh is needed.
 *
 * @param oldTypeName the old type name
 * @param newTypeName the new type name
 * @return true if refresh is needed
 */
private boolean alterTypeMentionsInOtherTypes(String oldTypeName, String newTypeName) {
  // only modify locally modifiable types, but scan all types to give appropriate error msgs
  TypeSystemDescription typeSystem = getMergedTypeSystemDescription();
  boolean refreshNeeded = false;
  boolean remergeNeeded = false;
  TypeDescription[] types = typeSystem.getTypes();
  for (int i = 0; i < types.length; i++) {
    TypeDescription td = types[i];
    TypeDescription localTd = getLocalTypeDefinition(td);
    String typeName = td.getName();
    if (td.getSupertypeName().equals(oldTypeName)) {
      if (null != localTd) { // is a local type
        if (isImportedType(typeName)) {
          Utility
                  .popMessage(
                          "Imported type won't be changed",
                          "There is both a local and imported version of type, '"
                                  + typeName
                                  + "', which has a supertype which is the item being renamed.  Although the local version will be updated, but the imported one won't."
                                  + "This may cause an error when you save.",
                          MessageDialog.WARNING);
        }
        if (isBuiltInType(typeName)) {
          // invalid: changed some type name which was a supertype of a built in - but
          // all the supertypes of built-ins are unchangable.
          throw new InternalErrorCDE("invalid state");
        }
      } else { // is not a local type
        // can't be a built-in type because all the supertypes of built-ins are unchangeable
        Utility
                .popMessage(
                        "Imported type not changed",
                        "There is an imported type, '"
                                + typeName
                                + "', which has a supertype which is the item being renamed.  It won't be updated - this may cause an error when you save this descriptor."
                                + "  If it does, you will need to edit the imported type to change it.",
                        MessageDialog.WARNING);
        continue;
      }
      // guaranteed to have local type def here
      localTd.setSupertypeName(newTypeName);

      if (isImportedType(typeName)) {
        remergeNeeded = true;
        refreshNeeded = true;
      } else {
        td.setSupertypeName(newTypeName);
        updateGuiType(tt.getItems()[i], td);
      }
    }
    FeatureDescription fds[] = td.getFeatures();
    FeatureDescription localFds[] = (null == localTd) ? null : localTd.getFeatures();
    if (null != fds) {
      for (int j = 0; j < fds.length; j++) {
        FeatureDescription fd = fds[j];
        if (oldTypeName.equals(fd.getRangeTypeName())) {
          if (warnAndSkipIfImported(typeName))
            continue; // skipped if feature not present in local td, or no local td.

          setNamedFeatureDescriptionRange(localFds, fd.getName(), newTypeName);
          if (isImportedType(typeName)) {
            remergeNeeded = true;
            refreshNeeded = true;
          } else {
            fd.setRangeTypeName(newTypeName);
            updateGuiFeature(tt.getItems()[i].getItems()[j], fd, td);
          }
        }
      }
    }
  }
  if (remergeNeeded)
    rebuildMergedTypeSystem();
  return refreshNeeded;
}
 
Example 5
Source File: CasCreationUtils.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * Merges features into a TypeDescription.
 * 
 * @param aType
 *                TypeDescription into which to merge the features
 * @param aFeatures
 *                array of features to merge
 * 
 * @throws ResourceInitializationException
 *                 if an incompatibility exists
 */
protected static void mergeFeatures(TypeDescription aType, FeatureDescription[] aFeatures)
    throws ResourceInitializationException {
  FeatureDescription[] existingFeatures = aType.getFeatures();
  if (existingFeatures == null) {
    existingFeatures = EMPTY_FEAT_DESC_ARRAY;
  }

  for (int i = 0; i < aFeatures.length; i++) {
    String featName = aFeatures[i].getName();
    String rangeTypeName = aFeatures[i].getRangeTypeName();
    String elementTypeName = aFeatures[i].getElementType();
    Boolean multiRefsAllowed = aFeatures[i].getMultipleReferencesAllowed();

    // see if a feature already exists with this name
    FeatureDescription feat = null;
    for (int j = 0; j < existingFeatures.length; j++) {
      if (existingFeatures[j].getName().equals(featName)) {
        feat = existingFeatures[j];
        break;
      }
    }

    if (feat == null) {
      // doesn't exist; add it
      FeatureDescription featDesc = aType.addFeature(featName, aFeatures[i].getDescription(),
          rangeTypeName, elementTypeName, multiRefsAllowed);
      featDesc.setSourceUrl(aFeatures[i].getSourceUrl());
    } else {// feature does exist
      // check that the range types match
      if (!feat.getRangeTypeName().equals(rangeTypeName)) {
        throw new ResourceInitializationException(
            ResourceInitializationException.INCOMPATIBLE_RANGE_TYPES, new Object[] {
                aType.getName() + ":" + feat.getName(), rangeTypeName, feat.getRangeTypeName(),
                aType.getSourceUrlString() });
      }
      Boolean mra1 = feat.getMultipleReferencesAllowed();
      Boolean mra2 = multiRefsAllowed;

      // the logic here:
      // OK if both null
      // OK if both not-null, and are equals()
      // OK if one is null, the other has boolean-value of false (false is the default)
      // not ok otherwise

      if (!(((mra1 == null) && (mra2 == null)) || ((mra1 != null) && mra1.equals(mra2))
          || ((mra1 == null) && !mra2) || ((mra2 == null) && !mra1))) {
        throw new ResourceInitializationException(
            ResourceInitializationException.INCOMPATIBLE_MULTI_REFS, new Object[] {
                aType.getName() + ":" + feat.getName(), aType.getSourceUrlString() });
      }

      if (!elementTypesCompatible(feat.getElementType(), elementTypeName)) {
        throw new ResourceInitializationException(
            ResourceInitializationException.INCOMPATIBLE_ELEMENT_RANGE_TYPES, new Object[] {
                aType.getName() + TypeSystem.FEATURE_SEPARATOR + feat.getName(), elementTypeName,
                feat.getElementType(), aType.getSourceUrlString() });
      }
    }
  }
}