-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgWriter.java
More file actions
108 lines (79 loc) · 2.14 KB
/
ProgWriter.java
File metadata and controls
108 lines (79 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.io.*;
/**
*
*/
/**
* @author Chris Erlendson
*
*/
public class ProgWriter {
public void writeRec(Node<Attribute> node, BufferedWriter fbw) throws IOException
{
try{
/* answer node */
if (node.getData().getCls() != null) {
fbw.write("\tSystem.out.println(\"" + node.getData().getCls()
+ "\");\n");
} else {
/* question node */
int attr = node.getData().getCol();
for (Iterator<Node<Attribute>> it = node.children.iterator(); it.hasNext();) {
Node<Attribute> child = it.next();
fbw.write("if (input.get(" + attr + ").getValue().equals(\"" +
child.getData().getValue() + "\")) {\n");
writeRec(child, fbw);
fbw.write("}");
if (it.hasNext())
fbw.write(" else ");
}
fbw.newLine();
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
public void write(DecisionTree dt) throws IOException
{
FileWriter fstream = new FileWriter("OutputTree.java",true);
BufferedWriter fbw = new BufferedWriter(fstream);
fbw.write("\n");
writeRec(dt.root, fbw);
fbw.write("\n}\n}\n}");
fbw.close();
}
/**
* @param args
*/
public static void main(String[] args) {
DecisionTree dlt = new DecisionTree();
dlt.parser(args[0]);
InputStream inStream = null;
OutputStream outStream = null;
try{
File afile =new File("template.txt");
File bfile =new File("OutputTree.java");
inStream = new FileInputStream(afile);
outStream = new FileOutputStream(bfile);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = inStream.read(buffer)) > 0){
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
}catch(IOException e){
e.printStackTrace();
}
ProgWriter progWriter = new ProgWriter();
try {
progWriter.write(dlt);
} catch (IOException e) {
e.printStackTrace();
}
}
}