diff --git a/GUI.html b/GUI.html new file mode 100644 index 00000000..392fb19c --- /dev/null +++ b/GUI.html @@ -0,0 +1,59 @@ + + + + OpenNARS for Applications: Shell + + + + + + + + + + diff --git a/Shell.html b/Shell.html new file mode 100644 index 00000000..cb07d3b2 --- /dev/null +++ b/Shell.html @@ -0,0 +1,52 @@ + + + + OpenNARS for Applications + + + + + + + diff --git a/build.cmd b/build.cmd new file mode 100644 index 00000000..60dfa287 --- /dev/null +++ b/build.cmd @@ -0,0 +1,57 @@ +@echo off +setlocal enabledelayedexpansion + +:: delete NAR and src\RuleTable.c +del .\NAR.exe +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 diff --git a/build.sh b/build.sh index 6e87ef99..1fa0b7ce 100755 --- a/build.sh +++ b/build.sh @@ -2,13 +2,13 @@ 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="-flto -g -pthread -lpthread -D_POSIX_C_SOURCE=199506L -std=c++17 -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 -Wno-strict-prototypes -Wno-c++20-compat -Wno-missing-braces" -g++ $@ -DSTAGE=1 -Wall -Wextra -Wformat-security $NoWarn $BaseFlags +BaseFlags="-flto -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 -Wno-strict-prototypes" +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..569800e8 --- /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 -Wl,--stack,4194304 -DSTAGE=2 %Str% src/RuleTable.c -oNAR.exe +echo "Done." diff --git a/build_browser.sh b/build_browser.sh new file mode 100644 index 00000000..cdd8284f --- /dev/null +++ b/build_browser.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 -Wno-strict-prototypes" +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 $@ -sEXPORTED_FUNCTIONS=_malloc,_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." diff --git a/build_cpp.sh b/build_cpp.sh new file mode 100755 index 00000000..6e87ef99 --- /dev/null +++ b/build_cpp.sh @@ -0,0 +1,14 @@ +#!/bin/sh +rm NAR +rm src/RuleTable.c +set -e +Str=`ls src/*.c src/NetworkNAR/*.c | xargs` +echo $Str +echo "Compilation started:" +BaseFlags="-flto -g -pthread -lpthread -D_POSIX_C_SOURCE=199506L -std=c++17 -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 -Wno-strict-prototypes -Wno-c++20-compat -Wno-missing-braces" +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/build_ipad.sh b/build_ipad.sh new file mode 100755 index 00000000..b76b6c29 --- /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 -Wno-strict-prototypes" +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." diff --git a/src/Cycle.c b/src/Cycle.c index b64180a7..31ae01d1 100755 --- a/src/Cycle.c +++ b/src/Cycle.c @@ -1101,7 +1101,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 ad7b8475..b4587bf9 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/Globals.c b/src/Globals.c index 4babcd93..a5597971 100755 --- a/src/Globals.c +++ b/src/Globals.c @@ -67,3 +67,8 @@ void mysrand(unsigned int seed) { next = seed; } + +double mylog2(double n) +{ + return log(n) / log(2); +} diff --git a/src/Globals.h b/src/Globals.h index 4ec568e8..7e0b2ae6 100755 --- a/src/Globals.h +++ b/src/Globals.h @@ -34,7 +34,8 @@ ////////////// #include #include -#include +#include +#include //Macros// ////////// @@ -73,5 +74,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/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 e582605a..4908aba0 100755 --- a/src/main.c +++ b/src/main.c @@ -23,7 +23,6 @@ */ #include -#include #include #include #include @@ -31,7 +30,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[]) { @@ -101,19 +99,6 @@ void Process_Args(int argc, char *argv[]) NAR_Bandrobot(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/Bandrobot_Test.h b/src/system_tests/Bandrobot_Test.h index a15cafd3..f39951d0 100644 --- a/src/system_tests/Bandrobot_Test.h +++ b/src/system_tests/Bandrobot_Test.h @@ -22,6 +22,7 @@ * THE SOFTWARE. */ +#include "./../Shell.h" bool NAR_Bandrobot_Left_executed = false; double NAR_Bandrobot_amount; diff --git a/src/system_tests/UDPNAR_Test.h b/src/system_tests/UDPNAR_Test.h deleted file mode 100755 index a07bfa34..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 successful"); -} diff --git a/src/system_tests/system_tests.h b/src/system_tests/system_tests.h index 4476f3c8..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" @@ -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" #include "Bandrobot_Test.h" void Run_System_Tests() @@ -75,5 +74,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 e58f1207..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 successful"); -} diff --git a/src/unit_tests/unit_tests.h b/src/unit_tests/unit_tests.h index 9cecc14c..8f563ee5 100755 --- a/src/unit_tests/unit_tests.h +++ b/src/unit_tests/unit_tests.h @@ -32,7 +32,6 @@ #include "Stack_Test.h" #include "Table_Test.h" #include "HashTable_Test.h" -#include "UDP_Test.h" void Run_Unit_Tests() { @@ -46,5 +45,4 @@ void Run_Unit_Tests() RuleTable_Test(); Stack_Test(); HashTable_Test(); - UDP_Test(); }