Variables
A variable is a symbolic representation of a value. It can be used to build conditions and, in some cases, to pass data to actions. A variable can be global or local and it is associated with a container.
Global Variables
Global variables are used to build conditions and to provide data to actions. It is important to mention the concept of variables container, it is a logical group of variables and has a tree like structure. When generating a new variable, if it already exists for the given type in the current container or any of the parent containers, it will be used. If not, a new one is created in the current container. In practice, this means that no more than one variable can be created for the same type inside a specific container. The following shows you an example:
String orderEntryRaoVariable = context.generateVariable(OrderEntryRAO.class); RuleIrTypeCondition irCondition = new RuleIrTypeCondition(); irCondition.setVariable(orderEntryRaoVariable);
Local Variables
Local variables can be used the same way as global variables, but they are only valid in the scope of a specific condition. Only some conditions support local variables. Many variables can be created for the same type; this allows more flexible conditions. Actions cannot access local variables. The following shows you an example:
final RuleIrLocalVariablesContainer variablesContainer = context.createLocalContainer(); final String containsOrderEntryRaoVariable = context.generateLocalVariable(variablesContainer, OrderEntryRAO.class); final List<RuleIrCondition> irConditions = newArrayList(); irConditions.add(newAttributeConditionFor(containsOrderEntryRaoVariable) .withAttribute(ORDER_ENTRY_RAO_PRODUCT_CODE_ATTRIBUTE) .withOperator(RuleIrAttributeOperator.EQUAL) .withValue(product) .build()); irConditions.add(newAttributeConditionFor( containsOrderEntryRaoVariable) .withAttribute(QUANTITY_PARAM) .withOperator(RuleIrAttributeOperator.valueOf(operator.name())) .withValue(quantity) .build()); irConditions.add(newAttributeRelationConditionFor(cartRaoVariable) .withAttribute(CART_RAO_ENTRIES_ATTRIBUTE) .withOperator(RuleIrAttributeOperator.CONTAINS) .withTargetVariable(containsOrderEntryRaoVariable) .build()); irConditions.addAll(getConsumptionSupport().newProductConsumedCondition(context, containsOrderEntryRaoVariable)); final RuleIrExistsCondition irExistsProductCondition = new RuleIrExistsCondition(); irExistsProductCondition.setVariablesContainer(variablesContainer); irExistsProductCondition.setChildren(irConditions); irQualifyingProductsCondition.getChildren().add(irExistsProductCondition);