public abstract class CachedStrategy extends Strategy implements GraphAnalyzer
getCachedResult()
), and are recomputed only
when the graph has changed since the last request (via _result()
) for
the strategy result.
The status of the cache content can be queried with the obsolete()
method to determine if a subsequent invocation of _result()
by the
derived classes will trigger recomputation of the analysis.
The graph changes tracked by an analyzer are restricted to changes in the
graph topology (the set of nodes and edges). For example, changes to edge/node
weights that may affect the result of an analysis are not tracked, since
analyzers have no specific knowledge of weights. In such cases, it is the
responsibility of the client (or derived analyzer class) to invalidate the
cached result (see reset()
) when changes to graph weights or other
non-topology information render the cached result obsolete. For this reason,
some caution is generally required when using analyzers whose results depend on
more than just the graph topology.
In these cases the client should check for data consistency
(through the Analyzer.valid()
method) as well as changes in them and
calling the reset()
method in case of data changes.
[1] G. Ramalingam. Bounded Incremental Computation. PhD thesis, University of Wisconsin at Madison, August 1993.
Red (cxh) |
Red (cxh) |
Constructor and Description |
---|
CachedStrategy(Graph graph)
No instance of this object can be created as it is abstract.
|
Modifier and Type | Method and Description |
---|---|
protected java.lang.Object |
_compute()
Perform the graph analysis and return the resulting value.
|
protected java.lang.Object |
_convertResult(java.lang.Object result)
Convert the cached result (
getCachedResult() )
to a form that is
suitable for the client to access (via _result() ). |
protected java.lang.Object |
_result()
Return the result (cached value) of the analyzer on the associated
graph.
|
boolean |
cachingStatus()
Return the caching status of the strategy.
|
void |
disableCaching()
Disable caching.
|
void |
enableCaching()
Enable caching.
|
java.lang.Object |
getCachedResult()
The result of the most recent cached computation of the analysis,
as determined by
_compute() , and without
conversion by _convertResult(Object) . |
Graph |
graph()
The graph associated with the Strategy.
|
boolean |
obsolete()
Test whether or not the cached result of the analyzer is
obsolete relative to the associated graph.
|
void |
reset()
Reset the analyzer to invalidate any cached value (i.e., to force
recomputation the next time a result of the computation is needed).
|
void |
setCachedResult(CachedStrategy cacher)
Set the cached value of this analyzer to the cached value of another
analyzer.
|
java.lang.String |
toString()
Return a description of the strategy.
|
public CachedStrategy(Graph graph)
graph
- The graph which the analyzer is using to compute its
result.public boolean cachingStatus()
public void disableCaching()
public void enableCaching()
public java.lang.Object getCachedResult()
_compute()
, and without
conversion by _convertResult(Object)
.setCachedResult(CachedStrategy)
public Graph graph()
graph
in interface GraphAnalyzer
public boolean obsolete()
_result()
).
If the cached result is obsolete and the cache is enabled,
then a subsequent invocation of _result()
will trigger recomputation of the analysis.public void reset()
public void setCachedResult(CachedStrategy cacher)
cacher
- The other analyzer.getCachedResult()
public java.lang.String toString()
protected java.lang.Object _compute()
getCachedResult()
provides the result of the
previous invocation of the analysis; this value can be
used, for example, to facilitate incremental analyses.
This method just returns null, and will typically be overridden
in each derived class to perform the appropriate graph analysis.protected java.lang.Object _convertResult(java.lang.Object result)
getCachedResult()
)
to a form that is
suitable for the client to access (via _result()
).
This base class method just returns a reference to the cached result.
However, it may be appropriate for derived classes to override this
method. For example, if the object returned by the analyzer is mutable,
one may wish to override this method to copy the cached
value (or convert it to some other form) before returning it.
Then changes made by the client to the returned value will
not affect the cached value in the analysis.
This consideration is important for incremental analyzers that
use the cached value across successive invocations of the
analyzer.result
- The cached result to be converted._result()
.protected final java.lang.Object _result()
_compute()
), if the graph
has changed since the last time _result()
was invoked.
Otherwise, the cache value is simply returned (in O(1) time).