Java Code Examples for org.apache.uima.resource.metadata.TypeSystemDescription#getTypes()

The following examples show how to use org.apache.uima.resource.metadata.TypeSystemDescription#getTypes() . 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: DefinedTypesWithSupers.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Update.
 */
private void update() {
  cachedResult.clear();

  // for aggregates, this is the fully-merged type system
  // for all systems, it is the type system with imports resolved
  TypeSystemDescription typeSystemDescription = modelRoot.getMergedTypeSystemDescription();

  if (typeSystemDescription == null)
    return; // cleared table

  TypeDescription[] types = typeSystemDescription.getTypes();
  TypeSystem typeSystem = modelRoot.descriptorCAS.get().getTypeSystem();

  String typeName;
  Map allTypes = modelRoot.allTypes.get();
  for (int i = 0; i < types.length; i++) {
    cachedResult.add(typeName = types[i].getName());
    Type nextType = (Type) allTypes.get(typeName);
    while (nextType != null) {
      nextType = typeSystem.getParent(nextType);
      if (nextType != null)
        cachedResult.add(nextType.getName());
    }
  }
}
 
Example 2
Source File: TypeSection.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
@Override
public void refresh() {
  super.refresh();

  tt.removeAll();

  TypeSystemDescription tsdFull = getMergedTypeSystemDescription();

  TypeDescription[] tdsFull = tsdFull.getTypes();
  if (null != tdsFull) {
    for (int i = 0; i < tdsFull.length; i++) {
      addTypeToGUI(tdsFull[i]);
    }
  }

  if (tt.getItemCount() > 0)
    tt.setSelection(tt.getItems()[0]);
  packTree(tt);
  enable();
}
 
Example 3
Source File: TypeSection.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the types requiring this one.
 *
 * @param typeName the type name
 * @return the types requiring this one
 */
private String[] getTypesRequiringThisOne(String typeName) {
  List upstreamTypeNames = new ArrayList();

  TypeSystemDescription typeSystem = getMergedTypeSystemDescription();

  TypeDescription[] types = typeSystem.getTypes();
  for (int i = 0; i < types.length; i++) {
    if (!types[i].getName().equals(typeName)) {
      if (typeRequiresType(types[i], typeName)) {
        upstreamTypeNames.add(types[i].getName());
      }
    }
  }
  return (String[]) upstreamTypeNames.toArray(new String[upstreamTypeNames.size()]);
}
 
Example 4
Source File: TypeSection.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
@Override
public void enable() {
  jcasGenButton.setEnabled(tt.getItemCount() > 0);
  TypeSystemDescription tsd = getTypeSystemDescription();
  exportButton.setEnabled(false);
  if (null != tsd) {
    TypeDescription[] tsa = tsd.getTypes();
    if (null != tsa)
      exportButton.setEnabled(tsa.length > 0);
  }
  if (isAggregate()) {
    addTypeButton.setEnabled(false);
    addButton.setEnabled(false);
    editButton.setEnabled(false);
    removeButton.setEnabled(false);
  } else {
    addTypeButton.setEnabled(true);
    boolean editable = tt.getSelectionCount() == 1 && isLocalItem(tt.getSelection()[0]);
    addButton.setEnabled(editable);
    editButton.setEnabled(editable);
    removeButton.setEnabled(editable);
  }

}
 
Example 5
Source File: TypeSystemAnalysis.java    From webanno with Apache License 2.0 5 votes vote down vote up
private void analyzeTypeSystem(TypeSystemDescription aTSD)
    throws ResourceInitializationException
{
    // We create a CAS from which we can obtain an instantiated type system. With that, it
    // is easier to check type inheritance.
    CAS cas = WebAnnoCasUtil.createCas(aTSD);
    TypeSystem ts = cas.getTypeSystem();

    for (TypeDescription td : aTSD.getTypes()) {
        analyzeType(ts, aTSD, td);
    }
    
    log.debug("Recognized {} of {} types as layers ({}%)", getLayers().size(),
            aTSD.getTypes().length, getLayers().size() * 100 / aTSD.getTypes().length);
}
 
