## Friday, October 26, 2012

### Visitor design pattern in python

Visitor pattern uses double dispatch which is simple but powerful.
class Node:
def __init__(self):
self.children = []
self.children.append(node)
def printIt(self):
print("Node")
def accept(self, visitor):
visitor.visit(self)

class NodeA(Node):
def printIt(self):
print("NodeA")

class NodeB(Node):
def printIt(self):
print("NodeB")

class NodeVisitor:
def visit(self, node):
node.printIt()
if len(node.children) > 0:
for child in node.children:
child.accept(self)

if __name__ == "__main__":
n = Node()
n1 = NodeA()
n2 = NodeA()
n3 = NodeB()
n4 = NodeB()

v = NodeVisitor()
v.visit(n)

Node -+- NodeA -+- NodeB
|         +- NodeB
+- NodeA

Node
NodeA
NodeB
NodeB
NodeA


### Get info in the project with eclipse/java

private static Set< ICompilationUnit> getFiles(String projname)
throws CoreException {
IWorkspaceRoot ws = ResourcesPlugin.getWorkspace().getRoot();
IProject proj = ws.getProject(projname);
IJavaProject javaProject = JavaCore.create(proj);
Set< ICompilationUnit> files = new HashSet< ICompilationUnit>();
javaProject.open(new NullProgressMonitor());
for (IPackageFragment packFrag : javaProject.getPackageFragments()) {
for (ICompilationUnit icu : packFrag.getCompilationUnits()) {
}
}
javaProject.close();
return files;
}

ICompilationUnit file = …;
IResource resource = null;
try {
resource = file.getUnderlyingResource();
} catch (JavaModelException e) {
e.printStackTrace();
}
if (resource.getType() == IResource.FILE) {
IFile ifile = (IFile) resource;
String path = ifile.getRawLocation().toString();
}


What it does is get IWorkspaceRoot, and from it, you can get IJavaProject. Then, open with NullProgressMonitor(). For all of the PackageFragment, we can find ICompilationUnit, and add it to the files HashSet.

The issue is how to get the physical path from the compilation unit?
private String getWorkbenchDirectory() throws FileNotFoundException
{
IPath path = Platform.getLocation();
String filePath = path.toOSString();
return filePath;
}


# References

### useful eclipse hotkey

1. F3 - for finding the definition of method

MacJournal

### package in Java/eclipse

1. Source is located in src directory and build result is located in bin directory
2. In source code you don’t have to located the source file following package. For example, you have Hello.java, you don’t have to create directory abc and put the java source code it it when your package name is abc
3. After the compilation, the created class is located in bin/abc/Hello.class,
4. When executing the class in the package you need to specify the package with ‘.’, for example, java abc.Hello for executing it.

MacJournal

### Precision and Recall

From wikipedia

The left side is relevant domain and the circle means retrieved domain.

The perfect case can be the circle encloses only the left hand side, which is not always true, so we have precision and recall.

Precision means how precise we retrieved from samples. It focuses on not retrieving wrong samples. It’s the ratio between green circle and whole circle. Whole circle means what I retrieved and green circle means what I retrieved correctly.

So, precision says how accurate your retrieved result is. The more the less false positive.

When precision is low, it just means you include too many wrong samples.

Recall means how many samples you missed. It focuses on not missing correct samples. Think about the case car company recalls defected cars. It cares about the cars that it needs to recall, it doesn’t care about recalling non-defect cars. It’s the ratio between green circle and left side. Left area means the samples that I need to retrieve.

So, recall says did you good at not letting anything behind. The more the less false negative.

MacJournal article