Advanced Customization > Business Logic Customization > Customizations in Type and Attribute Management > Using a BusinessAlgorithm in a Calculated Attribute > Sample Code
  
Sample Code
Packaged Samples
AbstractCarambolaAttributeSumBusinessAlgorithm
This is an example business algorithm that calculates and return the numerical "sum" of the specified attribute on all of the parts in a part structure. This algorithm is intended to be used in a "Formula" property on a numeric attribute. The internal name of the attribute to sum is passed in as the first argument to the algorithm.
Because the technique to add a list of numbers varies based on the data type, subclasses of this abstract class will actually handle the summing of values of different data types. We provide 3 subclasses that handle these data-type specific details:
IntCarambolaAttributeSumBusinessAlgorithm
FloatCarambolaAttributeSumBusinessAlgorithm
FloatWithUnitCarambolaAttributeSumBusinessAlgorithm
These subclasses must override 2 data-type specific methods: one is to provide a typical sample value of that data type that is used for formula validation (getSampleValue()), and one is to add up a list of values of this data type (calculateSumValue()).
So to use this algorithm on a floating point attribute, enter something like the following expression in the formula property of a calculated attribute in the Type and Attribute Management utility:
execute("com.ptc.carambola.customization.examples.businessfield.FloatCarambolaAttributeSumBusinessAlgorithm", "<name of a floating point attribute>")
This example code can be viewed in this location: <Windchill>\Windchill\srclib\wnc\
File Used in This Example
<Windchill>\Windchill\srclib\wnc\Carambola-java.jar
Explanation of example code
The execute() method first gets the name of the attribute to sum, and the current business object (a WTPart). Next it calls a getAllPartStructure(final BusinessObject curBusinessObject) helper method to walk down the part structure tree of the current business object, returning a WTCollection of the parts in that structure. (This is done using the wt.navigation.PartRequest and wt.navigation.DependencyHelper classes.)
After getting the collection of parts in the structure, execute() next calls a getBusinessField( final String fieldName, final TypeIdentifier contextType ) helper to return the BusinessField object that should be summed over the structure.
Then execute() calls the getPartStructureValues(final WTCollection partCollection, final BusinessField fieldToSum ) helper to get a list of values of the desired business field on all of the parts in the structure. This helper first uses the com.ptc.core.businessfield.server.businessObject.BusinessObjectHelper class to create and load all of the business objects at once (if they aren’t already loaded). Then it loops over the business objects and grabs the value(s) in the fieldToSum business field and adds them to a list. Finally the list of values is returned.
Next, execute() calls the subclass-overridden calculateSumValue(List<Number> allValuesForAttribute) method to actually sum all of the values into a total. (If you take a look at the different subclasses you see that summing long values is simple, but adding floating point values needs to take into account the units & precision.)
Finally, execute() returns the total, which is then returned as the value of the “execute()” formula function. (If the Formula only contains the execute() function, then this is also the value of the entire formula and the attribute.)