/**
*
* @author yaemon
*/
package jp.ac.nii.uno.sheap;//megun:パッケージはjp.ac.nii.uno.sheap
import java.io.BufferedReader;
import java.io.File;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.lang.Long;
import java.lang.ProcessBuilder;
import java.lang.Runtime;
import java.lang.String;
import java.lang.Throwable;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import jp.ac.nii.uno.sheap.ProgressLogger;//megun:jp.ac.nii.uno.sheap.ProgressLoggerからインポート
import java.lang.System;
public class DoSachica
{
//いれもの準備ー
private final boolean debug = false; // if true, verbose to stderr. megun:trueだとstderrがうるさいくらいに出る
private final int commandsSteps = 10; // 10 steps of sachica batch without sort(megun:Sachicaをいっぺんに10ステップずつソートする)
private final int copyFileSteps = 2; // 2 sort steps of sachica batch. but not sort, only copy.(megun:二つのSachicaを一括処理)
private byte[] sachicaDataSeparator = { '\n' , '\n' };//megun:sachicaDataSeparator・sachicaを区切る改行タグ
private final String Prefixes[] = //megun:Prefixies−接頭辞
{ // __tmp1__ is merged file・megun:混ざったファイル
// __tmp2__ output repeat string・megun:繰り返す文字列を出力
"sachica_repeated",//sachicaリピート
// __tmp3__ g,G:Exchange A<->T, G<->C for first, or 2nd to last string
"sachica_exchanged",//sachica変換
"sachica_linecount", // __tmp4__ is wc -l//sachicaラインカウント
"sachica_graphic" , // __tmp5__//sachicaグラフィック
"sachica_bold_graphic" // __tmp6__//sachicaボールドグラフィック
};
//いれもの準備完了!
//ファイルカモン♪
public DoSachica( File inputFile1 , File inputFile2 , File outputFile, File logFile,
long dataLength , long outputErrorCount )//DoSachicaメソッド
//ファイルがこなくてがっかりしたらここをよめ。→うーんちょっとよくわかんないなー
throws IOException
{
//ログとり開始〜
ProgressLogger progress = new ProgressLogger( logFile );//進行ログ
//念のため日付をいれときますか。
Date now = new Date();//megun:現時刻の取得
String tmpFileSuffix = Long.toString( now.getTime());//mgn:時刻を文字列化・suffixは接尾詞
//デバックモードだったら丁寧に頼む。
if ( debug )//mgn:debugがtrueの場合
{
System.err.println( "tmpFileSuffix = " + tmpFileSuffix );//mgn:tmpFileSuffixを表示
}
//megun:tmpFiles, Commands, fileToCopyを宣言
ArrayList<File> tmpFiles = new ArrayList<File>( Prefixes.length ); //megun:tenpFiles=Prefixesの長さを求める ※複数形!!!
ArrayList<String[]> Commands = new ArrayList<String[]>( commandsSteps ); //mgn:Commands=commandsSteps(38行目)をリスト化
ArrayList<String[]> fileToCopy = new ArrayList<String[]>( copyFileSteps ); //mgn:fileToCopy=copyFileSteps(39行目)をリスト化
//さてそろそろはじめるかー
try
{
//ファイルが二個ありますね。
File inputToSachica = mergeInputFile( inputFile1, inputFile2 , tmpFileSuffix ); //mgn:inputToSachica=ファイル1+ファイル2+imtFileSuffixを混ぜる
//比較コマンドさんに助けをもとめた!
initCommandsList( Commands , fileToCopy, tmpFiles ,
inputToSachica.getAbsolutePath() , outputFile.getAbsolutePath(),//mgn:入出力ファイルの絶対パスを取得
Long.toString( dataLength ) ,//mgn:データの長さを文字列化
Long.toString( outputErrorCount ) ,//mgn:エラーの数を文字列化
tmpFileSuffix );//mgn:tmpFileSuffix
//デバックモードならここを丁寧にね。
if ( debug )//mgn:debugがtrueの場合
{
printCommands( Commands );//mgn:Commandsを表示
}
//子供をつくった〜
doChildProcess( Commands , /*progress*/prosess );//mgn:208行目にdoChildProcessメソッド
fileCopyWithoutSort( fileToCopy , progress);//232行目からfileCopyWithoutSortメソッド
if ( ! debug )//mgm:debugがfalseの場合・デバッグモードじゃない時
{
inputToSachica.delete();//mgn:デバッグしてないと消去
}
}
catch( IOException e )//mgn:例外
{
progress.write( e.getLocalizedMessage() );//mgn:ローカライズされたメッセージを取得
}
// Is it need? mgn:(これいるの?)
finally
{
progress.close();//mgn:進行ログを閉じる
}
}
//入力ファイルを比較するメソッド
private void initCommandsList(//CommandListを初期化
// output
ArrayList<String[]> Commands , ArrayList<String[]> fileToCopy,
// working files
ArrayList<File> tmpFiles,//mgn:複数形
// normal paramator
String inputFileName , String outputFileName ,
String dataLength , String outputErrorCount ,
String tmpFileSuffix)
throws IOException
{
//一時ファイルの名前をつける
ArrayList<String> tmpFilesNames = new ArrayList<String>( Prefixes.length );//mgn:tmpFileName=Prefixesの長さの配列
//
for ( int i = 0 ; i < Prefixes.length ; i++ )//mgn:Prefixesの分まで
{
File tmpFile = File.createTempFile( Prefixes[i], tmpFileSuffix );//mgn:tmpFile=Prefixed配列にtmpFileSuffixを収納 ※単数形!!!!!!
tmpFiles.add( i, tmpFile );//mgn:tmpFilesのi番目にtmpFileを加える
tmpFilesNames.add( i , tmpFile.getPath() );//mgn:tmpFilesNamesのi番目にtmpFileのgetPathを加える
tmpFile.deleteOnExit();//mgn:tmpFileの中身を消す
}
final String CurrentPath = "." + File.separator;//mgn:カレントパス
final String sachica = CurrentPath + "sachica";//.+sachica
final String pairsort = CurrentPath + "pairsort";//.+pairsort
final String sachicax = CurrentPath + "sachicax";//.+sachicax
final String sachicam = CurrentPath + "sachicam";//.+sachicam
// final String sort = CurrentPath + "sort";//.+sort
//Commandsに解析結果を投入
Commands.add( 0, new String[]
{ sachica , "picoNV" , "-s", "'N'", "-k" , "255", "-h",
tmpFilesNames.get(2) , inputFileName, dataLength , outputErrorCount ,
tmpFilesNames.get(0) } );
Commands.add( 1, new String[]
{ sachica , "picoNRGV", "-s", "'N'", "-k" , "255",
inputFileName, dataLength , outputErrorCount ,
tmpFilesNames.get(1) } );
Commands.add( 2, new String[]
{ pairsort , tmpFilesNames.get(0) , outputFileName + ".out" } );
Commands.add( 3, new String[]
{ sachicax , outputFileName + ".out" , "3000", "3" , "300",
outputFileName + "x.out" } );
Commands.add( 4, new String[]
{ pairsort , tmpFilesNames.get(1) , outputFileName + "r.out" } );
Commands.add( 5, new String[]
{ sachicax , outputFileName + "r.out" , "3000" , "3" , "300",
outputFileName + "rx.out" } );
Commands.add( 6, new String[]
{ sachicam , "-H" , tmpFilesNames.get(2), "-R",
outputFileName + "r.out" ,
outputFileName + ".out" ,
"1000" , "0" ,
outputFileName + ".pgm" } );
Commands.add( 7, new String[]
{ sachicam , "-t" , "1" , "-H" ,
tmpFilesNames.get(2), "-R",
outputFileName + "rx.out" ,
outputFileName + "x.out" ,
"1000" , "0" ,
outputFileName + "x.pgm" } );
Commands.add( 8, new String[]
{ sachicam , "-d" , tmpFilesNames.get(3),
outputFileName + "x.out" ,
"1000" , "0" ,
tmpFilesNames.get(4) } );
Commands.add( 9, new String[]
{ sachicam , "-E",
tmpFilesNames.get(3) ,
outputFileName + "rx.out" ,
"1000" , "0" ,
tmpFilesNames.get(4)
} );
/*
// sort -n (GNU) not sort numeric ( with -t"," option too )
Commands.add( 9, new String[]
{ sort , "-n" , tmpFilesNames.get(3) , ">" ,
outputFileName + "_pos.out" } );
Commands.add( 11, new String[]
{ sort , "-n" , tmpFilesNames.get(3) , ">" ,
outputFileName + "r_pos.out" } );
*/
//fileToCopyに追加
fileToCopy.add( 0, new String[]
{ tmpFilesNames.get(3) , outputFileName + "_pos.out" } );
fileToCopy.add( 1, new String[]
{ tmpFilesNames.get(3) , outputFileName + "r_pos.out" } );
}
//子プロセスを作るメソッド(Commands,Progress)
private void doChildProcess( ArrayList<String[]> Commands , ProgressLogger progress )
throws IOException
{
String[] command;//commandsじゃなくてcommand
ProcessBuilder process = new ProcessBuilder( "" );//ProcessBuilderから出てきました
process.redirectErrorStream( true );
for ( Iterator<String[]> walker = Commands.iterator(); walker.hasNext();)//walker数=Commands数になるまで
{
command = walker.next();
process.command( command );
Process child = process.start();//child=スタート
final BufferedReader logReader = new BufferedReader(
new InputStreamReader( child.getInputStream() )
);
String line;//String型の変数line
while( (line = logReader.readLine()) != null )
{
progress.write( line );//lineの情報を書き出す。
}
}
}
private void fileCopyWithoutSort( ArrayList<String[]> fileToCopyPair , ProgressLogger progress )//fileToCopy、 ProgressLogger
throws IOException//ファイルを並べ替えずにコピーするメソッド
{
for ( Iterator<String[]> walker = fileToCopyPair.iterator(); walker.hasNext(); )//walker=fileToCopyPairになるまで
{
String[] pair = walker.next();//pairという名の配列
progress.write( "Create " + pair[1] );//書き出す
File src = new File( pair[0] );//srcとdstを宣言
File dst = new File( pair[1] );
BufferedReader srcStream = new BufferedReader( new FileReader( src ));//入力
BufferedWriter dstStream = new BufferedWriter( new FileWriter( dst ));//出力
String line;//String型・line
while ( ( line = srcStream.readLine() ) != null )//同じ値が出るまで
{
dstStream.write( line );//lineの中を入力
dstStream.newLine();
}
srcStream.close();
dstStream.close();
}
}
private File mergeInputFile( File inputFile1, File inputFile2 , String tmpFileSuffix)
throws IOException
{
if ( debug )
{
System.err.println( "I'm in mergeInputFile " + inputFile1.getPath() + " " + tmpFileSuffix );
}
final String tmpFilePrefix = "sheap";
File inputToSachica = File.createTempFile( tmpFilePrefix, tmpFileSuffix );
if ( debug )
{
System.err.println( "merged file name is " + inputToSachica.getAbsolutePath() );
}
FileOutputStream out = new FileOutputStream( inputToSachica );
FileInputStream in_1 = new FileInputStream( inputFile1 );
dataOnlyWrite( out, in_1 );
in_1.close();
out.write( sachicaDataSeparator );
FileInputStream in_2 = new FileInputStream( inputFile2 );
dataOnlyWrite( out, in_2 );
in_2.close();
// Accept Java's buggy method
// out.flush();
out.close();
return inputToSachica;
}
private void dataOnlyWrite( FileOutputStream out , FileInputStream in )
throws IOException
{
BufferedReader readStream = new BufferedReader( new InputStreamReader( in ));
BufferedWriter outStream = new BufferedWriter( new OutputStreamWriter( out ));
String line;
while( ( line = readStream.readLine() ) != null )
{
if ( line.charAt(0) != '>' )
{
outStream.write( line );
outStream.newLine( );
}
}
outStream.flush();
}
// to debug only
private void printCommands( ArrayList<String[]> Commands )
{
String[] command;
for ( Iterator<String[]> walker = Commands.iterator(); walker.hasNext();)
{
command = walker.next();
for ( int i = 0 ; i < command.length ; i++ )
{
System.err.println( command[i] + " " );
}
System.err.println( '\n' );
}
}
}
漏れは生きていていいんでしょうか。久々の鬱期。
最終更新:2008年07月09日 20:57