Skip to content

Commit 418faa0

Browse files
author
khashab2
committed
Merge remote-tracking branch 'origin/master' into obAlgo
2 parents fd3cf63 + 4051a81 commit 418faa0

File tree

11 files changed

+308
-29
lines changed

11 files changed

+308
-29
lines changed

lbjava-examples/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<artifactId>lbjava-project</artifactId>
55
<groupId>edu.illinois.cs.cogcomp</groupId>
6-
<version>1.2.3</version>
6+
<version>1.2.4</version>
77
</parent>
88

99
<modelVersion>4.0.0</modelVersion>
@@ -21,12 +21,12 @@
2121
<dependency>
2222
<groupId>edu.illinois.cs.cogcomp</groupId>
2323
<artifactId>LBJava</artifactId>
24-
<version>1.2.3</version>
24+
<version>1.2.4</version>
2525
</dependency>
2626
<dependency>
2727
<groupId>edu.illinois.cs.cogcomp</groupId>
2828
<artifactId>lbjava-maven-plugin</artifactId>
29-
<version>1.2.3</version>
29+
<version>1.2.4</version>
3030
</dependency>
3131
<dependency>
3232
<groupId>edu.illinois.cs.cogcomp</groupId>
@@ -40,7 +40,7 @@
4040
<plugin>
4141
<groupId>edu.illinois.cs.cogcomp</groupId>
4242
<artifactId>lbjava-maven-plugin</artifactId>
43-
<version>1.2.3</version>
43+
<version>1.2.4</version>
4444
<configuration>
4545
<lbjavaInputFileList>
4646
<param>${project.basedir}/src/main/lbj/BadgesClassifier.lbj</param>

lbjava-mvn-plugin/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>lbjava-project</artifactId>
77
<groupId>edu.illinois.cs.cogcomp</groupId>
8-
<version>1.2.3</version>
8+
<version>1.2.4</version>
99
</parent>
1010

1111
<artifactId>lbjava-maven-plugin</artifactId>
@@ -76,7 +76,7 @@
7676
<dependency>
7777
<groupId>edu.illinois.cs.cogcomp</groupId>
7878
<artifactId>LBJava</artifactId>
79-
<version>1.2.3</version>
79+
<version>1.2.4</version>
8080
<type>jar</type>
8181
<scope>compile</scope>
8282
</dependency>
@@ -95,7 +95,7 @@
9595
<extension>
9696
<groupId>org.apache.maven.wagon</groupId>
9797
<artifactId>wagon-ssh</artifactId>
98-
<version>2.4</version>
98+
<version>2.10</version>
9999
</extension>
100100
</extensions>
101101
<plugins>

lbjava/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<artifactId>lbjava-project</artifactId>
55
<groupId>edu.illinois.cs.cogcomp</groupId>
6-
<version>1.2.3</version>
6+
<version>1.2.4</version>
77
</parent>
88

99
<modelVersion>4.0.0</modelVersion>
@@ -82,7 +82,7 @@
8282
<extension>
8383
<groupId>org.apache.maven.wagon</groupId>
8484
<artifactId>wagon-ssh</artifactId>
85-
<version>2.4</version>
85+
<version>2.10</version>
8686
</extension>
8787
</extensions>
8888
<plugins>

lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/IR/ClassifierName.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package edu.illinois.cs.cogcomp.lbjava.IR;
22

33
import edu.illinois.cs.cogcomp.lbjava.Pass;
4+
import edu.illinois.cs.cogcomp.lbjava.SemanticAnalysis;
5+
import edu.illinois.cs.cogcomp.lbjava.TranslateToJava;
6+
import edu.illinois.cs.cogcomp.lbjava.classify.Classifier;
47

58

69
/**
@@ -24,6 +27,11 @@ public class ClassifierName extends ClassifierExpression
2427
**/
2528
public Name referent;
2629

30+
/**
31+
* Is used to distinguish {@link Classifier}s that are defined as fields.
32+
* Used during {@link TranslateToJava}.
33+
*/
34+
public boolean isField;
2735

