How to programmatically add/modify a rule? Hello,
I have the Rules module. I tried to programmatically create or modify a rule but it doesn't work. What I tried so far : I created the 
MY_MODULE.rules_defaults.inc file and wrote this code :

function MY_MODULE_default_rules_configuration() {
$configs = array();
$rule = { "rules_name" : {
  "LABEL" : "rules ",
  "PLUGIN" : "reaction rule",
  "OWNER" : "rules",
  "REQUIRES" : [ "rules", "mimemail", "uc_cart" ],
  "ON" : { "uc_checkout_complete" : [] },
  "DO" : [
    { "component_rules_commerce_order_status_pending" : { "order" : [ "site:current-cart-order" ] } },
    { "mimemail" : {
        "key" : "[site:name]",
        "to" : "mail@mail.com",
        "subject" : "[site:name]",
        "body" : "Random text",
        "language" : [ "" ]
      }
    }
  ]
}
}
 $configs['rules_name'] = rules_import($rule);
  return $configs;
}


Yet it doesn't work.
I also tried with MY_MODULE_default_rules_configuration_alter() but to no avail.
You have to create new Rule via the provided APIs and HOOKs from the Rules module.

From creating rules through the UI you have seen their configuration attributes, such as


Rules Event
Rules Condition
Rules Action etc.


Now you have all the useful HOOKs to create new ones. If you want to add a new Rules event to your System then go for hook_rules_event_info()


/**
* Implementation of hook_rules_event_info().
* @ingroup rules
*/
function your_module_rules_event_info() {
  return array(
    'your_module_custom_event' => array(
      'label' => t('A custom event happened in your application'),
      'module' => 'your_module',
      'arguments' => array(
        'param1' => array('type' => 'user', 'label' => t('The user object')),
      ),
    ),
  );
}



You can invoke this event also from custom code in your module as 


rules_invoke_event('your_module_custom_event', $param1);


If you want to add some custom conditions to a pre defined rule then use 
hook_rules_condition_info()


/**
 * Implements hook_rules_condition_info().
 * Checks if the given user is male or not.
 */
function your_module_rules_condition_info() {
  return array(
    'user_is_a_male' => array(
      'group' => t('test'),
      'label' => t('If user is male'),
      'parameter' => array(
        'user' => array(
          'type' => 'user',
          'label' => t('Validate registered users if male'),
        )
      ),
    ),
  );
}
function user_is_a_male($user) {
  // your custom condition to return boolean TRUE or FALSE
}


To alter pre-defined Rules you might use hook_rules_component_alter or hook_default_rules_configuration_alter but again depending on needs these will vary. Go through via the link above and you will find all.

FYI, you can export Rules via Features module with your custom HOOKs as well.
And these custom HOOK's should be placed on a file as "your_module.rules.inc" so Rules module will include these automatically.