blog
Class AbstractPartialWorld

java.lang.Object
  extended by blog.AbstractPartialWorld
All Implemented Interfaces:
PartialWorld
Direct Known Subclasses:
DefaultPartialWorld, PartialWorld.EmptyPartialWorld, PartialWorldDiff

public abstract class AbstractPartialWorld
extends java.lang.Object
implements PartialWorld

An implementation of the PartialWorld interface that just requires concrete subclasses to initialize some protected variables.


Nested Class Summary
 
Nested classes/interfaces inherited from interface blog.PartialWorld
PartialWorld.EmptyPartialWorld
 
Field Summary
protected  java.util.Map assertedIdToPOPApp
          Map from asserted ObjectIdentifiers to the NumberVars that they satisfy.
protected  java.util.Map basicVarToValue
          Map from instantiated basic variables to their values.
protected  DGraph bayesNet
          Bayes net containing instantiated basic variables, origin variables, and those derived variables that have been explicitly added.
protected  java.util.Map commIdToPOPApp
          Map from common ground ObjectIdentifiers (including asserted ones) to the NumberVars that they satisfy.
protected  java.util.Map derivedVarToValue
          Map from derived variables in the Bayes net to their values.
protected  java.util.Set dirtyVars
          Variables that have been added or removed, or have had their values changed, since the last call to updateParentsAndProbs.
protected  java.util.Set idTypes
           
protected  java.util.List listeners
           
protected  MultiMap objToUsesAsArg
          Map from objects to the instantiated BasicVars that use them as arguments.
protected  MultiMap objToUsesAsValue
          Map from objects to the instantiated BasicVars that have them as values.
protected  IndexedMultiMap popAppToAssertedIds
          Map from NumberVars to IndexedSets of the ObjectIdentifiers that are asserted to satify them.
protected  IndexedMultiMap popAppToCommIds
          Map from NumberVars to IndexedSets of the common ground ObjectIdentifiers (including asserted ones) that satisfy them.
protected  java.util.Map varToLogProb
          Map from instantiated VarWithDistrib objects to Double values representing their log probabilities given their parents.
protected  MapWithPreimages varToUninstParent
          MapWithPreimages from BayesNetVars to their first uninstantiated parents.
 
Fields inherited from interface blog.PartialWorld
EMPTY_INST, UNDET
 
Constructor Summary
AbstractPartialWorld(java.util.Set idTypes)
          Creates a new partial world.
 
