org.apache.uima.resource.metadata.NameValuePair Java Examples

The following examples show how to use org.apache.uima.resource.metadata.NameValuePair. 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: PearSpecifier_implTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void testXmlization() throws Exception {
  try {
    PearSpecifier pearSpec = new PearSpecifier_impl();
    pearSpec.setPearPath("/home/user/uimaApp/installedPears/testpear");
    pearSpec.setParameters(new Parameter_impl("param1", "val1"),
            new Parameter_impl("param2", "val2"));
    pearSpec.setPearParameters(new NameValuePair[] { new NameValuePair_impl("param1", "val1"),
        new NameValuePair_impl("param2", "val2") });

    StringWriter sw = new StringWriter();
    pearSpec.toXML(sw);
    PearSpecifier pearSpec2 = (PearSpecifier) UIMAFramework.getXMLParser().parse(
            new XMLInputSource(new ByteArrayInputStream(sw.getBuffer().toString().getBytes(encoding)),
                    null));
    assertEquals(pearSpec, pearSpec2);
  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}
 
Example #2
Source File: CPEFactory.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Find a parameter with a given name in the in-memory component descriptor.
 *
 * @param aCps -
 *          parameter settings from the component's descriptor
 * @param aParamName -
 *          name of the parameter to look for
 * @return - parameter as {@link NameValuePair} instance. If not found, returns null.
 * @throws Exception -
 *           any error
 */
private NameValuePair findMatchingNameValuePair(ConfigurationParameterSettings aCps,
        String aParamName) throws Exception {

  NameValuePair[] nvp = aCps.getParameterSettings();

  // Find a parameter with a given name
  for (int i = 0; nvp != null && i < nvp.length; i++) {
    if (nvp[i].getName() != null) {
      if (nvp[i].getName().equalsIgnoreCase(aParamName)) {
        return nvp[i]; // Found it
      }
    }
  }
  return null; // Parameter with a given name does not exist
}
 
Example #3
Source File: CasDataUtils.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public static NameValuePair[] getCasDataFeatures(CasData aCasData, String aFeatureStructureName) {
  NameValuePair[] valuePairSet = null;
  Iterator<FeatureStructure> it = aCasData.getFeatureStructures();

  while (it.hasNext()) {
    FeatureStructure fs = it.next();
    if (fs.getType().equals(aFeatureStructureName)) {
      String[] featureNames = fs.getFeatureNames();
      if (featureNames == null) {
        // return empty set
        return new NameValuePair[0];
      }
      valuePairSet = new NameValuePair[featureNames.length];
      for (int i = 0; i < featureNames.length; i++) {
        valuePairSet[i] = new NameValuePair_impl();
        valuePairSet[i].setName(featureNames[i]);
        valuePairSet[i].setValue(fs.getFeatureValue(featureNames[i]).toString());
        // System.out.println("DATACasUtils.getCasDataFeatures()-Name::"+valuePairSet[i].getName()+"
        // Value:::"+valuePairSet[i].getValue().toString());
      }
    }
  }
  return valuePairSet;
}
 
Example #4
Source File: CPEFactory.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Replace component's parameters. Its parameter values will be changed to match those defined in
 * the CPE descriptor.
 *
 * @param aResourceSpecifier -
 *          component's descriptor containing parameters to override
 * @param aCpe_cps the a cpe cps
 * @param aComponentName the a component name
 * @throws Exception -
 *           failure during processing
 */
private void overrideParameterSettings(ResourceSpecifier aResourceSpecifier,
        CasProcessorConfigurationParameterSettings aCpe_cps,
        String aComponentName) throws Exception {

  if (aCpe_cps != null && aCpe_cps.getParameterSettings() != null) {
    // Extract new parameters from the CPE descriptor
    // Parameters are optional, so test and do the override if necessary
    org.apache.uima.collection.metadata.NameValuePair[] nvp = aCpe_cps.getParameterSettings();

    for (int i = 0; i < nvp.length; i++) {
      // Next parameter to overridde
      if (nvp[i] != null) {
        overrideParameterIfExists(aResourceSpecifier, nvp[i], aComponentName);
      }
    }
  }
}
 
Example #5
Source File: DATACasUtils.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the cas data features.
 *
 * @param aCasData the a cas data
 * @param aFeatureStructureName the a feature structure name
 * @return tbd
 */
public static NameValuePair[] getCasDataFeatures(CasData aCasData, String aFeatureStructureName) {
  NameValuePair[] valuePairSet = null;
  Iterator it = aCasData.getFeatureStructures();

  while (it.hasNext()) {
    Object object = it.next();
    if (object instanceof FeatureStructure
            && ((FeatureStructure) object).getType().equals(aFeatureStructureName)) {
      FeatureStructure fs = (FeatureStructure) object;
      String[] featureNames = fs.getFeatureNames();
      if (featureNames == null) {
        // return empty set
        return new NameValuePair[0];
      }
      valuePairSet = new NameValuePair[featureNames.length];
      for (int i = 0; i < featureNames.length; i++) {
        valuePairSet[i] = new NameValuePair_impl();
        valuePairSet[i].setName(featureNames[i]);
        valuePairSet[i].setValue(fs.getFeatureValue(featureNames[i]).toString());
      }
    }
  }
  return valuePairSet;
}
 
Example #6
Source File: ConfigurationManagerImplBase.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Utility method that gets a NameValuePair array containing the specific parameter settings.
 * 
 * @param aGroupName
 *          name of group containing params, may be null
 * @param aParams
 *          array of parameters to look up
 * @param aContextName
 *          context containing parameters
 * 
 * @return array containing settings of the specific parameters
 */
private NameValuePair[] getParamSettings(String aGroupName, ConfigurationParameter[] aParams,
        String aContextName) {
  List<NameValuePair> result = new ArrayList<>();
  // iterate over config. param _declarations_
  if (aParams != null) {
    for (int i = 0; i < aParams.length; i++) {
      ConfigurationParameter param = aParams[i];
      NameValuePair nvp = UIMAFramework.getResourceSpecifierFactory().createNameValuePair();
      nvp.setName(param.getName());
      // look up value in context
      String qualifiedName = this.makeQualifiedName(aContextName, param.getName(), aGroupName);
      nvp.setValue(this.lookup(qualifiedName));
      result.add(nvp);
    }
  }
  NameValuePair[] resultArr = new NameValuePair[result.size()];
  result.toArray(resultArr);
  return resultArr;
}
 
Example #7
Source File: ConfigurationParameterSettings_impl.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * @see ConfigurationParameterSettings#getParameterValue(java.lang.String,
 *      java.lang.String)
 */
