arfficionado v0.1.0 Arfficionado.Handler behaviour View Source

Handler behaviour.

Arfficionado.read/4 will:

  1. call init(arg) to obtain the initial handler state (arg defaults to nil)
  2. parse the ARFF header and

    • call relation with the relation name and optional comment
    • call attributes with the list of attributes found in the header
    • call line_comment for lines that consists of commentary only
    • call begin_data with optional comment to indicate that the header is finished and instance data will follow
  3. parse the ARFF data section

    • call instance for each line of data, reporting instance values, weight and optional comment
    • call line_comment for lines that consists of commentary only
  4. call close

Arfficionado.read/4 will pass in the current handler state on each callback invocation. Callback functions return an updated handler state and generally indicate whether to continue reading the ARFF file or to stop early.

Once the ARFF file is exhausted, an error is encountered in the ARFF file, or the handler has indicated that it wishes to stop the processing, the close callback will be invoked, allowing the handler to create the final result state (and clean up resources).

Implement this behaviour as per your application's requirements (for example, including side effects such as creating and writing to an ETS table). The sources for Arfficionado.ListHandler and Arfficionado.MapHandler may provide some general guidance.

Link to this section Summary

Types

An attribute definition with name, type and optional comment.

Attribute type.

List of attribute definitions (ARFF header).

A comment.

Date format.

Name for an attribute/relation.

A comment or nil.

Your handler's state.

Success/failure indicator.

t()

Arfficionado.Handler type.

Return value for most handler callbacks.

An attribute-value.

An instance's attribute-values.

Instance weight.

Callbacks

Invoked once all @attributes have been parsed.

Invoked when @data has been encountered, reporting the optional comment following @data.

Invoked when the processing has finished. The first argument indicates whether processing was successful (:ok) or an error was encountered (:error).

Creates initial state from given argument.

Invoked for each data instance, reporting the list of attribute-values (in the order given in the ARFF header), the instance weight (defaulting to 1) and an optional comment.

Invoked when a line is encountered that contains nothing but a comment.

Invoked when @relation is encountered, reporting the relation name and an optional comment.

Link to this section Types

Link to this type

attribute()

View Source
attribute() :: {:attribute, name(), attribute_type(), optional_comment()}

An attribute definition with name, type and optional comment.

Link to this type

attribute_type()

View Source
attribute_type() ::
  :numeric | {:nominal, [atom()]} | :string | {:date, date_format()}

Attribute type.

Link to this type

attributes()

View Source
attributes() :: [attribute()]

List of attribute definitions (ARFF header).

A comment.

Link to this type

date_format()

View Source
date_format() :: :iso_8601 | String.t()

Date format.

Name for an attribute/relation.

Link to this type

optional_comment()

View Source
optional_comment() :: comment() | nil

A comment or nil.

Your handler's state.

Link to this type

status()

View Source
status() :: :ok | :error

Success/failure indicator.

Arfficionado.Handler type.

Link to this type

updated_state()

View Source
updated_state() :: {:cont, state()} | {:halt, state()}

Return value for most handler callbacks.

An attribute-value.

Link to this type

values()

View Source
values() :: [value()]

An instance's attribute-values.

Instance weight.

Link to this section Callbacks

Link to this callback

attributes(attributes, state)

View Source
attributes(attributes(), state()) :: updated_state()

Invoked once all @attributes have been parsed.

Link to this callback

begin_data(optional_comment, state)

View Source (optional)
begin_data(optional_comment(), state()) :: updated_state()

Invoked when @data has been encountered, reporting the optional comment following @data.

Link to this callback

close(status, state)

View Source
close(status(), state()) :: state()

Invoked when the processing has finished. The first argument indicates whether processing was successful (:ok) or an error was encountered (:error).

Creates initial state from given argument.

Link to this callback

instance(values, weight, optional_comment, state)

View Source
instance(values(), weight(), optional_comment(), state()) :: updated_state()

Invoked for each data instance, reporting the list of attribute-values (in the order given in the ARFF header), the instance weight (defaulting to 1) and an optional comment.

Return {:cont, updated_state} to continue parsing, or {:halt, updated_state} to stop early.

Link to this callback

line_comment(comment, state)

View Source (optional)
line_comment(comment(), state()) :: updated_state()

Invoked when a line is encountered that contains nothing but a comment.

Link to this callback

relation(name, optional_comment, state)

View Source (optional)
relation(name(), optional_comment(), state()) :: updated_state()

Invoked when @relation is encountered, reporting the relation name and an optional comment.