<commit_before>/**
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.*
 */

/* global API */

/**
 * Model of a Rule loaded from the Rules Engine
 * @constructor
 * @param {Gateway} gateway - The remote gateway to which to talk
 * @param {RuleDescription?} desc - Description of the rule to load
 * @param {Function?} onUpdate - Listener for when update is called
 */
function Rule(gateway, desc, onUpdate) {
  this.gateway = gateway;
  this.onUpdate = onUpdate;

  if (desc) {
    this.id = desc.id;
    this.enabled = desc.enabled;
    if (desc.name) {
      this.name = desc.name;
    } else {
      this.name = 'Rule Name';
    }
    this.trigger = desc.trigger;
    this.effect = desc.effect;
  } else {
    this.enabled = true;
  }
}

/**
 * Validate and save the rule
 * @return {Promise}
 */
Rule.prototype.update = function() {
  if (this.onUpdate) {
    this.onUpdate();
  }
  let desc = this.toDescription();
  if (!desc) {
    return Promise.reject('invalid description');
  }

  let fetchOptions = {
    headers: API.headers(),
    method: 'PUT',
    body: JSON.stringify(desc)
  };
  fetchOptions.headers['Content-Type'] = 'application/json';

  let request = null;
  if (typeof(this.id) !== 'undefined') {
    request = fetch('/rules/' + this.id, fetchOptions);
  } else {
    fetchOptions.method = 'POST';
    request = fetch('/rules/', fetchOptions).then(res => {
      return res.json();
    }).then(rule => {
      this.id = rule.id;
    });
  }
  return request;
};

/**
 * Delete the rule
 * @return {Promise}
 */
Rule.prototype.delete = function() {
  let fetchOptions = {
    headers: API.headers(),
    method: 'DELETE'
  };

  if (typeof(this.id) === 'undefined') {
    return;
  }

  return fetch('/rules/' + this.id, fetchOptions);
};

/**
 * Convert this rule into a serialized description
 * @return {RuleDescription?} description or null if not a valid rule
 */
Rule.prototype.toDescription = function() {
  if (!this.trigger || !this.effect) {
    return null;
  }
  return {
    enabled: this.enabled,
    name: this.name,
    trigger: this.trigger,
    effect: this.effect
  };
};

// Helper function for selecting the thing corresponding to a property
const RuleUtils = {
  byProperty: function byProperty(property) {
    return function(option) {
      let optProp = option.properties[property.name];
      return optProp && (optProp.href === property.href);
    };
  }
};

/**
 * Convert the rule's trigger's description to a human-readable string
 * @return {String}
 */
Rule.prototype.toTriggerHumanDescription = function() {
  let triggerThing = this.gateway.things.filter(
    RuleUtils.byProperty(this.trigger.property)
  )[0];

  let triggerStr = `${triggerThing.name} `;
  if (this.trigger.type === 'BooleanTrigger') {
    triggerStr += 'is ';
    if (!this.trigger.onValue) {
      triggerStr += 'not ';
    }
    triggerStr += this.trigger.property.name;
  } else {
    triggerStr += `${this.trigger.property.name} is `;
    if (this.trigger.levelType === 'LESS') {
      triggerStr += 'less than ';
    } else {
      triggerStr += 'greater than ';
    }
    triggerStr += this.trigger.level;
  }

  return triggerStr;
};

/**
 * Convert the rule's effect's description to a human-readable string
 * @return {String}
 */
