summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2017-01-20 22:19:09 +0100
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2017-01-20 22:19:09 +0100
commitdf3657b2a99ef20da99ac3c6c02f43cc23e70fca (patch)
tree86a9e72bbbbaf7296b2d7cd2725a8bc42611a1f3 /src/core
parent0d49fb74eadcf933f696420cd182077927680d26 (diff)
Moving towards a server/clients structure.
Diffstat (limited to 'src/core')
-rw-r--r--src/core/CMakeLists.txt1
-rw-r--r--src/core/index_types.h2
-rw-r--r--src/core/sequence.h9
-rw-r--r--src/core/sequence_creation.c102
-rw-r--r--src/core/sequence_from_string.c70
-rw-r--r--src/core/sequence_to_string.c97
6 files changed, 238 insertions, 43 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index 1e1daa8..bc722d7 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -5,6 +5,7 @@ set(
${CMAKE_CURRENT_SOURCE_DIR}/sequence.c
${CMAKE_CURRENT_SOURCE_DIR}/sequence_creation.c
${CMAKE_CURRENT_SOURCE_DIR}/sequence_from_string.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/sequence_to_string.c
)
set(SRC_FILES ${SRC_FILES} PARENT_SCOPE)
diff --git a/src/core/index_types.h b/src/core/index_types.h
index ad56d52..a71121c 100644
--- a/src/core/index_types.h
+++ b/src/core/index_types.h
@@ -1,6 +1,8 @@
#ifndef _ZoO_CORE_INDEX_TYPES_H_
#define _ZoO_CORE_INDEX_TYPES_H_
+#include <limits.h>
+
/* Must be unsigned. */
typedef unsigned int ZoO_index;
diff --git a/src/core/sequence.h b/src/core/sequence.h
index 77ecd6c..1e286ab 100644
--- a/src/core/sequence.h
+++ b/src/core/sequence.h
@@ -3,6 +3,9 @@
#include "../core/char_types.h"
#include "../core/index_types.h"
+
+#include "../pipe/pipe.h"
+
#include "../knowledge/knowledge_types.h"
#include "sequence_types.h"
@@ -13,7 +16,8 @@ int ZoO_sequence_from_undercase_string
const ZoO_index string_length,
struct ZoO_knowledge k [const restrict static 1],
ZoO_index * sequence [const restrict static 1],
- ZoO_index sequence_length [const restrict static 1]
+ ZoO_index sequence_length [const restrict static 1],
+ const struct ZoO_pipe io [const restrict static 1]
);
/*
@@ -44,7 +48,8 @@ int ZoO_sequence_create_from
const struct ZoO_knowledge k [const restrict static 1],
const ZoO_index markov_order,
ZoO_index * sequence [const restrict static 1],
- size_t sequence_size [const restrict static 1]
+ size_t sequence_size [const restrict static 1],
+ const struct ZoO_pipe io [const restrict static 1]
);
/*
diff --git a/src/core/sequence_creation.c b/src/core/sequence_creation.c
index f460629..c5ca6af 100644
--- a/src/core/sequence_creation.c
+++ b/src/core/sequence_creation.c
@@ -3,10 +3,11 @@
#include <string.h>
#include <stdint.h> /* defines SIZE_MAX */
-#include "../io/error.h"
-
#include "../core/index.h"
-#include "../core/knowledge.h"
+
+#include "../pipe/pipe.h"
+
+#include "../knowledge/knowledge.h"
#include "sequence.h"
@@ -66,7 +67,8 @@ static int left_append
(
const ZoO_index word_id,
ZoO_index * sequence [const restrict],
- const size_t sequence_size
+ const size_t sequence_size,
+ const struct ZoO_pipe io [const restrict static 1]
)
{
ZoO_index * new_sequence;
@@ -75,6 +77,7 @@ static int left_append
{
ZoO_S_ERROR
(
+ io,
"Left side append aborted, as the new sequence's size would overflow."
);
@@ -87,6 +90,7 @@ static int left_append
{
ZoO_S_ERROR
(
+ io,
"Left side append aborted, as memory for the new sequence could not be"
" allocated."
);
@@ -118,6 +122,8 @@ static int left_append
* to fit there.
* This requires the reallocation of {sequence}. The freeing of the previous
* memory space is handled. If an error happened, {*sequence} remains untouched.
+ * Semaphore:
+ * Takes then releases access for {k}.
* Returns:
* 0 on success.
* -1 iff nothing fitting was found.
@@ -133,13 +139,16 @@ static int extend_left
ZoO_index * sequence [const restrict static 1],
const size_t sequence_size,
const ZoO_index markov_order,
- const struct ZoO_knowledge k [const restrict static 1]
+ const struct ZoO_knowledge k [const restrict static 1],
+ const struct ZoO_pipe io [const restrict static 1]
)
{
const ZoO_index * restrict preceding_words;
const ZoO_index * restrict preceding_words_weights;
ZoO_index preceding_words_weights_sum;
+ (void) ZoO_knowledge_lock_access(k, io);
+
if
(
ZoO_knowledge_find_preceding_words
@@ -149,10 +158,13 @@ static int extend_left
markov_order,
&preceding_words,
&preceding_words_weights,
- &preceding_words_weights_sum
+ &preceding_words_weights_sum,
+ io
) < 0
)
{
+ (void) ZoO_knowledge_unlock_access(k, io);
+
return -1;
}
@@ -168,13 +180,18 @@ static int extend_left
preceding_words_weights_sum
),
sequence,
- sequence_size
+ sequence_size,
+ io
) < 0
)
{
+ (void) ZoO_knowledge_unlock_access(k, io);
+
return -3;
}
+ (void) ZoO_knowledge_unlock_access(k, io);
+
return 0;
}
@@ -206,14 +223,15 @@ static int complete_left_part_of_sequence
size_t sequence_size [const restrict static 1],
const ZoO_index markov_order,
ZoO_index credits [const restrict],
- const struct ZoO_knowledge k [const restrict static 1]
+ const struct ZoO_knowledge k [const restrict static 1],
+ const struct ZoO_pipe io [const restrict static 1]
)
{
for (;;)
{
if ((credits == (ZoO_index *) NULL) || (*credits > 0))
{
- if (extend_left(sequence, *sequence_size, markov_order, k) < 0)
+ if (extend_left(sequence, *sequence_size, markov_order, k, io) < 0)
{
/* We are sure *sequence[0] is defined. */
if (*sequence[0] == ZoO_START_OF_SEQUENCE_ID)
@@ -255,6 +273,7 @@ static int complete_left_part_of_sequence
case ZoO_END_OF_SEQUENCE_ID:
ZoO_S_WARNING
(
+ io,
"END OF LINE was added at the left part of an sequence."
);
@@ -291,7 +310,8 @@ static int right_append
ZoO_index * sequence [const restrict],
const ZoO_index word_id,
const size_t sequence_size,
- const ZoO_index sequence_length
+ const ZoO_index sequence_length,
+ const struct ZoO_pipe io [const restrict static 1]
)
{
ZoO_index * new_sequence;
@@ -300,6 +320,7 @@ static int right_append
{
ZoO_S_ERROR
(
+ io,
"Right side append aborted, as the new sequence's size would overflow."
);
@@ -317,6 +338,7 @@ static int right_append
{
ZoO_S_ERROR
(
+ io,
"Right side append aborted, as memory for the new sequence could not "
"be allocated."
);
@@ -336,6 +358,8 @@ static int right_append
* to fit there.
* This requires the reallocation of {sequence}. The freeing of the previous
* memory space is handled. If an error happened, {*sequence} remains untouched.
+ * Semaphore:
+ * Takes then releases access for {k}.
* Returns:
* 0 on success.
* -1 iff nothing fitting was found.
@@ -352,7 +376,8 @@ static int extend_right
const size_t sequence_size,
const ZoO_index markov_order,
const ZoO_index sequence_length,
- const struct ZoO_knowledge k [const restrict static 1]
+ const struct ZoO_knowledge k [const restrict static 1],
+ const struct ZoO_pipe io [const restrict static 1]
)
{
const ZoO_index * restrict following_words;
@@ -360,6 +385,8 @@ static int extend_right
ZoO_index following_words_weights_sum;
+ (void) ZoO_knowledge_lock_access(k, io);
+
if
(
ZoO_knowledge_find_following_words
@@ -374,6 +401,8 @@ static int extend_right
) < 0
)
{
+ (void) ZoO_knowledge_unlock_access(k, io);
+
return -1;
}
@@ -390,13 +419,18 @@ static int extend_right
following_words_weights_sum
),
sequence_size,
- sequence_length
+ sequence_length,
+ io
) < 0
)
{
+ (void) ZoO_knowledge_unlock_access(k, io);
+
return -3;
}
+ (void) ZoO_knowledge_unlock_access(k, io);
+
return 0;
}
@@ -428,7 +462,8 @@ static int complete_right_part_of_sequence
size_t sequence_size [const restrict static 1],
const ZoO_index markov_order,
ZoO_index credits [const restrict],
- const struct ZoO_knowledge k [const restrict static 1]
+ const struct ZoO_knowledge k [const restrict static 1],
+ const struct ZoO_pipe io [const restrict static 1]
)
{
ZoO_index sequence_length;
@@ -447,7 +482,8 @@ static int complete_right_part_of_sequence
*sequence_size,
markov_order,
sequence_length,
- k
+ k,
+ io
) < 0
)
{
@@ -492,6 +528,7 @@ static int complete_right_part_of_sequence
case ZoO_START_OF_SEQUENCE_ID:
ZoO_S_WARNING
(
+ io,
"END OF LINE was added at the right part of an sequence."
);
@@ -525,13 +562,15 @@ static int allocate_initial_sequence
(
ZoO_index * sequence [const restrict static 1],
size_t sequence_size [const restrict static 1],
- const ZoO_index markov_order
+ const ZoO_index markov_order,
+ const struct ZoO_pipe io [const restrict static 1]
)
{
if ((SIZE_MAX / sizeof(ZoO_index)) > ((size_t) markov_order))
{
ZoO_S_ERROR
(
+ io,
"Unable to store size of the initial sequence in a size_t variable."
"Either reduce the size of a ZoO_index or the markovian order."
);
@@ -551,6 +590,7 @@ static int allocate_initial_sequence
ZoO_S_ERROR
(
+ io,
"Unable to allocate the memory required for an new sequence."
);
@@ -581,7 +621,8 @@ static int initialize_sequence
ZoO_index sequence [const restrict static 1],
const ZoO_index initial_word,
const ZoO_index markov_order,
- const struct ZoO_knowledge k [const static 1]
+ const struct ZoO_knowledge k [const static 1],
+ const struct ZoO_pipe io [const restrict static 1]
)
{
const ZoO_index * restrict following_sequences_ref;
@@ -596,6 +637,9 @@ static int initialize_sequence
return 0;
}
+ /* TODO */
+ (void) ZoO_knowledge_lock_access(k, io);
+
if
(
ZoO_knowledge_get_following_sequences_ref
@@ -604,12 +648,16 @@ static int initialize_sequence
initial_word,
&following_sequences_ref,
&following_sequences_weights,
- &following_sequences_weights_sum
+ &following_sequences_weights_sum,
+ io
) < 0
)
{
+ (void) ZoO_knowledge_unlock_access(k, io);
+
ZoO_S_ERROR
(
+ io,
"Unable to find any sequence that would precede the initial word."
);
@@ -628,7 +676,8 @@ static int initialize_sequence
following_sequences_weights_sum
)
],
- &chosen_sequence
+ &chosen_sequence,
+ io
);
/* Safe if 'allocate_initial_sequence' succeeded. */
@@ -639,6 +688,8 @@ static int initialize_sequence
((((size_t) markov_order) - 1) * sizeof(ZoO_index))
);
+ (void) ZoO_knowledge_unlock_access(k, io);
+
return 0;
}
@@ -654,15 +705,16 @@ int ZoO_sequence_create_from
const struct ZoO_knowledge k [const restrict static 1],
const ZoO_index markov_order,
ZoO_index * sequence [const restrict static 1],
- size_t sequence_size [const restrict static 1]
+ size_t sequence_size [const restrict static 1],
+ const struct ZoO_pipe io [const restrict static 1]
)
{
- if (allocate_initial_sequence(sequence, sequence_size, markov_order) < 0)
+ if (allocate_initial_sequence(sequence, sequence_size, markov_order, io) < 0)
{
return -1;
}
- if (initialize_sequence(*sequence, initial_word, markov_order, k) < 0)
+ if (initialize_sequence(*sequence, initial_word, markov_order, k, io) < 0)
{
free((void *) *sequence);
*sequence_size = 0;
@@ -678,7 +730,8 @@ int ZoO_sequence_create_from
sequence_size,
markov_order,
credits,
- k
+ k,
+ io
) < 0
)
{
@@ -696,7 +749,8 @@ int ZoO_sequence_create_from
sequence_size,
markov_order,
credits,
- k
+ k,
+ io
) < 0
)
{
@@ -709,7 +763,7 @@ int ZoO_sequence_create_from
if ((*sequence_size / sizeof(ZoO_index)) < 3)
{
/* 2 elements, for start and stop. */
- ZoO_S_ERROR("Created sequence was empty.");
+ ZoO_S_ERROR(io, "Created sequence was empty.");
free((void *) *sequence);
*sequence_size = 0;
diff --git a/src/core/sequence_from_string.c b/src/core/sequence_from_string.c
index 51d7049..deaec86 100644
--- a/src/core/sequence_from_string.c
+++ b/src/core/sequence_from_string.c
@@ -6,7 +6,7 @@
#include "../core/char.h"
#include "../core/index.h"
-#include "../cli/cli.h"
+#include "../pipe/pipe.h"
#include "../knowledge/knowledge.h"
@@ -16,7 +16,8 @@ static int add_word_id_to_sequence
(
const ZoO_index word_id,
ZoO_index * sequence [const restrict static 1],
- ZoO_index sequence_length [const restrict static 1]
+ ZoO_index sequence_length [const restrict static 1],
+ const struct ZoO_pipe io [const restrict static 1]
)
{
ZoO_index * new_sequence;
@@ -32,7 +33,7 @@ static int add_word_id_to_sequence
if (new_sequence == (ZoO_index *) NULL)
{
- ZoO_S_ERROR("Unable to reallocate a sequence to add word ids to it.");
+ ZoO_S_ERROR(io, "Unable to reallocate a sequence to add word ids to it.");
return -1;
}
@@ -43,13 +44,19 @@ static int add_word_id_to_sequence
/******************************************************************************/
/** HANDLING PUNCTUATION ******************************************************/
/******************************************************************************/
+
+/*
+ * Semaphore:
+ * Takes then releases access for {k}.
+ */
static int add_punctuation_to_sequence
(
const ZoO_char string [const restrict static 1],
const ZoO_char punctuation,
ZoO_index * sequence [const restrict static 1],
ZoO_index sequence_length [const restrict static 1],
- const struct ZoO_knowledge k [const restrict static 1]
+ const struct ZoO_knowledge k [const restrict static 1],
+ const struct ZoO_pipe io [const restrict static 1]
)
{
ZoO_index word_id;
@@ -58,10 +65,15 @@ static int add_punctuation_to_sequence
as_word[0] = punctuation;
as_word[1] = '\0';
- if (ZoO_knowledge_find_word_id(k, as_word, 2, &word_id) < 0)
+ (void) ZoO_knowledge_lock_access(k, io);
+
+ if (ZoO_knowledge_find_word_id(k, as_word, 2, &word_id, io) < 0)
{
+ (void) ZoO_knowledge_unlock_access(k, io);
+
ZoO_PROG_ERROR
(
+ io,
"'%s' was defined as a punctuation, was found in a string, yet is not"
" defined in the knowledge database.",
as_word
@@ -70,7 +82,9 @@ static int add_punctuation_to_sequence
return -1;
}
- if (add_word_id_to_sequence(word_id, sequence, sequence_length) < 0)
+ (void) ZoO_knowledge_unlock_access(k, io);
+
+ if (add_word_id_to_sequence(word_id, sequence, sequence_length, io) < 0)
{
return -1;
}
@@ -91,6 +105,11 @@ static int word_is_punctuation_terminated
/******************************************************************************/
/** HANDLING WORDS ************************************************************/
/******************************************************************************/
+
+/*
+ * Semaphore:
+ * Takes then releases access for {k}.
+ */
static int add_word_to_sequence
(
const ZoO_char string [const restrict static 1],
@@ -98,7 +117,8 @@ static int add_word_to_sequence
const ZoO_index word_length,
ZoO_index * sequence [const restrict static 1],
ZoO_index sequence_length [const restrict static 1],
- struct ZoO_knowledge k [const restrict static 1]
+ struct ZoO_knowledge k [const restrict static 1],
+ const struct ZoO_pipe io [const restrict static 1]
)
{
ZoO_index word_id;
@@ -109,6 +129,8 @@ static int add_word_to_sequence
return 0;
}
+ (void) ZoO_knowledge_lock_access(k, io);
+
if
(
ZoO_knowledge_learn_word
@@ -116,14 +138,19 @@ static int add_word_to_sequence
k,
(string + word_start),
word_length,
- &word_id
+ &word_id,
+ io
) < 0
)
{
+ (void) ZoO_knowledge_unlock_access(k, io);
+
return -1;
}
- if (add_word_id_to_sequence(word_id, sequence, sequence_length) < 0)
+ (void) ZoO_knowledge_unlock_access(k, io);
+
+ if (add_word_id_to_sequence(word_id, sequence, sequence_length, io) < 0)
{
return -1;
}
@@ -138,7 +165,8 @@ static int add_finding_to_sequence
const ZoO_index word_length,
ZoO_index * sequence [const restrict static 1],
ZoO_index sequence_length [const restrict static 1],
- struct ZoO_knowledge k [const restrict static 1]
+ struct ZoO_knowledge k [const restrict static 1],
+ const struct ZoO_pipe io [const restrict static 1]
)
{
ZoO_index punctuation;
@@ -161,7 +189,8 @@ static int add_finding_to_sequence
(word_length - punctuation),
sequence,
sequence_length,
- k
+ k,
+ io
) < 0
)
{
@@ -179,7 +208,8 @@ static int add_finding_to_sequence
string[word_start + word_length - 1],
sequence,
sequence_length,
- k
+ k,
+ io
) < 0
)
)
@@ -232,14 +262,17 @@ static int find_word
/******************************************************************************/
/** EXPORTED ******************************************************************/
-/******************************************************************************/
+/******************************************************************************/a
+
+/* See: "sequence.h" */
int ZoO_sequence_from_undercase_string
(
const ZoO_char string [const restrict],
const ZoO_index string_length,
struct ZoO_knowledge k [const restrict static 1],
ZoO_index * sequence [const restrict static 1],
- ZoO_index sequence_length [const restrict static 1]
+ ZoO_index sequence_length [const restrict static 1],
+ const struct ZoO_pipe io [const restrict static 1]
)
{
ZoO_index word_start, word_length;
@@ -256,7 +289,8 @@ int ZoO_sequence_from_undercase_string
(
ZoO_START_OF_SEQUENCE_ID,
sequence,
- sequence_length
+ sequence_length,
+ io
) < 0
)
{
@@ -279,7 +313,8 @@ int ZoO_sequence_from_undercase_string
word_length,
sequence,
sequence_length,
- k
+ k,
+ io
) < 0
)
{
@@ -299,7 +334,8 @@ int ZoO_sequence_from_undercase_string
(
ZoO_END_OF_SEQUENCE_ID,
sequence,
- sequence_length
+ sequence_length,
+ io
) < 0
)
{
diff --git a/src/core/sequence_to_string.c b/src/core/sequence_to_string.c
new file mode 100644
index 0000000..16fc859
--- /dev/null
+++ b/src/core/sequence_to_string.c
@@ -0,0 +1,97 @@
+#define _POSIX_C_SOURCE 200809L
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h> /* defines SIZE_MAX */
+
+#include "../core/char.h"
+#include "../core/index.h"
+
+#include "../cli/cli.h"
+
+#include "../knowledge/knowledge.h"
+
+#include "sequence.h"
+
+/* TODO */
+static int add_word
+(
+ const ZoO_index word_id,
+ const size_t destination_size,
+ struct ZoO_knowledge k [const restrict static 1],
+ ZoO_char destination [const restrict static max_destination_length],
+ size_t destination_used_size [const restrict static 1],
+ const struct ZoO_pipe io [const restrict static 1]
+)
+{
+ const ZoO_char * word;
+ size_t word_size;
+
+ (void) ZoO_knowledge_lock_access(k, io);
+ ZoO_knowledge_get_word(k, word_id, &word, &word_size, io);
+ (void) ZoO_knowledge_unlock_access(k, io);
+
+ if ((destination_used_size + word_size) > max_destination_length)
+ {
+ }
+
+ if
+ (
+ (word_size == 2)
+ && ZoO_char_is_punctuation(word[0])
+ )
+ {
+ snprintf
+ (
+ (destination + *destination_used_size),
+ word_size,
+ ZoO_CHAR_STRING_SYMBOL,
+ current_sentence
+ );
+ }
+ else
+ {
+ }
+
+ return 0;
+}
+/******************************************************************************/
+/** EXPORTED ******************************************************************/
+/******************************************************************************/
+
+int ZoO_sequence_to_undercase_string
+(
+ const ZoO_index sequence [const restrict static 1],
+ const ZoO_index sequence_length,
+ const ZoO_index max_destination_length,
+ struct ZoO_knowledge k [const restrict static 1],
+ ZoO_char destination [const restrict static max_destination_length],
+ ZoO_index destination_length [const restrict static 1],
+ const struct ZoO_pipe io [const restrict static 1]
+)
+{
+ ZoO_index i;
+ const ZoO_index actual_length = (sequence_length - 1);
+
+ for (i = 0; i < actual_length; ++i)
+ {
+ if
+ (
+ add_word
+ (
+ sequence[i],
+ max_destination_length,
+ k,
+ destination,
+ destination_length,
+ io
+ ) < 0
+ )
+ {
+ *destination_length = 0;
+
+ return -1;
+ }
+ }
+
+ return 0;
+}