added some tests

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@849 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2002-09-05 17:39:32 +00:00
parent b5d41174ec
commit 41566479b9
10 changed files with 402 additions and 136 deletions

Binary file not shown.

View File

@ -0,0 +1,73 @@
/*
* User: tom
* Date: Sep 5, 2002
* Time: 11:06:40 AM
*/
package test.net.sourceforge.pmd.dcpd;
import net.jini.space.JavaSpace;
import net.jini.core.event.EventRegistration;
import net.jini.core.event.RemoteEventListener;
import net.jini.core.entry.Entry;
import net.jini.core.entry.UnusableEntryException;
import net.jini.core.transaction.Transaction;
import net.jini.core.transaction.TransactionException;
import net.jini.core.lease.Lease;
import net.sourceforge.pmd.cpd.TokenSets;
import net.sourceforge.pmd.dcpd.TokenSetsWrapper;
import java.rmi.MarshalledObject;
import java.rmi.RemoteException;
import java.util.List;
import java.util.Stack;
import java.util.ArrayList;
public class MockJavaSpace implements JavaSpace {
private List tileWrappers;
private List writtenEntries = new ArrayList();
public void setTileWrappers(List tileWrappers) {
this.tileWrappers = tileWrappers;
}
public List getWrittenEntries() {
return this.writtenEntries;
}
// JavaSpace
public EventRegistration notify(Entry entry, Transaction transaction, RemoteEventListener listener, long l, MarshalledObject object) throws TransactionException, RemoteException {
return null;
}
public Entry read(Entry entry, Transaction transaction, long l) throws UnusableEntryException, TransactionException, InterruptedException, RemoteException {
return null;
}
public Entry readIfExists(Entry entry, Transaction transaction, long l) throws UnusableEntryException, TransactionException, InterruptedException, RemoteException {
return null;
}
public Entry snapshot(Entry entry) throws RemoteException {
return null;
}
public Entry take(Entry entry, Transaction transaction, long l) throws UnusableEntryException, TransactionException, InterruptedException, RemoteException {
Entry result = null;
if (!tileWrappers.isEmpty()) {
result = (Entry)tileWrappers.get(0);
tileWrappers = tileWrappers.subList(1, tileWrappers.size());
}
return result;
}
public Entry takeIfExists(Entry entry, Transaction transaction, long l) throws UnusableEntryException, TransactionException, InterruptedException, RemoteException {
return null;
}
public Lease write(Entry entry, Transaction transaction, long l) throws TransactionException, RemoteException {
writtenEntries.add(entry);
return null;
}
// JavaSpace
}

View File

@ -0,0 +1,76 @@
/*
* User: tom
* Date: Sep 5, 2002
* Time: 11:04:26 AM
*/
package test.net.sourceforge.pmd.dcpd;
import junit.framework.TestCase;
import net.jini.space.JavaSpace;
import net.jini.core.lease.Lease;
import net.sourceforge.pmd.dcpd.*;
import net.sourceforge.pmd.cpd.*;
import java.io.StringReader;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.Stack;
public class TileExpanderTest extends TestCase {
public TileExpanderTest(String name) {
super(name);
}
public void test1() throws Throwable {
TokenSets tokenSets = TileExpanderTest.createTokenSets();
MockJavaSpace space = new MockJavaSpace();
Job job = new Job("foo", new Integer(1));
Occurrences occ = new Occurrences(tokenSets);
int tilesSoFar=0;
List tilesToWrite = new ArrayList();
for (Iterator i = occ.getTiles(); i.hasNext();) {
Tile tile = (Tile)i.next();
TileWrapper tw = new TileWrapper(tile,
marshal(occ.getOccurrences(tile)),
job.id,
TileWrapper.NOT_DONE,
new Integer(tilesSoFar),
null, null);
tilesToWrite.add(tw);
tilesSoFar++;
}
space.setTileWrappers(tilesToWrite);
TileExpander expander = new TileExpander(space, new TokenSetsWrapper(tokenSets, job.id));
expander.expandAvailableTiles();
List writtenEntries = space.getWrittenEntries();
assertEquals(7, writtenEntries.size());
}
public static List marshal(Iterator i) {
List list = new ArrayList();
while (i.hasNext()) {
list.add(i.next());
}
return list;
}
public static TokenSets createTokenSets() throws Throwable {
TokenSets tokenSets = new TokenSets();
TokenList tokenList1 = new TokenList("list1");
JavaTokensTokenizer tokenizer = new JavaTokensTokenizer();
tokenizer.tokenize(tokenList1, new StringReader("public class Foo {}"));
tokenSets.add(tokenList1);
TokenList tokenList2 = new TokenList("list2");
tokenizer.tokenize(tokenList2, new StringReader("public class Bar {}"));
tokenSets.add(tokenList2);
return tokenSets;
}
}

