Additional Windchill Capabilities > Manufacturing Process Management > Customizing the Process Plan Explorer > Creating Custom Formulas
  
Creating Custom Formulas
A formula set is used to relate a set of attributes with the corresponding formulas or equations that are to be used to calculate their value. The components that make up a formula set are as follows:
FormulaSet identifies the set
FormulaMetaData defines the name and description for a Java formula
FormulaValuatedLink relates an attribute name to a formula definition
The following graphic is an example of data that needs to be created in order to add custom formulas:
The Formula Sets tab is used to select pre-defined formula sets that can be used to calculate the time and cost associated with an operation. For example, a standard time and cost formula set, and a periodic time and cost formula set. These formula sets take into account the number of parts to be produced in the operation and use the specifications defined for work center (For example, setup time, queue time). Formulas and formula sets are used when the Calculate Time and Cost action has been selected, and when you are loading process plans and operations into the Manufacturing Gantt Explorer.
While multiple formula sets can be associated with a work center, one formula set can be designated as the default formula set for the work center. It is this formula set that is assigned to an operation when the work center is allocated to that operation.
* 
Formulas must be assigned to a Formula set. If you do not have an existing formula set, you must define one before you can define a custom formula. The PSE can be customized to allow you to use the PSE to add additional values to your customized formula sets. For more information, see the PSE documentation.
To create a custom formula, use the following procedure:
1. Using the LoadFromFile utility, load an XML-based load file where <csvname> is unique to the system. This load file must contain all of the following elements:
csvFormulaSet - This creates a FormulaSet object.
csvFormulaMetaData - This creates a FormulaMetaData object.
csvAddFormulaMetaData - This links the FormulaMetaData to the FormulaSet.
<csvFormulaSet handler="com.ptc.windchill.mpml.formula.LoadFormula.
createFormulaSet">
<csvuser></csvuser>
<csvname>Standard Time and Cost</csvname>
<csvdescription>This formula Set is used to calculate the time and
cost of an Operation</csvdescription>
<csvfolder>/Default/Design</csvfolder>
<csvparentcontextPath></csvparentcontextPath>
<csvorganizationName></csvorganizationName>
</csvFormulaSet>
2. Create a formula metadata by linking to a java class name that implements Formula interface and returns a calculated value. For example, FloatingPointWithUnits.
Using the LoadFromFile utility, load an XML-based load file where <csvname> is unique to the system and where <csvformulaClassName> is the Java Class implementing formula interface.
<csvFormulaMetaData handler="com.ptc.windchill.mpml.formula.
LoadFormula.createFormulaMetaData">
<csvuser></csvuser>
<csvname>StandardAttributeValueFormulaTime</csvname>
<csvdescription>Standard Attribute Value Formula</csvdescription>
<csvtype>time</csvtype>

