Example #1
0
  /**
   * Gets a component associated with the given parameter name. First search the component in
   * property table, then try to get component by name from the manager, then creates component with
   * default properties.
   *
   * @param name the parameter name
   * @return the component associated with the name
   * @throws edu.cmu.sphinx.util.props.PropertyException if the component does not exist or is of
   *     the wrong type.
   */
  public Configurable getComponent(String name) throws PropertyException {
    S4PropWrapper s4PropWrapper = getProperty(name, S4Component.class);
    Configurable configurable = null;

    S4Component s4Component = (S4Component) s4PropWrapper.getAnnotation();
    Class<? extends Configurable> expectedType = s4Component.type();

    Object propVal = propValues.get(name);

    if (propVal != null && propVal instanceof Configurable) {
      return (Configurable) propVal;
    }

    if (propVal != null && propVal instanceof String) {
      PropertySheet ps = cm.getPropertySheet(flattenProp(name));
      if (ps != null) configurable = ps.getOwner();
      else
        throw new InternalConfigurationException(
            getInstanceName(), name, "component '" + flattenProp(name) + "' is missing");
    }

    if (configurable != null && !expectedType.isInstance(configurable))
      throw new InternalConfigurationException(
          getInstanceName(), name, "mismatch between annotation and component type");

    if (configurable != null) {
      propValues.put(name, configurable);
      return configurable;
    }

    configurable = getComponentFromAnnotation(name, s4Component);

    propValues.put(name, configurable);
    return configurable;
  }
Example #2
0
  /**
   * Returns the property names <code>name</code> which is still wrapped into the annotation
   * instance.
   *
   * @param name the name of the property
   * @param propertyClass the class of the property
   * @return the wrapper around property
   * @throws PropertyException if there is no such property
   */
  public S4PropWrapper getProperty(String name, Class<?> propertyClass) throws PropertyException {
    if (!propValues.containsKey(name))
      throw new InternalConfigurationException(
          getInstanceName(),
          name,
          "Unknown property '" + name + "' ! Make sure that you've annotated it.");

    S4PropWrapper s4PropWrapper = registeredProperties.get(name);

    if (s4PropWrapper == null) {
      throw new InternalConfigurationException(
          getInstanceName(),
          name,
          "Property is not an annotated property of " + getConfigurableClass());
    }

    try {
      propertyClass.cast(s4PropWrapper.getAnnotation());
    } catch (ClassCastException e) {
      throw new InternalConfigurationException(
          e,
          getInstanceName(),
          name,
          "Property annotation "
              + s4PropWrapper.getAnnotation()
              + " doesn't match the required type "
              + propertyClass.getName());
    }

    return s4PropWrapper;
  }
Example #3
0
  /**
   * Gets the value associated with this name
   *
   * @param name the name
   * @return the value
   * @throws edu.cmu.sphinx.util.props.PropertyException if the named property is not of this type
   */
  public int getInt(String name) throws PropertyException {
    S4PropWrapper s4PropWrapper = getProperty(name, S4Integer.class);
    S4Integer s4Integer = (S4Integer) s4PropWrapper.getAnnotation();

    if (propValues.get(name) == null) {
      boolean isDefDefined = !(s4Integer.defaultValue() == S4Integer.NOT_DEFINED);

      if (s4Integer.mandatory()) {
        if (!isDefDefined)
          throw new InternalConfigurationException(
              getInstanceName(), name, "mandatory property is not set!");
      } else if (!isDefDefined)
        throw new InternalConfigurationException(
            getInstanceName(), name, "no default value for non-mandatory property");

      propValues.put(name, s4Integer.defaultValue());
    }

    Object propObject = propValues.get(name);
    Integer propValue =
        propObject instanceof Integer ? (Integer) propObject : Integer.decode(flattenProp(name));

    int[] range = s4Integer.range();
    if (range.length != 2)
      throw new InternalConfigurationException(
          getInstanceName(),
          name,
          Arrays.toString(range) + " is not of expected range type, which is {minValue, maxValue)");

    if (propValue < range[0] || propValue > range[1])
      throw new InternalConfigurationException(
          getInstanceName(), name, " is not in range (" + Arrays.toString(range) + ')');

    return propValue;
  }
