Advanced Customization > Business Logic Customization > Customizations in Type and Attribute Management > Using a BusinessAlgorithm in a Calculated Attribute > Procedure – Using a BusinessAlgorithm in a Calculated Attribute
  
Procedure – Using a BusinessAlgorithm in a Calculated Attribute
Implement the BusinessAlgorithm Class
BusinessAlgorithm is an interface that defines two methods: execute and getSampleValue.
public Object execute(BusinessAlgorithmContext context, Object[] args);
public Object getSampleValue();
To use your complex computations in a calculated attribute you need to start by creating a new class that implements this interface. The execute method should return the final result that you want to see in a Windchill UI or use in further calculation within the calculated attribute formula. The getSampleValue API is used when formulas are created and validated in the Type and Attribute Management utility. It should return a realistic example value for the result of execute. This is so that it is not necessary to do the real, presumably expensive, computations in execute in order to validate the return type of your BusinessAlgorithm.
Here’s a very simple example of a 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”;
}

}
Create the calculated attribute
To use the output of your calculations in a calculated attribute you use the new execute function.
Here are the steps to invoke the HelloWorldBusinessAlgorithm
1. In the Windchill Type and Attribute Management utility create a string calculated attribute.
2. On the set properties step define the formula that calls the execute function like so:
execute(“com.mycompany.HelloWorldBusinessAlgorithm”, “My Function Works”)
3. This formula evaluates to the string “Hello World: My Function Works” and that is what is displayed in the UI.
In this case the result of the execute function on the BusinessAlgorithm is a string. So we created a string calculated attribute. The result of the function could be any of the datatypes available for calculated attributes. When creating your calculated attribute, be sure the datatype matches the expected return type of your BusinessAlgorithm class.
Additional Formula Examples
When creating your formula on your calculated attribute you can combine the execute function with any of the other functions and operands available for formulas. However, one of the following conditions must be met:
The data type returned from your formula is supported by the function or operand you are trying to use,
The datatype returned from the other function or operand is supported by your BusinessAlgorithm.
You can pass attribute values or function results to the execute function as arguments, and you can pass as many arguments as you need. For Example:
execute(“com.mycompany.MyComplexBusinessAlgorithm”, <atttributeName1>, round(<attributeName2>), 100*<attributeName3>)
You can also pass the result of the execute function to other functions in the formula. For Example:
round(execute(“com.mycompany.MyComplexBusinessAlgorithm”, <atttributeName1>, 100*<attributeName3>))