2836
/**
2937
* Full constructor. Line and byte offset information is taken from the

lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/IR/SymbolTable.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package edu.illinois.cs.cogcomp.lbjava.IR;
22

33
import java.io.File;
4+
import java.lang.reflect.Field;
45
import java.util.Arrays;
56
import java.util.HashMap;
67
import java.util.HashSet;
@@ -162,7 +163,14 @@ public Type put(String name, Type type) {
162163
* no type information could be found.
163164
**/
164165
public Type get(ClassifierName name) {
165-
return get(name.referent.toString());
166+
String nameStr = name.referent.toString();
167+
Type classifierType = get(nameStr);
168+
try {
169+
Class<?> className = classForName(name);
170+
name.isField = className != null && className.getField(nameStr) != null;
171+
}
172+
catch (NoSuchFieldException e) {}
173+
return classifierType;
166174
}
167175

168176

@@ -322,6 +330,14 @@ public Class<?> classForName(Name name) {
322330
if (s.endsWith(".*")) prefixes.add(s.substring(0, s.length() - 1));
323331
else if (s.endsWith("." + name.name[0]))
324332
prefixes.add(s.substring(0, s.length() - name.name[0].length()));
333+
334+
// Try to see if this is static field in one of the imported classes
335+
try {
336+
Class<?> importedClass = Class.forName(s);
337+
Field field = importedClass.getField(name.toString());
338+
if (field != null) result = importedClass;
339+
}
340+
catch (Exception e) { }
325341
}
326342

327343
for (Iterator<String> I = prefixes.iterator(); I.hasNext() && result == null; ) {
@@ -363,7 +379,17 @@ else if (s.endsWith("." + name.name[0]))
363379
catch (Exception e) { }
364380
catch (NoClassDefFoundError e) { }
365381
}
366-
}
382+
383+
// Try to see if this is static field in one of the imported classes (prefixes)
384+
if (prefix.isEmpty()) continue;
385+
try {
386+
Class<?> importedClass = Class.forName(prefix.substring(0, prefix.length()-1));
387+
Field field = importedClass.getField(name.toString());
388+
if (field != null) result = importedClass;
389+
}
390+
catch (Exception e) { }
391+
392+
}
367393

368394
return result;
369395
}

lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/TranslateToJava.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,12 @@ public static void generateLearnerBody(PrintStream out,
394394
out.println(" setEncoding(" + lce.featureEncoding + ");");
395395
if (lce.labeler != null)
396396
out.println(" setLabeler(new " + lce.labeler.name + "());");
397-
out.println(" setExtractor(new " + lce.extractor.name + "());");
397+
if (isField(lce.extractor)) {
398+
String fieldClass = AST.globalSymbolTable.classForName(lce.extractor.name).getSimpleName();
399+
out.println(" setExtractor(" + fieldClass + "." + lce.extractor.name + ");");
400+
}
401+
else
402+
out.println(" setExtractor(new " + lce.extractor.name + "());");
398403
out.println(" isClone = false;");
399404
out.println(" }\n");
400405
}
@@ -472,7 +477,12 @@ public static void generateLearnerBody(PrintStream out,
472477
+ AST.globalSymbolTable.getPackage() + "\";");
473478
out.println(tabs + "name = \"" + lceName + "\";");
474479
out.println(tabs + "setLabeler(new " + lce.labeler.name + "());");
475-
out.println(tabs + "setExtractor(new " + lce.extractor.name + "());");
480+
if (isField(lce.extractor)) {
481+
String fieldClass = AST.globalSymbolTable.classForName(lce.extractor.name).getSimpleName();
482+
out.println(tabs + "setExtractor(" + fieldClass + "." + lce.extractor.name + ");");
483+
}
484+
else
485+
out.println(tabs + "setExtractor(new " + lce.extractor.name + "());");
476486
tabs = "\t\t";
477487
out.println(tabs + "}\n");
478488
out.println(tabs + "isClone = false;");
@@ -794,6 +804,9 @@ public static void generateLearnerBody(PrintStream out,
794804
}
795805
}
796806

807+
private static boolean isField(ClassifierExpression ce) {
808+
return ce instanceof ClassifierName && ((ClassifierName) ce).isField;
809+
}
797810

