Package com.bayesserver
Class Variable
- java.lang.Object
-
- com.bayesserver.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
Nodein 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 Summary
Constructors Constructor Description Variable()Initializes a new instance of theVariableclass, withVariableValueTypediscrete and zero states.Variable(String name)Initializes a new instance of theVariableclass, withVariableValueTypediscrete, zero states, and the specified name.Variable(String name, int states)Initializes a new instance of theVariableclass, withVariableValueTypediscrete and the specified [name] and adds the number of states specified in [states].Variable(String name, State... states)Initializes a new instance of theVariableclass, withVariableValueTypediscrete and the specified name and adds the states specified in [states].Variable(String name, VariableValueType valueType)Initializes a new instance of theVariableclass with the specified name and value type.Variable(String name, VariableValueType valueType, VariableKind kind)Initializes a new instance of theVariableclass with the specified name, kind and value type.Variable(String name, String[] states)Initializes a new instance of theVariableclass, withVariableValueTypediscrete and the specified name and adds the states specified in [states].
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intcompareTo(Variable other)Variablecopy()Copies this instance.StatefindStateByValue(Object value)Finds a state based on a state value.CustomPropertyCollectiongetCustomProperties()Gets custom properties associated with this instance.StringgetDescription()An optional description for the variable.StringgetExpressionAlias()Gets a c-style name for a variable that can be used as an alias in expressions.QueryExpressiongetFunction()Gets an expression, which is evaluated during a query, and can be based on other queries and expressions.intgetIndex()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.VariableKindgetKind()Gets the kind of variable, such as Probability, Decision, Utility or Function.StringgetName()Gets the name of the variable.NodegetNode()Gets theNodethis instance belongs to, if any.StateCollectiongetStates()Returns the collection of states belonging to the variable.StateValueTypegetStateValueType()Gets the type of value that states belonging to this variable can represent.VariableValueTypegetValueType()Gets the variable's value type, e.g.voidsetDescription(String value)An optional description for the variable.voidsetExpressionAlias(String value)Sets a c-style name for a variable that can be used as an alias in expressions.voidsetFunction(QueryExpression value)Sets an expression, which is evaluated during a query, and can be based on other queries and expressions.voidsetName(String value)Sets the name of the variable.voidsetStateValueType(StateValueType value)Sets the type of value that states belonging to this variable can represent.StringtoString()Returns the name of the variable, or an empty string if the name is null.
-
-
-
Constructor Detail
-
Variable
public Variable()
Initializes a new instance of theVariableclass, withVariableValueTypediscrete and zero states.
-
Variable
public Variable(String name)
Initializes a new instance of theVariableclass, withVariableValueTypediscrete, 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 theVariableclass 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 theVariableclass 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 theVariableclass, withVariableValueTypediscrete 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 theVariableclass, withVariableValueTypediscrete 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 associatedVariable, 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 theVariableclass, withVariableValueTypediscrete 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 associatedVariable.- 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.
-
compareTo
public int compareTo(Variable other)
- Specified by:
compareToin interfaceComparable<Variable>
-
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.
-
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.
-
getNode
public Node getNode()
Gets theNodethis 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.
-
-