public TypeSystemDescription createTypeSystemDescription()

in ruta-core/src/main/java/org/apache/uima/ruta/descriptor/RutaDescriptorBuilder.java [82:295]


  public TypeSystemDescription createTypeSystemDescription(RutaDescriptorInformation desc,
          String typeSystemOutput, RutaBuildOptions options, String[] enginePaths)
          throws InvalidXMLException, ResourceInitializationException, IOException,
          URISyntaxException {

    var typeSystemDescription = uimaFactory.createTypeSystemDescription();

    ResourceManager rm = UIMAFramework.newDefaultResourceManager();
    if (options.getClassLoader() != null) {
      rm = new ResourceManager_impl(options.getClassLoader());
    }

    if (enginePaths != null) {
      String dataPath = "";
      for (String string : enginePaths) {
        dataPath += string + File.pathSeparator;
      }
      rm.setDataPath(dataPath);
    }

    var typeNameMap = new HashMap<String, String>();
    var initialTypeSystem = UIMAFramework.getXMLParser()
            .parseTypeSystemDescription(new XMLInputSource(defaultTypeSystem));

    CAS cas = CasCreationUtils.createCas(initialTypeSystem, null, new FsIndexDescription[0]);
    fillTypeNameMap(typeNameMap, cas.getTypeSystem());
    cas.release();

    List<TypeSystemDescription> toInclude = new ArrayList<>();
    List<Import> importList = new ArrayList<>();
    Import_impl import_impl = new Import_impl();
    if (options.isImportByName()) {
      String name = initialTypeSystem.getName();
      import_impl.setName(name);
    } else if (options.isResolveImports()) {
      String absoluteLocation = initialTypeSystem.getSourceUrlString();
      import_impl.setLocation(absoluteLocation);
    } else {
      URI uri = null;
      try {
        uri = defaultTypeSystem.toURI();
      } catch (URISyntaxException e) {
        // do nothing
      }
      if (uri != null) {
        String relativeLocation = getRelativeLocation(uri, typeSystemOutput);
        if (relativeLocation != null) {
          import_impl.setLocation(relativeLocation);
        } else {
          import_impl.setName(initialTypeSystem.getName());
        }
      } else {
        toInclude.add(initialTypeSystem);
      }
    }
    addImportIfValid(importList, import_impl);

    var descriptorRutaResourceLoader = new RutaResourceLoader(rm, enginePaths,
            options.getClassLoader());

    for (String eachName : desc.getImportedTypeSystems()) {
      Resource resource = descriptorRutaResourceLoader.getResourceWithDotNotation(eachName, ".xml");

      URL url = null;
      boolean include = false;
      if (resource != null) {
        url = resource.getURL();
      }

      if (url == null) {
        url = checkImportExistence(eachName, ".xml", options.getClassLoader());
        include = true;
        if (url == null) {
          throw new FileNotFoundException(
                  "Build process can't find " + eachName + " in " + desc.getScriptName());
        }
      }

      TypeSystemDescription each = getTypeSystemDescriptor(url, rm);
      if (each != null) {
        fillTypeNameMap(typeNameMap, each);
        if (include) {
          // need to include the complete type system because an import is not possible
          each.resolveImports(rm);
          toInclude.add(each);
        } else {
          import_impl = new Import_impl();
          if (options.isImportByName()) {
            import_impl.setName(eachName);
          } else if (options.isResolveImports()) {
            String absoluteLocation = each.getSourceUrlString();
            import_impl.setLocation(absoluteLocation);
          } else {
            String relativeLocation = getRelativeLocation(url.toURI(), typeSystemOutput);
            if (relativeLocation == null) {
              import_impl.setName(eachName);
            } else {
              File parentFile = new File(typeSystemOutput).getParentFile();
              File targetFile = new File(parentFile, relativeLocation);
              if (!targetFile.exists()) {
                import_impl.setName(eachName);
              } else {
                import_impl.setLocation(relativeLocation);
              }
            }
          }
          addImportIfValid(importList, import_impl);
        }
      } else {
        throw new FileNotFoundException(
                "Build process can't find " + eachName + " in " + desc.getScriptName());
      }
    }
    for (String eachName : desc.getImportedScripts()) {
      Resource resource = descriptorRutaResourceLoader.getResourceWithDotNotation(eachName,
              options.getTypeSystemSuffix() + ".xml");
      URL url = null;
      if (resource != null) {
        url = resource.getURL();
      }
      if (url == null) {
        url = checkImportExistence(eachName, options.getTypeSystemSuffix() + ".xml",
                options.getClassLoader());
        if (url == null) {
          throw new FileNotFoundException("Build process can't find " + eachName
                  + options.getTypeSystemSuffix() + ".xml" + " in " + desc.getScriptName());
        }
      }
      TypeSystemDescription each = getTypeSystemDescriptor(url, rm);
      if (each != null) {
        fillTypeNameMap(typeNameMap, each);
        import_impl = new Import_impl();
        if (options.isImportByName()) {
          import_impl.setName(eachName + options.getTypeSystemSuffix());
        } else if (options.isResolveImports()) {
          String absoluteLocation = each.getSourceUrlString();
          import_impl.setLocation(absoluteLocation);
        } else {
          String relativeLocation = getRelativeLocation(url.toURI(), typeSystemOutput);
          import_impl.setLocation(relativeLocation);
        }
        addImportIfValid(importList, import_impl);
      } else {
        throw new FileNotFoundException(
                "Build process can't find " + eachName + " in " + desc.getScriptName());
      }
    }
    typeSystemDescription = CasCreationUtils.mergeTypeSystems(toInclude, rm);
    if (!importList.isEmpty()) {
      Import[] newImports = importList.toArray(new Import[0]);
      typeSystemDescription.setImports(newImports);
    }
    if (options.isResolveImports()) {
      typeSystemDescription.resolveImports(rm);
    }

    // TODO hotfixes: where do I get the final types??
    Set<String> finalTypes = new HashSet<>();
    finalTypes.addAll(Arrays.asList("uima.cas.Boolean", "uima.cas.Byte", "uima.cas.Short",
            "uima.cas.Integer", "uima.cas.Long", "uima.cas.Float", "uima.cas.Double",
            "uima.cas.BooleanArray", "uima.cas.ByteArray", "uima.cas.ShortArray",
            "uima.cas.IntegerArray", "uima.cas.LongArray", "uima.cas.FloatArray",
            "uima.cas.DoubleArray", "uima.cas.StringArray", "uima.cas.FSArray"));

    int typeIndex = 0;
    for (String eachType : desc.getTypeShortNames()) {
      StringTriple typeTriple = desc.getTypeTriples().get(typeIndex);
      typeTriple = resolveType(typeTriple, typeNameMap, desc.getScriptName());
      if (typeSystemDescription.getType(typeTriple.getName()) != null) {
        continue;
      }
      if (!finalTypes.contains(typeTriple.getParent())) {
        TypeDescription newType = typeSystemDescription.addType(typeTriple.getName(),
                typeTriple.getDescription(), typeTriple.getParent());

        Collection<StringTriple> collection = desc.getFeatures().get(eachType);
        if (collection != null) {
          for (StringTriple eachFeature : collection) {
            eachFeature = resolveFeature(eachFeature, typeNameMap);
            newType.addFeature(eachFeature.getName(), eachFeature.getDescription(),
                    eachFeature.getParent());
            // capability.addInputFeature(eachFeature.getName());
            // capability.addOutputFeature(eachFeature.getName());
          }
        }
      }
      typeIndex++;
    }

    Set<String> names = new HashSet<>();
    Collection<TypeDescription> types = new HashSet<>();
    for (TypeDescription each : typeSystemDescription.getTypes()) {
      String name = each.getName();
      if (!names.contains(name)) {
        names.add(name);
        types.add(each);
      }
    }

    TypeDescription[] presentTypes = typeSystemDescription.getTypes();

    types.addAll(Arrays.asList(presentTypes));
    typeSystemDescription.setTypes(types.toArray(new TypeDescription[0]));
    String descName = desc.getScriptName() + options.getTypeSystemSuffix();
    if (!StringUtils.isBlank(desc.getPackageString())) {
      descName = desc.getPackageString() + "." + descName;
    }
    typeSystemDescription.setName(descName);
    if (typeSystemOutput != null) {
      File typeSystemFile = getFile(typeSystemOutput);
      typeSystemDescription.setSourceUrl(typeSystemFile.toURI().toURL());
    }
    return typeSystemDescription;
  }