Example #4
0
  /**
   * Gets the value associated with this name
   *
   * @param name the name
   * @return the value
   */
  public String getString(String name) throws PropertyException {
    S4PropWrapper s4PropWrapper = getProperty(name, S4String.class);
    S4String s4String = ((S4String) s4PropWrapper.getAnnotation());

    if (propValues.get(name) == null) {
      boolean isDefDefined = !s4String.defaultValue().equals(S4String.NOT_DEFINED);

      if (s4String.mandatory()) {
        if (!isDefDefined)
          throw new InternalConfigurationException(
              getInstanceName(), name, "mandatory property is not set!");
      }
      propValues.put(name, isDefDefined ? s4String.defaultValue() : null);
    }

    String propValue = flattenProp(name);

    // Check range
    List<String> range = Arrays.asList(s4String.range());
    if (!range.isEmpty() && !range.contains(propValue))
      throw new InternalConfigurationException(
          getInstanceName(), name, " is not in range (" + range + ')');

    return propValue;
  }
Example #5
0
  /**
   * Gets the value associated with this name
   *
   * @param name the name
   * @return the value
   * @throws edu.cmu.sphinx.util.props.PropertyException if the named property is not of this type
   */
  public Boolean getBoolean(String name) throws PropertyException {
    S4PropWrapper s4PropWrapper = getProperty(name, S4Boolean.class);
    S4Boolean s4Boolean = (S4Boolean) s4PropWrapper.getAnnotation();

    if (propValues.get(name) == null) propValues.put(name, s4Boolean.defaultValue());

    Object propObject = propValues.get(name);
    Boolean propValue;

    if (propObject instanceof Boolean) propValue = (Boolean) propObject;
    else propValue = Boolean.valueOf(flattenProp(name));

    return propValue;
  }
Example #6
0
  /**
   * Returns the type of the given property.
   *
   * @param propName the name of the property
   * @return the type of the property
   */
  public PropertyType getType(String propName) {
    S4PropWrapper wrapper = registeredProperties.get(propName);
    if (wrapper == null) {
      throw new InternalConfigurationException(
          getInstanceName(), propName, " is not a valid property of" + getConfigurableClass());
    }

    Annotation annotation = wrapper.getAnnotation();
    if (annotation instanceof S4Component) return PropertyType.COMPONENT;
    else if (annotation instanceof S4ComponentList) return PropertyType.COMPONENT_LIST;
    else if (annotation instanceof S4Integer) return PropertyType.INT;
    else if (annotation instanceof S4Double) return PropertyType.DOUBLE;
    else if (annotation instanceof S4Boolean) return PropertyType.BOOLEAN;
    else if (annotation instanceof S4String) return PropertyType.STRING;
    else throw new RuntimeException("Unknown property type");
  }
Example #7
0
  /**
   * Gets the value associated with this name
   *
   * @param name the name
   * @return the value
   * @throws edu.cmu.sphinx.util.props.PropertyException if the named property is not of this type
   */
  public double getDouble(String name) throws PropertyException {
    S4PropWrapper s4PropWrapper = getProperty(name, S4Double.class);
    S4Double s4Double = (S4Double) s4PropWrapper.getAnnotation();

    if (propValues.get(name) == null) {
      boolean isDefDefined = !(s4Double.defaultValue() == S4Double.NOT_DEFINED);

      if (s4Double.mandatory()) {
        if (!isDefDefined)
          throw new InternalConfigurationException(
              getInstanceName(), name, "mandatory property is not set!");
      } else if (!isDefDefined)
        throw new InternalConfigurationException(
            getInstanceName(), name, "no default value for non-mandatory property");

      propValues.put(name, s4Double.defaultValue());
    }

    Object propObject = propValues.get(name);
    Double propValue;

    if (propObject instanceof Double) propValue = (Double) propObject;
    else if (propObject instanceof Number) propValue = ((Number) propObject).doubleValue();
    else propValue = Double.valueOf(flattenProp(name));

    double[] range = s4Double.range();
    if (range.length != 2)
      throw new InternalConfigurationException(
          getInstanceName(),
          name,
          Arrays.toString(range) + " is not of expected range type, which is {minValue, maxValue)");

    if (propValue < range[0] || propValue > range[1])
      throw new InternalConfigurationException(
          getInstanceName(), name, " is not in range (" + Arrays.toString(range) + ')');

    return propValue;
  }