过程 - 在计算的属性中使用 BusinessAlgorithm
实现 BusinessAlgorithm 类
BusinessAlgorithm 是用于定义以下两种方法的接口:executegetSampleValue
public Object execute(BusinessAlgorithmContext context, Object[] args);
public Object getSampleValue();
要在计算的属性中使用复杂计算,需要首先创建一个实现此接口的新类。execute 方法应返回要在 Windchill UI 中查看的最终结果,或在计算的属性公式内的进一步计算中使用。在“类型和属性管理”实用程序中创建和验证公式时,将使用 getSampleValue API。它应返回 execute 结果的真实示例值。借助此功能,则无需在 execute 中实际进行成本非常高的计算来验证 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",而且这也是用户界面中显示的内容。
在这种情况下,BusinessAlgorithm 上的 execute 函数的结果是字符串。因此,我们创建了一个字符串计算的属性。函数的结果可以是适用于计算的属性的任何数据类型。创建计算的属性时,请确保该数据类型与 BusinessAlgorithm 类的预期返回类型相匹配。
其他公式示例
在计算的属性上创建公式时,可以将 execute 函数与任何其他函数以及公式的可用运算对象组合在一起。但是,必须满足以下条件之一:
您尝试使用的函数或运算对象支持公式所返回的数据类型。
您的 BusinessAlgorithm 支持从其他函数或运算对象返回的数据类型。
可将属性值或函数结果作为自变量传递给 execute 函数,并且可以根据需要传递任意数量的自变量。例如:
execute(“com.mycompany.MyComplexBusinessAlgorithm”, <atttributeName1>, round(<attributeName2>), 100*<attributeName3>)
也可将 execute 函数的结果传递给公式中的其他函数。例如:
round(execute(“com.mycompany.MyComplexBusinessAlgorithm”, <atttributeName1>, 100*<attributeName3>))
这对您有帮助吗?