Exceptions

All exceptions in fsm-tools inherit from AutomatonException. Each exception carries a unique numeric error code computed from three axes:

The error code is computed as follows:

code = 1000 × CHOMSKY_GRAMMARS[grammar]
     +  100 × COMPONENTS[component]
     +    1 × ACTIONS[action]

Given any code, the grammar level, component, and action can be recovered unambiguously. All messages are in English (en-US).

Base classes

class fsm_tools.AutomatonException(grammar: str, component: str, action: str, locale: str | None = None, **event)[source]

Bases: Exception

Generic exception for errors in formal automata. Computes a unique error code based on the Chomsky grammar hierarchy level, the component, and the action.

Localized error codes are fetched from errors.mo. General exception messages are fetched from exception_msg.mo.

This class implements the exception basis for formal grammar automata.

Parameters:
  • grammar (str) – name of the formal grammar hierarchy level.

  • component (str) – name of a formal grammar’s component .

  • action (str) – name of an action on a formal grammar’s component.

  • locale (str) – name of the locale if not the environment locale will be used.

Returns:

None

Return type:

None

Raises:

ValueError.

set_locale(new_locale: str) None[source]
generate_message(locale: str) str[source]
class fsm_tools.AutomatonError(grammar: str, component: str, action: str, locale: str | None = None, **event)[source]

Bases: AutomatonException

This class implements the exception basis for formal grammar automata.

Parameters:
  • grammar (str) – name of the formal grammar hierarchy level.

  • component (str) – name of a formal grammar’s component .

  • action (str) – name of an action on a formal grammar’s component.

  • locale (str) – name of the locale if not the environment locale will be used.

Returns:

None

Return type:

None

Raises:

ValueError.

event = {}
generate_message(locale: str) str[source]

Typed exceptions

class fsm_tools.ReadError(grammar_level, component, locale=None, **event)[source]

Bases: AutomatonError

Error raised for reading actions (01, AttributeError).

This class implements the exception basis for formal grammar automata.

Parameters:
  • grammar (str) – name of the formal grammar hierarchy level.

  • component (str) – name of a formal grammar’s component .

  • action (str) – name of an action on a formal grammar’s component.

  • locale (str) – name of the locale if not the environment locale will be used.

Returns:

None

Return type:

None

Raises:

ValueError.

class fsm_tools.AddError(grammar_level, component, locale=None, **event)[source]

Bases: AutomatonError

Error raised for addition actions (02, ValueError).

This class implements the exception basis for formal grammar automata.

Parameters:
  • grammar (str) – name of the formal grammar hierarchy level.

  • component (str) – name of a formal grammar’s component .

  • action (str) – name of an action on a formal grammar’s component.

  • locale (str) – name of the locale if not the environment locale will be used.

Returns:

None

Return type:

None

Raises:

ValueError.

class fsm_tools.RemoveError(grammar_level, component, locale=None, **event)[source]

Bases: AutomatonError

Error raised for removal actions (03, KeyError).

This class implements the exception basis for formal grammar automata.

Parameters:
  • grammar (str) – name of the formal grammar hierarchy level.

  • component (str) – name of a formal grammar’s component .

  • action (str) – name of an action on a formal grammar’s component.

  • locale (str) – name of the locale if not the environment locale will be used.

Returns:

None

Return type:

None

Raises:

ValueError.

class fsm_tools.ModifyError(grammar_level, component, locale=None, **event)[source]

Bases: AutomatonError

Error raised for modification actions (04, ValueError).

This class implements the exception basis for formal grammar automata.

Parameters:
  • grammar (str) – name of the formal grammar hierarchy level.

  • component (str) – name of a formal grammar’s component .

  • action (str) – name of an action on a formal grammar’s component.

  • locale (str) – name of the locale if not the environment locale will be used.

Returns:

None

Return type:

None

Raises:

ValueError.

class fsm_tools.ValidationError(grammar_level, component, locale=None, **event)[source]

Bases: AutomatonError

Error raised for validation actions (05, AssertionError).

This class implements the exception basis for formal grammar automata.

Parameters:
  • grammar (str) – name of the formal grammar hierarchy level.

  • component (str) – name of a formal grammar’s component .

  • action (str) – name of an action on a formal grammar’s component.

  • locale (str) – name of the locale if not the environment locale will be used.

Returns:

None

Return type:

None

Raises:

ValueError.

class fsm_tools.SearchError(grammar_level, component, locale=None, **event)[source]

