/*
* Created on Dec 16, 2003
*/
package hu.u_szeged.nbo.geometria.main;
import hu.u_szeged.nbo.geometria.element.GeomPlace;
import hu.u_szeged.nbo.geometria.element.GeomTeam;
import hu.u_szeged.nbo.geometria.element.GeomWork;
import hu.u_szeged.nbo.geometria.exception.GeomFileNotFoundException;
import hu.u_szeged.nbo.geometria.exception.GeomWrongFileTypeException;
import hu.u_szeged.nbo.geometria.exception.GeomWrongIndexException;
import hu.u_szeged.nbo.geometria.type.GeomHalmaz;
import hu.u_szeged.nbo.geometria.type.GeomIntMatrix;
import hu.u_szeged.nbo.geometria.type.GeomIntVektor;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import java.util.TreeMap;
/**
* @author Sindely D�niel
*/
public class GeomResources {
// a felhaszn�lhat� elemek:
private TreeMap places;
private TreeMap teams;
private TreeMap works;
public GeomIntMatrix matrixRoadCost;
public GeomIntMatrix matrixTypePair;
GeomIntVektor placeIndexes;
GeomIntVektor teamTypeIndexes;
GeomIntVektor workTypeIndexes;
private double nagyM = 1.0;
private double nagyP = 1.0;
private double nagyU = 1.0;
private double nagyB = 1.0;
private int nagyT = 60;
private int kisP = 1;
private int gamma = 15;
private int epszilon = gamma/2;
private double kappa = gamma/2;
private double lambda = 1;
private int xPercent = 10;
private int rPercent = 100;
private int hp = 2;
private int K = 0;
private int L = 0;
private int maxRoadCost = 0;
private GeomLogger logger = GeomLogger.getInstance();
private static GeomResources instance = null;
public static GeomResources getInstance(){
return instance;
}
public GeomResources(String path) throws GeomWrongFileTypeException, IOException, GeomWrongIndexException {
if(instance == null) instance = this;
//path = "resources/" + path + "/";
path +="/";
setParams(path + "params.lst");
places = GeomPlace.beolvas(path + "places.lst");
placeIndexes = GeomPlace.getPlaceIndexes();
matrixRoadCost = GeomIntMatrix.beolvas(path + "roadcost.mtx");
int dif = matrixRoadCost.getIndexX().searchDiff(placeIndexes);
if(dif==-1){
throw new GeomWrongFileTypeException(GeomWrongFileTypeException.INVALID_PLACE_INDEX_NUM_EXCEPTION, path+"roadcost.mtx", 2);
}else if(dif!=0){
throw new GeomWrongIndexException(GeomWrongIndexException.NO_SPECIFIED_PLACE_INDEX_EXCEPTION, path+"roadcost.mtx", dif+1, matrixRoadCost.getIndexX().getElement(dif));
}
dif = matrixRoadCost.getIndexY().searchDiff(placeIndexes);
if(dif==-1){
throw new GeomWrongFileTypeException(GeomWrongFileTypeException.INVALID_PLACE_INDEX_NUM_EXCEPTION, path+"roadcost.mtx", 1);
}else if(dif!=0){
throw new GeomWrongIndexException(GeomWrongIndexException.NO_SPECIFIED_PLACE_INDEX_EXCEPTION, path+"roadcost.mtx", 1, matrixRoadCost.getIndexY().getElement(dif));
}
countMaxRoadCost(matrixRoadCost);
matrixTypePair = GeomIntMatrix.beolvas(path + "typepair.mtx");
teamTypeIndexes = matrixTypePair.getIndexX();
workTypeIndexes = matrixTypePair.getIndexY();
teams = GeomTeam.beolvas(path + "teams.lst");
works = GeomWork.beolvas(path + "works.lst");
// teszt();
}
/**
*
*/
private void countMaxRoadCost(GeomIntMatrix matrix) {
for(int i=1; i<=matrix.getMeret().getX(); i++){
for(int j=1; j<=matrix.getMeret().getY(); j++){
if(maxRoadCost < matrix.getElement(i, j)) maxRoadCost = matrix.getElement(i, j);
}
}
}
public GeomPlace getPlaceById(int id) throws GeomWrongIndexException{
GeomPlace place = (GeomPlace)places.get(new Integer(id));
if(place!=null){
return place;
} else {
throw new GeomWrongIndexException();
}
}
public GeomTeam getTeamById(int id){
return (GeomTeam)teams.get(new Integer(id));
}
public GeomWork getWorkById(int id){
return (GeomWork)works.get(new Integer(id));
}
public void teszt(){
System.out.println("Tartom�nyok:");
listaz(places);
System.out.println("Er�forr�sok:");
listaz(teams);
System.out.println("Munk�k:");
listaz(works);
System.out.println("K�lts�gm�trix:");
matrixRoadCost.kiir();
System.out.println("K�lts�gm�trix indexX:");
matrixRoadCost.getIndexX().kiir();
System.out.println("K�lts�gm�trix indexY:");
matrixRoadCost.getIndexY().kiir();
System.out.println("T�pusm�trix:");
matrixTypePair.kiir();
System.out.println("T�pusm�trix indexX:");
matrixTypePair.getIndexX().kiir();
System.out.println("T�pusm�trix indexY:");
matrixTypePair.getIndexY().kiir();
System.out.println("Tartom�ny indexek:");
placeIndexes.kiir();
// System.out.println("Er�forr�s indexek:");
// teamIndexes.kiir();
// System.out.println("Munka indexek:");
// workIndexes.kiir();
// System.out.println("Er�forr�s t�pus indexek:");
// teamTypeIndexes.kiir();
// System.out.println("Munka t�pus indexek:");
// workTypeIndexes.kiir();
}
private void listaz(TreeMap map){
Iterator iter = map.values().iterator();
while(iter.hasNext()){
System.out.println(iter.next());
}
System.out.println();
}
/**
* @return Returns the places.
*/
public GeomHalmaz getPlaces() {
return new GeomHalmaz(places.values());
}
/**
* @return Returns the teams.
*/
public GeomHalmaz getTeams() {
return new GeomHalmaz(teams.values());
}
/**
* @return Returns the works.
*/
public GeomHalmaz getWorks() {
return new GeomHalmaz(works.values());
}
/**
* @return Returns the teamTypeIndexes.
*/
public GeomIntVektor getTeamTypeIndexes() {
return teamTypeIndexes;
}
/**
* @return Returns the workTypeIndexes.
*/
public GeomIntVektor getWorkTypeIndexes() {
return workTypeIndexes;
}
/**
* @param string
*/
private void setParams(String fileName) throws IOException, GeomWrongFileTypeException {
int sor = 1;
try{
BufferedReader buf = new BufferedReader(new FileReader(fileName));
int id;
String nev;
String ertek;
StringTokenizer st;
buf.readLine(); //a fejl�c miatt
while(buf.ready()){
sor++;
String s = buf.readLine();
st = new StringTokenizer(s);
if(!st.hasMoreTokens()){
continue;
}
nev = st.nextToken();
if(nev.trim().substring(0,1).equals("#")){
continue;
}
ertek = st.nextToken();
if(nev.trim().equals("M") || nev.trim().toLowerCase().equals("nagym")){
nagyM = Double.parseDouble(ertek);
if(nagyM<0) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NEGATIVE_NUMBER_EXCEPTION, fileName, "nagyM");
}else if(nev.trim().equals("P") || nev.trim().toLowerCase().equals("nagyp")){
nagyP = Double.parseDouble(ertek);
if(nagyP<0) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NEGATIVE_NUMBER_EXCEPTION, fileName, "nagyP");
}else if(nev.trim().equals("U") || nev.trim().toLowerCase().equals("nagyu")){
nagyU = Double.parseDouble(ertek);
if(nagyU<0) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NEGATIVE_NUMBER_EXCEPTION, fileName, "nagyU");
}else if(nev.trim().equals("B") || nev.trim().toLowerCase().equals("nagyb")){
nagyB = Double.parseDouble(ertek);
if(nagyB<0) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NEGATIVE_NUMBER_EXCEPTION, fileName, "nagyB");
}else if(nev.trim().equals("T") || nev.trim().toLowerCase().equals("nagyt")){
nagyT = Integer.parseInt(ertek);
if(nagyT<0) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NEGATIVE_NUMBER_EXCEPTION, fileName, "nagyT");
}else if(nev.trim().equals("p") || nev.trim().toLowerCase().equals("kisp")){
kisP = Integer.parseInt(ertek);
if(kisP<0) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NEGATIVE_NUMBER_EXCEPTION, fileName, "kisP");
}else if(nev.trim().equals("gamma")){
gamma = Integer.parseInt(ertek);
if(gamma<0) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NEGATIVE_NUMBER_EXCEPTION, fileName, "gamma");
}else if(nev.trim().equals("epszilon")){
epszilon = Integer.parseInt(ertek);
// if(epszilon<0) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NEGATIVE_NUMBER_EXCEPTION, fileName, "epszilon");
}else if(nev.trim().equals("kappa")){
kappa = Double.parseDouble(ertek);
// if(kappa<0) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NEGATIVE_NUMBER_EXCEPTION, fileName, "kappa");
}else if(nev.trim().equals("lambda")){
lambda = Double.parseDouble(ertek);
// if(lambda<0) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NEGATIVE_NUMBER_EXCEPTION, fileName, "lambda");
}else if(nev.trim().equals("x%")){
xPercent = Integer.parseInt(ertek);
if(xPercent<0 || xPercent>100) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NOT_0_TO_100_EXCEPTION, fileName, "xPercent");
}else if(nev.trim().equals("r%")){
rPercent = Integer.parseInt(ertek);
if(rPercent<0 || rPercent>100) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NOT_0_TO_100_EXCEPTION, fileName, "rPercent");
}else if(nev.trim().equals("hp")){
hp = Integer.parseInt(ertek);
if(hp<0) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NEGATIVE_NUMBER_EXCEPTION, fileName, "hp");
}else if(nev.trim().equals("K")){
K = Integer.parseInt(ertek);
if(K!=0 && K!=1) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NOT_0_OR_1_EXCEPTION, fileName, sor, "K");
}else if(nev.trim().equals("L")){
L = Integer.parseInt(ertek);
if(L!=0 && L!=1) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NOT_0_OR_1_EXCEPTION, fileName, sor, "L");
}else if(nev.trim().toLowerCase().equals("kapij")){
if(ertek.trim().equals("1")) logger.setKAPijEnabled(true);
else if(!ertek.trim().equals("0")) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NOT_0_OR_1_EXCEPTION, fileName, sor);
}else if(nev.trim().toLowerCase().equals("hozzarendelesimeret")){
if(ertek.trim().equals("1")) logger.setHozzarendMeretEnabled(true);
else if(!ertek.trim().equals("0")) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NOT_0_OR_1_EXCEPTION, fileName, sor);
}else if(nev.trim().toLowerCase().equals("hozzarendelesiarany")){
if(ertek.trim().equals("1")) logger.setHozzarendAranyEnabled(true);
else if(!ertek.trim().equals("0")) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NOT_0_OR_1_EXCEPTION, fileName, sor);
}else if(nev.trim().toLowerCase().equals("tau")){
if(ertek.trim().equals("1")) logger.setTauEnabled(true);
else if(!ertek.trim().equals("0")) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NOT_0_OR_1_EXCEPTION, fileName, sor);
}else if(nev.trim().toLowerCase().equals("hozzarendelesek")){
if(ertek.trim().equals("1")) logger.setHozzarendelesEnabled(true);
else if(!ertek.trim().equals("0")) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NOT_0_OR_1_EXCEPTION, fileName, sor);
}else if(nev.trim().toLowerCase().equals("munkakizarasok")){
if(ertek.trim().equals("1")) logger.setWorkFinishEnabled(true);
else if(!ertek.trim().equals("0")) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NOT_0_OR_1_EXCEPTION, fileName, sor);
}else if(nev.trim().toLowerCase().equals("eroforraskizarasok")){
if(ertek.trim().equals("1")) logger.setTeamFinishEnabled(true);
else if(!ertek.trim().equals("0")) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NOT_0_OR_1_EXCEPTION, fileName, sor);
}else if(nev.trim().toLowerCase().equals("szazalek")){
if(ertek.trim().equals("1")) logger.setPercentEnabled(true);
else if(!ertek.trim().equals("0")) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NOT_0_OR_1_EXCEPTION, fileName, sor);
}else if(nev.trim().toLowerCase().equals("e'halmaz")){
if(ertek.trim().equals("1")) logger.setEvesszoEnabled(true);
else if(!ertek.trim().equals("0")) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NOT_0_OR_1_EXCEPTION, fileName, sor);
}else if(nev.trim().toLowerCase().equals("m'halmaz")){
if(ertek.trim().equals("1")) logger.setMvesszoEnabled(true);
else if(!ertek.trim().equals("0")) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NOT_0_OR_1_EXCEPTION, fileName, sor);
}else if(nev.trim().toLowerCase().equals("parameterek")){
if(ertek.trim().equals("1")) logger.setParametersEnabled(true);
else if(!ertek.trim().equals("0")) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NOT_0_OR_1_EXCEPTION, fileName, sor);
}else if(nev.trim().toLowerCase().equals("hozzarendelesifeladat")){
if(ertek.trim().equals("1")) logger.setHozzarendelesiFeladatEnabled(true);
else if(!ertek.trim().equals("0")) throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NOT_0_OR_1_EXCEPTION, fileName, sor);
}
if(st.hasMoreTokens() && !st.nextToken().startsWith("#")){
throw new GeomWrongFileTypeException(GeomWrongFileTypeException.MUCH_TOKEN_EXCEPTION, fileName, sor);
}
}
} catch (FileNotFoundException e){
throw new GeomFileNotFoundException(fileName);
} catch (NoSuchElementException e){
throw new GeomWrongFileTypeException(GeomWrongFileTypeException.FEW_TOKEN_EXCEPTION, fileName, sor);
} catch (NumberFormatException e){
throw new GeomWrongFileTypeException(GeomWrongFileTypeException.NUM_CONVERT_EXCEPTION, fileName, sor);
}
}
/**
* @return Returns the epszilon.
*/
public int getEpszilon() {
return epszilon;
}
/**
* @return Returns the gamma.
*/
public int getGamma() {
return gamma;
}
/**
* @return Returns the kisP.
*/
public int getKisP() {
return kisP;
}
/**
* @return Returns the nagyP.
*/
public double getNagyP() {
return nagyP;
}
/**
* @return Returns the nagyT.
*/
public int getNagyT() {
return nagyT;
}
/**
* @return Returns the nagyU.
*/
public double getNagyU() {
return nagyU;
}
/**
* @return Returns the kappa.
*/
public double getKappa() {
return kappa;
}
/**
* @return Returns the lambda.
*/
public double getLambda() {
return lambda;
}
/**
* @return Returns the nagyB.
*/
public double getNagyB() {
return nagyB;
}
/**
* @return Returns the nagyM.
*/
public double getNagyM() {
return nagyM;
}
/**
* @return Returns the rPercent.
*/
public int getRPercent() {
return rPercent;
}
/**
* @return Returns the xPercent.
*/
public int getXPercent() {
return xPercent;
}
/**
* @return Returns the hp.
*/
public int getHp() {
return hp;
}
/**
* @return Returns the k.
*/
public int getK() {
return K;
}
/**
* @return Returns the maxRoadCost.
*/
public int getMaxRoadCost() {
return maxRoadCost;
}
/**
* @return Returns the l.
*/
public int getL() {
return L;
}
}