Execute reaction rule programmatically I want to run Commerce rule "Create a new account for an anonymous order" from code. I can create user with user_save and etc, but I like this rule and want to reuse it.
After a hour I finished with this code:  

// Load reaction rule by name.
$rule = rules_config_load('commerce_checkout_new_account');
// We need $state for 'evaluate' or 'fire'.
$state = new RulesState();
// Add 'commerce_order' parameter to state.
$state-&gt;addVariable('commerce_order', $line_item_wrapper-&gt;order-&gt;value(), array('type' =&gt; 'commerce_order', 'label' =&gt; 'Commerce order'));
// Execute rule.
$rule-&gt;evaluate($state);
// How can I get created user account from $rule?
// Now I have to do user_load_by_mail().


It works but I don't think it is the best way.
I especially don't like  to create RulesState manually. I know there are setUpState() and lots of other methods. I want to find the best solution without creating new Rules component.
As a related solution, it's much easier to execute a Rules Component programmatically. They're designed for that.

For example, consider a Rules Component (type "Rule") that accepts a $commerce_order parameter.

In this case, it happens to process a queued order item from a cron queue, pushing the order to a remote system via an API.

function MODULE_queue_iwi_push_instant_sale($item){
  try{
    if($order = commerce_order_load($item['order_id'])){
      // Invoke the rules component programmatically
      // passing the $order parameter
      rules_invoke_component('rules_iwi_push_instant_sale', $order);
    }
  }catch(Exception $e){
    watchdog_exception('my_module', $e);
  }
}


I like to think of Rules Components as similar to functions that can be called, passing parameters and potentially returning a value. They can do other magical things as well. See rules_invoke_component

Reaction rules on the other hand are event driven, triggered by some event such as node_save and passed the relevant parameters. See rules_invoke_event

More info on Rules Components here: https://drupal.org/node/1582182