View File

@ -0,0 +1,55 @@
/*
* User: tom
* Date: Sep 5, 2002
* Time: 12:06:26 PM
*/
package test.net.sourceforge.pmd.dcpd;
import junit.framework.TestCase;
import net.sourceforge.pmd.cpd.TokenSets;
import net.sourceforge.pmd.cpd.Occurrences;
import net.sourceforge.pmd.cpd.Tile;
import net.sourceforge.pmd.dcpd.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
public class TileGathererTest extends TestCase {
public TileGathererTest(String name) {
super(name);
}
public void test1() throws Throwable {
TokenSets tokenSets = TileExpanderTest.createTokenSets();
MockJavaSpace space = new MockJavaSpace();
Job job = new Job("foo", new Integer(1));
Occurrences occ = new Occurrences(tokenSets);
// do the expansion from 1 to 2 tokens and write those expansions
// back to the mock space so TileGatherer can read them
int tilesSoFar=0;
List tilesToWrite = new ArrayList();
for (Iterator i = occ.getTiles(); i.hasNext();) {
Tile tile = (Tile)i.next();
TileWrapper tw = new TileWrapper(tile,
TileExpanderTest.marshal(occ.getOccurrences(tile)),
job.id,
TileWrapper.NOT_DONE,
new Integer(tilesSoFar),
null, null);
tilesToWrite.add(tw);
tilesSoFar++;
}
space.setTileWrappers(tilesToWrite);
TileExpander expander = new TileExpander(space, new TokenSetsWrapper(tokenSets, job.id));
expander.expandAvailableTiles();
space.setTileWrappers(space.getWrittenEntries());
// now the test
TileGatherer tileGatherer = new TileGatherer(space, job);
Occurrences newOcc = tileGatherer.gather(occ.size());
assertEquals(2, newOcc.size());
}
}

View File

