-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateMachineWriter.java
More file actions
125 lines (101 loc) · 3.48 KB
/
StateMachineWriter.java
File metadata and controls
125 lines (101 loc) · 3.48 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package io;
import lowlevel.Cluster;
import lowlevel.State;
import lowlevel.StateMachine;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Set;
/**
* Created by Julian Käuser on 03.06.2017.
*/
public class StateMachineWriter {
/**
*
* @param fsm the StateMachine object to write out
* @param destination the directory where the written .kiss2
* file shall be placed
*/
public static void writeFSM(StateMachine fsm, String destination){
if (fsm==null || destination==null){
System.out.println("no fsm written - destination or fsm unknown");
return;
}
// catch the "fsm-has-no-name"-case
if (fsm.name==null){
System.out.println("fsm has no name, name set to time+date of execution");
fsm.name=new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
}
StringBuilder bld = new StringBuilder();
bld.append("# "+fsm.name +" encoded cluster-wise\n");
bld.append(".model "+fsm.name);
bld.append(buildInputs(fsm));
// latch initilizations
String[] latches = getLatches(fsm);
for (String l : latches){
bld.append(".latch "+l+"\n");
}
bld.append(".start_kiss");
bld.append(".i "+fsm.getNumInputs()+"\n");
bld.append(".o "+fsm.getNumOutputs()+"\n");
// states and transitions
//bld.append(".p "+fsm.getNumTransistions()+"\n");
//bld.append(".s "+fsm.getNumStates()+"\n");
//bld.append(".r "+fsm.getResetState());
bld.append(".end_kiss\n");
//latch mapping
bld.append(".latch_order ");
for (int ii=0; ii<latches.length; ii++){
bld.append(latches[ii]);
}
bld.append("\n");
// print codes of states
for (Cluster c : fsm.getClusters()){
for (State s : c.getStateArray()){
bld.append(".code "+s.getName()+" "+c.getCode()+s.getCode()+"\n");
}
}
bld.append(".end");
destination = (destination.endsWith(System.lineSeparator())) ? destination : (destination + System.lineSeparator());
destination += fsm.name+"_clusterEncoded.kiss2";
try (FileWriter out = new FileWriter(destination)) {
BufferedWriter buf = new BufferedWriter(out);
buf.write(bld.toString());
buf.flush();
}
catch (IOException e){
e.printStackTrace();
}
}
private static String[] getLatches(StateMachine fsm){
String[] latches = null;
if (fsm==null) {
latches = new String[1];
latches[0] = "";
return latches;
}
return latches;
}
private static String[] getExternalClusterLatches(StateMachine fsm){
String[] latches;
Set<Cluster> clusters = fsm.getClusters();
latches = new String[clusters.size()];
int ii=0;
for (Cluster cluster : clusters){
latches[ii] = "v"+cluster.getID()+ " ";
}
return latches;
}
private static String[] getClusterInternalLatches(Cluster cluster){
String[] latches = null;
int len;
return latches;
}
private static String buildInputs(StateMachine fsm){
String ins = "";
return ins;
}
}