Skip to content

Commit

Permalink
18540: Enables distance queries to retrieve any number of other value…
Browse files Browse the repository at this point in the history
…s from entity labels, MINOR (#38)
  • Loading branch information
howsohazard authored Dec 3, 2023
1 parent 54704d5 commit d47a3b4
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 60 deletions.
10 changes: 5 additions & 5 deletions docs/language.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/Amalgam/amlg_code/full_test.amlg
Original file line number Diff line number Diff line change
Expand Up @@ -2470,6 +2470,12 @@
))
)

(print "weighted query list of lists with multiple values: "
(compute_on_contained_entities "TestContainerExec" (list
(query_nearest_generalized_distance 5 (list "y" ) (list 0) (null) (null) (null) (null) 1 1 (null) (null) (null) (null) (list "y" "x"))
))
)

(create_entities "OverflowQueryContainer" (null) )
(create_entities (list "OverflowQueryContainer" "sess") (lambda (null ##.steps (list 1 2))))
(create_entities "OverflowQueryContainer" (lambda (null ##a 2)))
Expand Down
13 changes: 8 additions & 5 deletions src/Amalgam/entity/EntityManipulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,32 +137,35 @@ class EntityManipulation
template<typename EntityReference, typename GetEntityFunction>
static inline EvaluableNodeReference ConvertResultsToEvaluableNodes(
std::vector<DistanceReferencePair<EntityReference>> &results,
EvaluableNodeManager *enm, bool as_sorted_list, StringInternPool::StringID additional_sorted_list_label,
EvaluableNodeManager *enm, bool as_sorted_list, std::vector<StringInternPool::StringID> &additional_sorted_list_labels,
GetEntityFunction get_entity)
{
if(as_sorted_list)
{
//build list of results
EvaluableNode *query_return = enm->AllocNode(ENT_LIST);
auto &qr_ocn = query_return->GetOrderedChildNodesReference();
qr_ocn.resize(additional_sorted_list_label == string_intern_pool.NOT_A_STRING_ID ? 2 : 3);
//returning ids and computed values plus any additional values being retrieved
qr_ocn.resize(2 + additional_sorted_list_labels.size());

qr_ocn[0] = CreateListOfStringsIdsFromIteratorAndFunction(results, enm,
[get_entity](auto &drp) { return get_entity(drp.reference)->GetIdStringId(); });
qr_ocn[1] = CreateListOfNumbersFromIteratorAndFunction(results, enm, [](auto drp) { return drp.distance; });

//if adding on a label, retrieve the values from the entities
if(additional_sorted_list_label != string_intern_pool.NOT_A_STRING_ID)
for(size_t label_offset = 0; label_offset < additional_sorted_list_labels.size(); label_offset++)
{
auto label = additional_sorted_list_labels[label_offset];

//make a copy of the value at additionalSortedListLabel for each entity
EvaluableNode *list_of_values = enm->AllocNode(ENT_LIST);
qr_ocn[2] = list_of_values;
qr_ocn[2 + label_offset] = list_of_values;
auto &list_ocn = list_of_values->GetOrderedChildNodes();
list_ocn.resize(results.size());
for(size_t i = 0; i < results.size(); i++)
{
Entity *entity = get_entity(results[i].reference);
list_ocn[i] = entity->GetValueAtLabel(additional_sorted_list_label, enm, false);
list_ocn[i] = entity->GetValueAtLabel(label, enm, false);

//update cycle checks and idempotency
if(list_ocn[i] != nullptr)
Expand Down
4 changes: 2 additions & 2 deletions src/Amalgam/entity/EntityQueries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ EvaluableNodeReference EntityQueryCondition::GetMatchingEntities(Entity *contain
distance_transform.TransformDistances(entity_values, returnSortedList);

return EntityManipulation::ConvertResultsToEvaluableNodes<Entity *>(entity_values,
enm, returnSortedList, additionalSortedListLabel, [](auto entity) { return entity; });
enm, returnSortedList, additionalSortedListLabels, [](auto entity) { return entity; });
}

case ENT_QUERY_WITHIN_GENERALIZED_DISTANCE:
Expand Down Expand Up @@ -782,7 +782,7 @@ EvaluableNodeReference EntityQueryCondition::GetMatchingEntities(Entity *contain
distance_transform.TransformDistances(entity_values, returnSortedList);

return EntityManipulation::ConvertResultsToEvaluableNodes<Entity *>(entity_values,
enm, returnSortedList, additionalSortedListLabel, [](auto entity) { return entity; });
enm, returnSortedList, additionalSortedListLabels, [](auto entity) { return entity; });
}

default:
Expand Down
15 changes: 10 additions & 5 deletions src/Amalgam/entity/EntityQueries.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ class EntityQueryCondition
//pairs of ids and pairs of values
std::vector<std::pair<StringInternPool::StringID, std::pair<EvaluableNodeImmediateValue, EvaluableNodeImmediateValue>>> pairedLabels;

std::vector<StringInternPool::StringID> positionLabels; //the labels that comprise each dimension of the position
std::vector<EvaluableNodeImmediateValue> valueToCompare;//sometimes used for position values in conjunction with positionLabels
//the labels that comprise each dimension of the position
std::vector<StringInternPool::StringID> positionLabels;

//the labels corresponding to positionLabels when appropriate
std::vector<EvaluableNodeImmediateValue> valueToCompare;

GeneralizedDistance distParams;

Expand Down Expand Up @@ -108,10 +111,12 @@ class EntityQueryCondition
//indicates whether a compute result should be returned as a sorted list
bool returnSortedList;

//for ENT_QUERY_NEAREST_GENERALIZED_DISTANCE and ENT_QUERY_WITHIN_GENERALIZED_DISTANCE, if returnSortedList is true, additionally return this label if valid
StringInternPool::StringID additionalSortedListLabel;
//for ENT_QUERY_NEAREST_GENERALIZED_DISTANCE and ENT_QUERY_WITHIN_GENERALIZED_DISTANCE, if returnSortedList is true,
// additionally return these labels if valid
std::vector<StringInternPool::StringID> additionalSortedListLabels;

//if conviction_of_removal is true, then it will compute the conviction as if the entities were removed, if false, will compute added or included
//if conviction_of_removal is true, then it will compute the conviction as if the entities were removed, if false,
// will compute added or included
bool convictionOfRemoval;

//if true, use concurrency if applicable
Expand Down
30 changes: 25 additions & 5 deletions src/Amalgam/entity/EntityQueryBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,15 +371,25 @@ namespace EntityQueryBuilder
}

cur_condition->returnSortedList = false;
cur_condition->additionalSortedListLabel = string_intern_pool.NOT_A_STRING_ID;
cur_condition->additionalSortedListLabels.clear();
if(condition_type == ENT_QUERY_WITHIN_GENERALIZED_DISTANCE || condition_type == ENT_QUERY_NEAREST_GENERALIZED_DISTANCE || condition_type == ENT_COMPUTE_ENTITY_DISTANCE_CONTRIBUTIONS)
{
if(ocn.size() > NUM_MINKOWSKI_DISTANCE_QUERY_PARAMETERS + 0)
{
EvaluableNode *list_param = ocn[NUM_MINKOWSKI_DISTANCE_QUERY_PARAMETERS + 0];
cur_condition->returnSortedList = EvaluableNode::IsTrue(list_param);
if(!EvaluableNode::IsEmptyNode(list_param) && list_param->GetType() != ENT_TRUE && list_param->GetType() != ENT_FALSE)
cur_condition->additionalSortedListLabel = EvaluableNode::ToStringIDIfExists(list_param);
if(!EvaluableNode::IsEmptyNode(list_param))
{
if(list_param->GetType() == ENT_STRING)
{
cur_condition->additionalSortedListLabels.push_back(list_param->GetStringIDReference());
}
else
{
for(auto label_node : list_param->GetOrderedChildNodes())
cur_condition->additionalSortedListLabels.push_back(EvaluableNode::ToStringIDIfExists(label_node));
}
}
}
}
else if(condition_type == ENT_COMPUTE_ENTITY_CONVICTIONS || condition_type == ENT_COMPUTE_ENTITY_GROUP_KL_DIVERGENCE || condition_type == ENT_COMPUTE_ENTITY_KL_DIVERGENCES)
Expand All @@ -394,8 +404,18 @@ namespace EntityQueryBuilder
{
EvaluableNode *list_param = ocn[NUM_MINKOWSKI_DISTANCE_QUERY_PARAMETERS + 1];
cur_condition->returnSortedList = EvaluableNode::IsTrue(list_param);
if(!EvaluableNode::IsEmptyNode(list_param) && list_param->GetType() != ENT_TRUE && list_param->GetType() != ENT_FALSE)
cur_condition->additionalSortedListLabel = EvaluableNode::ToStringIDIfExists(list_param);
if(!EvaluableNode::IsEmptyNode(list_param))
{
if(list_param->GetType() == ENT_STRING)
{
cur_condition->additionalSortedListLabels.push_back(list_param->GetStringIDReference());
}
else
{
for(auto label_node : list_param->GetOrderedChildNodes())
cur_condition->additionalSortedListLabels.push_back(EvaluableNode::ToStringIDIfExists(label_node));
}
}
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Amalgam/entity/EntityQueryCaches.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ void EntityQueryCaches::EnsureLabelsAreCached(EntityQueryCondition *cond)
labels_to_add.push_back(cond->weightLabel);
}

if(cond->additionalSortedListLabel != StringInternPool::NOT_A_STRING_ID)
for(auto label : cond->additionalSortedListLabels)
{
if(!DoesHaveLabel(cond->additionalSortedListLabel))
labels_to_add.push_back(cond->additionalSortedListLabel);
if(!DoesHaveLabel(label))
labels_to_add.push_back(label);
}

break;
Expand Down Expand Up @@ -1249,7 +1249,7 @@ EvaluableNodeReference EntityQueryCaches::GetMatchingEntitiesFromQueryCaches(Ent
|| last_query_type == ENT_COMPUTE_ENTITY_KL_DIVERGENCES)
{
return EntityManipulation::ConvertResultsToEvaluableNodes<size_t>(compute_results,
enm, last_query->returnSortedList, last_query->additionalSortedListLabel,
enm, last_query->returnSortedList, last_query->additionalSortedListLabels,
[&contained_entities](auto entity_index) { return contained_entities[entity_index]; });
}
else //if there are no compute results, return an assoc of the requested labels for each entity
Expand Down
74 changes: 40 additions & 34 deletions src/Amalgam/out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ abcdef
"2020-06-08 lunes 11.33.48"
)
--indices--
(list "4" "a" "c" "b")
(list "a" "c" "b" "4")
(list
0
1
Expand All @@ -1020,7 +1020,7 @@ abcdef
7
)
--values--
(list "d" 1 3 2)
(list 1 3 2 "d")
(list
"a"
1
Expand All @@ -1041,7 +1041,7 @@ abcdef
4
"d"
)
(list "d" 1 0 3 2)
(list 1 0 3 2 "d")
(list
1
2
Expand Down Expand Up @@ -1252,7 +1252,7 @@ current_index: 2
interpreter "C:\\Users\\Chris Hazard\\Desktop\\Howso_repos\\amalgam\\x64\\MT_Release_EXE\\Amalgam.exe"
raaa 2
rwww 1
start_time 1701482606.173477
start_time 1701560610.772288
www 1
x 12
zz 10
Expand Down Expand Up @@ -1295,7 +1295,7 @@ current_index: 2
interpreter "C:\\Users\\Chris Hazard\\Desktop\\Howso_repos\\amalgam\\x64\\MT_Release_EXE\\Amalgam.exe"
raaa 2
rwww 1
start_time 1701482606.173477
start_time 1701560610.772288
www 1
x 12
zz 10
Expand Down Expand Up @@ -1337,7 +1337,7 @@ current_index: 2
interpreter "C:\\Users\\Chris Hazard\\Desktop\\Howso_repos\\amalgam\\x64\\MT_Release_EXE\\Amalgam.exe"
raaa 2
rwww 1
start_time 1701482606.173477
start_time 1701560610.772288
www 1
x 12
zz 10
Expand Down Expand Up @@ -1485,7 +1485,7 @@ infinity test c or d: (list "c" @(get (target 0) 0) "d" @(get (target 0) 0))

(assoc a 29 b 44 c 27)

(list "3" "8" "1")
(list "7" "3" "1")

--get_rand_seed--
��ȼ�\�KOaV�T z�
Expand Down Expand Up @@ -1605,7 +1605,7 @@ e:
- .inf

25: (assoc a 1)
current date-time in epoch: 2023-12-01-21.03.26.5207280
current date-time in epoch: 2023-12-02-18.43.31.0723590
2020-06-07 00:22:59
1391230800
1391230800
Expand Down Expand Up @@ -2689,10 +2689,10 @@ MergeEntityChild1
(associate "x" 3 "y" 4)
MergeEntityChild2
(associate "p" 3 "q" 4)
_2280722175
(associate "E" 3 "F" 4)
_2169689611
(associate "e" 3 "f" 4)
_2280722175
(associate "E" 3 "F" 4)
--union_entities--
(associate "b" 4 "a" 3 "c" 3)
MergeEntityChild1
Expand All @@ -2710,17 +2710,6 @@ MergeEntityChild2
"w"
7
)
_2280722175
(associate
"E"
3
"F"
4
"G"
5
"H"
6
)
_2169689611
(associate
"e"
Expand All @@ -2732,11 +2721,7 @@ _2169689611
"h"
6
)
(parallel
##p
(list "_3990396532" "_3330773578" "_3990396532" "_3330773578")
)
_3990396532
_2280722175
(associate
"E"
3
Expand All @@ -2747,6 +2732,10 @@ _3990396532
"H"
6
)
(parallel
##p
(list "_3990396532" "_3330773578" "_3990396532" "_3330773578")
)
_3330773578
(associate
"e"
Expand All @@ -2758,6 +2747,17 @@ _3330773578
"h"
6
)
_3990396532
(associate
"E"
3
"F"
4
"G"
5
"H"
6
)
--difference_entities--
(declare
(assoc _ (null))
Expand Down Expand Up @@ -3388,10 +3388,10 @@ MergeEntityChild2
"w"
7
)
_2280722175
(associate "E" 3 "F" 4)
_2169689611
(associate "e" 3 "f" 4 "g" 5)
(associate "e" 3 "f" 4)
_2280722175
(associate "E" 3 "F" 4 "G" 5)
--get_entity_comments--
Full test
This is a suite of unit tests.
Expand Down Expand Up @@ -3446,7 +3446,7 @@ deep sets

--set_entity_root_permission--
RootTest
1701482606.71256
1701560611.471558
(true)

RootTest
Expand Down Expand Up @@ -3670,7 +3670,7 @@ hello
)
)
)
(set_entity_rand_seed new_entity "�0]e��0є-I���")
(set_entity_rand_seed new_entity "<����gR0є-I���")
(set_entity_rand_seed
(first
(create_entities
Expand All @@ -3680,7 +3680,7 @@ hello
)
)
)
" �9Y�J�z�����"
"��<�����Xe/���$�"
)
(set_entity_rand_seed
(first
Expand Down Expand Up @@ -3713,7 +3713,7 @@ hello
)
)
)
(set_entity_rand_seed new_entity "�0]e��0є-I���")
(set_entity_rand_seed new_entity "<����gR0є-I���")
(set_entity_rand_seed
(first
(create_entities
Expand Down Expand Up @@ -3953,6 +3953,12 @@ weighted query list of lists: (list
(list 1 2 4 10 100)
(list -1 2 4 10 100)
)
weighted query list of lists with multiple values: (list
(list "Child2" "Child6" "Child1" "Child7" "Child3")
(list 1 2 4 10 100)
(list -1 2 4 10 100)
(list -1 1 3 0 100)
)
(list
(list
"_290180734"
Expand Down Expand Up @@ -4654,4 +4660,4 @@ Expecting 1000: 1000
concurrent entity writes successful: (true)

--total execution time--
1.2170350551605225
1.2860238552093506

0 comments on commit d47a3b4

Please sign in to comment.