[wosaic commit] r338 - trunk/src/wosaic/algorithms

2 views
Skip to first unread message

codesite...@google.com

unread,
Sep 17, 2008, 3:50:44 PM9/17/08
to wosaic...@googlegroups.com
Author: swegner2
Date: Wed Sep 17 12:50:21 2008
New Revision: 338

Modified:
trunk/src/wosaic/algorithms/AbstractAlgorithm.java
trunk/src/wosaic/algorithms/BruteForceAlgorithm.java

Log:
More work on the BruteForceAlgorithm-- I think it's ready to encorporate

Modified: trunk/src/wosaic/algorithms/AbstractAlgorithm.java
==============================================================================
--- trunk/src/wosaic/algorithms/AbstractAlgorithm.java (original)
+++ trunk/src/wosaic/algorithms/AbstractAlgorithm.java Wed Sep 17 12:50:21
2008
@@ -3,6 +3,8 @@
*/
package wosaic.algorithms;

+import java.util.ArrayList;
+
import wosaic.utilities.Mosaic;
import wosaic.utilities.Pixel;

@@ -13,7 +15,7 @@
*
* @author swegner2
*/
-public abstract class AbstractAlgorithm {
+public abstract class AbstractAlgorithm implements Runnable {
/**
* Default constructor for AbstractAlgorithm class.
*
@@ -32,6 +34,25 @@
* @param pixel The new pixel
*/
abstract public void AddPixel(Pixel pixel);
+
+ /**
+ * Consider a list of new Pixels that have been generated from the source
plugin.
+ * The default behavior is to simply process them one-by-one in
AddPixel(). However,
+ * new Algorithms are encouraged to override this as appropriate
+ *
+ * @param pixels The new Pixel objects to consider
+ */
+ public void AddPixels(ArrayList<Pixel> pixels) {
+ for (Pixel pixel : pixels)
+ AddPixel(pixel);
+ }
+
+ /**
+ * Notify the Algorithm that all Pixels have been retrieved by the plugin.
+ * For some algorithms, this will prompt them to start processing. For
others,
+ * this will mean that they can stop waiting, and simply return.
+ */
+ abstract public void FinishedAddingPixels();

/**
* The mosaic object that we will be filling

Modified: trunk/src/wosaic/algorithms/BruteForceAlgorithm.java
==============================================================================
--- trunk/src/wosaic/algorithms/BruteForceAlgorithm.java (original)
+++ trunk/src/wosaic/algorithms/BruteForceAlgorithm.java Wed Sep 17
12:50:21 2008
@@ -3,6 +3,11 @@
*/
package wosaic.algorithms;

+import java.util.ArrayList;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+
import wosaic.utilities.Mosaic;
import wosaic.utilities.Pixel;

@@ -16,6 +21,11 @@
*/
public class BruteForceAlgorithm extends AbstractAlgorithm {

+ protected LinkedBlockingQueue<Pixel> PixelQueue;
+ protected AtomicBoolean running;
+ protected AtomicBoolean pluginFinished;
+ private long TIMEOUT = 50;
+
/**
* Default constructor-- simple call our superclass constructor
*
@@ -24,6 +34,9 @@
*/
public BruteForceAlgorithm(Mosaic mos, int[][][] colorMap) {
super(mos, colorMap);
+ PixelQueue = new LinkedBlockingQueue<Pixel>();
+ running = new AtomicBoolean(false);
+ pluginFinished = new AtomicBoolean(false);
}

/**
@@ -35,6 +48,19 @@
*/
@Override
public void AddPixel(Pixel pixel) {
+ // Start running if we're not
+ boolean wasRunning = running.getAndSet(true);
+ if (!wasRunning) {
+ Thread workThread = new Thread(this, "AlgorithmThread");
+ workThread.run();
+ }
+
+ PixelQueue.add(pixel);
+ PixelQueue.notifyAll();
+ }
+
+ private void processPixel(Pixel pixel) {
+
final int[] avgColors = new int[3];
for (int r = 0; r < Mos.getParams().resRows; r++)
for (int c = 0; c < Mos.getParams().resCols; c++) {
@@ -49,9 +75,59 @@
else if (matchScore < Mos.getScoreAt(r, c))
Mos.UpdatePixel(r, c, pixel, matchScore);
}
+ }
+
+ /**
+ * Consider a list of new Pixels that have been generated from the source
plugin.
+ * The default behavior is to simply process them one-by-one in
AddPixel(). However,
+ * new Algorithms are encouraged to override this as appropriate
+ *
+ * @param pixels The new Pixel objects to consider
+ */
+ @Override
+ public void AddPixels(ArrayList<Pixel> pixels) {
+ // Start running if we're not
+ boolean wasRunning = running.getAndSet(true);
+ if (!wasRunning) {
+ Thread workThread = new Thread(this, "AlgorithmThread");
+ workThread.run();
+ }
+
+ PixelQueue.addAll(pixels);
+ PixelQueue.notifyAll();
+ }

- // FIXME: What does this do?
- notifyAll();
+ @Override
+ public void run() {
+ boolean waitForMore = true;
+ boolean finished = false;
+
+ while(!finished) {
+ if (waitForMore)
+ waitForMore = !pluginFinished.get();
+
+ Pixel pixel = null;
+ try {
+ pixel = PixelQueue.poll(TIMEOUT, TimeUnit.MILLISECONDS);
+ } catch (InterruptedException ex) {
+ // We've been canceled! Cleanup?
+ waitForMore = false;
+ }
+
+ if (pixel == null) {
+ if (waitForMore)
+ // Simply try again
+ continue;
+ else
+ finished = true;
+
+ } else // pixel != null
+ processPixel(pixel);
+ }
}

+ @Override
+ public void FinishedAddingPixels() {
+ pluginFinished.set(true);
+ }
}

Reply all
Reply to author
Forward
0 new messages