Class SessionNode

java.lang.Object
edu.cmu.tetrad.session.SessionNode
All Implemented Interfaces:
Node, TetradSerializable, Serializable, Comparable<Node>

public class SessionNode extends Object implements Node

Represents a node in a session for a model in a particular class. The sets of possible model classes for this node are given in the constructors of the model classes for the node. Parents (also SessionNodes) may be added to this node provided some combination of the parents' model classes serves a partial argument set to some constructor of the one of this node's model classes. To put it slightly differently, parents can be added to this node one at a time, though at any step along the way it ought to be possible (perhaps by adding more parent nodes) to use the parent models to construct a model of one of the legal types for this node.> 0

To retrieve the list of classes for which models may be created, call the getConsistentModelClasses . To construct a model for a particular model choice, call createModel method for the desired class. If the model has a parameterizing object, this object may be passed in using the createParameterizedModel method. For parameterized models, the model object is treated simply as an additional parent to the model and therefore must appear as an argument to one of the constructors of the model.> 0

This node keeps track of its parents and its children and keeps these two sets of SessionNodes in sync.> 0

Author:
josephramsey
See Also:
  • Constructor Details

    • SessionNode

      public SessionNode(Class modelClass)
      Creates a new session node capable of implementing the given model class.
    • SessionNode

      public SessionNode(String boxType, String displayName, Class modelClass)
      Creates a new session node with the given name, capable of implementing the given model class.
      Parameters:
      boxType - The name of the box type--for instance, "Graph."
      displayName - The name of this particular session node. Any non-null string.
      modelClass - A single model class associated with this session node.
    • SessionNode

      public SessionNode(Class[] modelClasses)
      Creates a new session node with the given name capable of implementing the given model classes.
    • SessionNode

      public SessionNode(String boxType, String displayName, Class[] modelClasses)
      Creates a new session node with the given name capable of implementing the given model classes. When models are created, they will be of one of these classes. Reflection will be used to create the models by matching the models of the parent Session Nodes to constructor arguments of the class given as argument to the createModel method, which must itself be one of these model classes.
      Parameters:
      boxType - The name of the box type--for instance, "Graph."
      displayName - The name of this particular session node. Any non-null string.
      modelClasses - An array of model classes associated with this session node.
  • Method Details

    • serializableInstance

      public static SessionNode serializableInstance()
      Generates a simple exemplar of this class to test serialization.
    • addParent

      public boolean addParent(SessionNode parent)
      Adds a parent to this node provided the resulting set of parents taken together provides some combination of possible model classes that can be used as a constructor to some one of the model classes for this node.
    • isConsistentParent

      public boolean isConsistentParent(SessionNode parent)
    • isConsistentParent

      public boolean isConsistentParent(SessionNode parent, List<SessionNode> existingNodes)
    • addParent2

      public boolean addParent2(SessionNode parent)
      Same as addParent except tests if this has already been created. If so the user is asked whether to add parent and update parent's desendents or to cancel the operation.
    • removeParent

      public boolean removeParent(SessionNode parent)
      Removes a parent from the node.
    • getParents

      public Set<SessionNode> getParents()
      Returns:
      the set of parents.
    • getNumParents

      public int getNumParents()
      Returns:
      the number of parents.
    • addChild

      public boolean addChild(SessionNode child)
      Adds a child to the node, provided this node can be added as a parent to the child node.
    • containsChild

      public boolean containsChild(SessionNode child)
      Returns:
      true iff the given node is child of this node.
    • removeChild

      public boolean removeChild(SessionNode child)
      Removes a child from the node.
    • getChildren

      public Set<SessionNode> getChildren()
      Returns:
      the set of children.
    • getNumChildren

      public int getNumChildren()
      Returns:
      the number of children.
    • createModel

      public boolean createModel(boolean simulation)
      Creates a model, provided the class of the model can be uniquely determined without any further hints. If a model was created previously, the previous model class is used. If there is only one consistent model class, than that model class is used. Otherwise, an exception is thrown.
      Returns:
      true iff this node contains a model when this method completes.
      Throws:
      RuntimeException - if the model could not be created.
    • createModel

      public void createModel(Class modelClass, boolean simulation) throws Exception
      Creates a model of the given class using models of the parent SessionNodes as constructor arguments. If no appropriate constructor is available, no model is created, and the method returns false. If the attempt to construct a model using reflection fails, the stack trace is printed to System.err and an IllegalArgumentException is thrown. t
      Throws:
      RuntimeException - if the attempt to construct the model throws either an IllegalAccessException, an InstantiationException, or an InvocationTargetException. In this case, a stack trace is printed to System.err.
      Exception
    • getLoggerConfig

      public TetradLoggerConfig getLoggerConfig()
    • getLoggerConfig

      public TetradLoggerConfig getLoggerConfig(Class modelClass)
    • destroyModel

      public void destroyModel()
      Sets the model to null. This step must be performed before a new model can be created.
    • forgetOldModel

      public void forgetOldModel()
      Forgets the old model so that it can't be used to recapture parameter values.
    • getModelClasses

      public Class[] getModelClasses()
      Returns:
      the class of the model.
    • setModelClasses

      public final void setModelClasses(Class[] modelClasses)
      Sets the model classes to the new array of model classes.
    • getConsistentModelClasses

      public Class[] getConsistentModelClasses(boolean exact)
      Parameters:
      exact -
      Returns:
      those model classes among the possible model classes that are at least consistent with the model class of the parent session nodes, in the sense that possibly with the addition of more parent session nodes, and assuming that the models of the parent session nodes are non-null, it is possible to construct a model in one of the legal classes for this node using the parent models as arguments to some constructor in that class.
    • getModel

      public SessionModel getModel()
      Returns:
      the model, or null if no model has been created yet.
    • getLastModelClass

      public Class getLastModelClass()
      Returns:
      the class of the last model that was created, or null if no model has been created yet.
    • addSessionListener

      public void addSessionListener(SessionListener l)
      Adds a session listener.
    • removeSessionListener

      public void removeSessionListener(SessionListener l)
      Removes a session listener.
    • isFreshlyCreated

      public boolean isFreshlyCreated()
      Returns:
      true iff this node is in a freshly created state. A node that is in a freshly created state has no model, no parents, no children, and no listeners. It does, however, have the array of possible model classes that it was constructed with, and it may or may not have a name.
    • resetToFreshlyCreated

      public void resetToFreshlyCreated()
      Resets this sesion node to the state it was in when first constructed. Removes any parents or children, destroys the model if there is one, and resets all listeners. Fires an event for each action taken.
    • restrictConnectionsToList

      public void restrictConnectionsToList(List sessionNodes)
      Removes any parents or children of the node that are not in the given list.
    • restrictListenersToSessionNodes

      public void restrictListenersToSessionNodes()
      Removes any listeners that are not SessionNodes.
    • isStructurallyIdentical

      public boolean isStructurallyIdentical(SessionNode node)

      Tests whether two session nodes that are not necessarily object identical are nevertheless identical in structure. This method should not be made to override equals since equals is used in the Collections API to determine, for example, containment in an ArrayList, and the sense of equality needed for that is object identity. Nevertheless, for certain other purposes, such as checking serialization, a looser sense of structural identity is helpful.> 0

      Two SessionNodes are structurally identical just in case their possible model classes are equal, the parameter type arrays used to construct their models are equal, their models themselves are equal, and the model classes of the parent and child SessionNodes are equal. We dare not check equality of parents and children outright for fear of circularity.> 0

    • getBoxType

      public String getBoxType()
      Gets the (optional) name of this node. May be null.
    • setBoxType

      public final void setBoxType(String boxType)
      Sets the (optional) name for this node. May be null.
    • putParam

      public void putParam(Class modelClass, Parameters param)
      Sets the parameter object for the given model class to the given object.
    • getParam

      public Parameters getParam(Class modelClass)
      Gets the parameter object for the givem model class.
    • removeParam

      public void removeParam(Class modelClass)
      Removes the parameter object for the given model class.
    • getModelConstructorArguments

      public Object[] getModelConstructorArguments(Class modelClass)
    • getName

      public String getName()
      Description copied from interface: Node
      Returns the name of this node.
      Specified by:
      getName in interface Node
      Returns:
      the name of the node.
    • setName

      public void setName(String name)
      Description copied from interface: Node
      Sets the name of this node.
      Specified by:
      setName in interface Node
      Parameters:
      name - the name of this node.
    • getNodeType

      public NodeType getNodeType()
      Description copied from interface: Node
      Returns the node type for this node.
      Specified by:
      getNodeType in interface Node
      Returns:
      the node type for this node.
    • setNodeType

      public void setNodeType(NodeType nodeType)
      Description copied from interface: Node
      Sets the node type for this node.
      Specified by:
      setNodeType in interface Node
      Parameters:
      nodeType - the node type for this node.
    • toString

      public String toString()
      Prints out the name of the session node.
      Specified by:
      toString in interface Node
      Overrides:
      toString in class Object
      Returns:
      a string representation of the node.
    • getCenterX

      public int getCenterX()
      Description copied from interface: Node
      Returns the x coordinate of the center of this node.
      Specified by:
      getCenterX in interface Node
      Returns:
      the x coordinate of the center of the node.
    • setCenterX

      public void setCenterX(int centerX)
      Description copied from interface: Node
      Sets the x coordinate of the center of this node.
      Specified by:
      setCenterX in interface Node
      Parameters:
      centerX - This coordinate.
    • getCenterY

      public int getCenterY()
      Description copied from interface: Node
      Returns the y coordinate of the center of this node.
      Specified by:
      getCenterY in interface Node
      Returns:
      the y coordinate of the center of the node.
    • setCenterY

      public void setCenterY(int centerY)
      Description copied from interface: Node
      Sets the y coordinate of the center of this node.
      Specified by:
      setCenterY in interface Node
      Parameters:
      centerY - This coordinate.
    • setCenter

      public void setCenter(int centerX, int centerY)
      Description copied from interface: Node
      Sets the (x, y) coordinates of the center of this node.
      Specified by:
      setCenter in interface Node
      Parameters:
      centerX - The x coordinate.
      centerY - The y coordinate.
    • addPropertyChangeListener

      public void addPropertyChangeListener(PropertyChangeListener l)
      Description copied from interface: Node
      Adds a property change listener.
      Specified by:
      addPropertyChangeListener in interface Node
      Parameters:
      l - This listener.
    • like

      public Node like(String name)
      Description copied from interface: Node
      Creates a new node of the same type as this one with the given name.
      Specified by:
      like in interface Node
      Parameters:
      name - the name of the new node.
      Returns:
      the new node.
    • compareTo

      public int compareTo(Node node)
      Description copied from interface: Node
      Returns the hashcode for this node.
      Specified by:
      compareTo in interface Comparable<Node>
      Specified by:
      compareTo in interface Node
      Parameters:
      node - the object to be compared.
      Returns:
      the hashcode for this node.
    • existsParameterizedConstructor

      public boolean existsParameterizedConstructor(Class modelClass)
    • getRepetition

      public int getRepetition()
    • setRepetition

      public void setRepetition(int repetition)
    • useClonedModel

      public boolean useClonedModel()
      Returns:
      true if the cloning operation was successful, false if not. If the cloning operation was not successful, the model will not have been altered.
    • forgetSavedModel

      public void forgetSavedModel()
    • restoreOriginalModel

      public void restoreOriginalModel()
    • isConsistentModelClass

      public boolean isConsistentModelClass(Class<Type1> modelClass, List nodes, boolean exact)
      Determines whether a given model class is consistent with the models contained in the given List of nodes, in the sense that the model class has a constructor that can take the models of the nodes as arguments.
    • existsConstructor

      public boolean existsConstructor(Class modelClass, Class[] argumentTypes)

      Tests whether the model class has an argument that takes all of the given argument classes (or more) as arguments. The purpose of this is to allow parent nodes to be added one at a time to this node, whether or not any of the nodes in question have non-null models.> 0

    • getAssignableClass

      public Class getAssignableClass(List classes, Class clazz)

      Returns the first class c in classes that clazz is assignable to.> 0

    • assignParameters

      public Object[] assignParameters(Class[] parameterTypes, List objects) throws RuntimeException

      Returns the objects in the List as an array in the same order as the parameter types. If an exact match cannot be found, throws a RuntimeException with an appropriate message.

      Parameters:
      parameterTypes - a list of classes; if any of them is null, a NullPointerException will be thrown.
      objects - a List of objects. (The nulls will be automatically thrown out for this one.)
      Throws:
      RuntimeException
    • assignClasses

      public boolean assignClasses(Class[] constructorTypes, Class[] modelTypes, boolean exact, List<SessionNode> existingNodes) throws RuntimeException
      Throws:
      RuntimeException
    • getValueCombination

      public int[] getValueCombination(int index, int[] numValues)
      Returns:
      an array with a combination of particular values for variables given an array indicating the number of values for each variable.
    • getProduct

      public int getProduct(int[] arr)
      Returns:
      the product of the entries in the given array.
    • setNextEdgeAddAllowed

      public void setNextEdgeAddAllowed(boolean nextEdgeAddAllowed)
      True iff the next edge should not be added. (Included for GUI user control.) Reset to true every time an edge is added; edge adds must be disallowed individually. To disallow the next edge add, set to false.
    • getDisplayName

      public String getDisplayName()
    • setDisplayName

      public final void setDisplayName(String displayName)
    • getParameters

      public Parameters getParameters()
    • getNodeVariableType

      public NodeVariableType getNodeVariableType()
      Description copied from interface: Node
      Returns the node shape for this node.
      Specified by:
      getNodeVariableType in interface Node
      Returns:
      the intervention type
    • setNodeVariableType

      public void setNodeVariableType(NodeVariableType nodeVariableType)
      Description copied from interface: Node
      Sets the type (domain, interventional status, interventional value..) for this node variable
      Specified by:
      setNodeVariableType in interface Node
      Parameters:
      nodeVariableType - the type (domain, interventional status, interventional value..) for this node variable
    • getAllAttributes

      public Map<String,Object> getAllAttributes()
      Specified by:
      getAllAttributes in interface Node
    • getAttribute

      public Object getAttribute(String key)
      Specified by:
      getAttribute in interface Node
    • removeAttribute

      public void removeAttribute(String key)
      Specified by:
      removeAttribute in interface Node
    • addAttribute

      public void addAttribute(String key, Object value)
      Specified by:
      addAttribute in interface Node