public Object getParameterValue(String aGroupName, String aParamName) {
  if (aGroupName == null) {
    return getParameterValue(aParamName);
  } else {
    NameValuePair[] nvps = mSettingsForGroups.get(aGroupName);
    if (nvps != null) {
      for (int i = 0; i < nvps.length; i++) {
        if (aParamName.equals(nvps[i].getName())) {
          return nvps[i].getValue();
        }
      }
    }
    return null;
  }
}
 
Example #8
Source File: ResourceMetaData_impl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Validates configuration parameter settings within a group.
 * 
 * @param aNVPs
 *          the parameter settings
 * @param aGroupName
 *          the group
 * @param aParamDecls
 *          Configuration Parameter Declarations
 * 
 * @throws ResourceConfigurationException
 *           if the configuration parameter settings are invalid
 */
protected void validateConfigurationParameterSettings(NameValuePair[] aNVPs, String aGroupName,
        ConfigurationParameterDeclarations aParamDecls) throws ResourceConfigurationException {
  for (int i = 0; i < aNVPs.length; i++) {
    // look up the parameter info
    String name = aNVPs[i].getName();
    if (name == null) {
      throw new ResourceConfigurationException(InvalidXMLException.ELEMENT_NOT_FOUND,
              new Object[] { "name", "nameValuePair" });
    }
    ConfigurationParameter param = aParamDecls.getConfigurationParameter(aGroupName, name);
    if (param == null) {
      if (aGroupName == null) {
        throw new ResourceConfigurationException(
                ResourceConfigurationException.NONEXISTENT_PARAMETER, new Object[] { name,
                    getName() });
      } else {
        throw new ResourceConfigurationException(
                ResourceConfigurationException.NONEXISTENT_PARAMETER_IN_GROUP, new Object[] {
                    name, aGroupName, getName() });
      }
    } else {
      // check datatype
      validateConfigurationParameterDataTypeMatch(param, aNVPs[i]);
    }
  }
}
 
