0

Jpl ライブラリ (org.jpl7.*) を使用して Java から参照するプロローグ ファイル (エキスパート システム) があり、プロローグのクエリの出力を表示する UI があります。これは、すべてのコンソール コンテンツをインターフェースにリダイレクトするカスタム出力ストリームです (jTextAreaOUTPUT は、コンテンツをリダイレクトする場所です)。

public class CustomOutputStream extends OutputStream {
 private JTextArea jTextAreaOUTPUT;

 public CustomOutputStream(JTextArea textArea) {
    jTextAreaOUTPUT = textArea;
 }

 @Override
 public void write(int b) throws IOException {
    // redirects data to the text area
    jTextAreaOUTPUT.append(String.valueOf((char)b));
  // scrolls the text area to the end of data
    jTextAreaOUTPUT.setCaretPosition(jTextAreaOUTPUT.getDocument().getLength());
 }
}

これは、インターフェイス クラスにあるいくつかの行です。これは、カスタム出力ストリーム メソッドを呼び出します。

PrintStream printStream = new PrintStream(new CustomOutputStream(jTextAreaOUTPUT), true, "UTF-8");
 // keeps reference of standard output stream
 PrintStream standardOut = System.out;
 System.setOut(printStream);
 System.setErr(printStream);

いくつかの奇妙な理由で、このプロローグ ファイルでは動作しません (他のファイルで試してみましたが動作します): UI がフリーズし、コンテンツが Java コンソール (Eclipse) に表示され続けます。エキスパート システム ファイルはwrite、Prolog の命令で動作します (例: write('Lorem Ipsum')) 。

  1. なぜ standardOut が使用されないのですか? このように宣言してもよろしいですか?
  2. Eclipse コンソールに書き込む必要があるすべてのテキストを強制的にリダイレクトする方法はありますか?

また、プロローグで「 write Stream 」メソッドを使用しようとしましたが、(このプロローグ ファイルのみ、おそらく再帰が原因で) outpus が txt ファイルに書き込まれているにもかかわらず、UI がフリーズします。

4

1 に答える 1

0

ライターが一度に 1 文字ずつ書き込まない場合は、outputstream write(byte[] b), write(byte[] b, int off, int len) の他の書き込み関数をオーバーライドする必要がある場合があります。

OutputStream の他の書き込み関数をオーバーライドするには、既に記述した単一文字関数と同様のコードを提供するだけです。

public class CustomOutputStream extends OutputStream {

    private JTextArea jTextAreaOUTPUT;

    public CustomOutputStream(JTextArea textArea) {
        jTextAreaOUTPUT = textArea;
    }

    @Override
    public void write(int b) throws IOException {
        // redirects data to the text area
        jTextAreaOUTPUT.append(String.valueOf((char) b));
        // scrolls the text area to the end of data
        jTextAreaOUTPUT.setCaretPosition(jTextAreaOUTPUT.getDocument().getLength());
    }

    @Override
    public void write(byte[] b, int off, int len) throws IOException {
        // redirects data to the text area
        jTextAreaOUTPUT.append(new String(b, off, len));
        // scrolls the text area to the end of data
        jTextAreaOUTPUT.setCaretPosition(jTextAreaOUTPUT.getDocument().getLength());
    }

    @Override
    public void write(byte[] b) throws IOException {
        write(b,0,b.length);
    }

}
于 2015-10-05T12:06:52.380 に答える