Package jadex.rules.state.javaimpl
Class OAVWeakState
- java.lang.Object
-
- jadex.rules.state.javaimpl.OAVWeakState
-
-
Field Summary
Fields Modifier and Type Field Description protected OAVEventHandler
eventhandler
The OAV event handler.protected IOAVIdGenerator
generator
The id generator.protected boolean
nocheck
The flag to disable type checking.protected java.util.Map
objects
The objects table (id -> map).protected java.util.Map
objectusages
The usages of object ids (object id -> usages).protected static java.lang.Class[]
PCL
The argument types for property change listener adding/removal (cached for speed).protected java.util.Map
pcls
The Java beans property change listeners.protected IProfiler
profiler
The profiler.protected java.lang.ref.ReferenceQueue
queue
The reference queue for stale objects.protected java.util.Set
rootobjects
The root objects (will not be cleaned up when usages==0).protected ISynchronizator
synchronizator
The synchronizator (if any).protected OAVTypeModel
tmodel
The type models.protected java.util.Map
types
The object types (object -> type).
-
Constructor Summary
Constructors Constructor Description OAVWeakState(OAVTypeModel tmodel)
Create a new empty OAV state representation.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addAttributeValue(java.lang.Object object, OAVAttributeType attribute, java.lang.Object value)
Add an attribute of an object to the given value.void
addExternalObjectUsage(java.lang.Object id, java.lang.Object external)
Add an external usage of a state object.protected void
addJavaObjectUsage(java.lang.Object whichid, OAVAttributeType whichattr, java.lang.Object value)
When it is a Java object, it is not created in state, so we have to notify object addition to listeners on first usage.void
addJavaRootObject(java.lang.Object object)
Add a Java object as root object.void
addStateListener(IOAVStateListener listener, boolean bunch)
Add a new state listener.void
addSubstate(IOAVState substate)
Add a substate.protected boolean
checkMultiplicity(java.lang.Object object, OAVAttributeType attribute, java.lang.String allowedmult)
Ensure that multiplicity is ok.protected boolean
checkMultiplicity(java.lang.Object object, OAVAttributeType attribute, java.util.Set allowedmults)
Ensure that multiplicity is ok.protected boolean
checkTypeDefined(OAVObjectType type)
Test if a type is defined in one of the models.protected boolean
checkTypeHasAttribute(java.lang.Object object, OAVAttributeType attribute)
Ensure that a type has an attribute.protected boolean
checkValidStateObject(java.lang.Object object)
Test if the object is a valid state object, meaning that is either a root object or a non-root object with at least one usage.protected boolean
checkValueCompatibility(java.lang.Object object, OAVAttributeType attribute, java.lang.Object value)
Check if it is allowed to set or add an attribute value.java.lang.Object
cloneObject(java.lang.Object object, IOAVState targetstate)
Clone an object in the state (deep copy).boolean
containsKey(java.lang.Object object, OAVAttributeType attribute, java.lang.Object key)
Test if a key is contained in the map attribute.boolean
containsObject(java.lang.Object id)
Test if the state contains a specific object.java.lang.Object
createObject(OAVObjectType type)
Create an object.java.lang.Object
createRootObject(OAVObjectType type)
Create a root object.protected void
deregisterValue(java.lang.Object value)
Deregister a value for observation.void
dispose()
Dispose the state.void
dropObject(java.lang.Object object)
Drop an object from the state.boolean
equals(java.lang.Object a, java.lang.Object b)
Test if two values are equal according to current identity/equality settings.void
expungeStaleObjects()
Expunge stale objects.java.util.List
findCycle(java.util.Collection objects)
Find a cycle in a given set of objects.java.util.Collection
getAttributeKeys(java.lang.Object object, OAVAttributeType attribute)
Get the keys of an attribute of an object.java.lang.Object
getAttributeValue(java.lang.Object object, OAVAttributeType attribute)
Get an attribute value of an object.java.lang.Object
getAttributeValue(java.lang.Object object, OAVAttributeType attribute, java.lang.Object key)
Get an attribute value of an object.java.util.Collection
getAttributeValues(java.lang.Object object, OAVAttributeType attribute)
Get the values of an attribute of an object.protected java.lang.Object
getClonedOAVObject(IOAVState targetstate, java.util.Map handles, java.util.List todo, java.lang.Object oldval)
Get or create a clone of an oav object.java.util.Iterator
getDeepObjects()
Get all objects in the state.protected OAVWeakIdGenerator.OAVInternalObjectId
getInternalId(java.lang.Object id)
Get the internal object id.protected java.util.Map
getJavaObjectUsages(java.lang.Object id)
Get all object usages.protected java.util.Map
getObject(java.lang.Object id)
Get an object map for its id.java.util.Iterator
getObjects()
Get all objects in the state.IProfiler
getProfiler()
Get the profiler.java.util.Collection
getReferencingObjects(java.lang.Object value)
Get those objects referencing a given object.java.util.Iterator
getRootObjects()
Get the root objects of the state.int
getSize()
Get the number of objects in the state.IOAVState[]
getSubstates()
Get the substates.ISynchronizator
getSynchronizator()
Get the synchronizator (if any).OAVObjectType
getType(java.lang.Object id)
Get the type of an object.OAVTypeModel
getTypeModel()
Get the type model.java.util.Collection
getUnreferencedObjects()
Get all unreferenced objects.boolean
isIdentifier(java.lang.Object object)
Test if the object represents an identifier.boolean
isJavaIdentity()
Flag indicating that java objects are stored by identity instead of equality.protected boolean
isJavaNonValue(java.lang.Object obj)
Check if an object is a java object but not a value.void
notifyEventListeners()
Throw collected events and notify the listeners.protected void
registerValue(OAVJavaType type, java.lang.Object value)
Register a value for observation.void
removeAttributeValue(java.lang.Object object, OAVAttributeType attribute, java.lang.Object value)
Remove an attribute of an object to the given value.void
removeExternalObjectUsage(java.lang.Object id, java.lang.Object external)
Remove an external usage of a state object.protected void
removeJavaObjectUsage(java.lang.Object whichid, OAVAttributeType whichattr, java.lang.Object value)
Remove an object usage.void
removeJavaRootObject(java.lang.Object object)
Drop a Java object from root objects.void
removeStateListener(IOAVStateListener listener)
Remove a state listener.void
setAttributeValue(java.lang.Object object, OAVAttributeType attribute, java.lang.Object value)
Set an attribute of an object to the given value.void
setProfiler(IProfiler profiler)
Set the profiler.void
setSynchronizator(ISynchronizator synchronizator)
Set the synchronizator.java.lang.String
toString()
Get the string representation of the object.
-
-
-
Field Detail
-
PCL
protected static java.lang.Class[] PCL
The argument types for property change listener adding/removal (cached for speed).
-
tmodel
protected OAVTypeModel tmodel
The type models.
-
objects
protected java.util.Map objects
The objects table (id -> map).
-
types
protected java.util.Map types
The object types (object -> type).
-
generator
protected IOAVIdGenerator generator
The id generator.
-
nocheck
protected boolean nocheck
The flag to disable type checking.
-
objectusages
protected java.util.Map objectusages
The usages of object ids (object id -> usages).
-
rootobjects
protected java.util.Set rootobjects
The root objects (will not be cleaned up when usages==0).
-
pcls
protected java.util.Map pcls
The Java beans property change listeners.
-
eventhandler
protected OAVEventHandler eventhandler
The OAV event handler.
-
queue
protected java.lang.ref.ReferenceQueue queue
The reference queue for stale objects.
-
synchronizator
protected ISynchronizator synchronizator
The synchronizator (if any).
-
profiler
protected IProfiler profiler
The profiler.
-
-
Constructor Detail
-
OAVWeakState
public OAVWeakState(OAVTypeModel tmodel)
Create a new empty OAV state representation.
-
-
Method Detail
-
getTypeModel
public OAVTypeModel getTypeModel()
Get the type model.- Specified by:
getTypeModel
in interfaceIOAVState
- Returns:
- The type model.
-
createObject
public java.lang.Object createObject(OAVObjectType type)
Create an object. Creates an object identifier that can be used to store/retrieve attribute values. May reuse old object identifiers for performance.- Specified by:
createObject
in interfaceIOAVState
- Parameters:
type
- The object type (null for defining meta types).- Returns:
- An object identifier.
-
createRootObject
public java.lang.Object createRootObject(OAVObjectType type)
Create a root object. A root object will not be automatically garbage collected when no references point to this object any longer. Creates an object identifier that can be used to store/retrieve attribute values. May reuse old object identifiers for performance.- Specified by:
createRootObject
in interfaceIOAVState
- Returns:
- An object identifier.
-
dropObject
public void dropObject(java.lang.Object object)
Drop an object from the state. Recursively removes the object and all connected objects that are not referenced elsewhere.- Specified by:
dropObject
in interfaceIOAVState
- Parameters:
object
- The identifier of the object to remove.
-
addJavaRootObject
public void addJavaRootObject(java.lang.Object object)
Add a Java object as root object.- Specified by:
addJavaRootObject
in interfaceIOAVState
- Parameters:
object
- The Java object.
-
removeJavaRootObject
public void removeJavaRootObject(java.lang.Object object)
Drop a Java object from root objects.- Specified by:
removeJavaRootObject
in interfaceIOAVState
- Parameters:
object
- The Java object.
-
cloneObject
public java.lang.Object cloneObject(java.lang.Object object, IOAVState targetstate)
Clone an object in the state (deep copy).- Parameters:
object
- The handle to the object to be cloned.targetstate
- The target state in which the clone should be created.- Returns:
- The identifier of the newly created clone.
-
getClonedOAVObject
protected java.lang.Object getClonedOAVObject(IOAVState targetstate, java.util.Map handles, java.util.List todo, java.lang.Object oldval)
Get or create a clone of an oav object.- Parameters:
targetstate
- The target state.handles
- The handles.todo
- The todo list.oldval
- The old object.
-
containsObject
public boolean containsObject(java.lang.Object id)
Test if the state contains a specific object.- Specified by:
containsObject
in interfaceIOAVState
- Parameters:
id
- The object id.- Returns:
- True, if contained.
-
isIdentifier
public boolean isIdentifier(java.lang.Object object)
Test if the object represents an identifier.- Specified by:
isIdentifier
in interfaceIOAVState
- Parameters:
object
- The suspected object identifier.- Returns:
- True, if object identifier.
-
getType
public OAVObjectType getType(java.lang.Object id)
Get the type of an object.
-
getObjects
public java.util.Iterator getObjects()
Get all objects in the state.- Specified by:
getObjects
in interfaceIOAVState
-
getDeepObjects
public java.util.Iterator getDeepObjects()
Get all objects in the state.- Specified by:
getDeepObjects
in interfaceIOAVState
-
getRootObjects
public java.util.Iterator getRootObjects()
Get the root objects of the state.- Specified by:
getRootObjects
in interfaceIOAVState
-
getSize
public int getSize()
Get the number of objects in the state. Optional operation used for debugging only.
-
getUnreferencedObjects
public java.util.Collection getUnreferencedObjects()
Get all unreferenced objects.- Specified by:
getUnreferencedObjects
in interfaceIOAVState
- Returns:
- All unreferenced objects of the state.
-
findCycle
public java.util.List findCycle(java.util.Collection objects)
Find a cycle in a given set of objects.
-
getReferencingObjects
public java.util.Collection getReferencingObjects(java.lang.Object value)
Get those objects referencing a given object.- Specified by:
getReferencingObjects
in interfaceIOAVState
-
getAttributeValue
public java.lang.Object getAttributeValue(java.lang.Object object, OAVAttributeType attribute)
Get an attribute value of an object.- Specified by:
getAttributeValue
in interfaceIOAVState
- Parameters:
object
- The identifier of the object.attribute
- The attribute identifier.- Returns:
- The value (basic, object id or java object).
-
setAttributeValue
public void setAttributeValue(java.lang.Object object, OAVAttributeType attribute, java.lang.Object value)
Set an attribute of an object to the given value.- Specified by:
setAttributeValue
in interfaceIOAVState
- Parameters:
object
- The identifier of the object.attribute
- The attribute identifier.value
- The value (basic, object id or java object).
-
isJavaNonValue
protected boolean isJavaNonValue(java.lang.Object obj)
Check if an object is a java object but not a value.
-
getAttributeValues
public java.util.Collection getAttributeValues(java.lang.Object object, OAVAttributeType attribute)
Get the values of an attribute of an object.- Specified by:
getAttributeValues
in interfaceIOAVState
- Parameters:
object
- The identifier of the object.attribute
- The attribute identifier.- Returns:
- The values (basic, object ids or java objects).
-
getAttributeKeys
public java.util.Collection getAttributeKeys(java.lang.Object object, OAVAttributeType attribute)
Get the keys of an attribute of an object.- Specified by:
getAttributeKeys
in interfaceIOAVState
- Parameters:
object
- The identifier of the object.attribute
- The attribute identifier.- Returns:
- The keys for which values are stored.
-
getAttributeValue
public java.lang.Object getAttributeValue(java.lang.Object object, OAVAttributeType attribute, java.lang.Object key)
Get an attribute value of an object. Method only applicable for map attribute type.- Specified by:
getAttributeValue
in interfaceIOAVState
- Parameters:
object
- The identifier of the object.attribute
- The attribute identifier.key
- The key.- Returns:
- The value (basic, object id or java object).
-
containsKey
public boolean containsKey(java.lang.Object object, OAVAttributeType attribute, java.lang.Object key)
Test if a key is contained in the map attribute.- Specified by:
containsKey
in interfaceIOAVState
- Parameters:
object
- The identifier of the object.attribute
- The attribute identifier.key
- The key.- Returns:
- True if key is available.
-
addAttributeValue
public void addAttributeValue(java.lang.Object object, OAVAttributeType attribute, java.lang.Object value)
Add an attribute of an object to the given value.- Specified by:
addAttributeValue
in interfaceIOAVState
- Parameters:
object
- The identifier of the object.attribute
- The attribute identifier.value
- The value (basic, object id or java object).
-
removeAttributeValue
public void removeAttributeValue(java.lang.Object object, OAVAttributeType attribute, java.lang.Object value)
Remove an attribute of an object to the given value.- Specified by:
removeAttributeValue
in interfaceIOAVState
- Parameters:
object
- The identifier of the object.attribute
- The attribute identifier.value
- The value (basic, object id or java object).
-
addStateListener
public void addStateListener(IOAVStateListener listener, boolean bunch)
Add a new state listener.- Specified by:
addStateListener
in interfaceIOAVState
- Parameters:
listener
- The state listener.bunch
- True, for adding a bunch listener.
-
removeStateListener
public void removeStateListener(IOAVStateListener listener)
Remove a state listener.- Specified by:
removeStateListener
in interfaceIOAVState
- Parameters:
listener
- The state listener.
-
notifyEventListeners
public void notifyEventListeners()
Throw collected events and notify the listeners.- Specified by:
notifyEventListeners
in interfaceIOAVState
-
expungeStaleObjects
public void expungeStaleObjects()
Expunge stale objects.- Specified by:
expungeStaleObjects
in interfaceIOAVState
-
addExternalObjectUsage
public void addExternalObjectUsage(java.lang.Object id, java.lang.Object external)
Add an external usage of a state object. This prevents the oav object of being garbage collected as long as external references are present.- Specified by:
addExternalObjectUsage
in interfaceIOAVState
- Parameters:
id
- The oav object id.external
- The user object.
-
removeExternalObjectUsage
public void removeExternalObjectUsage(java.lang.Object id, java.lang.Object external)
Remove an external usage of a state object. This allows the oav object of being garbage collected when no further external references and no internal references are present.- Specified by:
removeExternalObjectUsage
in interfaceIOAVState
- Parameters:
id
- The oav object id.external
- The state external object.
-
getProfiler
public IProfiler getProfiler()
Get the profiler.- Specified by:
getProfiler
in interfaceIOAVState
-
setProfiler
public void setProfiler(IProfiler profiler)
Set the profiler.- Specified by:
setProfiler
in interfaceIOAVState
-
setSynchronizator
public void setSynchronizator(ISynchronizator synchronizator)
Set the synchronizator. The optional synchronizator is used to synchronize external modifications to the state (e.g. from bean changes). The synchronizator should only be set once, before the state is used.- Specified by:
setSynchronizator
in interfaceIOAVState
-
getSynchronizator
public ISynchronizator getSynchronizator()
Get the synchronizator (if any). The synchronizator (if available) can be used to synchronize access to the state with internal and external modifications.- Specified by:
getSynchronizator
in interfaceIOAVState
-
toString
public java.lang.String toString()
Get the string representation of the object.- Overrides:
toString
in classjava.lang.Object
- Returns:
- The string representation.
-
getObject
protected java.util.Map getObject(java.lang.Object id)
Get an object map for its id.- Parameters:
id
- The id.- Returns:
- The object map.
-
checkValueCompatibility
protected boolean checkValueCompatibility(java.lang.Object object, OAVAttributeType attribute, java.lang.Object value)
Check if it is allowed to set or add an attribute value. For this purpose it is checked if the value is either a) a ObjectId -> type check via OAVObjectType b) a normal Java object -> type check via OAVJavaType Additionally multiplicity is checked.- Throws:
java.lang.RuntimeException
- if value is not allowed.
-
checkTypeHasAttribute
protected boolean checkTypeHasAttribute(java.lang.Object object, OAVAttributeType attribute)
Ensure that a type has an attribute.- Parameters:
object
- The object.attribute
- The attribute.- Throws:
java.lang.RuntimeException
- if value is not allowed.
-
checkMultiplicity
protected boolean checkMultiplicity(java.lang.Object object, OAVAttributeType attribute, java.util.Set allowedmults)
Ensure that multiplicity is ok.- Parameters:
object
- The object.attribute
- The attribute.multiplicity
- The multiplicity.- Throws:
java.lang.RuntimeException
- if value is not allowed.
-
checkMultiplicity
protected boolean checkMultiplicity(java.lang.Object object, OAVAttributeType attribute, java.lang.String allowedmult)
Ensure that multiplicity is ok.- Parameters:
object
- The object.attribute
- The attribute.multiplicity
- The multiplicity.- Throws:
java.lang.RuntimeException
- if value is not allowed.
-
checkTypeDefined
protected boolean checkTypeDefined(OAVObjectType type)
Test if a type is defined in one of the models.- Parameters:
type
- The type.- Returns:
- True, if is defined.
-
checkValidStateObject
protected boolean checkValidStateObject(java.lang.Object object)
Test if the object is a valid state object, meaning that is either a root object or a non-root object with at least one usage.- Parameters:
object
- The object.- Returns:
- True, if valid.
-
addJavaObjectUsage
protected void addJavaObjectUsage(java.lang.Object whichid, OAVAttributeType whichattr, java.lang.Object value)
When it is a Java object, it is not created in state, so we have to notify object addition to listeners on first usage.
-
removeJavaObjectUsage
protected void removeJavaObjectUsage(java.lang.Object whichid, OAVAttributeType whichattr, java.lang.Object value)
Remove an object usage.- Parameters:
whichid
- The object that references the object.whichattr
- The attribute which references the object.value
- The object id/value to remove.dropset
- Already dropped objects in recursive drop (or null if none).
-
getJavaObjectUsages
protected java.util.Map getJavaObjectUsages(java.lang.Object id)
Get all object usages.- Returns:
- The usages for an object.
-
registerValue
protected void registerValue(OAVJavaType type, java.lang.Object value)
Register a value for observation. If its an expression then add the action, if its a bean then add the property listener.
-
deregisterValue
protected void deregisterValue(java.lang.Object value)
Deregister a value for observation. If its an expression then clear the action, if its a bean then remove the property listener.
-
getInternalId
protected OAVWeakIdGenerator.OAVInternalObjectId getInternalId(java.lang.Object id)
Get the internal object id.- Parameters:
id
- The id.- Returns:
- The internal id.
-
addSubstate
public void addSubstate(IOAVState substate)
Add a substate. Read accesses will be transparently mapped to substates. Write accesses to substates need not be supported and may generate UnsupportedOperationException.- Specified by:
addSubstate
in interfaceIOAVState
-
getSubstates
public IOAVState[] getSubstates()
Get the substates.- Specified by:
getSubstates
in interfaceIOAVState
-
isJavaIdentity
public boolean isJavaIdentity()
Flag indicating that java objects are stored by identity instead of equality.- Specified by:
isJavaIdentity
in interfaceIOAVState
-
-