diff --git a/lowlevel_framework/source/src/io/StateMachineWriter.java b/lowlevel_framework/source/src/io/StateMachineWriter.java new file mode 100644 index 0000000..df5ecb6 --- /dev/null +++ b/lowlevel_framework/source/src/io/StateMachineWriter.java @@ -0,0 +1,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 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; + + } +} diff --git a/lowlevel_framework/source/src/lowlevel/Cluster.java b/lowlevel_framework/source/src/lowlevel/Cluster.java new file mode 100644 index 0000000..206884f --- /dev/null +++ b/lowlevel_framework/source/src/lowlevel/Cluster.java @@ -0,0 +1,103 @@ +package lowlevel; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; + +/** + * Created by Julian Käuser on 03.06.2017. + */ +public class Cluster { + + private Set states; + private Set transitions; + + // should be either "binary" or "onehot" + private String internalEncoding; + + private long id; + + private int numInputs; + + public Cluster(){ + this.states = new HashSet(); + this.transitions = new HashSet(); + } + + public boolean addState(State state){ + // System.out.println("-----TEST-----"); + long[][] crappy_transitions= state.getTransitions(); + //ACHTUNG, war NICHT dokumentiert und ist super unsicher + for(int i=0;i getTransitions(){ + return this.transitions; + } + + public int getNumOfTransitions(){ + return this.transitions.size(); + } + + public String getEncodedCluster(){ + StringBuilder bld = new StringBuilder(); + + // ClusterEncoder enc = new ClusterEncoder(); + HashMap map = null; + switch (getEncoding()) { + case "binary": + //map = enc.encodeBinary(this.getStateArray(), this.getInputs()); + case "onehot": + //map = enc.encodeOneHot(this.getStateArray(), this.getInputs()); + } + for (State s : map.keySet()) { + String str = ".code "+s.getName()+" "+map.get(s)+"\n"; + bld.append(str); + } + return bld.toString(); + } + + public String getCode(){ + return ""; + } +} diff --git a/lowlevel_framework/source/src/lowlevel/State.java b/lowlevel_framework/source/src/lowlevel/State.java index fc4ea86..0aa1095 100644 --- a/lowlevel_framework/source/src/lowlevel/State.java +++ b/lowlevel_framework/source/src/lowlevel/State.java @@ -129,9 +129,9 @@ public long[][] getTransitions(){ int i=0; for(Map.Entry entry : nextStateMap.entrySet()){ - transitions[i][0] = this.code; - transitions[i][1] = entry.getKey(); - transitions[i][2] = entry.getValue().getCode(); + transitions[i][0] = this.code; //current State in code ? + transitions[i][1] = entry.getKey(); //Input Code ? + transitions[i][2] = entry.getValue().getCode(); //next State Code ? i++; } diff --git a/lowlevel_framework/source/src/lowlevel/StateMachine.java b/lowlevel_framework/source/src/lowlevel/StateMachine.java new file mode 100644 index 0000000..b4fcc8d --- /dev/null +++ b/lowlevel_framework/source/src/lowlevel/StateMachine.java @@ -0,0 +1,15 @@ +package lowlevel; + +import java.util.Set; + +/** + * Created by Julian Käuser on 03.06.2017. + */ +public class StateMachine { + + private Set clusters; + + public String getEncoding(){ + + } +} diff --git a/lowlevel_framework/source/src/lowlevel/Transition.java b/lowlevel_framework/source/src/lowlevel/Transition.java new file mode 100644 index 0000000..1c76116 --- /dev/null +++ b/lowlevel_framework/source/src/lowlevel/Transition.java @@ -0,0 +1,53 @@ +package lowlevel; + +/** + * Created by theChaoS on 04.06.2017. + */ +public class Transition { + private long input; + + private State targetState; //Ziel + private State originState; + private Cluster targetCluster; + private Cluster originCluster; + + public Transition(long input, State targetState, State startState, Cluster targetCluster, Cluster startCluster){ + this.input=input; + this.targetState=targetState; + this.originState=startState; + this.originCluster=startCluster; + this.targetCluster=targetCluster; + } + + public Transition(long input, State targetState, State startState, Cluster currentCluster){ + this.input=input; + this.targetState=targetState; + this.originState=startState; + this.originCluster=currentCluster; + this.targetCluster=currentCluster; + } + + public long getInput(){ + return this.input; + } + + public String getInputAsBinnary(){ + return Long.toBinaryString(this.input); + } + + public State getTargetState(){ + return this.targetState; + } + + public State getOriginState(){ + return this.originState; + } + + public Cluster getTargetCluster(){ + return this.targetCluster; + } + + public Cluster getOriginCluster(){ + return this.originCluster; + } +} diff --git a/lowlevel_framework/source/src/lowlevel/cluster_encoder.java b/lowlevel_framework/source/src/lowlevel/cluster_encoder.java index 39243fd..76a26bf 100644 --- a/lowlevel_framework/source/src/lowlevel/cluster_encoder.java +++ b/lowlevel_framework/source/src/lowlevel/cluster_encoder.java @@ -1,14 +1,54 @@ package lowlevel; +import java.util.HashMap; + /** + * Julians Idee für diese Klasse: Nehme bis zu drei States, und setze ein One-Hot-Encoding ins Slice. + * -> Ergo 3 Flipflops * Created by theChaoS on 03.06.2017. */ -public class clusterEncoder { +public class ClusterEncoder { + final static int k=6; + final static int nFF=4; + + + public ClusterEncoder(){ + + } + + /** + * Takes the selected states for a cluster, namely in the array states, and packs them into a LUT-feasible + * format. Returns the encoding for this state cluster in one hot + * @param states the states in the cluster, each entry non-null + * @return A mapping of states to strings containing their one-hot value (as chars of {0,1}) + * if encoding is not possible (too many states), return null + */ + public HashMap encodeOneHot (State[] states, int nIn){ + HashMap result = new HashMap(); + + /* + precondition: states have yet been chosen regarding their clusterability + */ + + + + + } + + /** + * Takes the selected states for a cluster, namely in the array states, and packs them into a LUT-feasible + * format. Returns the encoding for this state cluster in binary encoding + * @param states the states in the cluster + * @return A mapping of states to strings containing their binary value (as chars of {0,1}) + */ + public HashMap encodeBinary(State[] states, int nIn){ + HashMap result = new HashMap(); + } } -public class oneHotEncoder extends clusterEncoder{ +public class oneHotEncoder extends ClusterEncoder{ } diff --git a/lowlevel_framework/source/src/main/Main.java b/lowlevel_framework/source/src/main/Main.java index 9665c16..48d4c56 100644 --- a/lowlevel_framework/source/src/main/Main.java +++ b/lowlevel_framework/source/src/main/Main.java @@ -3,6 +3,11 @@ import io.Parser; import lowlevel.ParsedFile; +import java.io.File; +import java.io.FilenameFilter; + +import lowlevel.Cluster; + /** * Main class * @author Wolf & Gottschling @@ -19,8 +24,30 @@ public static void main(String[] args) { String relPath = "\\lowlevel_framework\\benchmarks\\kiss_files\\"; String userDir = System.getProperty("user.dir"); input_file_name = userDir+relPath+input_file_name; - - //ich füge mal nen KOmmentar ein + + File dir = new File(input_file_name); + ParsedFile[] inputFiles; + if (args.length>1 && args[1]!=null && args[1]=="-all" && dir.isDirectory()){ + // args[0] is a directory, and all files of the directory shall be parsed + FilenameFilter filter = new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + if (name.endsWith(".kiss") || name.endsWith(".kiss2")){ + return true; + } + return false; + } + }; + inputFiles = new ParsedFile[dir.listFiles(filter).length]; + int ii=0; + for (File file : dir.listFiles(filter)) { + Parser p = new Parser(); + p.parseFile(file.getAbsolutePath()); + inputFiles[ii]=p.getParsedFile(); + ii++; + } + }// from this point on one could work on the whole folder... just for benchmarking reasons + Parser p = new Parser(); p.parseFile(input_file_name); @@ -30,8 +57,10 @@ public static void main(String[] args) { - // TODO - here you go + // TODO - here you go + Cluster myCluster = new Cluster(); + myCluster.addState(fsm.getStates()[0]); //DAS kommentar !!! } else{ System.out.println("No input argument given");