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

Helpers for querying materialized `ExDatalog.Knowledge` for Logistiki facts.

The event under evaluation is always identified by the atom `:evt` inside the
Datalog program (see `Logistiki.Knowledge.Facts.event_id/0`).

# `account_roles`
*since 0.1.0* 

```elixir
@spec account_roles(ExDatalog.Knowledge.t()) :: %{required(atom()) =&gt; String.t()}
```

Returns all resolved `account_role` facts for the current event.

## Arguments

  * `knowledge` — `%ExDatalog.Knowledge{}`.

## Returns

  * `%{atom() => String.t()}` — role-to-account-code map (e.g.
    `%{cash_account: "ASSETS:CASH:USD:NOSTRO", ...}`).

## Examples

    iex> Logistiki.Knowledge.Query.account_roles(knowledge)
    %{cash_account: "ASSETS:CASH:USD:NOSTRO", client_liability_account: "LIABILITIES:CLIENT_DEPOSITS:USD:ACME:OPERATING"}

# `blocked?`
*since 0.1.0* 

```elixir
@spec blocked?(ExDatalog.Knowledge.t()) :: boolean()
```

True when the event is blocked by a business rule.

## Arguments

  * `knowledge` — `%ExDatalog.Knowledge{}`.

## Returns

  * `boolean()` — `true` if `blocked(:evt)` was derived.

## Examples

    iex> Logistiki.Knowledge.Query.blocked?(knowledge)
    false

# `policies`
*since 0.1.0* 

```elixir
@spec policies(ExDatalog.Knowledge.t()) :: [atom()]
```

Returns all derived `policy` facts for the current event.

## Arguments

  * `knowledge` — `%ExDatalog.Knowledge{}` — materialized knowledge.

## Returns

  * `[atom()]` — the policy atoms derived for `:evt` (e.g. `[:cash_deposit]`).
    Empty list if no policy matched.

## Examples

    iex> Logistiki.Knowledge.Query.policies(knowledge)
    [:cash_deposit]

# `required_dimensions`
*since 0.1.0* 

```elixir
@spec required_dimensions(ExDatalog.Knowledge.t()) :: %{required(atom()) =&gt; [atom()]}
```

Returns all `requires_dimension` facts grouped by policy.

## Arguments

  * `knowledge` — `%ExDatalog.Knowledge{}`.

## Returns

  * `%{atom() => [atom()]}` — policy-to-dimensions map (e.g.
    `%{cash_deposit: [:entity_id, :currency, :account_code]}`).

## Examples

    iex> Logistiki.Knowledge.Query.required_dimensions(knowledge)
    %{cash_deposit: [:entity_id, :currency, :account_code]}

# `requires_approval?`
*since 0.1.0* 

```elixir
@spec requires_approval?(ExDatalog.Knowledge.t()) :: boolean()
```

True when the event requires approval.

## Arguments

  * `knowledge` — `%ExDatalog.Knowledge{}`.

## Returns

  * `boolean()` — `true` if `requires_approval(:evt)` was derived.

## Examples

    iex> Logistiki.Knowledge.Query.requires_approval?(knowledge)
    false

# `template_postings`
*since 0.1.0* 

```elixir
@spec template_postings(ExDatalog.Knowledge.t()) :: [map()]
```

Returns all `template_posting` facts (for every policy).

## Arguments

  * `knowledge` — `%ExDatalog.Knowledge{}`.

## Returns

  * `[map()]` — posting specs with `:policy`, `:sequence`, `:direction`,
    `:role`, `:amount_var`, `:currency_var`.

## Examples

    iex> Logistiki.Knowledge.Query.template_postings(knowledge) |> hd()
    %{policy: :cash_deposit, sequence: 1, direction: :debit, role: :cash_account, amount_var: :event_amount, currency_var: :event_currency}

---

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