Method Summary
 boolean addDerivedVar(DerivedVar var)
          Adds the given derived variable to this world's Bayes net, and sets its parents appropriately.
 ObjectIdentifier addIdentifierForPOPApp(NumberVar popApp)
          Returns a new object identifier that is asserted to satisfy the given POP application in this world, and to be distinct from all other object identifiers in this world's common ground.
 void addListener(WorldListener listener)
          Adds the given listener to a list of listeners to be notified when changes are made to this partial world.
 java.util.Map assertedIdToPOPAppMap()
          Map from asserted ObjectIdentifiers to the POP applications (NumberVars) that they satisfy.
 void assertIdentifier(ObjectIdentifier id)
          Asserts that the given object identifier satisfies the POP application that it satisfies in this world's common ground.
 void assertIdentifier(ObjectIdentifier id, NumberVar newPOPApp)
          Asserts that the given object identifier satisfies the given POP application.
 java.util.Map basicVarToValueMap()
          Map from instantiated BasicVars to their values.
 void cloneFields(AbstractPartialWorld newWorld)
          Replicates all fields into the fields of newWorld.
 java.util.Map derivedVarToValueMap()
          Map from DerivedVars that are included in this world's Bayes net to their values.
 java.util.Set getAssertedIdentifiers()
          Returns the set of object identifiers that are asserted to exist in this partial world.
 IndexedSet getAssertedIdsForPOPApp(NumberVar popApp)
          Returns the set of object identifiers that are asserted to satisfy the given POP application in this partial world.
 DGraph getBayesNet()
          Returns a directed graph where the nodes are variables, and there is an edge from variable X to variable Y if Y depends directly on X given the context represented by this partial world.
 java.util.Set getDerivedVars()
          Returns the set of derived variables that are included in this world's Bayes net.
 java.util.Set getIdTypes()
          Returns the set of types that are represented with object identifiers in this partial world.
 java.util.Set getInstantiatedVars()
          Returns an unmodifiable Set representing the set of basic variables that are instantiated in this world.
 java.util.Set getInverseTuples(RandomFunction func, java.lang.Object val)
          Returns the set of argument lists that the function func maps to the value val.
 double getLogProbOfValue(BayesNetVar var)
          If var is an instantiated basic variable, this method returns the log probability of var having the value that it has in this world, given that its parents have the values specified in this world.
 NumberVar getPOPAppSatisfied(java.lang.Object obj)
          Returns the NumberVar (i.e., POP and generating objects) such that the given object satisfies that POP applied to those generating objects in this world.
 double getProbOfValue(BayesNetVar var)
          Uses PartialWorld.getLogProbOfValue(BayesNetVar) in order to return the probability of a value given its parents.
 ObjectSet getSatisfiers(NumberVar popApp)
          Returns the set of objects that satisfy the given POP application in this world.
 java.lang.Object getValue(BayesNetVar var)
          Returns the value of the given variable in this partial world, or null if the given variable is an uninstantiated basic variable.
 java.util.Set getVarsWithArg(java.lang.Object arg)
          Returns the set of instantiated basic variables that have the given object as an argument.
 java.util.Set getVarsWithValue(java.lang.Object value)
          Returns the set of basic variables that have the given object as their value in this world.
 boolean isOverloaded(NumberVar popApp)
          Returns true if the given POP application is overloaded: that is, the number of identifiers that are asserted to satisfy it is greater than the value of the corresponding number variable, or some identifiers are asserted to satisfy it and its number variable is not instantiated.
 MultiMap objToUsesAsArgMap()
          MultiMap from objects to the instantiated BasicVars that have them as arguments.
 MultiMap objToUsesAsValueMap()
          MultiMap from objects to the instantiated BasicVars that have them as values.
 IndexedMultiMap popAppToAssertedIdsMap()
          Map from POP applications (NumberVars) to IndexedSets of the ObjectIdentifiers that are asserted to satisfy them.
 void print(java.io.PrintStream s)
          Prints this partial world to the given stream.
 boolean removeDerivedVar(DerivedVar var)
          Removes the given derived variable from this world's Bayes net.
 void removeIdentifier(ObjectIdentifier id)
          Removes the given object identifier from the set of asserted identifiers in this partial world, and from this world's common ground.
 void removeListener(WorldListener listener)
          Removes the given listener from the list of objects to be notified when changes are made to this partial world.
 void setValue(BasicVar var, java.lang.Object value)
          Instantiates the given variable to the given value (replacing any previous value), or uninstantiates the variable if the given value is null.
 java.lang.String toString()
           
 void truncateList(RandomFunction f, java.lang.Object[] initialArgs, int len)
          Uninstantiates the variables for the values of f on all tuples of arguments of the form (arg1, ..., argK, i) where (arg1, ..., argK) are given by initialArgs and i is greater than or equal to len.
 void truncateNumberList(POP pop, java.lang.Object[] initialArgs, int len)
          Uninstantiates the number variables for pop on all tuples of arguments of the form (arg1, ..., argK, i) where (arg1, ..., argK) are given by initialArgs and i is greater than or equal to len.
 void updateBayesNet(DGraph newBayesNet, MapWithPreimages newVarToUninstParent, java.util.Map newVarLogProbs, java.util.Map newDerivedVarValues)
          Provides the Bayes net, the mapping from variables to uninstantiated parents, the mapping from basic variables to log probabilities, and the mapping from derived variables to values for this partial world.
protected  void updateParentsAndProbs()
          Ensure that parent sets, basic variable log probabilities, and derived variable values are all up to date.
 java.util.Map varToLogProbMap()
          Map from instantiated VarWithDistrib objects to Double values representing their log probabilities given their parents.
 MapWithPreimages varToUninstParentMap()
          MapWithPreimages from BayesNetVars to their first uninstantiated parents.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

