diva.sketch.toolbox
Class CachingStrokeRecognizer

java.lang.Object
  extended by diva.sketch.toolbox.CachingStrokeRecognizer
All Implemented Interfaces:
StrokeRecognizer

public class CachingStrokeRecognizer
extends Object
implements StrokeRecognizer

A proxy recognizer implementation that caches the result of the target recognizer. If the same stroke is passed in with the same number of vertices, it returns the cached recognition result rather than calling the target recognizer. This is useful, for example, if multiple "high-level" recognizers (HLRs) want to share a basic recognizer to recognize shapes. Suppose the user draws a square. Without a cache:

  1. The first HLR calls the basic recognizer, which performs recognition and returns a "square" recognition.
  2. The second HLR calls the basic recognizer, which performs recognition and returns a "square" recognition.
  3. And so on...

The uncached version results in a lot of wasted computation on the part of the basic recognizer. With a cache, the sequence looks like this:

  1. The first HLR calls the cached recognizer, which calls its "target" basic recognizer, which returns a "square" recognition.
  2. The second HLR calls the cached recognizer, which sees that the stroke has not changed since the last time it was invoked, and returns the cached recognition.
  3. And so on...
In this cached version, the basic recognition is only performed once.

Since we expect most users will want to use a single basic recognizer, this should save a lot of unnecessary computation. Having this caching behavior in its own class makes it self-contained and possibly reusable.

Version:
$Revision: 1.5 $
Author:
Michael Shilman (michaels@eecs.berkeley.edu)

Constructor Summary
CachingStrokeRecognizer(StrokeRecognizer target)
          Cache the recognitions of the given target recognizer.
 
Method Summary
 void clear()
          Manually clear the cache.
 boolean hasStrokeChanged(TimedStroke s)
          Return whether or not the given stroke is changed relative to the previous cached version.
 RecognitionSet strokeCompleted(TimedStroke s)
          Perform recognition in the target if the given stroke hasn't changed since the last time this method was called.
 RecognitionSet strokeModified(TimedStroke s)
          Perform recognition in the target if the given stroke hasn't changed since the last time this method was called.
 RecognitionSet strokeStarted(TimedStroke s)
          Perform recognition in the target if the given stroke hasn't changed since the last time this method was called.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CachingStrokeRecognizer

public CachingStrokeRecognizer(StrokeRecognizer target)
Cache the recognitions of the given target recognizer.

Method Detail

clear

public void clear()
Manually clear the cache.


hasStrokeChanged

public boolean hasStrokeChanged(TimedStroke s)
Return whether or not the given stroke is changed relative to the previous cached version. This currently returns whether the two strokes reference the same object and whether or not the given stroke has the same number of vertices as the previous version.

This method can be overridden to make the caching more accurate or more sophisticated. For example it could be configured to perform recognition only after "N" vertices have been added.


strokeCompleted

public RecognitionSet strokeCompleted(TimedStroke s)
Perform recognition in the target if the given stroke hasn't changed since the last time this method was called.

Specified by:
strokeCompleted in interface StrokeRecognizer

strokeModified

public RecognitionSet strokeModified(TimedStroke s)
Perform recognition in the target if the given stroke hasn't changed since the last time this method was called.

Specified by:
strokeModified in interface StrokeRecognizer

strokeStarted

public RecognitionSet strokeStarted(TimedStroke s)
Perform recognition in the target if the given stroke hasn't changed since the last time this method was called.

Specified by:
strokeStarted in interface StrokeRecognizer


Copyright © 2015 Central Laboratory of the Research Councils. All Rights Reserved.