Example 6
Source File: TypeSystemDescription_implTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testBuildFromXmlElement() throws Exception {
  try {
    File descriptor = JUnitExtension.getFile("TypeSystemDescriptionImplTest/TestTypeSystem.xml");
    TypeSystemDescription ts = UIMAFramework.getXMLParser().parseTypeSystemDescription(
            new XMLInputSource(descriptor));

    assertEquals("TestTypeSystem", ts.getName());
    assertEquals("This is a test.", ts.getDescription());
    assertEquals("The Apache Software Foundation", ts.getVendor());
    assertEquals("0.1", ts.getVersion());
    Import[] imports = ts.getImports();
    assertEquals(3, imports.length);
    assertEquals("org.apache.uima.resource.metadata.impl.TypeSystemImportedByName", imports[0]
            .getName());
    assertNull(imports[0].getLocation());
    assertNull(imports[1].getName());
    assertEquals("TypeSystemImportedByLocation.xml", imports[1].getLocation());

    TypeDescription[] types = ts.getTypes();
    assertEquals(6, types.length);
    TypeDescription paragraphType = types[4];
    assertEquals("Paragraph", paragraphType.getName());
    assertEquals("A paragraph.", paragraphType.getDescription());
    assertEquals("DocumentStructure", paragraphType.getSupertypeName());
    FeatureDescription[] features = paragraphType.getFeatures();
    assertEquals(2, features.length);
    assertEquals("sentences", features[0].getName());
    assertEquals("Direct references to sentences in this paragraph", features[0].getDescription());
    assertEquals("uima.cas.FSArray", features[0].getRangeTypeName());
    assertEquals("Sentence", features[0].getElementType());
    assertFalse(features[0].getMultipleReferencesAllowed());

    // ts.toXML(System.out);
  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}
 
Example 7
Source File: AnnotationSchemaServiceImpl.java    From webanno with Apache License 2.0 4 votes vote down vote up
/**
 * Check if the current CAS already contains the required type system.
 */
private boolean isUpgradeRequired(CAS aCas, TypeSystemDescription aTargetTypeSystem)
{
    TypeSystem ts = aCas.getTypeSystem();
    boolean upgradeRequired = false;
    nextType: for (TypeDescription tdesc : aTargetTypeSystem.getTypes()) {
        Type t = ts.getType(tdesc.getName());
        
        // Type does not exist
        if (t == null) {
            log.debug("CAS update required: type {} does not exist", tdesc.getName());
            upgradeRequired = true;
            break nextType;
        }
        
        // Super-type does not match
        if (!Objects.equals(tdesc.getSupertypeName(), ts.getParent(t).getName())) {
            log.debug("CAS update required: supertypes of {} do not match: {} <-> {}",
                    tdesc.getName(), tdesc.getSupertypeName(), ts.getParent(t).getName());
            upgradeRequired = true;
            break nextType;
        }
        
        // Check features
        for (FeatureDescription fdesc : tdesc.getFeatures()) {
            Feature f = t.getFeatureByBaseName(fdesc.getName());
            
            // Feature does not exist
            if (f == null) {
                log.debug("CAS update required: feature {} on type {} does not exist",
                        fdesc.getName(), tdesc.getName());
                upgradeRequired = true;
                break nextType;
            }
            
            // Range does not match
            if (CAS.TYPE_NAME_FS_ARRAY.equals(fdesc.getRangeTypeName())) {
                if (!Objects.equals(fdesc.getElementType(),
                        f.getRange().getComponentType().getName())) {
                    log.debug(
                            "CAS update required: ranges of feature {} on type {} do not match: {} <-> {}",
                            fdesc.getName(), tdesc.getName(), fdesc.getRangeTypeName(),
                            f.getRange().getName());
                    upgradeRequired = true;
                    break nextType;
                }
            }
            else {
                if (!Objects.equals(fdesc.getRangeTypeName(), f.getRange().getName())) {
                    log.debug(
                            "CAS update required: ranges of feature {} on type {} do not match: {} <-> {}",
                            fdesc.getName(), tdesc.getName(), fdesc.getRangeTypeName(),
                            f.getRange().getName());
                    upgradeRequired = true;
                    break nextType;
                }
            }
        }
    }
    
    return upgradeRequired;
}
 
Example 8
Source File: BlueAnnotationViewGenerator.java    From bluima with Apache License 2.0 4 votes vote down vote up
/**
    * Automatically generates a style map for the given type system. The style
    * map will be returned as an XML string.
    * 
    * @param aTypeSystem
    *            the type system for which a style map will be generated
    * 
    * @return a String containing the XML style map
    */
   public static String autoGenerateStyleMap(TypeSystemDescription aTypeSystem) {
// styles used in automatically generated style maps

final String[] STYLES = { "color:black; background:lightblue;",
	"color:black; background:lightgreen;",
	"color:black; background:orange;",
	"color:black; background:yellow;",
	"color:black; background:pink;",
	"color:black; background:salmon;",
	"color:black; background:cyan;",
	"color:black; background:violet;",
	"color:black; background:tan;",
	"color:white; background:brown;",
	"color:white; background:blue;",
	"color:white; background:green;",
	"color:white; background:red;",
	"color:white; background:mediumpurple;" };

TypeDescription[] types = aTypeSystem.getTypes();

// generate style map by mapping each type to a background color
StringBuffer buf = new StringBuffer();

buf.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
buf.append("<styleMap>\n");

for (int i = 0; i < types.length; i++) {
    String outputType = types[i].getName();
    String label = outputType;
    int lastDot = outputType.lastIndexOf('.');
    if (lastDot > -1) {
	label = outputType.substring(lastDot + 1);
    }

    buf.append("<rule>\n");
    buf.append("<pattern>");
    buf.append(outputType);
    buf.append("</pattern>\n");
    buf.append("<label>");
    buf.append(label);
    buf.append("</label>\n");
    buf.append("<style>");
    buf.append(STYLES[i % STYLES.length]);
    buf.append("</style>\n");
    buf.append("</rule>\n");
}

buf.append("</styleMap>\n");

return buf.toString();
   }
 
Example 9
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 10
Source File: AnnotationViewGenerator.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * Automatically generates a style map for the given type system. The style map will be returned
 * as an XML string.
 * 
 * @param aTypeSystem
 *          the type system for which a style map will be generated
 * 
 * @return a String containing the XML style map
 */
public static String autoGenerateStyleMap(TypeSystemDescription aTypeSystem) {
  // styles used in automatically generated style maps

  final String[] STYLES = { "color:black; background:lightblue;",
      "color:black; background:lightgreen;", "color:black; background:orange;",
      "color:black; background:yellow;", "color:black; background:pink;",
      "color:black; background:salmon;", "color:black; background:cyan;",
      "color:black; background:violet;", "color:black; background:tan;",
      "color:white; background:brown;", "color:white; background:blue;",
      "color:white; background:green;", "color:white; background:red;",
      "color:white; background:mediumpurple;" };

  TypeDescription[] types = aTypeSystem.getTypes();

  // generate style map by mapping each type to a background color
  StringBuffer buf = new StringBuffer();

  buf.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
  buf.append("<styleMap>\n");

  for (int i = 0; i < types.length; i++) {
    String outputType = types[i].getName();
    String label = outputType;
    int lastDot = outputType.lastIndexOf('.');
    if (lastDot > -1) {
      label = outputType.substring(lastDot + 1);
    }

    buf.append("<rule>\n");
    buf.append("<pattern>");
    buf.append(outputType);
    buf.append("</pattern>\n");
    buf.append("<label>");
    buf.append(label);
    buf.append("</label>\n");
    buf.append("<style>");
    buf.append(STYLES[i % STYLES.length]);
    buf.append("</style>\n");
    buf.append("</rule>\n");
  }

  buf.append("</styleMap>\n");

  return buf.toString();
}