| summaryrefslogtreecommitdiff |
diff options
Diffstat (limited to 'src/knowledge')
| -rw-r--r-- | src/knowledge/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | src/knowledge/knowledge.h | 68 | ||||
| -rw-r--r-- | src/knowledge/knowledge_finalize.c | 55 | ||||
| -rw-r--r-- | src/knowledge/knowledge_get_random_sequence.c | 83 | ||||
| -rw-r--r-- | src/knowledge/knowledge_get_random_target.c | 108 | ||||
| -rw-r--r-- | src/knowledge/knowledge_learn_markov_sequence.c | 4 | ||||
| -rw-r--r-- | src/knowledge/knowledge_learn_sequence.c | 7 | ||||
| -rw-r--r-- | src/knowledge/knowledge_learn_word.c | 6 | ||||
| -rw-r--r-- | src/knowledge/knowledge_search.c | 224 | ||||
| -rw-r--r-- | src/knowledge/knowledge_swt_tws_modifications.c | 311 | ||||
| -rw-r--r-- | src/knowledge/knowledge_types.h | 26 |
11 files changed, 627 insertions, 269 deletions
diff --git a/src/knowledge/CMakeLists.txt b/src/knowledge/CMakeLists.txt index 1245321..ba3293d 100644 --- a/src/knowledge/CMakeLists.txt +++ b/src/knowledge/CMakeLists.txt @@ -2,9 +2,13 @@ set( SRC_FILES ${SRC_FILES} ${CMAKE_CURRENT_SOURCE_DIR}/knowledge.c ${CMAKE_CURRENT_SOURCE_DIR}/knowledge_finalize.c + ${CMAKE_CURRENT_SOURCE_DIR}/knowledge_get_random_sequence.c + ${CMAKE_CURRENT_SOURCE_DIR}/knowledge_get_random_target.c + ${CMAKE_CURRENT_SOURCE_DIR}/knowledge_learn_markov_sequence.c ${CMAKE_CURRENT_SOURCE_DIR}/knowledge_learn_sequence.c ${CMAKE_CURRENT_SOURCE_DIR}/knowledge_learn_word.c ${CMAKE_CURRENT_SOURCE_DIR}/knowledge_search.c + ${CMAKE_CURRENT_SOURCE_DIR}/knowledge_swt_tws_modifications.c ) set(SRC_FILES ${SRC_FILES} PARENT_SCOPE) diff --git a/src/knowledge/knowledge.h b/src/knowledge/knowledge.h index e868943..7f1bb42 100644 --- a/src/knowledge/knowledge.h +++ b/src/knowledge/knowledge.h @@ -61,24 +61,6 @@ int ZoO_knowledge_learn_markov_sequence const struct ZoO_pipe io [const restrict static 1] ); -int ZoO_knowledge_get_swt_sequences_ref -( - const struct ZoO_knowledge k [const static 1], - const ZoO_index initial_word, - const ZoO_index * restrict following_sequences_ref [const restrict static 1], - const ZoO_index * restrict following_sequences_weights [const restrict static 1], - ZoO_index following_sequences_weights_sum [const static 1], - const struct ZoO_pipe io [const restrict static 1] -); - -int ZoO_knowledge_get_sequence -( - const struct ZoO_knowledge k [const static 1], - const ZoO_index sequences_ref, - const ZoO_index * restrict sequence [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] -); - int ZoO_knowledge_get_word ( const struct ZoO_knowledge k [const static 1], @@ -106,26 +88,50 @@ int ZoO_knowledge_find_word_id ZoO_index result [const restrict static 1] ); -int ZoO_knowledge_find_tws_targets +int ZoO_knowledge_find_sequence ( const struct ZoO_knowledge k [const static 1], - const ZoO_index sequence [const restrict], - const ZoO_index markov_order, - const ZoO_index * restrict targets [const restrict static 1], - const ZoO_index * restrict targets_weights [const restrict static 1], - ZoO_index targets_weights_sum [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + const ZoO_index sequence [const restrict static 1], + const ZoO_index markov_order, /* Pre: (> 1) */ + ZoO_index sequence_id [const restrict static 1] +); + +int ZoO_knowledge_find_markov_sequence +( + const ZoO_index sequence_id, + const struct ZoO_knowledge_sequence_collection sc [const restrict static 1], + ZoO_index result [const restrict static 1] ); -int ZoO_knowledge_find_swt_targets +int ZoO_knowledge_find_sequence_target +( + const ZoO_index target_id, + const struct ZoO_knowledge_sequence_data sd [const restrict static 1], + ZoO_index result [const restrict static 1] +); + +int ZoO_knowledge_random_tws_target ( const struct ZoO_knowledge k [const static 1], - const ZoO_index sequence [const restrict], - const size_t sequence_length, + ZoO_index target [const restrict static 1], + const ZoO_index word_id, + const ZoO_index sequence_id +); + +int ZoO_knowledge_random_swt_target +( + const struct ZoO_knowledge k [const static 1], + const ZoO_index sequence_id, + const ZoO_index word_id, + ZoO_index target [const restrict static 1] +); + +int ZoO_knowledge_copy_random_swt_sequence +( + const struct ZoO_knowledge k [const static 1], + ZoO_index sequence [const restrict static 1], + const ZoO_index word_id, const ZoO_index markov_order, - const ZoO_index * restrict targets [const restrict static 1], - const ZoO_index * restrict targets_weights [const restrict static 1], - ZoO_index targets_weights_sum [const restrict static 1], const struct ZoO_pipe io [const restrict static 1] ); diff --git a/src/knowledge/knowledge_finalize.c b/src/knowledge/knowledge_finalize.c index 9546650..6c249f0 100644 --- a/src/knowledge/knowledge_finalize.c +++ b/src/knowledge/knowledge_finalize.c @@ -2,52 +2,51 @@ #include "knowledge.h" -static void knowledge_sequence_collection_finalize + +static void knowledge_sequence_data_finalize ( - struct ZoO_knowledge_sequence_collection c [const restrict static 1] + struct ZoO_knowledge_sequence_data sd [const restrict static 1] ) { - ZoO_index i; + sd->occurrences = 0; - if (c->sequences_ref != (ZoO_index *) NULL) + if (sd->targets != (struct ZoO_knowledge_target *) NULL) { - free((void *) c->sequences_ref); - c->sequences_ref = (ZoO_index *) NULL; - } + free((void *) sd->targets); - if (c->sequences_ref_sorted != (ZoO_index *) NULL) - { - free((void *) c->sequences_ref_sorted); - c->sequences_ref_sorted = (ZoO_index *) NULL; + sd->targets = (struct ZoO_knowledge_target *) NULL; } - if (c->occurrences != (ZoO_index *) NULL) - { - free((void *) c->occurrences); - c->occurrences = (ZoO_index *) NULL; - } + sd->targets_length = 0; + +} + +static void knowledge_sequence_collection_finalize +( + struct ZoO_knowledge_sequence_collection c [const restrict static 1] +) +{ + ZoO_index i; + for (i = 0; i < c->sequences_ref_length; ++i) { - free((void *) c->targets[i]); - free((void *) c->targets_occurrences[i]); + knowledge_sequence_data_finalize(c->sequences_ref + i); } - c->sequences_ref_length = 0; - - if (c->targets != (ZoO_index **) NULL) + if (c->sequences_ref != (struct ZoO_knowledge_sequence_data *) NULL) { - free((void *) c->targets); - c->targets != (ZoO_index **) NULL; + free((void *) c->sequences_ref); + c->sequences_ref = (struct ZoO_knowledge_sequence_data *) NULL; } - free((void *) c->targets_length); - - if (c->targets_occurrences != (ZoO_index **) NULL) + if (c->sequences_ref_sorted != (ZoO_index *) NULL) { - free((void *) c->targets_occurrences); - c->targets_occurrences != (ZoO_index **) NULL; + free((void *) c->sequences_ref_sorted); + c->sequences_ref_sorted = (ZoO_index *) NULL; } + + c->sequences_ref_length = 0; } static void knowledge_word_finalize diff --git a/src/knowledge/knowledge_get_random_sequence.c b/src/knowledge/knowledge_get_random_sequence.c new file mode 100644 index 0000000..9055d31 --- /dev/null +++ b/src/knowledge/knowledge_get_random_sequence.c @@ -0,0 +1,83 @@ +#include <stdlib.h> + +#include "../core/char.h" +#include "../core/index.h" +#include "../sequence/sequence.h" + +#include "../pipe/pipe.h" + +#include "knowledge.h" + +static int weighted_random_pick +( + const struct ZoO_knowledge_sequence_collection sc [const restrict static 1], + const ZoO_index sum, + ZoO_index result [const restrict static 1] +) +{ + ZoO_index accumulator, random_number; + + accumulator = 0; + + if (sum == 0) + { + return -1; + } + + random_number = ZoO_index_random_up_to(sum); + /*@ ensures (0 <= random_number <= weights_sum); @*/ + + *result = 0; + + for (;;) + { + accumulator += sc->sequences_ref[*result].occurrences; + + if (accumulator < random_number) + { + *result += 1; + } + else + { + *result = sc->sequences_ref[*result].id; + + return 0; + } + } +} + +int ZoO_knowledge_copy_random_swt_sequence +( + const struct ZoO_knowledge k [const static 1], + ZoO_index sequence [const restrict static 1], + const ZoO_index word_id, + const ZoO_index markov_order, + const struct ZoO_pipe io [const restrict static 1] +) +{ + ZoO_index sequence_id; + + if + ( + weighted_random_pick + ( + &(k->words[word_id].swt), + k->words[word_id].occurrences, + &sequence_id + ) < 0 + ) + { + /* TODO: Err message. */ + + return -1; + } + + memcpy + ( + (void *) sequence, + (const void *) k->sequences[sequence_id], + (((size_t) (markov_order - 1)) * sizeof(ZoO_index)) + ); + + return 0; +} diff --git a/src/knowledge/knowledge_get_random_target.c b/src/knowledge/knowledge_get_random_target.c new file mode 100644 index 0000000..d9bf8a9 --- /dev/null +++ b/src/knowledge/knowledge_get_random_target.c @@ -0,0 +1,108 @@ +#include <stdlib.h> + +#include "../core/char.h" +#include "../core/index.h" +#include "../sequence/sequence.h" + +#include "../pipe/pipe.h" + +#include "knowledge.h" + +static int weighted_random_pick +( + const struct ZoO_knowledge_sequence_data sd [const restrict static 1], + ZoO_index result [const restrict static 1] +) +{ + ZoO_index accumulator, random_number; + + accumulator = 0; + + if (sd->occurrences == 0) + { + return -1; + } + + random_number = ZoO_index_random_up_to(sd->occurrences); + /*@ ensures (0 <= random_number <= weights_sum); @*/ + + *result = 0; + + for (;;) + { + accumulator += sd->targets[*result].occurrences; + + if (accumulator < random_number) + { + *result += 1; + } + else + { + *result = sd->targets[*result].id; + + return 0; + } + } +} + +int ZoO_knowledge_random_tws_target +( + const struct ZoO_knowledge k [const static 1], + ZoO_index target [const restrict static 1], + const ZoO_index word_id, + const ZoO_index sequence_id +) +{ + ZoO_index s_index; + + if + ( + ZoO_knowledge_find_markov_sequence + ( + sequence_id, + &(k->words[word_id].tws), + &s_index + ) < 0 + ) + { + return -1; + } + + return + weighted_random_pick + ( + &(k->words[word_id].tws.sequences_ref[s_index]), + target + ); +} + +int ZoO_knowledge_random_swt_target +( + const struct ZoO_knowledge k [const static 1], + const ZoO_index sequence_id, + const ZoO_index word_id, + ZoO_index target [const restrict static 1] +) +{ + ZoO_index s_index; + + if + ( + ZoO_knowledge_find_markov_sequence + ( + sequence_id, + &(k->words[word_id].swt), + &s_index + ) < 0 + ) + { + return -1; + } + + return + weighted_random_pick + ( + &(k->words[word_id].swt.sequences_ref[s_index]), + target + ); +} diff --git a/src/knowledge/knowledge_learn_markov_sequence.c b/src/knowledge/knowledge_learn_markov_sequence.c index 4258c4a..35cc123 100644 --- a/src/knowledge/knowledge_learn_markov_sequence.c +++ b/src/knowledge/knowledge_learn_markov_sequence.c @@ -244,7 +244,7 @@ static int add_sequence /** SEARCH EXISTING SEQUENCES *************************************************/ /******************************************************************************/ -static int find_sequence +int ZoO_knowledge_find_sequence ( const struct ZoO_knowledge k [const static 1], const ZoO_index sequence [const restrict static 1], @@ -332,7 +332,7 @@ int ZoO_knowledge_learn_markov_sequence if ( - find_sequence + ZoO_knowledge_find_sequence ( k, sequence, diff --git a/src/knowledge/knowledge_learn_sequence.c b/src/knowledge/knowledge_learn_sequence.c index 927b186..6a666bd 100644 --- a/src/knowledge/knowledge_learn_sequence.c +++ b/src/knowledge/knowledge_learn_sequence.c @@ -8,8 +8,6 @@ #include "knowledge.h" - - /******************************************************************************/ /** LEARN FOLLOWING SEQUENCE **************************************************/ /******************************************************************************/ @@ -47,6 +45,7 @@ static int add_swt_sequence const ZoO_index sequence [const restrict static 1], const size_t index, const size_t sequence_length, + const ZoO_index markov_order, ZoO_index buffer [const restrict static 1], const ZoO_index buffer_length, const struct ZoO_pipe io [const restrict static 1] @@ -134,6 +133,7 @@ static int add_tws_sequence const ZoO_index sequence [const restrict static 1], const size_t index, const size_t sequence_length, + const ZoO_index markov_order, ZoO_index buffer [const restrict static 1], const ZoO_index buffer_length, const struct ZoO_pipe io [const restrict static 1] @@ -220,12 +220,14 @@ int ZoO_knowledge_learn_sequence for (i = 0; i < sequence_length; ++i) { + /* TODO: handle failure. */ add_tws_sequence ( k, sequence, i, sequence_length, + markov_order, buffer, buffer_length, io @@ -237,6 +239,7 @@ int ZoO_knowledge_learn_sequence sequence, i, sequence_length, + markov_order, buffer, buffer_length, io diff --git a/src/knowledge/knowledge_learn_word.c b/src/knowledge/knowledge_learn_word.c index 5d932d2..8430bc0 100644 --- a/src/knowledge/knowledge_learn_word.c +++ b/src/knowledge/knowledge_learn_word.c @@ -15,13 +15,9 @@ static void initialize_sequence_collection struct ZoO_knowledge_sequence_collection c [const restrict static 1] ) { - c->sequences_ref = (ZoO_index *) NULL; + c->sequences_ref = (struct ZoO_knowledge_sequence_data *) NULL; c->sequences_ref_length = 0; c->sequences_ref_sorted = (ZoO_index *) NULL; - c->occurrences = (ZoO_index *) NULL; - c->targets = (ZoO_index **) NULL; - c->targets_length = (ZoO_index *) NULL; - c->targets_occurrences = (ZoO_index **) NULL; } static void initialize_word diff --git a/src/knowledge/knowledge_search.c b/src/knowledge/knowledge_search.c index 198da1d..cc1934e 100644 --- a/src/knowledge/knowledge_search.c +++ b/src/knowledge/knowledge_search.c @@ -73,106 +73,36 @@ int ZoO_knowledge_find_word_id } } -/* pre: \length(sequence) >= markov_order */ -int ZoO_knowledge_find_tws_targets +int ZoO_knowledge_find_markov_sequence ( - const struct ZoO_knowledge k [const static 1], - const ZoO_index sequence [restrict static 1], - const ZoO_index markov_order, /* Pre: (> 0) */ - const ZoO_index * restrict targets [const restrict static 1], - const ZoO_index * restrict targets_weights [const restrict static 1], - ZoO_index targets_weights_sum [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + const ZoO_index sequence_id, + const struct ZoO_knowledge_sequence_collection sc [const restrict static 1], + ZoO_index result [const restrict static 1] ) { /* This is a binary search */ int cmp; - ZoO_index i, current_min, current_max, local_sequence; - const ZoO_index * restrict candidate; - const ZoO_index markov_sequence_length = (markov_order - 1); - const ZoO_index word = sequence[0]; - - if - ( - (word >= k->words_length) - || (k->words[word].occurrences == 0) - ) - { - ZoO_S_ERROR - ( - io, - "Attempting to find the TWS targets of an unknown word." - ); - - *targets = (const ZoO_index *) NULL; - *targets_weights = (const ZoO_index *) NULL; - *targets_weights_sum = 0; - - return -1; - } - - - if (markov_order == 1) - { - /* Special case: empty sequences. */ - *targets = (const ZoO_index *) k->words[word].tws.targets; - - *targets_weights = - (const ZoO_index *) k->words[word].tws.targets_occurrences; - - *targets_weights_sum = k->words[word].occurrences; - - return 0; - } - - - /* pre: \length(sequence) >= markov_order */ - /* markov_order > 1 */ - sequence += 1; /* get the relevant part of the sequence. */ - - /* Handles the case where the list is empty ********************************/ - current_max = k->words[word].tws.sequences_ref_length; + ZoO_index i, current_min, current_max; - if (current_max == 0) + if (sc->sequences_ref_length == 0) { - *targets = (const ZoO_index *) NULL; - *targets_weights = (const ZoO_index *) NULL; - *targets_weights_sum = 0; - - ZoO_S_ERROR - ( - io, - "Attempting to find the TWS targets of a sequence that never had any." - ); + *result = 0; - return -2; + return -1; } - /***************************************************************************/ current_min = 0; - current_max -= 1; + current_max = (sc->sequences_ref_length - 1); for (;;) { i = (current_min + ((current_max - current_min) / 2)); - local_sequence = k->words[word].tws.sequences_ref_sorted[i]; - - (void) ZoO_knowledge_get_sequence - ( - k, - k->words[word].tws.sequences_ref[local_sequence], - &candidate, - io - ); - cmp = - ZoO_sequence_cmp + ZoO_index_cmp ( - sequence, - markov_sequence_length, - candidate, - markov_sequence_length + sequence_id, + sc->sequences_ref[sc->sequences_ref_sorted[i]].id ); if (cmp > 0) @@ -181,137 +111,57 @@ int ZoO_knowledge_find_tws_targets if (current_min > current_max) { - *targets = (const ZoO_index *) NULL; - *targets_weights = (const ZoO_index *) NULL; - *targets_weights_sum = 0; + *result = current_min; - return -2; + return -1; } } else if (cmp < 0) { if ((current_min > current_max) || (i == 0)) { - *targets = (const ZoO_index *) NULL; - *targets_weights = (const ZoO_index *) NULL; - *targets_weights_sum = 0; + *result = current_min; - return -2; + return -1; } current_max = (i - 1); } else { - *targets = k->words[word].tws.targets[local_sequence]; - - *targets_weights = - k->words[word].tws.targets_occurrences[local_sequence]; - - *targets_weights_sum = - k->words[word].tws.occurrences[local_sequence]; + *result = sc->sequences_ref_sorted[i]; return 0; } } } -int ZoO_knowledge_find_swt_targets +int ZoO_knowledge_find_sequence_target ( - const struct ZoO_knowledge k [const static 1], - const ZoO_index sequence [restrict static 1], - const size_t sequence_length, - const ZoO_index markov_order, - const ZoO_index * restrict targets [const restrict static 1], - const ZoO_index * restrict targets_weights [const restrict static 1], - ZoO_index targets_weights_sum [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + const ZoO_index target_id, + const struct ZoO_knowledge_sequence_data sd [const restrict static 1], + ZoO_index result [const restrict static 1] ) { /* This is a binary search */ int cmp; - ZoO_index i, current_min, current_max, local_sequence; - const ZoO_index * restrict candidate; - const ZoO_index markov_sequence_length = (markov_order - 1); - const ZoO_index word = sequence[sequence_length - 1]; - - if - ( - (word >= k->words_length) - || (k->words[word].occurrences == 0) - ) - { - ZoO_S_ERROR - ( - io, - "Attempting to find the SWT targets of an unknown word." - ); - - *targets = (const ZoO_index *) NULL; - *targets_weights = (const ZoO_index *) NULL; - *targets_weights_sum = 0; - - return -1; - } - - if (markov_order == 1) - { - /* Special case: empty sequences. */ - *targets = (const ZoO_index *) k->words[word].swt.targets; - - *targets_weights = - (const ZoO_index *) k->words[word].swt.targets_occurrences; - - *targets_weights_sum = k->words[word].occurrences; - - return 0; - } - - sequence = (sequence + (sequence_length - markov_order)); - /* Handles the case where the list is empty ********************************/ - current_max = k->words[word].swt.sequences_ref_length; + ZoO_index i, current_min, current_max; - if (current_max == 0) + if (sd->targets_length == 0) { - *targets = (const ZoO_index *) NULL; - *targets_weights = (const ZoO_index *) NULL; - *targets_weights_sum = 0; - - ZoO_S_WARNING - ( - io, - "Attempting to find the SWT targets of a sequence that never had any." - ); + *result = 0; - return -2; + return -1; } - /***************************************************************************/ current_min = 0; - current_max -= 1; + current_max = (sd->targets_length - 1); for (;;) { i = (current_min + ((current_max - current_min) / 2)); - local_sequence = k->words[word].swt.sequences_ref_sorted[i]; - - (void) ZoO_knowledge_get_sequence - ( - k, - k->words[word].swt.sequences_ref[local_sequence], - &candidate, - io - ); - - cmp = - ZoO_sequence_cmp - ( - sequence, - markov_sequence_length, - candidate, - markov_sequence_length - ); + cmp = ZoO_index_cmp(target_id, sd->targets[i].id); if (cmp > 0) { @@ -319,35 +169,25 @@ int ZoO_knowledge_find_swt_targets if (current_min > current_max) { - *targets = (const ZoO_index *) NULL; - *targets_weights = (const ZoO_index *) NULL; - *targets_weights_sum = 0; + *result = current_min; - return -2; + return -1; } } else if (cmp < 0) { if ((current_min > current_max) || (i == 0)) { - *targets = (const ZoO_index *) NULL; - *targets_weights = (const ZoO_index *) NULL; - *targets_weights_sum = 0; + *result = current_min; - return -2; + return -1; } current_max = (i - 1); } else { - *targets = k->words[word].swt.targets[local_sequence]; - - *targets_weights = - k->words[word].swt.targets_occurrences[local_sequence]; - - *targets_weights_sum = - k->words[word].swt.occurrences[local_sequence]; + *result = i; return 0; } diff --git a/src/knowledge/knowledge_swt_tws_modifications.c b/src/knowledge/knowledge_swt_tws_modifications.c index 87305f2..40e2e3b 100644 --- a/src/knowledge/knowledge_swt_tws_modifications.c +++ b/src/knowledge/knowledge_swt_tws_modifications.c @@ -1,5 +1,157 @@ +#include <stdlib.h> + +#include "../core/index.h" + +#include "../pipe/pipe.h" + #include "knowledge.h" +static int add_target +( + struct ZoO_knowledge_sequence_data sd [const restrict static 1], + const ZoO_index target_id, + const ZoO_index s_index, + const ZoO_index t_index, + const struct ZoO_pipe io [const restrict static 1] +) +{ + struct ZoO_knowledge_target * new_p; + + /* (sd->targets_length == ZoO_INDEX_MAX) => target_id \in sd->targets. */ + + sd->targets_length += 1; + + new_p = + (struct ZoO_knowledge_target *) realloc + ( + (void *) sd->targets, + (sd->targets_length * sizeof(struct ZoO_knowledge_target)) + ); + + if (new_p == (struct ZoO_knowledge_target *) NULL) + { + ZoO_S_ERROR + ( + io, + "[E] Unable to allocate memory required to store more targets." + ); + + sd->targets_length -= 1; + + return -1; + } + + sd->targets = new_p; + + if (t_index != (sd->targets_length - 1)) + { + memmove + ( + (void *) (sd->targets + t_index + 1), + (const void *) (sd->targets + t_index), + (size_t) + ( + ((sd->targets_length - t_index) - 1) + * sizeof(struct ZoO_knowledge_target) + ) + ); + } + + sd->targets[t_index].id = target_id; + sd->targets[t_index].occurrences = 0; + + return 0; +} + +static int add_sequence +( + struct ZoO_knowledge_sequence_collection sc [const restrict static 1], + const ZoO_index sequence_id, + ZoO_index s_index [const restrict static 1], + const struct ZoO_pipe io [const restrict static 1] +) +{ + struct ZoO_knowledge_sequence_data * new_p; + ZoO_index * new_ps; + + /* + * (sc->sequences_ref_length == ZoO_INDEX_MAX) => + * sequence_id \in sc->sequences_ref. + */ + + sc->sequences_ref_length += 1; + + new_p = + (struct ZoO_knowledge_sequence_data *) realloc + ( + (void *) sc->sequences_ref, + (sc->sequences_ref_length * sizeof(struct ZoO_knowledge_sequence_data)) + ); + + if (new_p == (struct ZoO_knowledge_sequence_data *) NULL) + { + ZoO_S_ERROR + ( + io, + "[E] Unable to allocate memory required to store new preceding or " + " following sequence." + ); + + sc->sequences_ref_length -= 1; + + return -1; + } + + sc->sequences_ref = new_p; + + new_ps = + (ZoO_index *) realloc + ( + (void *) sc->sequences_ref_sorted, + (sc->sequences_ref_length * sizeof(ZoO_index)) + ); + + if (new_p == (struct ZoO_knowledge_sequence_data *) NULL) + { + ZoO_S_ERROR + ( + io, + "[E] Unable to allocate memory required to store new preceding or " + " following sequence." + ); + + sc->sequences_ref_length -= 1; + + return -1; + } + + sc->sequences_ref_sorted = new_ps; + + if (*s_index != (sc->sequences_ref_length - 1)) + { + memmove + ( + (void *) (sc->sequences_ref_sorted + (*s_index) + 1), + (const void *) (sc->sequences_ref_sorted + (*s_index)), + (size_t) + ( + ((sc->sequences_ref_length - (*s_index)) - 1) + * sizeof(ZoO_index) + ) + ); + } + + sc->sequences_ref_sorted[*s_index] = (sc->sequences_ref_length - 1); + *s_index = (sc->sequences_ref_length - 1); + + sc->sequences_ref[*s_index].id = sequence_id; + sc->sequences_ref[*s_index].occurrences = 0; + sc->sequences_ref[*s_index].targets = (struct ZoO_knowledge_target *) NULL; + sc->sequences_ref[*s_index].targets_length = 0; + + return -1; +} + int ZoO_knowledge_strengthen_swt ( struct ZoO_knowledge k [const restrict static 1], @@ -9,9 +161,85 @@ int ZoO_knowledge_strengthen_swt const struct ZoO_pipe io [const restrict static 1] ) { - /* TODO */ + ZoO_index s_index, t_index; - return -1; + if + ( + ZoO_knowledge_find_markov_sequence + ( + sequence_id, + &(k->words[word_id].swt), + &s_index + ) < 0 + ) + { + if + ( + add_sequence + ( + &(k->words[word_id].swt), + sequence_id, + &s_index, + io + ) < 0 + ) + { + return -1; + } + } + + if + ( + ZoO_knowledge_find_sequence_target + ( + target_id, + (k->words[word_id].swt.sequences_ref + s_index), + &t_index + ) < 0 + ) + { + if + ( + add_target + ( + &(k->words[word_id].swt.sequences_ref[s_index]), + target_id, + s_index, + t_index, + io + ) < 0 + ) + { + return -1; + } + } + + if + ( + ( + k->words[word_id].swt.sequences_ref[s_index].occurrences + == ZoO_INDEX_MAX + ) + || + ( + k->words[word_id].swt.sequences_ref[s_index].targets[t_index].occurrences + == ZoO_INDEX_MAX + ) + ) + { + ZoO_S_WARNING + ( + io, + "[W] Unable to strengthen SWT link: link is already at max strength." + ); + + return 1; + } + + k->words[word_id].swt.sequences_ref[s_index].occurrences += 1; + k->words[word_id].swt.sequences_ref[s_index].targets[t_index].occurrences += 1; + + return 0; } int ZoO_knowledge_strengthen_tws @@ -23,7 +251,84 @@ int ZoO_knowledge_strengthen_tws const struct ZoO_pipe io [const restrict static 1] ) { - /* TODO */ + ZoO_index s_index, t_index; + + if + ( + ZoO_knowledge_find_markov_sequence + ( + sequence_id, + &(k->words[word_id].tws), + &s_index + ) < 0 + ) + { + if + ( + add_sequence + ( + &(k->words[word_id].tws), + sequence_id, + &s_index, + io + ) < 0 + ) + { + return -1; + } + } + + + if + ( + ZoO_knowledge_find_sequence_target + ( + target_id, + (k->words[word_id].tws.sequences_ref + s_index), + &t_index + ) < 0 + ) + { + if + ( + add_target + ( + &(k->words[word_id].tws.sequences_ref[s_index]), + target_id, + s_index, + t_index, + io + ) < 0 + ) + { + return -1; + } + } + + if + ( + ( + k->words[word_id].tws.sequences_ref[s_index].occurrences + == ZoO_INDEX_MAX + ) + || + ( + k->words[word_id].tws.sequences_ref[s_index].targets[t_index].occurrences + == ZoO_INDEX_MAX + ) + ) + { + ZoO_S_ERROR + ( + io, + "[E] Unable to strengthen TWS link: link is already at max strength." + ); + + return -1; + } + + k->words[word_id].tws.sequences_ref[s_index].occurrences += 1; + k->words[word_id].tws.sequences_ref[s_index].targets[t_index].occurrences += 1; return -1; } diff --git a/src/knowledge/knowledge_types.h b/src/knowledge/knowledge_types.h index 9db77bd..53330ce 100644 --- a/src/knowledge/knowledge_types.h +++ b/src/knowledge/knowledge_types.h @@ -1,20 +1,32 @@ #ifndef _ZoO_KNOWLEDGE_KNOWLEDGE_TYPES_H_ #define _ZoO_KNOWLEDGE_KNOWLEDGE_TYPES_H_ -#include <pthread.h> +#ifndef ZoO_RUNNING_FRAMA_C + #include <pthread.h> +#endif #include "../core/index_types.h" #include "../core/char_types.h" +struct ZoO_knowledge_target +{ + ZoO_index id; + ZoO_index occurrences; +}; + +struct ZoO_knowledge_sequence_data +{ + ZoO_index id; + ZoO_index occurrences; + struct ZoO_knowledge_target * targets; + ZoO_index targets_length; +}; + struct ZoO_knowledge_sequence_collection { - ZoO_index * sequences_ref; + struct ZoO_knowledge_sequence_data * sequences_ref; ZoO_index sequences_ref_length; ZoO_index * sequences_ref_sorted; - ZoO_index * occurrences; - ZoO_index ** targets; - ZoO_index * targets_length; - ZoO_index ** targets_occurrences; }; struct ZoO_knowledge_word @@ -38,7 +50,9 @@ struct ZoO_knowledge ZoO_index ** sequences; ZoO_index sequences_length; ZoO_index * sequences_sorted; +#ifndef ZoO_RUNNING_FRAMA_C pthread_mutex_t mutex; +#endif }; #endif |