798811
/**
799812
* This method generates a string signature of the given method. The
@@ -1830,11 +1843,17 @@ public boolean accept(File directory, String name) {
18301843
for (ClassifierExpressionList.ClassifierExpressionListIterator I =
18311844
cg.components.listIterator();
18321845
I.hasNext(); ) {
1833-
String name = I.nextItem().name.toString();
1846+
ClassifierExpression ce = I.nextItem();
1847+
String name = ce.name.toString();
18341848
if (declared.add(name)) {
1835-
String nameNoDots = name.replace('.', '$');
1836-
out.println(" private static final " + name + " __" + nameNoDots
1837-
+ " = new " + name + "();");
1849+
String nameNoDots = name.replace('.', '$');
1850+
if (isField(ce)) {
1851+
String fieldClass = AST.globalSymbolTable.classForName(ce.name).getSimpleName();
1852+
out.println(" private static final " + fieldClass + " __" + nameNoDots + " = " + fieldClass + "." + name + ";");
1853+
}
1854+
else {
1855+
out.println(" private static final " + name + " __" + nameNoDots + " = new " + name + "();");
1856+
}
18381857
}
18391858
}
18401859
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package edu.illinois.cs.cogcomp.lbjava;
2+
3+
import edu.illinois.cs.cogcomp.lbjava.IR.*;
4+
import edu.illinois.cs.cogcomp.lbjava.frontend.Yylex;
5+
import edu.illinois.cs.cogcomp.lbjava.frontend.parser;
6+
import org.junit.After;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
import java.io.ByteArrayInputStream;
11+
import java.io.File;
12+
import java.io.FilenameFilter;
13+
import java.lang.reflect.Method;
14+
import java.net.URL;
15+
import java.net.URLClassLoader;
16+
import java.util.HashSet;
17+
18+
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertTrue;
20+
21+
/**
22+
* Tests the main functions of LBJava:
23+
* <ul>
24+
* <li>{@link SemanticAnalysis}</li>
25+
* <li>{@link ClassifierCSE}</li>
26+
* <li>{@link RevisionAnalysis}</li>
27+
* <li>{@link TranslateToJava}</li>
28+
* </ul>
29+
*
30+
* @author Christos Christodoulopoulos
31+
*/
32+
public class MainTest {
33+
34+
private String generateLBJavaScript(String learnerName, String extractor) {
35+
return "import java.util.Vector;\n" +
36+
"import edu.illinois.cs.cogcomp.lbjava.VectorParser;\n" +
37+
"import edu.illinois.cs.cogcomp.lbjava.PredefinedFeature;\n" +
38+
"import edu.illinois.cs.cogcomp.lbjava.PredefinedLabel;\n" +
39+
"\n" +
40+
"discrete "+learnerName+"(Vector v) <-\n" +
41+
"learn PredefinedLabel\n" +
42+
"\tusing "+extractor+"\n" +
43+
"\tfrom new VectorParser(\"target/test-classes/test1.train\")\n" +
44+
"\twith new NaiveBayes()\n" +
45+
"\ttestFrom new VectorParser(\"target/test-classes/test2.train\")\n" +
46+
"end";
47+
}
48+
49+
@Before
50+
public void setUp() throws Exception {
51+
Main.fileNames = new HashSet<>();
52+
Main.generatedSourceDirectory = "target/test-classes/lbj";
53+
Main.classDirectory = "target/test-classes";
54+
Main.classPackageDirectory = "target/test-classes/lbj";
55+
Main.sourceDirectory = "target/test-classes/lbj";
56+
57+
// The auto-generated code directory needs to be added to classpath
58+
URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
59+
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
60+
method.setAccessible(true);
61+
method.invoke(urlClassLoader, getClass().getResource("/lbj/."));
62+
}
63+
64+
@Test
65+
public void testOneFeature() throws Exception {
66+
String input = generateLBJavaScript("OneFeatLearner", "testFeature1");
67+
Yylex scanner = new Yylex(new ByteArrayInputStream(input.getBytes()));
68+
AST ast = (AST) new parser(scanner).parse().value;
69+
70+
Main.runSemanticAnalysis(ast);
71+
72+
assertEquals(3, SemanticAnalysis.dependorGraph.size());
73+
assertEquals(1, ast.declarations.size());
74+
75+
ASTNode[] astNodes = ast.declarations.iterator().next().iterator().children;
76+
assertEquals(3, astNodes.length);
77+
assertTrue(astNodes[0] instanceof ClassifierReturnType);
78+
assertEquals("discrete", astNodes[0].toString());
79+
assertTrue(astNodes[2] instanceof LearningClassifierExpression);
80+
LearningClassifierExpression lce = (LearningClassifierExpression) astNodes[2];
81+
assertEquals("testFeature1", lce.extractor.getName());
82+
assertTrue(((ClassifierName) lce.extractor).isField);
83+
assertEquals("PredefinedFeature", AST.globalSymbolTable.classForName(lce.extractor.getName()).getSimpleName());
84+
85+
new RevisionAnalysis(ast).run(ast);
86+
new ClassifierCSE(ast).run(ast);
87+
new TranslateToJava(ast).run(ast);
88+
new Train(ast, 0).run(ast);
89+
}
90+
91+
@Test
92+
public void testTwoFeatures() throws Exception {
93+
String input = generateLBJavaScript("TwoFeatLearner", "testFeature1, testFeature2");
94+
Yylex scanner = new Yylex(new ByteArrayInputStream(input.getBytes()));
95+
AST ast = (AST) new parser(scanner).parse().value;
96+
97+
Main.runSemanticAnalysis(ast);
98+
99+
assertEquals(5, SemanticAnalysis.dependorGraph.size());
100+
assertEquals(1, ast.declarations.size());
101+
102+
ASTNode[] astNodes = ast.declarations.iterator().next().iterator().children;
103+
assertEquals(3, astNodes.length);
104+
assertTrue(astNodes[0] instanceof ClassifierReturnType);
105+
assertEquals("discrete", astNodes[0].toString());
106+
assertTrue(astNodes[2] instanceof LearningClassifierExpression);
107+
LearningClassifierExpression lce = (LearningClassifierExpression) astNodes[2];
108+
assertEquals("TwoFeatLearner$$1", lce.extractor.getName());
109+
ClassifierExpressionList components = ((CompositeGenerator) lce.extractor).components;
110+
assertEquals(2, components.size());
111+
ClassifierName component1 = (ClassifierName) components.iterator().next();
112+
assertTrue(component1.isField);
113+
assertEquals("PredefinedFeature", AST.globalSymbolTable.classForName(component1.getName()).getSimpleName());
114+
115+
new RevisionAnalysis(ast).run(ast);
116+
new ClassifierCSE(ast).run(ast);
117+
new TranslateToJava(ast).run(ast);
118+
new Train(ast, 0).run(ast);
119+
}
120+
121+
@After
122+
public void cleanup() {
123+
//Make sure we don't leave our auto-generated files behind
124+
File lbjDir = new File(Main.generatedSourceDirectory);
125+
File[] dirFiles = lbjDir.listFiles(new FilenameFilter() {
126+
127+
public boolean accept(File dir, String name) {
128+
return !name.endsWith(".lbj");
129+
}
130+
});
131+
for (File file: dirFiles) assert file.delete() : "Could not delete file " + file;
132+
}
133+
}

