How to programmatically add/modify a rule?
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.