Bases: AutomatonError

Error raised for search actions (06, KeyError).

This class implements the exception basis for formal grammar automata.

Parameters:
  • grammar (str) – name of the formal grammar hierarchy level.

  • component (str) – name of a formal grammar’s component .

  • action (str) – name of an action on a formal grammar’s component.

  • locale (str) – name of the locale if not the environment locale will be used.

Returns:

None

Return type:

None

Raises:

ValueError.

class fsm_tools.RemoveComponentError(grammar_level, component, locale=None, **event)[source]

Bases: AutomatonError

Error raised for component withdrawal actions (19, RuntimeError).

This class implements the exception basis for formal grammar automata.

Parameters:
  • grammar (str) – name of the formal grammar hierarchy level.

  • component (str) – name of a formal grammar’s component .

  • action (str) – name of an action on a formal grammar’s component.

  • locale (str) – name of the locale if not the environment locale will be used.

Returns:

None

Return type:

None

Raises:

ValueError.

Constants

The following constants define the valid values for grammar levels, components, and actions. They are used internally by the exception system.

This file contains constants that will be used to manage the components, actions and grammars of the Chomsky hierarchy and automata.

fsm_tools.constants.CHOMSKY_GRAMMARS = {'Context-Free': 3, 'Context-Sensitive': 2, 'Recursively Enumerable': 1, 'Regular': 4}

CHOMSKY_GRAMMAR is a dictionary that maps the different types of grammars in the Chomsky hierarchy to their corresponding integer values. These integer values are primarily used for message handling, error reporting, and processing purposes.

Each grammar type in the hierarchy is assigned a unique integer value based on its rank in the hierarchy. The values are incremented by 1 from their theoretical positions (e.g., Type-0 → 1, Type-1 → 2, etc.) to ensure the values are suitable for computation and indexing in various operations.

The mapping is as follows:
  • “Type-0” (Recursively Enumerable or Unrestricted Grammar): Value 1

  • “Type-1” (Context-Sensitive Grammar): Value 2

  • “Type-2” (Context-Free Grammar): Value 3

  • “Type-3” (Regular Grammar): Value 4

This structure allows the grammar types to be easily referenced and utilized in applications where a numeric representation is required.

fsm_tools.constants.COMPONENTS = {'alphabet': 1, 'blank': 11, 'grammar': 4, 'head': 8, 'moves': 9, 'register': 10, 'stack': 5, 'states': 3, 'tape': 7, 'transitions': 2, 'validation': 6}

COMPONENTS is a dictionary that assigns a unique integer value to each key representing a fundamental component in computational models and formal grammar processing.

The integer values serve as identifiers, used for ordering, processing, or error handling in various operations. The values are assigned in a manner that reflects their relative importance or usage frequency in certain contexts.

The mapping is as follows:
  • “alphabet”: Value 1 - Represents the set of terminal symbols in a grammar.

  • “states”: Value 3 - Represents the set of non-terminal symbols or states in an automaton.

  • “transitions”: Value 2 - Represents the rules or transitions that govern state changes.

  • “stack”: Value 5 - Represents the stack used in pushdown automata.

  • “grammar”: Value 4 - Represents the formal grammar associated with the automaton or model.

  • “validation”: Value 6 - Represents the validation component for string recognition or processing.

These integer values provide a straightforward way to reference or prioritize components in computational workflows.

fsm_tools.constants.ACTIONS = {'add': 2, 'modify': 4, 'move': 8, 'read': 1, 'remove': 3, 'search': 6, 'validate': 5, 'withdraw': 19, 'write': 7}

ACTIONS is a dictionary that maps common operations or actions to unique integer values, providing a standardized way to reference and process these actions in computational workflows.

The integer values are used as identifiers for ordering, prioritization, or error handling during the execution of various tasks. These values are designed to be consistent and easily computable.

The mapping is as follows:
  • “read”: Value 1 - Represents the action of reading or retrieving data.

  • “add”: Value 2 - Represents the action of adding new data or elements.

  • “remove”: Value 3 - Represents the action of deleting or removing elements.

  • “modify”: Value 4 - Represents the action of changing or updating existing data.

  • “validate”: Value 5 - Represents the action of checking correctness or conformity.

  • “search”: Value 6 - Represents the action of locating specific elements or data.

  • “withdraw”: Value 19 - Represents the action of retracting or taking back elements.

These integer values ensure consistency in referencing actions across different modules or systems, enabling streamlined processing and error management.