Rule.prototype.toEffectHumanDescription = function() {
  let effectThing = this.gateway.things.filter(
    RuleUtils.byProperty(this.effect.property)
  )[0];

  let effectStr = '';
  if (this.effect.property.name === 'on' &&
    effectThing.type === 'onOffSwitch') {
    effectStr = `turn ${effectThing.name} `;
    if (this.effect.value) {
      effectStr += 'on';
    } else {
      effectStr += 'off';
    }
    if (this.effect.type === 'SET') {
      effectStr += ' permanently';
    }
    return effectStr;
  }
  if (this.effect.type === 'SET') {<commit_msg>Remove onOffSwitch check from rule summarizing<commit_after>/**
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.*
 */

/* global API */

/**
 * Model of a Rule loaded from the Rules Engine
 * @constructor
 * @param {Gateway} gateway - The remote gateway to which to talk
 * @param {RuleDescription?} desc - Description of the rule to load
 * @param {Function?} onUpdate - Listener for when update is called
 */
function Rule(gateway, desc, onUpdate) {
  this.gateway = gateway;
  this.onUpdate = onUpdate;

  if (desc) {
    this.id = desc.id;
    this.enabled = desc.enabled;
    if (desc.name) {
      this.name = desc.name;
    } else {
      this.name = 'Rule Name';
    }
    this.trigger = desc.trigger;
    this.effect = desc.effect;
  } else {
    this.enabled = true;
  }
}

/**
 * Validate and save the rule
 * @return {Promise}
 */
Rule.prototype.update = function() {
  if (this.onUpdate) {
    this.onUpdate();
  }
  let desc = this.toDescription();
  if (!desc) {
    return Promise.reject('invalid description');
  }

  let fetchOptions = {
    headers: API.headers(),
    method: 'PUT',
    body: JSON.stringify(desc)
  };
  fetchOptions.headers['Content-Type'] = 'application/json';

  let request = null;
  if (typeof(this.id) !== 'undefined') {
    request = fetch('/rules/' + this.id, fetchOptions);
  } else {
    fetchOptions.method = 'POST';
    request = fetch('/rules/', fetchOptions).then(res => {
      return res.json();
    }).then(rule => {
      this.id = rule.id;
    });
  }
  return request;
};

/**
 * Delete the rule
 * @return {Promise}
 */
Rule.prototype.delete = function() {
  let fetchOptions = {
    headers: API.headers(),
    method: 'DELETE'
  };

  if (typeof(this.id) === 'undefined') {
    return;
  }

  return fetch('/rules/' + this.id, fetchOptions);
};

/**
 * Convert this rule into a serialized description
 * @return {RuleDescription?} description or null if not a valid rule
 */
Rule.prototype.toDescription = function() {
  if (!this.trigger || !this.effect) {
    return null;
  }
  return {
    enabled: this.enabled,
    name: this.name,
    trigger: this.trigger,
    effect: this.effect
  };
};

// Helper function for selecting the thing corresponding to a property
const RuleUtils = {
  byProperty: function byProperty(property) {
    return function(option) {
      let optProp = option.properties[property.name];
      return optProp && (optProp.href === property.href);
    };
  }
};

/**
 * Convert the rule's trigger's description to a human-readable string
 * @return {String}
 */
Rule.prototype.toTriggerHumanDescription = function() {
  let triggerThing = this.gateway.things.filter(
    RuleUtils.byProperty(this.trigger.property)
  )[0];

  let triggerStr = `${triggerThing.name} `;
  if (this.trigger.type === 'BooleanTrigger') {
    triggerStr += 'is ';
    if (!this.trigger.onValue) {
      triggerStr += 'not ';
    }
    triggerStr += this.trigger.property.name;
  } else {
    triggerStr += `${this.trigger.property.name} is `;
    if (this.trigger.levelType === 'LESS') {
      triggerStr += 'less than ';
    } else {
      triggerStr += 'greater than ';
    }
    triggerStr += this.trigger.level;
  }

  return triggerStr;
};

/**
 * Convert the rule's effect's description to a human-readable string
 * @return {String}
 */
Rule.prototype.toEffectHumanDescription = function() {
  let effectThing = this.gateway.things.filter(
    RuleUtils.byProperty(this.effect.property)
  )[0];

  let effectStr = '';
  if (this.effect.property.name === 'on') {
    effectStr = `turn ${effectThing.name} `;
    if (this.effect.value) {
      effectStr += 'on';
    } else {
      effectStr += 'off';
    }
    if (this.effect.type === 'SET') {
      effectStr += ' permanently';
    }
    return effectStr;
  }
  if (this.effect.type === 'SET') {
{'fraction_non_alphanumeric': 0.15013202565069786, 'fraction_numerical': 0.0015088645794039985, 'mean_word_length': 2.9279012345679014, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5360713', 'n_tokens_mistral': 2537, 'n_tokens_neox': 2488, 'n_words': 663}