The "Fit", and some Java Code

Gerald de Jong (NOSPAM.gdj@xs4all.nl)
Mon, 18 May 1998 04:33:27 +0200 (MET DST)

this newsgroup seems to be primarily used for announcements about
conferences and calls for papers. this is all fine, but the ideas
contained in Fuzzy Logic can be extremely enlightening in many other
contexts that that of a University research department or a Japanese
company. kids should be using this stuff!

in an effort to hopefully cause a little more action in this
newsgroup, i've decided to post my fuzzy logic Java sources, which are
based on a class called "Fit", which represents a "Fuzzy Bit". once
you make the small step to think in terms of such a data type, many
things become very easy to manage.. even rendering things on the
screen.

the Fit contains a fuzzy bit, and can only be asked what its value is
by giving it a context within which it can express itself. this way
you need not know _how_ it stores its fuzzy bit value. my choice was
to store it in an integer, but it makes little difference.

what follows is the source of the core abstract class. the rest can
be found at my website http://www.beautifulcode.nl/fuzzy. if other
people get into this, we should maybe change the java package name to
"comp.ai.fuzzy". feel free to compile up the source, learn from it,
and eventually give me a hand taking it to new levels.

public abstract class Fit {

/**
* this is a big integer value representing "true". which one
doesn't
* make all that much difference.
*/

public static int SPAN = 0x00001000;

/**
* for convenience, learning, and debugging, every fit has a name
*/

protected String name = null;

/**
* construct a fit, given its name.
*/

protected Fit(String theName) {
name = theName;
}

/**
* access the name
*/

public String getName() {
return(name);
}

/**
* get the value of the fit in absolute terms. this method is
defined to be
* abstract because at this level we have no idea how to determine
what the
* fit's value is. the concrete subclasses will have to figure
that out.
*/

protected abstract int getValue();

/**
* some kinds of fits allow you to mutate their value. if it's not
allowed,
* you'll get a runtime exception here.
*/

protected abstract void setValue(int theValue);

/**
* make this fit equal to another
*/

public void setValue(Fit theFit) {
setValue(theFit.getValue());
}

/**
* set the value using a rational
*/

public void setValue(int theNumerator, int theDenominator) {
setValue((theNumerator*SPAN)/theDenominator);
}

/**
* set the value using a needle on a range.
*/

public void setValue(int theLow, int theNeedle, int theHigh) {
setValue(theNeedle-theLow,theHigh-theLow);
}

/**
* access the value by giving the fit a context in which to express
* itself to you.
*/

public int getValue(int theLow, int theHigh) {
int val = getValue();
if ( val < 0 || val > SPAN ) {
throw new RuntimeException("Illegal fit value for
\""+name+"\": "+val);
}
return(((SPAN-val)*theLow + val*theHigh)/SPAN);
}

/**
* check to see if the truth value is at one of the ends
*/

public boolean isExtreme() {
int val = getValue();
return(val == 0 || val == SPAN);
}

/**
* make a string, showing the percentage
*/

public String toString() {
return(name+"("+getValue(0,100)+"%)");
}
}

Gerald A. de Jong, Beautiful Code bv, Rotterdam, NL