public class CARTImpl extends Object implements CART
NODE feat operand value qfalse
Where feat
is an string that represents a feature
to pass to the findFeature
method of an item.
The value
represents the value to be compared against
the feature obtained from the item via the feat
string.
The operand
is the operation to do the comparison. The
available operands are as follows:
[[[TODO: provide support for the IN operator.]]]
For < and >, this CART coerces the value and feature to float's. For =, this CART coerces the value and feature to string and checks for string equality. For MATCHES, this CART uses the value as a regular expression and compares the obtained feature to that.
A CART is represented by an array in this implementation. The
qfalse
value represents the index of the array to go to if
the comparison does not match. In this implementation, qtrue index
is always implied, and represents the next element in the
array. The root node of the CART is the first element in the array.
The interpretations always start at the root node of the CART and continue until a final node is found. The final nodes have the following form:
LEAF value
Where value
represents the value of the node.
Reaching a final node indicates the interpretation is over and the
value of the node is the interpretation result.
Modifier and Type | Class and Description |
---|---|
(package private) static class |
CARTImpl.ComparisonNode
A decision Node that compares two values.
|
(package private) static class |
CARTImpl.DecisionNode
A decision node that determines the next Node to go to in the CART.
|
(package private) static class |
CARTImpl.LeafNode
The final Node of a CART.
|
(package private) static class |
CARTImpl.MatchingNode
A Node that checks for a regular expression match.
|
(package private) static class |
CARTImpl.Node
A node for the CART.
|
Modifier and Type | Field and Description |
---|---|
(package private) CARTImpl.Node[] |
cart
The CART.
|
(package private) int |
curNode
The number of nodes in the CART.
|
(package private) static String |
LEAF
Entry in file represents a final node.
|
private static Logger |
LOGGER
Logger instance.
|
(package private) static String |
NODE
Entry in file represents a node.
|
(package private) static String |
OPERAND_MATCHES
OPERAND_MATCHES
|
(package private) static String |
TOTAL
Entry in file represents the total number of nodes in the
file.
|
Modifier | Constructor and Description |
---|---|
|
CARTImpl(BufferedReader reader,
int nodes)
Creates a new CART by reading from the given reader.
|
private |
CARTImpl(int numNodes)
Creates a new CART that will be populated with nodes later.
|
|
CARTImpl(URL url)
Creates a new CART by reading from the given URL.
|
Modifier and Type | Method and Description |
---|---|
void |
dumpBinary(DataOutputStream os)
Dumps this CART to the output stream.
|
void |
dumpDot(PrintWriter out)
Dump the CART tree as a dot file.
|
protected String |
dumpDotNodeColor(CARTImpl.Node n) |
protected String |
dumpDotNodeShape(CARTImpl.Node n) |
protected CARTImpl.Node |
getNode(String type,
StringTokenizer tokenizer,
int currentNode)
Gets the node based upon the type and tokenizer.
|
Object |
interpret(Item item)
Passes the given item through this CART and returns the
interpretation.
|
static CART |
loadBinary(ByteBuffer bb)
Loads a CART from the input byte buffer.
|
static CART |
loadBinary(DataInputStream is)
Loads a CART from the input stream.
|
protected void |
parseAndAdd(String line)
Creates a node from the given input line and add it to the CART.
|
protected Object |
parseValue(String string)
Coerces a string into a value.
|
static final String TOTAL
static final String NODE
static final String LEAF
static final String OPERAND_MATCHES
CARTImpl.Node[] cart
transient int curNode
public CARTImpl(URL url) throws IOException
url
- the location of the CART dataIOException
- if errors occur while reading the datapublic CARTImpl(BufferedReader reader, int nodes) throws IOException
reader
- the source of the CART datanodes
- the number of nodes to read for this cartIOException
- if errors occur while reading the dataprivate CARTImpl(int numNodes)
numNodes
- the number of nodespublic void dumpBinary(DataOutputStream os) throws IOException
dumpBinary
in interface CART
os
- the output streamIOException
- if an error occurs during outputpublic void dumpDot(PrintWriter out)
The dot tool is part of the graphviz distribution at http://www.graphviz.org/. If installed, call it as "dot -O -Tpdf *.dot" from the console to generate pdfs.
out
- The PrintWriter to write to.protected String dumpDotNodeColor(CARTImpl.Node n)
protected String dumpDotNodeShape(CARTImpl.Node n)
public static CART loadBinary(ByteBuffer bb) throws IOException
bb
- the byte bufferIOException
- if an error occurs during output
Note that cart nodes are really saved as strings that
have to be parsed.public static CART loadBinary(DataInputStream is) throws IOException
is
- the input streamIOException
- if an error occurs during output
Note that cart nodes are really saved as strings that
have to be parsed.protected void parseAndAdd(String line)
line
- a line of input to parseprotected CARTImpl.Node getNode(String type, StringTokenizer tokenizer, int currentNode)
type
- NODE
or LEAF
tokenizer
- the StringTokenizer containing the data to getcurrentNode
- the index of the current node we're looking atprotected Object parseValue(String string)
string
- of the form "type(value)"; for example, "Float(2.3)"WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses