-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathPersistentmemorylogicloop.c
130 lines (111 loc) · 4.13 KB
/
Persistentmemorylogicloop.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#ifndef PMLL_H
#define PMLL_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h> // For sleep function (POSIX). Use <windows.h> for Windows.
#include <pthread.h> // Optional: For multi-threading
#define STATE_FILE "pmll_state.dat" // Persistent storage file
#define LOG_FILE "pmll_log.txt" // Log file for debugging
// Define a structure to represent the persistent loop state
typedef struct {
int iteration_count; // Number of iterations executed
double last_value; // Example persistent variable
char last_input[256]; // Stores last processed input
} PMLLState;
// Function prototypes
void initialize_state(PMLLState *state);
void save_state(const PMLLState *state);
bool load_state(PMLLState *state);
void logic_loop(PMLLState *state);
void log_event(const char *message);
bool NovelTopic(const char *input, PMLLState *state);
void NovelInput(const char *input, PMLLState *state);
#endif // PMLL_H
#include "PMLL.h"
// Initialize state, loading from file if available; otherwise, set defaults
void initialize_state(PMLLState *state) {
if (!load_state(state)) {
state->iteration_count = 0;
state->last_value = 1.0; // Default starting value
strcpy(state->last_input, ""); // Initialize empty input memory
log_event("Initialized new state.");
} else {
char log_msg[256];
snprintf(log_msg, sizeof(log_msg),
"Resumed state: Iteration %d, Last Value: %f, Last Input: %s",
state->iteration_count, state->last_value, state->last_input);
log_event(log_msg);
}
}
// Save state to a persistent file
void save_state(const PMLLState *state) {
FILE *file = fopen(STATE_FILE, "wb");
if (!file) {
perror("Error saving state");
log_event("ERROR: Unable to save state.");
return;
}
fwrite(state, sizeof(PMLLState), 1, file);
fclose(file);
log_event("State saved successfully.");
}
// Load state from the persistent file
bool load_state(PMLLState *state) {
FILE *file = fopen(STATE_FILE, "rb");
if (!file) {
log_event("No previous state found, starting fresh.");
return false;
}
fread(state, sizeof(PMLLState), 1, file);
fclose(file);
return true;
}
// Log system events to a file
void log_event(const char *message) {
FILE *log_file = fopen(LOG_FILE, "a");
if (log_file) {
fprintf(log_file, "%s\n", message);
fclose(log_file);
}
}
// Detect if a novel topic is introduced based on changes in input context
bool NovelTopic(const char *input, PMLLState *state) {
return strcmp(input, state->last_input) != 0;
}
// Process a novel input, updating the persistent state
void NovelInput(const char *input, PMLLState *state) {
if (NovelTopic(input, state)) {
strncpy(state->last_input, input, sizeof(state->last_input) - 1);
state->last_input[sizeof(state->last_input) - 1] = '\0'; // Null-terminate
log_event("Detected Novel Input");
save_state(state);
}
}
// The persistent memory logic loop (ensures state is retained indefinitely)
void logic_loop(PMLLState *state) {
char input[256];
while (1) { // Infinite loop ensures persistence
printf("Enter new input (or type 'exit' to quit): ");
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = '\0'; // Remove newline
if (strcmp(input, "exit") == 0) {
printf("Exiting loop...\n");
log_event("User exited loop.");
save_state(state); // Save final state before exiting
break;
}
NovelInput(input, state); // Process novel input
state->iteration_count++;
state->last_value *= 1.1; // Example computation
char log_msg[256];
snprintf(log_msg, sizeof(log_msg),
"Iteration: %d, Last Value: %f, Last Input: %s",
state->iteration_count, state->last_value, state->last_input);
log_event(log_msg);
printf("%s\n", log_msg); // Console output
save_state(state); // Persist state every iteration
sleep(1); // Simulated processing delay
}
}