<csvformulaClassName>com.ptc.windchill.mpml.formula.Standard AttributeValueFormula</csvformulaClassName>
<csvfolder>/Default/Design</csvfolder>
<csvparentcontextPath></csvparentcontextPath>
<csvorganizationName></csvorganizationName>
<csvorganizationID></csvorganizationID>
</csvFormulaMetaData>
3. Link the formula metadata to the formula set by using an attribute name.
For example, when the service to calculate the formula is called, all the formula metadata associated to the formula set will be executed and a map of all calculated values will be returned using the attribute name as the key. .
Using the LoadFromFile utility, load an XML-based load file where <csvattributeName> is the time and cost reusable attribute on the MPMOperation object
<csvAddFormulaMetaData handler="com.ptc.windchill.mpml.formula.
LoadFormula.addFormulaMetaDataToFormulaSet" >
<csvformulaSetName>Standard Time and Cost</csvformulaSetName>
<csvformulaMetaDataName>StandardAttributeValueFormula Time</csvformulaMetaDataName>
<csvattributeName>IBA|MPM_Attr_MPMOperationSetupTime </csvattributeName>
</csvAddFormulaMetaData>
When creating a java class that implements Formula interface, these inputs are accessible out-of-the-box in the inputs map.
FormulaHelper.FORMULAVALUATED: the object on which the formula set is linked and that casts to ForumlaValuated.
FormulatHelper.FORMULAVALUATED_TI: the object type instance on which the formula set is linked and that casts to TypeInstance. It can be null.
FormulaHelper.FORMULAVALUATED_ATTRIBUTE_NAME: the attribute name used to link the formula meta data to the formula set and that casts to String. This will be used as the key in the results map.
FormulaHelper.LOT: the lot value specified by the user in the context of the Calculate Time and Cost action. It casts to Double.
The following is an example of a formula java class:
import com.ptc.core.meta.common.AttributeIdentifier;
import com.ptc.core.meta.common.AttributeTypeIdentifier;
import com.ptc.core.meta.common.TypeinstanceIdentifier;
import com.ptc.core.meta.context.common.AttributecontextSpec
import com.ptc.core.meta.server.TypeIdentifierUtility;
import com.ptc.core.meta.type.common.TypeInstance;
import com.ptc.core.meta.type.common.TypeInstanceFactory;
import com.ptc.windchill.mpml.MPMLinkHelper;
import java.text.NumberFormat;
import java.util.HashMap;
import wt.units.FloatingPointWithUnits;
import wt.util.WTContext;
import util.WTException;

public class StandardAttributeValueFormula implements Formula{
private static final String RESOURCE = "com.ptc.windchill.mpml.formula.formulaResource";
private static NumberFormat numberFormat =
NumberFormat.getInstance(WTContext.getContext().getLocale());
/** Creates a new instance of StandardAttributeValueFormula */
* This Formula simply return the value of the attribute.
public StandardAttributeValueFormula() {
}
public FloatingPointWithUnits calculate(HashMap inputs)throws
InvalidFormulaInputException, WTException{
//Get the object on which the formula is call
Object object = inputs.get(FormulaHelper.FORMULAVALUATED);
//Get the TI on which the formula is call
TypeInstance ti =(TypeInstance)inputs.get(FormulaHelper.FORMULAVALUATED_TI);
//Get the attribute that is calculated by the formula
String attribute_name = (String)inputs.get(FormulaHelper.FORMULAVALUATED_ATTRIBUTE_NAME);
if(object == null)
throw new InvalidFormulaInputException( RESOURCE, formulaResource.REQUIRED_FORMULA_INPUT_MISSING, new Object[]{FormulaHelper.FORMULAVALUATED} );;
TypeInstanceIdentifier tii = null;

//Get the TI of the object if null
if(ti==null){
tii = TypeIdentifierUtility.getTypeInstanceIdentifier(object);
ti = TypeInstanceFactory.newTypeInstance(tii);
}else
tii = (TypeInstanceIdentifier)ti.getIdentifier();
//Get ATI for the attribute to calculate
AttributeTypeIdentifier ati = (AttributeTypeIdentifier)MPMLinkHelper.getIdentifierFactory().get(attribute_name,tii.getDefinitionIdentifier());
AttributeIdentifier[] ais = ti.getAttributeIdentifiers(ati);
//If the attribute value is not in the TI, update it to get the value
if(ais.length<1){
AttributecontextSpec fl = new AttributecontextSpec();
fl.putEntry(ati);
ti = MPMLinkHelper.updateTypeInstance(new TypeInstance[] {ti}, fl, null)[0];
ais = ti.getAttributeIdentifiers(ati);
}
//Get the attribute value
Object value = null;
if(ais.length>0)
value = ti.get(ais[0]);
//Return the value of the attribute if it’s a FloatingPointWithUnits
if(value instanceof FloatingPointWithUnits)
return (FloatingPointWithUnits)value;
// value is null
return FloatingPointWithUnits.valueOf(numberFormat.format(0));
}
}
* 
It is also possible to add more input to the formula by creating a new action and then using formula services to call the execution of the formula set. For more information on creating new actions, see the PSE documentation.