<commit_before>import NSDictionary from "../Base/NSDictionary";
import IGomlInterface from "../Interface/IGomlInterface";
import Ensure from "../Base/Ensure";
import IAttributeDeclaration from "./IAttributeDeclaration";
import AttributeConverter from "./AttributeConverter";
import NSIdentity from "../Base/NSIdentity";
import GrimoireInterface from "../GrimoireInterface";
import Component from "./Component";

/**
 * Management a single attribute with specified type. Converter will serve a value with object with any type instead of string.
 * When attribute is changed, emit a "change" event. When attribute is requested, emit a "get" event.
 * If responsive flag is not true, event will not be emitted.
 */
class Attribute {

  public name: NSIdentity;
  public declaration: IAttributeDeclaration;
  public converter: AttributeConverter;
  public component: Component;


  private _value: any;
  private _handlers: ((attr: Attribute) => void)[] = [];

  public get tree(): IGomlInterface {
    return this.component.tree;
  }

  public get companion(): NSDictionary<any> {
    return this.component.companion;
  }

  /**
   * Get a value with specified type.
   * @return {any} value with specified type.
   */
  public get Value(): any {
    try {
      return (this.converter as any).convert(this._value);
    } catch (e) {
      console.error(e); // TODO should be more convenient error handling
    }
  }

  /**
   * Set a value with any type.
   * @param {any} val Value with string or specified type.
   */
  public set Value(val: any) {
    this._value = val;
    this._notifyChange();
  }

  /**
   * Construct a new attribute with name of key and any value with specified type. If constant flag is true, This attribute will be immutable.
   * If converter is not served, string converter will be set as default.
   * @param {string}        key       Key of this attribute.
   * @param {any}           value     Value of this attribute.
   * @param {ConverterBase} converter Converter of this attribute.
   * @param {boolean}       constant  Whether this attribute is immutable or not. False as default.
   */
  constructor(name: string, declaration: IAttributeDeclaration, component: Component) {
    this.name = new NSIdentity(component.name.ns, name);
    this.component = component;
    this.declaration = declaration;
    const converterName = Ensure.ensureTobeNSIdentity(declaration.converter);
    this.converter = GrimoireInterface.converters.get(converterName);
    if (this.converter === void 0) {
      // When the specified converter was not found
      throw new Error(`Specified converter ${converterName} was not found from registered converters.\n Component: ${this.component.name.fqn}\n Attribute: ${this.name.name}`);
    }
    this.converter = {
      convert: this.converter.convert.bind(this),
      name: this.converter.name<commit_msg>fixed bug on error message<commit_after>import NSDictionary from "../Base/NSDictionary";
import IGomlInterface from "../Interface/IGomlInterface";
import Ensure from "../Base/Ensure";
import IAttributeDeclaration from "./IAttributeDeclaration";
import AttributeConverter from "./AttributeConverter";
import NSIdentity from "../Base/NSIdentity";
import GrimoireInterface from "../GrimoireInterface";
import Component from "./Component";

/**
 * Management a single attribute with specified type. Converter will serve a value with object with any type instead of string.
 * When attribute is changed, emit a "change" event. When attribute is requested, emit a "get" event.
 * If responsive flag is not true, event will not be emitted.
 */
class Attribute {

  public name: NSIdentity;
  public declaration: IAttributeDeclaration;
  public converter: AttributeConverter;
  public component: Component;


  private _value: any;
  private _handlers: ((attr: Attribute) => void)[] = [];

  public get tree(): IGomlInterface {
    return this.component.tree;
  }

  public get companion(): NSDictionary<any> {
    return this.component.companion;
  }

  /**
   * Get a value with specified type.
   * @return {any} value with specified type.
   */
  public get Value(): any {
    try {
      return (this.converter as any).convert(this._value);
    } catch (e) {
      console.error(e); // TODO should be more convenient error handling
    }
  }

  /**
   * Set a value with any type.
   * @param {any} val Value with string or specified type.
   */
  public set Value(val: any) {
    this._value = val;
    this._notifyChange();
  }

  /**
   * Construct a new attribute with name of key and any value with specified type. If constant flag is true, This attribute will be immutable.
   * If converter is not served, string converter will be set as default.
   * @param {string}        key       Key of this attribute.
   * @param {any}           value     Value of this attribute.
   * @param {ConverterBase} converter Converter of this attribute.
   * @param {boolean}       constant  Whether this attribute is immutable or not. False as default.
   */
  constructor(name: string, declaration: IAttributeDeclaration, component: Component) {
    this.name = new NSIdentity(component.name.ns, name);
    this.component = component;
    this.declaration = declaration;
    const converterName = Ensure.ensureTobeNSIdentity(declaration.converter);
    this.converter = GrimoireInterface.converters.get(converterName);
    if (this.converter === void 0) {
      // When the specified converter was not found
      throw new Error(`Specified converter ${converterName.name} was not found from registered converters.\n Component: ${this.component.name.fqn}\n Attribute: ${this.name.name}`);
    }
    this.converter = {
      convert: this.converter.convert.bind(this),
      name: this.converter.name
{'fraction_non_alphanumeric': 0.10121527777777778, 'fraction_numerical': 0.00034722222222222224, 'mean_word_length': 3.458978328173375, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3268231', 'n_tokens_mistral': 1559, 'n_tokens_neox': 1524, 'n_words': 575}