basicVarToValue

protected java.util.Map basicVarToValue
Map from instantiated basic variables to their values.


objToUsesAsValue

protected MultiMap objToUsesAsValue
Map from objects to the instantiated BasicVars that have them as values.


objToUsesAsArg

protected MultiMap objToUsesAsArg
Map from objects to the instantiated BasicVars that use them as arguments.


assertedIdToPOPApp

protected java.util.Map assertedIdToPOPApp
Map from asserted ObjectIdentifiers to the NumberVars that they satisfy.


popAppToAssertedIds

protected IndexedMultiMap popAppToAssertedIds
Map from NumberVars to IndexedSets of the ObjectIdentifiers that are asserted to satify them.


commIdToPOPApp

protected java.util.Map commIdToPOPApp
Map from common ground ObjectIdentifiers (including asserted ones) to the NumberVars that they satisfy.


popAppToCommIds

protected IndexedMultiMap popAppToCommIds
Map from NumberVars to IndexedSets of the common ground ObjectIdentifiers (including asserted ones) that satisfy them.


bayesNet

protected DGraph bayesNet
Bayes net containing instantiated basic variables, origin variables, and those derived variables that have been explicitly added.


varToUninstParent

protected MapWithPreimages varToUninstParent
MapWithPreimages from BayesNetVars to their first uninstantiated parents. A variable is included as a key in this map only if it is not supported in this partial world, which means it has an uninstantiated parent.


varToLogProb

protected java.util.Map varToLogProb
Map from instantiated VarWithDistrib objects to Double values representing their log probabilities given their parents. The value is null if a variable is not supported in this world.


derivedVarToValue

protected java.util.Map derivedVarToValue
Map from derived variables in the Bayes net to their values. Even if the values in this map are out of date, the key set is the set of derived variables in the Bayes net.


dirtyVars

protected java.util.Set dirtyVars
Variables that have been added or removed, or have had their values changed, since the last call to updateParentsAndProbs.


listeners

protected java.util.List listeners

idTypes

protected java.util.Set idTypes
Constructor Detail

AbstractPartialWorld

public AbstractPartialWorld(java.util.Set idTypes)
Creates a new partial world. Identifiers will be used to represent the user-defined types in the set idTypes.

Parameters:
idTypes - Set of Type objects
Method Detail

getInstantiatedVars

public java.util.Set getInstantiatedVars()
Description copied from interface: PartialWorld
Returns an unmodifiable Set representing the set of basic variables that are instantiated in this world.

Specified by:
getInstantiatedVars in interface PartialWorld
Returns:
unmodifiable Set of BasicVar objects

getValue

public java.lang.Object getValue(BayesNetVar var)
Description copied from interface: PartialWorld
Returns the value of the given variable in this partial world, or null if the given variable is an uninstantiated basic variable. Note that this method returns null on all basic variables that are not instantiated in this world, even if the value of the variable is determined because its arguments cannot exist.

Specified by:
getValue in interface PartialWorld

setValue

public void setValue(BasicVar var,
                     java.lang.Object value)
Description copied from interface: PartialWorld
Instantiates the given variable to the given value (replacing any previous value), or uninstantiates the variable if the given value is null. This method instantiates the variable even if some of its arguments are concrete non-guaranteed objects that do not necessarily exist in this world.

