# `Logistiki.Knowledge.Facts`
[🔗](https://github.com/thanos/logistiki/blob/v0.1.0/lib/logistiki/knowledge/facts.ex#L1)

Generates runtime Datalog facts from a normalized business event.

The event is represented in the Datalog program by the fixed atom `:evt`.
Generated facts are added to the program before materialization.

## Generated relations

| Relation | Arity | Example |
|----------|-------|---------|
| `event_type` | 2 | `{:evt, :deposit_received}` |
| `event_currency` | 2 | `{:evt, "USD"}` |
| `event_amount_cents` | 2 | `{:evt, 100000}` |
| `event_fee_type` | 2 | `{:evt, :wire_fee}` |
| `event_entity_type` | 2 | `{:evt, :corporate}` |
| `event_product` | 2 | `{:evt, :retail_deposit}` |
| `event_account` | 2 | `{:evt, "LIABILITIES:CLIENT_DEPOSITS:USD:ACME:OPERATING"}` |
| `event_cash_account` | 2 | `{:evt, "ASSETS:CASH:USD:NOSTRO"}` |
| `event_fee_income_account` | 2 | `{:evt, "INCOME:FEES:WIRE"}` |
| `event_interest_expense_account` | 2 | `{:evt, "EXPENSES:INTEREST"}` |
| `event_destination_account` | 2 | `{:evt, "LIABILITIES:CLIENT_DEPOSITS:USD:ACME:PAYROLL"}` |

Only fields with non-nil values generate facts.

# `event_id`
*since 0.1.0* 

```elixir
@spec event_id() :: :evt
```

Returns the fixed event identifier used inside the Datalog program.

## Returns

  * `:evt` — the atom representing the current event in all Datalog rules.

## Examples

    iex> Logistiki.Knowledge.Facts.event_id()
    :evt

# `generate`
*since 0.1.0* 

```elixir
@spec generate(Logistiki.Event.Normalized.t()) :: [{atom(), [term()]}]
```

Generates a list of `{relation, values}` facts for `normalized`.

## Arguments

  * `normalized` — `%Logistiki.Event.Normalized{}` — the flattened event.

## Returns

  * `[{atom(), [term()]}]` — a list of `{relation_name, argument_list}`
    tuples ready to be added to the Datalog program.

## Examples

    iex> facts = Logistiki.Knowledge.Facts.generate(%Logistiki.Event.Normalized{
    ...>   type: "deposit_received", amount: Decimal.new("1000.00"), currency: "USD",
    ...>   account_code: "LIAB:ACME", cash_account_code: "ASSETS:CASH"
    ...> })
    iex> {:event_type, [:evt, :deposit_received]} in facts
    true
    iex> {:event_currency, [:evt, "USD"]} in facts
    true
    iex> {:event_amount_cents, [:evt, 100000]} in facts
    true

---

*Consult [api-reference.md](api-reference.md) for complete listing*
