From ecd9daf87f54e3ec03579acfa926e1810d4ac408 Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Fri, 9 Sep 2022 13:06:05 +0200 Subject: [PATCH 01/31] Update: removal of network-related code to not depend on POSIX networking --- build.sh | 4 +- src/Cycle.c | 1 - src/Cycle.h | 1 - src/NetworkNAR/Metric.c | 53 --------------- src/NetworkNAR/Metric.h | 49 -------------- src/NetworkNAR/UDP.c | 59 ----------------- src/NetworkNAR/UDP.h | 55 ---------------- src/NetworkNAR/UDPNAR.c | 110 -------------------------------- src/NetworkNAR/UDPNAR.h | 49 -------------- src/main.c | 14 ---- src/system_tests/UDPNAR_Test.h | 54 ---------------- src/system_tests/system_tests.h | 2 - src/unit_tests/UDP_Test.h | 52 --------------- src/unit_tests/unit_tests.h | 2 - 14 files changed, 2 insertions(+), 503 deletions(-) delete mode 100755 src/NetworkNAR/Metric.c delete mode 100755 src/NetworkNAR/Metric.h delete mode 100755 src/NetworkNAR/UDP.c delete mode 100755 src/NetworkNAR/UDP.h delete mode 100755 src/NetworkNAR/UDPNAR.c delete mode 100755 src/NetworkNAR/UDPNAR.h delete mode 100755 src/system_tests/UDPNAR_Test.h delete mode 100755 src/unit_tests/UDP_Test.h diff --git a/build.sh b/build.sh index c91e9190..e379733c 100755 --- a/build.sh +++ b/build.sh @@ -2,10 +2,10 @@ rm NAR rm src/RuleTable.c set -e -Str=`ls src/*.c src/NetworkNAR/*.c | xargs` +Str=`ls src/*.c | xargs` echo $Str echo "Compilation started:" -BaseFlags="-g -pthread -lpthread -D_POSIX_C_SOURCE=199506L -std=c++20 -g3 -O3 $Str -lm -oNAR" +BaseFlags="-g -D_POSIX_C_SOURCE=199506L -std=c++20 -g3 -O3 $Str -lm -oNAR" NoWarn="-Wno-unknown-pragmas -Wno-tautological-compare -Wno-dollar-in-identifier-extension -Wno-unused-parameter -Wno-unused-variable -Wno-write-strings -Wno-missing-field-initializers -Wno-narrowing" g++ $@ -DSTAGE=1 -Wall -Wextra -Wformat-security $NoWarn $BaseFlags echo "First stage done, generating RuleTable.c now, and finishing compilation." diff --git a/src/Cycle.c b/src/Cycle.c index 47c437f8..37ea3d71 100755 --- a/src/Cycle.c +++ b/src/Cycle.c @@ -695,7 +695,6 @@ void Cycle_RelativeForgetting(long currentTime) void Cycle_Perform(long currentTime) { - Metric_send("NARNode.Cycle", 1); //1a. Retrieve BELIEF_EVENT_SELECTIONS events from cyclings events priority queue (which includes both input and derivations) Cycle_PopEvents(selectedBeliefs, selectedBeliefsPriority, &beliefsSelectedCnt, &cycling_belief_events, BELIEF_EVENT_SELECTIONS); //2a. Process incoming belief events from FIFO, building implications utilizing input sequences diff --git a/src/Cycle.h b/src/Cycle.h index 929ae2c4..2f8cd1a8 100755 --- a/src/Cycle.h +++ b/src/Cycle.h @@ -41,7 +41,6 @@ #include "RuleTable.h" #include "Variable.h" #include "Stats.h" -#include "./NetworkNAR/Metric.h" //Methods// //-------// diff --git a/src/NetworkNAR/Metric.c b/src/NetworkNAR/Metric.c deleted file mode 100755 index 334511cd..00000000 --- a/src/NetworkNAR/Metric.c +++ /dev/null @@ -1,53 +0,0 @@ -/* - * The MIT License - * - * Copyright 2020 The OpenNARS authors. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "Metric.h" - -static int graphite_sockfd = 0; - -void Metric_send( const char* path, int value) -{ - char message[GRAPHITE_MAX_MSG_LEN] = {0}; - if(graphite_sockfd == 0) - { - graphite_sockfd = UDP_INIT_Sender(); - } - sprintf(message, "%s:%d|c", path, value); - assert(strlen(message) < GRAPHITE_MAX_MSG_LEN, "Metric Graphite msg too long!"); - UDP_SendData(graphite_sockfd, - GRAPHITE_IP_ADDRESS, - GRAPHITE_STATSD_PORT, - message, - GRAPHITE_MAX_MSG_LEN); -} diff --git a/src/NetworkNAR/Metric.h b/src/NetworkNAR/Metric.h deleted file mode 100755 index e79bf952..00000000 --- a/src/NetworkNAR/Metric.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * The MIT License - * - * Copyright 2020 The OpenNARS authors. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef H_METRIC -#define H_METRIC - -////////////// -// Metrics // -////////////// -//Support for sending metrics to Graphite server - -//References// -//----------// -#include -#include "UDP.h" - -//Parameters// -//----------// -#define GRAPHITE_IP_ADDRESS "127.00.1" -#define GRAPHITE_STATSD_PORT 8125 -#define GRAPHITE_MAX_MSG_LEN 130 - -//Methods// -//-------// -//Sends metrics to a graphite statsd server :| example: "foo:1|c" -void Metric_send(const char* path, int value); - -#endif diff --git a/src/NetworkNAR/UDP.c b/src/NetworkNAR/UDP.c deleted file mode 100755 index 70c3749d..00000000 --- a/src/NetworkNAR/UDP.c +++ /dev/null @@ -1,59 +0,0 @@ -/* - * The MIT License - * - * Copyright 2020 The OpenNARS authors. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "UDP.h" - -int UDP_INIT_Receiver(char *ip, int port) -{ - int sockfd; - struct sockaddr_in address_me = {0}; - sockfd = socket(AF_INET, SOCK_DGRAM, 0); - address_me.sin_family = AF_INET; - address_me.sin_port = htons(port); - address_me.sin_addr.s_addr = inet_addr(ip); - bind(sockfd, (struct sockaddr*)&address_me, sizeof(address_me)); - return sockfd; -} - -int UDP_INIT_Sender() -{ - return socket(PF_INET, SOCK_DGRAM, 0); -} - -void UDP_ReceiveData(int sockfd, char *buffer, int buffersize) -{ - struct sockaddr_in address_other; - socklen_t addr_size = sizeof(address_other); - recvfrom(sockfd, buffer, buffersize, 0, (struct sockaddr*)& address_other, &addr_size); - IN_DEBUG( printf("//UDP Data received: %s\n", buffer); ) -} - -void UDP_SendData(int sockfd, char *ip, int port, char *buffer, int buffersize) -{ - struct sockaddr_in address_destination = {0}; - address_destination.sin_family = AF_INET; - address_destination.sin_port = htons(port); - address_destination.sin_addr.s_addr = inet_addr(ip); - sendto(sockfd, buffer, buffersize, 0, (struct sockaddr*)&address_destination, sizeof(address_destination)); -} diff --git a/src/NetworkNAR/UDP.h b/src/NetworkNAR/UDP.h deleted file mode 100755 index f254f982..00000000 --- a/src/NetworkNAR/UDP.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * The MIT License - * - * Copyright 2020 The OpenNARS authors. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef H_UDP -#define H_UDP - -//////////////////// -// UDP networking // -//////////////////// -//Support for UDP packet transfer - -//References// -//----------// -#include -#include -#include -#include -#include -#include -#include -#include "./../Globals.h" - -//Methods// -//-------// -//Inits a UDP receive socket, returns the socketfd -int UDP_INIT_Receiver(char *ip, int port); -//Inits an UDP send socket, returns a socketfd -int UDP_INIT_Sender(); -//Receives data from socket into buffer, up to buffersize bytes -void UDP_ReceiveData(int sockfd, char *buffer, int buffersize); -//Sends buffer content to target using the socket -void UDP_SendData(int sockfd, char *ip, int port, char *buffer, int buffersize); - -#endif diff --git a/src/NetworkNAR/UDPNAR.c b/src/NetworkNAR/UDPNAR.c deleted file mode 100755 index dcaf1389..00000000 --- a/src/NetworkNAR/UDPNAR.c +++ /dev/null @@ -1,110 +0,0 @@ -/* - * The MIT License - * - * Copyright 2020 The OpenNARS authors. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "UDPNAR.h" - -volatile bool Stopped = false; -pthread_cond_t start_cond = PTHREAD_COND_INITIALIZER; -pthread_mutex_t start_mutex = PTHREAD_MUTEX_INITIALIZER; -pthread_mutex_t nar_mutex = PTHREAD_MUTEX_INITIALIZER; - -void* Reasoner_Thread_Run(void* timestep_address) -{ - pthread_mutex_lock(&start_mutex); - pthread_cond_signal(&start_cond); - pthread_mutex_unlock(&start_mutex); - long timestep = *((long*) timestep_address); - assert(timestep >= 0, "Nonsensical timestep for UDPNAR!"); - while(!Stopped) - { - pthread_mutex_lock(&nar_mutex); - NAR_Cycles(1); - pthread_mutex_unlock(&nar_mutex); - if(timestep >= 0) - { - struct timespec ts = {0, timestep}; - nanosleep(&ts, NULL); //wait another timestep - } - } - return NULL; -} - -void* Receive_Thread_Run(void *sockfd_address) -{ - pthread_mutex_lock(&start_mutex); - pthread_cond_signal(&start_cond); - pthread_mutex_unlock(&start_mutex); - int sockfd = *((int*) sockfd_address); - for(;;) - { - char buffer[NARSESE_LEN_MAX]; - UDP_ReceiveData(sockfd, buffer, NARSESE_LEN_MAX); - if(Stopped) //avoids problematic buffer states due to socket shutdown, most portable solution! - { - break; - } - pthread_mutex_lock(&nar_mutex); - int cmd = Shell_ProcessInput(buffer); - if(cmd == SHELL_RESET) //reset? - { - Shell_NARInit(); - } - pthread_mutex_unlock(&nar_mutex); - } - return NULL; -} - -pthread_t thread_reasoner, thread_receiver; -bool Started = false; -int receiver_sockfd; -void UDPNAR_Start(char *ip, int port, long timestep) -{ - assert(!Stopped, "UDPNAR was already started!"); - Shell_NARInit(); - receiver_sockfd = UDP_INIT_Receiver(ip, port); - //Create reasoner thread and wait for its creation - pthread_mutex_lock(&start_mutex); - pthread_create(&thread_reasoner, NULL, Reasoner_Thread_Run, ×tep); - pthread_cond_wait(&start_cond, &start_mutex); - pthread_mutex_unlock(&start_mutex); - //Create receive thread and wait for its creation - pthread_mutex_lock(&start_mutex); - pthread_create(&thread_receiver, NULL, Receive_Thread_Run, &receiver_sockfd); - pthread_cond_wait(&start_cond, &start_mutex); - pthread_mutex_unlock(&start_mutex); - puts("//UDPNAR started!"); - fflush(stdout); - Started = true; -} - -void UDPNAR_Stop() -{ - assert(Started, "UDPNAR not started, call UDPNAR_Start first!"); - Stopped = true; - shutdown(receiver_sockfd, SHUT_RDWR); //sufficient on Linux to get out of blocking ops on socket, insufficient on Mac - close(receiver_sockfd); //sufficient on Mac to get out of blocking ops on socket, insufficient on Linux (hence, use both!) - pthread_join(thread_reasoner, NULL); - pthread_join(thread_receiver, NULL); - Stats_Print(currentTime); -} diff --git a/src/NetworkNAR/UDPNAR.h b/src/NetworkNAR/UDPNAR.h deleted file mode 100755 index eb218dc9..00000000 --- a/src/NetworkNAR/UDPNAR.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * The MIT License - * - * Copyright 2020 The OpenNARS authors. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef H_UDPNAR -#define H_UDPNAR - -////////////// -// UDPNAR // -////////////// -//A networking NAR using UDP to receive Narsese - -//References// -//----------// -#include "UDP.h" -#include "./../Shell.h" -#include -#include -#include -#include - -//Methods// -//-------// -//Starts the UDPNAR with a reasoning speed given by timestep, example: 10000000L = 10ms -void UDPNAR_Start(char *ip, int port, long timestep); -//Stops the UDPNAR, cancelling its threads -void UDPNAR_Stop(); - -#endif diff --git a/src/main.c b/src/main.c index ba100d06..0b1e3381 100755 --- a/src/main.c +++ b/src/main.c @@ -31,7 +31,6 @@ #include "./unit_tests/unit_tests.h" #include "./system_tests/system_tests.h" #include "Shell.h" -#include "./NetworkNAR/UDPNAR.h" void Process_Args(int argc, char *argv[]) { @@ -96,19 +95,6 @@ void Process_Args(int argc, char *argv[]) NAR_Robot(iterations); } } - if(!strcmp(argv[1],"UDPNAR")) // ./NAR UDPNAR IP PORT timestep(ns per cycle) printDerivations - { - char *ip = argv[2]; - int port = atoi(argv[3]); - long timestep = atol(argv[4]); - bool printDerivations = !strcmp("true", argv[5]); - PRINT_DERIVATIONS = printDerivations; - UDPNAR_Start(ip, port, timestep); - puts("//press any key and enter to quit!"); - fflush(stdout); - getchar(); - UDPNAR_Stop(); - } } if(inspectionOnExit) { diff --git a/src/system_tests/UDPNAR_Test.h b/src/system_tests/UDPNAR_Test.h deleted file mode 100755 index 123a7058..00000000 --- a/src/system_tests/UDPNAR_Test.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * The MIT License - * - * Copyright 2020 The OpenNARS authors. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "./../NetworkNAR/UDPNAR.h" - -bool NAR_UDPNAR_Test_op_left_executed = false; -Feedback NAR_UDPNAR_Test_op_left(Term args) -{ - NAR_UDPNAR_Test_op_left_executed = true; - return (Feedback) {0}; -} - -void NAR_UDPNAR_Test() -{ - puts(">>UDPNAR test start"); - char *ip = "127.0.0.1"; - int port = 50001; - long timestep = 10000000L; //10ms - UDPNAR_Start(ip, port, timestep); - NAR_AddOperation("^left", NAR_UDPNAR_Test_op_left); - int sockfd_sender = UDP_INIT_Sender(); - char *send_data1 = "<(a &/ ^left) =/> g>."; - UDP_SendData(sockfd_sender, ip, port, send_data1, strlen(send_data1)+1); - char *send_data2 = "a. :|:"; - UDP_SendData(sockfd_sender, ip, port, send_data2, strlen(send_data2)+1); - char *send_data3 = "g! :|:"; - UDP_SendData(sockfd_sender, ip, port, send_data3, strlen(send_data3)+1); - struct timespec ts = {0, timestep}; - nanosleep(&ts, NULL); //wait another timestep - assert(NAR_UDPNAR_Test_op_left_executed, "UDPNAR operation wasn't executed!!"); - UDPNAR_Stop(); - puts(">>UDPNAR test successul"); -} diff --git a/src/system_tests/system_tests.h b/src/system_tests/system_tests.h index 44144f8b..da15b8a1 100755 --- a/src/system_tests/system_tests.h +++ b/src/system_tests/system_tests.h @@ -64,7 +64,6 @@ void DRAW_LINE(double x, double y, double angle, int len, char *canvas, char sym #include "Testchamber_Test.h" #include "Sequence_Test.h" #include "Alien_Test.h" -#include "UDPNAR_Test.h" void Run_System_Tests() { @@ -74,5 +73,4 @@ void Run_System_Tests() NAR_Multistep_Test(); NAR_Multistep2_Test(); NAR_Sequence_Test(); - NAR_UDPNAR_Test(); } diff --git a/src/unit_tests/UDP_Test.h b/src/unit_tests/UDP_Test.h deleted file mode 100755 index e83af14c..00000000 --- a/src/unit_tests/UDP_Test.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * The MIT License - * - * Copyright 2020 The OpenNARS authors. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "./../NetworkNAR/UDP.h" -#include - -void *Receiver_Test_Thread_Run(void *sockfd_receiver_address) -{ - int sockfd_receiver = *((int*) sockfd_receiver_address); - int receive_size = 1024; - char receive_data[receive_size]; - UDP_ReceiveData(sockfd_receiver, receive_data, receive_size); - assert(!strcmp(receive_data, "<(a &/ ^left) =/> g>."), "We didn't receive what we sent!"); - return NULL; -} - -void UDP_Test() -{ - puts(">>UDP test start"); - char *ip = "127.0.0.1"; - int port = 50000; - int sockfd_receiver = UDP_INIT_Receiver(ip, port); - pthread_t thread_receiver; - pthread_create(&thread_receiver, NULL, Receiver_Test_Thread_Run, &sockfd_receiver); - nanosleep((const struct timespec[]){{0, 10000000L}}, NULL); //wait for 10ms - int sockfd_sender = UDP_INIT_Sender(); - char *send_data = "<(a &/ ^left) =/> g>."; - UDP_SendData(sockfd_sender, ip, port, send_data, strlen(send_data)+1); - pthread_join(thread_receiver, NULL); - puts(">>UDP test successul"); -} diff --git a/src/unit_tests/unit_tests.h b/src/unit_tests/unit_tests.h index e9973552..5235ca9e 100755 --- a/src/unit_tests/unit_tests.h +++ b/src/unit_tests/unit_tests.h @@ -31,7 +31,6 @@ #include "Stack_Test.h" #include "Table_Test.h" #include "HashTable_Test.h" -#include "UDP_Test.h" void Run_Unit_Tests() { @@ -44,5 +43,4 @@ void Run_Unit_Tests() RuleTable_Test(); Stack_Test(); HashTable_Test(); - UDP_Test(); } From 5647fcc5b854979475ce2a919c9f48466d5d1bd9 Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Thu, 3 Nov 2022 17:23:07 +0100 Subject: [PATCH 02/31] Update: portability improvements --- build.bat | 10 ++++++++++ src/Globals.c | 6 ++++++ src/Globals.h | 4 ++++ src/system_tests/system_tests.h | 2 +- 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 build.bat diff --git a/build.bat b/build.bat new file mode 100644 index 00000000..6cbf9a52 --- /dev/null +++ b/build.bat @@ -0,0 +1,10 @@ +Str=src/Cycle.c src/Decision.c src/Event.c src/Globals.c src/HashTable.c src/Inference.c src/InvertedAtomIndex.c src/main.c src/Memory.c src/NAL.c src/NAR.c src/Narsese.c src/PriorityQueue.c src/RuleTable.c src/Shell.c src/Stack.c src/Stamp.c src/Stats.c src/Table.c src/Term.c src/Truth.c src/Usage.c src/Variable.c +echo %Str% +echo "Compilation started:" +BaseFlags="-lm -oNAR" +NoWarn="-Wno-unknown-pragmas -Wno-tautological-compare -Wno-dollar-in-identifier-extension -Wno-unused-parameter -Wno-unused-variable -Wno-write-strings -Wno-missing-field-initializers -Wno-narrowing" +tcc -DSTAGE=1 -Wall -Wextra -Wformat-security $NoWarn -oNAR +echo "First stage done, generating RuleTable.c now, and finishing compilation." +./NAR NAL_GenerateRuleTable > ./src/RuleTable.c +tcc -DSTAGE=2 %NoWarn% -oNAR src/RuleTable.c +echo "Done." diff --git a/src/Globals.c b/src/Globals.c index 4babcd93..5c5847f0 100755 --- a/src/Globals.c +++ b/src/Globals.c @@ -25,6 +25,7 @@ #include "Globals.h" #include #include +#include void Globals_assert(bool b, char* message) { @@ -67,3 +68,8 @@ void mysrand(unsigned int seed) { next = seed; } + +double mylog2(double n) +{ + return log2(n) / log2(2); +} diff --git a/src/Globals.h b/src/Globals.h index c70ff015..3e33b80d 100755 --- a/src/Globals.h +++ b/src/Globals.h @@ -69,5 +69,9 @@ void mysrand(unsigned int seed); //Stringification macro: #define STR_HELPER(x) #x #define STR(x) STR_HELPER(x) +double mylog2(double n); +#ifndef log2 +#define log2 mylog2 +#endif #endif diff --git a/src/system_tests/system_tests.h b/src/system_tests/system_tests.h index db6ed277..a0d7c976 100755 --- a/src/system_tests/system_tests.h +++ b/src/system_tests/system_tests.h @@ -50,7 +50,7 @@ void DRAW_LINE(double x, double y, double angle, int len, char *canvas, char sym } } #define CLEAR_SCREEN do{ fputs("\033[1;1H\033[2J", stdout); } while(0) -#define SLEEP do{ nanosleep((const struct timespec[]){{0, 20000000L}}, NULL); } while(0) //POSIX sleep +#define SLEEP do{ /*not implemented*/ } while(0) //sleep #include "Alphabet_Test.h" #include "Procedure_Test.h" From 0f51738065d4e5b7466626cddbf55640bc9c7b0e Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Thu, 3 Nov 2022 19:58:42 +0100 Subject: [PATCH 03/31] Update: removal of unistd include --- src/main.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main.c b/src/main.c index 4903ac59..49886f94 100755 --- a/src/main.c +++ b/src/main.c @@ -23,7 +23,6 @@ */ #include -#include #include #include #include From 1182d08c0c6dce616a6088d4d765975b92e8b0bb Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Fri, 4 Nov 2022 07:17:16 +0100 Subject: [PATCH 04/31] Update: build scripts separated --- build.bat | 10 ---------- build.sh | 8 ++++---- build_ReactOS.bat | 9 +++++++++ build_cpp.sh | 14 ++++++++++++++ src/Globals.c | 3 +-- src/Globals.h | 3 ++- 6 files changed, 30 insertions(+), 17 deletions(-) delete mode 100644 build.bat create mode 100644 build_ReactOS.bat create mode 100755 build_cpp.sh diff --git a/build.bat b/build.bat deleted file mode 100644 index 6cbf9a52..00000000 --- a/build.bat +++ /dev/null @@ -1,10 +0,0 @@ -Str=src/Cycle.c src/Decision.c src/Event.c src/Globals.c src/HashTable.c src/Inference.c src/InvertedAtomIndex.c src/main.c src/Memory.c src/NAL.c src/NAR.c src/Narsese.c src/PriorityQueue.c src/RuleTable.c src/Shell.c src/Stack.c src/Stamp.c src/Stats.c src/Table.c src/Term.c src/Truth.c src/Usage.c src/Variable.c -echo %Str% -echo "Compilation started:" -BaseFlags="-lm -oNAR" -NoWarn="-Wno-unknown-pragmas -Wno-tautological-compare -Wno-dollar-in-identifier-extension -Wno-unused-parameter -Wno-unused-variable -Wno-write-strings -Wno-missing-field-initializers -Wno-narrowing" -tcc -DSTAGE=1 -Wall -Wextra -Wformat-security $NoWarn -oNAR -echo "First stage done, generating RuleTable.c now, and finishing compilation." -./NAR NAL_GenerateRuleTable > ./src/RuleTable.c -tcc -DSTAGE=2 %NoWarn% -oNAR src/RuleTable.c -echo "Done." diff --git a/build.sh b/build.sh index e379733c..d4ee5247 100755 --- a/build.sh +++ b/build.sh @@ -5,10 +5,10 @@ set -e Str=`ls src/*.c | xargs` echo $Str echo "Compilation started:" -BaseFlags="-g -D_POSIX_C_SOURCE=199506L -std=c++20 -g3 -O3 $Str -lm -oNAR" -NoWarn="-Wno-unknown-pragmas -Wno-tautological-compare -Wno-dollar-in-identifier-extension -Wno-unused-parameter -Wno-unused-variable -Wno-write-strings -Wno-missing-field-initializers -Wno-narrowing" -g++ $@ -DSTAGE=1 -Wall -Wextra -Wformat-security $NoWarn $BaseFlags +BaseFlags="-g -D_POSIX_C_SOURCE=199506L -pedantic -std=c99 -g3 -O3 $Str -lm -oNAR" +NoWarn="-Wno-unknown-pragmas -Wno-tautological-compare -Wno-dollar-in-identifier-extension -Wno-unused-parameter -Wno-unused-variable" +gcc $@ -DSTAGE=1 -Wall -Wextra -Wformat-security $NoWarn $BaseFlags echo "First stage done, generating RuleTable.c now, and finishing compilation." ./NAR NAL_GenerateRuleTable > ./src/RuleTable.c -g++ $@ -mfpmath=sse -msse2 -DSTAGE=2 $NoWarn $BaseFlags src/RuleTable.c || (echo "Error with SSE, hence compiling without SSE:" && g++ $@ -DSTAGE=2 $NoWarn $BaseFlags src/RuleTable.c) +gcc $@ -mfpmath=sse -msse2 -DSTAGE=2 $NoWarn $BaseFlags src/RuleTable.c || (echo "Error with SSE, hence compiling without SSE:" && gcc $@ -DSTAGE=2 $NoWarn $BaseFlags src/RuleTable.c) echo "Done." diff --git a/build_ReactOS.bat b/build_ReactOS.bat new file mode 100644 index 00000000..357c4fd8 --- /dev/null +++ b/build_ReactOS.bat @@ -0,0 +1,9 @@ +set Str=src/Cycle.c src/Decision.c src/Event.c src/Globals.c src/HashTable.c src/Inference.c src/InvertedAtomIndex.c src/main.c src/Memory.c src/NAL.c src/NAR.c src/Narsese.c src/PriorityQueue.c src/Shell.c src/Stack.c src/Stamp.c src/Stats.c src/Table.c src/Term.c src/Truth.c src/Usage.c src/Variable.c +echo %Str% +echo "Compilation started:" +set NoWarn=-Wno-unknown-pragmas -Wno-tautological-compare -Wno-dollar-in-identifier-extension -Wno-unused-parameter -Wno-unused-variable -Wno-write-strings -Wno-missing-field-initializers -Wno-narrowing +C:/tcc/tcc -DSTAGE=1 -Wall -Wextra -Wformat-security %NoWarn% %Str% -oNAR.exe +echo "First stage done, generating RuleTable.c now, and finishing compilation." +NAR NAL_GenerateRuleTable > src/RuleTable.c +C:/tcc/tcc -DSTAGE=2 %Str% src/RuleTable.c -oNAR.exe +echo "Done." diff --git a/build_cpp.sh b/build_cpp.sh new file mode 100755 index 00000000..e379733c --- /dev/null +++ b/build_cpp.sh @@ -0,0 +1,14 @@ +#!/bin/sh +rm NAR +rm src/RuleTable.c +set -e +Str=`ls src/*.c | xargs` +echo $Str +echo "Compilation started:" +BaseFlags="-g -D_POSIX_C_SOURCE=199506L -std=c++20 -g3 -O3 $Str -lm -oNAR" +NoWarn="-Wno-unknown-pragmas -Wno-tautological-compare -Wno-dollar-in-identifier-extension -Wno-unused-parameter -Wno-unused-variable -Wno-write-strings -Wno-missing-field-initializers -Wno-narrowing" +g++ $@ -DSTAGE=1 -Wall -Wextra -Wformat-security $NoWarn $BaseFlags +echo "First stage done, generating RuleTable.c now, and finishing compilation." +./NAR NAL_GenerateRuleTable > ./src/RuleTable.c +g++ $@ -mfpmath=sse -msse2 -DSTAGE=2 $NoWarn $BaseFlags src/RuleTable.c || (echo "Error with SSE, hence compiling without SSE:" && g++ $@ -DSTAGE=2 $NoWarn $BaseFlags src/RuleTable.c) +echo "Done." diff --git a/src/Globals.c b/src/Globals.c index 5c5847f0..a5597971 100755 --- a/src/Globals.c +++ b/src/Globals.c @@ -25,7 +25,6 @@ #include "Globals.h" #include #include -#include void Globals_assert(bool b, char* message) { @@ -71,5 +70,5 @@ void mysrand(unsigned int seed) double mylog2(double n) { - return log2(n) / log2(2); + return log(n) / log(2); } diff --git a/src/Globals.h b/src/Globals.h index 3e33b80d..4b4825dc 100755 --- a/src/Globals.h +++ b/src/Globals.h @@ -34,7 +34,8 @@ ////////////// #include #include -#include +#include +#include //Macros// ////////// From 70d694370684ed760192cb31155674cb8df7a5ca Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Fri, 4 Nov 2022 08:00:19 +0100 Subject: [PATCH 05/31] Update: increased stack size for ReactOS build --- build_ReactOS.bat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build_ReactOS.bat b/build_ReactOS.bat index 357c4fd8..569800e8 100644 --- a/build_ReactOS.bat +++ b/build_ReactOS.bat @@ -5,5 +5,5 @@ set NoWarn=-Wno-unknown-pragmas -Wno-tautological-compare -Wno-dollar-in-identif C:/tcc/tcc -DSTAGE=1 -Wall -Wextra -Wformat-security %NoWarn% %Str% -oNAR.exe echo "First stage done, generating RuleTable.c now, and finishing compilation." NAR NAL_GenerateRuleTable > src/RuleTable.c -C:/tcc/tcc -DSTAGE=2 %Str% src/RuleTable.c -oNAR.exe +C:/tcc/tcc -Wl,--stack,4194304 -DSTAGE=2 %Str% src/RuleTable.c -oNAR.exe echo "Done." From 3e0606a30a9d990db9f94ce74c7c6f311141b1ce Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Tue, 13 Dec 2022 08:11:59 +0100 Subject: [PATCH 06/31] Update: build_ipad.sh: Ipad build script using wasm --- build_ipad.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100755 build_ipad.sh diff --git a/build_ipad.sh b/build_ipad.sh new file mode 100755 index 00000000..efe778e3 --- /dev/null +++ b/build_ipad.sh @@ -0,0 +1,17 @@ +#!/bin/sh +rm NAR +rm src/RuleTable.c +set -e +Str=`ls src/*.c | xargs` +echo $Str +echo "Compilation started:" +BaseFlags="-g -D_POSIX_C_SOURCE=199506L -pedantic -std=c99 -g3 -O3 $Str -lm -oNAR" +NoWarn="-Wno-unknown-pragmas -Wno-tautological-compare -Wno-dollar-in-identifier-extension -Wno-unused-parameter -Wno-unused-variable" +gcc $@ -DSTAGE=1 -Wall -Wextra -Wformat-security $NoWarn $BaseFlags +echo "First stage done, generating RuleTable.c now, and finishing compilation." +./NAR NAL_GenerateRuleTable > ./src/RuleTable.c +export WASI_SDK_PATH=/home/tc/wasi-sdk +CC="${WASI_SDK_PATH}/bin/clang --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot" +BaseFlags="-g -D_POSIX_C_SOURCE=199506L -pedantic -std=c99 -g3 -O3 $Str -lm -oNAR.wasm" +/home/tc/wasi-sdk/build/install/opt/bin/clang $@ -DSTAGE=2 $NoWarn $BaseFlags src/RuleTable.c +echo "Done." From d693beb7d6e80cd49c3fe11e9a93605e7908c592 Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Mon, 13 Nov 2023 00:37:06 +0100 Subject: [PATCH 07/31] Update: for browser build, always run shell --- src/main.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main.c b/src/main.c index 49886f94..b5dade81 100755 --- a/src/main.c +++ b/src/main.c @@ -139,6 +139,10 @@ int main(int argc, char *argv[]) mysrand(SEED); #else mysrand(666); +#endif +#ifdef BROWSER + Shell_Start(); + return 0; #endif Process_Args(argc, argv); if(argc == 1) From 37d32c733daebee214a4372f5efe9febed1a72bf Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Mon, 13 Nov 2023 00:37:24 +0100 Subject: [PATCH 08/31] Update: build script for browser --- build_browser.sh | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 build_browser.sh diff --git a/build_browser.sh b/build_browser.sh new file mode 100644 index 00000000..8ceb0efa --- /dev/null +++ b/build_browser.sh @@ -0,0 +1,19 @@ +#!/bin/sh +rm NAR +rm src/RuleTable.c +set -e +Str=`ls src/*.c | xargs` +echo $Str +echo "Compilation started:" +BaseFlags="-g -D_POSIX_C_SOURCE=199506L -pedantic -std=c99 -g3 -O3 $Str -lm -oNAR" +NoWarn="-Wno-unknown-pragmas -Wno-tautological-compare -Wno-dollar-in-identifier-extension -Wno-unused-parameter -Wno-unused-variable" +gcc $@ -DSTAGE=1 -Wall -Wextra -Wformat-security $NoWarn $BaseFlags +echo "First stage done, generating RuleTable.c now, and finishing compilation." +./NAR NAL_GenerateRuleTable > ./src/RuleTable.c +export WASI_SDK_PATH=/home/tc/wasi-sdk +CC="${WASI_SDK_PATH}/bin/clang --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot" +BaseFlags="-g -D_POSIX_C_SOURCE=199506L -pedantic -std=c99 -g3 -O3 $Str -lm -oNAR.html" +emcc $@ -DBROWSER -DSTAGE=2 $NoWarn $BaseFlags -s TOTAL_MEMORY=900mb src/RuleTable.c +echo "Done." + +#-sEXPORTED_FUNCTIONS=_main,_NAR_INIT,_NAR_AddInput -sEXPORTED_RUNTIME_METHODS=ccall,cwrap From cfddd29f2a864eb3bb447b7461a68f6950d0190e Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Mon, 13 Nov 2023 12:05:59 +0100 Subject: [PATCH 09/31] Update: stack size needs to be greater to not cause issues with emscripten --- build_browser.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/build_browser.sh b/build_browser.sh index 8ceb0efa..aaa20c25 100644 --- a/build_browser.sh +++ b/build_browser.sh @@ -13,7 +13,5 @@ echo "First stage done, generating RuleTable.c now, and finishing compilation." export WASI_SDK_PATH=/home/tc/wasi-sdk CC="${WASI_SDK_PATH}/bin/clang --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot" BaseFlags="-g -D_POSIX_C_SOURCE=199506L -pedantic -std=c99 -g3 -O3 $Str -lm -oNAR.html" -emcc $@ -DBROWSER -DSTAGE=2 $NoWarn $BaseFlags -s TOTAL_MEMORY=900mb src/RuleTable.c +emcc $@ -DBROWSER -sSTACK_SIZE=10485760 -DSTAGE=2 $NoWarn $BaseFlags -s TOTAL_MEMORY=900mb src/RuleTable.c echo "Done." - -#-sEXPORTED_FUNCTIONS=_main,_NAR_INIT,_NAR_AddInput -sEXPORTED_RUNTIME_METHODS=ccall,cwrap From c3b42680f60d0ca47a7d66818100085e57634ba2 Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Mon, 13 Nov 2023 13:49:12 +0100 Subject: [PATCH 10/31] Update: added Shell.html which accepts URL args --- Shell.html | 110 +++++++++++++++++++++++++++++++++++++++++++++++ build_browser.sh | 2 +- src/main.c | 4 -- 3 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 Shell.html diff --git a/Shell.html b/Shell.html new file mode 100644 index 00000000..62ead0b2 --- /dev/null +++ b/Shell.html @@ -0,0 +1,110 @@ + + + + + + Emscripten-Generated Code + + + + +
+
Downloading...
+ + +
+ +
+ +
+ +
+ + + + + + diff --git a/build_browser.sh b/build_browser.sh index aaa20c25..1e0e7336 100644 --- a/build_browser.sh +++ b/build_browser.sh @@ -13,5 +13,5 @@ echo "First stage done, generating RuleTable.c now, and finishing compilation." export WASI_SDK_PATH=/home/tc/wasi-sdk CC="${WASI_SDK_PATH}/bin/clang --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot" BaseFlags="-g -D_POSIX_C_SOURCE=199506L -pedantic -std=c99 -g3 -O3 $Str -lm -oNAR.html" -emcc $@ -DBROWSER -sSTACK_SIZE=10485760 -DSTAGE=2 $NoWarn $BaseFlags -s TOTAL_MEMORY=900mb src/RuleTable.c +emcc $@ -sSTACK_SIZE=10485760 -DSTAGE=2 $NoWarn $BaseFlags -s TOTAL_MEMORY=900mb src/RuleTable.c echo "Done." diff --git a/src/main.c b/src/main.c index b5dade81..49886f94 100755 --- a/src/main.c +++ b/src/main.c @@ -139,10 +139,6 @@ int main(int argc, char *argv[]) mysrand(SEED); #else mysrand(666); -#endif -#ifdef BROWSER - Shell_Start(); - return 0; #endif Process_Args(argc, argv); if(argc == 1) From 6021152ee4bc7071d4cfd2360aedd1f08f955496 Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Mon, 13 Nov 2023 15:48:25 +0100 Subject: [PATCH 11/31] Update: get rid of the convoluted emscripten shell, replace with saner version --- Shell.html | 134 +++++++++++------------------------------------------ 1 file changed, 28 insertions(+), 106 deletions(-) diff --git a/Shell.html b/Shell.html index 62ead0b2..ab669e5e 100644 --- a/Shell.html +++ b/Shell.html @@ -1,110 +1,32 @@ - - - - - - Emscripten-Generated Code - - - - -
-
Downloading...
- - -
- -
- -
- -
+ + + + OpenNARS for Applications: Shell + + - - - - + + From 305c8a0c54a16ed9f50c8772d697332ab8206118 Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Mon, 13 Nov 2023 16:36:56 +0100 Subject: [PATCH 12/31] Update: log to console and skip POSIX clear --- Shell.html | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Shell.html b/Shell.html index ab669e5e..502800ff 100644 --- a/Shell.html +++ b/Shell.html @@ -10,6 +10,8 @@ var element = document.getElementById('output'); function outputProcess(text) { + text = text.replace("\033[1;1H\033[2J",""); + console.log(text); element.value += text + "\n"; } //which parameter to pass to ONA From 1829670312ab09b530c3079ea2306af650d2f1fb Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Mon, 13 Nov 2023 16:57:00 +0100 Subject: [PATCH 13/31] Update: support animated examples --- Shell.html | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Shell.html b/Shell.html index 502800ff..ba16c10b 100644 --- a/Shell.html +++ b/Shell.html @@ -8,11 +8,22 @@ From de1684848b8239a8da7f00bdfd6f87d756aa2d52 Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Mon, 13 Nov 2023 17:09:14 +0100 Subject: [PATCH 14/31] Update: replace ansi escapes as suggested by ChatGPT --- Shell.html | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Shell.html b/Shell.html index ba16c10b..fff536a9 100644 --- a/Shell.html +++ b/Shell.html @@ -19,7 +19,9 @@ framearray[writeindex++] = frame; frame = ""; } - text = text.replace("\033[1;1H\033[2J",""); + text = text.replace("\033[1;1H\033[2J","") + const ansiEscape = /\x1B\[\d+(;\d+)*m/g; + text = text.replace(ansiEscape, ''); console.log(text); frame += text + "\n"; element.value += text + "\n"; From ffa60ef2700bc0d2ae7b21f1baa46b104a13d29b Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Mon, 13 Nov 2023 17:33:25 +0100 Subject: [PATCH 15/31] Update: title update: --- Shell.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Shell.html b/Shell.html index fff536a9..cb07d3b2 100644 --- a/Shell.html +++ b/Shell.html @@ -1,7 +1,7 @@ - OpenNARS for Applications: Shell + OpenNARS for Applications From e1e85786f07ad49dbe4b865d5c89c8b8e51a0302 Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Wed, 15 Nov 2023 12:20:07 +0100 Subject: [PATCH 16/31] Update: export relevant functions so they can be used by the browser --- build_browser.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build_browser.sh b/build_browser.sh index 1e0e7336..8f59a9ca 100644 --- a/build_browser.sh +++ b/build_browser.sh @@ -13,5 +13,5 @@ echo "First stage done, generating RuleTable.c now, and finishing compilation." export WASI_SDK_PATH=/home/tc/wasi-sdk CC="${WASI_SDK_PATH}/bin/clang --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot" BaseFlags="-g -D_POSIX_C_SOURCE=199506L -pedantic -std=c99 -g3 -O3 $Str -lm -oNAR.html" -emcc $@ -sSTACK_SIZE=10485760 -DSTAGE=2 $NoWarn $BaseFlags -s TOTAL_MEMORY=900mb src/RuleTable.c +emcc $@ -sEXPORTED_FUNCTIONS=_NAR_INIT,_NAR_AddInputNarsese,_free -sEXPORTED_RUNTIME_METHODS=stringToNewUTF8 -sSTACK_SIZE=10485760 -DSTAGE=2 $NoWarn $BaseFlags -s TOTAL_MEMORY=900mb src/RuleTable.c echo "Done." From a696cbbfd7c5295c0366b19cd580464516339a91 Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Wed, 15 Nov 2023 12:28:20 +0100 Subject: [PATCH 17/31] Update: export the shell init and input instead so commands are also supported --- build_browser.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build_browser.sh b/build_browser.sh index 8f59a9ca..bb5b30a8 100644 --- a/build_browser.sh +++ b/build_browser.sh @@ -13,5 +13,5 @@ echo "First stage done, generating RuleTable.c now, and finishing compilation." export WASI_SDK_PATH=/home/tc/wasi-sdk CC="${WASI_SDK_PATH}/bin/clang --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot" BaseFlags="-g -D_POSIX_C_SOURCE=199506L -pedantic -std=c99 -g3 -O3 $Str -lm -oNAR.html" -emcc $@ -sEXPORTED_FUNCTIONS=_NAR_INIT,_NAR_AddInputNarsese,_free -sEXPORTED_RUNTIME_METHODS=stringToNewUTF8 -sSTACK_SIZE=10485760 -DSTAGE=2 $NoWarn $BaseFlags -s TOTAL_MEMORY=900mb src/RuleTable.c +emcc $@ -sEXPORTED_FUNCTIONS=Shell_NARInit,Shell_ProcessInput,_free -sEXPORTED_RUNTIME_METHODS=stringToNewUTF8 -sSTACK_SIZE=10485760 -DSTAGE=2 $NoWarn $BaseFlags -s TOTAL_MEMORY=900mb src/RuleTable.c echo "Done." From ce3860c45f45b8b1b080b5cdd347652f8a206e9c Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Wed, 15 Nov 2023 12:29:22 +0100 Subject: [PATCH 18/31] Update: export the shell init and input instead so commands are also supported --- build_browser.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build_browser.sh b/build_browser.sh index bb5b30a8..fb130565 100644 --- a/build_browser.sh +++ b/build_browser.sh @@ -13,5 +13,5 @@ echo "First stage done, generating RuleTable.c now, and finishing compilation." export WASI_SDK_PATH=/home/tc/wasi-sdk CC="${WASI_SDK_PATH}/bin/clang --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot" BaseFlags="-g -D_POSIX_C_SOURCE=199506L -pedantic -std=c99 -g3 -O3 $Str -lm -oNAR.html" -emcc $@ -sEXPORTED_FUNCTIONS=Shell_NARInit,Shell_ProcessInput,_free -sEXPORTED_RUNTIME_METHODS=stringToNewUTF8 -sSTACK_SIZE=10485760 -DSTAGE=2 $NoWarn $BaseFlags -s TOTAL_MEMORY=900mb src/RuleTable.c +emcc $@ -sEXPORTED_FUNCTIONS=_Shell_NARInit,_Shell_ProcessInput,_free -sEXPORTED_RUNTIME_METHODS=stringToNewUTF8 -sSTACK_SIZE=10485760 -DSTAGE=2 $NoWarn $BaseFlags -s TOTAL_MEMORY=900mb src/RuleTable.c echo "Done." From fd80a7863886eaec5da2a935dc78518c33139b11 Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Wed, 15 Nov 2023 12:53:58 +0100 Subject: [PATCH 19/31] Update: interactive shell --- Interactive_Shell.html | 59 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Interactive_Shell.html diff --git a/Interactive_Shell.html b/Interactive_Shell.html new file mode 100644 index 00000000..a9faf244 --- /dev/null +++ b/Interactive_Shell.html @@ -0,0 +1,59 @@ + + + + OpenNARS for Applications: Shell + + + + + + + + + + + + From e5e2f3b555154f0bd225e752341c5049948ebc47 Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Wed, 15 Nov 2023 13:06:53 +0100 Subject: [PATCH 20/31] Update: also main exported --- Shell.html | 1 + build_browser.sh | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Shell.html b/Shell.html index cb07d3b2..1f66c2cb 100644 --- a/Shell.html +++ b/Shell.html @@ -44,6 +44,7 @@ var interval = setInterval(function(){ if(readindex != -1 && readindex < framearray.length){ element.value = framearray[readindex++]; + element.scrollTop = element.scrollHeight; } }, 50) diff --git a/build_browser.sh b/build_browser.sh index fb130565..010217c7 100644 --- a/build_browser.sh +++ b/build_browser.sh @@ -13,5 +13,5 @@ echo "First stage done, generating RuleTable.c now, and finishing compilation." export WASI_SDK_PATH=/home/tc/wasi-sdk CC="${WASI_SDK_PATH}/bin/clang --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot" BaseFlags="-g -D_POSIX_C_SOURCE=199506L -pedantic -std=c99 -g3 -O3 $Str -lm -oNAR.html" -emcc $@ -sEXPORTED_FUNCTIONS=_Shell_NARInit,_Shell_ProcessInput,_free -sEXPORTED_RUNTIME_METHODS=stringToNewUTF8 -sSTACK_SIZE=10485760 -DSTAGE=2 $NoWarn $BaseFlags -s TOTAL_MEMORY=900mb src/RuleTable.c +emcc $@ -sEXPORTED_FUNCTIONS=_main,_Shell_NARInit,_Shell_ProcessInput,_free -sEXPORTED_RUNTIME_METHODS=stringToNewUTF8 -sSTACK_SIZE=10485760 -DSTAGE=2 $NoWarn $BaseFlags -s TOTAL_MEMORY=900mb src/RuleTable.c echo "Done." From b174c2b76bedfe10ceafdda597c07e457f79775c Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Wed, 15 Nov 2023 13:07:52 +0100 Subject: [PATCH 21/31] Update: also main exported --- Shell.html | 1 + 1 file changed, 1 insertion(+) diff --git a/Shell.html b/Shell.html index 1f66c2cb..19036723 100644 --- a/Shell.html +++ b/Shell.html @@ -25,6 +25,7 @@ console.log(text); frame += text + "\n"; element.value += text + "\n"; + element.scrollTop = element.scrollHeight; readindex = 0; } //which parameter to pass to ONA From c1cd356f409ee6c5ba5ebd4e88e16de59fe6a5f0 Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Wed, 15 Nov 2023 13:08:57 +0100 Subject: [PATCH 22/31] Update: browser GUI added --- Interactive_Shell.html => GUI.html | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Interactive_Shell.html => GUI.html (100%) diff --git a/Interactive_Shell.html b/GUI.html similarity index 100% rename from Interactive_Shell.html rename to GUI.html From e10c6a3c19b007191fee767ec98c4d3f3f5a3820 Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Wed, 15 Nov 2023 13:19:48 +0100 Subject: [PATCH 23/31] Update: avoid scroll to top, this is expensive for the browser --- Shell.html | 2 -- 1 file changed, 2 deletions(-) diff --git a/Shell.html b/Shell.html index 19036723..cb07d3b2 100644 --- a/Shell.html +++ b/Shell.html @@ -25,7 +25,6 @@ console.log(text); frame += text + "\n"; element.value += text + "\n"; - element.scrollTop = element.scrollHeight; readindex = 0; } //which parameter to pass to ONA @@ -45,7 +44,6 @@ var interval = setInterval(function(){ if(readindex != -1 && readindex < framearray.length){ element.value = framearray[readindex++]; - element.scrollTop = element.scrollHeight; } }, 50) From c4c8a76c27197faf820c9ad5af7737d79e0f9542 Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Wed, 15 Nov 2023 13:21:26 +0100 Subject: [PATCH 24/31] Update: do not print stdout --- GUI.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GUI.html b/GUI.html index a9faf244..b6c45c96 100644 --- a/GUI.html +++ b/GUI.html @@ -30,8 +30,8 @@ var Module = { arguments: ['nothing'], - 'print': function(text) { print('stdout: ' + text) }, - 'printErr': function(text) { print('stderr: ' + text) }, + 'print': function(text) { print(text) }, + 'printErr': function(text) { print(text) }, }; From a00421bd2dbbcfb0a5c58c7318ce3dfb13307ef9 Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Wed, 15 Nov 2023 13:23:20 +0100 Subject: [PATCH 25/31] Update: only 20 textrows --- GUI.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GUI.html b/GUI.html index b6c45c96..7dde1011 100644 --- a/GUI.html +++ b/GUI.html @@ -6,7 +6,7 @@ - + - - From c1363a056bcea03eee21181f39a906ae91a544f8 Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Tue, 16 Jan 2024 21:22:21 +0100 Subject: [PATCH 28/31] Update: added the missing introduction of variables for functional equivalence --- src/Decision.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/Decision.c b/src/Decision.c index e8bc3311..f9c1a180 100755 --- a/src/Decision.c +++ b/src/Decision.c @@ -84,6 +84,20 @@ void Decision_Execute(long currentTime, Decision *decision) .truth = imp1.truth, .occurrenceTime = currentTime }; Memory_AddInputEvent(&e_imp, currentTime); + Event e_imp2 = e_imp; + bool intro_success1; + e_imp2.term = Variable_IntroduceImplicationVariables(e_imp.term, &intro_success1, true); + if(intro_success1) + { + Memory_AddInputEvent(&e_imp2, currentTime); + } + Event e_imp3 = e_imp; + bool intro_success2; + e_imp2.term = Variable_IntroduceImplicationVariables(e_imp.term, &intro_success2, false); + if(intro_success2) + { + Memory_AddInputEvent(&e_imp3, currentTime); + } } bool success2 = false; Implication imp2 = Inference_BeliefInduction(&b, &a, &success2); @@ -94,6 +108,20 @@ void Decision_Execute(long currentTime, Decision *decision) .truth = imp2.truth, .occurrenceTime = currentTime }; Memory_AddInputEvent(&e_imp, currentTime); + Event e_imp2 = e_imp; + bool intro_success1; + e_imp2.term = Variable_IntroduceImplicationVariables(e_imp.term, &intro_success1, true); + if(intro_success1) + { + Memory_AddInputEvent(&e_imp2, currentTime); + } + Event e_imp3 = e_imp; + bool intro_success2; + e_imp2.term = Variable_IntroduceImplicationVariables(e_imp.term, &intro_success2, false); + if(intro_success2) + { + Memory_AddInputEvent(&e_imp3, currentTime); + } } } } From ce79633d3bd04cec2c7f65684a23c5b828eb04d3 Mon Sep 17 00:00:00 2001 From: Patrick Hammer Date: Thu, 5 Sep 2024 14:41:26 +0200 Subject: [PATCH 29/31] Update: comparative relations from discrete spaces compatible with comparative relations from continuous measurement input --- src/Config.h | 2 +- src/Cycle.c | 49 +++++++++++++++++++++---------------------------- 2 files changed, 22 insertions(+), 29 deletions(-) diff --git a/src/Config.h b/src/Config.h index 7baa7a8a..76c6e5f3 100755 --- a/src/Config.h +++ b/src/Config.h @@ -93,7 +93,7 @@ //Whether numeric term similarity is allowed to be used (allows conditioning results on absolute value to transfer to similar values) #define NUMERIC_TERM_SIMILARITY true //Whether relations should be derived from two element sequences with attribute relations (allows Transposition / comparative relations to be formed in sensorimotor) -#define ATTRIBUTE_TERM_RELATIONS false +#define ATTRIBUTE_TERM_RELATIONS true /*---------------------------------*/ /* Temporal compounding parameters */ diff --git a/src/Cycle.c b/src/Cycle.c index 5358fb18..1538d4c4 100755 --- a/src/Cycle.c +++ b/src/Cycle.c @@ -478,10 +478,9 @@ void Cycle_ProcessBeliefEvents(long currentTime) seq.term.atoms[0] == Narsese_CopulaIndex(SEQUENCE) && seq.term.atoms[1] == Narsese_CopulaIndex(INHERITANCE) && seq.term.atoms[2] == Narsese_CopulaIndex(INHERITANCE) && seq.term.atoms[3] == Narsese_CopulaIndex(PRODUCT) && seq.term.atoms[5] == Narsese_CopulaIndex(PRODUCT) && Term_Equal(&ATTR1, &ATTR2)) { - Atom REL_EQU = Narsese_AtomicTermIndex("EQUAL"); - Atom REL_NEQU = Narsese_AtomicTermIndex("UNEQUAL"); - Atom REL_LARGER = Narsese_AtomicTermIndex("LARGER"); - Atom REL_SMALLER = Narsese_AtomicTermIndex("SMALLER"); + Atom REL_EQU = Narsese_CopulaIndex(SIMILARITY); + Atom REL_LARGER = Narsese_CopulaIndex(SEQUENCE); + bool smaller = false; Atom relation = REL_EQU; if(Narsese_hasAtomValue(seq.term.atoms[8]) && Narsese_hasAtomValue(seq.term.atoms[12])) { @@ -490,44 +489,38 @@ void Cycle_ProcessBeliefEvents(long currentTime) if(v1 > v2) { relation = REL_LARGER; + smaller = false; } if(v1 < v2) { - relation = REL_SMALLER; - } - } - else - { - Term v1 = Term_ExtractSubterm(&seq.term, 8); //could be a compound in this - Term v2 = Term_ExtractSubterm(&seq.term, 12);//case - if(!Term_Equal(&v1, &v2)) - { - relation = REL_NEQU; + relation = REL_LARGER; + smaller = true; } - } - RELATE: - { Term construct = {0}; - //<(a * b) --> (EQUAL . shape)>. + //<(a * b) --> (= shape)>. // 1 2 3 4 5 6 7 - //--> * . a b EQUAL shape + //--> * = a b shape @ // 0 1 2 3 4 5 6 construct.atoms[0] = Narsese_CopulaIndex(INHERITANCE); construct.atoms[1] = Narsese_CopulaIndex(PRODUCT); - construct.atoms[2] = Narsese_CopulaIndex(SET_ELEMT); - Term_OverrideSubterm(&construct, 3, &LOC1); - Term_OverrideSubterm(&construct, 4, &LOC2); - construct.atoms[5] = relation; + construct.atoms[2] = relation; //+ + if(smaller) + { + Term_OverrideSubterm(&construct, 3, &LOC2); + Term_OverrideSubterm(&construct, 4, &LOC1); + } + else + { + Term_OverrideSubterm(&construct, 3, &LOC1); + Term_OverrideSubterm(&construct, 4, &LOC2); + } + Term_OverrideSubterm(&construct, 5, &ATTR1); Term_OverrideSubterm(&construct, 6, &ATTR1); + construct.atoms[6] = Narsese_CopulaIndex(SET_TERMINATOR); Event seq_rel = seq; seq_rel.term = construct; Memory_AddEvent(&seq_rel, currentTime, 1.0, false, true, false, 0); //complexity penalized } - if(relation == REL_LARGER || relation == REL_SMALLER) - { - relation = REL_NEQU; - goto RELATE; - } } } if(is_op_seq && selectedBeliefsPriority[h] >= 1.0) From 015093939404f6a485066def471876ccaa0fcb39 Mon Sep 17 00:00:00 2001 From: ARCJ137442 <61109168+ARCJ137442@users.noreply.github.com> Date: Sun, 29 Sep 2024 09:55:30 +0800 Subject: [PATCH 30/31] Create: Added build. cmd for compilation on Windows Translate shell script `build.sh` into Windows, aiming to build `NAR.exe` and changing variable `NoWarn` to fit the platform environments - +`-Wno-implicit-function-declaration -Wno-pointer-to-int-cast -Wno-int-to-pointer-cast` - -`-Wno-dollar-in-identifier-extension` Environment of testing: - Windows version: Windows 11 - GCC version: gcc (x86_64-posix-seh) 12.2.0 - GCC source: MinGW - Terminal: CMD, PowerShell - Compilation target --- build.cmd | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 build.cmd diff --git a/build.cmd b/build.cmd new file mode 100644 index 00000000..b0a52e53 --- /dev/null +++ b/build.cmd @@ -0,0 +1,57 @@ +@echo off +setlocal enabledelayedexpansion + +:: delete NAR and src\RuleTable.c +del NAR +del src\RuleTable.c + +:: setup error handling +set ERRORLEVEL=0 + +:: get all .c file path +for /r "src" %%f in (*.c) do ( + set "Str=!Str! %%f" +) + +:: print all .c file path and notify user the compilation is started +echo !Str! +echo Compilation started: + +:: setup compiler parameters of gcc +set BaseFlags=-flto -g -pthread -lpthread -D_POSIX_C_SOURCE=199506L -pedantic -std=c99 -g3 -O3 !Str! -lm -oNAR + +:: mute the output of compiler warnings +set NoWarn=-Wno-unknown-pragmas -Wno-tautological-compare -Wno-unused-parameter -Wno-unused-variable -Wno-strict-prototypes -Wno-implicit-function-declaration -Wno-pointer-to-int-cast -Wno-int-to-pointer-cast + +:: 1st stage compilation +gcc %* -DSTAGE=1 -Wall -Wextra -Wformat-security %NoWarn% %BaseFlags% + +:: check if compilation was successful +if %ERRORLEVEL% neq 0 ( + echo Error during first stage compilation. + goto :eof +) + +:: notify user that 1st stage is done, and start generating RuleTable.c +echo First stage done, generating RuleTable.c now, and finishing compilation. + +:: generate RuleTable.c uses 1st stage binary +call NAR.exe NAL_GenerateRuleTable > src\RuleTable.c + +:: try second stage compilation with SSE flag +gcc %* -mfpmath=sse -msse2 -DSTAGE=2 %NoWarn% %BaseFlags% src\RuleTable.c +if %ERRORLEVEL% equ 0 ( + echo Done. +) else ( + echo Error with SSE, hence compiling without SSE: + :: if error, try compilation without SSE: + gcc %* -DSTAGE=2 %NoWarn% %BaseFlags% src\RuleTable.c + if %ERRORLEVEL% equ 0 ( + echo Done. + ) else ( + echo Compilation failed. + ) +) + +:end +endlocal \ No newline at end of file From d84caedbfec5e77003dd707394deb0a9bbc735cf Mon Sep 17 00:00:00 2001 From: ARCJ137442 <61109168+ARCJ137442@users.noreply.github.com> Date: Sun, 29 Sep 2024 10:08:50 +0800 Subject: [PATCH 31/31] Update: build.cmd: now delete `.\NAR.exe` not `.\NAR` --- build.cmd | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.cmd b/build.cmd index b0a52e53..60dfa287 100644 --- a/build.cmd +++ b/build.cmd @@ -2,8 +2,8 @@ setlocal enabledelayedexpansion :: delete NAR and src\RuleTable.c -del NAR -del src\RuleTable.c +del .\NAR.exe +del .\src\RuleTable.c :: setup error handling set ERRORLEVEL=0