<commit_before>/**
 * @license
 * Copyright 2019 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.nanos.ruler;

import foam.core.*;
import foam.dao.ArraySink;
import foam.dao.DAO;
import foam.dao.ReadOnlyDAO;
import foam.dao.Sink;
import foam.mlang.sink.GroupBy;
import foam.nanos.pool.FixedThreadPool;

import java.lang.Exception;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

public class RuleEngine extends ContextAwareSupport {
  private DAO delegate_ = null;
  private DAO ruleHistoryDAO_ = null;
  private AtomicBoolean stops_ = new AtomicBoolean(false);
  private Map<Long, Object> results_ = new HashMap<>();
  private Map<Long, RuleHistory> savedRuleHistory_ = new HashMap<>();
  private Rule currentRule_ = null;
  private X systemX_;

  public RuleEngine(X x, X systemX, DAO delegate) {
    setX(x);
    setDelegate(delegate);
    ruleHistoryDAO_ = (DAO) x.get("ruleHistoryDAO");
    systemX_ = systemX;
  }

  public DAO getDelegate() {
    return delegate_;
  }

  public void setDelegate(DAO delegate) {
    this.delegate_ = delegate;
  }

  /**
   * Executes rules by applying their actions synchronously then submitting
   * a chain of their async actions to thread pool for execution.
   *
   * Each rule would check object applicability before applying action.
   *
   * @param rules - Rules to be considered applying
   * @param obj - FObject supplied to rules for execution
   * @param oldObj - Old FObject supplied to rules for execution
   */
  public void execute(List<Rule> rules, FObject obj, FObject oldObj) {
    CompoundContextAgent compoundAgent = new CompoundContextAgent();
    ContextualizingRuleAgency agency = new ContextualizingRuleAgency(new CompoundAgency(compoundAgent), x_, systemX_);
    for (Rule rule : rules) {
      if ( stops_.get() ) break;
      applyRule(rule, obj, oldObj, agency);
      agency.submit(x_, x -> saveHistory(rule, obj));
    }
    compoundAgent.execute(x_);

    asyncApplyRules(rules, obj, oldObj);
  }

  /**
   * Probes rules execution by applying actions and skipping
   * execution of agents that contain code that effects the system
   *
   * @param rules - Rules to be considered applying
   * @param obj - FObject supplied to rules for execution
   * @param rulerProbe -
   * @param oldObj - Old FObject supplied to rules for execution
   */
  public void probe(List<Rule> rules, RulerProbe rulerProbe, FObject obj, FObject oldObj) {
    for (Rule rule : rules) {
      ProbeAgency probeAgenjt = new ProbeAgency();
      if ( stops_.get() ) {
        rulerProbe.addTestedRule(rule.getId(), probeAgent.toString() + "Not executed because was overridden and forced to stop.", false);
        continue;
      }
      try {
        applyRule(rule, obj, oldObj, new ProbeAgency());
        rulerProbe.addTestedRule(rule.getId(), probeAgent.toString(), true);<commit_msg>fix typo<commit_after>/**
 * @license
 * Copyright 2019 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.nanos.ruler;

import foam.core.*;
import foam.dao.ArraySink;
import foam.dao.DAO;
import foam.dao.ReadOnlyDAO;
import foam.dao.Sink;
import foam.mlang.sink.GroupBy;
import foam.nanos.pool.FixedThreadPool;

import java.lang.Exception;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

public class RuleEngine extends ContextAwareSupport {
  private DAO delegate_ = null;
  private DAO ruleHistoryDAO_ = null;
  private AtomicBoolean stops_ = new AtomicBoolean(false);
  private Map<Long, Object> results_ = new HashMap<>();
  private Map<Long, RuleHistory> savedRuleHistory_ = new HashMap<>();
  private Rule currentRule_ = null;
  private X systemX_;

  public RuleEngine(X x, X systemX, DAO delegate) {
    setX(x);
    setDelegate(delegate);
    ruleHistoryDAO_ = (DAO) x.get("ruleHistoryDAO");
    systemX_ = systemX;
  }

  public DAO getDelegate() {
    return delegate_;
  }

  public void setDelegate(DAO delegate) {
    this.delegate_ = delegate;
  }

  /**
   * Executes rules by applying their actions synchronously then submitting
   * a chain of their async actions to thread pool for execution.
   *
   * Each rule would check object applicability before applying action.
   *
   * @param rules - Rules to be considered applying
   * @param obj - FObject supplied to rules for execution
   * @param oldObj - Old FObject supplied to rules for execution
   */
  public void execute(List<Rule> rules, FObject obj, FObject oldObj) {
    CompoundContextAgent compoundAgent = new CompoundContextAgent();
    ContextualizingRuleAgency agency = new ContextualizingRuleAgency(new CompoundAgency(compoundAgent), x_, systemX_);
    for (Rule rule : rules) {
      if ( stops_.get() ) break;
      applyRule(rule, obj, oldObj, agency);
      agency.submit(x_, x -> saveHistory(rule, obj));
    }
    compoundAgent.execute(x_);

    asyncApplyRules(rules, obj, oldObj);
  }

  /**
   * Probes rules execution by applying actions and skipping
   * execution of agents that contain code that effects the system
   *
   * @param rules - Rules to be considered applying
   * @param obj - FObject supplied to rules for execution
   * @param rulerProbe -
   * @param oldObj - Old FObject supplied to rules for execution
   */
  public void probe(List<Rule> rules, RulerProbe rulerProbe, FObject obj, FObject oldObj) {
    for (Rule rule : rules) {
      ProbeAgency probeAgent = new ProbeAgency();
      if ( stops_.get() ) {
        rulerProbe.addTestedRule(rule.getId(), probeAgent.toString() + "Not executed because was overridden and forced to stop.", false);
        continue;
      }
      try {
        applyRule(rule, obj, oldObj, new ProbeAgency());
        rulerProbe.addTestedRule(rule.getId(), probeAgent.toString(), true);
{'fraction_non_alphanumeric': 0.09814502529510961, 'fraction_numerical': 0.002023608768971332, 'mean_word_length': 3.6481191222570533, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2960674', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1740, 'n_words': 552}