@ -27,7 +27,6 @@ public class DCPD {
private JavaSpace space;
private Job job;
private TokenSetsWrapper tokenSetWrapper;
private Results results;
public DCPD(String javaSpaceURL) {
try {
@ -42,20 +41,21 @@ public class DCPD {
space.write(tokenSetWrapper, null, Lease.FOREVER);
System.out.println("Crunching");
DGST dgst = new DGST(space, job, tokenSetWrapper.tokenSets, 50);
dgst.crunch(new CPDListenerImpl());
System.out.println(render());
DGST dgst = new DGST(space, job, tokenSetWrapper.tokenSets, 2);
Results results = dgst.crunch(new CPDListenerImpl());
System.out.println("Cleaning up");
space.take(tokenSetWrapper, null, 200);
space.take(job, null, 200);
System.out.println(render(results));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Couldn't connect to the space on " + javaSpaceURL);
}
}
public String getImage(Tile tile) {
public String getImage(Tile tile, Results results) {
try {
Iterator i = results.getOccurrences(tile);
TokenEntry firstToken = (TokenEntry)i.next();
@ -68,7 +68,7 @@ public class DCPD {
protected String EOL = System.getProperty("line.separator", "\n");
private String render() {
private String render(Results results) {
StringBuffer sb = new StringBuffer();
for (Iterator i = results.getTiles(); i.hasNext();) {
Tile tile = (Tile)i.next();
@ -81,7 +81,7 @@ public class DCPD {
sb.append("Starting at line " + tok.getBeginLine() + " in " + tok.getTokenSrcID());
sb.append(EOL);
}
sb.append(getImage(tile));
sb.append(getImage(tile, results));
sb.append(EOL);
}
return sb.toString();

View File

@ -25,9 +25,9 @@ public class DCPDWorker {
private TokenSetsWrapper tsw;
private JavaSpace space;
public DCPDWorker() {
public DCPDWorker(JavaSpace space) {
try {
space = Util.findSpace(Util.SPACE_SERVER);
this.space = space;
// register for future jobs
space.notify(new Job(), null, new JobAddedListener(space, this), Lease.FOREVER, null);
// get a job if there are any out there
@ -50,7 +50,8 @@ public class DCPDWorker {
while (true) {
System.out.println("Starting expansion");
doExpansion();
TileExpander te = new TileExpander(space, tsw);
te.expandAvailableTiles();
System.out.println("Done, sleeping");
Thread.currentThread().sleep(1000);
}
@ -60,58 +61,11 @@ public class DCPDWorker {
}
}
private void doExpansion() throws RemoteException, UnusableEntryException, TransactionException, InterruptedException{
Entry twQuery = space.snapshot(new TileWrapper(null, null, currentJob.id, TileWrapper.NOT_DONE, null, null, null));
TileWrapper tileWrapper = null;
while ((tileWrapper = (TileWrapper)space.take(twQuery, null, 10)) != null) {
System.out.println("got " + tileWrapper.tile.getImage());
Occurrences results = expand(tileWrapper);
int expansionIndex = 0;
for (Iterator i = results.getTiles();i.hasNext();) {
Tile tile = (Tile)i.next();
List theseOccurrences = marshal(results.getOccurrences(tile));
TileWrapper newTW = new TileWrapper(tile,
theseOccurrences,
currentJob.id,
TileWrapper.DONE,
tileWrapper.originalTilePosition,
new Integer(expansionIndex),
new Integer(results.size()));
space.write(newTW, null, Lease.FOREVER);
System.out.println("Wrote " + newTW.getExpansionIndexPicture());
expansionIndex++;
}
}
}
private List marshal(Iterator i) {
List list = new ArrayList();
while (i.hasNext()) {
list.add(i.next());
}
return list;
}
private Occurrences expand(TileWrapper tileWrapper) {
Occurrences newOcc = new Occurrences(new CPDNullListener());
for (Iterator i = tileWrapper.occurrences.iterator(); i.hasNext();) {
TokenEntry tok = (TokenEntry)i.next();
TokenList tokenSet = tsw.tokenSets.getTokenList(tok);
if (tokenSet.hasTokenAfter(tileWrapper.tile, tok)) {
TokenEntry token = (TokenEntry)tokenSet.get(tok.getIndex() + tileWrapper.tile.getTokenCount());
// make sure the next token hasn't already been used in an occurrence
if (!newOcc.contains(token)) {
Tile newTile = tileWrapper.tile.copy();
newTile.add(token);
newOcc.addTile(newTile, tok);
}
}
}
return newOcc;
}
public static void main(String[] args) {
new DCPDWorker();
try {
new DCPDWorker(Util.findSpace(Util.SPACE_SERVER));
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -29,26 +29,43 @@ public class DGST {
this.job = job;
}
public void crunch(CPDListener listener) {
public Results crunch(CPDListener listener) {
Occurrences occ = new Occurrences(tokenSets, listener);
try {
scatter(occ);
space.write(job, null, Lease.FOREVER);
expand(occ);
System.out.println("DONE");
} catch (Exception e) {
e.printStackTrace();
}
return results;
}
private void expand(Occurrences occ) throws RemoteException, UnusableEntryException, TransactionException, InterruptedException {
while (!occ.isEmpty()) {
occ = gather(occ.size()-1);
TileGatherer tg = new TileGatherer(space, job);
occ = tg.gather(occ.size());
addToResults(occ);
System.out.println("************* Scatter..gather complete; tile count now " + occ.size());
scatter(occ);
System.out.println("scatter..gather complete; tile count now " + occ.size());
}
}
private void addToResults(Occurrences occ) {
for (Iterator i = occ.getTiles(); i.hasNext();) {
Tile tile = (Tile)i.next();
if (tile.getTokenCount() > this.minimumTileSize) {
for (Iterator j = occ.getOccurrences(tile); j.hasNext();) {
TokenEntry te = (TokenEntry)j.next();
results.addTile(tile, te);
}
}
}
}
private void scatter(Occurrences occ) throws TransactionException, RemoteException {
System.out.println("Scattering");
int tilesSoFar=0;
for (Iterator i = occ.getTiles(); i.hasNext();) {
Tile tile = (Tile)i.next();
@ -59,57 +76,15 @@ public class DGST {
new Integer(tilesSoFar),
null, null);
space.write(tw, null, Lease.FOREVER);
//System.out.println("Scattering " + tw);
tilesSoFar++;
if (tilesSoFar % 10 == 0) {
if (tilesSoFar % 25 == 0) {
System.out.println("Written " + tilesSoFar + " tiles so far");
}
}
System.out.println("Done scattering");
}
private Occurrences gather(int originalOccurrencesCount) throws RemoteException, UnusableEntryException, TransactionException, InterruptedException {
System.out.println("STARTING TO GATHER");
Occurrences occ = new Occurrences(new CPDNullListener());
for (int i=0;i<originalOccurrencesCount; i++) {
// this gets tile (6:0/3:4:0/2:3)
TileWrapper tw = (TileWrapper)space.take(new TileWrapper(null,
null,
job.id,
TileWrapper.DONE,
new Integer(i), null, null), null, Lease.FOREVER);
System.out.println("Took " + tw.getExpansionIndexPicture());
addAllExpansions(i, tw, occ);
}
System.out.println("DONE GATHERING");
return occ;
}
private void addAllExpansions(int originalPosition, TileWrapper firstTileWrapper, Occurrences occ) throws RemoteException, UnusableEntryException, TransactionException, InterruptedException {
addTileWrapperToOccurrences(firstTileWrapper, occ);
for (int i=1; i<firstTileWrapper.expansionsTotal.intValue(); i++) {
TileWrapper nextExpansion = (TileWrapper)space.take(new TileWrapper(null,
null,
firstTileWrapper.jobID,
TileWrapper.DONE,
new Integer(originalPosition),
new Integer(i),
firstTileWrapper.expansionsTotal), null, Lease.FOREVER);
System.out.println("Took " + nextExpansion.getExpansionIndexPicture());
addTileWrapperToOccurrences(nextExpansion, occ);
}
}
private void addTileWrapperToOccurrences(TileWrapper tw, Occurrences occ) {
for (int i=0; i<tw.occurrences.size(); i++) {
if (!occ.containsAnyTokensIn(tw.tile)) {
occ.addTile(tw.tile, (TokenEntry)tw.occurrences.get(i));
}
}
}
private List marshal(Iterator i) {
List list = new ArrayList();
while (i.hasNext()) {
@ -118,22 +93,4 @@ public class DGST {
return list;
}
private void expandTile(Occurrences oldOcc, Occurrences newOcc, Tile tile, CPDListener listener, int tilesSoFar, int totalTiles) {
for (Iterator i = oldOcc.getOccurrences(tile); i.hasNext();) {
TokenEntry tok = (TokenEntry)i.next();
TokenList tokenSet = tokenSets.getTokenList(tok);
if (tokenSet.hasTokenAfter(tile, tok)) {
TokenEntry token = (TokenEntry)tokenSet.get(tok.getIndex() + tile.getTokenCount());
// make sure the next token hasn't already been used in an occurrence
if (!newOcc.contains(token)) {
Tile newTile = tile.copy();
newTile.add(token);
newOcc.addTile(newTile, tok);
listener.addedNewTile(newTile, tilesSoFar, totalTiles);
}
}
}
newOcc.deleteSoloTiles();
}
}

View File

@ -0,0 +1,85 @@
/*
* User: tom
* Date: Sep 5, 2002
* Time: 11:26:28 AM
*/
package net.sourceforge.pmd.dcpd;
import net.jini.core.entry.UnusableEntryException;
import net.jini.core.entry.Entry;
import net.jini.core.transaction.TransactionException;
import net.jini.core.lease.Lease;
import net.jini.space.JavaSpace;
import net.sourceforge.pmd.cpd.*;
import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
public class TileExpander {
private JavaSpace space;
private TokenSetsWrapper tsw;
public TileExpander(JavaSpace space, TokenSetsWrapper tsw) {
this.space = space;
this.tsw = tsw;
}
public void expandAvailableTiles() throws RemoteException, UnusableEntryException, TransactionException, InterruptedException{
Entry twQuery = space.snapshot(new TileWrapper(null, null, tsw.jobID, TileWrapper.NOT_DONE, null, null, null));
TileWrapper tileWrapperToExpand = null;
while ((tileWrapperToExpand = (TileWrapper)space.take(twQuery, null, 10)) != null) {
//System.out.println("Expanding " + tileWrapperToExpand.tile.getImage());
Occurrences results = expand(tileWrapperToExpand);
int expansionIndex = 0;
for (Iterator i = results.getTiles();i.hasNext();) {
Tile tile = (Tile)i.next();
TileWrapper tileWrapperToWrite = new TileWrapper(tile,
marshal(results.getOccurrences(tile)),
tsw.jobID,
TileWrapper.DONE,
tileWrapperToExpand.originalTilePosition,
new Integer(expansionIndex),
new Integer(results.size()));
space.write(tileWrapperToWrite, null, Lease.FOREVER);
System.out.println("Wrote " + tileWrapperToWrite + "; occurrences = " + tileWrapperToWrite.occurrences.size());
expansionIndex++;
}
}
}
private List marshal(Iterator i) {
List list = new ArrayList();
while (i.hasNext()) {
list.add(i.next());
}
return list;
}
private Occurrences expand(TileWrapper tileWrapper) {
Occurrences newOcc = new Occurrences(new CPDNullListener());
for (Iterator i = tileWrapper.occurrences.iterator(); i.hasNext();) {
TokenEntry tok = (TokenEntry)i.next();
TokenList tokenSet = tsw.tokenSets.getTokenList(tok);
if (tokenSet.hasTokenAfter(tileWrapper.tile, tok)) {
TokenEntry token = (TokenEntry)tokenSet.get(tok.getIndex() + tileWrapper.tile.getTokenCount());
// make sure the next token hasn't already been used in an occurrence
if (!newOcc.contains(token)) {
Tile newTile = tileWrapper.tile.copy();
newTile.add(token);
newOcc.addTile(newTile, tok);
}
} else {
// we have to put something back in the space to let DGST know that
// this tile has been processed... so let's just put a one occurrence tile in there
Tile newTile = tileWrapper.tile.copy();
newOcc.addTile(newTile, tok);
}
}
return newOcc;
}
}

View File

@ -0,0 +1,70 @@
/*
* User: tom
* Date: Sep 5, 2002
* Time: 12:03:03 PM
*/
package net.sourceforge.pmd.dcpd;
import net.sourceforge.pmd.cpd.Occurrences;
import net.sourceforge.pmd.cpd.CPDNullListener;
import net.sourceforge.pmd.cpd.TokenEntry;
import net.sourceforge.pmd.cpd.Results;
import net.jini.core.entry.UnusableEntryException;
import net.jini.core.transaction.TransactionException;
import net.jini.core.lease.Lease;
import net.jini.space.JavaSpace;
import java.rmi.RemoteException;
public class TileGatherer {
private JavaSpace space;
private Job job;
public TileGatherer(JavaSpace space, Job job) {
this.space = space;
this.job = job;
}
public Occurrences gather(int originalOccurrencesCount) throws RemoteException, UnusableEntryException, TransactionException, InterruptedException {
Occurrences occ = new Occurrences(new CPDNullListener());
for (int i=0;i<originalOccurrencesCount; i++) {
TileWrapper tw = (TileWrapper)space.take(new TileWrapper(null,
null,
job.id,
TileWrapper.DONE,
new Integer(i), new Integer(0), null), null, Lease.FOREVER);
addAllExpansions(i, tw, occ);
}
return occ;
}
private void addAllExpansions(int originalPosition, TileWrapper firstTileWrapper, Occurrences occ) throws RemoteException, UnusableEntryException, TransactionException, InterruptedException {
TileWrapper nextExpansion = firstTileWrapper;
for (int i=0; i<firstTileWrapper.expansionsTotal.intValue(); i++) {
if (i>0) {
nextExpansion = (TileWrapper)space.take(new TileWrapper(null,
null,
firstTileWrapper.jobID,
TileWrapper.DONE,
new Integer(originalPosition),
new Integer(i),
firstTileWrapper.expansionsTotal), null, Lease.FOREVER);
}
//System.out.println("Gathered " + nextExpansion + "; occurrences = " + nextExpansion.occurrences.size());
// here's where we discard solo tiles
if (nextExpansion.occurrences.size() > 1) {
addTileWrapperToOccurrences(nextExpansion, occ);
}
}
}
private void addTileWrapperToOccurrences(TileWrapper tw, Occurrences occ) {
for (int i=0; i<tw.occurrences.size(); i++) {
if (!occ.containsAnyTokensIn(tw.tile)) {
occ.addTile(tw.tile, (TokenEntry)tw.occurrences.get(i));
}
}
}
}

View File

@ -19,9 +19,7 @@ public class TileWrapper implements Entry {
public Integer jobID;
public List occurrences;
public Integer isDone;
public Integer originalTilePosition;
public Integer expansionIndex;
public Integer expansionsTotal;
@ -32,18 +30,16 @@ public class TileWrapper implements Entry {
this.jobID = jobID;
this.occurrences = occurrences;
this.isDone = isDone;
this.originalTilePosition = originalTilePosition;
this.expansionIndex = expansionIndex;
this.expansionsTotal = expansionsTotal;
}
public String toString() {
return "TileWrapper " + tile.getImage() + ":" + jobID + ":" + occurrences.size() + ":" + isDone + ":" + getExpansionIndexPicture();
return tile.getImage() + ":" + isDone + ":" + getExpansionIndexPicture();
}
public String getExpansionIndexPicture() {
return "(" + originalTilePosition + "->" + expansionIndex + "/" + expansionsTotal + ")";
return "(" + originalTilePosition + ":" + expansionIndex + "/" + expansionsTotal + ")";
}
}