Class Variable

  • All Implemented Interfaces:
    Cloneable, Comparable<Variable>

    public final class Variable
    extends Object
    implements Comparable<Variable>, Cloneable
    Represents a discrete or continuous random variable. Note that variable names must be unique per network, and are case sensitive.

    A Node in a Bayesian network can contain one or more variables.

    Although a discrete variable can contain a different number of states, a continuous variable always contains a single state.

    • Constructor Detail

      • Variable

        public Variable()
        Initializes a new instance of the Variable class, with VariableValueType discrete and zero states.
      • Variable

        public Variable​(String name)
        Initializes a new instance of the Variable class, with VariableValueType discrete, zero states, and the specified name.
        Parameters:
        name - The name of the variable. Can be null.
      • Variable

        public Variable​(String name,
                        VariableValueType valueType,
                        VariableKind kind)
        Initializes a new instance of the Variable class with the specified name, kind and value type. If discrete, no states are added.
        Parameters:
        name - The name of the variable. Can be null.
        valueType - The value type, e.g. continuous or discrete.
        kind - The kind of Variable, e.g. Probability, Decision or Utility.
      • Variable

        public Variable​(String name,
                        VariableValueType valueType)
        Initializes a new instance of the Variable class with the specified name and value type. If discrete, no states are added.
        Parameters:
        name - The name of the variable. Can be null.
        valueType - The value type, e.g. continuous or discrete.
      • Variable

        public Variable​(String name,
                        int states)
        Initializes a new instance of the Variable class, with VariableValueType discrete and the specified [name] and adds the number of states specified in [states].
        Parameters:
        name - The name to give the Variable, which can be null or empty.
        states - The number of states to add to the Variable. States will be given default names.
        Throws:
        IllegalArgumentException - Raised when the [states] parameter is less than zero.
      • Variable

        public Variable​(String name,
                        String[] states)
        Initializes a new instance of the Variable class, with VariableValueType discrete and the specified name and adds the states specified in [states].
        Parameters:
        name - The name of the node and variable.
        states - The states to add to the associated Variable, specified by their names.
        Throws:
        NullPointerException - Raised when [states] is null.
        IllegalArgumentException - Raised when the names in [states] are not unique, or are null or empty.
      • Variable

        public Variable​(String name,
                        State... states)
        Initializes a new instance of the Variable class, with VariableValueType discrete and the specified name and adds the states specified in [states].
        Parameters:
        name - The name of the node and variable.
        states - The states to add to the associated Variable.
        Throws:
        NullPointerException - Raised when [states] is null.
        IllegalArgumentException - Raised when the names in [states] are not unique, or are null or empty.
    • Method Detail

      • findStateByValue

        public State findStateByValue​(Object value)
        Finds a state based on a state value. E.g. finds the state who's interval contains the supplied value, or who's integer state value matches the supplied integer.
        Parameters:
        value - The value to test against state values. Type will depend on StateValueType. e.g. pass a double if the state value type is a double interval.
        Returns:
        The matching state or null.
      • getStateValueType

        public StateValueType getStateValueType()
        Gets the type of value that states belonging to this variable can represent. For example an interval.
      • setStateValueType

        public void setStateValueType​(StateValueType value)
        Sets the type of value that states belonging to this variable can represent. For example an interval.
      • copy

        public Variable copy()
        Copies this instance.
        Returns:
        A new Variable instance.
      • getCustomProperties

        public CustomPropertyCollection getCustomProperties()
        Gets custom properties associated with this instance. Custom properties allow storage of custom information, which will be saved with the network.
      • getDescription

        public String getDescription()
        An optional description for the variable.
      • setDescription

        public void setDescription​(String value)
        An optional description for the variable.
      • getFunction

        public QueryExpression getFunction()
        Gets an expression, which is evaluated during a query, and can be based on other queries and expressions.
      • setFunction

        public void setFunction​(QueryExpression value)
        Sets an expression, which is evaluated during a query, and can be based on other queries and expressions.
      • getValueType

        public VariableValueType getValueType()
        Gets the variable's value type, e.g. continuous or discrete.
        Returns:
        The variable's value type.
      • getKind

        public VariableKind getKind()
        Gets the kind of variable, such as Probability, Decision, Utility or Function.
      • getStates

        public StateCollection getStates()
        Returns the collection of states belonging to the variable. Continuous variables always have a single state.
        See Also:
        State
      • toString

        public String toString()
        Returns the name of the variable, or an empty string if the name is null.
        Overrides:
        toString in class Object
        Returns:
        A String containing the name of the variable which is empty if the name is null.
      • getNode

        public Node getNode()
        Gets the Node this instance belongs to, if any.
        Returns:
        The node the instance belongs to, if any.
      • getExpressionAlias

        public String getExpressionAlias()
        Gets a c-style name for a variable that can be used as an alias in expressions.
      • setExpressionAlias

        public void setExpressionAlias​(String value)
        Sets a c-style name for a variable that can be used as an alias in expressions.
      • getName

        public String getName()
        Gets the name of the variable. When a variable is added to a network, its name must be unique. Comparisons are case sensitive.
      • setName

        public void setName​(String value)
        Sets the name of the variable. When a variable is added to a network, its name must be unique. Comparisons are case sensitive.
      • getIndex

        public int getIndex()
        The Index of this instance in the collection of variables belonging to a network, or -1 if the variable does not belong to a node and hence a network.