All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.molgenis.semanticmapper.utils.AlgorithmGeneratorHelper Maven / Gradle / Ivy

package org.molgenis.semanticmapper.utils;

import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.molgenis.data.meta.model.Attribute;
import org.molgenis.data.meta.model.EntityType;

public class AlgorithmGeneratorHelper {
  private static final Pattern MAGMA_ATTRIBUTE_PATTERN =
      Pattern.compile("\\$\\('([^\\$\\(\\)]*)'\\)");

  private AlgorithmGeneratorHelper() {}

  public static Set extractSourceAttributesFromAlgorithm(
      String algorithm, EntityType sourceEntityType) {
    if (StringUtils.isNotBlank(algorithm)) {
      Set attributeNames = new HashSet<>();
      Matcher matcher = MAGMA_ATTRIBUTE_PATTERN.matcher(algorithm);
      while (matcher.find()) {
        attributeNames.add(matcher.group(1));
      }
      return attributeNames.stream()
          .map(sourceEntityType::getAttribute)
          .filter(Objects::nonNull)
          .collect(Collectors.toSet());
    }
    return Collections.emptySet();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy