working on the PathNode includes() logic, and moar documentation and

unit testing
This commit is contained in:
Vincent Batts 2012-11-01 17:14:34 -04:00
parent f06d01718a
commit 4dbdeb4916
7 changed files with 241 additions and 60 deletions

129
pom.xml
View file

@ -1,47 +1,92 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.redhat.trie</groupId>
<artifactId>trie</artifactId>
<version>0.0.1-SNAPSHOT</version>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.redhat.trie</groupId>
<artifactId>trie</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk16</artifactId>
<version>1.44</version>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk16</artifactId>
<version>1.44</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>com.hashbangbash.trie.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<tags>
<tag>
<name>methodundertest</name>
<placement>m</placement>
<head>Method Under Test:</head>
</tag>
<tag>
<name>servicesused</name>
<placement>m</placement>
<head>Services Used:</head>
</tag>
<tag>
<name>testdescription</name>
<placement>m</placement>
<head>Description of Test:</head>
</tag>
<tag>
<name>datadependencies</name>
<placement>m</placement>
<head>Data Dependencies:</head>
</tag>
<tag>
<name>inputs</name>
<placement>m</placement>
<head>Inputs:</head>
</tag>
<tag>
<name>externalconsumers</name>
<placement>m</placement>
<head>External Consumers:</head>
</tag>
<tag>
<name>expectedresults</name>
<placement>m</placement>
<head>Expected Results:</head>
</tag>
</tags>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>com.hashbangbash.trie.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>

View file

@ -36,6 +36,9 @@ public class NodePair implements Comparable {
this.connection = connection;
}
/**
* pretty information
*/
public String toString() {
return "Name: " + name + ", Connection: " + connection.getId();
}

View file

@ -74,7 +74,7 @@ public class PathNode {
/*
* same number of children with the same names for child nodes
*/
boolean isEquivalentTo(PathNode that) {
public boolean isEquivalentTo(PathNode that) {
if (this.getChildren().size() != that.getChildren().size()) {
return false;
}
@ -95,6 +95,47 @@ public class PathNode {
return true;
}
/**
* check whether current PathNode, includes the paths in PathNode that, like a mask.
*
* TODO - this is a stub
*
* @param that PathNode to check for
* @return boolean of truth!
*/
public boolean includes(PathNode that) {
// if we are at the end of the tree we're checking against,
// then it includes everything up to this point.
if (this.getChildren().size() == 0 || that.getChildren().size() == 0) {
return true;
}
// why can java allow a list of primitives ...
List<Boolean> found = new ArrayList<Boolean>();
boolean result;
for (NodePair thisnp : this.getChildren()) {
for (NodePair thatnp : that.getChildren()) {
// keep checking, even if we hist a variablized value
if (thisnp.getName().startsWith("$") || thisnp.getName().equals(thatnp.getName())) {
result = thisnp.getConnection().includes(thatnp.getConnection());
found.add(new Boolean(result).booleanValue());
break;
}
found.add(Boolean.FALSE);
}
}
if (found.contains(Boolean.FALSE)) {
return false;
} else {
return true;
}
}
/**
* pretty information
*/
public String toString() {
String parentList = "";
for (PathNode parent : parents) {

View file

@ -112,8 +112,8 @@ public class PathTree {
*/
public void setPayload(byte[] payload) {
this.modified = true;
setNodeBits(null);
setNodeCount(0);
this.setNodeBits(null);
this.setNodeCount(0);
this.pathNodeContext = new NodeContext();
this.huffNodeContext = new NodeContext();
@ -190,10 +190,10 @@ public class PathTree {
} catch (IOException ex) {
throw new PayloadException();
}
setDictOffset(inf.getBytesRead());
this.setDictOffset(inf.getBytesRead());
int weight = 1;
for (String name : byteArrayToStringList(baos.toByteArray())) {
for (String name : this.byteArrayToStringList(baos.toByteArray())) {
this.pathDictionary.add(new HuffNode(getHuffNodeContext(), name, weight++));
}
this.pathDictionary.add(new HuffNode(HuffNode.END_NODE, weight));
@ -210,17 +210,18 @@ public class PathTree {
*/
private List<HuffNode> getNodeDictionary() throws PayloadException {
if (this.pathDictionary == null) {
getPathDictionary(); // this has to run before the nodeDictionary bits are ready
this.getPathDictionary(); // this has to run before the nodeDictionary bits are ready
}
if (this.modified || this.pathDictionary == null || this.nodeDictionary == null) {
this.nodeDictionary = new ArrayList<HuffNode>();
setNodeBits(new StringBuffer());
this.setNodeBits(new StringBuffer());
ByteArrayInputStream bais = new ByteArrayInputStream(getPayload(),
(new Long(getDictOffset())).intValue(), (new Long(getPayload().length - getDictOffset()).intValue()));
ByteArrayInputStream bais = new ByteArrayInputStream(this.getPayload(),
(new Long(this.getDictOffset())).intValue(),
(new Long(this.getPayload().length - this.getDictOffset()).intValue()));
int value = bais.read();
// check for size bits
setNodeCount(value);
this.setNodeCount(value);
if (value > 127) {
byte[] count = new byte[value - 128];
try {
@ -232,7 +233,7 @@ public class PathTree {
for (int k = 0; k < value - 128; k++) {
total = (total << 8) | (count[k] & 0xFF);
}
setNodeCount(total);
this.setNodeCount(total);
}
value = bais.read();
while (value != -1) {
@ -240,11 +241,11 @@ public class PathTree {
for (int pad = 0; pad < 8 - someBits.length(); pad++) {
this.nodeBits.append("0");
}
this.nodeBits.append(someBits);
this.getNodeBits().append(someBits);
value = bais.read();
}
for (int j = 0; j < getNodeCount(); j++) {
for (int j = 0; j < this.getNodeCount(); j++) {
this.nodeDictionary.add(new HuffNode(new PathNode(getPathNodeContext()), j));
}
}
@ -319,7 +320,7 @@ public class PathTree {
public void setContentSets(List<String> contentSets) throws PayloadException {
this.modified = true;
this.nodeBits = null;
setNodeCount(0);
this.setNodeCount(0);
this.pathNodeContext = new NodeContext();
this.huffNodeContext = new NodeContext();
@ -365,9 +366,9 @@ public class PathTree {
PathNode endMarker = new PathNode(new NodeContext());
for (String path : contents) {
StringTokenizer st = new StringTokenizer(path, "/");
makePathForURL(st, parent, endMarker);
this.makePathForURL(st, parent, endMarker);
}
condenseSubTreeNodes(endMarker);
this.condenseSubTreeNodes(endMarker);
return parent;
}
@ -768,7 +769,7 @@ public class PathTree {
for (NodePair child : parent.getChildren()) {
if (child.getName().equals(childVal) &&
!child.getConnection().equals(endMarker)) {
makePathForURL(st, child.getConnection(), endMarker);
this.makePathForURL(st, child.getConnection(), endMarker);
isNew = false;
}
}
@ -778,7 +779,7 @@ public class PathTree {
next = new PathNode(parent.getContext());
parent.addChild(new NodePair(childVal, next));
next.addParent(parent);
makePathForURL(st, next, endMarker);
this.makePathForURL(st, next, endMarker);
} else {
parent.addChild(new NodePair(childVal, endMarker));
if (!endMarker.getParents().contains(parent)) {

View file

@ -0,0 +1,18 @@
package com.redhat.trie;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
public class TestNodePair {
@Test
public void testNew0() {
NodePair np = new NodePair("foo", new PathNode());
assertNotNull(np);
}
}

View file

@ -0,0 +1,23 @@
package com.redhat.trie;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
public class TestPathNode {
@Test
public void testNew0() {
PathNode pn = new PathNode();
assertNotNull(pn);
}
@Test
public void testNew1() {
PathNode pn = new PathNode(new NodeContext());
assertNotNull(pn);
}
}

View file

@ -0,0 +1,50 @@
package com.redhat.trie;
import java.util.List;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
public class TestPathTree {
@Test
public void testNew0() {
PathTree pt = new PathTree();
assertNotNull(pt);
}
@Test
public void testNew1() {
PathTree pt = new PathTree();
assertNotNull(pt);
}
private InputStream resStream(String filename) {
return getClass().getClassLoader().getResourceAsStream(filename);
}
private List<String> loadContents(String filename) throws IOException {
String content;
List<String> contentList = new ArrayList<String>();
InputStream in = resStream(filename);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
try {
while ((content = br.readLine()) != null) {
contentList.add(content);
}
} catch (IOException ex) {
throw ex;
}
return contentList;
}
}