If the variable is a number variable and the given value is less than its old value, then all variables defined on concrete non-guaranteed objects that no longer exist are uninstantiated. Variables defined on object identifiers that satisfy this number variable are not affected (we don't know which identifiers to remove). We also don't do anything about variables whose values no longer exist.

Specified by:
setValue in interface PartialWorld

truncateList

public void truncateList(RandomFunction f,
                         java.lang.Object[] initialArgs,
                         int len)
Description copied from interface: PartialWorld
Uninstantiates the variables for the values of f on all tuples of arguments of the form (arg1, ..., argK, i) where (arg1, ..., argK) are given by initialArgs and i is greater than or equal to len. Assumes that in the current world, there are no instantiated variables beyond the first uninstantiated variable in this sequence.

Specified by:
truncateList in interface PartialWorld

truncateNumberList

public void truncateNumberList(POP pop,
                               java.lang.Object[] initialArgs,
                               int len)
Description copied from interface: PartialWorld
Uninstantiates the number variables for pop on all tuples of arguments of the form (arg1, ..., argK, i) where (arg1, ..., argK) are given by initialArgs and i is greater than or equal to len. Assumes that in the current world, there are no instantiated variables beyond the first uninstantiated variable in this sequence.

Specified by:
truncateNumberList in interface PartialWorld

getVarsWithValue

public java.util.Set getVarsWithValue(java.lang.Object value)
Description copied from interface: PartialWorld
Returns the set of basic variables that have the given object as their value in this world.

Specified by:
getVarsWithValue in interface PartialWorld
Returns:
unmodifiable Set of BasicVar

getVarsWithArg

public java.util.Set getVarsWithArg(java.lang.Object arg)
Description copied from interface: PartialWorld
Returns the set of instantiated basic variables that have the given object as an argument.

Specified by:
getVarsWithArg in interface PartialWorld
Returns:
unmodifiable Set of BasicVar

getInverseTuples

public java.util.Set getInverseTuples(RandomFunction func,
                                      java.lang.Object val)
Description copied from interface: PartialWorld
Returns the set of argument lists that the function func maps to the value val. This method requires func to be a random function, since the values of non-random and origin functions may not be represented explicitly.

Specified by:
getInverseTuples in interface PartialWorld
Returns:
Set of List objects representing argument lists

getLogProbOfValue

public double getLogProbOfValue(BayesNetVar var)
Description copied from interface: PartialWorld
If var is an instantiated basic variable, this method returns the log probability of var having the value that it has in this world, given that its parents have the values specified in this world. Otherwise returns 0 (log of 1), because uninstantiated variables and non-basic variables do not contribute to the joint distribution. This method yields a fatal error if the world is not complete enough to determine the given variable's distribution.

Specified by:
getLogProbOfValue in interface PartialWorld

getProbOfValue

public double getProbOfValue(BayesNetVar var)
Description copied from interface: PartialWorld
Uses PartialWorld.getLogProbOfValue(BayesNetVar) in order to return the probability of a value given its parents.

Specified by:
getProbOfValue in interface PartialWorld

getSatisfiers

public ObjectSet getSatisfiers(NumberVar popApp)
Description copied from interface: PartialWorld
Returns the set of objects that satisfy the given POP application in this world. The objects may be represented as concrete objects or identifiers, depending on the way this PartialWorld implementation handles objects of the relevant type.

The set returned by this method will remain correct if new basic random variables are instantiated or new object identifiers are added (to this world's common ground or its set of asserted identifiers). The set may not remain correct if already-instantiated random variables are changed, or if object identifiers are moved from one number variable to another or removed from the world.

Specified by:
getSatisfiers in interface PartialWorld

getPOPAppSatisfied

public NumberVar getPOPAppSatisfied(java.lang.Object obj)
Description copied from interface: PartialWorld
Returns the NumberVar (i.e., POP and generating objects) such that the given object satisfies that POP applied to those generating objects in this world. Returns null if the given object does not satisfy any POP application (this includes the case where obj is an object identifier that is not valid in this world).

Specified by:
getPOPAppSatisfied in interface PartialWorld

getIdTypes

public java.util.Set getIdTypes()
Description copied from interface: PartialWorld
Returns the set of types that are represented with object identifiers in this partial world.

Specified by:
getIdTypes in interface PartialWorld
Returns:
unmodifiable Set of Type

getAssertedIdentifiers

public java.util.Set getAssertedIdentifiers()
Description copied from interface: PartialWorld
Returns the set of object identifiers that are asserted to exist in this partial world.

Specified by:
getAssertedIdentifiers in interface PartialWorld
Returns:
unmodifiable Set of ObjectIdentifier

getAssertedIdsForPOPApp

public IndexedSet getAssertedIdsForPOPApp(NumberVar popApp)
Description copied from interface: PartialWorld
Returns the set of object identifiers that are asserted to satisfy the given POP application in this partial world. The behavior of this method does not depend on whether the number variable for this POP application is instantiated or not.

Specified by:
getAssertedIdsForPOPApp in interface PartialWorld
Returns:
unmodifiable IndexedSet of ObjectIdentifier

assertIdentifier

public void assertIdentifier(ObjectIdentifier id,
                             NumberVar newPOPApp)
Description copied from interface: PartialWorld
Asserts that the given object identifier satisfies the given POP application. The given identifier does not need to be in this world's common ground already; if it is, any previous assertion about it is removed. The assertion is made even if the corresponding number variable is not instantiated, and even if adding this identifier will overload that number variable.

Specified by:
assertIdentifier in interface PartialWorld

assertIdentifier

public void assertIdentifier(ObjectIdentifier id)
Description copied from interface: PartialWorld
Asserts that the given object identifier satisfies the POP application that it satisfies in this world's common ground. That is, this method adds id to the set of asserted identifiers in this world.

Specified by:
assertIdentifier in interface PartialWorld

addIdentifierForPOPApp

public ObjectIdentifier addIdentifierForPOPApp(NumberVar popApp)
Description copied from interface: PartialWorld
Returns a new object identifier that is asserted to satisfy the given POP application in this world, and to be distinct from all other object identifiers in this world's common ground. The identifier is added even if the corresponding number variable is not instantiated, and even if adding this identifier will overload that number variable.

Specified by:
addIdentifierForPOPApp in interface PartialWorld

removeIdentifier

public void removeIdentifier(ObjectIdentifier id)
Description copied from interface: PartialWorld
Removes the given object identifier from the set of asserted identifiers in this partial world, and from this world's common ground. Automatically uninstantiates all variables that have this identifier as an argument (variables that have this identifier as a value are unaffected; the client should give new values to all such variables). Has no effect if the given identifier is not currently asserted or in the common ground.

Specified by:
removeIdentifier in interface PartialWorld

isOverloaded

public boolean isOverloaded(NumberVar popApp)
Description copied from interface: PartialWorld
Returns true if the given POP application is overloaded: that is, the number of identifiers that are asserted to satisfy it is greater than the value of the corresponding number variable, or some identifiers are asserted to satisfy it and its number variable is not instantiated.

Specified by:
isOverloaded in interface PartialWorld

getBayesNet

public DGraph getBayesNet()
Description copied from interface: PartialWorld
Returns a directed graph where the nodes are variables, and there is an edge from variable X to variable Y if Y depends directly on X given the context represented by this partial world. The graph includes at least the variables that are instantiated in this partial world. It may also include certain derived variables that are not instantiated because their values are determined by the instantiated variables.

If a variable var is not supported in this world, then its parents in the returned graph are those variables accessed by var.getDistrib (or var.getValue if var is a derived variable) before the first uninstantiated variable.

Specified by:
getBayesNet in interface PartialWorld

updateBayesNet

public void updateBayesNet(DGraph newBayesNet,
                           MapWithPreimages newVarToUninstParent,
                           java.util.Map newVarLogProbs,
                           java.util.Map newDerivedVarValues)
Description copied from interface: PartialWorld
Provides the Bayes net, the mapping from variables to uninstantiated parents, the mapping from basic variables to log probabilities, and the mapping from derived variables to values for this partial world. This method relieves the partial world of the need to recompute parent sets, log probabilities, and derived variable values itself. It is useful when some other object -- such as a PartialWorldDiff -- has already updated these data structures.

Specified by:
updateBayesNet in interface PartialWorld
Parameters:
newBayesNet - DGraph whose nodes are BayesNetVar objects
newVarToUninstParent - Map from BayesNetVar to BasicVar
newVarLogProbs - Map from BasicVar to Double
newDerivedVarValues - Map from DerivedVar to Object

getDerivedVars

public java.util.Set getDerivedVars()
Description copied from interface: PartialWorld
Returns the set of derived variables that are included in this world's Bayes net.

Specified by:
getDerivedVars in interface PartialWorld
Returns:
unmodifiable Set of DerivedVar

addDerivedVar

public boolean addDerivedVar(DerivedVar var)
Description copied from interface: PartialWorld
Adds the given derived variable to this world's Bayes net, and sets its parents appropriately.

Specified by:
addDerivedVar in interface PartialWorld
Returns:
true if the variable was actually added; false if it was already present

removeDerivedVar

public boolean removeDerivedVar(DerivedVar var)
Description copied from interface: PartialWorld
Removes the given derived variable from this world's Bayes net.

Specified by:
removeDerivedVar in interface PartialWorld
Returns:
true if the variable was actually removed; false if it was not present

print

public void print(java.io.PrintStream s)
Description copied from interface: PartialWorld
Prints this partial world to the given stream.

Specified by:
print in interface PartialWorld

basicVarToValueMap

public java.util.Map basicVarToValueMap()
Description copied from interface: PartialWorld
Map from instantiated BasicVars to their values.

Specified by:
basicVarToValueMap in interface PartialWorld

objToUsesAsValueMap

public MultiMap objToUsesAsValueMap()
Description copied from interface: PartialWorld
MultiMap from objects to the instantiated BasicVars that have them as values.

Specified by:
objToUsesAsValueMap in interface PartialWorld

objToUsesAsArgMap

public MultiMap objToUsesAsArgMap()
Description copied from interface: PartialWorld
MultiMap from objects to the instantiated BasicVars that have them as arguments.

Specified by:
objToUsesAsArgMap in interface PartialWorld

assertedIdToPOPAppMap

public java.util.Map assertedIdToPOPAppMap()
Description copied from interface: PartialWorld
Map from asserted ObjectIdentifiers to the POP applications (NumberVars) that they satisfy.

Specified by:
assertedIdToPOPAppMap in interface PartialWorld

popAppToAssertedIdsMap

public IndexedMultiMap popAppToAssertedIdsMap()
Description copied from interface: PartialWorld
Map from POP applications (NumberVars) to IndexedSets of the ObjectIdentifiers that are asserted to satisfy them. POP applications with no identifiers are not necessarily included in this map.

Specified by:
popAppToAssertedIdsMap in interface PartialWorld

varToUninstParentMap

public MapWithPreimages varToUninstParentMap()
Description copied from interface: PartialWorld
MapWithPreimages from BayesNetVars to their first uninstantiated parents. A variable is included as a key in this map only if it is not supported by this world, which means it has an uninstantiated parent.

Specified by:
varToUninstParentMap in interface PartialWorld

varToLogProbMap

public java.util.Map varToLogProbMap()
Description copied from interface: PartialWorld
Map from instantiated VarWithDistrib objects to Double values representing their log probabilities given their parents.

Specified by:
varToLogProbMap in interface PartialWorld

derivedVarToValueMap

public java.util.Map derivedVarToValueMap()
Description copied from interface: PartialWorld
Map from DerivedVars that are included in this world's Bayes net to their values.

Specified by:
derivedVarToValueMap in interface PartialWorld

addListener

public void addListener(WorldListener listener)
Description copied from interface: PartialWorld
Adds the given listener to a list of listeners to be notified when changes are made to this partial world.

Specified by:
addListener in interface PartialWorld

removeListener

public void removeListener(WorldListener listener)
Description copied from interface: PartialWorld
Removes the given listener from the list of objects to be notified when changes are made to this partial world.

Specified by:
removeListener in interface PartialWorld

updateParentsAndProbs

protected void updateParentsAndProbs()
Ensure that parent sets, basic variable log probabilities, and derived variable values are all up to date. We do this in batch because recomputing these things is somewhat expensive, and it's pointless to recompute the log probability for a variable when one of its parents changes if all its other parents are also about to change.


cloneFields

public void cloneFields(AbstractPartialWorld newWorld)
Replicates all fields into the fields of newWorld. This is meant to be invoked by clone methods of extending classes, allowing their writing without access to all, and detailed knowledge of, AbstractPartialWorld fields.


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object