Example #9
Source File: ConfigurableDataResourceSpecifier_implTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testXmlization() throws Exception {
  try {
    // create a ConfigurableDataResourceSpecifier
    ConfigurableDataResourceSpecifier_impl cspec = new ConfigurableDataResourceSpecifier_impl();
    cspec.setUrl("jdbc:db2:MyDatabase");
    ResourceMetaData md = new ResourceMetaData_impl();
    cspec.setMetaData(md);
    md.setName("foo");
    ConfigurationParameterDeclarations decls = new ConfigurationParameterDeclarations_impl();
    ConfigurationParameter param = new ConfigurationParameter_impl();
    param.setName("param");
    param.setType("String");
    decls.addConfigurationParameter(param);
    md.setConfigurationParameterDeclarations(decls);
    ConfigurationParameterSettings settings = new ConfigurationParameterSettings_impl();
    NameValuePair nvp = new NameValuePair_impl();
    nvp.setName("param");
    nvp.setValue("bar");
    settings.setParameterSettings(new NameValuePair[] { nvp });
    md.setConfigurationParameterSettings(settings);

    // wrtie to XML
    StringWriter sw = new StringWriter();
    cspec.toXML(sw);
    String xmlStr = sw.getBuffer().toString();

    // parse back
    ByteArrayInputStream inStream = new ByteArrayInputStream(xmlStr.getBytes(StandardCharsets.UTF_8));
    XMLInputSource in = new XMLInputSource(inStream, null);
    ConfigurableDataResourceSpecifier_impl parsedSpec = (ConfigurableDataResourceSpecifier_impl) UIMAFramework
            .getXMLParser().parse(in);

    assertEquals(cspec, parsedSpec);

  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}
 
Example #10
Source File: MetaDataObject_implTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the {@link MetaDataObject#equals(Object)} method.
 */
public void testEquals() throws Exception {
  try {
    Assert.assertEquals(unknownFruit, unknownFruit);
    Assert.assertEquals(apple1, apple2);
    Assert.assertEquals(apple2, apple1);
    Assert.assertTrue(!unknownFruit.equals(apple1));
    Assert.assertTrue(!apple1.equals(orange));
    Assert.assertTrue(!apple1.equals(null));

    Assert.assertEquals(apple1, apple1.clone());
    Assert.assertEquals(fruitBag, fruitBag.clone());
    Assert.assertTrue(!apple1.equals(orange.clone()));
    
    // test with maps
    ConfigurationParameterSettings cps1 = UIMAFramework.getResourceSpecifierFactory().createConfigurationParameterSettings();
    cps1.getSettingsForGroups().put("k1", new NameValuePair[] {new NameValuePair_impl("s1", "o1")});
    cps1.getSettingsForGroups().put("k2", new NameValuePair[] {new NameValuePair_impl("s2", "o2")});
    ConfigurationParameterSettings cps2 = UIMAFramework.getResourceSpecifierFactory().createConfigurationParameterSettings();
    cps2.getSettingsForGroups().put("k1", new NameValuePair[] {new NameValuePair_impl("s1", "o1")});
    cps2.getSettingsForGroups().put("k2", new NameValuePair[] {new NameValuePair_impl("s2", "o2")});
    
    Assert.assertEquals(cps1, cps2);
    Assert.assertEquals(cps1, cps2.clone());
    
    cps2.getSettingsForGroups().put("k2", new NameValuePair[] {new NameValuePair_impl("s2", "ox2")});
    Assert.assertFalse(cps1.equals(cps2));
         
  } catch (RuntimeException e) {
    JUnitExtension.handleException(e);
  }
}
 
Example #11
Source File: FixedFlowController.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public static FlowControllerDescription makeDefaultDescription() {
  FlowControllerDescription desc = getResourceSpecifierFactory().createFlowControllerDescription();
  
  desc.setImplementationName(FixedFlowController.class.getName());
  
  ProcessingResourceMetaData metaData = desc.getFlowControllerMetaData();
  metaData.setName("Fixed Flow Controller");
  metaData.setDescription("Simple FlowController that uses the FixedFlow element of the\n" + 
      "\t\taggregate descriptor to determine a linear flow.");
  metaData.setVendor("The Apache Software Foundation");
  metaData.setVersion("1.0");
  
  Capability capability = getResourceSpecifierFactory().createCapability();
  metaData.setCapabilities(new Capability[] { capability });
 
  ConfigurationParameter param = getResourceSpecifierFactory().createConfigurationParameter();
  param.setName("ActionAfterCasMultiplier");
  param.setType("String");
  param.setDescription("The action to be taken after a CAS has been input to a CAS Multiplier and the CAS Multiplier has finished processing it.\n" + 
      "\t\t Valid values are:\n" + 
      "\t\t\tcontinue - the CAS continues on to the next element in the flow\n" + 
      "\t\t\tstop - the CAS will no longer continue in the flow, and will be returned from the aggregate if possible.\n" + 
      "\t\t\tdrop - the CAS will no longer continue in the flow, and will be dropped (not returned from the aggregate) if possible.\t \n" + 
      "\t\t\tdropIfNewCasProduced (the default) - if the CAS multiplier produced a new CAS as a result of processing this CAS, then this\n" + 
      "\t\t\t\tCAS will be dropped.  If not, then this CAS will continue.");
  ConfigurationParameterDeclarations parameterDeclarations = getResourceSpecifierFactory().createConfigurationParameterDeclarations();
  parameterDeclarations.setConfigurationParameters(new ConfigurationParameter[] { param });
  metaData.setConfigurationParameterDeclarations(parameterDeclarations);
  
  NameValuePair paramSetting = getResourceSpecifierFactory().createNameValuePair();
  paramSetting.setName("ActionAfterCasMultiplier");
  paramSetting.setValue("dropIfNewCasProduced");
  ConfigurationParameterSettings parameterSettings = getResourceSpecifierFactory().createConfigurationParameterSettings();
  parameterSettings.setParameterSettings(new NameValuePair[] { paramSetting });
  metaData.setConfigurationParameterSettings(parameterSettings);

  return desc;
}
 
Example #12
Source File: FixedFlowController.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public static FlowControllerDescription getDescription() {
  FlowControllerDescription desc = getResourceSpecifierFactory().createFlowControllerDescription();
  
  desc.setImplementationName(FixedFlowController.class.getName());
  
  ProcessingResourceMetaData metaData = desc.getFlowControllerMetaData();
  metaData.setName("Fixed Flow Controller");
  metaData.setDescription("Simple FlowController that uses the FixedFlow element of the\n" + 
      "\t\taggregate descriptor to determine a linear flow.");
  metaData.setVendor("The Apache Software Foundation");
  metaData.setVersion("1.0");
  
  Capability capability = getResourceSpecifierFactory().createCapability();
  metaData.setCapabilities(new Capability[] { capability });
  
  ConfigurationParameter param = getResourceSpecifierFactory().createConfigurationParameter();
  param.setName("ActionAfterCasMultiplier");
  param.setType("String");
  param.setDescription("The action to be taken after a CAS has been input to a CAS Multiplier and the CAS Multiplier has finished processing it.\n" + 
      "\t\t Valid values are:\n" + 
      "\t\t\tcontinue - the CAS continues on to the next element in the flow\n" + 
      "\t\t\tstop - the CAS will no longer continue in the flow, and will be returned from the aggregate if possible.\n" + 
      "\t\t\tdrop - the CAS will no longer continue in the flow, and will be dropped (not returned from the aggregate) if possible.\t \n" + 
      "\t\t\tdropIfNewCasProduced (the default) - if the CAS multiplier produced a new CAS as a result of processing this CAS, then this\n" + 
      "\t\t\t\tCAS will be dropped.  If not, then this CAS will continue.");
  ConfigurationParameterDeclarations parameterDeclarations = getResourceSpecifierFactory().createConfigurationParameterDeclarations();
  parameterDeclarations.setConfigurationParameters(new ConfigurationParameter[] { param });
  metaData.setConfigurationParameterDeclarations(parameterDeclarations);
  
  NameValuePair paramSetting = getResourceSpecifierFactory().createNameValuePair();
  paramSetting.setName("ActionAfterCasMultiplier");
  paramSetting.setValue("dropIfNewCasProduced");
  ConfigurationParameterSettings parameterSettings = getResourceSpecifierFactory().createConfigurationParameterSettings();
  parameterSettings.setParameterSettings(new NameValuePair[] { paramSetting });
  metaData.setConfigurationParameterSettings(parameterSettings);
  
  return desc;
}
 
Example #13
Source File: XMLParser_implTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testParsePearSpecifier() throws Exception {
  XMLInputSource in = new XMLInputSource(
          JUnitExtension.getFile("XmlParserTest/TestPearSpecifier.xml"));
  PearSpecifier pearSpec = this.mXmlParser.parsePearSpecifier(in);
  assertEquals("/home/user/uimaApp/installedPears/testpear", pearSpec.getPearPath());
  
  assertThat(pearSpec.getParameters())
      .extracting(Parameter::getName, Parameter::getValue)
      .containsExactly(tuple("legacyParam1", "legacyVal1"), tuple("legacyParam2", "legacyVal2"));

  assertThat(pearSpec.getPearParameters())
      .extracting(NameValuePair::getName, NameValuePair::getValue)
      .containsExactly(tuple("param1", "stringVal1"), tuple("param2", true));
}
 
Example #14
Source File: ResourceMetaData_impl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Validate that a value is of an appropriate data type for assignment to the given parameter.
 * <P>
 * NOTE: this method can cause a change to the NameValuePair object in the case where the value of
 * a parameter is an empty Object[] and the parameter type is an array of a different type. In
 * this case the empty object array will be replaced by an empty array of the appropriate type.
 * 
 * @param aParam
 *          configuration parameter
 * @param aNVP
 *          name value pair containing candidate value
 * 
 * @throws ResourceConfigurationException
 *           if the data types do not match
 */
protected void validateConfigurationParameterDataTypeMatch(ConfigurationParameter aParam,
        NameValuePair aNVP) throws ResourceConfigurationException {
  String paramName = aParam.getName();
  String paramType = aParam.getType();
  if (aNVP.getValue() == null) {
    throw new ResourceConfigurationException(ResourceConfigurationException.CONFIG_SETTING_ABSENT,
            new Object[] { paramName });
  }
  Class<?> valClass = aNVP.getValue().getClass();

  if (aParam.isMultiValued()) // value must be an array
  {
    if (!valClass.isArray()) {
      throw new ResourceConfigurationException(ResourceConfigurationException.ARRAY_REQUIRED,
              new Object[] { paramName, getName() });
    }
    valClass = valClass.getComponentType();
    // check for zero-length array special case
    if (Array.getLength(aNVP.getValue()) == 0 && valClass.equals(Object.class)) {
      aNVP.setValue(Array.newInstance(getClassForParameterType(paramType), 0));
      return;
    }
  }

  if (valClass != getClassForParameterType(paramType)) {
    throw new ResourceConfigurationException(
            ResourceConfigurationException.PARAMETER_TYPE_MISMATCH, new Object[] { getName(),
                valClass.getName(), paramName, paramType });
    /*  Parameter type mismatch in component "{0}".  A value of class {1} cannot be 
        assigned to the configuration parameter {2}, which has type {3}.
     */
  }
}
 
Example #15
Source File: ConfigurationParameterSettings_impl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Overridden becuase of settingsForGroups property, which is a Map and isn't handled by default
 * XMLization routines.
 * 
 * @see XMLizable#buildFromXMLElement(org.w3c.dom.Element,
 *      org.apache.uima.util.XMLParser)
 */
public void buildFromXMLElement(Element aElement, XMLParser aParser,
        XMLParser.ParsingOptions aOptions) throws InvalidXMLException {
  List<XMLizable> nvps = new ArrayList<>();
  // get all child nodes
  NodeList childNodes = aElement.getChildNodes();
  for (int i = 0; i < childNodes.getLength(); i++) {
    Node curNode = childNodes.item(i);
    if (curNode instanceof Element) {
      Element elem = (Element) curNode;
      // check element tag name
      if ("nameValuePair".equals(elem.getTagName())) {
        nvps.add(aParser.buildObject(elem, aOptions));
      } else if ("settingsForGroup".equals(elem.getTagName())) {
        String key = elem.getAttribute("name");

        List<XMLizable> vals = new ArrayList<>();
        NodeList arrayNodes = elem.getChildNodes();
        for (int j = 0; j < arrayNodes.getLength(); j++) {
          Node curArrayNode = arrayNodes.item(j);
          if (curArrayNode instanceof Element) {
            Element valElem = (Element) curArrayNode;
            vals.add(aParser.buildObject(valElem));
          }
        }
        if (!vals.isEmpty()) {
          NameValuePair[] valArr = new NameValuePair[vals.size()];
          vals.toArray(valArr);
          mSettingsForGroups.put(key, valArr);
        }
      } else {
        throw new InvalidXMLException(InvalidXMLException.UNKNOWN_ELEMENT, new Object[] { elem
                .getTagName() });
      }
    }
  }
  NameValuePair[] nvpArr = new NameValuePair[nvps.size()];
  nvps.toArray(nvpArr);
  setParameterSettings(nvpArr);
}
 
Example #16
Source File: CasMetaData.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the cas meta data.
 *
 * @return the cas meta data
 */
public NameValuePair[] getCasMetaData() {
  if (casMetaData == null) {
    return new NameValuePair[0];
  }
  return casMetaData;
}
 
Example #17
Source File: CPEFactory.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Replace a primitive value of a given parameter with a value defined in the CPE descriptor.
 *
 * @param aType -
 *          type of the primitive value ( String, Integer, Boolean, or Float)
 * @param aMandatoryParam the a mandatory param
 * @param aCps -
 *          parameter settings from the component's descriptor
 * @param aCPE_nvp -
 *          parameter containing array of values to replace values in the component's descriptor
 * @param aComponentName the a component name
 * @throws Exception -
 */
private void replacePrimitive(String aType, boolean aMandatoryParam,
        ConfigurationParameterSettings aCps,
        org.apache.uima.collection.metadata.NameValuePair aCPE_nvp,
        String aComponentName) throws Exception {
  boolean newParamSetting = false;

  // Get a new value for the primitive param
  Object aValueObject = aCPE_nvp.getValue();
  String param_name = aCPE_nvp.getName();

  // Find corresponding parameter in the in-memory component descriptor
  NameValuePair nvp = findMatchingNameValuePair(aCps, param_name.trim());
  if (nvp == null) {
    newParamSetting = true;
    nvp = new NameValuePair_impl();
    nvp.setName(param_name);
  }
  // Copy a new value based on type
  if (aType.equals("String") && aValueObject instanceof String) {
    nvp.setValue(aValueObject);
  } else if (aType.equals("Integer") && aValueObject instanceof Integer) {
    nvp.setValue(aValueObject);
  } else if (aType.equals("Float") && aValueObject instanceof Float) {
    nvp.setValue(aValueObject);

  } else if (aType.equals("Boolean") && aValueObject instanceof Boolean) {
    nvp.setValue(aValueObject);
  }

  if (newParamSetting) {
    aCps.setParameterValue(null, nvp.getName(), nvp.getValue());
  }
}
 
Example #18
Source File: ConfigurationParameterSettings_impl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * @see ConfigurationParameterSettings#setParameterSettings(NameValuePair[])
 */
public void setParameterSettings(NameValuePair[] aSettings) {
  if (aSettings == null) {
    throw new UIMA_IllegalArgumentException(UIMA_IllegalArgumentException.ILLEGAL_ARGUMENT,
            new Object[] { "null", "aSettings", "setParameterSettings" });
  }
  mParameterSettings = aSettings;
}
 
Example #19
Source File: CPEFactory.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Replace array values found in the component's descriptor with values found in the CPE
 * descriptor.
 *
 * @param aType -
 *          primitive type of the array (Sting, Integer, Float, or Boolean)
 * @param aMandatoryParam the a mandatory param
 * @param aCps -
 *          parameter settings from the component's descriptor
 * @param aCPE_nvp -
 *          parameter containing array of values to replace values in the component's descriptor
 * @param aComponentName the a component name
 * @throws Exception -
 *           any error
 */
private void replaceArray(String aType, boolean aMandatoryParam,
        ConfigurationParameterSettings aCps,
        org.apache.uima.collection.metadata.NameValuePair aCPE_nvp,
        String aComponentName) throws Exception {
  boolean newParamSetting = false;
  Object valueObject = aCPE_nvp.getValue();
  String param_name = aCPE_nvp.getName();
  // Find in the component in-memory descriptor a parameter with a given name
  NameValuePair nvp = findMatchingNameValuePair(aCps, param_name.trim());
  if (nvp == null) {
    // Parameter setting does not exist in the component's descriptor so create new
    newParamSetting = true;
    nvp = new NameValuePair_impl();
    nvp.setName(param_name);
  }
  // Override component's parameters based on type
  if (aType.equals("String") && valueObject instanceof String[]) {
    nvp.setValue(valueObject);
  } else if (aType.equals("Integer") && valueObject instanceof Integer[]) {
    nvp.setValue(valueObject);
  } else if (aType.equals("Float") && valueObject instanceof Float[]) {
    nvp.setValue(valueObject);
  } else if (aType.equals("Boolean") && valueObject instanceof Boolean[]) {
    nvp.setValue(valueObject);
  }
  if (newParamSetting) {
    aCps.setParameterValue(null, nvp.getName(), nvp.getValue());
  }

}
 
Example #20
Source File: CPEFactory.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Override component's parameters. This overridde effects the in-memory settings. The actual
 * component's descriptor will not be changed.
 *
 * @param aResourceSpecifier -
 *          in-memory descriptor of the component
 * @param aCPE_nvp -
 *          parameter represented as name-value pair. If the name of the parameter is found in the
 *          component's descriptor its value will be changed.
 * @param aComponentName the a component name
 * @return true, if successful
 * @throws Exception -
 *           error during processing
 */
private boolean overrideParameterIfExists(ResourceSpecifier aResourceSpecifier,
        org.apache.uima.collection.metadata.NameValuePair aCPE_nvp,
        String aComponentName) throws Exception {
  // Retrieve component's parameter settings from the in-memory descriptor
  ConfigurationParameterDeclarations cpd = ((ResourceCreationSpecifier) aResourceSpecifier)
          .getMetaData().getConfigurationParameterDeclarations();
  // Get the name of the parameter to find in the component's parameter list
  String param_name = aCPE_nvp.getName().trim();
  // Extract parameter with a matching name from the parameter declaration section of the
  // component's descriptor
  ConfigurationParameter cparam = cpd.getConfigurationParameter(null, param_name);
  if (cparam != null) {
    // Retrieve component's parameter settings from the in-memory descriptor
    ConfigurationParameterSettings cps = ((ResourceCreationSpecifier) aResourceSpecifier)
            .getMetaData().getConfigurationParameterSettings();
    // Determie if it is a multi-value parameter (array)
    boolean isMultiValue = cparam.isMultiValued();
    // Check if there is a match based on param name
    if (cparam.getName().equals(param_name)) {
      boolean mandatory = cparam.isMandatory();
      if (isMultiValue) {
        // Override array with values from the CPE descriptor
        replaceArray(cparam.getType(), mandatory, cps, aCPE_nvp, aComponentName);
      } else {
        // Override primitive parameter with value from the CPE descriptor
        replacePrimitive(cparam.getType(), mandatory, cps, aCPE_nvp,
                aComponentName);
      }
      return true; // Found a match and did the override
    }

  }
  return false; // The parameter does not exist in the component's descriptor
}
 
Example #21
Source File: CasProcessorConfigurationJAXBImpl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a value for a given deployment parameter
 * 
 * @param aDeployParameter - name of the parameter
 * @return - value for parameter name
 */
public String getDeploymentParameter(String aDeployParameter) {
  String desc = null;
  if (aDeployParameter == null || deploymentParameters == null) {
    return null;
  }
  for (int i = 0; i < deploymentParameters.size(); i++) {
    NameValuePair nvp = (NameValuePair) deploymentParameters.get(i);
    if (aDeployParameter.equals(nvp.getName().trim())) {
      desc = (String) nvp.getValue();
      break;
    }
  }
  return desc;
}
 
Example #22
Source File: ConfigurationParameterSettings_impl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * @see ConfigurationParameterSettings#getParameterValue(String)
 */
public Object getParameterValue(String aParamName) {
  NameValuePair[] nvps = getParameterSettings();
  if (nvps != null) {
    for (int i = 0; i < nvps.length; i++) {
      if (aParamName.equals(nvps[i].getName())) {
        return nvps[i].getValue();
      }
    }
  }
  return null;
}
 
Example #23
Source File: TestResourceInterface_impl.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * @see org.apache.uima.resource.Resource#setConfigurationParameters(NameValuePair[])
 */
public void setConfigurationParameters(NameValuePair[] aSettings)
        throws ResourceConfigurationException {
}
 
Example #24
Source File: JCasUtils.java    From termsuite-core with Apache License 2.0 4 votes vote down vote up
public static void showAEConfig(AnalysisEngine analysisEngine) {
	for(NameValuePair pair:analysisEngine.getMetaData().getConfigurationParameterSettings().getParameterSettings()) {
		System.out.println("** " + pair.getName() + ": " + pair.getValue());
	}
}
 
Example #25
Source File: ResourceSpecifierFactory_impl.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * @see org.apache.uima.ResourceSpecifierFactory#createNameValuePair()
 */
public NameValuePair createNameValuePair() {
  return (NameValuePair) createObject(NameValuePair.class);
}
 
Example #26
Source File: TestResourceInterface_impl.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * @see org.apache.uima.resource.Resource#setConfigurationParameters(NameValuePair[])
 */
public void setConfigurationParameters(NameValuePair[] aSettings)
        throws ResourceConfigurationException {
  //do nothing
}
 
Example #27
Source File: PearSpecifier_impl.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void setPearParameters(NameValuePair... pearParameters) {
  this.mPearParameters = pearParameters;
}
 
Example #28
Source File: PearSpecifier_impl.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public NameValuePair[] getPearParameters() {
  return this.mPearParameters;
}
 
Example #29
Source File: ConfigurationManagerImplBase.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public ConfigurationParameterSettings getCurrentConfigParameterSettings(String aContextName) {
  // get declarations
  ConfigurationParameterDeclarations decls = mContextNameToParamDeclsMap
          .get(aContextName);

  ConfigurationParameterSettings settings = UIMAFramework.getResourceSpecifierFactory()
          .createConfigurationParameterSettings();

  ConfigurationParameter[] paramsInNoGroup = decls.getConfigurationParameters();
  if (paramsInNoGroup.length > 0) // no groups declared
  {
    settings.setParameterSettings(getParamSettings(null, paramsInNoGroup, aContextName));
  } else
  // groups declared
  {
    ConfigurationGroup[] groups = decls.getConfigurationGroups();
    if (groups != null) {
      for (int i = 0; i < groups.length; i++) {
        String[] names = groups[i].getNames();
        {
          for (int j = 0; j < names.length; j++) {
            // common params
            NameValuePair[] commonParamSettings = getParamSettings(names[j], decls
                    .getCommonParameters(), aContextName);
            NameValuePair[] specificParamSettings = getParamSettings(names[j], groups[i]
                    .getConfigurationParameters(), aContextName);
            NameValuePair[] mergedSettings = new NameValuePair[commonParamSettings.length
                    + specificParamSettings.length];
            System.arraycopy(commonParamSettings, 0, mergedSettings, 0,
                    commonParamSettings.length);
            System.arraycopy(specificParamSettings, 0, mergedSettings,
                    commonParamSettings.length, specificParamSettings.length);
            settings.getSettingsForGroups().put(names[j], mergedSettings);
          }
        }
      }
    }
  }

  return settings;
}
 
Example #30
Source File: AnalysisEnginePoolTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testReconfigure() throws Exception {
  try {
    // create simple primitive TextAnalysisEngine descriptor (using TestAnnotator class)
    AnalysisEngineDescription primitiveDesc = new AnalysisEngineDescription_impl();
    primitiveDesc.setPrimitive(true);
    primitiveDesc.getMetaData().setName("Test Primitive TAE");
    primitiveDesc
            .setAnnotatorImplementationName("org.apache.uima.analysis_engine.impl.TestAnnotator");
    ConfigurationParameter p1 = new ConfigurationParameter_impl();
    p1.setName("StringParam");
    p1.setDescription("parameter with String data type");
    p1.setType(ConfigurationParameter.TYPE_STRING);
    primitiveDesc.getMetaData().getConfigurationParameterDeclarations()
            .setConfigurationParameters(new ConfigurationParameter[] { p1 });
    primitiveDesc.getMetaData().getConfigurationParameterSettings().setParameterSettings(
            new NameValuePair[] { new NameValuePair_impl("StringParam", "Test1") });

    // create pool
    AnalysisEnginePool pool = new AnalysisEnginePool("taePool", 3, primitiveDesc);

    AnalysisEngine tae = pool.getAnalysisEngine();
    try {
      // check value of string param (TestAnnotator saves it in a static field)
      assertEquals("Test1", TestAnnotator.stringParamValue);

      // reconfigure
      tae.setConfigParameterValue("StringParam", "Test2");
      tae.reconfigure();

      //test again
      assertEquals("Test2", TestAnnotator.stringParamValue);

      // check pool metadata
      pool.getMetaData().setUUID(tae.getMetaData().getUUID());
      Assert.assertEquals(tae.getMetaData(), pool.getMetaData());
    } finally {
      pool.releaseAnalysisEngine(tae);
    }
  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}