Packages

A high-performance, analytical Datalog engine for Gleam

Current section

Files

Jump to
aarondb src aarondb@engine@cognitive.erl
Raw

src/aarondb@engine@cognitive.erl

-module(aarondb@engine@cognitive).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/aarondb/engine/cognitive.gleam").
-export([solve/8]).
-file("src/aarondb/engine/cognitive.gleam", 71).
-spec resolve_part(
aarondb@shared@ast:part(),
gleam@dict:dict(binary(), aarondb@fact:value())
) -> gleam@option:option(aarondb@fact:value()).
resolve_part(Part, Ctx) ->
case Part of
{var, Name} ->
gleam@option:from_result(gleam_stdlib:map_get(Ctx, Name));
{val, Val} ->
{some, Val};
{uid, Uid} ->
{some, {ref, Uid}};
{attr_val, S} ->
{some, {str, S}};
{lookup, {_, Val@1}} ->
{some, Val@1}
end.
-file("src/aarondb/engine/cognitive.gleam", 12).
-spec solve(
aarondb@shared@state:db_state(),
aarondb@shared@ast:part(),
aarondb@shared@ast:part(),
float(),
binary(),
gleam@dict:dict(binary(), aarondb@fact:value()),
gleam@option:option(integer()),
gleam@option:option(integer())
) -> list(gleam@dict:dict(binary(), aarondb@fact:value())).
solve(
Db_state,
Concept,
Context,
Threshold,
Engram_var,
Ctx,
As_of_tx,
As_of_valid
) ->
Concept_val = resolve_part(Concept, Ctx),
Context_val = resolve_part(Context, Ctx),
Active_concept = begin
_pipe = case Concept_val of
{some, V} ->
aarondb@index:get_datoms_by_val(
erlang:element(4, Db_state),
<<"engram/concept"/utf8>>,
V
);
none ->
aarondb@index:get_all_datoms_for_attr(
erlang:element(3, Db_state),
<<"engram/concept"/utf8>>
)
end,
_pipe@1 = aarondb@engine@entity:filter_by_time(
_pipe,
As_of_tx,
As_of_valid
),
aarondb@engine@entity:filter_active(_pipe@1, Db_state)
end,
Active_context = begin
_pipe@2 = case Context_val of
{some, V@1} ->
aarondb@index:get_datoms_by_val(
erlang:element(4, Db_state),
<<"engram/context"/utf8>>,
V@1
);
none ->
aarondb@index:get_all_datoms_for_attr(
erlang:element(3, Db_state),
<<"engram/context"/utf8>>
)
end,
_pipe@3 = aarondb@engine@entity:filter_by_time(
_pipe@2,
As_of_tx,
As_of_valid
),
aarondb@engine@entity:filter_active(_pipe@3, Db_state)
end,
Concept_eids = begin
_pipe@4 = gleam@list:map(
Active_concept,
fun(D) -> erlang:element(2, D) end
),
gleam@set:from_list(_pipe@4)
end,
Context_eids = begin
_pipe@5 = gleam@list:map(
Active_context,
fun(D@1) -> erlang:element(2, D@1) end
),
gleam@set:from_list(_pipe@5)
end,
_pipe@6 = gleam@set:intersection(Concept_eids, Context_eids),
_pipe@7 = gleam@set:to_list(_pipe@6),
gleam@list:filter_map(
_pipe@7,
fun(Eid) ->
Relevance_datoms = begin
_pipe@8 = aarondb@index:get_datoms_by_entity_attr(
erlang:element(3, Db_state),
Eid,
<<"engram/relevance"/utf8>>
),
_pipe@9 = aarondb@engine@entity:filter_by_time(
_pipe@8,
As_of_tx,
As_of_valid
),
aarondb@engine@entity:filter_active(_pipe@9, Db_state)
end,
Score = case Relevance_datoms of
[D@2 | _] ->
case erlang:element(4, D@2) of
{float, F} ->
F;
{int, I} ->
erlang:float(I);
_ ->
+0.0
end;
[] ->
1.0
end,
case Score >= Threshold of
true ->
{ok, gleam@dict:insert(Ctx, Engram_var, {ref, Eid})};
false ->
{error, nil}
end
end
).