<commit_before>import { HashTable, Helpers } from "./helpers";
import { Question } from "./question";
import { Serializer } from "./jsonobject";
import { QuestionFactory } from "./questionfactory";
import { LocalizableString } from "./localizablestring";
import { ExpressionRunner } from "./conditions";

/**
 * A Model for expression question. It is a read-only question. It calculates value based on epxression property.
 */
export class QuestionExpressionModel extends Question {
  private expressionIsRunning: boolean;
  private expressionRunner: ExpressionRunner;
  constructor(public name: string) {
    super(name);
    this.createLocalizableString("format", this);
    var self = this;
    this.registerFunctionOnPropertyValueChanged("expression", function() {
      if (self.expressionRunner) {
        self.expressionRunner = new ExpressionRunner(self.expression);
      }
    });
  }
  public getType(): string {
    return "expression";
  }
  /**
   * Use this property to display the value in your own format. Make sure you have "{0}" substring in your string, to display the actual value.
   */
  public get format(): string {
    return this.getLocalizableStringText("format", "");
  }
  public set format(val: string) {
    this.setLocalizableStringText("format", val);
  }
  get locFormat(): LocalizableString {
    return this.getLocalizableString("format");
  }
  /**
   * The Expression that used to calculate the question value. You may use standard operators like +, -, * and /, squares (). Here is the example of accessing the question value {questionname}.
   * <br/>Example: "({quantity} * {price}) * (100 - {discount}) / 100"
   */
  public get expression(): string {
    return this.getPropertyValue("expression", "");
  }
  public set expression(val: string) {
    this.setPropertyValue("expression", val);
  }
  public locCalculation() {
    this.expressionIsRunning = true;
  }
  public unlocCalculation() {
    this.expressionIsRunning = false;
  }
  public runCondition(values: HashTable<any>, properties: HashTable<any>) {
    super.runCondition(values, properties);
    if (!this.expression || this.expressionIsRunning) return;
    this.locCalculation();
    if (!this.expressionRunner)
      this.expressionRunner = new ExpressionRunner(this.expression);
    var newValue = this.expressionRunner.run(values, properties);
    if (!Helpers.isTwoValueEquals(newValue, this.value)) {
      this.value = newValue;
    }
    this.unlocCalculation();
  }<commit_msg>Support async expression in Question Expression,  #1806<commit_after>import { HashTable, Helpers } from "./helpers";
import { Question } from "./question";
import { Serializer } from "./jsonobject";
import { QuestionFactory } from "./questionfactory";
import { LocalizableString } from "./localizablestring";
import { ExpressionRunner } from "./conditions";

/**
 * A Model for expression question. It is a read-only question. It calculates value based on epxression property.
 */
export class QuestionExpressionModel extends Question {
  private expressionIsRunning: boolean;
  private expressionRunner: ExpressionRunner;
  constructor(public name: string) {
    super(name);
    this.createLocalizableString("format", this);
    var self = this;
    this.registerFunctionOnPropertyValueChanged("expression", function() {
      if (self.expressionRunner) {
        self.expressionRunner = new ExpressionRunner(self.expression);
      }
    });
  }
  public getType(): string {
    return "expression";
  }
  /**
   * Use this property to display the value in your own format. Make sure you have "{0}" substring in your string, to display the actual value.
   */
  public get format(): string {
    return this.getLocalizableStringText("format", "");
  }
  public set format(val: string) {
    this.setLocalizableStringText("format", val);
  }
  get locFormat(): LocalizableString {
    return this.getLocalizableString("format");
  }
  /**
   * The Expression that used to calculate the question value. You may use standard operators like +, -, * and /, squares (). Here is the example of accessing the question value {questionname}.
   * <br/>Example: "({quantity} * {price}) * (100 - {discount}) / 100"
   */
  public get expression(): string {
    return this.getPropertyValue("expression", "");
  }
  public set expression(val: string) {
    this.setPropertyValue("expression", val);
  }
  public locCalculation() {
    this.expressionIsRunning = true;
  }
  public unlocCalculation() {
    this.expressionIsRunning = false;
  }
  public runCondition(values: HashTable<any>, properties: HashTable<any>) {
    super.runCondition(values, properties);
    if (!this.expression || this.expressionIsRunning) return;
    this.locCalculation();
    if (!this.expressionRunner) {
      this.expressionRunner = new ExpressionRunner(this.expression);
    }
    this.expressionRunner.onRunComplete = newValue => {
      if (!Helpers.isTwoValueEquals(newValue, this.value)) {
        this.value = newValue;
      }
      this.unlocCalculation();
    };
    this.expressionRunner.run(values, properties);
  }
{'fraction_non_alphanumeric': 0.12033765213977228, 'fraction_numerical': 0.0035335689045936395, 'mean_word_length': 3.9037536092396534, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, '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': '6582585', 'n_tokens_mistral': 1381, 'n_tokens_neox': 1357, 'n_words': 398}