高度なカスタマイズ > ビジネスロジックのカスタマイズ > 「タイプおよび属性の管理」でのカスタマイズ > 計算属性での BusinessAlgorithm の使用 > 手順 - 計算属性で BusinessAlgorithm を使用する
  
手順 - 計算属性で BusinessAlgorithm を使用する
BusinessAlgorithm クラスの実装
BusinessAlgorithmexecutegetSampleValue の 2 つのメソッドを定義するインタフェースです。
public Object execute(BusinessAlgorithmContext context, Object[] args);
public Object getSampleValue();
計算属性で複雑な計算を使用するには、このインタフェースを実装する新しいクラスを最初に作成する必要があります。execute メソッドによって返される最終結果が Windchill UI に表示され、計算属性の式でさらなる計算に使用されます。「タイプおよび属性の管理」ユーティリティで式を作成して検証する際には getSampleValue API が使用されます。これは実行結果に応じた現実的なサンプルの値を返します。これにより、BusinessAlgorithm のリターンタイプを検証するために、実際の、おそらくは費用のかさむ計算を実行する必要がなくなります。
BusinessAlgorithm の非常に簡単な例を次に示します。
/**
* Hello World example BusinessAlgorithm
*/
final public class HelloWorldBusinessAlgorithm implements BusinessAlgorithm {
@Override
public Object execute(BusinessAlgorithmContext context, Object[] args) {
// Your complex calculations should go here
return “Hello World: “ + args[0];
}
@Override
public Object getSampleValue() {
// A realistic example value of the same type as the result of execute
// should be returned here. For this case a string.
return “A Sample Value”;
}
}
計算属性の作成
計算属性で計算の出力を使用するため、新しい execute 関数を使用します。
HelloWorldBusinessAlgorithm を呼び出すには次のステップを実行します。
1. Windchill の「タイプおよび属性の管理」ユーティリティで、文字列タイプの計算属性を作成します。
2. 「プロパティを設定」ステップで、execute 関数を呼び出す次のような式を定義します。
execute(“com.mycompany.HelloWorldBusinessAlgorithm”, “My Function Works”)
3. この式の結果は "Hello World: My Function Works" という文字列になり、この文字列が UI に表示されます。
この場合、BusinessAlgorithm の execute 関数の結果は文字列になります。文字列タイプの計算属性を作成したのはこのためです。この関数の結果は、計算属性に使用可能な任意のデータタイプをとることができます。計算属性を作成する際には、BusinessAlgorithm クラスの想定されるリターンタイプとデータタイプが一致するようにします。
その他の式の例
計算属性の式を作成する際には、execute 関数と、式に使用可能なその他の関数やオペランドを組み合わせることができます。ただし、次のいずれかの条件が満たされている必要があります。
式から返されるデータタイプが、使用する関数やオペランドでサポートされていること。
ほかの関数またはオペランドから返されるデータタイプが BusinessAlgorithm でサポートされていること。
属性値または関数の結果を引数として execute 関数に渡すことができます。引数は必要なだけいくつでも渡すことができます。たとえば、次のようになります。
execute(“com.mycompany.MyComplexBusinessAlgorithm”, <atttributeName1>, round(<attributeName2>), 100*<attributeName3>)
execute 関数の結果を式内のほかの関数に渡すこともできます。たとえば、次のようになります。
round(execute(“com.mycompany.MyComplexBusinessAlgorithm”, <atttributeName1>, 100*<attributeName3>))