lbjava/src/test/java/edu/illinois/cs/cogcomp/lbjava/NaiveBayesTest.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,13 @@
88
import java.net.URL;
99
import java.net.URLClassLoader;
1010

11+
import org.junit.After;
1112
import org.junit.Test;
1213

1314
public class NaiveBayesTest {
1415

1516
@Test
1617
public void test() throws Exception {
17-
//Make sure the lbjava dir is clean
18-
File lbjDir = new File(getClass().getResource("/lbj/.").getPath());
19-
File[] dirFiles = lbjDir.listFiles(new FilenameFilter() {
20-
21-
public boolean accept(File dir, String name) {
22-
return !name.endsWith(".lbj");
23-
}
24-
});
25-
for (File file: dirFiles) file.delete();
26-
2718
File lbjFile = new File(getClass().getResource("/lbj/naive-bayes.lbj").getFile());
2819

2920
// The auto-generated code directory needs to be added to classpath
@@ -34,6 +25,19 @@ public boolean accept(File dir, String name) {
3425
String[] args = { "-d", lbjFile.getParent(), lbjFile.getPath() };
3526
Main.main(args);
3627
}
28+
29+
@After
30+
public void cleanup() {
31+
//Make sure we don't leave our auto-generated files behind
32+
File lbjDir = new File(Main.generatedSourceDirectory);
33+
File[] dirFiles = lbjDir.listFiles(new FilenameFilter() {
34+
35+
public boolean accept(File dir, String name) {
36+
return !name.endsWith(".lbj");
37+
}
38+
});
39+
for (File file: dirFiles) assert file.delete() : "Could not delete file " + file;
40+
}
3741

3842
@SuppressWarnings({ "unchecked", "rawtypes" })
3943
private static void addPath(URL u) throws Exception {

0 commit comments

Comments
 (0)