条件での自動例外処理
Java 例外を生成するルール条件をルール・エンジンが処理し続けられるように、意思決定サービス・プロジェクト・レベルで例外処理を自動化できます。 条件での自動例外処理をアクティブ化すると、デフォルトでは 4 つの Java 例外のタイプがエンジンによって処理されますが、ルール・エンジンで処理する Java 例外のタイプのリストをカスタマイズできます。
自動例外処理が有効な場合、ルール・エンジンはルール条件の評価時に不明の値を無視します。 ルール・プロジェクトのプロパティー・ウィンドウの 「ルール・エンジン」 パネルの Rule Designer で、この機能を有効または無効にすることができます。 自動例外処理は、デフォルトでは無効です。
自動例外処理が有効な場合、以下のサブクラスがルール・エンジンによって処理されます。
- ArithmeticException
- NumberFormatException
- NullPointerException
- IndexOutOfBoundsException
条件での自動例外処理が有効な場合、評価時に例外となる条件内の式は不明値として扱われ、ルール・エンジンによって処理されます。 ルール・エンジンは、以下のような 3 値論理を使用してルール条件全体を評価します。
条件式が不明値になったとしても、ルール・エンジンがルール条件を真または偽と評価した場合、ルール・アクションのロジックが適用されます。 条件式が不明値になることが理由で、ルール・エンジンがルール条件を不明と評価した場合、ルールは実行されません。
例えば、次のルールの場合、顧客名の値が NULL で顧客の年が 20 のとき、最初の条件は解決できず、2 番目の条件は満たされます。
if
the name of the customer is 'Paul'
or the age of the customer is 20
then
reject the loan;
このルールでは、論理 or
演算子が使用されるため、ローンを拒否するアクションが実行されます。 顧客の年が 20 でない場合、
最初の条件は解決できず、2 番目の条件も満たされません。 この場合、ルールは実行されないため、ローンは拒否されません。
ルール変数
定義が不明の場合、条件部分は残りの条件にかかわりなく、自動的に不明と見なされます。
ルール変数 'statusIsNew'
が不明の場合、条件は不明と見なされ、ルール・アクションは実行されません。
definitions
set 'statusIsNew' to the status of 'the borrower' starts with “New";
if it is not true that 'statusIsNew' and the comment of 'the loan' contains "similar"
then
reject 'the loan' ;
add "duplicate detected" to the messages of 'the loan' ;
else
add "no duplicate found" to the messages of 'the loan' ;
条件部分の評価を可能にするには、ルール変数のすべてを不明のものとは別個に解決する必要があります。 ルール変数 statusIsDuplicate
が不明の場合、条件は不明と見なされ、or
条件が存在しその部分が true であっても、ルール・アクションは何も実行されません。 例えば、次のルールでは、'the loan'
のコメントに "similar"
が含まれていません。
set 'statusIsDuplicate' to the customer status of 'the borrower' starts with "Duplicate";
if
'statusIsDuplicate' or the comment of 'the loan' contains "similar"
then
reject 'the loan' ;
add "duplicate detected" to the messages of 'the loan' ;
else
add "no duplicate found" to the messages of 'the loan' ;
コレクションを使用する条件
ルール・エンジンがオブジェクトのコレクションをアセンブルする場合、where
節が不明または false のオブジェクトは無視されます。 このため、コレクション内のオブジェクト数へのテストは、where
節が true と評価されるオブジェクトのみカウントされる、という動作を反映します。
If the number of ...
If there is no ...
If there is at least one ...
If there are at least <number> ...
If there are more than <number> ...
If there are less than <number> ...
If there are <number> ...
次のサンプルは、コレクションを使用してオブジェクト数をテストする条件を示しています。 このサンプルでは、コレクション (the borrowers
) には、属性値が既知の借り手のみが含まれています。 そのため、カウント操作が実行されると (is less than
3
)、属性が不明な借り手はカウントに含まれません。 つまり、3 人の借り手が評価され、1 人の借り手の名前が不明で、他の 2 人の名前が 'borrower'
の名前である場合、条件は True となり、then
アクションが実行されます。
if
the number of borrowers where the name of each borrower is the name of 'the borrower',
is less than 3,
then
add "less than 3 similar barrowers found" to the messages of 'the loan' ;
else
add "duplicate detected" to the messages of 'the loan' ;
reject 'the loan' ;
以下の例で、where
節が 2 人の借り手に関しては真で、2 人の借り手に関しては不明の場合、ルール・エンジンはルール条件を偽と見なします。 else
アクションが実行されます。
If there are more than 3 borrowers in the past borrowers of 'the loan' where the name of each borrower is the name of 'the borrower',
then
reject 'the loan';
add "more than 3 similar borrowers found" to the messages of 'the loan' ;
else
add "3 or fewer similar borrowers found " to the messages of 'the loan' ;
例外タイプの追加
自動的に処理される例外タイプのリストをカスタマイズするには、対応するクラスの BOM を作成し、特定のプロパティー de.autoCatchExceptionInConditions
と de.doNotAutoCatchExceptionInConditions
を該当する各クラスに追加します。 Rule Designerで自動例外処理が有効になっている場合、動作は、以下のサブクラスに属する例外のルールの条件部分で発生する例外を自動的に処理します。
- ArithmeticException
- NumberFormatException
- NullPointerException
- IndexOutOfBoundsException
BOM エディターを使用して、対応するクラスに de.autoCatchExceptionInConditions
というプロパティーを設定することで、自動的に処理される例外タイプを追加して、デフォルトの動作をカスタマイズできます。
package java.lang;
public class ClassCastException
extends java.lang.RuntimeException
property "de.autoCatchExceptionInConditions" "true"
{
public ClassCastException();
public ClassCastException(string arg);
}
例外タイプの除外
de.autoCatchExceptionInConditions
という名前のプロパティーを使用して BOM 内のクラスの定義をオーバーライドすることによって、処理されるいずれかのクラスの例外タイプを自動例外処理から除外できます。 例えば、ArithmeticException
クラスから de.autoCatchExceptionInConditions
プロパティーを削除することで、演算例外を自動例外処理から除外できます。
public class ArithmeticException
extends java.lang.RuntimeException
{
public ArithmeticException();
public ArithmeticException(string arg);
}
de.doNotAutoCatchExceptionInConditions という名前のプロパティー (太字部分) を設定して、4 つのデフォルト・クラスのサブクラスを自動例外処理から除外できます。
public class StringIndexOutOfBoundsException
extends java.lang.IndexOutOfBoundsException
property "de.doNotAutoCatchExceptionInConditions" "true"
{
public StringIndexOutOfBoundsException();
public StringIndexOutOfBoundsException(string arg);
public StringIndexOutOfBoundsException(int arg);
}
ロギング
自動例外処理は標準の Java™ ロガーのロギング・サービスを使用します。したがって、ユーザーはメッセージ・タイプとレベルに基づいてメッセージをログに記録できるほか、実行時にそれらのメッセージをフォーマット設定したり保管したりする方法を制御できます。
Rule Designerで、自動例外処理で発生するアクティビティー・トレースをキャプチャーするには、最初に logging.properties ファイルを定義し、ルール実行構成またはデバッグ構成で使用される JVM でそのファイルを使用できるようにする必要があります。 次に、 「実行構成」 ウィンドウの意思決定操作の 「パラメーター」&「引数」 タブで、VM 引数 -Djava.util.logging.config.file=file_path/logging.properties または -Djava.util.logging.config.file="${workspace_loc}/your_project/logging.properties"のいずれかを入力します。
FINE
に設定されます。handlers = java.util.logging.ConsoleHandler
############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################
# Limit the messages that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
com.ibm.rules.engine.aeh.level = FINE
サーバー環境で自動例外処理のログを使用可能にするには、logging.properties ファイルを定義し、アプリケーション・サーバーを構成することで、JVM に対してそのファイルを使用可能にします。ロガー名 com.ibm.rules.engine.aeh
のログ・レベルは FINE
に設定する必要があります。
Java ロギング API の概要については、 Java ロギングの概要を参照してください。 java.util.logging Java ロガーについて詳しくは、 Oracle 資料の「 java.util.logging 」を参照してください。