/* ZZMap.java * * Copyright (c) 2000, Ted Nelson and Tuomas Lukka * * You may use and distribute under the terms of either the GNU Lesser * General Public License, either version 2 of the license or, * at your choice, any later version. Alternatively, you may use and * distribute under the terms of the XPL. * * See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of * the licenses. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README * file for more details. * */ /* * Written by Kimmo Wideroos */ /* abstract base class for different maps to be used in views */ package org.gzigzag.map; import org.gzigzag.*; import java.util.*; public abstract class ZZMap implements Runnable, Cloneable { public static boolean dbg = false; static void p(String s) { if(dbg) System.out.println(s); } static void pa(String s) { System.out.println(s); } final public class NoFocusException extends Exception { public NoFocusException() { super("No focus defined!"); } } float _erosionCoeff = (float)0.2; int _ITERATION_RATE = 500; int TIMEOUT = 5000; String _weightdim = "d.weights"; protected InputReader inputReader = null; public void setErosionCoeff(float ec) { _erosionCoeff = ec; } public void setIterationRate(int ir) { _ITERATION_RATE = ir; } public void setWeightDim(String wd) { _weightdim = wd; } // 'Particle' represents input vector and its 2d position public interface Particle { int id(); ZZCell cell(); int q(int ind); int[] q(); void setq(int ind, int q); float x(); float y(); void x(float new_x); void y(float new_y); float[] coord(); void coord(float[] new_coord); float priority(); int size(); } public interface InputReader { boolean contextChanged(String[] dims, ZZCell context); void read(String[] dims, ZZCell context, ZZCell[][] ids, int[][][] inputs); } protected float[] weights; //protected float[] particleWeights; protected float weight0; protected Particle[] inputs; protected int[] shown_inputs; private int focusId = -1; private boolean changed = true; private boolean init = false; private volatile Thread t; private volatile boolean threadSuspended = false; private long update_time = 0; public boolean equals(ZZMap map) { return this.getClass().equals(map.getClass()); } protected class WeightWatcher implements ZZObs { private ZZCell c; private int ind; public void chg() { float new_weight; new_weight = Float.parseFloat(c.getText(this)); //updateParticleWeights(new_weight-weights[ind], ind); weights[ind] = new_weight; } public WeightWatcher(ZZCell c, int ind) { this.c = c; this.ind = ind; } } protected void rollbackWeights(ZZCell mroot) { ZZCell c = mroot; for(int i=0; i 0) { t = new Thread(this); t.start(); t.setPriority(t.MIN_PRIORITY); } // map's input reader decides whether it is time for update if(!inputReader.contextChanged(dims, accursed)) { System.out.println("update returns false!"); return false; } else { System.out.println("update returns true!"); } threadSuspended = true; ZZCell[][] cells = new ZZCell[1][]; int[][][] input_vecs = new int[1][][]; inputReader.read(dims, accursed, cells, input_vecs); System.out.println("read!"); inputs = new Particle[cells[0].length]; for(int i=0; i TIMEOUT) { System.out.println("ZZMap timeout - stop!"); stop(); } if(iterate()) changed = true; if(weights != null) erodeWeights(); if(changed) { ZZUpdateManager.chg(); } } } catch(Exception e) { e.printStackTrace(); System.out.println("Exception in ZZMap: "+e); } } } public synchronized void stop() { t = null; notify(); } public int focusId() throws NoFocusException { if(focusId < 0) throw new NoFocusException(); return focusId; } public boolean changed() { return changed; } public void changed(boolean new_value) { changed = new_value; } // return 'true', if something changed radically during iteration public abstract boolean iterate(); }