Commit 55cacc1a authored by Linxiao Zeng's avatar Linxiao Zeng

Use HashMap to store all the config informations

parent 210b28ed
......@@ -5,21 +5,68 @@ import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LoadConfiguration {
public static void main(String[] args) {
loadConfig();
Map<String, Map<String, Map<String, Object>>> confDic = loadConfig("conf.txt");
System.out.println("------------------Return Conf--------------------");
System.out.println("Machines:");
Map<String,Map<String, Object>> Machines = confDic.get("Machines");
Set<Entry<String, Map<String, Object>>> entrys = Machines.entrySet();
for(Entry e:entrys) {
System.out.println("");
System.out.println(e.getKey()+":");//MachineName
Map<String, Object> machineDic = (Map<String, Object>) e.getValue();
Set<Entry<String, Object>> attributs = machineDic.entrySet();
for(Entry entry:attributs) {
System.out.println(" "+entry.getKey()+":"+entry.getValue());//attributName:attribut
}
}
System.out.println("Lignes:");
Map<String,Map<String, Object>> Lignes = confDic.get("Lignes");
Set<Entry<String, Map<String, Object>>> lignesEntrys = Lignes.entrySet();
for(Entry lignesEntry:lignesEntrys) {
System.out.println("");
System.out.println(lignesEntry.getKey()+":");//LignesName
Map<String, Object> ligneDic = (Map<String, Object>) lignesEntry.getValue();
Set<Entry<String, Object>> attributs = ligneDic.entrySet();
for(Entry entry:attributs) {
System.out.println(" "+entry.getKey()+":"+entry.getValue());//attributName:attribut
}
}
}
public static void loadConfig() {
/**
* load configuration of Machines and Lignes from .txt file
* @return a Map: {"Machines": MachinesDic, "Lignes": LignesDic}\
* MachinesDic contains Machines in the form of {name : machine}, in which machine is
* an HashMap: {"inputs":inputList,"outputs":outputList,
* "processTime":pT,"errorProb":eP,"x":x,"y":y}
* LigneList contains Ligne in the form of {name : ligne}, in which ligne is
* an HashMap: {"source":source,"target":target,"capacity":capacity}
* */
public static Map<String, Map<String, Map<String, Object>>> loadConfig(String filePath) {
// TODO Auto-generated method stub
try {
FileReader fr = new FileReader("conf.txt");
FileReader fr = new FileReader(filePath);//"conf.txt"
BufferedReader br = new BufferedReader(fr);
String ligne;
Map<String, Map<String, Map<String, Object>>> confDic = new HashMap<String, Map<String, Map<String, Object>>>();
// List<Map> MachineList = new ArrayList<Map>();
// List<Map> LigneList = new ArrayList<Map>();
Map<String,Map<String, Object>> Machines = new HashMap<String, Map<String, Object>>();
Map<String,Map<String, Object>> Lignes = new HashMap<String, Map<String, Object>>();
while ((ligne=br.readLine())!=null) {
// System.out.println("lu: "+ligne);
String [] t = ligne.split(":",2);
......@@ -35,6 +82,7 @@ public class LoadConfiguration {
System.out.println(" positionx:" + m.group(6)); //int
System.out.println(" positiony:" + m.group(7)); //int
System.out.println("");
String name = m.group(1);
// handle input List
List<InOutput> inputList = handleList(m.group(2),false);
......@@ -45,6 +93,17 @@ public class LoadConfiguration {
int positionx = Integer.parseInt(m.group(6));
int positiony = Integer.parseInt(m.group(7));
System.out.println("-------------------");
Map<String,Object> MachineDic = new HashMap<String, Object>();
// MachineDic.put("name", name);
MachineDic.put("inputs", inputList);
MachineDic.put("outputs", outputList);
MachineDic.put("processTime", processTime);
MachineDic.put("errorProb", errorProb);
MachineDic.put("x", positionx);
MachineDic.put("y", positiony);
// MachineList.add(MachineDic);
Machines.put(name, MachineDic);
}
}else if(t[0].equals("ligne")) {
// case: Type ligne
......@@ -52,9 +111,17 @@ public class LoadConfiguration {
if(params.length == 4) {
String name = params[0];
String Msource = params[1];
String Mcible = params[2];
String Mtarget = params[2];
int capacity = Integer.parseInt(params[3]);
System.out.println(name+":"+Msource+" -> "+Mcible+", capacity#"+capacity);
System.out.println(name+":"+Msource+" -> "+Mtarget+", capacity#"+capacity);
Map<String,Object> LigneDic = new HashMap<String, Object>();
// LigneDic.put("name", name);
LigneDic.put("source", Msource);
LigneDic.put("target", Mtarget);
LigneDic.put("capacity", capacity);
// LigneList.add(LigneDic);
Lignes.put(name, LigneDic);
}else {
System.out.println("file is not in correct format!");
throw new IOException("file is not in correct format, can't parse!");
......@@ -64,11 +131,15 @@ public class LoadConfiguration {
throw new IOException("unknown type, can't parse!");
}
}
confDic.put("Machines", Machines);
confDic.put("Lignes", Lignes);
return confDic;
} catch (FileNotFoundException e) {
System.err.println("lignes.txt pas trouv");
} catch (IOException e) {
System.err.println("erreur lors de la lecture du fichier");
}
return null;
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment