Commit 6ff157ff authored by Linxiao Zeng's avatar Linxiao Zeng

Reform LoadConfiguration with better method

parent fad59d46
##########################
## Java
##########################
*.class
.mtj.tmp/
#*.jar
*.war
*.ear
hs_err_pid*
##########################
## Maven
##########################
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
##########################
## IntelliJ
##########################
*.iml
.idea/
*.ipr
*.iws
out/
.idea_modules/
##########################
## Eclipse
##########################
.metadata
.classpath
.project
.settings/
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.loadpath
##########################
## NetBeans
##########################
nbproject/private/
build/
nbbuild/
dist/
nbdist/
nbactions.xml
nb-configuration.xml
##########################
## OS X
##########################
.DS_Store
transf:transfo-1;[produit1:1];[ligne2:produit2:2];2000;.02;(200,100)
transf:entree-1;[];[produit1:1];0;0;(100,100)
transf:sortie-1;[produit2:1];[];0;0;(300,100)
ligne:ligne1;entree-1;transfo-1;5
ligne:ligne2;transfo-1;sortie-1;1
\ No newline at end of file
transf:transfo_1;[produit1:1];[ligne2:produit2:2];2000;.02;(200,100)
transf:entree_1;[];[ligne1:produit1:1];0;0;(100,100)
transf:sortie_1;[produit2:1];[];0;0;(300,100)
ligne:ligne1;entree_1;transfo_1;5
ligne:ligne2;transfo_1;sortie_1;1
\ No newline at end of file
package gui;
public class InOutput {
private String type;
private int quantity;
public InOutput(String type, int quantity) {
super();
this.type = type;
this.quantity = quantity;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
@Override
public String toString(){
return "Input:(type:"+this.type+", quantity"+this.quantity+")";
}
}
......@@ -4,12 +4,17 @@ import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LoadConfiguration {
public static void main(String[] args) {
loadConfig();
}
public static void loadConfig() {
// TODO Auto-generated method stub
try {
FileReader fr = new FileReader("conf.txt");
......@@ -19,30 +24,30 @@ public class LoadConfiguration {
// System.out.println("lu: "+ligne);
String [] t = ligne.split(":",2);
if(t[0].equals("transf")) {
String [] params = t[1].split(";");
if(params.length == 6) {
String name = params[0];
//input
//output
int processTime = Integer.parseInt(params[3]);
double errorProb = Double.parseDouble(params[4]);
//position
Matcher m = Pattern.compile("\\((\\d+),(\\d+)\\)").matcher(params[5]);
int positionx=0,positiony=0;
while(m.find()) {
// System.out.println(" goupe 1:" + m.group(1));
// System.out.println(" goupe 2:" + m.group(2));
positionx = Integer.parseInt(m.group(1));
positiony = Integer.parseInt(m.group(2));
}
//
System.out.println(name+":"+"pT#"+processTime+",errorPb#"+errorProb+",position#("+positionx+","+positiony+")");
}else {
System.out.println("file is not in correct format!");
throw new IOException("file is not in correct format, can't parse!");
}
// // case: Type Machine, i.e. Entree Transfo Sortie
Matcher m = Pattern.compile("(\\w*);\\[(.*)];\\[(.*)];(\\d*);([\\d.]*);\\((\\d*),(\\d*)\\)").matcher(t[1]);
while(m.find()) {
System.out.println("name:" + m.group(1)); //String
System.out.println(" input:" + m.group(2)); //List<Input>: (productType,quantity)
System.out.println(" output:" + m.group(3)); //List<Output>: (ligneName,productType,quantity)
System.out.println(" processTime:" + m.group(4)); //int
System.out.println(" errorProb:" + m.group(5)); //double
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);
// handle output List
List<InOutput> outputList = handleList(m.group(3),true);
int processTime = Integer.parseInt(m.group(4));
double errorProb = Double.parseDouble(m.group(4));
int positionx = Integer.parseInt(m.group(6));
int positiony = Integer.parseInt(m.group(7));
System.out.println("-------------------");
}
}else if(t[0].equals("ligne")) {
// case: Type ligne
String [] params = t[1].split(";");
if(params.length == 4) {
String name = params[0];
......@@ -65,5 +70,42 @@ public class LoadConfiguration {
System.err.println("erreur lors de la lecture du fichier");
}
}
/**
*
* a static method for parse the a list of IO elements from IO String
* @param ioString String from which to extract the IO element
* @param isOutput to distinguish the functionality of input/output, false for input String
* */
private static List<InOutput> handleList(String ioString, boolean isOutput){
List<InOutput> inputList = new ArrayList<InOutput>();
if(!ioString.equals("")) {
String[] inoutputs = ioString.split(",");
for(String s:inoutputs) {
if(!isOutput) {
String[] input = s.split(":",2);
// System.out.println(input.length);
InOutput in = new InOutput(input[0], Integer.parseInt(input[1]));
inputList.add(in);
}else {
String[] output = s.split(":",3);
// System.out.println(input.length);
InOutput in = new Output(output[0], output[1], Integer.parseInt(output[2]));
inputList.add(in);
}
}
for(InOutput in:inputList) {
System.out.println(in);
}
}else {
if(!isOutput) {
System.out.println("input is null!");
}else {
System.out.println("output is null!");
}
}
return inputList;
}
}
package gui;
public class Output extends InOutput{
private String ligneName;
public Output(String ligneName, String type, int quantity) {
super(type,quantity);
this.ligneName = ligneName;
}
public String getLigneName() {
return ligneName;
}
public void setLigneName(String ligneName) {
this.ligneName = ligneName;
}
@Override
public String toString(){
return "Output:(name:"+ this.ligneName+", type:"+this.getType()+", quantity"+this.getQuantity()+")";
}
}
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