it.ssc.step.readdata.ReadDataFromFile Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsr331-ssc Show documentation
Show all versions of jsr331-ssc Show documentation
This is a JSR331 interface for SSC (Software for the Calculation of the Simplex) is a java library for solving linear programming problems v. 3.0.1.
SSC was designed and developed by Stefano Scarioli.
The newest version!
package it.ssc.step.readdata;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import it.ssc.context.Config;
import it.ssc.i18n.RB;
import it.ssc.log.SscLogger;
import it.ssc.parser.InputDichiarationInfo;
import it.ssc.parser.InputSubDichiarationAction;
import it.ssc.parser.InputSubDichiarationInterface;
import it.ssc.parser.InputSubDichiarationVar;
import it.ssc.parser.exception.InvalidDateFormatException;
import it.ssc.pdv.PDV;
import it.ssc.pdv.PDVAll;
import it.ssc.pdv.PDVField;
import it.ssc.ref.Input;
import it.ssc.ref.InputFile;
import it.ssc.step.exception.ErrorStepInvocation;
/**
* Leggo una stringa di input, la suddivido in token Mantengo sempre la stringa
* originaria. Vedo se il primo campo e a lettura libera o no. Se si prendo il
* primo token, altrimenti faccio substring della stringa originaria. Leggo il
* valore e lo memorizzo nel PDV.
*
* @author Stefano Scarioli
*
*/
class ReadDataFromFile implements ReadDataInterface {
private static final Logger logger=SscLogger.getLogger();
private InputFile ref_file;
private InputDichiarationInfo info_input_step;
private OptionsRead options_read;
/* RICORDARSI DELLA GESTIONE DEI MISSING */
ReadDataFromFile(Input ref, OptionsRead options_read) {
/*
* Posso tranquillamente fare il cast perche ho controllato a monte,
* nella classe read data, che e' di tipo DataRefFile
*/
this.ref_file = (InputFile) ref;
this.info_input_step = ref_file.getInputDichiarationInfo();
this.options_read=options_read;
}
public PDV createPDV() throws InvalidDateFormatException {
int num_var = info_input_step.getColumnCount();
PDV pdv = new PDV();
pdv.setTokenMissing(this.ref_file.getMissingValue());
for (int _a = 0; _a < num_var; _a++) {
String name_var = info_input_step.getDichiarationVar(_a + 1).getNameVar();
Class type_var=info_input_step.getDichiarationVar(_a + 1).getTypeVar();
PDVField pdv_field = pdv.addNewField(name_var,type_var);
pdv_field.lentgh_field = info_input_step.getDichiarationVar(_a + 1).getLengthVar();
pdv_field.date_format_input = info_input_step.getDichiarationVar(_a + 1).getFormatDate();
pdv_field.manager_missing_value = info_input_step.getDichiarationVar(_a + 1).getSettingMissing();
pdv_field.type_sql=info_input_step.getDichiarationVar(_a + 1).getTypeSql();
}
return pdv;
}
public SourceDataInterface getSourceData() throws IOException {
return new SourceData(ref_file.getFile());
}
private final class SourceData implements SourceDataInterface {
private ArrayList list_input_step;
private BufferedReader buff;
private String line_memorized;
private String substring_start_end;
private Pattern separator = ref_file.getSeparator();
private long obs_lette;
private long max_obs_read;
private Iterator iter_step;
private int star_column , end_column, point_row = 0, index_array_pdv;
private Scanner scanner_str;
private boolean log_active;
private SourceData(File file) throws IOException {
this.buff = new BufferedReader(new FileReader(file));
this.obs_lette=0;
this.log_active=true;
this.list_input_step = info_input_step.getInputDichiarationInfo();
this.max_obs_read=options_read.getMaxObsRead();
}
public void setLogActive(boolean active) {
log_active=active;
}
public boolean readFromSourceWriteIntoPDV(PDVAll pdv) throws Exception {
star_column = 0;
end_column = 0;
String name_var = null;
iter_step = list_input_step.iterator();
scanner_str = null;
line_memorized = null;
//commentato il 23/09/2012 poiche trasferito in trsformationData
//pdv.setRecordDeleted(false);
if(max_obs_read!=-1 && obs_lette >= max_obs_read) return false;
try {
while (iter_step.hasNext()) {
InputSubDichiarationInterface isi = iter_step.next();
if (isi.getTypeInputStep() == InputSubDichiarationInterface.TYPE_INPUT_STEP.DICHIARATION_VAR) {
InputSubDichiarationVar isd = (InputSubDichiarationVar) isi;
name_var = isd.getNameVar();
if (isd.isColumnFormat()) {
star_column = isd.getStartColumn();
end_column = isd.getEndColumn();
if (line_memorized == null) {
if ((line_memorized = buff.readLine()) == null) {
return false;
}
}
try {
substring_start_end = line_memorized.substring(star_column - 1, end_column);
pdv.getField(name_var).loadValue(substring_start_end);
}
catch(java.lang.StringIndexOutOfBoundsException sbo) {
logger.log(Level.WARNING,"Indice della stringa fuori dalla lunhezza del record.+ " +Config.NL+
" All'osservazione "+(this.obs_lette+1)+" " +sbo.getMessage()+
". Il valore della variabile "+name_var+" e' stata settato a null");
pdv.getField(name_var).is_null=true;
}
catch(java.lang.NumberFormatException nfe) {
logger.log(Level.WARNING,"All'osservazione "+(this.obs_lette+1)+" " +nfe.getMessage()+
". Il valore della variabile "+name_var+" e' stata settata a null");
pdv.getField(name_var).is_null=true;
}
}
// Tipo di formato di input libero
else {
if (line_memorized == null) {
if ((line_memorized = buff.readLine()) == null) {
return false;
}
}
if (end_column != 0) {
if (scanner_str != null) scanner_str.close();
scanner_str = new Scanner(line_memorized.substring(end_column)).useDelimiter(separator);
end_column = 0;
}
else if (scanner_str == null) {
scanner_str = new Scanner(line_memorized).useDelimiter(separator);
}
try {
pdv.getField(name_var).loadValueFromScanner(scanner_str);
}
catch(java.lang.NumberFormatException nfe) {
logger.log(Level.WARNING,"All'osservazione "+(this.obs_lette+1)+" " +nfe.getMessage()+
". Il valore della variabile "+name_var+" e' stato settato a null");
pdv.getField(name_var).is_null=true;
}
}
index_array_pdv++;
}
//se di tipo InputSubDichiarationInterface.TYPE_INPUT_STEP.DICHIARATION_ACTION
else {
end_column = 0;
if (scanner_str != null) scanner_str.close();
scanner_str = null;
InputSubDichiarationAction ise = (InputSubDichiarationAction) isi;
if( ise.isReadLineNext()) {
if ((line_memorized = buff.readLine()) == null) {
return false;
}
}
else if ((point_row = ise.getPointRow()) > 0) {
while (point_row > 0) {
point_row--;
if ((line_memorized = buff.readLine()) == null) {
return false;
}
}
}
else if ((end_column = (ise.getPointColumn()-1)) >= 0) ;
}
}
if (scanner_str != null) scanner_str.close();
}
catch (Exception e) {
//logger.log(Level.SEVERE,"Interrotta la lettura (obs:"+(this.obs_lette+1)+") del file di input "+ ref_file.getFile().getAbsolutePath());
logger.log(Level.SEVERE,RB.format("it.ssc.step.readdata.ReadDataFromFile.msg1",(this.obs_lette+1))+ ref_file.getFile().getAbsolutePath());
throw e;
}
obs_lette++;
return true;
}
public void readNullFromSourceWriteIntoPDV(PDVAll pdv) throws ErrorStepInvocation {
throw new ErrorStepInvocation("Questo metodo non dovrebbe essere mai richiamato sourcedata.readNullFromSourceWriteIntoPDV(PDVAll pdv)");
}
public void close() throws IOException {
if(log_active) logger.log(Level.INFO,RB.format("it.ssc.step.readdata.ReadDataFromFile.msg2")+" "+ref_file.getFile().getAbsolutePath()+" :"+obs_lette);
if (buff != null) {
buff.close();
}
}
}
}