diff options
author | Johannes 'josch' Schauer <josch@mister-muffin.de> | 2020-10-23 08:27:38 +0200 |
---|---|---|
committer | Johannes 'josch' Schauer <josch@mister-muffin.de> | 2020-10-23 08:27:38 +0200 |
commit | 3b91637afd9716881bec3a34c3ace369c82ef61c (patch) | |
tree | 2445bd2a7aadd1fd90bc76e5ab7491a8403d6f75 /fuzzylite | |
parent | 2b00fcb77bb6e7b769ee34a114679838e00156d1 (diff) |
New upstream version 6.0+dfsg
Diffstat (limited to 'fuzzylite')
238 files changed, 42148 insertions, 0 deletions
diff --git a/fuzzylite/CMakeLists.txt b/fuzzylite/CMakeLists.txt new file mode 100644 index 0000000..28435e2 --- /dev/null +++ b/fuzzylite/CMakeLists.txt @@ -0,0 +1,301 @@ +cmake_minimum_required(VERSION 2.8.8) + +if (APPLE) + set(CMAKE_OSX_DEPLOYMENT_TARGET 10.9) +endif() + +project(fuzzylite CXX) + + +if (APPLE) + cmake_policy(SET CMP0042 NEW) +endif() +if (MSVC) + cmake_policy(SET CMP0054 NEW) +endif() + + +###DEFINES SECTION +if(NOT CMAKE_VERBOSE_MAKEFILE) + set(CMAKE_VERBOSE_MAKEFILE false) +endif() + +if( NOT CMAKE_BUILD_TYPE ) + set( CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel." FORCE ) +endif() + +add_definitions(-DFL_BUILD_PATH="${CMAKE_SOURCE_DIR}") #used to determine FL__FILE__ + +option(FL_BUILD_SHARED "Build shared library" ON) +option(FL_BUILD_STATIC "Build static library" ON) +if(FL_BUILD_SHARED) + option(FL_BUILD_BINARY "Build fuzzylite binary" ON) +endif() + +option(FL_CPP98 "Builds utilizing C++98, i.e., passing -std=c++98" OFF) +option(FL_USE_FLOAT "Use fl::scalar as float" OFF) +option(FL_BACKTRACE "Provide backtrace information in case of errors" ON) + +option(FL_BUILD_TESTS "Builds the unit tests" ON) + +if(FL_USE_FLOAT) + add_definitions(-DFL_USE_FLOAT) +endif(FL_USE_FLOAT) + +if(FL_BACKTRACE) + add_definitions(-DFL_BACKTRACE) +endif() + +if (MSVC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19) +#C++11 not available before Visual Studio 2015 + if (NOT FL_CPP98) + set(FL_CPP98 ON) + endif() +endif() + +if(FL_CPP98) + add_definitions(-DFL_CPP98) + if(NOT MSVC) + #Set C++98 by default in Clang and others + add_definitions(-std=c++98) + endif() +else() + if(NOT MSVC) + #Set C++11 by default in Clang and others + add_definitions(-std=c++11) + endif() +endif(FL_CPP98) + +#Put all binaries in same location +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY bin) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY bin) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY bin) + +if(NOT MSVC) +#TODO: Remove -Werror before release. +#Add Unix compilation flags + set(CMAKE_CXX_FLAGS "-pedantic -Wall -Wextra -Werror ${CMAKE_CXX_FLAGS}") + + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}") + set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}") + + if(NOT APPLE) + set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--no-undefined ${CMAKE_SHARED_LINKER_FLAGS}") #To avoid undefined methods in library + endif() +endif() + +if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") +#Address fl::null errors of literal null conversion + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-non-literal-null-conversion") +endif() + + +if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + if (CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 6) + #In GNU gcc v6, the default is C++11 + if (FL_CPP98) + #set the default to C++98 + #Fix error: 'template<class> class std::auto_ptr' is deprecated with gcc-6 + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++98") + endif() + endif() + #In GNU gcc 4.7, Op::str(T, std::size_t(0)) raises a warning of type-limits + if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-type-limits") + endif() +endif() + + +set(FL_LIBS) + +if(MSVC) +#Set compilation flags in Windows + set(CMAKE_CXX_FLAGS "/W4 /EHsc") + #Wx: Treat warnings as errors. W4: All warnings + #http://msdn.microsoft.com/en-us/library/thxezb7y.aspx + #EHsc: call destructors on __try __catch, and to ignore C4530: C++ exception handler used. Note, unwind semantics are not enabled + #Add Backtrace library + if (FL_BACKTRACE) + set(FL_LIBS dbghelp) + endif() +endif() + + +if(APPLE) +#Fix ld: symbol(s) not found for architecture x86_64 on mac + set(FL_LIBS stdc++) +endif() + + +###BUILD SECTION + +include_directories(.) + +file(STRINGS FL_HEADERS fl-headers) +file(STRINGS FL_SOURCES fl-sources) +file(STRINGS FL_TESTS fl-tests) + +string(REGEX REPLACE "\n" " " ${fl-headers} ${fl-headers}) +string(REGEX REPLACE "\n" " " ${fl-sources} ${fl-sources}) +string(REGEX REPLACE "\n" " " ${fl-tests} ${fl-tests}) + +message("${exepath}") + + +set(CMAKE_DEBUG_POSTFIX debug) + +if (MSVC OR CMAKE_GENERATOR STREQUAL Xcode) + if(FL_BUILD_SHARED) + add_library(fl-shared SHARED ${fl-headers} ${fl-sources}) + endif() + + if(FL_BUILD_STATIC) + add_library(fl-static STATIC ${fl-headers} ${fl-sources}) + endif() +else() + if(FL_BUILD_SHARED OR FL_BUILD_STATIC) + add_library(fl-obj OBJECT ${fl-headers} ${fl-sources}) + if(NOT MINGW) + set_target_properties(fl-obj PROPERTIES COMPILE_FLAGS "-fPIC") + endif() + endif() + + if(FL_BUILD_SHARED) + add_library(fl-shared SHARED $<TARGET_OBJECTS:fl-obj>) + endif(FL_BUILD_SHARED) + + if(FL_BUILD_STATIC) + add_library(fl-static STATIC $<TARGET_OBJECTS:fl-obj>) + endif(FL_BUILD_STATIC) +endif() + +if(FL_BUILD_SHARED) + set_target_properties(fl-shared PROPERTIES OUTPUT_NAME fuzzylite) + set_target_properties(fl-shared PROPERTIES DEBUG_POSTFIX -debug) + target_compile_definitions(fl-shared PRIVATE FL_EXPORT_LIBRARY) + set_target_properties(fl-shared PROPERTIES VERSION 6.0) + target_link_libraries(fl-shared ${FL_LIBS}) +endif() + +if(FL_BUILD_STATIC) + set_target_properties(fl-static PROPERTIES OUTPUT_NAME fuzzylite-static) + set_target_properties(fl-static PROPERTIES DEBUG_POSTFIX -debug) + set_target_properties(fl-static PROPERTIES VERSION 6.0) + target_link_libraries(fl-static ${FL_LIBS}) +endif() + +if(FL_BUILD_BINARY) + add_executable(fl-bin src/main.cpp) + set_target_properties(fl-bin PROPERTIES OUTPUT_NAME fuzzylite) + set_target_properties(fl-bin PROPERTIES OUTPUT_NAME fuzzylite IMPORT_PREFIX tmp-) #To prevent LNK1149 in Windows + set_target_properties(fl-bin PROPERTIES DEBUG_POSTFIX -debug) + target_compile_definitions(fl-bin PRIVATE FL_IMPORT_LIBRARY) #if building with fl-shared + target_link_libraries(fl-bin fl-shared ${FL_LIBS}) +endif(FL_BUILD_BINARY) + +if(FL_BUILD_TESTS) + add_executable(fl-test ${fl-headers} ${fl-tests}) + set_target_properties(fl-test PROPERTIES OUTPUT_NAME fuzzylite-tests) + set_target_properties(fl-test PROPERTIES OUTPUT_NAME fuzzylite-tests IMPORT_PREFIX tmp-) #To prevent LNK1149 in Windows + set_target_properties(fl-test PROPERTIES DEBUG_POSTFIX -debug) + + target_compile_definitions(fl-test PRIVATE FL_IMPORT_LIBRARY) + if (FL_CPP98) + target_compile_definitions(fl-test PRIVATE CATCH_CONFIG_NO_CPP11) + endif() + + if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + #Ignore QuickTest macro expansion comparison: CHECK(xstr(4+10) == "4+10") + target_compile_options(fl-test PRIVATE -Wno-address) + endif() + if (MSVC) + target_compile_options(fl-test PRIVATE /wd4130) + endif() + + target_link_libraries(fl-test fl-shared ${FL_LIBS}) + + enable_testing() + add_test(NAME RunTests COMMAND fl-test) +# add_test(NAME ListTests COMMAND fl-test --list-tests) +# set_tests_properties(ListTests PROPERTIES PASS_REGULAR_EXPRESSION "[0-9]+ test case") +# add_test(NAME ListTags COMMAND fl-test --list-tags) +# set_tests_properties(ListTags PROPERTIES PASS_REGULAR_EXPRESSION "[0-9]+ tag") +endif() + +###INSTALL SECTION +if(NOT FL_INSTALL_BINDIR) + set(FL_INSTALL_BINDIR bin) +endif() + +if(NOT FL_INSTALL_LIBDIR) + if(NOT CMAKE_INSTALL_LIBDIR) + set(FL_INSTALL_LIBDIR lib) + else() + set(FL_INSTALL_LIBDIR ${CMAKE_INSTALL_LIBDIR}) + endif() +endif() + +if(FL_BUILD_BINARY) + install(TARGETS fl-bin + RUNTIME DESTINATION ${FL_INSTALL_BINDIR} + LIBRARY DESTINATION ${FL_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${FL_INSTALL_LIBDIR} + ) +endif() + +if(FL_BUILD_SHARED) + install(TARGETS fl-shared + RUNTIME DESTINATION ${FL_INSTALL_BINDIR} + LIBRARY DESTINATION ${FL_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${FL_INSTALL_LIBDIR} + ) +endif() + +if(FL_BUILD_STATIC) + install(TARGETS fl-static + RUNTIME DESTINATION ${FL_INSTALL_BINDIR} + LIBRARY DESTINATION ${FL_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${FL_INSTALL_LIBDIR} + ) +endif() + +install(DIRECTORY fl/ DESTINATION include/fl) + +#pkg-config +configure_file(${CMAKE_SOURCE_DIR}/fuzzylite.pc.in ${CMAKE_BINARY_DIR}/fuzzylite.pc @ONLY) +install(FILES ${CMAKE_BINARY_DIR}/fuzzylite.pc DESTINATION ${FL_INSTALL_LIBDIR}/pkgconfig) + +message("=====================================") +message("fuzzylite v6.0\n") +message("FL_CPP98=${FL_CPP98}") +message("FL_USE_FLOAT=${FL_USE_FLOAT}") +message("FL_BACKTRACE=${FL_BACKTRACE}") +message("FL_LIBS=${FL_LIBS}") +message("FL_INSTALL_BINDIR=${FL_INSTALL_BINDIR}") +message("FL_INSTALL_LIBDIR=${FL_INSTALL_LIBDIR}") +message("FL_BUILD_TESTS=${FL_BUILD_TESTS}") +message("") +message("CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}") +message("CMAKE_CXX_COMPILER_ID=${CMAKE_CXX_COMPILER_ID}") +message("CMAKE_CXX_COMPILER_VERSION=${CMAKE_CXX_COMPILER_VERSION}") +message("CMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}") +message("CMAKE_OSX_DEPLOYMENT_TARGET=${CMAKE_OSX_DEPLOYMENT_TARGET}") +message("COMPILE_DEFINITIONS:") +get_directory_property(fl-definitions DIRECTORY ${CMAKE_SOURCE_DIR} COMPILE_DEFINITIONS ) +foreach(d ${fl-definitions}) + message( STATUS "Defined: " ${d} ) +endforeach() + +message("=====================================\n") + +###UNINSTALL SECTION +#configure_file( + #"${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in" + #"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" + #IMMEDIATE @ONLY) + +#add_custom_target(uninstall + #COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/uninstall.cmake) + +#unix uninstall +#xargs rm < install_manifest.txt diff --git a/fuzzylite/FL_HEADERS b/fuzzylite/FL_HEADERS new file mode 100644 index 0000000..b852ad8 --- /dev/null +++ b/fuzzylite/FL_HEADERS @@ -0,0 +1,111 @@ +fl/Benchmark.h +fl/Complexity.h +fl/Console.h +fl/activation/Activation.h +fl/activation/First.h +fl/activation/General.h +fl/activation/Highest.h +fl/activation/Last.h +fl/activation/Lowest.h +fl/activation/Proportional.h +fl/activation/Threshold.h +fl/defuzzifier/Bisector.h +fl/defuzzifier/Centroid.h +fl/defuzzifier/Defuzzifier.h +fl/defuzzifier/IntegralDefuzzifier.h +fl/defuzzifier/LargestOfMaximum.h +fl/defuzzifier/MeanOfMaximum.h +fl/defuzzifier/SmallestOfMaximum.h +fl/defuzzifier/WeightedAverage.h +fl/defuzzifier/WeightedAverageCustom.h +fl/defuzzifier/WeightedDefuzzifier.h +fl/defuzzifier/WeightedSum.h +fl/defuzzifier/WeightedSumCustom.h +fl/Engine.h +fl/Exception.h +fl/factory/ActivationFactory.h +fl/factory/CloningFactory.h +fl/factory/ConstructionFactory.h +fl/factory/DefuzzifierFactory.h +fl/factory/FactoryManager.h +fl/factory/FunctionFactory.h +fl/factory/HedgeFactory.h +fl/factory/SNormFactory.h +fl/factory/TermFactory.h +fl/factory/TNormFactory.h +fl/fuzzylite.h +fl/Headers.h +fl/hedge/Any.h +fl/hedge/Extremely.h +fl/hedge/Hedge.h +fl/hedge/HedgeFunction.h +fl/hedge/Not.h +fl/hedge/Seldom.h +fl/hedge/Somewhat.h +fl/hedge/Very.h +fl/imex/CppExporter.h +fl/imex/Exporter.h +fl/imex/FclExporter.h +fl/imex/FclImporter.h +fl/imex/FisExporter.h +fl/imex/FisImporter.h +fl/imex/FldExporter.h +fl/imex/FllExporter.h +fl/imex/FllImporter.h +fl/imex/Importer.h +fl/imex/JavaExporter.h +fl/imex/RScriptExporter.h +fl/norm/Norm.h +fl/norm/s/AlgebraicSum.h +fl/norm/s/BoundedSum.h +fl/norm/s/DrasticSum.h +fl/norm/s/EinsteinSum.h +fl/norm/s/HamacherSum.h +fl/norm/s/Maximum.h +fl/norm/s/NilpotentMaximum.h +fl/norm/s/NormalizedSum.h +fl/norm/s/SNormFunction.h +fl/norm/s/UnboundedSum.h +fl/norm/SNorm.h +fl/norm/t/AlgebraicProduct.h +fl/norm/t/BoundedDifference.h +fl/norm/t/DrasticProduct.h +fl/norm/t/EinsteinProduct.h +fl/norm/t/HamacherProduct.h +fl/norm/t/Minimum.h +fl/norm/t/NilpotentMinimum.h +fl/norm/t/TNormFunction.h +fl/norm/TNorm.h +fl/Operation.h +fl/rule/Antecedent.h +fl/rule/Consequent.h +fl/rule/Expression.h +fl/rule/RuleBlock.h +fl/rule/Rule.h +fl/term/Activated.h +fl/term/Aggregated.h +fl/term/Bell.h +fl/term/Binary.h +fl/term/Concave.h +fl/term/Constant.h +fl/term/Cosine.h +fl/term/Discrete.h +fl/term/Function.h +fl/term/Gaussian.h +fl/term/GaussianProduct.h +fl/term/Linear.h +fl/term/PiShape.h +fl/term/Ramp.h +fl/term/Rectangle.h +fl/term/SigmoidDifference.h +fl/term/Sigmoid.h +fl/term/SigmoidProduct.h +fl/term/Spike.h +fl/term/SShape.h +fl/term/Term.h +fl/term/Trapezoid.h +fl/term/Triangle.h +fl/term/ZShape.h +fl/variable/InputVariable.h +fl/variable/OutputVariable.h +fl/variable/Variable.h diff --git a/fuzzylite/FL_SOURCES b/fuzzylite/FL_SOURCES new file mode 100644 index 0000000..16d8ab4 --- /dev/null +++ b/fuzzylite/FL_SOURCES @@ -0,0 +1,102 @@ +src/Benchmark.cpp +src/Complexity.cpp +src/Console.cpp +src/activation/First.cpp +src/activation/General.cpp +src/activation/Highest.cpp +src/activation/Last.cpp +src/activation/Lowest.cpp +src/activation/Proportional.cpp +src/activation/Threshold.cpp +src/defuzzifier/Bisector.cpp +src/defuzzifier/Centroid.cpp +src/defuzzifier/IntegralDefuzzifier.cpp +src/defuzzifier/LargestOfMaximum.cpp +src/defuzzifier/MeanOfMaximum.cpp +src/defuzzifier/SmallestOfMaximum.cpp +src/defuzzifier/WeightedAverage.cpp +src/defuzzifier/WeightedAverageCustom.cpp +src/defuzzifier/WeightedDefuzzifier.cpp +src/defuzzifier/WeightedSum.cpp +src/defuzzifier/WeightedSumCustom.cpp +src/Engine.cpp +src/Exception.cpp +src/factory/ActivationFactory.cpp +src/factory/DefuzzifierFactory.cpp +src/factory/FactoryManager.cpp +src/factory/FunctionFactory.cpp +src/factory/HedgeFactory.cpp +src/factory/SNormFactory.cpp +src/factory/TermFactory.cpp +src/factory/TNormFactory.cpp +src/fuzzylite.cpp +src/hedge/Any.cpp +src/hedge/Extremely.cpp +src/hedge/HedgeFunction.cpp +src/hedge/Not.cpp +src/hedge/Seldom.cpp +src/hedge/Somewhat.cpp +src/hedge/Very.cpp +src/imex/CppExporter.cpp +src/imex/Exporter.cpp +src/imex/FclExporter.cpp +src/imex/FclImporter.cpp +src/imex/FisExporter.cpp +src/imex/FisImporter.cpp +src/imex/FldExporter.cpp +src/imex/FllExporter.cpp +src/imex/FllImporter.cpp +src/imex/Importer.cpp +src/imex/JavaExporter.cpp +src/imex/RScriptExporter.cpp +src/main.cpp +src/norm/s/AlgebraicSum.cpp +src/norm/s/BoundedSum.cpp +src/norm/s/DrasticSum.cpp +src/norm/s/EinsteinSum.cpp +src/norm/s/HamacherSum.cpp +src/norm/s/Maximum.cpp +src/norm/s/NilpotentMaximum.cpp +src/norm/s/NormalizedSum.cpp +src/norm/s/SNormFunction.cpp +src/norm/s/UnboundedSum.cpp +src/norm/t/AlgebraicProduct.cpp +src/norm/t/BoundedDifference.cpp +src/norm/t/DrasticProduct.cpp +src/norm/t/EinsteinProduct.cpp +src/norm/t/HamacherProduct.cpp +src/norm/t/Minimum.cpp +src/norm/t/NilpotentMinimum.cpp +src/norm/t/TNormFunction.cpp +src/rule/Antecedent.cpp +src/rule/Consequent.cpp +src/rule/Expression.cpp +src/rule/RuleBlock.cpp +src/rule/Rule.cpp +src/term/Activated.cpp +src/term/Aggregated.cpp +src/term/Bell.cpp +src/term/Binary.cpp +src/term/Concave.cpp +src/term/Constant.cpp +src/term/Cosine.cpp +src/term/Discrete.cpp +src/term/Function.cpp +src/term/Gaussian.cpp +src/term/GaussianProduct.cpp +src/term/Linear.cpp +src/term/PiShape.cpp +src/term/Ramp.cpp +src/term/Rectangle.cpp +src/term/Sigmoid.cpp +src/term/SigmoidDifference.cpp +src/term/SigmoidProduct.cpp +src/term/Spike.cpp +src/term/SShape.cpp +src/term/Term.cpp +src/term/Trapezoid.cpp +src/term/Triangle.cpp +src/term/ZShape.cpp +src/variable/InputVariable.cpp +src/variable/OutputVariable.cpp +src/variable/Variable.cpp diff --git a/fuzzylite/FL_TESTS b/fuzzylite/FL_TESTS new file mode 100644 index 0000000..3897a77 --- /dev/null +++ b/fuzzylite/FL_TESTS @@ -0,0 +1,16 @@ +test/catch.hpp +test/MainTest.cpp +test/BenchmarkTest.cpp +test/QuickTest.cpp +test/activation/ThresholdTest.cpp +test/hedge/HedgeFunctionTest.cpp +test/imex/FldExporterTest.cpp +test/imex/FllImporterTest.cpp +test/imex/RScriptExporterTest.cpp +test/norm/NormFunctionTest.cpp +test/term/AggregatedTest.cpp +test/term/DiscreteTest.cpp +test/term/FunctionTest.cpp +test/term/TrapezoidTest.cpp +test/term/TriangleTest.cpp +test/variable/VariableTest.cpp diff --git a/fuzzylite/build.bat b/fuzzylite/build.bat new file mode 100644 index 0000000..11f62f8 --- /dev/null +++ b/fuzzylite/build.bat @@ -0,0 +1,122 @@ +@echo off +setlocal EnableDelayedExpansion +set argc=0 +set valid="no" + +for %%a in (%*) do ( + set /A argc+=1 + if /I "%%a"=="help" ( + call:usage + goto:eof + ) + if /I "%%a"=="all" set valid="yes" + if /I "%%a"=="release" set valid="yes" + if /I "%%a"=="debug" set valid="yes" + if /I "%%a"=="clean" set valid="yes" + if /I "%%a"=="documentation" set valid="yes" + + if !valid!=="no" ( + echo Invalid option: %%a + call:usage + goto:eof + ) +) + +if %argc%==0 echo Building schedule: all +if not %argc%==0 echo Building schedule: %* +echo Starting in 3 seconds... +ping 1.1.1.1 -n 1 -w 3000 > nul +rem sleep 3 ::This function makes command line DOS-esque C:\Archiv~1 + +if %argc%==0 (call:all) + +for %%a in (%*) do (call:%%a) + +goto:eof + +:debug + echo. + echo. + echo **************************************** + echo STARTING: debug + + if not exist debug mkdir debug + cd debug + cmake .. -G"NMake Makefiles" -DCMAKE_BUILD_TYPE=Debug -DFL_BACKTRACE=ON -DFL_USE_FLOAT=OFF -DFL_CPP98=OFF -DFL_BUILD_TESTS=ON + nmake + cd .. + + echo. + echo FINISHED: debug + echo **************************************** + goto:eof + +:release + echo. + echo. + echo **************************************** + echo STARTING: release + + if not exist release mkdir release + cd release + cmake .. -G"NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DFL_BACKTRACE=OFF -DFL_USE_FLOAT=OFF -DFL_CPP98=OFF -DFL_BUILD_TESTS=ON + nmake + cd .. + + echo. + echo FINISHED: release + echo **************************************** + goto:eof + +:all + echo. + echo. + echo **************************************** + echo STARTING: all + call:release + call:debug + echo. + echo FINISHED: all + echo **************************************** + goto:eof + +:documentation + echo. + echo. + echo **************************************** + echo STARTING: documentation + + cd .. + doxygen + rem TODO: cd back to previous directory. Maybe use: cd /D %~dp0 + echo. + echo FINISHED: documentation + echo **************************************** + goto:eof +:clean + echo. + echo. + echo **************************************** + echo STARTING: clean + @echo on + if exist debug rmdir /S /Q debug + if exist release rmdir /S /Q release + if exist CMakeFiles rmdir /S /Q CMakeFiles + if exist CMakeCache.txt del CMakeCache.txt + @echo off + echo. + echo FINISHED: clean + echo **************************************** + goto:eof + +:usage + echo Usage: build.bat [options] + echo where [options] can be any of the following: + echo ^ all builds fuzzylite in debug and release mode (default) + echo ^ debug builds fuzzylite in debug mode + echo ^ release builds fuzzylite in release mode + echo ^ clean erases previous builds + echo ^ help shows this information + echo. + +ENDLOCAL diff --git a/fuzzylite/build.sh b/fuzzylite/build.sh new file mode 100755 index 0000000..da39905 --- /dev/null +++ b/fuzzylite/build.sh @@ -0,0 +1,98 @@ +#!/bin/bash + +if [ -z "$FL_USE_FLOAT" ]; then + FL_USE_FLOAT="OFF" +fi + +if [ -z "$FL_CPP98" ]; then + FL_CPP98="OFF" +fi + +if [ -z "$FL_BUILD_TESTS" ]; then + FL_BUILD_TESTS="OFF" +fi + +debug(){ + set -e + mkdir -p debug + cd debug + cmake .. -G"Unix Makefiles" -DFL_USE_FLOAT=${FL_USE_FLOAT} -DFL_CPP98=${FL_CPP98} -DCMAKE_BUILD_TYPE=Debug -DFL_BACKTRACE=ON -DFL_BUILD_TESTS=${FL_BUILD_TESTS} + make all + if [ "${FL_BUILD_TESTS}" == "ON" ]; then + (export CTEST_OUTPUT_ON_FAILURE=TRUE; make test) + fi + cd .. +} + +release(){ + set -e + mkdir -p release + cd release + cmake .. -G"Unix Makefiles" -DFL_USE_FLOAT=${FL_USE_FLOAT} -DFL_CPP98=${FL_CPP98} -DCMAKE_BUILD_TYPE=Release -DFL_BACKTRACE=ON -DFL_BUILD_TESTS=${FL_BUILD_TESTS} + make all + if [ "${FL_BUILD_TESTS}" == "ON" ]; then + (export CTEST_OUTPUT_ON_FAILURE=TRUE; make test) + fi + cd .. +} + +documentation(){ + set -e + cd .. + doxygen Doxyfile + cd - +} + +all(){ + debug + release +} + +clean(){ + rm -rf release debug CMakeFiles +} + +usage(){ + printf 'Usage:\t[bash] ./build.sh [options]\n' + printf "where\t[options] can be any of the following:\n" + printf "\tall\t\t builds fuzzylite in debug and release mode (default)\n" + printf "\tdebug\t\t builds fuzzylite in debug mode\n" + printf "\trelease\t\t builds fuzzylite in release mode\n" + printf "\tclean\t\t erases previous builds\n" + printf "\thelp\t\t shows this information\n" + printf "\n" +} + +############################# +echo "Parameters: $@" + +OPTIONS=( "all" "release" "debug" "clean" "documentation" "help") +BUILD=( ) + +for arg in "$@" +do + if [[ "$arg" == "help" ]]; then usage && exit 0; fi + + if [[ "$arg" == "all" || "$arg" == "debug" || "$arg" == "release" || "$arg" == "clean" || "$arg" == "documentation" ]]; + then BUILD+=( $arg ); else echo "Invalid option: $arg" && usage && exit 2; + fi +done + +if [ ${#BUILD[@]} -eq 0 ]; then BUILD+=( "release" "debug" ); fi + +echo "Building schedule: ${BUILD[@]}" +echo "Starting in 3 seconds..." +sleep 3 + +for option in "${BUILD[@]}" +do + printf "\n\n" + printf "******************************\n" + printf "STARTING: $option\n" + eval ${option} + printf "\nFINISHED: $option\n" + printf "******************************\n\n" +done + + + diff --git a/fuzzylite/fl/Benchmark.h b/fuzzylite/fl/Benchmark.h new file mode 100644 index 0000000..9a212db --- /dev/null +++ b/fuzzylite/fl/Benchmark.h @@ -0,0 +1,400 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_BENCHMARK_H +#define FL_BENCHMARK_H + +#include "fl/fuzzylite.h" + +#include "fl/Complexity.h" +#include "fl/imex/FldExporter.h" + +#include <string> +#include <vector> + +namespace fl { + + class Engine; + + /** + The Benchmark class is designed to evaluate the performance of an Engine. + + @author Juan Rada-Vilela, Ph.D. + @see Engine + @since 6.0 + */ + class FL_API Benchmark { + private: + std::string _name; + Engine* _engine; + std::vector<std::vector<scalar> > _expected; + std::vector<std::vector<scalar> > _obtained; + std::vector<scalar> _times; + scalar _tolerance; + + public: + + /** + Unit of time to utilize in the results + */ + enum TimeUnit { + NanoSeconds, MicroSeconds, MilliSeconds, Seconds, Minutes, Hours + }; + + /** + Shape of the table of results + */ + enum TableShape { + Horizontal, Vertical + }; + + /** + Contents of the table of results + */ + enum TableContents { + Header = 1, Body = 2, HeaderAndBody = (Header | Body) + }; + + /** + Type of error between expected and obtained values + */ + enum ErrorType { + NonFinite, Accuracy, All + }; + + explicit Benchmark(const std::string& name = "", Engine* engine = fl::null, + scalar tolerance = fuzzylite::macheps()); + virtual ~Benchmark(); + FL_DEFAULT_COPY_AND_MOVE(Benchmark) + + /** + Sets the name of the benchmark + @param name is the name of the benchmark + */ + void setName(const std::string& name); + /** + Gets the name of the benchmark + @return name is the name of the benchmark + */ + std::string getName() const; + + /** + Sets the engine to benchmark + @param engine is the engine to benchmark + */ + void setEngine(Engine* engine); + /** + Gets the engine to benchmark + @return the engine to benchmark + */ + Engine* getEngine() const; + + /** + Sets the set of expected values from the engine, where the inner vector + contains the input values and output values + @param expected is the set of expected values from the engine + */ + void setExpected(const std::vector<std::vector<scalar> >& expected); + /** + Gets the set of expected values from the engine, where the inner vector + contains the input values and output values + @return the set of expected values from the engine + */ + const std::vector<std::vector<scalar> >& getExpected() const; + + /** + Sets the set of obtained values from the engine, where the inner vector + contains the input values and output values + @param obtained is the set of obtained values from the engine + */ + void setObtained(const std::vector<std::vector<scalar> >& obtained); + /** + Gets the set of obtained values from the engine, where the inner vector + contains the input values and output values + @return the set of obtained values from the engine + */ + const std::vector<std::vector<scalar> >& getObtained() const; + + /** + Sets the vector of nanoseconds taken to produce the set of obtained values + from the set of expected input values + @param times is the vector of nanoseconds taken to produce the set of obtained values + from the set of expected input values + */ + void setTimes(const std::vector<scalar> times); + /** + Gets the vector of nanoseconds taken to produce the set of obtained values + from the set of expected input values + @return the vector of nanoseconds taken to produce the set of obtained values + from the set of expected input values + */ + const std::vector<scalar>& getTimes() const; + + /** + Sets the tolerance above which the difference between an expected and + obtained value from the engine is considered an error + @param tolerance is the tolerance above which the difference between + an expected and obtained value from the engine is considered an error + */ + void setTolerance(scalar tolerance); + /** + Gets the tolerance above which the difference between an expected and + obtained value from the engine is considered an error + @return the tolerance above which the difference between an expected + and obtained value from the engine is considered an error + */ + scalar getTolerance() const; + + /** + Produces and loads into memory the set of expected values from the + engine + @param values is the number of values to evaluate the engine upon + @param scope is the scope of the values to generate + @throws Exception if the engine is not set + */ + virtual void prepare(int values, FldExporter::ScopeOfValues scope); + /** + Reads and loads into memory the set of expected values from the + engine + @param reader is the reader of a set of lines containing + space-separated values + @param numberOfLines is the maximum number of lines to read from the + reader, and a value $f@n=(\infty, -1]$f@ reads the entire file. + */ + virtual void prepare(std::istream& reader, long numberOfLines = -1); + + /** + Runs the benchmark on the engine only once + @return the time in nanoseconds required by the run, which is + also appended to the times stored in Benchmark::getTimes() + */ + virtual scalar runOnce(); + /** + Runs the benchmark on the engine multiple times + @param times is the number of times to run the benchmark on the engine + @return vector of the time in nanoseconds required by each run, which is + also appended to the times stored in Benchmark::getTimes() + */ + virtual std::vector<scalar> run(int times); + + /** + Resets the benchmark to be ready to run again + */ + virtual void reset(); + + /** + Indicates whether errors can be computed based on the expected and + obtained values from the benchmark. If the benchmark was prepared from + a file reader and the file included columns of expected output values + and the benchmark has been run at least once, then the benchmark can + automatically compute the errors and will automatically include them in + the results. + @return whether errors can be computed based on the expected and + obtained values from the benchmark + */ + virtual bool canComputeErrors() const; + + /** + Computes the mean squared error over all output variables considering + only those cases where there is an accuracy error as defined in + Benchmark::accuracyErrors(). + @return the mean squared error over all the output variables. + */ + virtual scalar meanSquaredError() const; + + /** + Computes the mean squared error of the given output variable + considering only those cases where there is an accuracy error + as defined in Benchmark::accuracyErrors(). + @param outputVariable is the output variable to compute the errors for + @return the mean squared error over the given output variable. + */ + virtual scalar meanSquaredError(const OutputVariable* outputVariable) const; + + /** + Computes the number of errors over all the output variables caused by + non-finite differences or accuracy differences. An error is counted when + the difference between the expected and obtained values is not finite, + or the absolute difference between the expected and obtained values + is not smaller than the tolerance. + @return the number of errors over all the output variables caused by + non-finite differences or accuracy differences + */ + virtual int allErrors() const; + + /** + Computes the number of errors of the given output variable caused by + non-finite differences or accuracy differences. An error is counted when + the difference between the expected and obtained values is not finite, + or the absolute difference between the expected and obtained values + is not smaller than the tolerance. + @param outputVariable is the output variable to account the errors for + @return the number of errors of the given output variable caused by + non-finite differences or accuracy differences + */ + virtual int allErrors(const OutputVariable* outputVariable) const; + + /** + Computes the number of errors over all the output variables caused by + non-finite differences (ie, infinity and NaN). An error is counted when + the difference between the expected and obtained values is not finite. + @return the number of errors over all the output variables caused by + non-finite differences + */ + virtual int nonFiniteErrors() const; + + /** + Computes the number of errors of the given output variable caused by + non-finite differences (ie, infinity and NaN). An error is counted when + the difference between the expected and obtained values is not finite. + @param outputVariable is the output variable to account the errors for + @return the number of errors of the given output variable caused by + non-finite differences + */ + virtual int nonFiniteErrors(const OutputVariable* outputVariable) const; + + /** + Computes the number of errors over all the output variables caused by + a significant difference in accuracy. An error is counted when the + absolute difference between the expected and obtained values + is not smaller than the tolerance. + + @f$\text{E} = \sum_y \sum_i \epsilon_i^y, \text{where } \epsilon_i^y = + \begin{cases} + 0 & \text{if} |e_i^y - o^y_i| < \theta\\ + 1 & \text{otherwise} + \end{cases} + @f$, + @f$y@f$ is the set of output variables, @f$e@f$ is the set of + expected output values, @f$o@f$ is the set of obtained output values, + and @f$\theta@f$ is the tolerance + + @return the number of errors over all the output variables caused by + a significant difference in accuracy + */ + virtual int accuracyErrors() const; + + + /** + Computes the number of errors over the given output variable caused by + a significant difference in accuracy. An error is counted when the + absolute difference between the expected and obtained values + is not smaller than the tolerance. + + @f$\text{E} = \sum_i \epsilon_i, \text{where } \epsilon_i = + \begin{cases} + 0 & \text{if} |e_i - o_i| < \theta\\ + 1 & \text{otherwise} + \end{cases} + @f$, + @f$e@f$ is the set of expected output values, + @f$o@f$ is the set of obtained output values, + and @f$\theta@f$ is the tolerance + + @param outputVariable is the output variable to account the errors for + @return the number of errors of the given output variable caused by + a significant difference in accuracy + */ + virtual int accuracyErrors(const OutputVariable* outputVariable) const; + + /** + Computes the number of errors of the given type over all the output + variables. + @param errorType is the type of error to account for + @return the number of errors over all the output variables + */ + virtual int numberOfErrors(ErrorType errorType) const; + + /** + Computes the number of errors of the given type over the given output + variable. + @param errorType is the type of error to account for + @param outputVariable is the output variable to account the errors for + @return the number of errors over the given output variable + */ + virtual int numberOfErrors(ErrorType errorType, + const OutputVariable* outputVariable) const; + + /** + Returns the name of the time unit + @param unit is the time unit + @return the name of the time unit + */ + static std::string stringOf(TimeUnit unit); + + /** + Returns the factor of the given unit from NanoSeconds + @param unit is the time unit + @return the factor of the given unit from NanoSeconds + */ + static scalar factorOf(TimeUnit unit); + /** + Converts the time to different scales + @param time is the time to convert + @param from is the units of the time to convert from + @param to is the units of the time to convert to + @return the time in the units specified + */ + static scalar convert(scalar time, TimeUnit from, TimeUnit to); + + /** + Returns the header of a horizontal table of results + @param runs is the number of times the benchmark will be run, hence + producing the relevant number of columns for each run + @param includeErrors indicates whether to include columns for computing + the errors + @return the header of a horizontal table of results + */ + virtual std::vector<std::string> header(int runs, bool includeErrors = true); + + /**Result is a type definition for a pair of strings*/ + typedef std::pair<std::string, std::string> Result; + /** + Computes and returns the results from the benchmark aggregating the + statistics of all the output variables + @param timeUnit is the unit of time of the results + @param includeTimes indicates whether to include the times of each run + @return the results from the benchmark + */ + virtual std::vector<Result> results(TimeUnit timeUnit = NanoSeconds, bool includeTimes = true) const; + + /** + Computes and returns the results from the benchmark for the given output + variable + @param outputVariable is the output variable to compute the statistics for + @param timeUnit is the unit of time of the results + @param includeTimes indicates whether to include the times of each run + @return the results from the benchmark + */ + virtual std::vector<Result> results(const OutputVariable* outputVariable, + TimeUnit timeUnit = NanoSeconds, bool includeTimes = true) const; + + /** + Formats the results + @param results is the vector of results + @param shape is the shape to present the table of results + @param contents indicates the information to include in the table of results + @param delimiter is the delimiter of the table of results + @return the formatted results from the benchmark + */ + virtual std::string format(std::vector<Result> results, TableShape shape, + TableContents contents, const std::string& delimiter = "\t") const; + }; + +} + +#endif /* FL_BENCHMARK_H */ + diff --git a/fuzzylite/fl/Complexity.h b/fuzzylite/fl/Complexity.h new file mode 100644 index 0000000..a63138f --- /dev/null +++ b/fuzzylite/fl/Complexity.h @@ -0,0 +1,297 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_COMPLEXITY_H +#define FL_COMPLEXITY_H + +#include "fl/fuzzylite.h" + +#include <vector> + +namespace fl { + class Engine; + class InputVariable; + class OutputVariable; + class Variable; + class RuleBlock; + class Rule; + + /** + The Complexity class is used throughout the library to estimate the + computational cost of the different components of the library + + @author Juan Rada-Vilela, Ph.D. + @see Engine + @see Variable + @see OutputVariable + @see RuleBlock + @see Activation + @see Rule + @see Antecedent + @see Consequent + @see Hedge + @see Norm + @since 6.0 + */ + + class FL_API Complexity { + private: + scalar _comparison; + scalar _arithmetic; + scalar _function; + + public: + explicit Complexity(scalar all = 0.0); + explicit Complexity(scalar comparison, scalar arithmetic, scalar function); + virtual ~Complexity(); + FL_DEFAULT_COPY_AND_MOVE(Complexity) + + Complexity& operator+=(const Complexity& other); + Complexity& operator-=(const Complexity& other); + Complexity& operator*=(const Complexity& other); + Complexity& operator/=(const Complexity& other); + + Complexity operator+(const Complexity& rhs) const; + Complexity operator-(const Complexity& rhs) const; + Complexity operator*(const Complexity& rhs) const; + Complexity operator/(const Complexity& rhs) const; + + bool operator==(const Complexity& rhs) const; + bool operator!=(const Complexity& rhs) const; + bool operator<(const Complexity& rhs) const; + bool operator<=(const Complexity& rhs) const; + bool operator>(const Complexity& rhs) const; + bool operator>=(const Complexity& rhs) const; + + /** + Increases the comparison measure by the given amount + @param comparison is the amount to increase the comparison measure by + @return the reference to the Complexity object with the updated comparison + measure + */ + virtual Complexity& comparison(scalar comparison); + virtual void setComparison(scalar comparison); + virtual scalar getComparison() const; + + /** + Increases the arithmetic measure by the given amount + @param arithmetic is the amount to increase the comparison measure by + @return the reference to the Complexity object with the updated arithmetic + measure + */ + virtual Complexity& arithmetic(scalar arithmetic); + virtual void setArithmetic(scalar arithmetic); + virtual scalar getArithmetic() const; + + /** + Increases the function measure by the given amount + @param function is the amount to increase the function measure by + @return the reference to the Complexity object with the updated function + measure + */ + virtual Complexity& function(scalar function); + virtual void setFunction(scalar function); + virtual scalar getFunction() const; + + /** + Returns a vector containing the measures of complexity + @return a vector containing the measures of complexity + */ + typedef std::pair<std::string, scalar> Measure; + virtual std::vector<Measure> measures() const; + + /** + Increases the complexity by the given parameter + @param x is the addend + @return the reference to the updated complexity + */ + virtual Complexity& plus(const Complexity& x); + /** + Reduces the complexity by the given parameter + @param x is the subtrahend + @return the reference to the updated complexity object + */ + virtual Complexity& minus(const Complexity& x); + /** + Multiplies the complexity by the given parameter + @param x is the multiplicand + @return the reference to the updated complexity object + */ + virtual Complexity& multiply(const Complexity& x); + /** + Divides the complexity by the given parameter + @param x is the divisor + @return the reference to the updated complexity object + */ + virtual Complexity& divide(const Complexity& x); + + /** + Increases each measure by the given parameter + @param x is the addend + @return the reference to the updated complexity + */ + virtual Complexity& plus(scalar x); + /** + Reduces each measure by the given parameter + @param x is the subtrahend + @return the reference to the updated complexity + */ + virtual Complexity& minus(scalar x); + /** + Multiplies each measure by the given parameter + @param x is the multiplicand + @return the reference to the updated complexity + */ + virtual Complexity& multiply(scalar x); + /** + Divides each measure by the given parameter + @param x is the divisor + @return the reference to the updated complexity + */ + virtual Complexity& divide(scalar x); + + /** + Compares the complexity for equality to another with the given tolerance + @param x is the complexity to compare against + @param macheps is the tolerance to compare floating-point values + @return `true` if every measure in this satisfies Op::isEq(this, x, macheps), + and `false` otherwise + */ + virtual bool equals(const Complexity& x, scalar macheps = fuzzylite::macheps()) const; + /** + Compares the complexity for strict inequality (less than) to another + with the given tolerance + @param x is the complexity to compare against + @param macheps is the tolerance to compare floating-point values + @return `true` if every measure in this satisfies Op::isLt(this, x, macheps), + and `false` otherwise + */ + virtual bool lessThan(const Complexity& x, scalar macheps = fuzzylite::macheps()) const; + /** + Compares the complexity for inequality (less than or equal to) to another + with the given tolerance + @param x is the complexity to compare against + @param macheps is the tolerance to compare floating-point values + @return `true` if every measure in this satisfies Op::isLE(this, x, macheps), + and `false` otherwise + */ + virtual bool lessThanOrEqualsTo(const Complexity& x, scalar macheps = fuzzylite::macheps()) const; + /** + Compares the complexity for strict inequality (greater than) to another + with the given tolerance + @param x is the complexity to compare against + @param macheps is the tolerance to compare floating-point values + @return `true` if every measure in this satisfies Op::isGt(this, x, macheps), + and `false` otherwise + */ + virtual bool greaterThan(const Complexity& x, scalar macheps = fuzzylite::macheps()) const; + /** + Compares the complexity for inequality (greater than or equal to) to + another with the given tolerance + @param x is the complexity to compare against + @param macheps is the tolerance to compare floating-point values + @return `true` if every measure in this satisfies Op::isGE(this, x, macheps), + and `false` otherwise + */ + virtual bool greaterThanOrEqualsTo(const Complexity& x, scalar macheps = fuzzylite::macheps()) const; + + /** + Computes the sum of the measures + @return the sum of the measures + */ + virtual scalar sum() const; + + /** + Computes the norm of the complexity + @return the norm of the complexity + */ + virtual scalar norm() const; + + /** + Returns the measures of the complexity + @return the measures of the complexity + */ + virtual std::string toString() const; + + /** + Computes the complexity of the given engine as the sum of complexities + of the rule blocks + @param engine is the engine for which to compute the complexity + @return the complexity of the given engine as the sum of complexities + of the rule blocks + */ + virtual Complexity compute(const Engine* engine) const; + + /** + Computes the complexity of the given input variable + @param inputVariable is the input variable for which to compute the complexity + @return the complexity of the given input variable + */ + virtual Complexity compute(const InputVariable* inputVariable) const; + /** + Computes the complexity of the given output variable + @param outputVariable is the output variable for which to compute the complexity + @return the complexity of the given output variable + */ + virtual Complexity compute(const OutputVariable* outputVariable) const; + + /** + Computes the complexity of the given input variables + @param inputVariables is the vector of input variables for which to + compute the complexity + @return the complexity of the given input variables + */ + virtual Complexity compute(const std::vector<InputVariable*>& inputVariables) const; + /** + Computes the complexity of the given output variables + @param outputVariables is the vector of output variables for which to + compute the complexity + @param complexityOfDefuzzification indicates whether to compute the + complexity of the variable including the defuzzification process + @return the complexity of the given output variables + */ + virtual Complexity compute(const std::vector<OutputVariable*>& outputVariables, + bool complexityOfDefuzzification = false) const; + /** + Computes the complexity of the given variables + @param variables is the vector of variables for which to compute the + complexity + @return the complexity of the given variables + */ + virtual Complexity compute(const std::vector<Variable*>& variables) const; + + /** + Computes the complexity of the given rule block + @param ruleBlock is the rule block for which to compute the complexity + @return the complexity of the given rule block + */ + virtual Complexity compute(const RuleBlock* ruleBlock) const; + + /** + Computes the complexity of the given rule blocks + @param ruleBlocks is the vector of rule blocks for which to compute the + complexity + @return Computes the complexity of the given rule blocks + */ + virtual Complexity compute(const std::vector<RuleBlock*>& ruleBlocks) const; + + }; + + +} + +#endif /* COMPLEXITY_H */ + diff --git a/fuzzylite/fl/Console.h b/fuzzylite/fl/Console.h new file mode 100644 index 0000000..b96d677 --- /dev/null +++ b/fuzzylite/fl/Console.h @@ -0,0 +1,155 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_CONSOLE_H +#define FL_CONSOLE_H + +#include "fl/fuzzylite.h" +#include <map> +#include <string> +#include <vector> + +namespace fl { + class Engine; + + /** + The Console class is a command-line tool that helps to utilize the + `fuzzylite` library. + + @author Juan Rada-Vilela, Ph.D. + @since 4.0 + */ + class FL_API Console { + public: + + /** + A command-line option given by key, value and description + */ + struct Option { + std::string key, value, description; + + explicit Option(const std::string& key = "", + const std::string& value = "", + const std::string& description = ""); + }; + + /**Keyword for input file*/ + static const std::string KW_INPUT_FILE; + /**Keyword for input file format*/ + static const std::string KW_INPUT_FORMAT; + /**Keyword for output file*/ + static const std::string KW_OUTPUT_FILE; + /**Keyword for output file format*/ + static const std::string KW_OUTPUT_FORMAT; + /**Keyword for built-in example*/ + static const std::string KW_EXAMPLE; + /**Keyword for number of decimals*/ + static const std::string KW_DECIMALS; + /**Keyword for file containing input data*/ + static const std::string KW_DATA_INPUT_FILE; + /**Keyword for number of values to generate*/ + static const std::string KW_DATA_VALUES; + /**Keyword for the scope of the number of values to generate*/ + static const std::string KW_DATA_VALUES_SCOPE; + /**Keyword for exporting headers in FLD*/ + static const std::string KW_DATA_EXPORT_HEADER; + /**Keyword for exporting input values in FLD*/ + static const std::string KW_DATA_EXPORT_INPUTS; + + /** + Creates a new Mamdani Engine based on the SimpleDimmer example + @return a new Mamdani Engine based on the SimpleDimmer example + */ + static Engine* mamdani(); + /** + Creates a new TakagiSugeno Engine based on the Approximation example of @f$sin(x)/x@f$ + @return a new TakagiSugeno Engine based on the Approximation example of @f$sin(x)/x@f$ + */ + static Engine* takagiSugeno(); + + /** + Creates a new Hybrid Engine based on the Tipper example using Mamdani + and TakagiSugeno outputs. + @return a new Hybrid Engine based on the Tipper example using Mamdani + and TakagiSugeno outputs. + */ + static Engine* hybrid(); + + + protected: + virtual std::map<std::string, std::string> parse(int argc, const char* argv[]); + virtual void process(const std::map<std::string, std::string>& options); + + virtual void process(const std::string& input, std::ostream& writer, + const std::string& inputFormat, const std::string& outputFormat, + const std::map<std::string, std::string>& options); + + virtual int readCharacter(); + virtual void interactive(std::ostream& writer, Engine* engine); + virtual std::string interactiveHelp(); + + virtual void exportAllExamples(const std::string& from, const std::string& to); + virtual void exportAllExamples(const std::string& from, const std::string& to, + const std::string& examplesPath, const std::string& outputPath); + + /** + Benchmarks the engine described in the FLL file against the dataset + contained in the FLD file. + + @param fllFile is the file describing the engine in FLL format + @param fldFile is the file containing the dataset in FLD format + @param runs is the number of runs to evaluate the benchmarks + @param writer is the output where the results will be written to + @throws Exception if something goes wrong reading the files, importing the + engines or evaluating the benchmark + */ + + virtual void benchmark(const std::string& fllFile, const std::string& fldFile, + int runs, std::ofstream* writer = fl::null) const; + /** + Benchmarks the list of engines against the list of datasets, both described + as absolute or relative paths + + @param fllFileList is the file containing the list of paths of engines in + FLL format + @param fldFileList is the file containing the list of paths of datasets in + FLD format + @param runs is the number of runs to evaluate the benchmarks + @param writer is the output where the results will be written to + @throws Exception if something goes wrong reading the files, importing the + engines or evaluating the benchmark + */ + virtual void benchmarks(const std::string& fllFileList, const std::string& fldFileList, + int runs, std::ofstream* writer = fl::null) const; + + public: + /** + Returns a string representation of the usage of the command-line tool + @return a string representation of the usage of the command-line tool + */ + virtual std::string usage(); + + /** + Returns a vector of the options available from the command line + @return a vector of the options available from the command line + */ + virtual std::vector<Option> availableOptions(); + + static int main(int argc, const char* argv[]); + }; +} +#endif /* FL_CONSOLE_H */ + diff --git a/fuzzylite/fl/Engine.h b/fuzzylite/fl/Engine.h new file mode 100644 index 0000000..3a88478 --- /dev/null +++ b/fuzzylite/fl/Engine.h @@ -0,0 +1,478 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_ENGINE_H +#define FL_ENGINE_H + +#include "fl/fuzzylite.h" + +#include "fl/Complexity.h" + +#include <string> +#include <vector> + +namespace fl { + + class InputVariable; + class OutputVariable; + class Variable; + class RuleBlock; + class Hedge; + class TNorm; + class SNorm; + class Defuzzifier; + class Activation; + + /** + The Engine class is the core class of the library as it groups the + necessary components of a fuzzy logic controller. + + @author Juan Rada-Vilela, Ph.D. + @see InputVariable + @see OutputVariable + @see RuleBlock + @since 4.0 + */ + class FL_API Engine { + private: + std::string _name; + std::string _description; + std::vector<InputVariable*> _inputVariables; + std::vector<OutputVariable*> _outputVariables; + std::vector<RuleBlock*> _ruleBlocks; + + void copyFrom(const Engine& source); + + protected: + void updateReferences() const; + + public: + explicit Engine(const std::string& name = ""); + Engine(const Engine& other); + Engine& operator=(const Engine& other); + virtual ~Engine(); + FL_DEFAULT_MOVE(Engine) + + /** + Configures the engine with the given operators + @param conjunction is a TNorm registered in the TNormFactory + @param disjunction is an SNorm registered in the SNormFactory + @param implication is an TNorm registered in the TNormFactory + @param aggregation is an SNorm registered in the SNormFactory + @param defuzzifier is a defuzzifier registered in the DefuzzifierFactory + @param activation is an activation method registered in the ActivationFactory + */ + virtual void configure(const std::string& conjunction, + const std::string& disjunction, + const std::string& implication, + const std::string& aggregation, + const std::string& defuzzifier, + const std::string& activation); + + /** + Configures the engine with clones of the given object operators, taking + ownership of the objects. + + @param conjunction is the operator to process the propositions joined + by `and` in the antecedent of the rules + @param disjunction is the operator to process the propositions + joined by `or` in the antecedent of the rules + @param implication is the operator to modify the consequents of the + rules based on the activation degree of the antecedents of the rules + @param aggregation is the operator to aggregate the resulting + implications of the rules + @param defuzzifier is the operator to transform the aggregated + implications into a single scalar value + @param activation is the activation method to activate and fire the + rule blocks + */ + virtual void configure(TNorm* conjunction, SNorm* disjunction, + TNorm* implication, SNorm* aggregation, + Defuzzifier* defuzzifier, Activation* activation); + + /** + Indicates whether the engine has been configured correctly and is + ready for operation. In more advanced engines, the result of this + method should be taken as a suggestion and not as a prerequisite to + operate the engine. + + @param status (if not null) contains the configuration errors of the engine + @return whether the engine is ready to operate + */ + virtual bool isReady(std::string* status = fl::null) const; + + /** + Computes the estimated complexity of operation of the engine + @return the estimated complexity of operation of the engine + */ + virtual Complexity complexity() const; + /** + Processes the engine in its current state as follows: (a) Clears the + aggregated fuzzy output variables, (b) Activates the rule blocks, and + (c) Defuzzifies the output variables + @see Aggregated::clear() + @see RuleBlock::activate() + @see OutputVariable::defuzzify() + */ + virtual void process(); + + /** + Restarts the engine by setting the values of the input variables to + fl::nan and clearing the output variables + @see Variable::setValue() + @see OutputVariable::clear() + */ + virtual void restart(); + + /** + Sets the name of the engine + @param name is the name of the engine + */ + virtual void setName(const std::string& name); + /** + Gets the name of the engine + @return the name of the engine + */ + virtual std::string getName() const; + + /** + Sets the description of the engine + @param description is the description of the engine + */ + virtual void setDescription(const std::string& description); + /** + Gets the description of the engine + @return the description of the engine + */ + virtual std::string getDescription() const; + + /** + Sets the value of the given input variable. + The cost of this method is O(n), where n is the number of + input variables in the engine. For performance, please get the + variables by index. + @param name is the name of the input variable + @param value is the value for the input variable + */ + virtual void setInputValue(const std::string& name, scalar value); + /** + Gets the value of the given output variable. + The cost of this method is O(n), where n is the number of + output variables in the engine. For performance, please get the + variables by index. + @param name is the name of the output variable + @return the value of the given output variable + */ + virtual scalar getOutputValue(const std::string& name); + + /** + Returns a string representation of the engine in the FuzzyLite + Language + @return a string representation of the engine in the FuzzyLite + Language + */ + virtual std::string toString() const; + + enum Type { + /**Mamdani: When the output variables have IntegralDefuzzifier%s*/ + Mamdani, + /**Larsen: When Mamdani and AlgebraicProduct is the implication operator of + the rule blocks */ + Larsen, + /**TakagiSugeno: When output variables have WeightedDefuzzifier%s of type + TakagiSugeno and the output variables have Constant, Linear, or + Function terms*/ + TakagiSugeno, + /**Tsukamoto: When output variables have WeightedDefuzzifier%s of type + Tsukamoto and the output variables only have monotonic terms + (Concave, Ramp, Sigmoid, SShape, and ZShape)*/ + Tsukamoto, + /**InverseTsukamoto: When output variables have WeightedDefuzzifier%s of type + TakagiSugeno and the output variables do not only have Constant, + Linear or Function terms*/ + InverseTsukamoto, + /**Hybrid: When output variables have different defuzzifiers*/ + Hybrid, + /**Unknown: When output variables have no defuzzifiers*/ + Unknown + }; + /** + Infers the type of the engine based on its current configuration + + @param name stores a string representation of the engine type (if the + pointer passed is not `fl::null`) + @param reason stores a string representation explaining the reasons + for the inferred type (if the pointer passed is not `fl::null`) + @return the inferred type of the engine based on its current + configuration + */ + virtual Type type(std::string* name = fl::null, std::string* reason = fl::null) const; + + /** + Creates a clone of the engine + @return a clone of the engine + */ + virtual Engine* clone() const; + + /** + Returns a vector that contains the input variables followed by the + output variables in the order of insertion + + @return a vector that contains the input variables followed by the + output variables in the order of insertion + */ + virtual std::vector<Variable*> variables() const; + + /** + Adds the input variable + @param inputVariable is the input variable + */ + virtual void addInputVariable(InputVariable* inputVariable); + /** + Sets the input variable at the given index + @param inputVariable is the input variable to set + @param index is the index at which the input variable is to be stored + @return the input variable previously stored at the given index + */ + virtual InputVariable* setInputVariable(InputVariable* inputVariable, std::size_t index); + /** + Inserts the input variable at the given index, shifting other + variables one position to the right + @param inputVariable is the input variable to insert + @param index is the index at which the input variable is to be + inserted + */ + virtual void insertInputVariable(InputVariable* inputVariable, std::size_t index); + /** + Gets the input variable at the given index + @param index is the given index + @return the input variable at the given index + */ + virtual InputVariable* getInputVariable(std::size_t index) const; + /** + Gets the input variable of the given name after iterating the input + variables. The cost of this method is O(n), where n is the number of + input variables in the engine. For performance, please get the + variables by index. + @param name is the name of the input variable + @return input variable of the given name + @throws fl::Exception if there is no variable with the given name + */ + virtual InputVariable* getInputVariable(const std::string& name) const; + /** + Removes the input variable at the given index (without deleting it) + and shifts the remaining input variables one position to the left + @param index is the given index + @return the input variable at the given index + */ + virtual InputVariable* removeInputVariable(std::size_t index); + /** + Removes the input variable of the given name (without deleting it) and + shifts the remaining input variables one position to the left + @param name is the name of the input variable + @return the input variable of the given name + @throws fl::Exception if there is no variable with the given name + */ + virtual InputVariable* removeInputVariable(const std::string& name); + /** + Indicates whether an input variable of the given name is in the input + variables + @param name is the name of the input variable + @return whether an input variable is registered with the given name + */ + virtual bool hasInputVariable(const std::string& name) const; + /** + Returns the number of input variables added to the engine + @return the number of input variables added to the engine + */ + virtual std::size_t numberOfInputVariables() const; + /** + Returns an immutable vector of input variables + @return an immutable vector of input variables + */ + virtual const std::vector<InputVariable*>& inputVariables() const; + /** + Sets the vector of input variables + @param inputVariables is the vector of input variables + */ + virtual void setInputVariables(const std::vector<InputVariable*>& inputVariables); + /** + Returns a mutable vector of input variables + @return a mutable vector of input variables + */ + virtual std::vector<InputVariable*>& inputVariables(); + + /** + Adds the output variable + @param outputVariable is the output variable + */ + virtual void addOutputVariable(OutputVariable* outputVariable); + /** + Sets the output variable at the given index + @param outputVariable is the output variable to set + @param index is the index at which the output variable is to be stored + @return the output variable previously stored at the given index + */ + virtual OutputVariable* setOutputVariable(OutputVariable* outputVariable, std::size_t index); + /** + Inserts the output variable at the given index, shifting other + variables one position to the right + @param outputVariable is the output variable to insert + @param index is the index at which the output variable is to be inserted + */ + virtual void insertOutputVariable(OutputVariable* outputVariable, std::size_t index); + /** + Gets the output variable at the given index + @param index is the given index + @return the output variable at the given index + */ + virtual OutputVariable* getOutputVariable(std::size_t index) const; + /** + Gets the output variable of the given name after iterating the output + variables. The cost of this method is O(n), where n is the number of + output variables in the engine. For performance, please get the + variables by index. + @param name is the name of the output variable + @return output variable of the given name + @throws fl::Exception if there is no variable with the given name + */ + virtual OutputVariable* getOutputVariable(const std::string& name) const; + + /** + Indicates whether an output variable of the given name is in the + output variables + @param name is the name of the output variable + @return whether an output variable is registered with the given name + */ + virtual bool hasOutputVariable(const std::string& name) const; + /** + Removes the output variable at the given index (without deleting it) + and shifts the remaining output variables one position to the left + @param index is the given index + @return the output variable at the given index + */ + virtual OutputVariable* removeOutputVariable(std::size_t index); + /** + Removes the output variable of the given name (without deleting it) + and shifts the remaining output variables one position to the left + @param name is the name of the output variable + @return the output variable of the given name + @throws fl::Exception if there is no variable with the given name + */ + virtual OutputVariable* removeOutputVariable(const std::string& name); + /** + Returns the number of output variables added to the engine + @return the number of output variables added to the engine + */ + virtual std::size_t numberOfOutputVariables() const; + /** + Returns an immutable vector of output variables + @return an immutable vector of output variables + */ + virtual const std::vector<OutputVariable*>& outputVariables() const; + /** + Sets the vector of output variables + @param outputVariables is the vector of output variables + */ + virtual void setOutputVariables(const std::vector<OutputVariable*>& outputVariables); + /** + Returns a mutable vector of output variables + @return a mutable vector of output variables + */ + virtual std::vector<OutputVariable*>& outputVariables(); + + /** + Adds the rule block + @param ruleBlock is the rule block + */ + virtual void addRuleBlock(RuleBlock* ruleBlock); + /** + Sets the rule block at the given index + @param ruleBlock is the rule block to set + @param index is the index at which the rule block is to be stored + @return the rule block previously stored at the given index + */ + virtual RuleBlock* setRuleBlock(RuleBlock* ruleBlock, std::size_t index); + /** + Inserts the rule block at the given index, shifting other blocks one + position to the right + @param ruleBlock is the rule block to insert + @param index is the index at which the rule block is to be inserted + */ + virtual void insertRuleBlock(RuleBlock* ruleBlock, std::size_t index); + /** + Gets the rule block at the given index + @param index is the given index + @return the rule block at the given index + */ + virtual RuleBlock* getRuleBlock(std::size_t index) const; + /** + Gets the rule block of the given name after iterating the rule blocks. + The cost of this method is O(n), where n is the number of + rule blocks in the engine. For performance, please get the rule blocks + by index. + @param name is the name of the rule block + @return rule block of the given name + @throws fl::Exception if there is no block with the given name + */ + virtual RuleBlock* getRuleBlock(const std::string& name) const; + /** + Indicates whether an rule block of the given name is in the rule + blocks + @param name is the name of the rule block + @return whether an rule block of the given name is in the rule blocks + + */ + virtual bool hasRuleBlock(const std::string& name) const; + /** + Removes the rule block at the given index (without deleting it) and + shifts the remaining rule blocks one position to the left + @param index is the given index + @return the rule block at the given index + */ + virtual RuleBlock* removeRuleBlock(std::size_t index); + /** + Removes the rule block of the given name (without deleting it) and + shifts the remaining rule blocks one position to the left + @param name is the name of the rule block + @return the rule block of the given name + @throws fl::Exception if there is no rule block with the given name + */ + virtual RuleBlock* removeRuleBlock(const std::string& name); + /** + Returns the number of rule blocks added to the engine + @return the number of rule blocks added to the engine + */ + virtual std::size_t numberOfRuleBlocks() const; + /** + Returns an immutable vector of rule blocks + @return an immutable vector of rule blocks + */ + virtual const std::vector<RuleBlock*>& ruleBlocks() const; + /** + Sets the vector of rule blocks + @param ruleBlocks is the vector of rule blocks + */ + virtual void setRuleBlocks(const std::vector<RuleBlock*>& ruleBlocks); + /** + Returns a mutable vector of rule blocks + @return a mutable vector of rule blocks + */ + virtual std::vector<RuleBlock*>& ruleBlocks(); + + }; +} +#endif /* FL_ENGINE_H */ diff --git a/fuzzylite/fl/Exception.h b/fuzzylite/fl/Exception.h new file mode 100644 index 0000000..60996f1 --- /dev/null +++ b/fuzzylite/fl/Exception.h @@ -0,0 +1,140 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_EXCEPTION_H +#define FL_EXCEPTION_H + +#include "fl/fuzzylite.h" + +#include <exception> +#include <string> +#include <vector> + +namespace fl { + + /** + + The Exception class is the only type of exception that is utilized + throughout the library. If the library is built with the compiling flag + `-DFL_BACKTRACE=ON`, the method Exception::btCallStack() will provide a + stack trace when an exception is thrown. Please, have in mind that + enabling the stack trace requires the external library `dbghelp` in + the Windows platform, which is generally available in the operating + system. + + @author Juan Rada-Vilela, Ph.D. + @since 4.0 + + */ + +#ifdef FL_WINDOWS + //Disable warning for dllexport of std::exception in Windows +#pragma warning (push) +#pragma warning (disable:4275) +#endif + + class FL_API Exception : public std::exception { +#ifdef FL_WINDOWS +#pragma warning (pop) +#endif + private: + std::string _what; + public: + explicit Exception(const std::string& what); + /** + Constructor to be used in conjunction with macro `FL_AT` + @param what is the message of the exception + @param file is the name of the file where the exception occurred + @param line is the line number in the file where the exception occurred + @param function is the name of the function where the exception occurred + */ + explicit Exception(const std::string& what, const std::string& file, int line, + const std::string& function); + virtual ~Exception() FL_INOEXCEPT FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(Exception) + + /** + Sets the message of the `std::exception` + @param what is the message of the `std::exception` + */ + virtual void setWhat(const std::string& what); + /** + Gets the message of the `std::exception` + @return the message of the `std::exception` + */ + virtual std::string getWhat() const; + /** + Gets the message of the `std::exception` + @return the message of the `std::exception` + */ + virtual const char* what() const FL_INOEXCEPT FL_IOVERRIDE; + + /** + Appends a message to the exception + @param whatElse is a message to the exception + */ + virtual void append(const std::string& whatElse); + /** + Appends an error trace to the exception. The method can be called + utilizing the macro `FL_AT` + @param file is the name of the file where the exception occurred + @param line is the line number in the file where the exception occurred + @param function is the name of the function where the exception occurred + */ + virtual void append(const std::string& file, int line, const std::string& function); + /** + Appends an error trace with a message to the exception. The method can be called + utilizing the macro `FL_AT` + @param whatElse is further information about the exception + @param file is the name of the file where the exception occurred + @param line is the line number in the file where the exception occurred + @param function is the name of the function where the exception occurred + */ + virtual void append(const std::string& whatElse, + const std::string& file, int line, const std::string& function); + + /** + Returns the stack trace (if enabled) + @return the stack trace (if enabled) + */ + static std::string btCallStack(); + + /** + Provides a signal handler to catch signals + @param signal is the code of the signal + */ + static void signalHandler(int signal); + + /** + Converts a given signal into an Exception (does not work very well on Windows) + @param signal is the code of the signal + */ + static void convertToException(int signal); + + /** + Provides a handler for `terminate` and `unexpected` signals + */ + static void terminate(); + /** + Logs the exception to console and proceeds the regular execution of the library + @param exception is the exception thrown + */ + static void catchException(const std::exception& exception); + + + }; +} +#endif /* FL_EXCEPTION_H */ diff --git a/fuzzylite/fl/Headers.h b/fuzzylite/fl/Headers.h new file mode 100644 index 0000000..1c41002 --- /dev/null +++ b/fuzzylite/fl/Headers.h @@ -0,0 +1,148 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_HEADERS_H +#define FL_HEADERS_H + +/** + The Headers.h file contains the headers of all the classes in the + `fuzzylite` library, thereby encouraging the use of the directive `#include + "fl/Headers.h"` in projects using the library. + */ + + +#include "fl/fuzzylite.h" + +#include "fl/Benchmark.h" +#include "fl/Complexity.h" +#include "fl/Console.h" +#include "fl/Engine.h" +#include "fl/Exception.h" + +#include "fl/activation/Activation.h" +#include "fl/activation/First.h" +#include "fl/activation/General.h" +#include "fl/activation/Highest.h" +#include "fl/activation/Last.h" +#include "fl/activation/Lowest.h" +#include "fl/activation/Proportional.h" +#include "fl/activation/Threshold.h" + +#include "fl/defuzzifier/Bisector.h" +#include "fl/defuzzifier/Centroid.h" +#include "fl/defuzzifier/Defuzzifier.h" +#include "fl/defuzzifier/IntegralDefuzzifier.h" +#include "fl/defuzzifier/SmallestOfMaximum.h" +#include "fl/defuzzifier/LargestOfMaximum.h" +#include "fl/defuzzifier/MeanOfMaximum.h" +#include "fl/defuzzifier/WeightedAverage.h" +#include "fl/defuzzifier/WeightedDefuzzifier.h" +#include "fl/defuzzifier/WeightedSum.h" + +#include "fl/factory/ActivationFactory.h" +#include "fl/factory/CloningFactory.h" +#include "fl/factory/ConstructionFactory.h" +#include "fl/factory/FactoryManager.h" +#include "fl/factory/FunctionFactory.h" +#include "fl/factory/DefuzzifierFactory.h" +#include "fl/factory/HedgeFactory.h" +#include "fl/factory/SNormFactory.h" +#include "fl/factory/TNormFactory.h" +#include "fl/factory/TermFactory.h" + +#include "fl/imex/CppExporter.h" +#include "fl/imex/FclImporter.h" +#include "fl/imex/FclExporter.h" +#include "fl/imex/FisImporter.h" +#include "fl/imex/FisExporter.h" +#include "fl/imex/FldExporter.h" +#include "fl/imex/FllImporter.h" +#include "fl/imex/FllExporter.h" +#include "fl/imex/JavaExporter.h" +#include "fl/imex/RScriptExporter.h" + +#include "fl/hedge/Any.h" +#include "fl/hedge/Extremely.h" +#include "fl/hedge/Hedge.h" +#include "fl/hedge/HedgeFunction.h" +#include "fl/hedge/Not.h" +#include "fl/hedge/Seldom.h" +#include "fl/hedge/Somewhat.h" +#include "fl/hedge/Very.h" + +#include "fl/Operation.h" + +#include "fl/norm/Norm.h" +#include "fl/norm/SNorm.h" +#include "fl/norm/TNorm.h" + +#include "fl/norm/s/AlgebraicSum.h" +#include "fl/norm/s/BoundedSum.h" +#include "fl/norm/s/DrasticSum.h" +#include "fl/norm/s/EinsteinSum.h" +#include "fl/norm/s/HamacherSum.h" +#include "fl/norm/s/Maximum.h" +#include "fl/norm/s/NilpotentMaximum.h" +#include "fl/norm/s/NormalizedSum.h" +#include "fl/norm/s/SNormFunction.h" +#include "fl/norm/s/UnboundedSum.h" + +#include "fl/norm/t/AlgebraicProduct.h" +#include "fl/norm/t/BoundedDifference.h" +#include "fl/norm/t/DrasticProduct.h" +#include "fl/norm/t/EinsteinProduct.h" +#include "fl/norm/t/HamacherProduct.h" +#include "fl/norm/t/Minimum.h" +#include "fl/norm/t/NilpotentMinimum.h" +#include "fl/norm/t/TNormFunction.h" + +#include "fl/rule/Antecedent.h" +#include "fl/rule/Consequent.h" +#include "fl/rule/Rule.h" +#include "fl/rule/RuleBlock.h" +#include "fl/rule/Expression.h" + +#include "fl/term/Aggregated.h" +#include "fl/term/Bell.h" +#include "fl/term/Binary.h" +#include "fl/term/Concave.h" +#include "fl/term/Constant.h" +#include "fl/term/Cosine.h" +#include "fl/term/Discrete.h" +#include "fl/term/Function.h" +#include "fl/term/Gaussian.h" +#include "fl/term/GaussianProduct.h" +#include "fl/term/Linear.h" +#include "fl/term/PiShape.h" +#include "fl/term/Ramp.h" +#include "fl/term/Rectangle.h" +#include "fl/term/SShape.h" +#include "fl/term/Sigmoid.h" +#include "fl/term/SigmoidDifference.h" +#include "fl/term/SigmoidProduct.h" +#include "fl/term/Spike.h" +#include "fl/term/Term.h" +#include "fl/term/Activated.h" +#include "fl/term/Trapezoid.h" +#include "fl/term/Triangle.h" +#include "fl/term/ZShape.h" + +#include "fl/variable/InputVariable.h" +#include "fl/variable/OutputVariable.h" +#include "fl/variable/Variable.h" + + +#endif /* FL_HEADERS_H */ diff --git a/fuzzylite/fl/Operation.h b/fuzzylite/fl/Operation.h new file mode 100644 index 0000000..8070116 --- /dev/null +++ b/fuzzylite/fl/Operation.h @@ -0,0 +1,1069 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_OPERATION_H +#define FL_OPERATION_H + +#include "fl/fuzzylite.h" + +#include "fl/Exception.h" + +#include <string> +#include <vector> + +namespace fl { + + /** + The Operation class contains methods for numeric operations, string + manipulation, and other functions, all of which are also accessible via + fl::Op. + + @author Juan Rada-Vilela, Ph.D. + @since 4.0 + */ + class Operation { + public: + + /** + Returns the minimum between the two parameters + @param a + @param b + @return @f$\min(a,b)@f$ + */ + template <typename T> + static T min(T a, T b); + + /** + Returns the maximum between the two parameters + @param a + @param b + @return @f$\max(a,b)@f$ + */ + template <typename T> + static T max(T a, T b); + + /** + Returns @f$x@f$ bounded in @f$[\min,\max]@f$ + @param x is the value to be bounded + @param min is the minimum value of the range + @param max is the maximum value of the range + @return @f$ + \begin{cases} + \min & \mbox{if $x < \min$} \cr + \max & \mbox{if $x > \max$} \cr + x & \mbox{otherwise} + \end{cases} + @f$ + */ + template <typename T> + static T bound(T x, T min, T max); + + /** + Indicates whether @f$x@f$ is within the boundaries (open or closed) + @param x is the value + @param min is the minimum of the range + @param max is the maximum of the range + @param geq determines whether the maximum is a closed interval + @param leq determines whether the minimum is a closed interval + @return @f$ + \begin{cases} + x \in [\min,\max] & \mbox{if $geq \wedge leq$} \cr + x \in (\min,\max] & \mbox{if $geq \wedge \bar{leq}$} \cr + x \in [\min, \max) & \mbox{if $\bar{geq} \wedge leq$} \cr + x \in (\min, \max) & \mbox{if $\bar{geq} \wedge \bar{leq}$} \cr + \end{cases} + @f$ + */ + template <typename T> + static bool in(T x, T min, T max, bool geq = true, bool leq = true); + + /** + Indicates whether @f$x@f$ is infinity + @param x is the value + @return whether @f$x@f$ is infinity + */ + template <typename T> + static bool isInf(T x); + + /** + Indicates whether @f$x@f$ is not-a-number (NaN) + @param x is the value + @return whether @f$x@f$ is not-a-number (NaN) + */ + template <typename T> + static bool isNaN(T x); + + /** + Indicates whether @f$x@f$ is finite, that is, @f$x \not\in + \{\pm\infty, \mathrm{NaN}\}@f$ + @param x is the value + @return whether @f$x@f$ is finite + */ + template <typename T> + static bool isFinite(T x); + + /** + Returns whether @f$a@f$ is less than @f$b@f$ at the given tolerance + @param a + @param b + @param macheps is the minimum difference upon which two + floating-point values are considered equivalent + @return whether @f$a@f$ is less than @f$b@f$ at the given tolerance + */ + static bool isLt(scalar a, scalar b, scalar macheps = fuzzylite::_macheps); + /** + Returns whether @f$a@f$ is less than or equal to @f$b@f$ at the given + tolerance + @param a + @param b + @param macheps is the minimum difference upon which two + floating-point values are considered equivalent + @return whether @f$a@f$ is less than or equal to @f$b@f$ at the given + tolerance + */ + static bool isLE(scalar a, scalar b, scalar macheps = fuzzylite::_macheps); + /** + Returns whether @f$a@f$ is equal to @f$b@f$ at the given tolerance + @param a + @param b + @param macheps is the minimum difference upon which two + floating-point values are considered equivalent + @return whether @f$a@f$ is equal to @f$b@f$ at the given tolerance + */ + static bool isEq(scalar a, scalar b, scalar macheps = fuzzylite::_macheps); + /** + Returns whether @f$a@f$ is greater than @f$b@f$ at the given tolerance + @param a + @param b + @param macheps is the minimum difference upon which two + floating-point values are considered equivalent + @return whether @f$a@f$ is greater than @f$b@f$ at the given tolerance + */ + static bool isGt(scalar a, scalar b, scalar macheps = fuzzylite::_macheps); + /** + Returns whether @f$a@f$ is greater than or equal to @f$b@f$ at the + given tolerance + @param a + @param b + @param macheps is the minimum difference upon which two + floating-point values are considered equivalent + @return whether @f$a@f$ is greater than or equal to @f$b@f$ at the + given tolerance + */ + static bool isGE(scalar a, scalar b, scalar macheps = fuzzylite::_macheps); + + /** + Linearly interpolates the parameter @f$x@f$ in range + `[fromMin,fromMax]` to a new value in the range `[toMin,toMax]` + @param x is the source value to interpolate + @param fromMin is the minimum value of the source range + @param fromMax is the maximum value of the source range + @param toMin is the minimum value of the target range + @param toMax is the maximum value of the target range + @return the source value linearly interpolated to the target range: + @f$ y = y_a + (y_b - y_a) \dfrac{x-x_a}{x_b-x_a} @f$ + */ + static scalar scale(scalar x, scalar fromMin, scalar fromMax, + scalar toMin, scalar toMax); + + /** + Linearly interpolates the parameter @f$x@f$ in range + `[fromMin,fromMax]` to a new value in the range `[toMin,toMax]`, + truncated to the range `[toMin,toMax]` if bounded is `true`. + @param x is the source value to interpolate + @param fromMin is the minimum value of the source range + @param fromMax is the maximum value of the source range + @param toMin is the minimum value of the target range + @param toMax is the maximum value of the target range + @param bounded determines whether the resulting value is bounded to + the range + @return the source value linearly interpolated to the target range: + @f$ y = y_a + (y_b - y_a) \dfrac{x-x_a}{x_b-x_a} @f$ + */ + static scalar scale(scalar x, scalar fromMin, scalar fromMax, + scalar toMin, scalar toMax, bool bounded); + + /** + Adds two values + @param a + @param b + @return @f$a+b@f$ + */ + static scalar add(scalar a, scalar b); + /** + Subtracts two values + @param a + @param b + @return @f$a-b@f$ + */ + static scalar subtract(scalar a, scalar b); + /** + Multiplies two values + @param a + @param b + @return @f$a\times b@f$ + */ + static scalar multiply(scalar a, scalar b); + /** + Divides two values + @param a + @param b + @return @f$a/b@f$ + */ + static scalar divide(scalar a, scalar b); + /** + Computes the modulo + @param a + @param b + @return @f$a \mod b@f$ + */ + static scalar modulo(scalar a, scalar b); + + /** + Computes the logical AND + @param a + @param b + @return @f$ + \begin{cases} + 1.0 & \mbox{if $a=1 \wedge b=1$}\cr + 0.0 & \mbox{otherwise} + \end{cases} + @f$ + */ + static scalar logicalAnd(scalar a, scalar b); + /** + Computes the logical OR + @param a + @param b + @return @f$ + \begin{cases} + 1.0 & \mbox{if $a=1 \vee b=1$}\cr + 0.0 & \mbox{otherwise} + \end{cases} + @f$ + */ + static scalar logicalOr(scalar a, scalar b); + /** + Returns the complement of the value + @param a + @return @f$ + \begin{cases} + 0.0 & \mbox{if $a=1$}\cr + 1.0 & \mbox{otherwise} + \end{cases} + @f$ + */ + static scalar logicalNot(scalar a); + /** + Negates the value + @param a + @return -a + */ + static scalar negate(scalar a); + /** + Rounds the value to the nearest integer + @param x + @return @f$ + \begin{cases} + \lfloor x + 0.5 \rfloor & \mbox{if $x > 0$}\cr + \lceil x - 0.5 \rceil & \mbox{otherwise} + \end{cases} + @f$ + */ + static scalar round(scalar x); + + /** + Returns whether @f$a@f$ is greater than or equal to @f$b@f$ at the + default tolerance + @param a + @param b + @return whether @f$a@f$ is greater than or equal to @f$b@f$ at the + default tolerance + */ + static scalar gt(scalar a, scalar b); + /** + Returns whether @f$a@f$ is greater than or equal to @f$b@f$ at the + default tolerance + @param a + @param b + @return whether @f$a@f$ is greater than or equal to @f$b@f$ at the + default tolerance + */ + static scalar ge(scalar a, scalar b); + /** + Returns whether @f$a@f$ is equal to @f$b@f$ at the default tolerance + @param a + @param b + @return whether @f$a@f$ is equal to @f$b@f$ at the default tolerance + */ + static scalar eq(scalar a, scalar b); + /** + Returns whether @f$a@f$ is different from @f$b@f$ at the default + tolerance + @param a + @param b + @return whether @f$a@f$ is different from @f$b@f$ at the default + tolerance + */ + static scalar neq(scalar a, scalar b); + /** + Returns whether @f$a@f$ is less than or equal to @f$b@f$ at the + default tolerance + @param a + @param b + @return whether @f$a@f$ is less than or equal to @f$b@f$ at the + default tolerance + */ + static scalar le(scalar a, scalar b); + /** + Returns whether @f$a@f$ is less than @f$b@f$ at the default tolerance + @param a + @param b + @return whether @f$a@f$ is less than @f$b@f$ at the default tolerance + */ + static scalar lt(scalar a, scalar b); + + /** + Returns a random number + */ + static int random(); + + /** + Increments @f$x@f$ by the unit, treating the entire vector as a + number. For example, incrementing a few times @f$x_0=\{0,0\}@f$ + within boundaries @f$[0,1]@f$ results in: @f$x_1=\{0,1\}@f$, + @f$x_2=\{1,0\}@f$, @f$x_3=\{1,1\}@f$, @f$x_4=\{0,0\}@f$. + @param x is the vector to increment + @param min is the minimum value of the dimension + @param max is the maximum value of the dimension + @return `true` if @f$x@f$ was incremented, `false` otherwise (e.g., + incrementing @f$x_3@f$ returns `false`). In earlier versions to 6.0, the + result was the inverse and indicated whether the counter had overflown + (most sincere apologies for this change). + */ + static bool increment(std::vector<int>& x, std::vector<int>& min, std::vector<int>& max); + /** + Increments @f$x@f$ by the unit at the given position, treating the + entire vector as a number. For example, incrementing + @f$x_0=\{0,0,0\}@f$ within boundaries @f$[0,1]@f$ at the second + position results in: @f$x_1=\{0,1,0\}@f$, @f$x_2=\{1,0,0\}@f$, + @f$x_3=\{1,1,0\}@f$, @f$x_4=\{0,0,0\}@f$. + @param x is the vector to increment + @param position is the position of the vector to increment, where + smaller values lead to higher significance digits + @param min is the minimum value of the dimension + @param max is the maximum value of the dimension + @return `true` if @f$x@f$ was incremented, `false` otherwise (e.g., + incrementing @f$x_3@f$ returns `false`). In earlier versions to 6.0, the + result was the inverse and indicated whether the counter had overflown + (most sincere apologies for this change). + */ + static bool increment(std::vector<int>& x, int position, std::vector<int>& min, std::vector<int>& max); + + /** + Computes the sum of the vector + */ + template <typename T> + static T sum(const std::vector<T>& x); + /** + Computes the mean of the vector + @param x is the vector + @return @f$\dfrac{\sum_i{x_i}}{|x|}@f$ + */ + template <typename T> + static scalar mean(const std::vector<T>& x); + /** + Computes the variance of the vector + @param x is the vector + @return @f$ \sum_i{ (x_i - \bar{x})^2 } / (|x| - 1) @f$ + */ + template <typename T> + static scalar variance(const std::vector<T>& x); + /** + Computes the variance of the vector using the given mean + @param x is the vector + @param mean is the mean value of the vector + @return @f$ \sum_i{ (x_i - \bar{x})^2 } / (|x| - 1) @f$ + */ + template <typename T> + static scalar variance(const std::vector<T>& x, scalar mean); + /** + Computes the standard deviation of the vector + @param x + @return @f$ \sqrt{\mbox{variance}(x, \bar{x})} @f$ + */ + template <typename T> + static scalar standardDeviation(const std::vector<T>& x); + /** + Computes the standard deviation of the vector using the given mean + @param x + @param mean is the mean value of x + @return @f$ \sqrt{\mbox{variance}(x, \bar{x})} @f$ + */ + template <typename T> + static scalar standardDeviation(const std::vector<T>& x, scalar mean); + + /** + Returns a valid name for variables + @param name + @return an name whose characters are in `[a-zA-Z_0-9.]` + */ + static std::string validName(const std::string& name); + + /** + Replaces the substrings that are equal to the given expression + @param str is the target string + @param find is the string to find + @param replace is the string to replace the findings + @param replaceAll whether all the substrings are to be replaced or + just the first string + @return the original string with replacements + */ + static std::string findReplace(const std::string& str, const std::string& find, + const std::string& replace, bool replaceAll = true); + + /** + Replaces the first substring that is equal to the given expression + @param str is the target string + @param find is the string to find + @param replace is the string to replace + @return the original string with the replacement + */ + static std::string replaceFirst(const std::string& str, const std::string& find, + const std::string& replace); + + /** + Replaces the every substring that is equal to the given expression + @param str is the target string + @param find is the string to find + @param replace is the string to replace + @return the original string with all of the replacements + */ + static std::string replaceAll(const std::string& str, const std::string& find, + const std::string& replace); + + /** + Splits the string around the given delimiter + @param str is the string to split + @param delimiter is the substrings on which the string will be split + @param ignoreEmpty whether the empty strings are discarded + @return the string split around the given delimiter + */ + static std::vector<std::string> split(const std::string& str, + const std::string& delimiter = " ", bool ignoreEmpty = true); + + /** + Removes whitespace at the beginning and end of the text + @param text + @return a space-trimmed string + */ + static std::string trim(const std::string& text); + + /** + Replaces every matching character in the text with the given + replacement + @param text is the string to have replacements + @param matchesChar is a pointer to a method that indicates whether + the given character is a match + @param replacement a string to replace a matching character + @return the string with every matching character replaced + */ + static std::string format(const std::string& text, int matchesChar(int), + const std::string& replacement = ""); + + /** + Parses the given string into a scalar value + @param x is the string to parse + @return the given string into a scalar value + @throws fl::Exception if the string does not contain a scalar value + */ + static scalar toScalar(const std::string& x); //throws (fl::Exception) + + /** + Parses the given string into a scalar value without throwing an + exception + @param x is the string to parse + @param alternative is the value to return if the string does not + contain a scalar value + @param ok contains whether the operation was successful (optional) + @return the given string into a scalar value or the alternative value + if the string does not contain a scalar value + */ + static scalar toScalar(const std::string& x, scalar alternative, + bool* ok = fl::null) FL_INOEXCEPT; + + /** + Parses the given string into a vector of scalar values + @param x is the string containing space-separated values to parse + @return the vector of scalar values + @throws fl::Exception if the string contains an invalid scalar value + */ + static std::vector<scalar> toScalars(const std::string& x); //throws (fl::Exception) + + /** + Parses the given string into a vector of scalar values + @param x is the string containing space-separated values to parse + @param alternative is the value to use if an invalid value is found + @param ok contains whether the operation was successful (optional) + @return the vector of scalar values + */ + static std::vector<scalar> toScalars(const std::string& x, scalar alternative, + bool* ok = fl::null) FL_INOEXCEPT; + + /** + Indicates whether the string can be converted to a numeric value. + @param x + @return whether the string can be converted to a numeric value + */ + static bool isNumeric(const std::string& x); + + /** + Returns a string representation of the given value + @param x is the value + @param decimals is the number of decimals to display + @param scalarFormat are the flags for the underlying std::ostringstream + @return a string representation of the given value + */ + template <typename T> + static std::string str(T x, int decimals = fuzzylite::_decimals, + std::ios_base::fmtflags scalarFormat = fuzzylite::_scalarFormat); + + /** + Joins a vector of elements by the given separator into a single + string. The elements are represented as strings utilizing the + Operation::str() method on each element + @param x is the vector of elements + @param separator is the string to add between the elements + @return a single string joining the vector of elements by the given + separator + */ + template <typename T> + static std::string join(const std::vector<T>& x, const std::string& separator); + + /** + Joins a variadic number of elements by the given separator into a + single string. The elements are represented as strings utilizing the + Operation::str() method on each element + @param items is the number of elements to join + @param separator is the string to add between the elements + @param first is the first element, which defines the type of the + subsequent elements + @param ... are the remaining elements to join + @return a single string joining the variadic number of elements by + the given separator + */ + template <typename T> + static std::string join(int items, const std::string& separator, T first, ...); + }; + + /**A shortened type to refer to Operation*/ + typedef Operation Op; +} + + +/** + Template implementation + */ + +#include "fl/defuzzifier/Defuzzifier.h" +#include "fl/norm/Norm.h" +#include "fl/norm/SNorm.h" +#include "fl/norm/TNorm.h" + +#include <algorithm> +#include <iomanip> +#include <cstdarg> +#include <cctype> + +namespace fl { + + template <typename T> + inline T Operation::min(T a, T b) { + if (Op::isNaN(a)) return b; + if (Op::isNaN(b)) return a; + return a < b ? a : b; + } + + template <typename T> + inline T Operation::max(T a, T b) { + if (Op::isNaN(a)) return b; + if (Op::isNaN(b)) return a; + return a > b ? a : b; + } + + template <typename T> + inline T Operation::bound(T x, T min, T max) { + if (x > max) return max; + if (x < min) return min; + return x; + } + + template <typename T> + inline bool Operation::in(T x, T min, T max, bool geq, bool leq) { + bool left = geq ? Op::isGE(x, min) : Op::isGt(x, min); + bool right = leq ? Op::isLE(x, max) : Op::isLt(x, max); + return (left and right); + } + + template <typename T> + inline bool Operation::isInf(T x) { + return x == fl::inf or x == -fl::inf; + } + + template <typename T> + inline bool Operation::isNaN(T x) { + return (x != x); + } + + template<typename T> + inline bool Operation::isFinite(T x) { + return not (x != x or x == fl::inf or x == -fl::inf); + } + + inline bool Operation::isLt(scalar a, scalar b, scalar macheps) { + return not (a == b or std::abs(a - b) < macheps or (a != a and b != b)) and a < b; + } + + inline bool Operation::isLE(scalar a, scalar b, scalar macheps) { + return a == b or std::abs(a - b) < macheps or (a != a and b != b) or a < b; + } + + inline bool Operation::isEq(scalar a, scalar b, scalar macheps) { + return a == b or std::abs(a - b) < macheps or (a != a and b != b); + } + + inline bool Operation::isGt(scalar a, scalar b, scalar macheps) { + return not (a == b or std::abs(a - b) < macheps or (a != a and b != b)) and a > b; + } + + inline bool Operation::isGE(scalar a, scalar b, scalar macheps) { + return a == b or std::abs(a - b) < macheps or (a != a and b != b) or a > b; + } + + inline scalar Operation::scale(scalar x, scalar fromMin, scalar fromMax, scalar toMin, scalar toMax) { + return (toMax - toMin) / (fromMax - fromMin) * (x - fromMin) + toMin; + } + + inline scalar Operation::scale(scalar x, scalar fromMin, scalar fromMax, scalar toMin, scalar toMax, bool bounded) { + scalar result = (toMax - toMin) / (fromMax - fromMin) * (x - fromMin) + toMin; + return bounded ? Op::bound(result, toMin, toMax) : result; + } + + inline scalar Operation::add(scalar a, scalar b) { + return a + b; + } + + inline scalar Operation::subtract(scalar a, scalar b) { + return a - b; + } + + inline scalar Operation::multiply(scalar a, scalar b) { + return a * b; + } + + inline scalar Operation::divide(scalar a, scalar b) { + return a / b; + } + + inline scalar Operation::modulo(scalar a, scalar b) { + return fmod(a, b); + } + + inline scalar Operation::logicalAnd(scalar a, scalar b) { + return (isEq(a, 1.0) and isEq(b, 1.0)) ? 1.0 : 0.0; + } + + inline scalar Operation::logicalOr(scalar a, scalar b) { + return (isEq(a, 1.0) or isEq(b, 1.0)) ? 1.0 : 0.0; + } + + inline scalar Operation::logicalNot(scalar a) { + return isEq(a, 1.0) ? 0.0 : 1.0; + } + + inline scalar Operation::negate(scalar a) { + return -a; + } + + inline scalar Operation::round(scalar x) { + return (x > 0.0) ? std::floor(x + 0.5) : std::ceil(x - 0.5); + } + + inline scalar Operation::gt(scalar a, scalar b) { + return isGt(a, b); + } + + inline scalar Operation::ge(scalar a, scalar b) { + return isGE(a, b); + } + + inline scalar Operation::eq(scalar a, scalar b) { + return isEq(a, b); + } + + inline scalar Operation::neq(scalar a, scalar b) { + return not isEq(a, b); + } + + inline scalar Operation::le(scalar a, scalar b) { + return isLE(a, b); + } + + inline scalar Operation::lt(scalar a, scalar b) { + return isLt(a, b); + } + + inline int Operation::random() { + return std::rand(); + } + + inline bool Operation::increment(std::vector<int>& x, std::vector<int>& min, std::vector<int>& max) { + return increment(x, -1 + int(x.size()), min, max); + } + + inline bool Operation::increment(std::vector<int>& x, int position, std::vector<int>& min, std::vector<int>& max) { + if (x.empty() or position < 0) return false; + + bool incremented = true; + if (x.at(position) < max.at(position)) { + ++x.at(position); + } else { + incremented = !(position == 0); + x.at(position) = min.at(position); + --position; + if (position >= 0) { + incremented = increment(x, position, min, max); + } + } + return incremented; + } + + template<typename T> + inline T Operation::sum(const std::vector<T>& x) { + T result = T(0); + for (std::size_t i = 0; i < x.size(); ++i) { + result += x.at(i); + } + return result; + } + + template<typename T> + inline scalar Operation::mean(const std::vector<T>& x) { + return scalar(sum(x)) / x.size(); + } + + template<typename T> + inline scalar Operation::standardDeviation(const std::vector<T>& x) { + return standardDeviation(x, mean(x)); + } + + template<typename T> + inline scalar Operation::standardDeviation(const std::vector<T>& x, scalar mean) { + if (x.empty()) return fl::nan; + if (x.size() == 1) return scalar(0.0); + return std::sqrt(variance(x, mean)); + } + + template<typename T> + inline scalar Operation::variance(const std::vector<T>& x) { + return variance(x, mean(x)); + } + + template<typename T> + inline scalar Operation::variance(const std::vector<T>& x, scalar mean) { + if (x.empty()) return fl::nan; + if (x.size() == 1) return scalar(0.0); + scalar result = 0.0; + for (std::size_t i = 0; i < x.size(); ++i) { + result += (x.at(i) - mean) * (x.at(i) - mean); + } + result /= -1 + x.size(); + return result; + } + + + + //Text Operations: + + inline std::string Operation::validName(const std::string& name) { + if (trim(name).empty()) return "unnamed"; + std::ostringstream ss; + for (std::size_t i = 0; i < name.length(); ++i) { + char c = name[i]; + if (c == '_' or c == '.' or isalnum(c)) { + ss << c; + } + } + return ss.str(); + } + + inline std::string Operation::findReplace(const std::string& str, const std::string& find, + const std::string& replace, bool replaceAll) { + std::ostringstream result; + std::size_t fromIndex = 0, nextIndex; + do { + nextIndex = str.find(find, fromIndex); + result << str.substr(fromIndex, nextIndex - fromIndex); + if (nextIndex != std::string::npos) + result << replace; + fromIndex = nextIndex + find.size(); + } while (replaceAll and nextIndex != std::string::npos); + return result.str(); + } + + inline std::string Operation::replaceFirst(const std::string& str, + const std::string& find, const std::string& replace) { + return findReplace(str, find, replace, false); + } + + inline std::string Operation::replaceAll(const std::string& str, + const std::string& find, const std::string& replace) { + return findReplace(str, find, replace, true); + } + + inline std::vector<std::string> Operation::split(const std::string& str, + const std::string& delimiter, bool ignoreEmpty) { + std::vector<std::string> result; + if (str.empty() or delimiter.empty()) { + result.push_back(str); + return result; + } + std::string::const_iterator position = str.begin(), next = str.begin(); + while (next != str.end()) { + next = std::search(position, str.end(), delimiter.begin(), delimiter.end()); + std::string token(position, next); + if (not (token.empty() and ignoreEmpty)) { + result.push_back(token); + } + if (next != str.end()) { + position = next + delimiter.size(); + } + } + return result; + } + + inline std::string Operation::trim(const std::string& text) { + if (text.empty()) return text; + if (not (std::isspace(text.at(0)) or std::isspace(text.at(text.size() - 1)))) + return text; + std::size_t start = 0, end = text.size() - 1; + while (start <= end and std::isspace(text.at(start))) { + ++start; + } + while (end >= start and std::isspace(text.at(end))) { + --end; + } + std::size_t length = end - start + 1; + if (length <= 0) return ""; + return text.substr(start, length); + } + + inline std::string Operation::format(const std::string& text, int matchesChar(int), + const std::string& replacement) { + std::ostringstream ss; + std::string::const_iterator it = text.begin(); + while (it != text.end()) { + if (matchesChar(*it)) { + ss << *it; + } else { + ss << replacement; + } + ++it; + } + return ss.str(); + } + + inline scalar Operation::toScalar(const std::string& x) { + std::istringstream iss(x); + scalar result; + iss >> result; + char strict; + if (not (iss.fail() or iss.get(strict))) return result; + + std::ostringstream _nan; + _nan << fl::nan; + if (x == _nan.str() or x == "nan") + return fl::nan; + + std::ostringstream pInf; + pInf << fl::inf; + if (x == pInf.str() or x == "inf") + return fl::inf; + + std::ostringstream nInf; + nInf << (-fl::inf); + if (x == nInf.str() or x == "-inf") + return -fl::inf; + + throw Exception("[conversion error] from <" + x + "> to scalar", FL_AT); + } + + inline scalar Operation::toScalar(const std::string& x, scalar alternative, bool* ok) FL_INOEXCEPT { + if (ok) *ok = true; + std::istringstream iss(x); + scalar result; + iss >> result; + char strict; + if (not (iss.fail() or iss.get(strict))) return result; + + std::ostringstream _nan; + _nan << fl::nan; + if (x == _nan.str() or x == "nan") + return fl::nan; + + std::ostringstream pInf; + pInf << fl::inf; + if (x == pInf.str() or x == "inf") + return fl::inf; + + std::ostringstream nInf; + nInf << (-fl::inf); + if (x == nInf.str() or x == "-inf") + return -fl::inf; + + if (ok) *ok = false; + return alternative; + } + + inline std::vector<scalar> Operation::toScalars(const std::string& x) { + std::vector<scalar> result; + std::istringstream tokenizer(x); + std::string token; + while (tokenizer >> token) { + result.push_back(Op::toScalar(token)); + } + return result; + } + + inline std::vector<scalar> Operation::toScalars(const std::string& x, + scalar alternative, bool* ok) FL_INOEXCEPT { + std::vector<scalar> result; + std::istringstream tokenizer(x); + std::string token; + bool allOK = true; + while (tokenizer >> token) { + bool good; + result.push_back(Op::toScalar(token, alternative, &good)); + allOK &= good; + } + if (ok) *ok = allOK; + return result; + } + + inline bool Operation::isNumeric(const std::string& x) { + try { + Op::toScalar(x); + return true; + } catch (...) { + return false; + } + } + + template <typename T> + inline std::string Operation::str(T x, int decimals, + std::ios_base::fmtflags scalarFormat) { + std::ostringstream ss; + if (scalarFormat != std::ios_base::fmtflags(0x0)) ss.flags(scalarFormat); + if (decimals >= 0) ss.precision(decimals); + if (Op::isNaN(x)) { + ss << "nan"; + } else if (Op::isInf(x)) { + ss << (x < T(0) ? "-inf" : "inf"); + } else if (decimals >= 0 //print x considering the given decimals regardless of macheps + and Op::isEq(scalar(x), 0.0, std::pow(10.0, -decimals))) { + ss << T(0); + } else ss << x; + return ss.str(); + } + + template <> FL_API + inline std::string Operation::str(const std::string& x, int decimals, + std::ios_base::fmtflags scalarFormat) { + FL_IUNUSED(decimals); + FL_IUNUSED(scalarFormat); + return x; + } + + template <typename T> + inline std::string Operation::join(const std::vector<T>& x, + const std::string& separator) { + std::ostringstream ss; + for (std::size_t i = 0; i < x.size(); ++i) { + ss << str(x.at(i)); + if (i + 1 < x.size()) ss << separator; + } + return ss.str(); + } + + template <> FL_API + inline std::string Operation::join(const std::vector<std::string>& x, + const std::string& separator) { + std::ostringstream ss; + for (std::size_t i = 0; i < x.size(); ++i) { + ss << x.at(i); + if (i + 1 < x.size()) ss << separator; + } + return ss.str(); + } + + template <typename T> + inline std::string Operation::join(int items, const std::string& separator, T first, ...) { + std::ostringstream ss; + ss << str(first); + if (items > 1) ss << separator; + va_list args; + va_start(args, first); + for (int i = 0; i < items - 1; ++i) { + ss << str(va_arg(args, T)); + if (i + 1 < items - 1) ss << separator; + } + va_end(args); + return ss.str(); + } + + template <> FL_API + inline std::string Operation::join(int items, const std::string& separator, + float first, ...) { + std::ostringstream ss; + ss << str(first); + if (items > 1) ss << separator; + va_list args; + va_start(args, first); + for (int i = 0; i < items - 1; ++i) { + ss << str(va_arg(args, double)); //automatic promotion + if (i + 1 < items - 1) ss << separator; + } + va_end(args); + return ss.str(); + } + + template <> FL_API + inline std::string Operation::join(int items, const std::string& separator, const char* first, ...) { + std::ostringstream ss; + ss << first; + if (items > 1) ss << separator; + va_list args; + va_start(args, first); + for (int i = 0; i < items - 1; ++i) { + ss << va_arg(args, const char*); + if (i + 1 < items - 1) ss << separator; + } + va_end(args); + return ss.str(); + } +} +#endif /* FL_OPERATION_H */ + diff --git a/fuzzylite/fl/activation/Activation.h b/fuzzylite/fl/activation/Activation.h new file mode 100644 index 0000000..562d53e --- /dev/null +++ b/fuzzylite/fl/activation/Activation.h @@ -0,0 +1,95 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_ACTIVATION_H +#define FL_ACTIVATION_H + +#include "fl/fuzzylite.h" + +#include "fl/Complexity.h" + +namespace fl { + class RuleBlock; + + /** + The Activation class is the abstract class for RuleBlock activation + methods. An activation method implements the criteria to activate the + rules within a given rule block. An activation method needs to process + every rule and determine whether the rule is to be activated or + deactivated. The activation methods were first introduced in version 6.0, + but in earlier versions the term `activation` referred to the TNorm that + modulated the consequent of a rule, which is now referred to as the + `implication` operator. + + @author Juan Rada-Vilela, Ph.D. + @see Rule + @see RuleBlock + @see ActivationFactory + @since 6.0 + */ + + class FL_API Activation { + public: + + Activation() { } + + virtual ~Activation() { } + + FL_DEFAULT_COPY_AND_MOVE(Activation) + + /** + Returns the name of the activation method, which is also utilized to + register the activation method in the ActivationFactory. + @return the name of the activation method + @see ActivationFactory + */ + virtual std::string className() const = 0; + + /** + Returns the parameters of the activation method, which can be used to + configure other instances of the activation method. + @return the parameters of the activation method + */ + virtual std::string parameters() const = 0; + + /** + Configures the activation method with the given parameters. + @param parameters contains a list of space-separated parameter values + */ + virtual void configure(const std::string& parameters) = 0; + + /** + Computes the estimated complexity of activating the given rule block + @return the estimated complexity of activating the given rule block + */ + virtual Complexity complexity(const RuleBlock* ruleBlock) const = 0; + + /** + Activates the rule block + @param ruleBlock is the rule block to activate + */ + virtual void activate(RuleBlock* ruleBlock) = 0; + + /** + Clones the activation method. + @return a clone of the activation method + */ + virtual Activation* clone() const = 0; + }; + +} + +#endif /* FL_ACTIVATION_H */ diff --git a/fuzzylite/fl/activation/First.h b/fuzzylite/fl/activation/First.h new file mode 100644 index 0000000..355b1a9 --- /dev/null +++ b/fuzzylite/fl/activation/First.h @@ -0,0 +1,104 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_FIRST_H +#define FL_FIRST_H + +#include "fl/activation/Activation.h" + +namespace fl { + + /** + The First class is a RuleBlock Activation method that activates the first + @f$n@f$ rules whose activation degrees are greater than or equal to the given + threshold. The rules are iterated in the order they were added to the rule block. + + @author Juan Rada-Vilela, Ph.D. + @see Last + @see Rule + @see RuleBlock + @see ActivationFactory + @since 6.0 + */ + + class FL_API First : public Activation { + private: + int _numberOfRules; + scalar _threshold; + public: + + explicit First(int numberOfRules = 1, scalar threshold = 0.0); + virtual ~First(); + FL_DEFAULT_COPY_AND_MOVE(First) + + virtual std::string className() const FL_IOVERRIDE; + + /** + Returns the number of rules and the threshold of the activation method + @return "numberOfRules threshold" + */ + virtual std::string parameters() const FL_IOVERRIDE; + + /** + Configures the activation method with the given number of rules and + threshold + @param parameters as "numberOfRules threshold" + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + /** + Sets the number of rules for the activation degree + @param numberOfRules is the number of rules for the activation degree + */ + virtual void setNumberOfRules(int numberOfRules); + + /** + Gets the number of rules for the activation degree + @return the number of rules for the activation degree + */ + virtual int getNumberOfRules() const; + + /** + Sets the threshold for the activation degree + @param threshold is the threshold for the activation degree + */ + virtual void setThreshold(scalar threshold); + + /** + Gets the threshold for the activation degree + @return the threshold for the activation degree + */ + virtual scalar getThreshold() const; + + virtual Complexity complexity(const RuleBlock* ruleBlock) const FL_IOVERRIDE; + + /** + Activates the first @f$n@f$ rules whose activation degrees are greater than or + equal to the given threshold. The rules are iterated in the order the + rules were added to the rule block. + @param ruleBlock is the rule block to activate + */ + virtual void activate(RuleBlock* ruleBlock) FL_IOVERRIDE; + + virtual First* clone() const FL_IOVERRIDE; + + static Activation* constructor(); + }; + +} + + +#endif /* FL_FIRST_H */ diff --git a/fuzzylite/fl/activation/General.h b/fuzzylite/fl/activation/General.h new file mode 100644 index 0000000..8b057e9 --- /dev/null +++ b/fuzzylite/fl/activation/General.h @@ -0,0 +1,73 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_GENERAL_H +#define FL_GENERAL_H + +#include "fl/fuzzylite.h" + +#include "fl/activation/Activation.h" + +namespace fl { + + /** + The General class is a RuleBlock Activation method that activates every + rule following the order in which the rules were added to the rule block. + + @author Juan Rada-Vilela, Ph.D. + @see Rule + @see RuleBlock + @see ActivationFactory + @since 6.0 + */ + + class FL_API General : public Activation { + public: + + General(); + virtual ~General(); + FL_DEFAULT_COPY_AND_MOVE(General) + + virtual std::string className() const FL_IOVERRIDE; + + /** + No parameters are required to configure the activation method. + @return an empty string + */ + virtual std::string parameters() const FL_IOVERRIDE; + + /** + No parameters are required to configure the activation method. + @param parameters is an empty string + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Complexity complexity(const RuleBlock* ruleBlock) const FL_IOVERRIDE; + + /** + Activates every rule in the given rule block following the order in + which the rules were added. + @param ruleBlock is the rule block to activate + */ + virtual void activate(RuleBlock* ruleBlock) FL_IOVERRIDE; + + virtual General* clone() const FL_IOVERRIDE; + + static Activation* constructor(); + }; +} + +#endif /* FL_GENERAL_H */ diff --git a/fuzzylite/fl/activation/Highest.h b/fuzzylite/fl/activation/Highest.h new file mode 100644 index 0000000..d3abc72 --- /dev/null +++ b/fuzzylite/fl/activation/Highest.h @@ -0,0 +1,86 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_HIGHEST_H +#define FL_HIGHEST_H + +#include "fl/fuzzylite.h" + +#include "fl/activation/Activation.h" + +namespace fl { + + /** + The Highest class is a RuleBlock Activation method that activates a given + number of rules with highest activation degrees in descending order. + + @author Juan Rada-Vilela, Ph.D. + @see Lowest + @see Rule + @see RuleBlock + @see ActivationFactory + @since 6.0 + */ + class FL_API Highest : public Activation { + private: + int _numberOfRules; + public: + explicit Highest(int numberOfRules = 1); + virtual ~Highest(); + FL_DEFAULT_COPY_AND_MOVE(Highest) + + virtual std::string className() const FL_IOVERRIDE; + + /** + Returns the number of rules to activate. + @return number of rules to activate + */ + virtual std::string parameters() const FL_IOVERRIDE; + + /** + Configures the activation method with the number of rules to activate. + @param parameters contains the number of rules to activate + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + /** + Sets the number of rules to activate + @param numberOfRules is the number of rules to activate + */ + virtual void setNumberOfRules(int numberOfRules); + + /** + Returns the number of rules to activate + @return the number of rules to activate + */ + virtual int getNumberOfRules() const; + + virtual Complexity complexity(const RuleBlock* ruleBlock) const FL_IOVERRIDE; + + /** + Activates the given number of rules with the highest activation + degrees + @param ruleBlock is the rule block to activate. + */ + virtual void activate(RuleBlock* ruleBlock) FL_IOVERRIDE; + + virtual Highest* clone() const FL_IOVERRIDE; + + static Activation* constructor(); + }; +} + +#endif /* FL_HIGHEST_H */ diff --git a/fuzzylite/fl/activation/Last.h b/fuzzylite/fl/activation/Last.h new file mode 100644 index 0000000..9dadede --- /dev/null +++ b/fuzzylite/fl/activation/Last.h @@ -0,0 +1,103 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_LAST_H +#define FL_LAST_H + + +#include "fl/activation/Activation.h" + +namespace fl { + + /** + The Last class is a RuleBlock Activation method that activates the last + @f$n@f$ rules whose activation degrees are greater than or equal to the given + threshold. The rules are iterated in the reverse order in which they were + added to the rule block. + + @author Juan Rada-Vilela, Ph.D. + @see First + @see Rule + @see RuleBlock + @see ActivationFactory + @since 6.0 + */ + + class FL_API Last : public Activation { + private: + int _numberOfRules; + scalar _threshold; + public: + explicit Last(int numberOfRules = 1, scalar threshold = 0.0); + virtual ~Last(); + FL_DEFAULT_COPY_AND_MOVE(Last) + + virtual std::string className() const FL_IOVERRIDE; + + /** + Returns the number of rules and the threshold of the activation method + @return "numberOfRules threshold" + */ + virtual std::string parameters() const FL_IOVERRIDE; + + /** + Configures the activation method with the given number of rules and + threshold + @param parameters as "numberOfRules threshold" + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + /** + Sets the number of rules for the activation degree + @param numberOfRules is the number of rules for the activation degree + */ + virtual void setNumberOfRules(int numberOfRules); + + /** + Gets the number of rules for the activation degree + @return the number of rules for the activation degree + */ + virtual int getNumberOfRules() const; + /** + Sets the threshold for the activation degree + @param threshold is the threshold for the activation degree + */ + virtual void setThreshold(scalar threshold); + + /** + Gets the threshold for the activation degree + @return the threshold for the activation degree + */ + virtual scalar getThreshold() const; + + + virtual Complexity complexity(const RuleBlock* ruleBlock) const FL_IOVERRIDE; + + /** + Activates the last @f$n@f$ rules whose activation degrees are greater + than the given threshold. The rules are iterated in the reverse order + that the rules were added to the rule block. + @param ruleBlock is the rule block to activate + */ + virtual void activate(RuleBlock* ruleBlock) FL_IOVERRIDE; + + virtual Last* clone() const FL_IOVERRIDE; + + static Activation* constructor(); + }; +} + +#endif /* FL_LAST_H */ diff --git a/fuzzylite/fl/activation/Lowest.h b/fuzzylite/fl/activation/Lowest.h new file mode 100644 index 0000000..dab8419 --- /dev/null +++ b/fuzzylite/fl/activation/Lowest.h @@ -0,0 +1,87 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_LOWEST_H +#define FL_LOWEST_H + +#include "fl/fuzzylite.h" + +#include "fl/activation/Activation.h" + +namespace fl { + + /** + The Lowest class is a RuleBlock Activation method that activates a given + number of rules with lowest activation degrees in ascending order + + @author Juan Rada-Vilela, Ph.D. + @see Highest + @see Rule + @see RuleBlock + @see ActivationFactory + @since 6.0 + */ + class FL_API Lowest : public Activation { + private: + int _numberOfRules; + public: + explicit Lowest(int numberOfRules = 1); + virtual ~Lowest(); + FL_DEFAULT_COPY_AND_MOVE(Lowest) + + virtual std::string className() const FL_IOVERRIDE; + + /** + Returns the number of rules to activate + @return number of rules to activate + */ + virtual std::string parameters() const FL_IOVERRIDE; + + /** + Configures the activation method with the number of rules to activate + @param parameters contains the number of rules to activate + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + /** + Sets the number of rules to activate + @param numberOfRules is the number of rules to activate + */ + virtual void setNumberOfRules(int numberOfRules); + + /** + Returns the number of rules to activate + @return the number of rules to activate + */ + virtual int getNumberOfRules() const; + + + virtual Complexity complexity(const RuleBlock* ruleBlock) const FL_IOVERRIDE; + + /** + Activates the rules with the lowest activation degrees in the given + rule block + @param ruleBlock is the rule block to activate + */ + virtual void activate(RuleBlock* ruleBlock) FL_IOVERRIDE; + + virtual Lowest* clone() const FL_IOVERRIDE; + + static Activation* constructor(); + }; +} + +#endif /* FL_LOWEST_H */ diff --git a/fuzzylite/fl/activation/Proportional.h b/fuzzylite/fl/activation/Proportional.h new file mode 100644 index 0000000..09e51a7 --- /dev/null +++ b/fuzzylite/fl/activation/Proportional.h @@ -0,0 +1,71 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_PROPORTIONAL_H +#define FL_PROPORTIONAL_H + +#include "fl/activation/Activation.h" + +namespace fl { + + /** + The Proportional class is a RuleBlock Activation method that activates + the rules utilizing activation degrees proportional to the activation + degrees of the other rules, thus the sum of the activation degrees is + equal to one. + + @author Juan Rada-Vilela, Ph.D. + @see Rule + @see RuleBlock + @see ActivationFactory + @since 6.0 + */ + class FL_API Proportional : public Activation { + public: + Proportional(); + virtual ~Proportional(); + FL_DEFAULT_COPY_AND_MOVE(Proportional) + + virtual std::string className() const FL_IOVERRIDE; + + /** + No parameters are required to configure the activation method + @return an empty string + */ + virtual std::string parameters() const FL_IOVERRIDE; + + /** + No parameters are required to configure the activation method + @param parameters is an empty string + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Complexity complexity(const RuleBlock* ruleBlock) const FL_IOVERRIDE; + + /** + Activates the rules utilizing activation degrees proportional to + the activation degrees of the other rules in the rule block. + @param ruleBlock is the rule block to activate. + */ + virtual void activate(RuleBlock* ruleBlock) FL_IOVERRIDE; + + virtual Proportional* clone() const FL_IOVERRIDE; + + static Activation* constructor(); + }; +} + +#endif /* FL_PROPORTIONAL_H */ diff --git a/fuzzylite/fl/activation/Threshold.h b/fuzzylite/fl/activation/Threshold.h new file mode 100644 index 0000000..0baadfa --- /dev/null +++ b/fuzzylite/fl/activation/Threshold.h @@ -0,0 +1,183 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_THRESHOLD_H +#define FL_THRESHOLD_H + +#include "fl/fuzzylite.h" + +#include "fl/activation/Activation.h" + +#include <vector> + +namespace fl { + + /** + The Threshold class is a RuleBlock Activation method that activates the + rules whose activation degrees satisfy the equation given by the + comparison operator and the threshold, and deactivates the rules which do + not satisfy the equation. + + @author Juan Rada-Vilela, Ph.D. + @see Rule + @see RuleBlock + @see ActivationFactory + @since 6.0 + */ + + class FL_API Threshold : public Activation { + public: + + /** + Comparison is an enumerator that provides six comparison operators + between the activation degree @f$a@f$ and the threshold @f$\theta@f$. + */ + enum Comparison { + /**@f$a < \theta@f$*/ + LessThan, + /**@f$a \leq \theta@f$*/ + LessThanOrEqualTo, + /**@f$a = \theta@f$*/ + EqualTo, + /**@f$a \neq \theta@f$*/ + NotEqualTo, + /**@f$a \geq \theta@f$*/ + GreaterThanOrEqualTo, + /**@f$a > \theta@f$*/ + GreaterThan + }; + private: + Comparison _comparison; + scalar _value; + public: + explicit Threshold(Comparison comparison = GreaterThanOrEqualTo, scalar threshold = 0.0); + explicit Threshold(const std::string& comparison, scalar threshold); + virtual ~Threshold(); + FL_DEFAULT_COPY_AND_MOVE(Threshold) + + virtual std::string className() const FL_IOVERRIDE; + + /** + Returns the comparison operator followed by the threshold. + @return comparison operator and threshold + */ + virtual std::string parameters() const FL_IOVERRIDE; + + /** + Configures the activation method with the comparison operator and the + threshold. + @param parameters is the comparison operator and threshold + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + /** + Sets the comparison operator for the activation method + @param comparison is the operator for the activation method + */ + virtual void setComparison(Comparison comparison); + + /** + Gets the comparison operator for the activation method + @return comparison operator for the activation method + */ + virtual Comparison getComparison() const; + + /** + Returns the comparison operator of the activation method + @return the comparison operator in (`==`, `!=`, `<`, `>`, `<=`, `>=`) + */ + virtual std::string comparisonOperator() const; + + /** + Returns the given comparison operator of the activation method + @param comparison is a valid enum value + @return the comparison operator for the given enum value + @throws fl::Exception if the given comparison operator is not valid + */ + virtual std::string comparisonOperator(Comparison comparison) const; + + /** + Returns the list of available comparison operators of the activation + method + @return (`==`, `!=`, `<`, `>`, `<=`, `>=`) + */ + virtual std::vector<std::string> availableComparisonOperators() const; + + + /** + Parses the comparison operator, or throws an + exception if the parameter does not correspond to a valid operator + @param comparisonOperator is an operator in (`==`, `!=`, `<`, `>`, + `<=`, `>=`) + */ + virtual Comparison parseComparison(const std::string& comparisonOperator) const; + + /** + Sets the threshold value of the activation method + @param value is the threshold value for activation degrees + */ + virtual void setValue(scalar value); + + /** + Gets the threshold value of the activation method + @return the threshold value of the activation method + */ + virtual scalar getValue() const; + + /** + Sets the comparison operator and the threshold for the activation + method + @param comparison is the comparison enumerator + @param value is the threshold of the activation method + */ + virtual void setThreshold(Comparison comparison, scalar value); + + /** + Sets the comparison operator and the threshold for the activation method + @param comparison is a valid comparison operator + @param value is the threshold for activation degrees + @throws fl::Exception if the comparison operator is not valid + */ + virtual void setThreshold(const std::string& comparison, scalar value); + + /** + Returns whether the activation method will activate a rule with + the given activation degree + @param activationDegree an activation degree + @return whether the comparison equation is satisfied with the + activation degree and the threshold + */ + virtual bool activatesWith(scalar activationDegree) const; + + + virtual Complexity complexity(const RuleBlock* ruleBlock) const FL_IOVERRIDE; + + /** + Activates the rules whose activation degrees satisfy the comparison + equation with the given threshold, and deactivate the rules which do + not. + @param ruleBlock is the rule block to activate + */ + virtual void activate(RuleBlock* ruleBlock) FL_IOVERRIDE; + + virtual Threshold* clone() const FL_IOVERRIDE; + + static Activation* constructor(); + }; + +} + +#endif /* FL_THRESHOLD_H */ diff --git a/fuzzylite/fl/defuzzifier/Bisector.h b/fuzzylite/fl/defuzzifier/Bisector.h new file mode 100644 index 0000000..046b185 --- /dev/null +++ b/fuzzylite/fl/defuzzifier/Bisector.h @@ -0,0 +1,64 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_BISECTOR_H +#define FL_BISECTOR_H + +#include "fl/defuzzifier/IntegralDefuzzifier.h" + +namespace fl { + + /** + The Bisector class is an IntegralDefuzzifier that computes the bisector + of a fuzzy set represented in a Term. + + @author Juan Rada-Vilela, Ph.D. + @see Centroid + @see IntegralDefuzzifier + @see Defuzzifier + @since 4.0 + */ + class FL_API Bisector : public IntegralDefuzzifier { + public: + explicit Bisector(int resolution = defaultResolution()); + virtual ~Bisector() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(Bisector) + + virtual std::string className() const FL_IOVERRIDE; + + virtual Complexity complexity(const Term* term) const FL_IOVERRIDE; + + /** + Computes the bisector of a fuzzy set. The defuzzification process + integrates over the fuzzy set utilizing the boundaries given as + parameters. The integration algorithm is the midpoint rectangle + method (https://en.wikipedia.org/wiki/Rectangle_method). + + @param term is the fuzzy set + @param minimum is the minimum value of the fuzzy set + @param maximum is the maximum value of the fuzzy set + @return the @f$x@f$-coordinate of the bisector of the fuzzy set + */ + virtual scalar defuzzify(const Term* term, + scalar minimum, scalar maximum) const FL_IOVERRIDE; + virtual Bisector* clone() const FL_IOVERRIDE; + + static Defuzzifier* constructor(); + }; +} + +#endif /* FL_BISECTOR_H */ + diff --git a/fuzzylite/fl/defuzzifier/Centroid.h b/fuzzylite/fl/defuzzifier/Centroid.h new file mode 100644 index 0000000..b056eac --- /dev/null +++ b/fuzzylite/fl/defuzzifier/Centroid.h @@ -0,0 +1,63 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_CENTROID_H +#define FL_CENTROID_H + +#include "fl/defuzzifier/IntegralDefuzzifier.h" + +namespace fl { + + /** + The Centroid class is an IntegralDefuzzifier that computes the centroid + of a fuzzy set represented in a Term. + + @author Juan Rada-Vilela, Ph.D. + @see BiSector + @see IntegralDefuzzifier + @see Defuzzifier + @since 4.0 + */ + class FL_API Centroid : public IntegralDefuzzifier { + public: + explicit Centroid(int resolution = defaultResolution()); + virtual ~Centroid() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(Centroid) + + virtual std::string className() const FL_IOVERRIDE; + + virtual Complexity complexity(const Term* term) const FL_IOVERRIDE; + + /** + Computes the centroid of a fuzzy set. The defuzzification process + integrates over the fuzzy set utilizing the boundaries given as + parameters. The integration algorithm is the midpoint rectangle + method (https://en.wikipedia.org/wiki/Rectangle_method). + + @param term is the fuzzy set + @param minimum is the minimum value of the fuzzy set + @param maximum is the maximum value of the fuzzy set + @return the @f$x@f$-coordinate of the centroid of the fuzzy set + */ + virtual scalar defuzzify(const Term* term, + scalar minimum, scalar maximum) const FL_IOVERRIDE; + virtual Centroid* clone() const FL_IOVERRIDE; + + static Defuzzifier* constructor(); + }; +} + +#endif /* FL_CENTROID_H */ diff --git a/fuzzylite/fl/defuzzifier/Defuzzifier.h b/fuzzylite/fl/defuzzifier/Defuzzifier.h new file mode 100644 index 0000000..25749eb --- /dev/null +++ b/fuzzylite/fl/defuzzifier/Defuzzifier.h @@ -0,0 +1,76 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_DEFUZZIFIER_H +#define FL_DEFUZZIFIER_H + +#include "fl/fuzzylite.h" + +#include "fl/Complexity.h" + +#include <string> + +namespace fl { + class Term; + + /** + The Defuzzifier class is the abstract class for defuzzifiers. + + @author Juan Rada-Vilela, Ph.D. + @see IntegralDefuzzifier + @see WeightedDefuzzifier + @since 4.0 + */ + class FL_API Defuzzifier { + public: + + Defuzzifier() { + } + + virtual ~Defuzzifier() { + } + FL_DEFAULT_COPY_AND_MOVE(Defuzzifier) + + /** + Returns the name of the class of the defuzzifier + @return the name of the class of the defuzzifier + */ + virtual std::string className() const = 0; + /** + Creates a clone of the defuzzifier + @return a clone of the defuzzifier + */ + virtual Defuzzifier* clone() const = 0; + + /** + Computes the complexity of defuzzifying the given term + @param term is the term to defuzzify + @return the complexity of defuzzifying the given term + */ + virtual Complexity complexity(const Term* term) const = 0; + /** + Defuzzifies the given fuzzy term utilizing the range `[minimum,maximum]` + @param term is the term to defuzzify, typically an Aggregated term + @param minimum is the minimum value of the range + @param maximum is the maximum value of the range + @return the defuzzified value of the given fuzzy term + */ + virtual scalar defuzzify(const Term* term, scalar minimum, scalar maximum) const = 0; + + }; +} + +#endif /* FL_DEFUZZIFIER_H */ diff --git a/fuzzylite/fl/defuzzifier/IntegralDefuzzifier.h b/fuzzylite/fl/defuzzifier/IntegralDefuzzifier.h new file mode 100644 index 0000000..9cfd132 --- /dev/null +++ b/fuzzylite/fl/defuzzifier/IntegralDefuzzifier.h @@ -0,0 +1,73 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_INTEGRALDEFUZZIFIER_H +#define FL_INTEGRALDEFUZZIFIER_H + +#include "fl/defuzzifier/Defuzzifier.h" + +namespace fl { + + /** + The IntegralDefuzzifier class is the base class for defuzzifiers which integrate + over the fuzzy set. + + @author Juan Rada-Vilela, Ph.D. + @since 4.0 + */ + class FL_API IntegralDefuzzifier : public Defuzzifier { + private: + static int _defaultResolution; + + int _resolution; + public: + explicit IntegralDefuzzifier(int resolution = defaultResolution()); + virtual ~IntegralDefuzzifier() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(IntegralDefuzzifier) + + /** + Sets the resolution of the defuzzifier. The resolution refers to the + number of divisions in which the range `[minimum,maximum]` is divided + in order to integrate the area under the curve + + @param resolution is the resolution of the defuzzifier + */ + virtual void setResolution(int resolution); + /** + Gets the resolution of the defuzzifier. The resolution refers to the + number of divisions in which the range `[minimum,maximum]` is divided + in order to integrate the area under the curve + + @return the resolution of the defuzzifier + */ + virtual int getResolution() const; + + /** + Sets the default resolution for integral-based defuzzifiers + @param defaultResolution is the default resolution for integral-based defuzzifiers + */ + static void setDefaultResolution(int defaultResolution); + /** + Gets the default resolution for integral-based defuzzifiers + @return the default resolution for integral-based defuzzifiers + */ + static int defaultResolution(); + + }; +} + +#endif /* INTEGRALDEFUZZIFIER_H */ + diff --git a/fuzzylite/fl/defuzzifier/LargestOfMaximum.h b/fuzzylite/fl/defuzzifier/LargestOfMaximum.h new file mode 100644 index 0000000..e325db5 --- /dev/null +++ b/fuzzylite/fl/defuzzifier/LargestOfMaximum.h @@ -0,0 +1,67 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_LARGESTOFMAXIMUM_H +#define FL_LARGESTOFMAXIMUM_H + +#include "fl/defuzzifier/IntegralDefuzzifier.h" + +namespace fl { + + /** + The LargestOfMaximum class is an IntegralDefuzzifier that computes the + largest value of the maximum membership function of a fuzzy set + represented in a Term. + + @author Juan Rada-Vilela, Ph.D. + @see SmallestOfMaximum + @see MeanOfMaximum + @see IntegralDefuzzifier + @see Defuzzifier + @since 4.0 + */ + class FL_API LargestOfMaximum : public IntegralDefuzzifier { + public: + explicit LargestOfMaximum(int resolution = defaultResolution()); + virtual ~LargestOfMaximum() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(LargestOfMaximum) + + virtual std::string className() const FL_IOVERRIDE; + + virtual Complexity complexity(const Term* term) const FL_IOVERRIDE; + + /** + Computes the largest value of the maximum membership function of a + fuzzy set. The largest value is computed by integrating over the + fuzzy set. The integration algorithm is the midpoint rectangle method + (https://en.wikipedia.org/wiki/Rectangle_method). + + @param term is the fuzzy set + @param minimum is the minimum value of the fuzzy set + @param maximum is the maximum value of the fuzzy set + @return the largest @f$x@f$-coordinate of the maximum membership + function value in the fuzzy set + */ + virtual scalar defuzzify(const Term* term, + scalar minimum, scalar maximum) const FL_IOVERRIDE; + virtual LargestOfMaximum* clone() const FL_IOVERRIDE; + + static Defuzzifier* constructor(); + }; +} + +#endif /* FL_LARGESTOFMAXIMUM_H */ + diff --git a/fuzzylite/fl/defuzzifier/MeanOfMaximum.h b/fuzzylite/fl/defuzzifier/MeanOfMaximum.h new file mode 100644 index 0000000..027f43b --- /dev/null +++ b/fuzzylite/fl/defuzzifier/MeanOfMaximum.h @@ -0,0 +1,67 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_MEANOFMAXIMUM_H +#define FL_MEANOFMAXIMUM_H + +#include "fl/defuzzifier/IntegralDefuzzifier.h" + +namespace fl { + + /** + The MeanOfMaximum class is an IntegralDefuzzifier that computes the mean + value of the maximum membership function of a fuzzy set represented in a + Term. + + @author Juan Rada-Vilela, Ph.D. + @see SmallestOfMaximum + @see MeanOfMaximum + @see IntegralDefuzzifier + @see Defuzzifier + @since 4.0 + */ + class FL_API MeanOfMaximum : public IntegralDefuzzifier { + public: + explicit MeanOfMaximum(int resolution = defaultResolution()); + virtual ~MeanOfMaximum() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(MeanOfMaximum) + + virtual std::string className() const FL_IOVERRIDE; + + virtual Complexity complexity(const Term* term) const FL_IOVERRIDE; + + /** + Computes the mean value of the maximum membership function + of a fuzzy set. The mean value is computed while integrating + over the fuzzy set. The integration algorithm is the midpoint + rectangle method (https://en.wikipedia.org/wiki/Rectangle_method). + + @param term is the fuzzy set + @param minimum is the minimum value of the fuzzy set + @param maximum is the maximum value of the fuzzy set + @return the mean @f$x@f$-coordinate of the maximum membership + function value in the fuzzy set + */ + virtual scalar defuzzify(const Term* term, + scalar minimum, scalar maximum) const FL_IOVERRIDE; + virtual MeanOfMaximum* clone() const FL_IOVERRIDE; + + static Defuzzifier* constructor(); + }; +} + +#endif /* FL_MEANOFMAXIMUM_H */ + diff --git a/fuzzylite/fl/defuzzifier/SmallestOfMaximum.h b/fuzzylite/fl/defuzzifier/SmallestOfMaximum.h new file mode 100644 index 0000000..94f8a7a --- /dev/null +++ b/fuzzylite/fl/defuzzifier/SmallestOfMaximum.h @@ -0,0 +1,67 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_SMALLESTOFMAXIMUM_H +#define FL_SMALLESTOFMAXIMUM_H + +#include "fl/defuzzifier/IntegralDefuzzifier.h" + +namespace fl { + + /** + The SmallestOfMaximum class is an IntegralDefuzzifier that computes the + smallest value of the maximum membership function of a fuzzy set + represented in a Term. + + @author Juan Rada-Vilela, Ph.D. + @see LargestOfMaximum + @see MeanOfMaximum + @see IntegralDefuzzifier + @see Defuzzifier + @since 4.0 + */ + class FL_API SmallestOfMaximum : public IntegralDefuzzifier { + public: + explicit SmallestOfMaximum(int resolution = defaultResolution()); + virtual ~SmallestOfMaximum() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(SmallestOfMaximum) + + virtual std::string className() const FL_IOVERRIDE; + + virtual Complexity complexity(const Term* term) const FL_IOVERRIDE; + + /** + Computes the smallest value of the maximum membership function in the + fuzzy set. The smallest value is computed while integrating over the + fuzzy set. The integration algorithm is the midpoint rectangle method + (https://en.wikipedia.org/wiki/Rectangle_method). + + @param term is the fuzzy set + @param minimum is the minimum value of the fuzzy set + @param maximum is the maximum value of the fuzzy set + @return the smallest @f$x@f$-coordinate of the maximum membership + function value in the fuzzy set + */ + virtual scalar defuzzify(const Term* term, + scalar minimum, scalar maximum) const FL_IOVERRIDE; + virtual SmallestOfMaximum* clone() const FL_IOVERRIDE; + + static Defuzzifier* constructor(); + }; +} + +#endif /* FL_SMALLESTOFMAXIMUM_H */ + diff --git a/fuzzylite/fl/defuzzifier/WeightedAverage.h b/fuzzylite/fl/defuzzifier/WeightedAverage.h new file mode 100644 index 0000000..7f8e9f6 --- /dev/null +++ b/fuzzylite/fl/defuzzifier/WeightedAverage.h @@ -0,0 +1,71 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_WEIGHTEDAVERAGE_H +#define FL_WEIGHTEDAVERAGE_H + +#include "fl/defuzzifier/WeightedDefuzzifier.h" + +namespace fl { + class Activated; + + /** + The WeightedAverage class is a WeightedDefuzzifier that computes the + weighted average of a fuzzy set represented in an Aggregated Term. + + @author Juan Rada-Vilela, Ph.D. + @see WeightedAverageCustom + @see WeightedSum + @see WeightedSumCustom + @see WeightedDefuzzifier + @see Defuzzifier + @since 4.0 + */ + class FL_API WeightedAverage : public WeightedDefuzzifier { + public: + explicit WeightedAverage(Type type = Automatic); + explicit WeightedAverage(const std::string& type); + virtual ~WeightedAverage() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(WeightedAverage) + + virtual std::string className() const FL_IOVERRIDE; + + virtual Complexity complexity(const Term* term) const FL_IOVERRIDE; + + /** + Computes the weighted average of the given fuzzy set represented in + an Aggregated term as @f$y = \dfrac{\sum_i w_iz_i}{\sum_i w_i} @f$, + where @f$w_i@f$ is the activation degree of term @f$i@f$, and + @f$z_i = \mu_i(w_i) @f$. + + From version 6.0, the implication and aggregation operators are not + utilized for defuzzification. + + @param term is the fuzzy set represented as an Aggregated Term + @param minimum is the minimum value of the range (only used for Tsukamoto) + @param maximum is the maximum value of the range (only used for Tsukamoto) + @return the weighted average of the given fuzzy set + */ + virtual scalar defuzzify(const Term* term, + scalar minimum, scalar maximum) const FL_IOVERRIDE; + virtual WeightedAverage* clone() const FL_IOVERRIDE; + + static Defuzzifier* constructor(); + }; +} + +#endif /* FL_WEIGHTEDAVERAGE_H */ + diff --git a/fuzzylite/fl/defuzzifier/WeightedAverageCustom.h b/fuzzylite/fl/defuzzifier/WeightedAverageCustom.h new file mode 100644 index 0000000..5597f97 --- /dev/null +++ b/fuzzylite/fl/defuzzifier/WeightedAverageCustom.h @@ -0,0 +1,77 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_WEIGHTEDAVERAGECUSTOM_H +#define FL_WEIGHTEDAVERAGECUSTOM_H + +#include "fl/defuzzifier/WeightedDefuzzifier.h" + +namespace fl { + class Activated; + + /** + The (experimental) WeightedAverageCustom class is a WeightedDefuzzifier that computes the + weighted average of a fuzzy set represented in an Aggregated Term utilizing + the fuzzy operators for implication and aggregation to compute the weighted + average. This is an experimental approach to take advantage of customization + thanks to the object-oriented design. + + @author Juan Rada-Vilela, Ph.D. + @see WeightedAverage + @see WeightedSum + @see WeightedSumCustom + @see WeightedDefuzzifier + @see Defuzzifier + @since 6.0 + */ + class FL_API WeightedAverageCustom : public WeightedDefuzzifier { + public: + explicit WeightedAverageCustom(Type type = Automatic); + explicit WeightedAverageCustom(const std::string& type); + virtual ~WeightedAverageCustom() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(WeightedAverageCustom) + + virtual std::string className() const FL_IOVERRIDE; + + virtual Complexity complexity(const Term* term) const FL_IOVERRIDE; + + /** + Computes the weighted average of the given fuzzy set represented as + an AggregatedTerm as @f$y = \dfrac{\sum_i w_iz_i}{\sum_i w_i} @f$, + where @f$w_i@f$ is the activation degree of term @f$i@f$, and + @f$z_i = \mu_i(w_i) @f$. + + If the implication and aggregation operators are set to fl::null (or + set to AlgebraicProduct and UnboundedSum, respectively), then the + operation of WeightedAverageCustom is the same as the WeightedAverage. + Otherwise, the implication and aggregation operators are utilized to + compute the multiplications and sums in @f$y@f$, respectively. + + @param term is the fuzzy set represented as an Aggregated Term + @param minimum is the minimum value of the range (only used for Tsukamoto) + @param maximum is the maximum value of the range (only used for Tsukamoto) + @return the weighted average of the given fuzzy set + */ + virtual scalar defuzzify(const Term* term, + scalar minimum, scalar maximum) const FL_IOVERRIDE; + virtual WeightedAverageCustom* clone() const FL_IOVERRIDE; + + static Defuzzifier* constructor(); + }; +} + +#endif /* FL_WEIGHTEDAVERAGECUSTOM_H */ + diff --git a/fuzzylite/fl/defuzzifier/WeightedDefuzzifier.h b/fuzzylite/fl/defuzzifier/WeightedDefuzzifier.h new file mode 100644 index 0000000..2045772 --- /dev/null +++ b/fuzzylite/fl/defuzzifier/WeightedDefuzzifier.h @@ -0,0 +1,90 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_WEIGHTEDDEFUZZIFIER_H +#define FL_WEIGHTEDDEFUZZIFIER_H + +#include "fl/defuzzifier/Defuzzifier.h" + +namespace fl { + class Activated; + + /** + The WeightedDefuzzifier class is the base class for defuzzifiers which + compute a weighted function on the fuzzy set without requiring to + integrate over the fuzzy set. + + @author Juan Rada-Vilela, Ph.D. + @since 5.0 + */ + class FL_API WeightedDefuzzifier : public Defuzzifier { + public: + + /**The Type enum indicates the type of the WeightedDefuzzifier based + the terms included in the fuzzy set.*/ + enum Type { + /**Automatic: Automatically inferred from the terms */ + Automatic, + /**TakagiSugeno: Manually set to TakagiSugeno (or Inverse Tsukamoto)*/ + TakagiSugeno, + /**Tsukamoto: Manually set to Tsukamoto*/ + Tsukamoto + }; + /** + Returns a string representation of the given type + @param type is the type of a defuzzifier + @return a string representation of the given type + */ + static std::string typeName(Type type); + private: + Type _type; + + public: + explicit WeightedDefuzzifier(Type type = Automatic); + explicit WeightedDefuzzifier(const std::string& type); + virtual ~WeightedDefuzzifier() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(WeightedDefuzzifier) + + /** + Sets the type of the weighted defuzzifier + @param type is the type of the weighted defuzzifier + */ + void setType(Type type); + /** + Gets the type of the weighted defuzzifier + @return the type of the weighted defuzzifier + */ + Type getType() const; + /** + Returns a string representation of the type of the defuzzifier + @return a string representation of the type of the defuzzifier + */ + virtual std::string getTypeName() const; + /** + Infers the type of the defuzzifier based on the given term. If the + given term is Constant, Linear or Function, then the type is + TakagiSugeno; otherwise, the type is Tsukamoto + + @param term is the given term + @return the inferred type of the defuzzifier based on the given term + */ + virtual Type inferType(const Term* term) const; + + }; +} + +#endif /* FL_WEIGHTEDDEFUZZIFIER_H */ + diff --git a/fuzzylite/fl/defuzzifier/WeightedSum.h b/fuzzylite/fl/defuzzifier/WeightedSum.h new file mode 100644 index 0000000..fb61ef3 --- /dev/null +++ b/fuzzylite/fl/defuzzifier/WeightedSum.h @@ -0,0 +1,71 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_WEIGHTEDSUM_H +#define FL_WEIGHTEDSUM_H + + +#include "fl/defuzzifier/WeightedDefuzzifier.h" + +namespace fl { + + /** + The WeightedSum class is a WeightedDefuzzifier that computes the + weighted sum of a fuzzy set represented in an Aggregated Term. + + @author Juan Rada-Vilela, Ph.D. + @see WeightedSumCustom + @see WeightedAverage + @see WeightedAverageCustom + @see WeightedDefuzzifier + @see Defuzzifier + @since 4.0 + */ + class FL_API WeightedSum : public WeightedDefuzzifier { + public: + explicit WeightedSum(Type type = Automatic); + explicit WeightedSum(const std::string& type); + virtual ~WeightedSum() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(WeightedSum) + + virtual std::string className() const FL_IOVERRIDE; + + virtual Complexity complexity(const Term* term) const FL_IOVERRIDE; + + /** + Computes the weighted sum of the given fuzzy set represented as an + Aggregated Term as @f$y = \sum_i{w_iz_i} @f$, + where @f$w_i@f$ is the activation degree of term @f$i@f$, and @f$z_i + = \mu_i(w_i) @f$. + + From version 6.0, the implication and aggregation operators are not + utilized for defuzzification. + + @param term is the fuzzy set represented as an AggregatedTerm + @param minimum is the minimum value of the range (only used for Tsukamoto) + @param maximum is the maximum value of the range (only used for Tsukamoto) + @return the weighted sum of the given fuzzy set + */ + virtual scalar defuzzify(const Term* term, + scalar minimum, scalar maximum) const FL_IOVERRIDE; + virtual WeightedSum* clone() const FL_IOVERRIDE; + + static Defuzzifier* constructor(); + }; +} + +#endif /* FL_WEIGHTEDSUM_H */ + diff --git a/fuzzylite/fl/defuzzifier/WeightedSumCustom.h b/fuzzylite/fl/defuzzifier/WeightedSumCustom.h new file mode 100644 index 0000000..c055d1e --- /dev/null +++ b/fuzzylite/fl/defuzzifier/WeightedSumCustom.h @@ -0,0 +1,78 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_WEIGHTEDSUMCUSTOM_H +#define FL_WEIGHTEDSUMCUSTOM_H + + +#include "fl/defuzzifier/WeightedDefuzzifier.h" + +namespace fl { + + /** + The (experimental) WeightedSumCustom class is a WeightedDefuzzifier that computes the + weighted sum of a fuzzy set represented in an Aggregated Term utilizing + the fuzzy operators for implication and aggregation to compute the weighted + sum. This is an experimental approach to take advantage of customization + thanks to the object-oriented design. + + @author Juan Rada-Vilela, Ph.D. + @see WeightedSum + @see WeightedAverage + @see WeightedAverageCustom + @see WeightedDefuzzifier + @see Defuzzifier + @since 6.0 + @experimental + */ + class FL_API WeightedSumCustom : public WeightedDefuzzifier { + public: + explicit WeightedSumCustom(Type type = Automatic); + explicit WeightedSumCustom(const std::string& type); + virtual ~WeightedSumCustom() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(WeightedSumCustom) + + virtual std::string className() const FL_IOVERRIDE; + + virtual Complexity complexity(const Term* term) const FL_IOVERRIDE; + + /** + Computes the weighted sum of the given fuzzy set represented in an + Aggregated Term as @f$y = \sum_i{w_iz_i} @f$, + where @f$w_i@f$ is the activation degree of term @f$i@f$, and @f$z_i + = \mu_i(w_i) @f$. + + If the implication and aggregation operators are set to fl::null (or + set to AlgebraicProduct and UnboundedSum, respectively), then the + operation of WeightedAverageCustom is the same as the WeightedAverage. + Otherwise, the implication and aggregation operators are utilized to + compute the multiplications and sums in @f$y@f$, respectively. + + @param term is the fuzzy set represented as an AggregatedTerm + @param minimum is the minimum value of the range (only used for Tsukamoto) + @param maximum is the maximum value of the range (only used for Tsukamoto) + @return the weighted sum of the given fuzzy set + */ + virtual scalar defuzzify(const Term* term, + scalar minimum, scalar maximum) const FL_IOVERRIDE; + virtual WeightedSumCustom* clone() const FL_IOVERRIDE; + + static Defuzzifier* constructor(); + }; +} + +#endif /* FL_WEIGHTEDSUMCUSTOM_H */ + diff --git a/fuzzylite/fl/factory/ActivationFactory.h b/fuzzylite/fl/factory/ActivationFactory.h new file mode 100644 index 0000000..6ebfc73 --- /dev/null +++ b/fuzzylite/fl/factory/ActivationFactory.h @@ -0,0 +1,46 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_ACTIVATIONFACTORY_H +#define FL_ACTIVATIONFACTORY_H + +#include "fl/fuzzylite.h" + +#include "fl/factory/ConstructionFactory.h" +#include "fl/activation/Activation.h" + +namespace fl { + + /** + The ActivationFactory class is a ConstructionFactory of Activation + methods for RuleBlock%s. + + @author Juan Rada-Vilela, Ph.D. + @see Activation + @see RuleBlock + @see ConstructionFactory + @see FactoryManager + @since 6.0 + */ + class FL_API ActivationFactory : public ConstructionFactory<Activation*> { + public: + ActivationFactory(); + virtual ~ActivationFactory() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(ActivationFactory) + }; +} + +#endif /* FL_ACTIVATIONFACTORY_H */ diff --git a/fuzzylite/fl/factory/CloningFactory.h b/fuzzylite/fl/factory/CloningFactory.h new file mode 100644 index 0000000..a21e1d2 --- /dev/null +++ b/fuzzylite/fl/factory/CloningFactory.h @@ -0,0 +1,226 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_CLONINGFACTORY_H +#define FL_CLONINGFACTORY_H + +#include "fl/fuzzylite.h" + +#include <map> +#include <string> +#include <vector> + +namespace fl { + + /** + The CloningFactory class is the base class for a factory whose objects + are created from a registered object by calling the `clone()` method. + + @param <T> is the class of the object to be cloned + + @author Juan Rada-Vilela, Ph.D. + @see FactoryManager + @since 5.0 + */ + + template <typename T> + class CloningFactory { + private: + std::string _name; + std::map<std::string, T> _objects; + + public: + explicit CloningFactory(const std::string& name = ""); + CloningFactory(const CloningFactory& other); + CloningFactory& operator=(const CloningFactory& other); + virtual ~CloningFactory(); + FL_DEFAULT_MOVE(CloningFactory) + + /** + Returns the name of the factory + @return the name of the factory + */ + virtual std::string name() const; + + /** + Registers the object in the factory and assumes its ownership + @param key is the unique name by which objects are registered + @param object is the object to be cloned via a `clone` method + */ + virtual void registerObject(const std::string& key, T object); + /** + Deregisters the given object from the factory and deletes it + @param key is the unique name by which objects are registered + */ + virtual void deregisterObject(const std::string& key); + /** + Checks whether the factory has the given object registered + @param key is the unique name by which objects are registered + @return whether the factory has the given object registered + */ + virtual bool hasObject(const std::string& key) const; + /** + Gets the object registered by the given key, not a clone of the object + @param key is the unique name by which objects are registered + @return the object registered by the given key + */ + virtual T getObject(const std::string& key) const; + /** + Creates a cloned object by executing the clone method on the registered object + @param key is the unique name by which objects are registered + @return a cloned object by executing the clone method on the registered object + */ + virtual T cloneObject(const std::string& key) const; + /** + Returns a vector of the available objects + @return a vector of the available objects + */ + virtual std::vector<std::string> available() const; + /** + Gets the map of registered keys and objects + @return the map of registered keys and objects + */ + virtual std::map<std::string, T>& objects(); + /** + Gets an immutable map of registered keys and objects + @return an immutable map of registered keys and objects + */ + virtual const std::map<std::string, T>& objects() const; + + }; +} + +/** + Template implementation + */ + +#include "fl/Exception.h" + +namespace fl { + + template<typename T> + inline CloningFactory<T>::CloningFactory(const std::string& name) : _name(name) { + + } + + template<typename T> + inline CloningFactory<T>::CloningFactory(const CloningFactory& other) { + typename std::map<std::string, T>::const_iterator it = other._objects.begin(); + while (it != other._objects.end()) { + T clone = fl::null; + if (it->second) clone = it->second->clone(); + this->_objects[it->first] = clone; + ++it; + } + } + + template<typename T> + inline CloningFactory<T>& CloningFactory<T>::operator=(const CloningFactory& other) { + if (this != &other) { + typename std::map<std::string, T>::const_iterator it = this->_objects.begin(); + while (it != this->_objects.end()) { + if (it->second) delete it->second; + ++it; + } + this->_objects.clear(); + + it = other._objects.begin(); + while (it != other._objects.end()) { + T clone = fl::null; + if (it->second) clone = it->second->clone(); + this->_objects[it->first] = clone; + ++it; + } + } + return *this; + } + + template<typename T> + inline CloningFactory<T>::~CloningFactory() { + typename std::map<std::string, T>::const_iterator it = this->_objects.begin(); + while (it != this->_objects.end()) { + if (it->second) delete it->second; + ++it; + } + } + + template<typename T> + inline std::string CloningFactory<T>::name() const { + return this->_name; + } + + template<typename T> + inline void CloningFactory<T>::registerObject(const std::string& key, T object) { + this->_objects[key] = object; + } + + template<typename T> + inline void CloningFactory<T>::deregisterObject(const std::string& key) { + typename std::map<std::string, T>::iterator it = this->_objects.find(key); + if (it != this->_objects.end()) { + this->_objects.erase(it); + delete it->second; + } + } + + template<typename T> + inline bool CloningFactory<T>::hasObject(const std::string& key) const { + typename std::map<std::string, T>::const_iterator it = this->_objects.find(key); + return (it != this->_objects.end()); + } + + template<typename T> + inline T CloningFactory<T>::getObject(const std::string& key) const { + typename std::map<std::string, T>::const_iterator it = this->_objects.find(key); + if (it != this->_objects.end()) { + if (it->second) return it->second; + } + return fl::null; + } + + template<typename T> + inline T CloningFactory<T>::cloneObject(const std::string& key) const { + typename std::map<std::string, T>::const_iterator it = this->_objects.find(key); + if (it != this->_objects.end()) { + if (it->second) return it->second->clone(); + return fl::null; + } + throw Exception("[cloning error] " + _name + " object by name <" + key + "> not registered", FL_AT); + } + + template<typename T> + inline std::vector<std::string> CloningFactory<T>::available() const { + std::vector<std::string> result; + typename std::map<std::string, T>::const_iterator it = this->_objects.begin(); + while (it != this->_objects.end()) { + result.push_back(it->first); + } + return result; + } + + template<typename T> + inline std::map<std::string, T>& CloningFactory<T>::objects() { + return this->_objects; + } + + template<typename T> + inline const std::map<std::string, T>& CloningFactory<T>::objects() const { + return this->_objects; + } +} + +#endif /* FL_CLONINGFACTORY_H */ + diff --git a/fuzzylite/fl/factory/ConstructionFactory.h b/fuzzylite/fl/factory/ConstructionFactory.h new file mode 100644 index 0000000..2558d7f --- /dev/null +++ b/fuzzylite/fl/factory/ConstructionFactory.h @@ -0,0 +1,202 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_CONSTRUCTIONFACTORY_H +#define FL_CONSTRUCTIONFACTORY_H + +#include "fl/fuzzylite.h" + +#include <map> +#include <string> +#include <vector> + +namespace fl { + + /** + The ConstructionFactory class is the base class for a factory whose + objects are created from a registered ConstructionFactory::Constructor. + + @author Juan Rada-Vilela, Ph.D. + @see FactoryManager + @since 5.0 + */ + + template <typename T> + class ConstructionFactory { + public: + /** + The Constructor type definition refers to a zero-parameter method + which returns an instance of T + */ + typedef T(*Constructor)(); + + private: + std::string _name; + std::map<std::string, Constructor> _constructors; + + public: + explicit ConstructionFactory(const std::string& name); + virtual ~ConstructionFactory(); + FL_DEFAULT_COPY_AND_MOVE(ConstructionFactory) + + /** + Returns the name of the factory + @return the name of the factory + */ + virtual std::string name() const; + + /** + Registers the constructor in the factory + @param key is the unique name by which constructors are registered + @param constructor is the pointer to the constructor of the object + */ + virtual void registerConstructor(const std::string& key, Constructor constructor); + /** + Deregisters from the factory the constructor associated to the given key + @param key is the unique name by which constructors are registered + */ + virtual void deregisterConstructor(const std::string& key); + /** + Checks whether the factory has a constructor registered by the given key + @param key is the unique name by which constructors are registered + @return whether the factory has the given constructor registered + */ + virtual bool hasConstructor(const std::string& key) const; + /** + Gets the constructor registered by the given key + @param key is the unique name by which constructors are registered + @return the pointer to the given constructor + */ + virtual Constructor getConstructor(const std::string& key) const; + /** + Creates an object by executing the constructor associated to the given key + @param key is the unique name by which constructors are registered + @return an object by executing the constructor associated to the given key + */ + virtual T constructObject(const std::string& key) const; + /** + Returns a vector of keys for the constructors available + @return a vector of keys for the constructors available + */ + virtual std::vector<std::string> available() const; + /** + Gets the map of registered keys and constructors + @return the map of registered keys and constructors + */ + virtual std::map<std::string, Constructor>& constructors(); + /** + Gets an immutable map of registered keys and constructors + @return an immutable map of registered keys and constructors + */ + virtual const std::map<std::string, Constructor>& constructors() const; + }; + +} + +/** + * Template implementation + */ + + +#include "fl/Exception.h" +#include "fl/defuzzifier/Defuzzifier.h" +#include "fl/hedge/Hedge.h" +#include "fl/norm/SNorm.h" +#include "fl/norm/TNorm.h" +#include "fl/term/Term.h" + +namespace fl { + + template <typename T> + inline ConstructionFactory<T>::ConstructionFactory(const std::string& name) : _name(name) { + + } + + template <typename T> + inline ConstructionFactory<T>::~ConstructionFactory() { + } + + template<typename T> + inline std::string ConstructionFactory<T>::name() const { + return this->_name; + } + + template <typename T> + inline void ConstructionFactory<T>::registerConstructor(const std::string& key, Constructor constructor) { + this->_constructors[key] = constructor; + } + + template <typename T> + inline void ConstructionFactory<T>::deregisterConstructor(const std::string& key) { + typename std::map<std::string, Constructor>::iterator it = this->_constructors.find(key); + if (it != this->_constructors.end()) { + this->_constructors.erase(it); + } + } + + template <typename T> + inline bool ConstructionFactory<T>::hasConstructor(const std::string& key) const { + typename std::map<std::string, Constructor>::const_iterator it = this->_constructors.find(key); + return (it != this->_constructors.end()); + } + + template <typename T> + inline typename ConstructionFactory<T>::Constructor ConstructionFactory<T>::getConstructor(const std::string& key) const { + typename std::map<std::string, Constructor>::const_iterator it = this->_constructors.find(key); + if (it != this->_constructors.end()) { + return it->second; + } + return fl::null; + } + + template <typename T> + inline T ConstructionFactory<T>::constructObject(const std::string& key) const { + typename std::map<std::string, Constructor>::const_iterator it = this->_constructors.find(key); + if (it != this->_constructors.end()) { + if (it->second) { + return it->second(); + } + return fl::null; + } + std::ostringstream ss; + ss << "[factory error] constructor of " + _name + " <" << key << "> not registered"; + throw Exception(ss.str(), FL_AT); + } + + template <typename T> + inline std::vector<std::string> ConstructionFactory<T>::available() const { + std::vector<std::string> result; + typename std::map<std::string, Constructor>::const_iterator it = this->_constructors.begin(); + while (it != this->_constructors.end()) { + result.push_back(it->first); + ++it; + } + return result; + } + + template<typename T> + inline std::map<std::string, typename ConstructionFactory<T>::Constructor>& ConstructionFactory<T>::constructors() { + return this->_constructors; + } + + template<typename T> + inline const std::map<std::string, typename ConstructionFactory<T>::Constructor>& ConstructionFactory<T>::constructors() const { + return this->_constructors; + } +} + +#endif /* FL_CONSTRUCTIONFACTORY_H */ + diff --git a/fuzzylite/fl/factory/DefuzzifierFactory.h b/fuzzylite/fl/factory/DefuzzifierFactory.h new file mode 100644 index 0000000..8fa4c1b --- /dev/null +++ b/fuzzylite/fl/factory/DefuzzifierFactory.h @@ -0,0 +1,75 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_DEFUZZIFIERFACTORY_H +#define FL_DEFUZZIFIERFACTORY_H + +#include "fl/factory/ConstructionFactory.h" + +#include "fl/defuzzifier/Defuzzifier.h" +#include "fl/defuzzifier/IntegralDefuzzifier.h" +#include "fl/defuzzifier/WeightedDefuzzifier.h" + +namespace fl { + + /** + The DefuzzifierFactory class is a ConstructionFactory of Defuzzifier%s. + + @author Juan Rada-Vilela, Ph.D. + @see Defuzzifier + @see ConstructionFactory + @see FactoryManager + @since 4.0 + */ + class FL_API DefuzzifierFactory : public ConstructionFactory<Defuzzifier*> { + public: + DefuzzifierFactory(); + virtual ~DefuzzifierFactory() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(DefuzzifierFactory) + + /** + Creates a Defuzzifier by executing the registered constructor + @param key is the unique name by which constructors are registered + @param resolution is the resolution of an IntegralDefuzzifier + @param type is the type of a WeightedDefuzzifier + @return a Defuzzifier by executing the registered constructor and + setting its resolution or type accordingly + */ + virtual Defuzzifier* constructDefuzzifier(const std::string& key, + int resolution, WeightedDefuzzifier::Type type) const; + + /** + Creates a Defuzzifier by executing the registered constructor + @param key is the unique name by which constructors are registered + @param resolution is the resolution of an IntegralDefuzzifier + @return a Defuzzifier by executing the registered constructor and + setting its resolution + */ + virtual Defuzzifier* constructDefuzzifier(const std::string& key, int resolution) const; + + /** + Creates a Defuzzifier by executing the registered constructor + @param key is the unique name by which constructors are registered + @param type is the type of a WeightedDefuzzifier + @return a Defuzzifier by executing the registered constructor and + setting its type + */ + virtual Defuzzifier* constructDefuzzifier(const std::string& key, WeightedDefuzzifier::Type type); + }; +} + +#endif /* DEFUZZIFIERFACTORY_H */ + diff --git a/fuzzylite/fl/factory/FactoryManager.h b/fuzzylite/fl/factory/FactoryManager.h new file mode 100644 index 0000000..79f7996 --- /dev/null +++ b/fuzzylite/fl/factory/FactoryManager.h @@ -0,0 +1,154 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_FACTORYMANAGER_H +#define FL_FACTORYMANAGER_H + +#include "fl/fuzzylite.h" + +#include "fl/factory/TNormFactory.h" +#include "fl/factory/SNormFactory.h" +#include "fl/factory/ActivationFactory.h" +#include "fl/factory/DefuzzifierFactory.h" +#include "fl/factory/TermFactory.h" +#include "fl/factory/HedgeFactory.h" +#include "fl/factory/FunctionFactory.h" + +namespace fl { + + /** + The FactoryManager class is a central class grouping different factories + of objects, together with a singleton instance to access each of the + factories throughout the library. + + + @author Juan Rada-Vilela, Ph.D. + @see TermFactory + @see TNormFactory + @see SNormFactory + @see HedgeFactory + @see ActivationFactory + @see DefuzzifierFactory + @see FunctionFactory + @since 4.0 + */ + class FL_API FactoryManager { + private: + FL_unique_ptr<TNormFactory> _tnorm; + FL_unique_ptr<SNormFactory> _snorm; + FL_unique_ptr<ActivationFactory> _activation; + FL_unique_ptr<DefuzzifierFactory> _defuzzifier; + FL_unique_ptr<TermFactory> _term; + FL_unique_ptr<HedgeFactory> _hedge; + FL_unique_ptr<FunctionFactory> _function; + + public: + FactoryManager(); + explicit FactoryManager(TNormFactory* tnorm, SNormFactory* snorm, + ActivationFactory* activation, DefuzzifierFactory* defuzzifier, + TermFactory* term, HedgeFactory* hedge, FunctionFactory* function); + explicit FactoryManager(const FactoryManager& other); + FactoryManager& operator=(const FactoryManager& other); + FL_DEFAULT_MOVE(FactoryManager) + virtual ~FactoryManager(); + + /** + Gets the static instance of the manager + @return the static instance of the manager + */ + static FactoryManager* instance(); + + /** + Sets the factory of TNorm%s + @param tnorm is the factory of TNorm%s + */ + virtual void setTnorm(TNormFactory* tnorm); + /** + Gets the factory of TNorm%s + @return the factory of TNorm%s + */ + virtual TNormFactory* tnorm() const; + + /** + Sets the factory of SNorm%s + @param snorm is the factory of SNorm%s + */ + virtual void setSnorm(SNormFactory* snorm); + /** + Gets the factory of SNorm%s + @return the factory of SNorm%s + */ + virtual SNormFactory* snorm() const; + + /** + Sets the factory of Activation methods + @param activation is the factory of Activation methods + */ + virtual void setActivation(ActivationFactory* activation); + /** + Gets the factory of Activation methods + @return the factory of Activation methods + */ + virtual ActivationFactory* activation() const; + + /** + Sets the factory of Defuzzifier%s + @param defuzzifier is the factory of Defuzzifier%s + */ + virtual void setDefuzzifier(DefuzzifierFactory* defuzzifier); + /** + Gets the factory of Defuzzifier%s + @return the factory of Defuzzifier%s + */ + virtual DefuzzifierFactory* defuzzifier() const; + + /** + Sets the factory of Term%s + @param term is the factory of Term%s + */ + virtual void setTerm(TermFactory* term); + /** + Gets the factory of Term%s + @return the factory of Term%s + */ + virtual TermFactory* term() const; + + /** + Sets the factory of Hedge%s + @param hedge is the factory of Hedge%s + */ + virtual void setHedge(HedgeFactory* hedge); + /** + Gets the factory of Hedge%s + @return the factory of Hedge%s + */ + virtual HedgeFactory* hedge() const; + + /** + Sets the factory of Function Element%s + @param function is the factory of Function Element%s + */ + virtual void setFunction(FunctionFactory* function); + /** + Gets the factory of Function Element%s + @return the factory of Function Element%s + */ + virtual FunctionFactory* function() const; + }; +} + +#endif /* FL_FACTORYMANAGER_H */ + diff --git a/fuzzylite/fl/factory/FunctionFactory.h b/fuzzylite/fl/factory/FunctionFactory.h new file mode 100644 index 0000000..84e0104 --- /dev/null +++ b/fuzzylite/fl/factory/FunctionFactory.h @@ -0,0 +1,61 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_FUNCTIONFACTORY_H +#define FL_FUNCTIONFACTORY_H + +#include "fl/factory/CloningFactory.h" + +#include "fl/term/Function.h" + +namespace fl { + + /** + The FunctionFactory class is a CloningFactory of operators and functions + utilized by the Function term. + + @author Juan Rada-Vilela, Ph.D. + @see Function + @see Element + @see CloningFactory + @see FactoryManager + @since 5.0 + */ + class FL_API FunctionFactory : public CloningFactory<Function::Element*> { + private: + void registerOperators(); + void registerFunctions(); + public: + FunctionFactory(); + virtual ~FunctionFactory() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(FunctionFactory) + + /** + Returns a vector of the operators available + @return a vector of the operators available + */ + virtual std::vector<std::string> availableOperators() const; + /** + Returns a vector of the functions available + @return a vector of the functions available + */ + virtual std::vector<std::string> availableFunctions() const; + + }; +} + +#endif /* FL_FUNCTIONFACTORY_H */ + diff --git a/fuzzylite/fl/factory/HedgeFactory.h b/fuzzylite/fl/factory/HedgeFactory.h new file mode 100644 index 0000000..e4733b2 --- /dev/null +++ b/fuzzylite/fl/factory/HedgeFactory.h @@ -0,0 +1,44 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_HEDGEFACTORY_H +#define FL_HEDGEFACTORY_H + +#include "fl/factory/ConstructionFactory.h" + +#include "fl/hedge/Hedge.h" + +namespace fl { + + /** + The HedgeFactory class is a ConstructionFactory of Hedge%s. + + @author Juan Rada-Vilela, Ph.D. + @see Hedge + @see ConstructionFactory + @see FactoryManager + @since 4.0 + */ + class FL_API HedgeFactory : public ConstructionFactory<Hedge*> { + public: + HedgeFactory(); + virtual ~HedgeFactory() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(HedgeFactory) + }; +} + +#endif /* FL_HEDGEFACTORY_H */ + diff --git a/fuzzylite/fl/factory/SNormFactory.h b/fuzzylite/fl/factory/SNormFactory.h new file mode 100644 index 0000000..111664a --- /dev/null +++ b/fuzzylite/fl/factory/SNormFactory.h @@ -0,0 +1,44 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_SNORMFACTORY_H +#define FL_SNORMFACTORY_H + +#include "fl/factory/ConstructionFactory.h" + +#include "fl/norm/SNorm.h" + +namespace fl { + + /** + The SNormFactory class is a ConstructionFactory of SNorm%s. + + @author Juan Rada-Vilela, Ph.D. + @see SNorm + @see ConstructionFactory + @see FactoryManager + @since 4.0 + */ + class FL_API SNormFactory : public ConstructionFactory<SNorm*> { + public: + SNormFactory(); + virtual ~SNormFactory() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(SNormFactory) + }; +} + +#endif /* FL_SNORMFACTORY_H */ + diff --git a/fuzzylite/fl/factory/TNormFactory.h b/fuzzylite/fl/factory/TNormFactory.h new file mode 100644 index 0000000..5093fa7 --- /dev/null +++ b/fuzzylite/fl/factory/TNormFactory.h @@ -0,0 +1,44 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_TNORMFACTORY_H +#define FL_TNORMFACTORY_H + +#include "fl/factory/ConstructionFactory.h" + +#include "fl/norm/TNorm.h" + +namespace fl { + + /** + The TNormFactory class is a ConstructionFactory of TNorm%s. + + @author Juan Rada-Vilela, Ph.D. + @see TNorm + @see ConstructionFactory + @see FactoryManager + @since 4.0 + */ + class FL_API TNormFactory : public ConstructionFactory<TNorm*> { + public: + TNormFactory(); + virtual ~TNormFactory() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(TNormFactory) + }; +} + +#endif /* FL_TNORMFACTORY_H */ + diff --git a/fuzzylite/fl/factory/TermFactory.h b/fuzzylite/fl/factory/TermFactory.h new file mode 100644 index 0000000..f6a5d60 --- /dev/null +++ b/fuzzylite/fl/factory/TermFactory.h @@ -0,0 +1,45 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_TERMFACTORY_H +#define FL_TERMFACTORY_H + + +#include "fl/factory/ConstructionFactory.h" + +#include "fl/term/Term.h" + +namespace fl { + + /** + The TermFactory class is a ConstructionFactory of Term%s. + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see ConstructionFactory + @see FactoryManager + @since 4.0 + */ + class FL_API TermFactory : public ConstructionFactory<Term*> { + public: + TermFactory(); + virtual ~TermFactory() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(TermFactory) + }; +} + +#endif /* FL_TERMFACTORY_H */ + diff --git a/fuzzylite/fl/fuzzylite.h b/fuzzylite/fl/fuzzylite.h new file mode 100644 index 0000000..3cca94b --- /dev/null +++ b/fuzzylite/fl/fuzzylite.h @@ -0,0 +1,427 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_FUZZYLITE_H +#define FL_FUZZYLITE_H + +#include <algorithm> +#include <cmath> +#include <iostream> +#include <sstream> +#include <limits> +#include <memory> +#include <cstddef> + +#ifndef FL_BUILD_PATH +#define FL_BUILD_PATH "" +#endif + +#if defined(_WIN32) || defined(WIN32) +#define FL_WINDOWS +#endif + +#if defined(unix) || defined(__unix__) || defined(__unix) +#define FL_UNIX +#endif + +#ifdef __APPLE__ +#define FL_APPLE +#ifndef FL_UNIX +#define FL_UNIX +#endif +#endif + +#define FL__FILE__ std::string(__FILE__).substr(std::string(FL_BUILD_PATH).size()) + +#define FL_LOG_PREFIX FL__FILE__ << " (" << __LINE__ << "):" + +#define FL_AT FL__FILE__, __LINE__, __FUNCTION__ + +#define FL_LOG(message) {if (fl::fuzzylite::isLogging()){std::cout << FL_LOG_PREFIX << message << std::endl;}} +#define FL_LOGP(message) {if (fl::fuzzylite::isLogging()){std::cout << message << std::endl;}} + +#define FL_DEBUG_BEGIN if (fl::fuzzylite::isDebugging()){ +#define FL_DEBUG_END } + +#define FL_DBG(message) FL_DEBUG_BEGIN\ + std::cout << FL__FILE__ << "::" << __FUNCTION__ << "[" << __LINE__ << "]:" \ + << message << std::endl;\ + FL_DEBUG_END + + +#ifdef FL_WINDOWS +#include <ciso646> //alternative operator spellings: +//#define and && +//#define or || +//#define not ! +//#define bitand & +//#define bitor | + +//@todo: Address warning 4251 by exporting members? +//http://www.unknownroad.com/rtfm/VisualStudio/warningC4251.html +#ifdef _MSC_VER +#pragma warning (disable:4251) +#endif + +//fuzzylite as a shared library is exported +//Applications linking with fuzzylite as a shared library need to import + +//fuzzylite as a static library does not export or import +//Applications linking with fuzzylite as a static library do not import + +#if defined(FL_EXPORT_LIBRARY) +#define FL_API __declspec(dllexport) +#elif defined(FL_IMPORT_LIBRARY) +#define FL_API __declspec(dllimport) +#else +#define FL_API +#endif + +#else +#define FL_API +#endif + +/** + The fl namespace is the namespace where all the classes of the `fuzzylite` + library are contained in. + + @author Juan Rada-Vilela, Ph.D. + @since 4.0 + */ +namespace fl { + /** + Represents floating-point values (typedef to float or double). + */ +#ifdef FL_USE_FLOAT + typedef float scalar; +#else + /** + Represents floating-point values as doubles. + */ + typedef double scalar; +#endif + +#define FL_IUNUSED(x) (void) (x) + +#ifdef __GNUC__ +#define FL_IUNUSED_DECL __attribute__((unused)) +#else +#define FL_IUNUSED_DECL +#endif + + /** + Represents the Not-A-Number scalar value + */ + const scalar nan FL_IUNUSED_DECL = std::numeric_limits<scalar>::quiet_NaN(); + /** + Represents the infinity scalar value + */ + const scalar inf FL_IUNUSED_DECL = std::numeric_limits<scalar>::infinity(); + +#ifdef FL_CPP98 + //C++98 defines + + //Pointers + /** + Represents the `C++11` or `C++98` null pointer depending on whether the + compilation flag `-DFL_CPP98` is set + */ + const long null = 0L; +#define FL_unique_ptr std::auto_ptr +#define FL_move_ptr(x) x + + //Identifiers +#define FL_IOVERRIDE +#define FL_IFINAL +#define FL_IDEFAULT +#define FL_IDELETE +#define FL_INOEXCEPT throw() +#define FL_ITHREAD_LOCAL + + //Constructors +#define FL_DEFAULT_COPY(Class) +#define FL_DEFAULT_MOVE(Class) +#define FL_DEFAULT_COPY_AND_MOVE(Class) + +#define FL_DISABLE_COPY(Class) \ + Class(const Class &);\ + Class &operator=(const Class &); + +#else + //C++11 defines + + //Pointers + /** + Represents the `C++11` or `C++98` null pointer depending on whether the + compilation flag `-DFL_CPP98` is set + */ + const std::nullptr_t null = nullptr; +#define FL_unique_ptr std::unique_ptr +#define FL_move_ptr(x) std::move(x) + + //Identifiers +#define FL_IOVERRIDE override +#define FL_IFINAL final +#define FL_IDEFAULT = default +#define FL_IDELETE = delete +#define FL_INOEXCEPT noexcept +#define FL_ITHREAD_LOCAL /*thread_local (commented for performance)*/ + + //Constructors +#define FL_DEFAULT_COPY(Class) \ + Class(const Class&) = default; \ + Class& operator=(const Class&) = default; +#define FL_DEFAULT_MOVE(Class) \ + Class(Class&&) = default; \ + Class& operator=(Class&&) = default; +#define FL_DEFAULT_COPY_AND_MOVE(Class) \ + Class(const Class&) = default; \ + Class& operator=(const Class&) = default;\ + Class(Class&&) = default; \ + Class& operator=(Class&&) = default; + +#define FL_DISABLE_COPY(Class) \ + Class(const Class &) = delete;\ + Class &operator=(const Class &) = delete; + +#endif + +} + + +namespace fl { + + /** + + The fuzzylite class contains global settings and information about the + library. + + @author Juan Rada-Vilela, Ph.D. + @since 4.0 + + */ + + class FL_API fuzzylite { + friend class Operation; + private: + static int _decimals; + static scalar _macheps; + static std::ios_base::fmtflags _scalarFormat; + static bool _logging; + static bool _debugging; + public: + /** + Returns the name of the `fuzzylite` library + @return the name of the `fuzzylite` library + */ + static std::string name(); + /** + Returns the version of the `fuzzylite` library + @return the version of the `fuzzylite` library + */ + static std::string version(); + /** + Returns the name of the `fuzzylite` library including the version + @return the name of the `fuzzylite` library including the version + */ + static std::string library(); + + /** + Returns the license under which the `fuzzylite` library is released + @return the license under which the `fuzzylite` library is released + */ + static std::string license(); + + /** + Returns the name of the author of the `fuzzylite` library + @return "Juan Rada-Vilela, Ph.D." + */ + static std::string author(); + + /** + Returns the name of the company that owns the `fuzzylite` library + @return "FuzzyLite Limited" + */ + static std::string company(); + + /** + Returns the website of the `fuzzylite` library + @return "http://www.fuzzylite.com/" + */ + static std::string website(); + + /** + Returns the number of decimals utilized when formatting scalar values + @return the number of decimals utilized when formatting scalar values + (default is 3) + */ + static int decimals(); + + /** + Sets the number of decimals utilized when formatting scalar values + @param decimals is the number of decimals utilized when formatting + scalar values + */ + static void setDecimals(int decimals); + + /** + Returns the minimum difference at which two floating-point values + are considered equivalent + @return the minimum difference at which two floating-point values + are considered equivalent (default is 1e-6) + */ + static scalar macheps(); + + /** + Sets the minimum difference at which two floating-point values are + considered equivalent + @param macheps is the minimum difference at which two floating-point + values are considered equivalent (default is 1e-6) + */ + static void setMachEps(scalar macheps); + + /** + Sets the default format to be utilized for every fl::scalar passed to + Op::str() + @param scalarFormat is the format to be utilized for every fl::scalar + passed to Op::str() + */ + static void setScalarFormat(std::ios_base::fmtflags scalarFormat); + + /** + Gets the default format to be utilized for every fl::scalar passed to + Op::str() + @return the format to be utilized for every fl::scalar passed to Op::str() + */ + static std::ios_base::fmtflags scalarFormat(); + + /** + Returns whether the library is logging information via the `FL_LOG` + macro + @return whether the library is logging information via the `FL_LOG` + macro + */ + static bool isLogging(); + + /** + Sets whether the library is set to log information using the macro + `FL_LOG` + @param logging indicates whether the library is set to log + information via the `FL_LOG` macro + */ + static void setLogging(bool logging); + + /** + Indicates whether the library is running in debug mode + @return `true` if the library is running in debug mode, and `false` + if it is running in release mode + */ + static bool isDebugging(); + + /** + Sets whether the library is set to run in debug mode + @param debugging indicates whether the library is set to run in debug mode + */ + static void setDebugging(bool debugging); + + /** + Returns the platform under which the `fuzzylite` library was built + @return `Unix` or `Windows` + */ + static std::string platform(); + + /** + Returns the name of the type of the floating-point variables + @return `double` or `float` + */ + static std::string floatingPoint(); + }; +} + + +namespace fl { + + inline std::string fuzzylite::name() { + return "fuzzylite"; + } + + inline std::string fuzzylite::library() { + return name() + " " + version(); + } + + inline std::string fuzzylite::version() { + return "6.0"; + } + + inline std::string fuzzylite::license() { + return "FuzzyLite License"; + } + + inline std::string fuzzylite::author() { + return "Juan Rada-Vilela, Ph.D."; + } + + inline std::string fuzzylite::company() { + return "FuzzyLite Limited"; + } + + inline std::string fuzzylite::website() { + return "http://www.fuzzylite.com/"; + } + + inline void fuzzylite::setDebugging(bool debugging) { + _debugging = debugging; + } + + inline bool fuzzylite::isDebugging() { + return _debugging; + } + + inline void fuzzylite::setDecimals(int decimals) { + _decimals = decimals; + } + + inline int fuzzylite::decimals() { + return _decimals; + } + + inline void fuzzylite::setScalarFormat(std::ios_base::fmtflags scalarFormat) { + _scalarFormat = scalarFormat; + } + + inline std::ios_base::fmtflags fuzzylite::scalarFormat() { + return _scalarFormat; + } + + inline void fuzzylite::setMachEps(scalar macheps) { + _macheps = macheps; + } + + inline scalar fuzzylite::macheps() { + return _macheps; + } + + inline void fuzzylite::setLogging(bool logging) { + _logging = logging; + } + + inline bool fuzzylite::isLogging() { + return _logging; + } +} + +#endif /* FL_FUZZYLITE_H */ + diff --git a/fuzzylite/fl/hedge/Any.h b/fuzzylite/fl/hedge/Any.h new file mode 100644 index 0000000..bf71842 --- /dev/null +++ b/fuzzylite/fl/hedge/Any.h @@ -0,0 +1,60 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_ANY_H +#define FL_ANY_H + +#include "fl/hedge/Hedge.h" + +namespace fl { + + /** + The Any class is a special Hedge that always returns `1.0`. Its + position with respect to the other hedges is last in the ordered set + (Not, Seldom, Somewhat, Very, Extremely, Any). The Antecedent of a Rule + considers Any to be a syntactically special hedge because it is not + followed by a Term (e.g., `if Variable is any then...`). Amongst hedges, + only Any has virtual methods to be overriden due to its particular case. + + @author Juan Rada-Vilela, Ph.D. + @see Hedge + @see HedgeFactory + @since 4.0 + */ + class FL_API Any : public Hedge { + public: + Any(); + virtual ~Any() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(Any) + + virtual std::string name() const FL_IOVERRIDE; + + + virtual Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the hedge for the given value + @param x is irrelevant + @return `1.0` + */ + virtual scalar hedge(scalar x) const FL_IOVERRIDE; + virtual Any* clone() const FL_IOVERRIDE; + + static Hedge* constructor(); + }; +} + +#endif /* FL_ANY_H */ diff --git a/fuzzylite/fl/hedge/Extremely.h b/fuzzylite/fl/hedge/Extremely.h new file mode 100644 index 0000000..fbd617a --- /dev/null +++ b/fuzzylite/fl/hedge/Extremely.h @@ -0,0 +1,55 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_EXTREMELY_H +#define FL_EXTREMELY_H + +#include "fl/hedge/Hedge.h" + +namespace fl { + + /** + The Extremely class is a Hedge located fifth in the ordered set + (Not, Seldom, Somewhat, Very, Extremely, Any). + + @author Juan Rada-Vilela, Ph.D. + @see Hedge + @see HedgeFactory + @since 4.0 + */ + class FL_API Extremely FL_IFINAL : public Hedge { + public: + std::string name() const FL_IOVERRIDE; + + Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the hedge for the membership function value @f$x@f$ + @param x is a membership function value + @return @f$ + \begin{cases} + 2x^2 & \mbox{if $x \le 0.5$} \cr + 1-2(1-x)^2 & \mbox{otherwise} \cr + \end{cases}@f$ + */ + scalar hedge(scalar x) const FL_IOVERRIDE; + Extremely* clone() const FL_IOVERRIDE; + + static Hedge* constructor(); + }; +} + +#endif /* FL_EXTREMELY_H */ diff --git a/fuzzylite/fl/hedge/Hedge.h b/fuzzylite/fl/hedge/Hedge.h new file mode 100644 index 0000000..9dac17c --- /dev/null +++ b/fuzzylite/fl/hedge/Hedge.h @@ -0,0 +1,77 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_HEDGE_H +#define FL_HEDGE_H + +#include "fl/fuzzylite.h" + +#include "fl/Complexity.h" + +#include <string> + +namespace fl { + + /** + The Hedge class is the abstract class for hedges. Hedges are utilized + within the Antecedent and Consequent of a Rule in order to modify the + membership function of a linguistic Term. + + @author Juan Rada-Vilela, Ph.D. + @see Antecedent + @see Consequent + @see Rule + @see HedgeFactory + @since 4.0 + */ + class FL_API Hedge { + public: + + Hedge() { + } + + virtual ~Hedge() { + } + FL_DEFAULT_COPY_AND_MOVE(Hedge) + + /** + Returns the name of the hedge + @return the name of the hedge + */ + virtual std::string name() const = 0; + + /** + Computes the estimated complexity of applying the hedge + @return the estimated complexity of applying the hedge + */ + virtual Complexity complexity() const = 0; + /** + Computes the hedge for the membership function value @f$x@f$ + @param x is a membership function value + @return the hedge of @f$x@f$ + */ + virtual scalar hedge(scalar x) const = 0; + + /** + Creates a clone of the hedge + @return a clone of the hedge. + */ + virtual Hedge* clone() const = 0; + + }; +} + +#endif /* FL_HEDGE_H */ diff --git a/fuzzylite/fl/hedge/HedgeFunction.h b/fuzzylite/fl/hedge/HedgeFunction.h new file mode 100644 index 0000000..bc8930d --- /dev/null +++ b/fuzzylite/fl/hedge/HedgeFunction.h @@ -0,0 +1,82 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_HEDGEFUNCTION_H +#define FL_HEDGEFUNCTION_H + +#include "fl/hedge/Hedge.h" + +#include "fl/term/Function.h" + +namespace fl { + + /** + The HedgeFunction class is a customizable Hedge via Function, which + computes any function based on the @f$x@f$ value. This hedge is not + registered with the HedgeFactory due to issues configuring the formula + within. To register the hedge, a static method with the + constructor needs to be manually created and registered. Please, check the + file `test/hedge/HedgeFunction.cpp` for further details. + + @author Juan Rada-Vilela, Ph.D. + @see Function + @see Hedge + @see HedgeFactory + @since 6.0 + */ + + class FL_API HedgeFunction FL_IFINAL : public Hedge { + private: + Function _function; + public: + explicit HedgeFunction(const std::string& formula = ""); + + std::string name() const FL_IOVERRIDE; + + /** + Returns the reference to the Function + @return the reference to the Function + */ + Function& function(); + + /** + Loads the function with the given formula + @param formula is a valid formula in infix notation + */ + void setFormula(const std::string& formula); + /** + Returns the formula loaded into the function + @return the formula loaded into the function + */ + std::string getFormula() const; + + Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the hedge for the membership function value @f$x@f$ utilizing + the given function via HedgeFunction::setFormula() + @param x is a membership function value + @return the evaluation of the function + */ + scalar hedge(scalar x) const FL_IOVERRIDE; + HedgeFunction* clone() const FL_IOVERRIDE; + + static Hedge* constructor(); + }; +} + +#endif /* FL_HEDGEFUNCTION_H */ + diff --git a/fuzzylite/fl/hedge/Not.h b/fuzzylite/fl/hedge/Not.h new file mode 100644 index 0000000..e981ac9 --- /dev/null +++ b/fuzzylite/fl/hedge/Not.h @@ -0,0 +1,51 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_NOT_H +#define FL_NOT_H + +#include "fl/hedge/Hedge.h" + +namespace fl { + + /** + The Not class is a Hedge located first in the ordered set + (Not, Seldom, Somewhat, Very, Extremely, Any). + + @author Juan Rada-Vilela, Ph.D. + @see Hedge + @see HedgeFactory + @since 4.0 + */ + class FL_API Not FL_IFINAL : public Hedge { + public: + std::string name() const FL_IOVERRIDE; + + Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the hedge for the membership function value @f$x@f$ + @param x is a membership function value + @return @f$1-x@f$ + */ + scalar hedge(scalar x) const FL_IOVERRIDE; + Not* clone() const FL_IOVERRIDE; + + static Hedge* constructor(); + }; +} + +#endif /* FL_NOT_H */ diff --git a/fuzzylite/fl/hedge/Seldom.h b/fuzzylite/fl/hedge/Seldom.h new file mode 100644 index 0000000..0bbedbf --- /dev/null +++ b/fuzzylite/fl/hedge/Seldom.h @@ -0,0 +1,56 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_SELDOM_H +#define FL_SELDOM_H + +#include "fl/hedge/Hedge.h" + +namespace fl { + + /** + The Seldom class is a Hedge located second in the ordered set + (Not, Seldom, Somewhat, Very, Extremely, Any). + + @author Juan Rada-Vilela, Ph.D. + @see Hedge + @see HedgeFactory + @since 4.0 + */ + class FL_API Seldom FL_IFINAL : public Hedge { + public: + std::string name() const FL_IOVERRIDE; + + Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the hedge for the membership function value @f$x@f$ + @param x is a membership function value + @return @f$ + \begin{cases} + \sqrt{0.5x} & \mbox{if $x \le 0.5$} \cr + 1-\sqrt{0.5(1-x)} & \mbox{otherwise}\cr + \end{cases} + @f$ + */ + scalar hedge(scalar x) const FL_IOVERRIDE; + Seldom* clone() const FL_IOVERRIDE; + + static Hedge* constructor(); + }; +} + +#endif /* FL_SELDOM_H */ diff --git a/fuzzylite/fl/hedge/Somewhat.h b/fuzzylite/fl/hedge/Somewhat.h new file mode 100644 index 0000000..ccd21ec --- /dev/null +++ b/fuzzylite/fl/hedge/Somewhat.h @@ -0,0 +1,52 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_SOMEWHAT_H +#define FL_SOMEWHAT_H + +#include "fl/hedge/Hedge.h" + +namespace fl { + + /** + The Somewhat class is a Hedge located third in the ordered set + (Not, Seldom, Somewhat, Very, Extremely, Any). + + @author Juan Rada-Vilela, Ph.D. + @see Hedge + @see HedgeFactory + @since 4.0 + */ + class FL_API Somewhat FL_IFINAL : public Hedge { + public: + std::string name() const FL_IOVERRIDE; + + + Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the hedge for the membership function value @f$x@f$ + @param x is a membership function value + @return @f$\sqrt{x}@f$ + */ + scalar hedge(scalar x) const FL_IOVERRIDE; + Somewhat* clone() const FL_IOVERRIDE; + + static Hedge* constructor(); + }; +} + +#endif /* FL_SOMEWHAT_H */ diff --git a/fuzzylite/fl/hedge/Very.h b/fuzzylite/fl/hedge/Very.h new file mode 100644 index 0000000..b80c994 --- /dev/null +++ b/fuzzylite/fl/hedge/Very.h @@ -0,0 +1,50 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_VERY_H +#define FL_VERY_H + +#include "fl/hedge/Hedge.h" + +namespace fl { + + /** + The Very class is a Hedge located fourth in the ordered set + (Not, Seldom, Somewhat, Very, Extremely, Any). + + @author Juan Rada-Vilela, Ph.D. + @see Hedge + @see HedgeFactory + @since 4.0 + */ + class FL_API Very FL_IFINAL : public Hedge { + public: + std::string name() const FL_IOVERRIDE; + + Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the hedge for the membership function value @f$x@f$ + @param x is a membership function value + @return @f$x^2@f$ + */ + scalar hedge(scalar x) const FL_IOVERRIDE; + Very* clone() const FL_IOVERRIDE; + + static Hedge* constructor(); + }; +} +#endif /* FL_VERY_H */ diff --git a/fuzzylite/fl/imex/CppExporter.h b/fuzzylite/fl/imex/CppExporter.h new file mode 100644 index 0000000..1c48225 --- /dev/null +++ b/fuzzylite/fl/imex/CppExporter.h @@ -0,0 +1,161 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_CPPEXPORTER_H +#define FL_CPPEXPORTER_H + +#include "fl/imex/Exporter.h" + +namespace fl { + class Engine; + class InputVariable; + class OutputVariable; + class Term; + class RuleBlock; + class Norm; + class Defuzzifier; + class Hedge; + class Activation; + + /** + The CppExporter class is an Exporter that translates an Engine and its + components to the `C++` programming language using the `fuzzylite` + library. + + @author Juan Rada-Vilela, Ph.D. + @see JavaExporter + @see Exporter + @since 4.0 + */ + class FL_API CppExporter : public Exporter { + private: + bool _usingNamespace; + bool _usingVariableNames; + public: + explicit CppExporter(bool usingNamespace = false, bool usingVariableNames = true); + virtual ~CppExporter() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(CppExporter) + + virtual std::string name() const FL_IOVERRIDE; + virtual std::string toString(const Engine* engine) const FL_IOVERRIDE; + + /** + Sets whether the fl namespace of the library is prepended to types + (e.g., fl::Engine) + + @param usingNamespace whether the fl namespace of the library is + prepended to types (e.g., fl::Engine) + */ + virtual void setUsingNamespace(bool usingNamespace); + /** + Gets whether the fl namespace of the library is prepended to types + (e.g., fl::Engine) + @return whether the fl namespace of the library is prepended to types + */ + virtual bool isUsingNamespace() const; + + /** + Returns the given text prepended with the `fl` namespace if + CppExporter::isUsingNamespace is `true`, or the text otherwise + + @param clazz is the text to be prepended the `fl::`. + @return the given text prepended with the `fl` namespace if + CppExporter::isUsingNamespace is `true`, or the text otherwise + */ + virtual std::string fl(const std::string& clazz) const; + /** + Sets whether variables are exported using their names + (e.g., `power->setValue(fl::nan)`) instead of numbered identifiers + (e.g., `inputVariable1->setValue(fl::nan)`) + @param usingVariableNames indicates whether variables are exported using + their names + */ + virtual void setUsingVariableNames(bool usingVariableNames); + /** + Gets whether variables are exported using their names + (e.g., `power->setValue(fl::nan)`) instead of numbered identifiers + (e.g., `inputVariable1->setValue(fl::nan)`) + @return whether variables are exported using their names + */ + virtual bool isUsingVariableNames() const; + + /** + Returns a string representation of InputVariable in the `C++` programming language + @param inputVariable is the input variable + @param engine is the engine in which the input variable is registered + @return a string representation of the input variable in the `C++` programming language + */ + virtual std::string toString(const InputVariable* inputVariable, const Engine* engine) const; + /** + Returns a string representation of the OutputVariable in the `C++` programming language + @param outputVariable is the output variable + @param engine is the engine in which the output variable is registered + @return a string representation of the output variable in the `C++` programming language + */ + virtual std::string toString(const OutputVariable* outputVariable, const Engine* engine) const; + /** + Returns a string representation of the RuleBlock in the `C++` programming language + @param ruleBlock is the rule block + @param engine is the engine in which the rule block is registered + @return a string representation of the rule block in the `C++` programming language + */ + virtual std::string toString(const RuleBlock* ruleBlock, const Engine* engine) const; + + /** + Returns a string representation of the Activation method in the `C++` programming language + @param activation is the activation method + @return a string representation of the activation method in the `C++` programming language + */ + virtual std::string toString(const Activation* activation) const; + + /** + Returns a string representation of the scalar value in the `C++` programming language + @param value is the scalar value + @return a string representation of the scalar value in the `C++` programming language + */ + virtual std::string toString(scalar value) const; + + /** + Returns a string representation of the Hedge in the `C++` programming language + @param hedge is the hedge + @return a string representation of the hedge in the `C++` programming language + */ + virtual std::string toString(const Hedge* hedge) const; + /** + Returns a string representation of the Term in the `C++` programming language + @param term is the term + @return a string representation of the term in the `C++` programming language + */ + virtual std::string toString(const Term* term) const; + /** + Returns a string representation of the Norm in the `C++` programming language + @param norm is the norm + @return a string representation of the norm in the `C++` programming language + */ + virtual std::string toString(const Norm* norm) const; + /** + Returns a string representation of the Defuzzifier in the `C++` programming language + @param defuzzifier is the defuzzifier + @return a string representation of the defuzzifier in the `C++` programming language + */ + virtual std::string toString(const Defuzzifier* defuzzifier) const; + + virtual CppExporter* clone() const FL_IOVERRIDE; + + }; +} +#endif /* FL_CPPEXPORTER_H */ + diff --git a/fuzzylite/fl/imex/Exporter.h b/fuzzylite/fl/imex/Exporter.h new file mode 100644 index 0000000..b883edf --- /dev/null +++ b/fuzzylite/fl/imex/Exporter.h @@ -0,0 +1,74 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_EXPORTER_H +#define FL_EXPORTER_H + +#include "fl/fuzzylite.h" + +#include <string> + +namespace fl { + class Engine; + + /** + The Exporter class is the abstract class for exporters to translate an + Engine into different formats. + + @todo declare methods for exporting other components (e.g., Variable) + + @author Juan Rada-Vilela, Ph.D. + @see Importer + @since 4.0 + */ + class FL_API Exporter { + public: + + Exporter(); + virtual ~Exporter(); + FL_DEFAULT_COPY_AND_MOVE(Exporter) + + /** + Returns a string representation of the engine + @param engine is the engine to export + @return a string representation of the engine + */ + virtual std::string toString(const Engine* engine) const = 0; + /** + Stores the string representation of the engine into the specified file + @param path is the full path of the file to export the engine to + @param engine is the engine to export + @throws fl::Exception if the file cannot be created + */ + virtual void toFile(const std::string& path, const Engine* engine) const; + + /** + Returns the name of the exporter + @return the name of the exporter + */ + virtual std::string name() const = 0; + + /** + Creates a clone of the exporter + @return a clone of the exporter + */ + virtual Exporter* clone() const = 0; + }; + +} + +#endif /* FL_EXPORTER_H */ + diff --git a/fuzzylite/fl/imex/FclExporter.h b/fuzzylite/fl/imex/FclExporter.h new file mode 100644 index 0000000..efec3c4 --- /dev/null +++ b/fuzzylite/fl/imex/FclExporter.h @@ -0,0 +1,105 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_FCLEXPORTER_H +#define FL_FCLEXPORTER_H + +#include "fl/imex/Exporter.h" + +namespace fl { + class InputVariable; + class OutputVariable; + class RuleBlock; + class Norm; + class TNorm; + class SNorm; + class Defuzzifier; + class Term; + + /** + The FclExporter class is an Exporter that translates an Engine and its + components to the Fuzzy Control Language specification. + + @author Juan Rada-Vilela, Ph.D. + @see FclImporter + @see Exporter + @since 4.0 + */ + class FL_API FclExporter : public Exporter { + private: + std::string _indent; + + public: + explicit FclExporter(const std::string& indent = " "); + virtual ~FclExporter() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(FclExporter) + + /** + Sets the indentation string within blocks + @param indent is the indentation string within blocks + */ + virtual void setIndent(const std::string& indent); + /** + Gets the indentation string within blocks + @return the indentation string within blocks + */ + virtual std::string getIndent() const; + + virtual std::string name() const FL_IOVERRIDE; + virtual std::string toString(const Engine* engine) const FL_IOVERRIDE; + + /** + Returns a string representation of the InputVariable according to the Fuzzy Control Language specification + @param variable is the input variable + @return a string representation of the input variable according to the Fuzzy Control Language specification + */ + virtual std::string toString(const InputVariable* variable) const; + /** + Returns a string representation of the OutputVariable according to the Fuzzy Control Language specification + @param variable is the output variable + @return a string representation of the output variable according to the Fuzzy Control Language specification + */ + virtual std::string toString(const OutputVariable* variable) const; + /** + Returns a string representation of the RuleBlock according to the Fuzzy Control Language specification + @param ruleBlock is the rule block + @return a string representation of the rule block according to the Fuzzy Control Language specification + */ + virtual std::string toString(const RuleBlock* ruleBlock) const; + /** + Returns a string representation of the Norm according to the Fuzzy Control Language specification + @param norm is the norm + @return a string representation of the norm according to the Fuzzy Control Language specification + */ + virtual std::string toString(const Norm* norm) const; + /** + Returns a string representation of the Defuzzifier according to the Fuzzy Control Language specification + @param defuzzifier is the defuzzifier + @return a string representation of the defuzzifier according to the Fuzzy Control Language specification + */ + virtual std::string toString(const Defuzzifier* defuzzifier) const; + /** + Returns a string representation of the Term according to the Fuzzy Control Language specification + @param term is the term + @return a string representation of the term according to the Fuzzy Control Language specification + */ + virtual std::string toString(const Term* term) const; + + virtual FclExporter* clone() const FL_IOVERRIDE; + }; +} + +#endif /* FL_FCLEXPORTER_H */ diff --git a/fuzzylite/fl/imex/FclImporter.h b/fuzzylite/fl/imex/FclImporter.h new file mode 100644 index 0000000..2e75432 --- /dev/null +++ b/fuzzylite/fl/imex/FclImporter.h @@ -0,0 +1,74 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_FCLIMPORTER_H +#define FL_FCLIMPORTER_H + +#include "fl/imex/Importer.h" + +#include <string> +#include <utility> +#include <vector> + + +namespace fl { + class Norm; + class TNorm; + class SNorm; + class Term; + class Defuzzifier; + + /** + The FclImporter class is an Importer that configures an Engine and its + components utilizing the Fuzzy Control Language specification. + + @author Juan Rada-Vilela, Ph.D. + @see FclExporter + @see Importer + @since 4.0 + */ + class FL_API FclImporter : public Importer { + public: + FclImporter(); + virtual ~FclImporter() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(FclImporter) + + virtual std::string name() const FL_IOVERRIDE; + + virtual Engine* fromString(const std::string& fcl) const FL_IOVERRIDE; + + virtual FclImporter* clone() const FL_IOVERRIDE; + + protected: + virtual void processBlock(const std::string& tag, const std::string& block, Engine* engine) const; + virtual void processVar(const std::string& var, const std::string& block, Engine* engine)const; + virtual void processFuzzify(const std::string& block, Engine* engine)const; + virtual void processDefuzzify(const std::string& block, Engine* engine)const; + virtual void processRuleBlock(const std::string& block, Engine* engine)const; + + virtual TNorm* parseTNorm(const std::string& line) const; + virtual SNorm* parseSNorm(const std::string& line) const; + virtual Term* parseTerm(const std::string& line, const Engine* engine) const; + + virtual Defuzzifier* parseDefuzzifier(const std::string& line) const; + virtual std::pair<scalar, bool> parseDefaultValue(const std::string& line) const; + virtual std::pair<scalar, scalar> parseRange(const std::string& line) const; + virtual std::pair<bool, bool> parseLocks(const std::string& line) const; + virtual bool parseEnabled(const std::string& line) const; + + }; +} +#endif /* FL_FCLIMPORTER_H */ diff --git a/fuzzylite/fl/imex/FisExporter.h b/fuzzylite/fl/imex/FisExporter.h new file mode 100644 index 0000000..a7f5d8c --- /dev/null +++ b/fuzzylite/fl/imex/FisExporter.h @@ -0,0 +1,121 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_FISEXPORTER_H +#define FL_FISEXPORTER_H + +#include "fl/imex/Exporter.h" + +#include <vector> + +namespace fl { + class Norm; + class TNorm; + class SNorm; + class Defuzzifier; + class Term; + class Rule; + class Proposition; + class Variable; + + /** + The FisExporter class is an Exporter that translates an Engine and its + components into the Fuzzy Inference System format for Matlab or Octave. + + @author Juan Rada-Vilela, Ph.D. + @see FisImporter + @see Exporter + @since 4.0 + */ + class FL_API FisExporter : public Exporter { + protected: + + virtual std::string translate(const std::vector<Proposition*>& propositions, + const std::vector<Variable*> variables) const; + + public: + FisExporter(); + virtual ~FisExporter() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(FisExporter) + + virtual std::string name() const FL_IOVERRIDE; + virtual std::string toString(const Engine* engine) const FL_IOVERRIDE; + + /** + Returns a string representation of the TNorm in the Fuzzy Inference System format + @param tnorm is the TNorm + @return a string representation of the TNorm in the Fuzzy Inference System format + */ + virtual std::string toString(const TNorm* tnorm) const; + + /** + Returns a string representation of the SNorm in the Fuzzy Inference System format + @param snorm is the SNorm + @return a string representation of the SNorm in the Fuzzy Inference System format + */ + virtual std::string toString(const SNorm* snorm) const; + + /** + Returns a string representation of the Defuzzifier in the Fuzzy Inference System format + @param defuzzifier is the defuzzifier + @return a string representation of the Defuzzifier in the Fuzzy Inference System format + */ + virtual std::string toString(const Defuzzifier* defuzzifier) const; + /** + Returns a string representation of the Term in the Fuzzy Inference System format + @param term is the term + @return a string representation of the term in the Fuzzy Inference System format + */ + virtual std::string toString(const Term* term) const; + + /** + Returns a string representation of the `[System]` configuration + @param engine is the engine + @return a string representation of the `[System]` configuration + */ + virtual std::string exportSystem(const Engine* engine) const; + /** + Returns a string representation of the `[Input]` configuration + @param engine is the engine + @return a string representation of the `[Input]` configuration + */ + virtual std::string exportInputs(const Engine* engine) const; + /** + Returns a string representation of the `[Output]` configuration + @param engine is the engine + @return a string representation of the `[Output]` configuration + */ + virtual std::string exportOutputs(const Engine* engine) const; + /** + Returns a string representation of the `[Rules]` configuration + @param engine is the engine + @return a string representation of the `[Rules]` configuration + */ + virtual std::string exportRules(const Engine* engine) const; + /** + Returns a string representation for the Rule in the Fuzzy Inference System format + @param rule is the rule + @param engine is the engine in which the rule is registered + @return a string representation for the rule in the Fuzzy Inference System format + */ + virtual std::string exportRule(const Rule* rule, const Engine* engine) const; + + virtual FisExporter* clone() const FL_IOVERRIDE; + }; +} + +#endif /* FL_FISEXPORTER_H */ + diff --git a/fuzzylite/fl/imex/FisImporter.h b/fuzzylite/fl/imex/FisImporter.h new file mode 100644 index 0000000..2c9ce36 --- /dev/null +++ b/fuzzylite/fl/imex/FisImporter.h @@ -0,0 +1,80 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_FISIMPORTER_H +#define FL_FISIMPORTER_H + +#include "fl/imex/Importer.h" + +#include <utility> +#include <vector> + + +namespace fl { + class Norm; + class TNorm; + class SNorm; + class Term; + class Defuzzifier; + class Variable; + + /** + The FisImporter class is an Importer that configures an Engine and its + components from utilizing the Fuzzy Inference System format for Matlab or + Octave. + + @author Juan Rada-Vilela, Ph.D. + @see FisExporter + @see Importer + @since 4.0 + */ + class FL_API FisImporter : public Importer { + public: + FisImporter(); + virtual ~FisImporter() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(FisImporter) + + virtual std::string name() const FL_IOVERRIDE; + + virtual Engine* fromString(const std::string& fcl) const FL_IOVERRIDE; + + virtual FisImporter* clone() const FL_IOVERRIDE; + + protected: + virtual void importSystem(const std::string& section, Engine* engine, + std::string& andMethod, std::string& orMethod, + std::string& impMethod, std::string& aggMethod, + std::string& defuzzMethod) const; + virtual void importInput(const std::string& section, Engine* engine) const; + virtual void importOutput(const std::string& section, Engine* engine) const; + virtual void importRules(const std::string& section, Engine* engine) const; + virtual std::string translateProposition(scalar code, Variable* variable) const; + + virtual std::string translateTNorm(const std::string& tnorm) const; + virtual std::string translateSNorm(const std::string& tnorm) const; + virtual std::string translateDefuzzifier(const std::string& defuzzifier) const; + + virtual Term* parseTerm(const std::string& line, const Engine* engine) const; + virtual Term* createInstance(const std::string& termClass, const std::string& name, + const std::vector<std::string>& params, const Engine* engine) const; + + virtual std::pair<scalar, scalar> parseRange(const std::string& range) const; + + }; +} + +#endif /* FL_FISIMPORTER_H */ + diff --git a/fuzzylite/fl/imex/FldExporter.h b/fuzzylite/fl/imex/FldExporter.h new file mode 100644 index 0000000..98a14c9 --- /dev/null +++ b/fuzzylite/fl/imex/FldExporter.h @@ -0,0 +1,248 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_FLDEXPORTER_H +#define FL_FLDEXPORTER_H + +#include "fl/imex/Exporter.h" + +#include <vector> + +namespace fl { + class Engine; + class InputVariable; + class OutputVariable; + + /** + The FldExporter class is an Exporter that evaluates an Engine and exports + its input values and output values to the FuzzyLite Dataset (FLD) format, + see [http://www.fuzzylite.com/fll-fld](http://www.fuzzylite.com/fll-fld) + for more information. + + @author Juan Rada-Vilela, Ph.D. + @see FllExporter + @see Exporter + @since 4.0 + */ + class FL_API FldExporter : public Exporter { + private: + std::string _separator; + bool _exportHeaders; + bool _exportInputValues; + bool _exportOutputValues; + public: + + /** + The ScopeOfValues refers to the scope of the equally-distributed values + to generate. + */ + enum ScopeOfValues { + /**Generates @f$n@f$ values for each variable*/ + EachVariable, + /**Generates @f$n@f$ values for all variables*/ + AllVariables + }; + explicit FldExporter(const std::string& separator = " "); + virtual ~FldExporter() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(FldExporter) + + virtual std::string name() const FL_IOVERRIDE; + + /** + Sets the separator of the dataset columns + @param separator is the separator of the dataset columns + */ + virtual void setSeparator(const std::string& separator); + /** + Gets the separator of the dataset columns + @return the separator of the dataset columns + */ + virtual std::string getSeparator() const; + + /** + Sets whether the header of the dataset is to be exported + @param exportHeaders indicates whether the header of the dataset is + to be exported + */ + virtual void setExportHeader(bool exportHeaders); + /** + Gets whether the header of the dataset is to be exported + @return whether the header of the dataset is to be exported + */ + virtual bool exportsHeader() const; + + /** + Sets whether the values of the input variables are to be exported + @param exportInputValues indicates whether the values of the input + variables are to be exported + */ + virtual void setExportInputValues(bool exportInputValues); + /** + Gets whether the values of the input variables are to be exported + @return whether the values of the input variables are to be exported + */ + virtual bool exportsInputValues() const; + + /** + Sets whether the values of the output variables are to be exported + @param exportOutputValues indicates whether the values of the output + variables are to be exported + */ + virtual void setExportOutputValues(bool exportOutputValues); + /** + Gets whether the values of the output variables are to be exported + @return whether the values of the output variables are to be exported + */ + virtual bool exportsOutputValues() const; + + /** + Gets the header of the dataset for the given engine + @param engine is the engine to be exported + @return the header of the dataset for the given engine + */ + virtual std::string header(const Engine* engine) const; + + /** + Returns a FuzzyLite Dataset from the engine. Please consider that the + engine will be `const_cast`ed to achieve so; that is, despite being + marked as `const`, the engine will be modified in order to compute + the output values based on the input values. + @param engine is the engine to export + @return a FuzzyLite Dataset from the engine + */ + virtual std::string toString(const Engine* engine) const FL_IOVERRIDE; + /** + Returns a FuzzyLite Dataset from the engine. + @param engine is the engine to export + @param values is the number of values to export + @param scope indicates the scope of the values + @return a FuzzyLite Dataset from the engine + */ + virtual std::string toString(Engine* engine, int values, ScopeOfValues scope = AllVariables) const; + + /** + Returns a FuzzyLite Dataset from the engine. + @param engine is the engine to export + @param values is the number of values to export + @param scope indicates the scope of the values + @param activeVariables contains the input variables to generate values for. + The input variables must be in the same order as in the engine. A value of + fl::null indicates the variable is not active. + @return a FuzzyLite Dataset from the engine + */ + virtual std::string toString(Engine* engine, int values, ScopeOfValues scope, + const std::vector<InputVariable*>& activeVariables) const; + /** + Returns a FuzzyLite Dataset from the engine. + @param engine is the engine to export + @param reader is the reader of a set of lines containing space-separated + input values + @return a FuzzyLite Dataset from the engine + */ + virtual std::string toString(Engine* engine, std::istream& reader) const; + + + using Exporter::toFile; + /** + Saves the engine as a FuzzyLite Dataset into the specified file + @param path is the full path of the file + @param engine is the engine to export + @param values is the number of values to export + @param scope indicates the scope of the values + */ + virtual void toFile(const std::string& path, Engine* engine, + int values, ScopeOfValues scope = AllVariables) const; + /** + Saves the engine as a FuzzyLite Dataset into the specified file + @param path is the full path of the file + @param engine is the engine to export + @param values is the number of values to export + @param scope indicates the scope of the values + @param activeVariables contains the input variables to generate values for. + The input variables must be in the same order as in the engine. A value of + fl::null indicates the variable is not active. + */ + virtual void toFile(const std::string& path, Engine* engine, + int values, ScopeOfValues scope, + const std::vector<InputVariable*>& activeVariables) const; + /** + Saves the engine as a FuzzyLite Dataset into the specified file + @param path is the full path of the file + @param engine is the engine to export + @param reader is the reader of a set of lines containing space-separated input values + */ + virtual void toFile(const std::string& path, Engine* engine, std::istream& reader) const; + + /** + Parses the string into a vector of values unless the string starts with `#` + @param values is a space-separated set of values + @return a vector of values + */ + virtual std::vector<scalar> parse(const std::string& values) const; + + /** + Writes the engine into the given writer + @param engine is the engine to export + @param writer is the output where the engine will be written to + @param values is the number of values to export + @param scope indicates the scope of the values + */ + virtual void write(Engine* engine, std::ostream& writer, int values, + ScopeOfValues scope = AllVariables) const; + /** + Writes the engine into the given writer + @param engine is the engine to export + @param writer is the output where the engine will be written to + @param values is the number of values to export + @param scope indicates the scope of the values + @param activeVariables contains the input variables to generate values for. + The input variables must be in the same order as in the engine. A value of + fl::null indicates the variable is not active. + */ + virtual void write(Engine* engine, std::ostream& writer, int values, ScopeOfValues scope, + const std::vector<InputVariable*>& activeVariables) const; + /** + Writes the engine into the given writer + @param engine is the engine to export + @param writer is the output where the engine will be written to + @param reader is the reader of a set of lines containing space-separated input values + */ + virtual void write(Engine* engine, std::ostream& writer, std::istream& reader) const; + /** + Writes the engine into the given writer + @param engine is the engine to export + @param writer is the output where the engine will be written to + @param inputValues is the vector of input values + */ + virtual void write(Engine* engine, std::ostream& writer, const std::vector<scalar>& inputValues) const; + /** + Writes the engine into the given writer + @param engine is the engine to export + @param writer is the output where the engine will be written to + @param inputValues is the vector of input values + @param activeVariables contains the input variables to generate values for. + The input variables must be in the same order as in the engine. A value of + fl::null indicates the variable is not active. + */ + virtual void write(Engine* engine, std::ostream& writer, const std::vector<scalar>& inputValues, + const std::vector<InputVariable*>& activeVariables) const; + + virtual FldExporter* clone() const FL_IOVERRIDE; + }; +} + +#endif /* FL_FLDEXPORTER_H */ + diff --git a/fuzzylite/fl/imex/FllExporter.h b/fuzzylite/fl/imex/FllExporter.h new file mode 100644 index 0000000..5a222e4 --- /dev/null +++ b/fuzzylite/fl/imex/FllExporter.h @@ -0,0 +1,168 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_FLLEXPORTER_H +#define FL_FLLEXPORTER_H + +#include "fl/imex/Exporter.h" + +#include <vector> + +namespace fl { + class Variable; + class InputVariable; + class OutputVariable; + class RuleBlock; + class Rule; + class Norm; + class Activation; + class Defuzzifier; + class Term; + + /** + The FllExporter class is an Exporter that translates an Engine and its + components to the FuzzyLite Language (FLL), see + [http://www.fuzzylite.com/fll-fld](http://www.fuzzylite.com/fll-fld) for + more information. + + @author Juan Rada-Vilela, Ph.D. + @see FllImporter + @see Exporter + @since 4.0 + */ + class FL_API FllExporter : public Exporter { + private: + std::string _indent; + std::string _separator; + public: + explicit FllExporter(const std::string& indent = " ", const std::string& separator = "\n"); + virtual ~FllExporter() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(FllExporter) + + virtual std::string name() const FL_IOVERRIDE; + + /** + Sets the indent string of the FuzzyLite Language + @param indent is the indent string of the FuzzyLite Language + */ + virtual void setIndent(const std::string& indent); + /** + Gets the indent string of the FuzzyLite Language + @return the indent string of the FuzzyLite Language + */ + virtual std::string getIndent() const; + + /** + Gets the separator of the FuzzyLite Language + @param separator of the FuzzyLite Language + */ + virtual void setSeparator(const std::string& separator); + /** + Gets the separator of the FuzzyLite Language + @return the separator of the FuzzyLite Language + */ + virtual std::string getSeparator() const; + + virtual std::string toString(const Engine* engine) const FL_IOVERRIDE; + + /** + Returns a string representation of the vector of variables in the FuzzyLite Language + @param variables is the vector of variables + @return a string representation of the vector of variables in the FuzzyLite Language + */ + virtual std::string toString(const std::vector<Variable*>& variables) const; + /** + Returns a string representation of the vector of input variables in the FuzzyLite Language + @param inputVariables is the vector of input variables + @return a string representation of the vector of input variables in the FuzzyLite Language + */ + virtual std::string toString(const std::vector<InputVariable*>& inputVariables) const; + /** + Returns a string representation of the vector of output variables in the FuzzyLite Language + @param outputVariables is a vector of output variables + @return a string representation of the vector of output variables in the FuzzyLite Language + */ + virtual std::string toString(const std::vector<OutputVariable*>& outputVariables) const; + /** + Returns a string representation of the vector of rule blocks in the FuzzyLite Language + @param ruleBlocks is the vector of rule blocks + @return a string representation of the vector of rule blocks in the FuzzyLite Language + */ + virtual std::string toString(const std::vector<RuleBlock*>& ruleBlocks) const; + + /** + Returns a string representation of the Variable in the FuzzyLite Language + @param variable is the variable + @return a string representation of the variable in the FuzzyLite Language + */ + virtual std::string toString(const Variable* variable) const; + /** + Returns a string representation of the InputVariable in the FuzzyLite Language + @param inputVariable is the input variable to export + @return a string representation of the input variable in the FuzzyLite Language + */ + virtual std::string toString(const InputVariable* inputVariable) const; + /** + Returns a string representation of the OutputVariable in the FuzzyLite Language + @param outputVariable is the output variable + @return a string representation of the output variable in the FuzzyLite Language + */ + virtual std::string toString(const OutputVariable* outputVariable) const; + + /** + Returns a string representation of the RuleBlock in the FuzzyLite Language + @param ruleBlock is the rule block + @return a string representation of the rule block in the FuzzyLite Language + */ + virtual std::string toString(const RuleBlock* ruleBlock) const; + /** + Returns a string representation of the Rule in the FuzzyLite Language + @param rule is the rule + @return a string representation of the rule in the FuzzyLite Language + */ + virtual std::string toString(const Rule* rule) const; + + /** + Returns a string representation of the Norm in the FuzzyLite Language + @param norm is the norm + @return a string representation of the norm in the FuzzyLite Language + */ + virtual std::string toString(const Norm* norm) const; + /** + Returns a string representation of the Activation method in the FuzzyLite Language + @param activation is the activation method + @return a string representation of the activation method in the FuzzyLite Language + */ + virtual std::string toString(const Activation* activation) const; + /** + Returns a string representation of the Defuzzifier in the FuzzyLite Language + @param defuzzifier is the defuzzifier + @return a string representation of the defuzzifier in the FuzzyLite Language + */ + virtual std::string toString(const Defuzzifier* defuzzifier) const; + /** + Returns a string representation of the Term in the FuzzyLite Language + @param term is the term + @return a string representation of the term in the FuzzyLite Language + */ + virtual std::string toString(const Term* term) const; + + virtual FllExporter* clone() const FL_IOVERRIDE; + }; +} + +#endif /* FL_FLLEXPORTER_H */ + diff --git a/fuzzylite/fl/imex/FllImporter.h b/fuzzylite/fl/imex/FllImporter.h new file mode 100644 index 0000000..055bb88 --- /dev/null +++ b/fuzzylite/fl/imex/FllImporter.h @@ -0,0 +1,91 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_FLLIMPORTER_H +#define FL_FLLIMPORTER_H + +#include "fl/imex/Importer.h" + +#include <utility> + +namespace fl { + class TNorm; + class SNorm; + class Activation; + class Term; + class Defuzzifier; + + /** + The FllImporter class is an Importer that configures an Engine and its + components utilizing the FuzzyLite Language (FLL), see + [http://www.fuzzylite.com/fll-fld](http://www.fuzzylite.com/fll-fld) for + more information. + + @author Juan Rada-Vilela, Ph.D. + @see FllExporter + @see Importer + @since 4.0 + @todo parse methods returning respective instances from blocks of text + */ + class FL_API FllImporter : public Importer { + private: + std::string _separator; + public: + explicit FllImporter(const std::string& separator = "\n"); + virtual ~FllImporter() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(FllImporter) + + /** + Sets the separator of the language (default separator is a new line '\n') + @param separator is the separator of the language + */ + virtual void setSeparator(const std::string& separator); + /** + Gets the separator of the language (default separator is a new line '\n') + @return the separator of the language + */ + virtual std::string getSeparator() const; + + virtual std::string name() const FL_IOVERRIDE; + virtual Engine* fromString(const std::string& fll) const FL_IOVERRIDE; + + virtual FllImporter* clone() const FL_IOVERRIDE; + + protected: + + virtual void process(const std::string& tag, const std::string& block, Engine* engine) const; + virtual void processInputVariable(const std::string& block, Engine* engine) const; + virtual void processOutputVariable(const std::string& block, Engine* engine) const; + virtual void processRuleBlock(const std::string& block, Engine* engine) const; + + virtual TNorm* parseTNorm(const std::string& name) const; + virtual SNorm* parseSNorm(const std::string& name) const; + virtual Activation* parseActivation(const std::string& name) const; + + virtual Term* parseTerm(const std::string& text, Engine* engine) const; + + virtual Defuzzifier* parseDefuzzifier(const std::string& line) const; + virtual std::pair<scalar, scalar> parseRange(const std::string& line) const; + virtual bool parseBoolean(const std::string& boolean) const; + + virtual std::pair<std::string, std::string> parseKeyValue(const std::string& text, + char separator = ':') const; + + }; +} + +#endif /* FL_FLLIMPORTER_H */ + diff --git a/fuzzylite/fl/imex/Importer.h b/fuzzylite/fl/imex/Importer.h new file mode 100644 index 0000000..1250e66 --- /dev/null +++ b/fuzzylite/fl/imex/Importer.h @@ -0,0 +1,71 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_IMPORTER_H +#define FL_IMPORTER_H + +#include "fl/fuzzylite.h" + +#include <string> + +namespace fl { + class Engine; + + /** + The Importer class is the abstract class for importers to configure an + Engine and its components from different text formats. + + @todo declare methods to import specific components + + @author Juan Rada-Vilela, Ph.D. + @see Exporter + @since 4.0 + */ + class FL_API Importer { + public: + + Importer(); + virtual ~Importer(); + FL_DEFAULT_COPY_AND_MOVE(Importer) + + /** + Imports the engine from the given text + @param text is the string representation of the engine to import from + @return the engine represented by the text + */ + virtual Engine* fromString(const std::string& text) const = 0; + /** + Imports the engine from the given file + @param path is the full path of the file containing the engine to import from + @return the engine represented by the file + */ + virtual Engine* fromFile(const std::string& path) const; + + /** + Returns the name of the importer + @return the name of the importer + */ + virtual std::string name() const = 0; + /** + Creates a clone of the importer + @return a clone of the importer + */ + virtual Importer* clone() const = 0; + }; +} + +#endif /* IMPORTER_H */ + diff --git a/fuzzylite/fl/imex/JavaExporter.h b/fuzzylite/fl/imex/JavaExporter.h new file mode 100644 index 0000000..a1817f6 --- /dev/null +++ b/fuzzylite/fl/imex/JavaExporter.h @@ -0,0 +1,152 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_JAVAEXPORTER_H +#define FL_JAVAEXPORTER_H + +#include "fl/imex/Exporter.h" + +namespace fl { + + class Engine; + class InputVariable; + class OutputVariable; + class RuleBlock; + class Term; + class Defuzzifier; + class Norm; + class SNorm; + class TNorm; + class Activation; + + /** + The JavaExporter class is an Exporter that translates an Engine and its + components to the `Java` programming language using the `jfuzzylite` + library. + + @author Juan Rada-Vilela, Ph.D. + @see CppExporter + @see Exporter + @since 4.0 + */ + class FL_API JavaExporter : public Exporter { + private: + bool _usingVariableNames; + public: + explicit JavaExporter(bool usingVariableNames = true); + virtual ~JavaExporter() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(JavaExporter) + + virtual std::string name() const FL_IOVERRIDE; + + /** + Sets whether variables are exported using their names + (e.g., `power.setValue(Double.NaN)`) instead of numbered identifiers + (e.g., `inputVariable1.setValue(Double.NaN)`) + @param usingVariableNames indicates whether variables are exported using + their names + */ + virtual void setUsingVariableNames(bool usingVariableNames); + + /** + Gets whether variables are exported using their names + (e.g., `power.setValue(Double.NaN)`) instead of numbered identifiers + (e.g., `inputVariable1.setValue(Double.NaN)`) + @return whether variables are exported using their names + */ + virtual bool isUsingVariableNames() const; + + virtual std::string toString(const Engine* engine) const FL_IOVERRIDE; + /** + Returns a string representation of the InputVariable in the Java + programming language + @param inputVariable is the input variable + @param engine is the engine in which the input variable is registered + @return a string representation of the input variable in the Java + programming language + */ + virtual std::string toString(const InputVariable* inputVariable, const Engine* engine) const; + /** + Returns a string representation of the OutputVariable in the Java + programming language + @param outputVariable is the output variable + @param engine is the engine in which the output variable is registered + @return a string representation of the output variable in the Java + programming language + */ + virtual std::string toString(const OutputVariable* outputVariable, const Engine* engine) const; + /** + Returns a string representation of the RuleBlock in the Java + programming language + @param ruleBlock is the rule block + @param engine is the engine in which the rule block is registered + @return a string representation of the rule block in the Java + programming language + */ + virtual std::string toString(const RuleBlock* ruleBlock, const Engine* engine) const; + + /** + Returns a string representation of the Term in the Java programming + language + @param term is the term + @return a string representation of the term in the Java programming + language + */ + virtual std::string toString(const Term* term) const; + + /** + Returns a string representation of the Activation method in the Java + programming language + @param activation is the activation method + @return a string representation of the activation method in the Java + programming language + */ + virtual std::string toString(const Activation* activation) const; + + /** + Returns a string representation of the Defuzzifier in the Java + programming language + @param defuzzifier is the defuzzifier + @return a string representation of the defuzzifier in the Java + programming language + */ + virtual std::string toString(const Defuzzifier* defuzzifier) const; + + /** + Returns a string representation of the Norm in the Java programming + language + @param norm is the norm + @return a string representation of the norm in the Java programming + language + */ + virtual std::string toString(const Norm* norm) const; + + /** + Returns a string representation of the scalar value in the Java + programming language + @param value is the scalar value + @return a string representation of the scalar value in the Java + programming language + */ + virtual std::string toString(scalar value) const; + + virtual JavaExporter* clone() const FL_IOVERRIDE; + + }; +} + +#endif /* FL_JAVAEXPORTER_H */ + diff --git a/fuzzylite/fl/imex/RScriptExporter.h b/fuzzylite/fl/imex/RScriptExporter.h new file mode 100644 index 0000000..c53c751 --- /dev/null +++ b/fuzzylite/fl/imex/RScriptExporter.h @@ -0,0 +1,246 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_RSCRIPTEXPORTER_H +#define FL_RSCRIPTEXPORTER_H + +#include "fl/imex/Exporter.h" +#include "fl/imex/FldExporter.h" + +#include <vector> + +namespace fl { + class Engine; + class InputVariable; + class OutputVariable; + + /** + The RScriptExporter class is an Exporter that creates an R script to plot one or + more surfaces of an engine for two input variables and any number of output + variables. + + @author Juan Rada-Vilela, Ph.D. + @see FldExporter + @see Exporter + @since 6.0 + */ + class FL_API RScriptExporter : public Exporter { + private: + std::string _minimumColor; + std::string _maximumColor; + std::string _contourColor; + + public: + RScriptExporter(); + virtual ~RScriptExporter() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(RScriptExporter) + + virtual std::string name() const FL_IOVERRIDE; + + /** + Sets the color to represent the minimum values. + @param minimumColor is the color to represent the minimum values + */ + void setMinimumColor(const std::string& minimumColor); + /** + Gets the color to represent the minimum values. + @return the color to represent the minimum values + */ + std::string getMinimumColor() const; + + /** + Sets the color to represent the maximum values. + @param maximumColor is the color to represent the maximum values + */ + void setMaximumColor(const std::string& maximumColor); + /** + Gets the color to represent the maximum values. + @return maximumColor is the color to represent the maximum values + */ + std::string getMaximumColor() const; + + /** + Sets the color to draw the contour lines + @param contourColor is the color to draw the contour lines + */ + void setContourColor(const std::string& contourColor); + /** + Gets the color to draw the contour lines + @return the color to draw the contour lines + */ + std::string getContourColor() const; + + /** + Returns an R script plotting multiple surfaces based on a data frame + generated with 1024 values in the scope of FldExporter::AllVariables + for the first two input variables. + @param engine is the engine to export + @return an R script plotting multiple surfaces for the first two input + variables in the engine. + */ + virtual std::string toString(const Engine* engine) const FL_IOVERRIDE; + + /** + Returns an R script plotting multiple surfaces based on a data frame + generated with the given number of values and scope for the two input + variables. + @param engine is the engine to export + @param a is the first input variable + @param b is the second input variable + @param values is the number of values to evaluate the engine + @param scope is the scope of the number of values to evaluate the engine + @param outputVariables are the output variables to create the surface for + @return an R script plotting multiple surfaces for the two input + variables on the output variables. + */ + virtual std::string toString(Engine* engine, InputVariable* a, InputVariable* b, + int values, FldExporter::ScopeOfValues scope, + const std::vector<OutputVariable*>& outputVariables) const; + + /** + Returns an R script plotting multiple surfaces based on the input stream + of values for the two input variables. + @param engine is the engine to export + @param a is the first input variable + @param b is the second input variable + @param reader is an input stream of data whose lines contain space-separated + input values + @param outputVariables are the output variables to create the surface for + @return an R script plotting multiple surfaces for the two input + variables on the output variables + */ + virtual std::string toString(Engine* engine, InputVariable* a, InputVariable* b, + std::istream& reader, const std::vector<OutputVariable*>& outputVariables) const; + + /** + Creates an R script file plotting multiple surfaces based on a data frame + generated with 1024 values in the scope of FldExporter::AllVariables + for the two input variables + @param filePath is the full path of the R script file + @param engine is the engine to export + */ + virtual void toFile(const std::string& filePath, const Engine* engine) const FL_IOVERRIDE; + + /** + Creates an R script file plotting multiple surfaces based on a data frame + generated with the given number of values and scope for the two input + variables + @param filePath is the full path of the R script file + @param engine is the engine to export + @param a is the first input variable + @param b is the second input variable + @param values is the number of values to evaluate the engine + @param scope is the scope of the number of values to evaluate the engine + @param outputVariables are the output variables to create the surface for + */ + virtual void toFile(const std::string& filePath, Engine* engine, + InputVariable* a, InputVariable* b, + int values, FldExporter::ScopeOfValues scope, + const std::vector<OutputVariable*>& outputVariables) const; + + /** + Creates an R script file plotting multiple surfaces based on the input stream + of values for the two input variables. + @param filePath is the full path of the R script file + @param engine is the engine to export + @param a is the first input variable + @param b is the second input variable + @param reader is an input stream of data whose lines contain space-separated + input values + @param outputVariables are the output variables to create the surface for + */ + virtual void toFile(const std::string& filePath, Engine* engine, + InputVariable* a, InputVariable* b, std::istream& reader, + const std::vector<OutputVariable*>& outputVariables) const; + + + /** + Writes an R script plotting multiple surfaces based on a manually + imported data frame containing the data for the two input variables + on the output variables. + @param engine is the engine to export + @param writer is the output where the engine will be written to + @param a is the first input variable + @param b is the second input variable + @param dataFramePath is the path where the data frame should be located + (the path will not be accessed, it will only be written to script) + @param outputVariables are the output variables to create the surface for + */ + virtual void writeScriptImportingDataFrame(const Engine* engine, std::ostream& writer, + InputVariable* a, InputVariable* b, const std::string& dataFramePath, + const std::vector<OutputVariable*>& outputVariables) const; + + /** + Writes an R script plotting multiple surfaces based on a data frame + generated with the given number of values and scope for the two input + variables on the output variables. + @param engine is the engine to export + @param writer is the output where the engine will be written to + @param a is the first input variable + @param b is the second input variable + @param values is the number of values to evaluate the engine + @param scope is the scope of the number of values to evaluate the engine + @param outputVariables are the output variables to create the surface for + */ + virtual void writeScriptExportingDataFrame(Engine* engine, std::ostream& writer, + InputVariable* a, InputVariable* b, int values, FldExporter::ScopeOfValues scope, + const std::vector<OutputVariable*>& outputVariables) const; + + /** + Writes an R script plotting multiple surfaces based on a data frame + generated with the given number of values and scope for the two input + variables on the output variables. + @param engine is the engine to export + @param writer is the output where the engine will be written to + @param a is the first input variable + @param b is the second input variable + @param reader is an input stream of data whose lines contain space-separated + input values + @param outputVariables are the output variables to create the surface for + */ + virtual void writeScriptExportingDataFrame(Engine* engine, std::ostream& writer, + InputVariable* a, InputVariable* b, std::istream& reader, + const std::vector<OutputVariable*>& outputVariables) const; + + protected: + /** + Writes the header of the R script (e.g., import libraries) + @param writer is the output where the header will be written to + @param engine is the engine to export + */ + virtual void writeScriptHeader(std::ostream& writer, const Engine* engine) const; + + /** + Writes the code to generate the surface plots for the input variables + on the output variables. + @param writer is the output where the engine will be written to + @param a is the first input variable + @param b is the second input variable + @param outputVariables are the output variables to create the surface for + */ + virtual void writeScriptPlots(std::ostream& writer, + InputVariable* a, InputVariable* b, + const std::vector<OutputVariable*>& outputVariables) const; + + + virtual RScriptExporter* clone() const FL_IOVERRIDE; + + }; + +} + +#endif /* FL_RSCRIPTEXPORTER_H */ + diff --git a/fuzzylite/fl/norm/Norm.h b/fuzzylite/fl/norm/Norm.h new file mode 100644 index 0000000..682d2ad --- /dev/null +++ b/fuzzylite/fl/norm/Norm.h @@ -0,0 +1,75 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_NORM_H +#define FL_NORM_H + +#include "fl/fuzzylite.h" + +#include "fl/Complexity.h" + +#include <string> + +namespace fl { + + /** + The Norm class is the abstract class for norms. + + @author Juan Rada-Vilela, Ph.D. + @see TNorm + @see SNorm + @see TNormFactory + @see SNormFactory + @since 4.0 + */ + class FL_API Norm { + public: + + Norm() { + } + + virtual ~Norm() { + } + + FL_DEFAULT_COPY_AND_MOVE(Norm) + /** + Returns the name of the class of the norm + @return the name of the class of the norm + */ + virtual std::string className() const = 0; + + /** + Computes the estimated complexity of computing the hedge + @return the complexity of computing the hedge + */ + virtual Complexity complexity() const = 0; + /** + Computes the norm for @f$a@f$ and @f$b@f$ + @param a is a membership function value + @param b is a membership function value + @return the norm between @f$a@f$ and @f$b@f$ + */ + virtual scalar compute(scalar a, scalar b) const = 0; + + /** + Creates a clone of the norm + @return a clone of the norm + */ + virtual Norm* clone() const = 0; + + }; +} +#endif /* FL_NORM_H */ diff --git a/fuzzylite/fl/norm/SNorm.h b/fuzzylite/fl/norm/SNorm.h new file mode 100644 index 0000000..a68081c --- /dev/null +++ b/fuzzylite/fl/norm/SNorm.h @@ -0,0 +1,51 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_SNORM_H +#define FL_SNORM_H + +#include "fl/norm/Norm.h" + +namespace fl { + + /** + The SNorm class is the base class for all S-Norms, and it is utilized as + the disjunction fuzzy logic operator and as the aggregation (or + `accumulation` in versions 5.0 and earlier) fuzzy logic operator. + + @author Juan Rada-Vilela, Ph.D. + @see RuleBlock::getDisjunction() + @see OutputVariable::fuzzyOutput() + @see Aggregated::getAggregation() + @see SNormFactory + @see Norm + @since 4.0 + */ + class FL_API SNorm : public Norm { + public: + + SNorm() { + } + + virtual ~SNorm() FL_IOVERRIDE { + } + + FL_DEFAULT_COPY_AND_MOVE(SNorm) + + virtual SNorm* clone() const FL_IOVERRIDE = 0; + }; +} +#endif /* FL_SNORM_H */ diff --git a/fuzzylite/fl/norm/TNorm.h b/fuzzylite/fl/norm/TNorm.h new file mode 100644 index 0000000..b1f7cb7 --- /dev/null +++ b/fuzzylite/fl/norm/TNorm.h @@ -0,0 +1,50 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_TNORM_H +#define FL_TNORM_H + +#include "fl/norm/Norm.h" + +namespace fl { + + /** + The TNorm class is the base class for T-Norms, and it is utilized as the + conjunction fuzzy logic operator and as the implication (or `activation` + in versions 5.0 and earlier) fuzzy logic operator. + + @author Juan Rada-Vilela, Ph.D. + @see RuleBlock::getConjunction() + @see RuleBlock::getImplication() + @see TNormFactory + @see Norm + @since 4.0 + */ + class FL_API TNorm : public Norm { + public: + + TNorm() { + } + + virtual ~TNorm() FL_IOVERRIDE { + } + + FL_DEFAULT_COPY_AND_MOVE(TNorm) + + virtual TNorm* clone() const FL_IOVERRIDE = 0; + }; +} +#endif /* TNORM_H */ diff --git a/fuzzylite/fl/norm/s/AlgebraicSum.h b/fuzzylite/fl/norm/s/AlgebraicSum.h new file mode 100644 index 0000000..2fbf4c1 --- /dev/null +++ b/fuzzylite/fl/norm/s/AlgebraicSum.h @@ -0,0 +1,53 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_ALGEBRAICSUM_H +#define FL_ALGEBRAICSUM_H + +#include "fl/norm/SNorm.h" + +namespace fl { + + /** + The AlgebraicSum class is an SNorm that computes the algebraic sum of + values any two values. + + @author Juan Rada-Vilela, Ph.D. + @see AlgebraicProduct + @see SNorm + @see SNormFactory + @see Norm + @since 4.0 + */ + class FL_API AlgebraicSum FL_IFINAL : public SNorm { + public: + std::string className() const FL_IOVERRIDE; + + Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the algebraic sum of two membership function values + @param a is a membership function value + @param b is a membership function value + @return @f$a+b-(a \times b)@f$ + */ + scalar compute(scalar a, scalar b) const FL_IOVERRIDE; + AlgebraicSum* clone() const FL_IOVERRIDE; + + static SNorm* constructor(); + }; +} +#endif /* FL_ALGEBRAICSUM_H */ diff --git a/fuzzylite/fl/norm/s/BoundedSum.h b/fuzzylite/fl/norm/s/BoundedSum.h new file mode 100644 index 0000000..cd9438e --- /dev/null +++ b/fuzzylite/fl/norm/s/BoundedSum.h @@ -0,0 +1,54 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_BOUNDEDSUM_H +#define FL_BOUNDEDSUM_H + +#include "fl/norm/SNorm.h" + +namespace fl { + + /** + The BoundedSum class is an SNorm that computes the bounded sum of any two + values. + + @author Juan Rada-Vilela, Ph.D. + @see BoundedDifference + @see SNorm + @see SNormFactory + @see Norm + @since 4.0 + */ + class FL_API BoundedSum FL_IFINAL : public SNorm { + public: + std::string className() const FL_IOVERRIDE; + + Complexity complexity() const FL_IOVERRIDE; + /** + Computes the bounded sum of two membership function values + @param a is a membership function value + @param b is a membership function value + @return @f$\min(1, a+b)@f$ + */ + scalar compute(scalar a, scalar b) const FL_IOVERRIDE; + BoundedSum* clone() const FL_IOVERRIDE; + + static SNorm* constructor(); + }; + +} + +#endif /* FL_BOUNDEDSUM_H */ diff --git a/fuzzylite/fl/norm/s/DrasticSum.h b/fuzzylite/fl/norm/s/DrasticSum.h new file mode 100644 index 0000000..a1e33fe --- /dev/null +++ b/fuzzylite/fl/norm/s/DrasticSum.h @@ -0,0 +1,55 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_DRASTICSUM_H +#define FL_DRASTICSUM_H + +#include "fl/norm/SNorm.h" + +namespace fl { + + /** + The DrasticSum class is an SNorm that computes the drastic sum of any two + values. + + @author Juan Rada-Vilela, Ph.D. + @see DrasticProduct + @see SNorm + @see SNormFactory + @see Norm + @since 4.0 + */ + class FL_API DrasticSum FL_IFINAL : public SNorm { + public: + std::string className() const FL_IOVERRIDE; + + Complexity complexity() const FL_IOVERRIDE; + /** + Computes the drastic sum of two membership function values + @param a is a membership function value + @param b is a membership function value + @return @f$\begin{cases} + \max(a,b) & \mbox{if $\min(a,b)=0$} \cr + 1 & \mbox{otherwise} + \end{cases}@f$ + */ + scalar compute(scalar a, scalar b) const FL_IOVERRIDE; + DrasticSum* clone() const FL_IOVERRIDE; + + static SNorm* constructor(); + }; +} +#endif /* FL_DRASTICSUM_H */ diff --git a/fuzzylite/fl/norm/s/EinsteinSum.h b/fuzzylite/fl/norm/s/EinsteinSum.h new file mode 100644 index 0000000..7450330 --- /dev/null +++ b/fuzzylite/fl/norm/s/EinsteinSum.h @@ -0,0 +1,52 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_EINSTEINSUM_H +#define FL_EINSTEINSUM_H + +#include "fl/norm/SNorm.h" + +namespace fl { + + /** + The EinsteinSum class is an SNorm that computes the einstein sum of any + two values. + + @author Juan Rada-Vilela, Ph.D. + @see EinsteinProduct + @see SNorm + @see SNormFactory + @see Norm + @since 4.0 + */ + class FL_API EinsteinSum FL_IFINAL : public SNorm { + public: + std::string className() const FL_IOVERRIDE; + + Complexity complexity() const FL_IOVERRIDE; + /** + Computes the Einstein sum of two membership function values + @param a is a membership function value + @param b is a membership function value + @return @f$a+b/(1+a \times b)@f$ + */ + scalar compute(scalar a, scalar b) const FL_IOVERRIDE; + EinsteinSum* clone() const FL_IOVERRIDE; + + static SNorm* constructor(); + }; +} +#endif /* FL_EINSTEINSUM_H */ diff --git a/fuzzylite/fl/norm/s/HamacherSum.h b/fuzzylite/fl/norm/s/HamacherSum.h new file mode 100644 index 0000000..0242512 --- /dev/null +++ b/fuzzylite/fl/norm/s/HamacherSum.h @@ -0,0 +1,52 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_HAMACHERSUM_H +#define FL_HAMACHERSUM_H + +#include "fl/norm/SNorm.h" + +namespace fl { + + /** + The HamacherSum class is an SNorm that computes the Hamacher sum of any + two values. + + @author Juan Rada-Vilela, Ph.D. + @see HamacherProduct + @see SNorm + @see SNormFactory + @see Norm + @since 4.0 + */ + class FL_API HamacherSum FL_IFINAL : public SNorm { + public: + std::string className() const FL_IOVERRIDE; + + Complexity complexity() const FL_IOVERRIDE; + /** + Computes the Hamacher sum of two membership function values + @param a is a membership function value + @param b is a membership function value + @return @f$a+b-(2\times a \times b)/(1-a\times b)@f$ + */ + scalar compute(scalar a, scalar b) const FL_IOVERRIDE; + HamacherSum* clone() const FL_IOVERRIDE; + + static SNorm* constructor(); + }; +} +#endif /* FL_HAMACHERSUM_H */ diff --git a/fuzzylite/fl/norm/s/Maximum.h b/fuzzylite/fl/norm/s/Maximum.h new file mode 100644 index 0000000..c8ce488 --- /dev/null +++ b/fuzzylite/fl/norm/s/Maximum.h @@ -0,0 +1,51 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_MAXIMUM_H +#define FL_MAXIMUM_H + +#include "fl/norm/SNorm.h" + +namespace fl { + + /** + The Maximum class is an SNorm that computes the maximum of any two values. + + @author Juan Rada-Vilela, Ph.D. + @see Minimum + @see SNorm + @see SNormFactory + @see Norm + @since 4.0 + */ + class FL_API Maximum FL_IFINAL : public SNorm { + public: + std::string className() const FL_IOVERRIDE; + + Complexity complexity() const FL_IOVERRIDE; + /** + Computes the maximum of two membership function values + @param a is a membership function value + @param b is a membership function value + @return @f$\max(a,b)@f$ + */ + scalar compute(scalar a, scalar b) const FL_IOVERRIDE; + Maximum* clone() const FL_IOVERRIDE; + + static SNorm* constructor(); + }; +} +#endif /* FL_MAXIMUM_H */ diff --git a/fuzzylite/fl/norm/s/NilpotentMaximum.h b/fuzzylite/fl/norm/s/NilpotentMaximum.h new file mode 100644 index 0000000..13e9793 --- /dev/null +++ b/fuzzylite/fl/norm/s/NilpotentMaximum.h @@ -0,0 +1,56 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_NILPOTENTMAXIMUM_H +#define FL_NILPOTENTMAXIMUM_H + +#include "fl/norm/SNorm.h" + +namespace fl { + + /** + The NilpotentMaximum class is an SNorm that computes the nilpotent + maximum of any two values. + + @author Juan Rada-Vilela, Ph.D. + @see NilpotentMinimum + @see SNorm + @see SNormFactory + @see Norm + @since 5.0 + */ + class FL_API NilpotentMaximum FL_IFINAL : public SNorm { + public: + std::string className() const FL_IOVERRIDE; + + Complexity complexity() const FL_IOVERRIDE; + /** + Computes the nilpotent maximum of two membership function values + @param a is a membership function value + @param b is a membership function value + @return @f$\begin{cases} + \max(a,b) & \mbox{if $a+b<0$} \cr + 1 & \mbox{otherwise} + \end{cases}@f$ + */ + scalar compute(scalar a, scalar b) const FL_IOVERRIDE; + NilpotentMaximum* clone() const FL_IOVERRIDE; + + static SNorm* constructor(); + }; +} + +#endif /* FL_NILPOTENTMAXIMUM_H */ diff --git a/fuzzylite/fl/norm/s/NormalizedSum.h b/fuzzylite/fl/norm/s/NormalizedSum.h new file mode 100644 index 0000000..e2e757b --- /dev/null +++ b/fuzzylite/fl/norm/s/NormalizedSum.h @@ -0,0 +1,52 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_NORMALIZEDSUM_H +#define FL_NORMALIZEDSUM_H + +#include "fl/norm/SNorm.h" + +namespace fl { + + /** + The NormalizedSum class is an SNorm that computes the normalized sum of + any two values. + + @author Juan Rada-Vilela, Ph.D. + @see SNorm + @see SNormFactory + @see Norm + @since 4.0 + */ + class FL_API NormalizedSum FL_IFINAL : public SNorm { + public: + std::string className() const FL_IOVERRIDE; + + Complexity complexity() const FL_IOVERRIDE; + /** + Computes the normalized sum of two membership function values + @param a is a membership function value + @param b is a membership function value + @return @f$(a+b)/\max(1, a + b)@f$ + */ + scalar compute(scalar a, scalar b) const FL_IOVERRIDE; + NormalizedSum* clone() const FL_IOVERRIDE; + + static SNorm* constructor(); + }; +} + +#endif /* FL_NORMALIZEDSUM_H */ diff --git a/fuzzylite/fl/norm/s/SNormFunction.h b/fuzzylite/fl/norm/s/SNormFunction.h new file mode 100644 index 0000000..a78b1aa --- /dev/null +++ b/fuzzylite/fl/norm/s/SNormFunction.h @@ -0,0 +1,81 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_SNORMFUNCTION_H +#define FL_SNORMFUNCTION_H + +#include "fl/norm/SNorm.h" + +#include "fl/term/Function.h" + +namespace fl { + + /** + The SNormFunction class is a customizable SNorm via Function, which + computes any function based on the @f$a@f$ and @f$b@f$ values. + This SNorm is not registered with the SNormFactory. + + @author Juan Rada-Vilela, Ph.D. + @see Function + @see SNorm + @see Norm + @see SNormFactory + @since 6.0 + */ + + class FL_API SNormFunction FL_IFINAL : public SNorm { + private: + Function _function; + public: + explicit SNormFunction(const std::string& formula = ""); + + /** + Returns the reference to the Function + @return the reference to the Function + */ + Function& function(); + + /** + Loads the function with the given formula + @param formula is a valid formula in infix notation + */ + void setFormula(const std::string& formula); + /** + Returns the formula loaded into the function + @return the formula loaded into the function + */ + std::string getFormula() const; + + std::string className() const FL_IOVERRIDE; + + Complexity complexity() const FL_IOVERRIDE; + /** + Computes the S-Norm utilizing the given function via + SNormFunction::setFormula(), which automatically assigns the values + of @f$a@f$ and @f$b@f$. + + @param a is a membership function value + @param b is a membership function value + @return the evaluation of the function + */ + scalar compute(scalar a, scalar b) const FL_IOVERRIDE; + SNormFunction* clone() const FL_IOVERRIDE; + + static SNorm* constructor(); + }; +} +#endif /* FL_SNORMFUNCTION_H */ + diff --git a/fuzzylite/fl/norm/s/UnboundedSum.h b/fuzzylite/fl/norm/s/UnboundedSum.h new file mode 100644 index 0000000..695fdfd --- /dev/null +++ b/fuzzylite/fl/norm/s/UnboundedSum.h @@ -0,0 +1,52 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_UNBOUNDEDSUM_H +#define FL_UNBOUNDEDSUM_H + +#include "fl/norm/SNorm.h" + +namespace fl { + + /** + The UnboundedSum class is an SNorm that computes the sum of any two values. + + @author Juan Rada-Vilela, Ph.D. + @see BoundedSum + @see SNorm + @see SNormFactory + @see Norm + @since 4.0 + */ + class FL_API UnboundedSum FL_IFINAL : public SNorm { + public: + std::string className() const FL_IOVERRIDE; + + Complexity complexity() const FL_IOVERRIDE; + /** + Computes the bounded sum of two membership function values + @param a is a membership function value + @param b is a membership function value + @return @f$\min(1, a+b)@f$ + */ + scalar compute(scalar a, scalar b) const FL_IOVERRIDE; + UnboundedSum* clone() const FL_IOVERRIDE; + + static SNorm* constructor(); + }; +} + +#endif /* FL_BOUNDEDSUM_H */ diff --git a/fuzzylite/fl/norm/t/AlgebraicProduct.h b/fuzzylite/fl/norm/t/AlgebraicProduct.h new file mode 100644 index 0000000..d19cb64 --- /dev/null +++ b/fuzzylite/fl/norm/t/AlgebraicProduct.h @@ -0,0 +1,52 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_ALGEBRAICPRODUCT_H +#define FL_ALGEBRAICPRODUCT_H + +#include "fl/norm/TNorm.h" + +namespace fl { + + /** + The AlgebraicProduct class is a TNorm that computes the algebraic product + of any two values. + + @author Juan Rada-Vilela, Ph.D. + @see AlgebraicSum + @see TNorm + @see TNormFactory + @see Norm + @since 4.0 + */ + class FL_API AlgebraicProduct FL_IFINAL : public TNorm { + public: + std::string className() const FL_IOVERRIDE; + + Complexity complexity() const FL_IOVERRIDE; + /** + Computes the algebraic product of two membership function values + @param a is a membership function value + @param b is a membership function value + @return @f$a\times b@f$ + */ + scalar compute(scalar a, scalar b) const FL_IOVERRIDE; + AlgebraicProduct* clone() const FL_IOVERRIDE; + + static TNorm* constructor(); + }; +} +#endif /* FL_ALGEBRAICPRODUCT_H */ diff --git a/fuzzylite/fl/norm/t/BoundedDifference.h b/fuzzylite/fl/norm/t/BoundedDifference.h new file mode 100644 index 0000000..250512b --- /dev/null +++ b/fuzzylite/fl/norm/t/BoundedDifference.h @@ -0,0 +1,52 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_BOUNDEDDIFFERENCE_H +#define FL_BOUNDEDDIFFERENCE_H + +#include "fl/norm/TNorm.h" + +namespace fl { + + /** + The BoundedDifference class is a TNorm that computes the bounded + difference between any two values. + + @author Juan Rada-Vilela, Ph.D. + @see BoundedSum + @see TNorm + @see TNormFactory + @see Norm + @since 4.0 + */ + class FL_API BoundedDifference FL_IFINAL : public TNorm { + public: + std::string className() const FL_IOVERRIDE; + + Complexity complexity() const FL_IOVERRIDE; + /** + Computes the bounded difference between two membership function values + @param a is a membership function value + @param b is a membership function value + @return @f$\max(0, a+b - 1)@f$ + */ + scalar compute(scalar a, scalar b) const FL_IOVERRIDE; + BoundedDifference* clone() const FL_IOVERRIDE; + + static TNorm* constructor(); + }; +} +#endif /* FL_BOUNDEDDIFFERENCE_H */ diff --git a/fuzzylite/fl/norm/t/DrasticProduct.h b/fuzzylite/fl/norm/t/DrasticProduct.h new file mode 100644 index 0000000..d0d490e --- /dev/null +++ b/fuzzylite/fl/norm/t/DrasticProduct.h @@ -0,0 +1,55 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_DRASTICPRODUCT_H +#define FL_DRASTICPRODUCT_H + +#include "fl/norm/TNorm.h" + +namespace fl { + + /** + The DrasticProduct class is a TNorm that computes the drastic product of + any two values. + + @author Juan Rada-Vilela, Ph.D. + @see DrasticSum + @see TNorm + @see TNormFactory + @see Norm + @since 4.0 + */ + class FL_API DrasticProduct FL_IFINAL : public TNorm { + public: + std::string className() const FL_IOVERRIDE; + + Complexity complexity() const FL_IOVERRIDE; + /** + Computes the drastic product of two membership function values + @param a is a membership function value + @param b is a membership function value + @return @f$\begin{cases} + \min(a,b) & \mbox{if $\max(a,b)=1$} \cr + 0 & \mbox{otherwise} + \end{cases}@f$ + */ + scalar compute(scalar a, scalar b) const FL_IOVERRIDE; + DrasticProduct* clone() const FL_IOVERRIDE; + + static TNorm* constructor(); + }; +} +#endif /* FL_DRASTICPRODUCT_H */ diff --git a/fuzzylite/fl/norm/t/EinsteinProduct.h b/fuzzylite/fl/norm/t/EinsteinProduct.h new file mode 100644 index 0000000..9d3a71b --- /dev/null +++ b/fuzzylite/fl/norm/t/EinsteinProduct.h @@ -0,0 +1,52 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_EINSTEINPRODUCT_H +#define FL_EINSTEINPRODUCT_H + +#include "fl/norm/TNorm.h" + +namespace fl { + + /** + The EinsteinProduct class is a TNorm that computes the Einstein product + of any two values. + + @author Juan Rada-Vilela, Ph.D. + @see EinsteinSum + @see TNorm + @see TNormFactory + @see Norm + @since 4.0 + */ + class FL_API EinsteinProduct FL_IFINAL : public TNorm { + public: + std::string className() const FL_IOVERRIDE; + + Complexity complexity() const FL_IOVERRIDE; + /** + Computes the Einstein product of two membership function values + @param a is a membership function value + @param b is a membership function value + @return @f$(a\times b)/(2-(a+b-a\times b))@f$ + */ + scalar compute(scalar a, scalar b) const FL_IOVERRIDE; + EinsteinProduct* clone() const FL_IOVERRIDE; + + static TNorm* constructor(); + }; +} +#endif /* FL_EINSTEINPRODUCT_H */ diff --git a/fuzzylite/fl/norm/t/HamacherProduct.h b/fuzzylite/fl/norm/t/HamacherProduct.h new file mode 100644 index 0000000..79c56ee --- /dev/null +++ b/fuzzylite/fl/norm/t/HamacherProduct.h @@ -0,0 +1,52 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_HAMACHERPRODUCT_H +#define FL_HAMACHERPRODUCT_H + +#include "fl/norm/TNorm.h" + +namespace fl { + + /** + The HamacherProduct class is a TNorm that computes the Hamacher product + of any two values. + + @author Juan Rada-Vilela, Ph.D. + @see HamacherSum + @see TNorm + @see TNormFactory + @see Norm + @since 4.0 + */ + class FL_API HamacherProduct FL_IFINAL : public TNorm { + public: + std::string className() const FL_IOVERRIDE; + + Complexity complexity() const FL_IOVERRIDE; + /** + Computes the Hamacher product of two membership function values + @param a is a membership function value + @param b is a membership function value + @return @f$(a \times b) / (a+b- a \times b)@f$ + */ + scalar compute(scalar a, scalar b) const FL_IOVERRIDE; + HamacherProduct* clone() const FL_IOVERRIDE; + + static TNorm* constructor(); + }; +} +#endif /* FL_HAMACHERPRODUCT_H */ diff --git a/fuzzylite/fl/norm/t/Minimum.h b/fuzzylite/fl/norm/t/Minimum.h new file mode 100644 index 0000000..5df1ae2 --- /dev/null +++ b/fuzzylite/fl/norm/t/Minimum.h @@ -0,0 +1,51 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_MINIMUM_H +#define FL_MINIMUM_H + +#include "fl/norm/TNorm.h" + +namespace fl { + + /** + The Minimum class is a TNorm that computes the minimum of any two values. + + @author Juan Rada-Vilela, Ph.D. + @see Maximum + @see TNorm + @see TNormFactory + @see Norm + @since 4.0 + */ + class FL_API Minimum FL_IFINAL : public TNorm { + public: + std::string className() const FL_IOVERRIDE; + + Complexity complexity() const FL_IOVERRIDE; + /** + Computes the minimum of two membership function values + @param a is a membership function value + @param b is a membership function value + @return @f$\min(a,b)@f$ + */ + scalar compute(scalar a, scalar b) const FL_IOVERRIDE; + Minimum* clone() const FL_IOVERRIDE; + + static TNorm* constructor(); + }; +} +#endif /* FL_MINIMUM_H */ diff --git a/fuzzylite/fl/norm/t/NilpotentMinimum.h b/fuzzylite/fl/norm/t/NilpotentMinimum.h new file mode 100644 index 0000000..2ea7f3c --- /dev/null +++ b/fuzzylite/fl/norm/t/NilpotentMinimum.h @@ -0,0 +1,55 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_NILPOTENTMINIMUM_H +#define FL_NILPOTENTMINIMUM_H + +#include "fl/norm/TNorm.h" + +namespace fl { + + /** + The NilpotentMinimum class is a TNorm that computes the nilpotent minimum + of any two values. + + @author Juan Rada-Vilela, Ph.D. + @see NilpotentMaximum + @see TNorm + @see TNormFactory + @see Norm + @since 5.0 + */ + class FL_API NilpotentMinimum FL_IFINAL : public TNorm { + public: + std::string className() const FL_IOVERRIDE; + + Complexity complexity() const FL_IOVERRIDE; + /** + Computes the nilpotent minimum of two membership function values + @param a is a membership function value + @param b is a membership function value + @return @f$\begin{cases} + \min(a,b) & \mbox{if $a+b>1$} \cr + 0 & \mbox{otherwise} + \end{cases}@f$ + */ + scalar compute(scalar a, scalar b) const FL_IOVERRIDE; + NilpotentMinimum* clone() const FL_IOVERRIDE; + + static TNorm* constructor(); + }; +} +#endif /* FL_NILPOTENTMINIMUM_H */ diff --git a/fuzzylite/fl/norm/t/TNormFunction.h b/fuzzylite/fl/norm/t/TNormFunction.h new file mode 100644 index 0000000..1193a40 --- /dev/null +++ b/fuzzylite/fl/norm/t/TNormFunction.h @@ -0,0 +1,81 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_TNORMFUNCTION_H +#define FL_TNORMFUNCTION_H + +#include "fl/norm/TNorm.h" + +#include "fl/term/Function.h" + +namespace fl { + + /** + The TNormFunction class is a customizable TNorm via Function, which + computes any function based on the @f$a@f$ and @f$b@f$ values. + This TNorm is not registered with the TNormFactory. + + @author Juan Rada-Vilela, Ph.D. + @see Function + @see TNorm + @see Norm + @see TNormFactory + @since 6.0 + */ + + class FL_API TNormFunction FL_IFINAL : public TNorm { + private: + Function _function; + public: + explicit TNormFunction(const std::string& formula = ""); + + /** + Returns the reference to the Function + @return the reference to the Function + */ + Function& function(); + + /** + Loads the function with the given formula + @param formula is a valid formula in infix notation + */ + void setFormula(const std::string& formula); + /** + Returns the formula loaded into the function + @return the formula loaded into the function + */ + std::string getFormula() const; + + std::string className() const FL_IOVERRIDE; + + Complexity complexity() const FL_IOVERRIDE; + /** + Computes the S-Norm utilizing the given function via + SNormFunction::setFormula(), which automatically assigns the values + of @f$a@f$ and @f$b@f$. + + @param a is a membership function value + @param b is a membership function value + @return the evaluation of the function + */ + scalar compute(scalar a, scalar b) const FL_IOVERRIDE; + TNormFunction* clone() const FL_IOVERRIDE; + + static TNorm* constructor(); + }; +} +#endif /* FL_TNORMFUNCTION_H */ + diff --git a/fuzzylite/fl/rule/Antecedent.h b/fuzzylite/fl/rule/Antecedent.h new file mode 100644 index 0000000..a424d95 --- /dev/null +++ b/fuzzylite/fl/rule/Antecedent.h @@ -0,0 +1,185 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_ANTECEDENT_H +#define FL_ANTECEDENT_H + +#include "fl/fuzzylite.h" + +#include "fl/Complexity.h" + +#include <string> + +namespace fl { + class Engine; + class Rule; + class TNorm; + class SNorm; + class Expression; + + /** + The Antecedent class is an expression tree that represents and evaluates + the antecedent of a Rule. The structure of a rule is: `if (antecedent) + then (consequent)`. The structure of the antecedent of a rule is: + + `if variable is [hedge]* term [(and|or) variable is [hedge]* term]*` + + where `*`-marked elements may appear zero or more times, elements in + brackets are optional, and elements in parentheses are compulsory. + + @author Juan Rada-Vilela, Ph.D. + @see Consequent + @see Rule + @since 4.0 + */ + class FL_API Antecedent { + private: + std::string _text; + FL_unique_ptr<Expression> _expression; + + public: + Antecedent(); + virtual ~Antecedent(); + + /** + Sets the antecedent in text + @param text is the antecedent in text + */ + virtual void setText(const std::string& text); + /** + Gets the antecedent in text + @return the antecedent in text + */ + virtual std::string getText() const; + + /** + Gets the expression tree of the antecedent + @return the expression tree of the antecedent + */ + virtual Expression* getExpression() const; + + /** + Sets the expression tree of the antecedent + @param expression is the expression tree of the antecedent + */ + virtual void setExpression(Expression* expression); + + /** + Indicates whether the antecedent is loaded + @return whether the antecedent is loaded + */ + virtual bool isLoaded() const; + + /** + Unloads the antecedent + */ + virtual void unload(); + + /** + Loads the antecedent with the text obtained from + Antecedent::getText() and uses the engine to identify and retrieve + references to the input variables and output variables as required + + @param engine is the engine from which the rules are part of + */ + virtual void load(const Engine* engine); + /** + Loads the antecedent with the given text and uses the engine to + identify and retrieve references to the input variables and output + variables as required + + @param antecedent is the antecedent of the rule in text + @param engine is the engine from which the rules are part of + */ + virtual void load(const std::string& antecedent, const Engine* engine); + + /** + Computes the estimated complexity of calculating the activation degree + @return the estimated complexity of calculating the activation degree + */ + virtual Complexity complexity(const TNorm* conjunction, const SNorm* disjunction) const; + /** + Computes the estimated complexity of recursively calculating the + activation degree from the given node + @return the estimated complexity of recursively calculating the + activation degree from the given node + */ + virtual Complexity complexity(const TNorm* conjunction, const SNorm* disjunction, + const Expression* node) const; + + + /** + Computes the activation degree of the antecedent on the expression + tree from the given node + + @param conjunction is the conjunction operator from the RuleBlock + @param disjunction is the disjunction operator from the RuleBlock + @param node is a node in the expression tree of the antecedent + @return the activation degree of the antecedent + */ + virtual scalar activationDegree(const TNorm* conjunction, const SNorm* disjunction, + const Expression* node) const; + + /** + Computes the activation degree of the antecedent on the expression + tree from the root node + + @param conjunction is the conjunction operator from the RuleBlock + @param disjunction is the disjunction operator from the RuleBlock + @return the activation degree of the antecedent on the expression tree + */ + virtual scalar activationDegree(const TNorm* conjunction, const SNorm* disjunction) const; + + /** + Returns a string representation of the expression tree in infix + notation + + @return a string representation of the expression tree in infix + notation + */ + virtual std::string toString() const; + + /** + Returns a string represention of the given expression tree utilizing + prefix notation + @param node is a node in the expression tree of the antecedent + @return a string represention of the given expression tree utilizing + prefix notation + */ + virtual std::string toPrefix(const Expression* node = fl::null) const; + /** + Returns a string represention of the given expression tree utilizing + infix notation + @param node is a node in the expression tree of the antecedent + @return a string represention of the given expression tree utilizing + infix notation + */ + virtual std::string toInfix(const Expression* node = fl::null) const; + /** + Returns a string represention of the given expression tree utilizing + postfix notation + @param node is a node in the expression tree of the antecedent + @return a string represention of the given expression tree utilizing + postfix notation + */ + virtual std::string toPostfix(const Expression* node = fl::null) const; + + + private: + FL_DISABLE_COPY(Antecedent) + }; +} +#endif /* FL_ANTECEDENT_H */ diff --git a/fuzzylite/fl/rule/Consequent.h b/fuzzylite/fl/rule/Consequent.h new file mode 100644 index 0000000..275d1e7 --- /dev/null +++ b/fuzzylite/fl/rule/Consequent.h @@ -0,0 +1,138 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_CONSEQUENT_H +#define FL_CONSEQUENT_H + +#include "fl/fuzzylite.h" + +#include "fl/Complexity.h" + +#include <string> +#include <vector> + +namespace fl { + class Engine; + class Rule; + class Proposition; + class TNorm; + + /** + The Consequent class is a proposition set that represents and evaluates + the consequent of a Rule.. The structure of a rule is: `if (antecedent) + then (consequent)`. The structure of the consequent of a rule is: + + `then variable is [hedge]* term [and variable is [hedge]* term]* [with + w]?` + + where `*`-marked elements may appear zero or more times, elements in + brackets are optional, elements in parentheses are compulsory, and + `?`-marked elements may appear once or not at all. + + @author Juan Rada-Vilela, Ph.D. + @see Antecedent + @see Rule + @since 4.0 + */ + + class FL_API Consequent { + private: + std::string _text; + std::vector<Proposition*> _conclusions; + + public: + Consequent(); + virtual ~Consequent(); + + /** + Sets the text of the consequent + @param text is the text of the consequent + */ + virtual void setText(const std::string& text); + /** + Gets the text of the consequent + @return the text of the consequent + */ + virtual std::string getText() const; + + /** + Computes the estimated complexity of modifying the consequents + @return the estimated complexity of modifying the consequents + */ + virtual Complexity complexity(const TNorm* implication) const; + /** + Returns an immutable vector of the propositions that represent the + Consequent of a Rule + @return an immutable vector of the set of propositions that represent + the Consequent of a Rule + */ + virtual const std::vector<Proposition*>& conclusions() const; + + /** + Returns the vector of propositions that represent the Consequent of a + Rule + @return the vector of propositions that represent the Consequent of a + Rule + */ + virtual std::vector<Proposition*>& conclusions(); + + /** + Indicates whether the consequent is loaded + @return whether the consequent is loaded + */ + virtual bool isLoaded(); + /** + Unloads the consequent + */ + virtual void unload(); + /** + Loads the consequent with text given from Consequent::getText() and + uses the engine to identify and retrieve references to the input + variables and output variables as required + @param engine is the engine from which the rules are part of + */ + virtual void load(const Engine* engine); + /** + Loads the consequent with the given text and uses the engine to + identify and retrieve references to the input variables and output + variables as required + @param consequent is the consequent of the rule in text + @param engine is the engine from which the rules are part of + */ + virtual void load(const std::string& consequent, const Engine* engine); + + /** + Modifies the proposition set according to the activation degree + (computed in the Antecedent of the Rule) and the implication operator + (given in the RuleBlock) + @param activationDegree is the activation degree computed in the + Antecedent of the Rule + @param implication is the implication operator configured in the + RuleBlock + */ + virtual void modify(scalar activationDegree, const TNorm* implication); + + /** + Returns a string representation of the Consequent + @return a string representation of the Consequent + */ + virtual std::string toString() const; + + private: + FL_DISABLE_COPY(Consequent) + }; +} +#endif /* FL_CONSEQUENT_H */ diff --git a/fuzzylite/fl/rule/Expression.h b/fuzzylite/fl/rule/Expression.h new file mode 100644 index 0000000..dbacc60 --- /dev/null +++ b/fuzzylite/fl/rule/Expression.h @@ -0,0 +1,133 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_EXPRESSION_H +#define FL_EXPRESSION_H + +#include "fl/fuzzylite.h" + +#include <string> +#include <vector> + + + +namespace fl { + class Variable; + class Hedge; + class Term; + + /** + The Expression class is the base class to build an expression tree. + + @author Juan Rada-Vilela, Ph.D. + @see Antecedent + @see Consequent + @see Rule + @since 4.0 + */ + class FL_API Expression { + public: + + enum Type { + Proposition, Operator + }; + Expression(); + virtual ~Expression(); + + /** + Returns the type of the expression + @return the type of the expression + */ + virtual Type type() const = 0; + virtual std::string toString() const = 0; + + private: + FL_DISABLE_COPY(Expression) + }; + + /** + The Proposition class is an Expression that represents a terminal node in + the expression tree as `variable is [hedge]* term`. + + @author Juan Rada-Vilela, Ph.D. + @see Antecedent + @see Consequent + @see Rule + @since 4.0 + */ + class FL_API Proposition FL_IFINAL : public Expression { + public: + /**Variable in `variable is [hedge]* term`*/ + Variable* variable; + /**Hedge%s in `variable is [hedge]* term`, owned by the object, + destroyed on destructor*/ + std::vector<Hedge*> hedges; + /**Term in `variable is [hedge]* term`*/ + Term* term; + + Proposition(); + ~Proposition() FL_IOVERRIDE; + + Expression::Type type() const FL_IOVERRIDE; + + /** + Returns a string representation of the proposition + @return a string representation of the proposition + */ + std::string toString() const FL_IOVERRIDE; + + + private: + FL_DISABLE_COPY(Proposition) + }; + + /** + The Operator class is an Expression that represents a non-terminal node + in the expression tree as a binary operator (i.e., `and` or `or`) on two + Expression nodes. + + @author Juan Rada-Vilela, Ph.D. + @see Antecedent + @see Consequent + @see Rule + @since 4.0 + */ + class FL_API Operator FL_IFINAL : public Expression { + public: + /**Name of the operator*/ + std::string name; + /**Left expression in the binary tree*/ + Expression* left; + /**Right expression in the binary tree*/ + Expression* right; + + Operator(); + ~Operator() FL_IOVERRIDE; + + Expression::Type type() const FL_IOVERRIDE; + + /** + Returns the name of the operator + @return the name of the operator + */ + std::string toString() const FL_IOVERRIDE; + + private: + FL_DISABLE_COPY(Operator) + + }; +} +#endif /* FL_FUZZYEXPRESSION_H */ diff --git a/fuzzylite/fl/rule/Rule.h b/fuzzylite/fl/rule/Rule.h new file mode 100644 index 0000000..b123d15 --- /dev/null +++ b/fuzzylite/fl/rule/Rule.h @@ -0,0 +1,305 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_RULE_H +#define FL_RULE_H + +#include "fl/fuzzylite.h" + +#include "fl/rule/Consequent.h" +#include "fl/rule/Antecedent.h" + +#include <map> +#include <string> + + +namespace fl { + class Engine; + class Hedge; + class TNorm; + class SNorm; + + /** + The Rule class is a conditional statement that contributes to the control + of an Engine. Each rule consists of an Antecedent and a Consequent, each + of which comprises propositions in the form `variable is term`. The + propositions in the Antecedent can be connected by the conjunctive `and` + or the disjunctive `or`, both of which are fuzzy logic operators (TNorm + and SNorm, respectively). Differently, the propositions in the Consequent + are independent from each other and are separated with a symbolic `and`. + The Term in any proposition can be preceded by a Hedge that modifies its + membership function to model cases such as Very, Somewhat, Seldom and + Not. Additionally, the contribution of a rule to the control of the + engine can be determined by its weight @f$w \in [0.0, 1.0]@f$, which is + equal to 1.0 if omitted. The structure of a rule is the following: `if + (antecedent) then (consequent) [with weight]`. The structures of + the antecedent and the consequent are: + + `if variable is [hedge]* term [(and|or) variable is [hedge]* term]*` + + `then variable is [hedge]* term [and variable is [hedge]* term]* [with w]?` + + where elements in brackets are optional, elements in parentheses are + compulsory, `*`-marked elements may appear zero or more times, and + `?`-marked elements may appear once or not at all. + + @author Juan Rada-Vilela, Ph.D. + @see Antecedent + @see Consequent + @see Hedge + @see RuleBlock + @since 4.0 + */ + class FL_API Rule { + private: + bool _enabled; + std::string _text; + scalar _weight; + scalar _activationDegree; + bool _triggered; + FL_unique_ptr<Antecedent> _antecedent; + FL_unique_ptr<Consequent> _consequent; + + public: + explicit Rule(const std::string& text = "", scalar weight = 1.0); + Rule(const Rule& other); + Rule& operator=(const Rule& other); + virtual ~Rule(); + FL_DEFAULT_MOVE(Rule) + + /** + Sets whether the rule is enabled. An enabled rule will be fired, whereas + a disabled rule will not. + @param enabled determines whether the rule is enabled + */ + virtual void setEnabled(bool enabled); + + /** + Gets whether the rule is enabled. An enabled rule will be fired, whereas + a disabled rule will not. + @return whether the rule is enabled + */ + virtual bool isEnabled() const; + + /** + Sets the text of the rule + @param text is the text of the rule + */ + virtual void setText(const std::string& text); + /** + Gets the text of the rule + @return the text of the rule + */ + virtual std::string getText() const; + + /** + Sets the weight of the rule + @param weight is the weight of the rule + */ + virtual void setWeight(scalar weight); + /** + Gets the weight of the rule + @return the weight of the rule + */ + virtual scalar getWeight() const; + + /** + Sets the antecedent of the rule + @param antecedent is the antecedent of the rule + */ + virtual void setAntecedent(Antecedent* antecedent); + /** + Gets the antecedent of the rule + @return the antecedent of the rule + */ + virtual Antecedent* getAntecedent() const; + + /** + Sets the consequent of the rule + @param consequent the consequent of the rule + */ + virtual void setConsequent(Consequent* consequent); + /** + Gets the consequent of the rule + @return the consequent of the rule + */ + virtual Consequent* getConsequent() const; + + /** + Sets the activation degree of the rule + @param activationDegree is the activation degree of the rule + */ + virtual void setActivationDegree(scalar activationDegree); + + /** + Gets the activation degree of the rule + @return the activation degree of the rule + */ + virtual scalar getActivationDegree() const; + + /** + Activates the rule by computing its activation degree using the given + conjunction and disjunction operators + @param conjunction is the conjunction operator + @param disjunction is the disjunction operator + @return the activation degree of the rule + */ + virtual scalar activateWith(const TNorm* conjunction, const SNorm* disjunction); + + /** + Deactivates the rule + */ + virtual void deactivate(); + + /** + Triggers the rule's implication (if the rule is enabled) using the + given implication operator and the underlying activation degree + @param implication is the implication operator + */ + virtual void trigger(const TNorm* implication); + + /** + Indicates whether the rule's implication was triggered + @return whether the rule's implication was triggered + */ + virtual bool isTriggered() const; + + /** + Returns a string representation of the rule in the FuzzyLite Language + @return a string representation of the rule in the FuzzyLite Language + */ + virtual std::string toString() const; + + /** + Indicates whether the rule is loaded + @return whether the rule is loaded + */ + virtual bool isLoaded() const; + /** + Unloads the rule + */ + virtual void unload(); + /** + Loads the rule with the text from Rule::getText(), and uses the + engine to identify and retrieve references to the input variables and + output variables as required + @param engine is the engine from which the rule is part of + */ + virtual void load(const Engine* engine); + /** + Loads the rule with the given text, and uses the engine to identify + and retrieve references to the input variables and output variables + as required + + @param rule is the rule in text + @param engine is the engine from which the rule is part of + */ + virtual void load(const std::string& rule, const Engine* engine); + + /** + Creates a clone of the rule without the rule being loaded + @return a clone of the rule without the rule being loaded + */ + virtual Rule* clone() const; + + /** + Parses and creates a new rule based on the text passed + @param rule is the rule in text + @param engine is the engine from which the rule is part of + @return a new rule parsed from the given text + */ + static Rule* parse(const std::string& rule, const Engine* engine); + + /** + Returns a string representation of the `if` keyword in rules + @return a string representation of the `if` keyword in rules + */ + inline static std::string ifKeyword() { + return "if"; + } + + /** + Returns a string representation of the `is` keyword in rules + @return a string representation of the `is` keyword in rules + */ + inline static std::string isKeyword() { + return "is"; + } + + /** + Returns a string representation of the `then` keyword in rules + @return a string representation of the `then` keyword in rules + */ + inline static std::string thenKeyword() { + return "then"; + } + + /** + Returns a string representation of the `and` keyword in rules + @return a string representation of the `and` keyword in rules + */ + inline static std::string andKeyword() { + return "and"; + } + + /** + Returns a string representation of the `or` keyword in rules + @return a string representation of the `or` keyword in rules + */ + inline static std::string orKeyword() { + return "or"; + } + + /** + Returns a string representation of the `with` keyword in rules + @return a string representation of the `with` keyword in rules + */ + inline static std::string withKeyword() { + return "with"; + } + + /** + Computes the estimated complexity of calculating the activation degree + of the rule + @param conjunction is the conjunction operator + @param disjunction is the disjunction operator + @return the estimated complexity of calculating the activation degree + of the rule + */ + virtual Complexity complexityOfActivation(const TNorm* conjunction, + const SNorm* disjunction) const; + + /** + Computes the estimated complexity of firing the rule + @param implication is the implication operator + @return the estimated complexity of firing the rule + */ + virtual Complexity complexityOfFiring(const TNorm* implication) const; + + /** + Returns the estimated complexity of activating and firing the rule + @param conjunction is the conjunction operator + @param disjunction is the disjunction operator + @param implication is the implication operator + @return the estimated complexity of activating and firing the rule + */ + virtual Complexity complexity(const TNorm* conjunction, + const SNorm* disjunction, const TNorm* implication) const; + + }; +} + +#endif /* FL_RULE_H */ diff --git a/fuzzylite/fl/rule/RuleBlock.h b/fuzzylite/fl/rule/RuleBlock.h new file mode 100644 index 0000000..d4ccace --- /dev/null +++ b/fuzzylite/fl/rule/RuleBlock.h @@ -0,0 +1,233 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_RULEBLOCK_H +#define FL_RULEBLOCK_H + +#include "fl/fuzzylite.h" + +#include "fl/activation/Activation.h" +#include "fl/Complexity.h" + +#include <string> +#include <vector> + + +namespace fl { + + class Engine; + class Rule; + class TNorm; + class SNorm; + + /** + The RuleBlock class contains a set of Rule%s and fuzzy logic + operators required to control an Engine. + + @author Juan Rada-Vilela, Ph.D. + @see Engine + @see Rule + @see Antecedent + @see Consequent + @since 4.0 + */ + class FL_API RuleBlock { + private: + bool _enabled; + std::string _name; + std::string _description; + std::vector<Rule*> _rules; + FL_unique_ptr<TNorm> _conjunction; + FL_unique_ptr<SNorm> _disjunction; + FL_unique_ptr<TNorm> _implication; + FL_unique_ptr<Activation> _activation; + + void copyFrom(const RuleBlock& source); + + public: + explicit RuleBlock(const std::string& name = ""); + RuleBlock(const RuleBlock& other); + RuleBlock& operator=(const RuleBlock& other); + virtual ~RuleBlock(); + FL_DEFAULT_MOVE(RuleBlock) + + /** + Enables the rule block + @param enabled whether the rule block is enabled + */ + virtual void setEnabled(bool enabled); + /** + Indicates whether the rule block is enabled + @return whether the rule block is enabled + */ + virtual bool isEnabled() const; + + /** + Activates the rule block + */ + virtual void activate(); + + /** + Sets the name of the rule block + @param name is the name of the rule block + */ + virtual void setName(std::string name); + /** + Gets the name of the rule block + @return the name of the rule block + */ + virtual std::string getName() const; + + /** + Gets the description of the rule block + @return the description of the rule block + */ + virtual std::string getDescription() const; + + /** + Sets the description of the rule block + @param description is the description of the rule block + */ + virtual void setDescription(const std::string& description); + /** + Sets the conjunction operator + @param conjunction is the conjunction operator + */ + virtual void setConjunction(TNorm* conjunction); + /** + Gets the conjunction operator + @return the conjunction operator + */ + virtual TNorm* getConjunction() const; + + /** + Sets the disjunction operator + @param disjunction is the disjunction operator + */ + virtual void setDisjunction(SNorm* disjunction); + /** + Gets the disjunction operator + @return the disjunction operator + */ + virtual SNorm* getDisjunction() const; + + /** + Sets the implication operator + @param implication is the implication operator + */ + virtual void setImplication(TNorm* implication); + /** + Gets the implication operator + @return the implication operator + */ + virtual TNorm* getImplication() const; + + /** + Sets the activation method + @param activation is the activation method + */ + virtual void setActivation(Activation* activation); + /** + Gets the activation method + @return the activation method + */ + virtual Activation* getActivation() const; + + /** + Unloads all the rules in the rule block + */ + virtual void unloadRules() const; + /** + Loads all the rules into the rule block + @param engine is the engine where this rule block is registered + */ + virtual void loadRules(const Engine* engine); + + /** + Unloads all the rules in the rule block and then loads each rule again + @param engine is the engine where this rule block is registered + */ + virtual void reloadRules(const Engine* engine); + + /** + Returns a string representation of the rule block in the FuzzyLite + Language + @return a string representation of the rule block in the FuzzyLite + Language + */ + virtual std::string toString() const; + + /** + Returns the estimated complexity of activating the rule block + @return the estimated complexity of activating the rule block + */ + virtual Complexity complexity() const; + + /** + Adds the given rule to the rule block + @param rule is the rule to add + */ + virtual void addRule(Rule* rule); + /** + Inserts the rule at the specified index, shifting other rules to + the right + @param rule is the rule to insert + @param index is the index at which to insert the rule + */ + virtual void insertRule(Rule* rule, std::size_t index); + /** + Gets the rule at the specified index + @param index is the index at which the rule is retrieved + @return the rule at the specified index + */ + virtual Rule* getRule(std::size_t index) const; + /** + Removes the rule at the specified index + @param index is the index at which the rule will be removed, + shifting other rules to the left + @return the rule at the specified index + */ + virtual Rule* removeRule(std::size_t index); + /** + Returns the number of rules added to the rule block + @return the number of rules added to the rule block + */ + virtual std::size_t numberOfRules() const; + /** + Sets the rules of the rule block + @param rules is a vector of rules + */ + virtual void setRules(const std::vector<Rule*>& rules); + /** + Returns an immutable vector of the rules added to the rule block + @return an immutable vector of the rules added to the rule block + */ + virtual const std::vector<Rule*>& rules() const; + /** + Returns a mutable vector of the rules added to the rule block + @return a mutable vector of the rules added to the rule block + */ + virtual std::vector<Rule*>& rules(); + + /** + Creates a clone of the rule block without the rules being loaded + @return a clone of the rule block without the rules being loaded + */ + virtual RuleBlock* clone() const; + + }; +} +#endif /* RULEBLOCK_H */ diff --git a/fuzzylite/fl/term/Activated.h b/fuzzylite/fl/term/Activated.h new file mode 100644 index 0000000..f122a25 --- /dev/null +++ b/fuzzylite/fl/term/Activated.h @@ -0,0 +1,107 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_ACTIVATED_H +#define FL_ACTIVATED_H + +#include "fl/term/Term.h" + +namespace fl { + class TNorm; + + /** + The Activated class is a special Term that contains pointers to the + necessary information of a term that has been activated as part of the + Antecedent of a Rule. The ownership of the pointers is not transferred to + objects of this class. The Activated class was named + `Thresholded` in versions 4.0 and earlier. + + @author Juan Rada-Vilela, Ph.D. + @see OutputVariable + @see Term + @since 5.0 + */ + class FL_API Activated : public Term { + private: + const Term* _term; + scalar _degree; + const TNorm* _implication; + + public: + explicit Activated(const Term* term = fl::null, scalar degree = 1.0, + const TNorm* implication = fl::null); + virtual ~Activated() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(Activated) + + virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"degree implication term"` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Does nothing. + @param parameters are irrelevant + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the implication of the activation degree and the membership + function value of @f$x@f$ + @param x is a value + @return @f$d \otimes \mu(x)@f$, where @f$d@f$ is the activation degree + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + virtual std::string toString() const FL_IOVERRIDE; + + /** + Sets the activated term + @param term is the activated term + */ + virtual void setTerm(const Term* term); + /** + Gets the activated term + @return the activated term + */ + virtual const Term* getTerm() const; + + /** + Sets the activation degree of the term + @param degree is the activation degree of the term + */ + virtual void setDegree(scalar degree); + /** + Gets the activation degree of the term + @return the activation degree of the term + */ + virtual scalar getDegree() const; + + /** + Sets the implication operator + @param implication is the implication operator + */ + virtual void setImplication(const TNorm* implication); + /** + Gets the implication operator + @return the implication operator + */ + virtual const TNorm* getImplication() const; + + virtual Activated* clone() const FL_IOVERRIDE; + }; +} +#endif /* FL_ACTIVATED_H */ diff --git a/fuzzylite/fl/term/Aggregated.h b/fuzzylite/fl/term/Aggregated.h new file mode 100644 index 0000000..af08c75 --- /dev/null +++ b/fuzzylite/fl/term/Aggregated.h @@ -0,0 +1,212 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_AGGREGATED_H +#define FL_AGGREGATED_H + +#include "fl/term/Term.h" + +#include "fl/term/Activated.h" + +#include <vector> + +namespace fl { + + class SNorm; + class TNorm; + + /** + The Aggregated class is a special Term that stores a fuzzy set with the + Activated terms from the Antecedent%s of a Rule, thereby serving mainly + as the fuzzy output value of the OutputVariable%s. The ownership of the + activated terms will be transfered to objects of this class, and + therefore their destructors will be called upon destruction of this term + (or calling Aggregated::clear()). + + @author Juan Rada-Vilela, Ph.D. + @see Antecedent + @see Rule + @see OutputVariable + @see Activated + @see Term + @since 6.0 + */ + class FL_API Aggregated : public Term { + private: + std::vector<Activated> _terms; + scalar _minimum, _maximum; + FL_unique_ptr<SNorm> _aggregation; + + void copyFrom(const Aggregated& source); + public: + explicit Aggregated(const std::string& name = "", + scalar minimum = fl::nan, + scalar maximum = fl::nan, + SNorm* aggregation = fl::null); + Aggregated(const Aggregated& other); + Aggregated& operator=(const Aggregated& other); + virtual ~Aggregated() FL_IOVERRIDE; + FL_DEFAULT_MOVE(Aggregated) + + virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"aggregation minimum maximum terms"` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Does nothing + @param parameters are irrelevant + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Aggregated* clone() const FL_IOVERRIDE; + + virtual Complexity complexity() const FL_IOVERRIDE; + virtual Complexity complexityOfMembership() const; + virtual Complexity complexityOfActivationDegree() const; + + /** + Aggregates the membership function values of @f$x@f$ utilizing the + aggregation operator + @param x is a value + @return @f$\sum_i{\mu_i(x)}, i \in \mbox{terms}@f$ + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + /** + Computes the aggregated activation degree for the given term. + If the same term is present multiple times, the aggregation operator + is utilized to sum the activation degrees of the term. If the + aggregation operator is fl::null, a regular sum is performed. + @param forTerm is the term for which to compute the aggregated + activation degree + @return the aggregated activation degree for the given term + */ + virtual scalar activationDegree(const Term* forTerm) const; + + /** + Iterates over the Activated terms to find the term with the maximum + activation degree + @return the term with the maximum activation degree + */ + virtual const Activated* highestActivatedTerm() const; + + virtual std::string toString() const FL_IOVERRIDE; + + /** + Sets the minimum of the range of the fuzzy set + @param minimum is the minimum of the range of the fuzzy set + */ + virtual void setMinimum(scalar minimum); + /** + Gets the minimum of the range of the fuzzy set + @return the minimum of the range of the fuzzy set + */ + virtual scalar getMinimum() const; + + /** + Sets the maximum of the range of the fuzzy set + @param maximum is the maximum of the range of the fuzzy set + */ + virtual void setMaximum(scalar maximum); + /** + Gets the maximum of the range of the fuzzy set + @return the maximum of the range of the fuzzy set + */ + virtual scalar getMaximum() const; + + /** + Sets the range of the fuzzy set to `[minimum, maximum]` + @param minimum is the minimum of the range of the fuzzy set + @param maximum is the maximum of the range of the fuzzy set + */ + virtual void setRange(scalar minimum, scalar maximum); + /** + Returns the magnitude of the range of the fuzzy set, + @return the magnitude of the range of the fuzzy set, + i.e., `maximum - minimum` + */ + virtual scalar range() const; + + /** + Sets the aggregation operator + @param aggregation is the aggregation operator + */ + virtual void setAggregation(SNorm* aggregation); + /** + Gets the aggregation operator + @return the aggregation operator + */ + virtual SNorm* getAggregation() const; + + /** + Adds a new Activated term (from the parameters) to the fuzzy set + @param term is the activated term + @param degree is the activation degree + @param implication is the implication operator + */ + virtual void addTerm(const Term* term, scalar degree, const TNorm* implication); + /** + Adds the activated term to the fuzzy set. The activated term + will be deleted when Aggregated::clear() + @param term is the activated term + */ + virtual void addTerm(const Activated& term); + /** + Gets the term at the given index + @param index is the index of the term + @return the activated term at the given index + */ + virtual const Activated& getTerm(std::size_t index) const; + /** + Removes the term at the given index without deleting the term + @param index is the index of the term + @return the removed term + */ + virtual const Activated& removeTerm(std::size_t index); + /** + Returns the number of activated terms + @return the number of activated terms + */ + virtual std::size_t numberOfTerms() const; + + /** + Sets the activated terms + @param terms is the activated terms + */ + virtual void setTerms(const std::vector<Activated>& terms); + /** + Returns an immutable vector of activated terms + @return an immutable vector of activated terms + */ + virtual const std::vector<Activated>& terms() const; + /** + Returns a mutable vector of activated terms + @return a mutable vector of activated terms + */ + virtual std::vector<Activated>& terms(); + /** + Indicates whether the vector of activated terms is empty + @return whether the vector of activated terms is empty + */ + virtual bool isEmpty() const; + /** + Clears and deletes the activated terms + */ + virtual void clear(); + }; +} +#endif /* FL_AGGREGATED_H */ diff --git a/fuzzylite/fl/term/Bell.h b/fuzzylite/fl/term/Bell.h new file mode 100644 index 0000000..e27d9c9 --- /dev/null +++ b/fuzzylite/fl/term/Bell.h @@ -0,0 +1,114 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_BELL_H +#define FL_BELL_H + +#include "fl/term/Term.h" + +namespace fl { + + /** + The Bell class is an extended Term that represents the generalized bell + curve membership function. + + @image html bell.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ + class FL_API Bell : public Term { + private: + scalar _center; + scalar _width; + scalar _slope; + public: + explicit Bell(const std::string& name = "", + scalar center = fl::nan, + scalar width = fl::nan, + scalar slope = fl::nan, + scalar height = 1.0); + virtual ~Bell() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(Bell) + + virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"center width slope [height]"` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"center width slope [height]"` + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$h / (1 + \left(|x-c|/w\right)^{2s}@f$ + + where @f$h@f$ is the height of the Term, + @f$c@f$ is the center of the Bell, + @f$w@f$ is the width of the Bell, + @f$s@f$ is the slope of the Bell + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + + /** + Sets the center of the bell curve + @param center is the center of the bell curve + */ + virtual void setCenter(scalar center); + /** + Gets the center of the bell curve + @return the center of the bell curve + */ + virtual scalar getCenter() const; + + /** + Sets the width of the bell curve + @param width is the width of the bell curve + */ + virtual void setWidth(scalar width); + /** + Gets the width of the bell curve + @return the width of the bell curve + */ + virtual scalar getWidth() const; + + /** + Sets the slope of the bell curve + @param slope is the slope of the bell curve + */ + virtual void setSlope(scalar slope); + /** + Gets the slope of the bell curve + @return the slope of the bell curve + */ + virtual scalar getSlope() const; + + virtual Bell* clone() const FL_IOVERRIDE; + + static Term* constructor(); + + }; +} +#endif /* FL_BELL_H */ diff --git a/fuzzylite/fl/term/Binary.h b/fuzzylite/fl/term/Binary.h new file mode 100644 index 0000000..c22a9e1 --- /dev/null +++ b/fuzzylite/fl/term/Binary.h @@ -0,0 +1,132 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_BINARY_H +#define FL_BINARY_H + +#include "fl/term/Term.h" + +namespace fl { + + /** + The Binary class is an edge Term that represents the binary membership + function. + + @image html binary.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 6.0 + */ + class FL_API Binary : public Term { + private: + scalar _start; + scalar _direction; + public: + + /** + Direction is an enumerator that indicates the direction of the + edge. + */ + enum Direction { + /** `(_|)` increases to the right (infinity)*/ + Positive, + /** `(--)` direction is NaN */ + Undefined, + /** `(|_)` increases to the left (-infinity)*/ + Negative + }; + + explicit Binary(const std::string& name = "", scalar start = fl::nan, + scalar direction = fl::nan, scalar height = 1.0); + virtual ~Binary() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(Binary) + + virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"start direction [height]"` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"start direction [height]"` + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$\begin{cases} + 1h & \mbox{if $ \left(s < d \vedge x \in [s, d)\right) \wedge + \left( s > d \vedge x \in (d, s] \right) $} \cr + 0h & \mbox{otherwise} + \end{cases}@f$ + + where @f$h@f$ is the height of the Term, + @f$s@f$ is the start of the Binary edge, + @f$d@f$ is the direction of the Binary edge. + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + + /** + Sets the start of the binary edge + @param start is the start of the binary edge + */ + virtual void setStart(scalar start); + /** + Gets the start of the binary edge + @return the start of the binary edge + */ + virtual scalar getStart() const; + + /** + Sets the direction of the binary edge. + + @f$\begin{cases} + \text{Positive} & \mbox{if $ d > s $}\cr + \text{Negative} & \mbox{if $ d < s $}\cr + \mbox{\tt NaN} & \mbox{otherwise} + \end{cases} + @f$ + + where @f$d@f$ is the given direction, and + @f$s@f$ is the start of the Binary edge + + @param direction is the direction of the binary edge + */ + virtual void setDirection(scalar direction); + /** + Gets the direction of the binary edge + @return the direction of the binary edge + */ + virtual scalar getDirection() const; + + /** + Gets the Direction of the binary edge as an enumerator + @return the Direction of the binary edge as an enumerator + */ + virtual Direction direction() const; + + virtual Binary* clone() const FL_IOVERRIDE; + + static Term* constructor(); + }; +} +#endif /* FL_BINARY_H */ diff --git a/fuzzylite/fl/term/Concave.h b/fuzzylite/fl/term/Concave.h new file mode 100644 index 0000000..12d7160 --- /dev/null +++ b/fuzzylite/fl/term/Concave.h @@ -0,0 +1,110 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_CONCAVE_H +#define FL_CONCAVE_H + +#include "fl/term/Term.h" + +namespace fl { + + /** + The Concave class is an edge Term that represents the concave membership + function. + + @image html concave.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 5.0 + */ + class FL_API Concave : public Term { + private: + scalar _inflection, _end; + public: + explicit Concave(const std::string& name = "", + scalar inflection = fl::nan, + scalar end = fl::nan, + scalar height = 1.0); + virtual ~Concave() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(Concave) + + virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term as + @return `"inflection end [height]"` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters given + @param parameters as `"inflection end [height]"` + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$\begin{cases} + h \times (e - i) / (2e - i - x) & \mbox{if $i \leq e \wedge x < e$ + (increasing concave)} \cr + h \times (i - e) / (-2e + i + x) & \mbox{if $i > e \wedge x > e$ + (decreasing concave)} \cr + h & \mbox{otherwise} \cr + \end{cases}@f$ + + where @f$h@f$ is the height of the Term, + @f$i@f$ is the inflection of the Concave, + @f$e@f$ is the end of the Concave + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + + virtual scalar tsukamoto(scalar activationDegree, + scalar minimum, scalar maximum) const FL_IOVERRIDE; + + virtual bool isMonotonic() const FL_IOVERRIDE; + + /** + Sets the inflection of the curve + @param inflection is the inflection of the curve + */ + virtual void setInflection(scalar inflection); + /** + Gets the inflection of the curve + @return the inflection of the curve + */ + virtual scalar getInflection() const; + + /** + Sets the end of the curve + @param end is the end of the curve + */ + virtual void setEnd(scalar end); + /** + Gets the end of the curve + @return the end of the curve + */ + virtual scalar getEnd() const; + + virtual Concave* clone() const FL_IOVERRIDE; + + static Term* constructor(); + }; +} +#endif /* FL_CONCAVE_H */ + diff --git a/fuzzylite/fl/term/Constant.h b/fuzzylite/fl/term/Constant.h new file mode 100644 index 0000000..40ba77b --- /dev/null +++ b/fuzzylite/fl/term/Constant.h @@ -0,0 +1,81 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_CONSTANT_H +#define FL_CONSTANT_H + +#include "fl/term/Term.h" + +namespace fl { + + /** + The Constant class is a (zero) polynomial Term that represents a constant + value @f$ f(x) = k @f$ + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ + class FL_API Constant : public Term { + private: + scalar _value; + + public: + explicit Constant(const std::string& name = "", + scalar value = fl::nan); + virtual ~Constant() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(Constant) + + virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"value"` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"value"` + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the membership function evaluated at @f$x@f$ + @param x is irrelevant + @return @f$c@f$, where @f$c@f$ is the constant value + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + + /** + Sets the constant value + @param value is the constant value + */ + virtual void setValue(scalar value); + /** + Gets the constant value + @return the constant value + */ + virtual scalar getValue() const; + + virtual Constant* clone() const FL_IOVERRIDE; + + static Term* constructor(); + }; +} +#endif /* FL_CONSTANT_H */ + diff --git a/fuzzylite/fl/term/Cosine.h b/fuzzylite/fl/term/Cosine.h new file mode 100644 index 0000000..b45ab85 --- /dev/null +++ b/fuzzylite/fl/term/Cosine.h @@ -0,0 +1,101 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_COSINE_H +#define FL_COSINE_H + +#include "fl/term/Term.h" + +namespace fl { + + /** + The Cosine class is an extended Term that represents the cosine + membership function. + + @image html cosine.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 5.0 + */ + + class FL_API Cosine : public Term { + private: + scalar _center, _width; + public: + explicit Cosine(const std::string& name = "", + scalar center = fl::nan, + scalar width = fl::nan, + scalar height = 1.0); + virtual ~Cosine() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(Cosine) + + virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"center width [height]"` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"center width [height]"` + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$\begin{cases} + 0h & \mbox{if $x < c - 0.5w \vee x > c + 0.5w$} \cr + 0.5h \times ( 1 + \cos(2.0 / w\pi(x-c))) & \mbox{otherwise} + \end{cases}@f$ + + where @f$h@f$ is the height of the Term, + @f$c@f$ is the center of the Cosine, + @f$w@f$ is the width of the Cosine + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + /** + Sets the center of the cosine + @param center is the center of the cosine + */ + virtual void setCenter(scalar center); + /** + Gets the center of the cosine + @return the center of the cosine + */ + virtual scalar getCenter() const; + + /** + Sets the width of the cosine + @param width is the width of the cosine + */ + virtual void setWidth(scalar width); + /** + Gets the width of the cosine + @return the width of the cosine + */ + virtual scalar getWidth() const; + + virtual Cosine* clone() const FL_IOVERRIDE; + + static Term* constructor(); + }; +} +#endif /* FL_COSINE_H */ + diff --git a/fuzzylite/fl/term/Discrete.h b/fuzzylite/fl/term/Discrete.h new file mode 100644 index 0000000..ba5ae70 --- /dev/null +++ b/fuzzylite/fl/term/Discrete.h @@ -0,0 +1,280 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_DISCRETE_H +#define FL_DISCRETE_H + +#include "fl/term/Term.h" + +#include "fl/defuzzifier/IntegralDefuzzifier.h" + +#include <vector> +#include <utility> + +namespace fl { + + /** + The Discrete class is a basic Term that represents a discrete membership + function. The pairs of values in any Discrete term **must** be sorted + ascendently because the membership function is computed using binary search + to find the lower and upper bounds of @f$x@f$. + + @image html discrete.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ + class FL_API Discrete : public Term { + public: + typedef std::pair<scalar, scalar> Pair; + private: + std::vector<Pair> _xy; + public: + explicit Discrete(const std::string& name = "", + const std::vector<Pair>& xy = std::vector<Pair>(), + scalar height = 1.0); + virtual ~Discrete() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(Discrete) + + virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term as `x1 y1 xn yn [height]` + @return `x1 y1 xn yn [height]` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters given as `x1 y1 xn yn [height]` + @param parameters as `x1 y1 xn yn [height]` + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + /** + Ascendantly sorts the given pairs of values by the @f$x@f$-value, + as it is required by the Discrete term. + @param pairs is a vector of pairs of values in the form @f$(x,y)@f$ + */ + static void sort(std::vector<Pair>& pairs); + + /** + Ascendantly sorts the pairs of values in this Discrete term by the + @f$x@f$-coordinate + */ + virtual void sort(); + + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the membership function evaluated at @f$x@f$ by using binary + search to find the lower and upper bounds of @f$x@f$ and then linearly + interpolating the membership function between the bounds. + @param x + @return @f$ \dfrac{h (y_{\max} - y_{\min})}{(x_{\max}- x_{\min})} (x - x_{\min}) + y_{\min}@f$ + + where @f$h@f$ is the height of the Term, + @f$x_{\min}@f$ and @f$x_{\max}@f$is are the lower and upper limits + of @f$x@f$ in `xy` (respectively), + @f$y_{\min}@f$ and @f$y_{\max}@f$is are the membership functions + of @f$\mu(x_{\min})@f$ and @f$\mu(x_{\max})@f$ (respectively) + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + + /** + Sets the vector of pairs defining the discrete membership function + @param pairs is the vector of pairs defining the discrete membership function + */ + virtual void setXY(const std::vector<Pair>& pairs); + /** + Gets an immutable vector of pairs defining the discrete membership function + @return an immutable vector of pairs defining the discrete membership function + */ + virtual const std::vector<Pair>& xy() const; + /** + Gets a mutable vector of pairs defining the discrete membership function + @return a mutable vector of pairs defining the discrete membership function + */ + virtual std::vector<Pair>& xy(); + /** + Gets the immutable pair @f$(x_i,y_i)@f$ at index @f$i@f$ + @param index is the index @f$i@f$ + @return the immutable pair @f$(x_i,y_i)@f$ + */ + virtual const Pair& xy(std::size_t index) const; + /** + Gets the mutable pair @f$(x_i,y_i)@f$ at index @f$i@f$ + @param index is the index @f$i@f$ + @return a mutable pair @f$(x_i,y_i)@f$ + */ + virtual Pair& xy(std::size_t index); + + /** + Creates, fills and returns a vector containing the @f$x@f$ values + @return a vector containing the @f$x@f$ values + */ + virtual std::vector<scalar> x() const; + + /** + Gets the @f$x@f$ value at the given index + @return the @f$x@f$ value at the given index + */ + virtual scalar x(std::size_t index) const; + + /** + Gets the reference to the @f$x@f$ value at the given index + @return the reference to the @f$x@f$ value at the given index + */ + virtual scalar& x(std::size_t index); + + /** + Creates, fills and returns a vector containing the @f$y@f$ values + @return a vector containing the @f$y@f$ values + */ + virtual std::vector<scalar> y() const; + + /** + Gets the @f$y@f$ value at the given index + @param index is the index + @return the @f$y@f$ value at the given index + */ + virtual scalar y(std::size_t index) const; + + /** + Gets the reference to the @f$y@f$ value at the given index + @param index is the index + @return the reference to the @f$y@f$ value at the given index + */ + virtual scalar& y(std::size_t index); + /** + Creates a vector of fl::scalar from a vector of Pair given in the + form @f$\left(\{x_1,y_1\},...,\{x_n,y_n\}\right)@f$ + @param xy is the vector of Pair + @return a vector of fl::scalar as @f$(x_1,y_1,...,x_n,y_n)@f$ + */ + static std::vector<scalar> toVector(const std::vector<Pair>& xy); + /** + Creates a vector of Pair from a vector of fl::scalar given in the + form @f$(x_1,y_1,...,x_n,y_n)@f$ + @param xy is a vector of fl::scalar given as + @f$(x_1,y_1,...,x_n,y_n)@f$ + @return a vector of Pair in the form + @f$\left(\{x_1,y_1\},...,\{x_n,y_n\}\right)@f$ + @throws fl::Exception if a value is missing, that is, if the length + of @f$xy@f$ is odd: @f$|xy|\mod 2 = 1@f$ + */ + static std::vector<Pair> toPairs(const std::vector<scalar>& xy); + /** + Creates a vector of Pair from a vector of fl::scalar given in the + form @f$(x_1,y_1,...,x_n,y_n)@f$ + @param xy is a vector of fl::scalar given as + @f$(x_1,y_1,...,x_n,y_n)@f$ possibly missing a value + @param missingValue is the replacement in the case a value is missing + from @f$xy@f$ + @return a vector of Pair in the form + @f$\left(\{x_1,y_1\},...,\{x_n,y_n\}\right)@f$ + */ + static std::vector<Pair> toPairs(const std::vector<scalar>& xy, + scalar missingValue) FL_INOEXCEPT; + + /** + Formats a vector of Pair into a std::string in the form + @f$(x_1,y_1) ... (x_n,y_n)@f$ + @param xy is the vector of Pair + @param prefix indicates the prefix of a Pair, e.g., `(` results in + @f$(x_i@f$ + @param innerSeparator indicates the separator between + @f$x@f$ and @f$y@f$, e.g., `,` results in @f$x_i,y_i@f$ + @param suffix indicates the postfix of a Pair, e.g., `]` results in + @f$y_i]@f$ + @param outerSeparator indicates the separator between Pair, e.g., + `;` results in @f$(x_i,y_i);(x_j,y_j)@f$ + @return a formatted string containing the pairs of @f$(x,y)@f$ values + */ + static std::string formatXY(const std::vector<Pair>& xy, + const std::string& prefix = "(", const std::string& innerSeparator = ",", + const std::string& suffix = ")", const std::string& outerSeparator = " "); + + /** + Discretizes the given term + @param term is the term to discretize + @param start is the value from which discretization starts + @param end is the value at which discretization ends + @param resolution is the number of equally-distributed samples to + perform between start and end + @param boundedMembershipFunction indicates whether to ensure that + @f$\mu(x)\in[0.0,1.0]@f$ + @return a Discrete term that approximates the given term + */ + static Discrete* discretize(const Term* term, scalar start, scalar end, + int resolution = IntegralDefuzzifier::defaultResolution(), + bool boundedMembershipFunction = true); + + virtual Discrete* clone() const FL_IOVERRIDE; + + static Term* constructor(); + + /** + Creates a Discrete term from a variadic set of values. + Beware: this method is unsafe and must be used with care by + ensuring: + - the value `argc` correctly and exactly determines the number of + values passed, + - the data type of each variadic arguments is the same, e.g., + @f$(1.0, 2.0, 3.0)@f$ are all fl::scalar, whereas in + @f$(1.0, 2, 3.0)@f$ the second term is an integer, which will cause + memory access issues due to the difference in size between + `int` and `fl::scalar`. + @param name is the name of the resulting term + @param argc is the number of values passed + @param x1 is the @f$x@f$ value of the first Pair + @param y1 is the @f$y@f$ value of the first Pair + @param ... are the remaining pairs of values @f$x_i@f$ and @f$y_i@f$ + @return a new Discrete term with the given parameters + */ + template <typename T> + static Discrete* create(const std::string& name, int argc, + T x1, T y1, ...); + }; +} + +/** + Template implementation + */ + +namespace fl { + + template <typename T> + inline Discrete* Discrete::create(const std::string& name, int argc, + T x1, T y1, ...) { + std::vector<scalar> xy(argc); + xy.at(0) = x1; + xy.at(1) = y1; + va_list args; + va_start(args, y1); + for (int i = 2; i < argc; ++i) { + xy.at(i) = (scalar) va_arg(args, T); + } + va_end(args); + + FL_unique_ptr<Discrete> result(new Discrete(name)); + if (xy.size() % 2 != 0) { + result->setHeight(xy.back()); + xy.pop_back(); + } + result->setXY(toPairs(xy)); + return result.release(); + } +} +#endif /* FL_DISCRETE_H */ diff --git a/fuzzylite/fl/term/Function.h b/fuzzylite/fl/term/Function.h new file mode 100644 index 0000000..14e3427 --- /dev/null +++ b/fuzzylite/fl/term/Function.h @@ -0,0 +1,399 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_FUNCTION_H +#define FL_FUNCTION_H + +#include "fl/term/Term.h" + +#include <map> +#include <string> + +namespace fl { + + class Engine; + + /** + The Function class is a polynomial Term that represents a generic + function @f$ f : x \mapsto f(x) @f$. Every Function object has a public + key-value map, namely Function::variables, that links variable names to + fl::scalar values, which are utilized to replace the variable names for + their respective values in the given formula whenever the function + @f$f@f$ is evaluated. Specifically, when the method + Function::membership() is called, the name and value of the variable + @f$x@f$ are automatically loaded into the map. Furthermore, if an Engine + is given, the names of its InputVariable%s and OutputVariable%s will also + be automatically loaded into the map linking to their respective input + values and (previously defuzzified) output values. The + Function::variables need to be manually loaded whenever variables other + than @f$x@f$, input variables, and output variables, are expressed in the + given formula, always having in mind that (a) the map replaces existing + keys, and (b) the variable @f$x@f$, and input variables and output + variables of an engine will automatically be replaced and will also take + precedence over previously loaded variables. + + Besides the use of Function as a linguistic Term, it is also utilized to + convert the text of the Antecedent of a Rule, expressed in infix + notation, into postfix notation. + + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @see FunctionFactory + @see Antecedent::load() + @since 4.0 + */ + class FL_API Function : public Term { + public: + typedef scalar(*Unary)(scalar); + typedef scalar(*Binary)(scalar, scalar); + + /** + The Element class represents a single element in a formula, be that + either a function or an operator. If the Element represents a + function, the function can be Unary or Binary, that is, the function + take one or two parameters (respectively). Else, if the Element + represents an operator, the parameters to be defined are its `arity`, + its `precedence`, and its `associativity`. + */ + struct FL_API Element { + + /** + Determines the type of the element + */ + enum Type { + Operator, Function + }; + /**Name of the element*/ + std::string name; + /**Description of the element*/ + std::string description; + /**Type of the element*/ + Type type; + /**Pointer to unary method*/ + Unary unary; + /**Pointer to binary method*/ + Binary binary; + /**Number of operands required*/ + int arity; + /**Precedence of the element: clarifies which procedures should be + performed first in a given mathematical expression + (https://en.wikipedia.org/wiki/Order_of_operations)*/ + int precedence; + /**Associativity of the element: determines how operators of the + same precedence are grouped in the absence of parentheses + (https://en.wikipedia.org/wiki/Operator_associativity)*/ + int associativity; + Element(const std::string& name, const std::string& description, Type type); + Element(const std::string& name, const std::string& description, + Type type, Unary unary, int precedence = 0, int associativity = -1); + Element(const std::string& name, const std::string& description, + Type type, Binary binary, int precedence = 0, int associativity = -1); + virtual ~Element(); + FL_DEFAULT_COPY_AND_MOVE(Element) + + /** + Indicates whether the element is a Type::Operator + @return whether the element is a Type::Operator + */ + virtual bool isOperator() const; + /** + Indicates whether the element is a Type::Function + @return whether the element is a Type::Function + */ + virtual bool isFunction() const; + /** + Clones the element + @return a clone of the element + */ + virtual Element* clone() const; + + /** + Returns a description of the element and its members + @return a description of the element and its members + */ + virtual std::string toString() const; + + }; + + /** + The Node class structures a binary tree by storing pointers to a left + Node and a right Node, and storing its content as a + Function::Element, the name of an InputVariable or OutputVariable, or + a constant value. + */ + struct FL_API Node { + /**The node takes an operation or a function*/ + FL_unique_ptr<Element> element; + /**The node can have an expression tree on the left*/ + FL_unique_ptr<Node> left; + /**The node can have an expression tree on the right*/ + FL_unique_ptr<Node> right; + /**The node can refer to a variable by name*/ + std::string variable; + /**The node can take an arbitrary floating-point value*/ + scalar value; + + explicit Node(Element* element, Node* left = fl::null, Node* right = fl::null); + explicit Node(const std::string& variable); + explicit Node(scalar value); + Node(const Node& source); + Node& operator=(const Node& rhs); + virtual ~Node(); + FL_DEFAULT_MOVE(Node) + + /** + Evaluates the node and substitutes the variables therein for the + values passed in the map. The expression tree is evaluated + recursively. + + @param variables is a map of substitutions of variable names for + fl::scalar%s + + @return a fl::scalar indicating the result of the evaluation of + the node + */ + virtual scalar evaluate(const std::map<std::string, scalar>* + variables = fl::null) const; + + /** + Computes the size of the subtree under the given node. The complexity + of calling this method is O(n). + @param node is the root of the subtree, which is this node if + fl::null is given + @return the size of the subtree under the given node + */ + virtual std::size_t treeSize(const Node* node = fl::null) const; + + /** + Computes the size of the subtree under the given node whose elements + are of the given type. The complexity of calling this method is O(n). + @param type is the type of elements to account for + @param node is the root of the subtree, which is this node if + fl::null is given + @return + */ + virtual std::size_t treeSize(Element::Type type, + const Node* node = fl::null) const; + + /** + Creates a clone of the node. + @return a clone of the node + */ + virtual Node* clone() const; + + /** + Returns a string with the name of the element, the name of the + variable, or the constant value, accordingly. + @return a string with the name of the element, the name of the + variable, or the constant value, accordingly. + */ + virtual std::string toString() const; + /** + Returns a prefix representation of the expression tree under the + given node + @param node is the node to start the prefix representation from. + If the node is `fl::null`, then the starting point is `this` node + @return a prefix representation of the expression tree under the + given node + */ + virtual std::string toPrefix(const Node* node = fl::null) const; + /** + Returns an infix representation of the expression tree under the + given node + @param node is the node to start the infix representation from. + If the node is `fl::null`, then the starting point is `this` node + @return an infix representation of the expression tree under the + given node + */ + virtual std::string toInfix(const Node* node = fl::null) const; + /** + Returns a postfix representation of the expression tree under the + given node + @param node is the node to start the postfix representation from. + If the node is `fl::null`, then the starting point is `this` node + @return a postfix representation of the expression tree under the + given node + */ + virtual std::string toPostfix(const Node* node = fl::null) const; + private: + void copyFrom(const Node& source); + }; + + + + + /****************************** + * Term + ******************************/ + + private: + FL_unique_ptr<Node> _root; + std::string _formula; + const Engine* _engine; + public: + /**A map of variables and substitution values**/ + mutable std::map<std::string, scalar> variables; + explicit Function(const std::string& name = "", + const std::string& formula = "", const Engine* engine = fl::null); + Function(const Function& other); + Function& operator=(const Function& other); + virtual ~Function() FL_IOVERRIDE; + FL_DEFAULT_MOVE(Function) + + /** + Creates a Function term with the given parameters + @param name is the name of the term + @param formula is the formula defining the membership function + @param engine is the engine to which the Function can have access + @return a Function term configured with the given parameters + @throws fl::Exception if the formula has a syntax error + */ + static Function* create(const std::string& name, + const std::string& formula, + const Engine* engine = fl::null); + + virtual Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the membership function value of @f$x@f$ at the root node. + If the engine has been set, the current values of the input variables + and output variables are added to the map of Function::variables. In + addition, the variable @f$x@f$ will also be added to the map. + @param x + @return the membership function value of @f$x@f$ at the root node + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + + /** + Computes the function value of this term using the given map of + variable substitutions. + @param variables is a map of substitution variables + @return the function value of this term using the given map of + variable substitutions. + */ + virtual scalar evaluate(const std::map<std::string, scalar>* variables = fl::null) const; + + virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term as `formula` + @return `formula` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters given as `formula` + @param parameters as `formula` + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + /** + Sets the formula of the function + @param formula is the formula of the function + */ + virtual void setFormula(const std::string& formula); + /** + Gets the formula of the function + @return the formula of the function + */ + virtual std::string getFormula() const; + + /** + Sets the engine to which the formula can refer + @param engine is the engine to which the formula can refer + */ + virtual void setEngine(const Engine* engine); + /** + Gets the engine to which the formula can refer + @return the engine to which the formula can refer + */ + virtual const Engine* getEngine() const; + + /** + Gets the root node of the expression tree defining the Function. The + root is `fl::null` if the formula has not been loaded. + @return the root node of the expression tree defining the Function, + or `fl::null` if the formula has not been loaded + */ + virtual Node* root() const; + + /** + Indicates whether the formula is loaded + @return whether the formula is loaded + */ + virtual bool isLoaded() const; + /** + Unloads the formula and resets the map of substitution variables. + */ + virtual void unload(); + /** + Loads the current formula expressed in infix notation + */ + virtual void load(); + /** + Loads the given formula expressed in infix notation + @param formula is the right-hand side of a mathematical equation + @throws fl::Exception if the formula has syntax errors + */ + virtual void load(const std::string& formula); + /** + Loads the given formula expressed in infix notation, and sets the + engine holding the variables to which the formula refers. + @param formula is the right-hand side of a mathematical equation + expressed in infix notation + @param engine is the engine to which the formula can refer + @throws fl::Exception if the formula has syntax errors + */ + virtual void load(const std::string& formula, const Engine* engine); + /** + Creates a node representing a binary expression tree from the given formula + @param formula is the right-hand side of a mathematical equation + expressed in infix notation + @return a node representing a binary expression tree from the given formula + @throws fl::Exception if the formula has syntax errors + */ + virtual Node* parse(const std::string& formula); + + /** + Translates the given formula to postfix notation + @param formula is the right-hand side of a mathematical equation + expressed in infix notation + @return the formula represented in postfix notation + @throws fl::Exception if the formula has syntax errors + */ + virtual std::string toPostfix(const std::string& formula) const; + + /** + Adds spaces to the formula to separate parentheses, commas and + function operators such that these are treated as tokens when parsing + the function. + @param formula is the right-hand side of a mathematical equation + expressed in infix notation + @return the formula with spaces before and after parentheses, commas + and function operators + */ + virtual std::string space(const std::string& formula) const; + + virtual void updateReference(const Engine* engine) FL_IOVERRIDE; + + virtual Function* clone() const FL_IOVERRIDE; + + static Term* constructor(); + + }; +} +#endif /* FL_FUNCTION_H */ + diff --git a/fuzzylite/fl/term/Gaussian.h b/fuzzylite/fl/term/Gaussian.h new file mode 100644 index 0000000..9a7475d --- /dev/null +++ b/fuzzylite/fl/term/Gaussian.h @@ -0,0 +1,99 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_GAUSSIAN_H +#define FL_GAUSSIAN_H + +#include "fl/term/Term.h" + +namespace fl { + + /** + The Gaussian class is an extended Term that represents the %Gaussian + curve membership function. + + @image html gaussian.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ + class FL_API Gaussian : public Term { + private: + scalar _mean; + scalar _standardDeviation; + + public: + explicit Gaussian(const std::string& name = "", + scalar mean = fl::nan, + scalar standardDeviation = fl::nan, + scalar height = 1.0); + virtual ~Gaussian() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(Gaussian) + + virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"mean standardDeviation [height]"` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"mean standardDeviation [height]"` + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$ h \times \exp(-(x-\mu)^2/(2\sigma^2))@f$ + + where @f$h@f$ is the height of the Term, + @f$\mu@f$ is the mean of the Gaussian, + @f$\sigma@f$ is the standard deviation of the Gaussian + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + + /** + Sets the mean of the Gaussian curve + @param mean is the mean of the Gaussian curve + */ + virtual void setMean(scalar mean); + /** + Gets the mean of the Gaussian curve + @return the mean of the Gaussian curve + */ + virtual scalar getMean() const; + + /** + Sets the standard deviation of the Gaussian curve + @param standardDeviation is the standard deviation of the Gaussian curve + */ + virtual void setStandardDeviation(scalar standardDeviation); + /** + Gets the standard deviation of the Gaussian curve + @return the standard deviation of the Gaussian curve + */ + virtual scalar getStandardDeviation() const; + + virtual Gaussian* clone() const FL_IOVERRIDE; + + static Term* constructor(); + }; +} +#endif /* FL_GAUSSIAN_H */ diff --git a/fuzzylite/fl/term/GaussianProduct.h b/fuzzylite/fl/term/GaussianProduct.h new file mode 100644 index 0000000..e4a500c --- /dev/null +++ b/fuzzylite/fl/term/GaussianProduct.h @@ -0,0 +1,138 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_GAUSSIANPRODUCT_H +#define FL_GAUSSIANPRODUCT_H + +#include "fl/term/Term.h" + +namespace fl { + + /** + The GaussianProduct class is an extended Term that represents the + two-sided %Gaussian membership function. + + @image html gaussianProduct.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ + class FL_API GaussianProduct : public Term { + private: + scalar _meanA; + scalar _standardDeviationA; + scalar _meanB; + scalar _standardDeviationB; + + public: + explicit GaussianProduct(const std::string& name = "", + scalar meanA = fl::nan, + scalar standardDeviationA = fl::nan, + scalar meanB = fl::nan, + scalar standardDeviationB = fl::nan, + scalar height = 1.0); + virtual ~GaussianProduct() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(GaussianProduct) + + virtual std::string className() const FL_IOVERRIDE; + /** + Provides the parameters of the term + @return `"meanA standardDeviationA meanB standardDeviationB [height]"` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"meanA standardDeviationA meanB + standardDeviationB [height]"` + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$ h \left((1 - i) + i \times \exp(-(x - \mu_a)^2 / + (2\sigma_a^2))\right) + \left((1 - j) + j \times \exp(-(x - \mu_b)^2 / (2 \sigma_b)^2)\right) + @f$ + + where @f$h@f$ is the height of the Term, + @f$\mu_a@f$ is the mean of the first GaussianProduct, + @f$\sigma_a@f$ is the standard deviation of the first + GaussianProduct, + @f$\mu_b@f$ is the mean of the second GaussianProduct, + @f$\sigma_b@f$ is the standard deviation of the second + GaussianProduct, + @f$i=\begin{cases}1 & \mbox{if $x \leq \mu_a$} \cr 0 + &\mbox{otherwise}\end{cases}@f$, + @f$j=\begin{cases}1 & \mbox{if $x \geq \mu_b$} \cr 0 + &\mbox{otherwise}\end{cases}@f$ + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + + /** + Sets the mean of the first %Gaussian curve + @param meanA is the mean of the first %Gaussian curve + */ + virtual void setMeanA(scalar meanA); + /** + Gets the mean of the first %Gaussian curve + @return the mean of the first %Gaussian curve + */ + virtual scalar getMeanA() const; + + /** + Sets the standard deviation of the first %Gaussian curve + @param standardDeviationA is the standard deviation of the first %Gaussian curve + */ + virtual void setStandardDeviationA(scalar standardDeviationA); + /** + Gets the standard deviation of the first %Gaussian curve + @return the standard deviation of the first %Gaussian curve + */ + virtual scalar getStandardDeviationA() const; + + /** + Sets the mean of the second %Gaussian curve + @param meanB is the mean of the second %Gaussian curve + */ + virtual void setMeanB(scalar meanB); + /** + Gets the mean of the second %Gaussian curve + @return the mean of the second %Gaussian curve + */ + virtual scalar getMeanB() const; + + /** + Sets the standard deviation of the second %Gaussian curve + @param standardDeviationB is the standard deviation of the second %Gaussian curve + */ + virtual void setStandardDeviationB(scalar standardDeviationB); + /** + Gets the standard deviation of the second %Gaussian curve + @return the standard deviation of the second %Gaussian curve + */ + virtual scalar getStandardDeviationB() const; + + virtual GaussianProduct* clone() const FL_IOVERRIDE; + + static Term* constructor(); + }; +} +#endif /* FL_GAUSSIANPRODUCT_H */ + diff --git a/fuzzylite/fl/term/Linear.h b/fuzzylite/fl/term/Linear.h new file mode 100644 index 0000000..1105a1d --- /dev/null +++ b/fuzzylite/fl/term/Linear.h @@ -0,0 +1,179 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_LINEAR_H +#define FL_LINEAR_H + +#include "fl/term/Term.h" + +namespace fl { + class Engine; + + /** + The Linear class is a linear polynomial Term expressed as @f$f(x)= + \mathbf{c}\mathbf{v}+k = \sum_i c_iv_i + k@f$, where variable @f$x@f$ is + not utilized, @f$\mathbf{v}@f$ is a vector of values from the input + variables, @f$\mathbf{c}@f$ is a vector of coefficients, and @f$k@f$ is a + constant. Hereinafter, the vector @f$\mathbf{c}^\star=\{c_1, \ldots, c_i, + \ldots, c_n, k\}@f$ refers to a vector containing the coefficients of + @f$\mathbf{c}@f$ and the constant @f$k@f$. + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ + class FL_API Linear : public Term { + private: + /**Contains the coefficients @f$c_i@f$ and the constant @f$k@f$*/ + std::vector<scalar> _coefficients; + const Engine* _engine; + public: + explicit Linear(const std::string& name = "", + const std::vector<scalar>& coefficients = std::vector<scalar>(), + const Engine* engine = fl::null); + virtual ~Linear() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(Linear) + + virtual std::string className() const FL_IOVERRIDE; + + /** + Returns the vector @f$\mathbf{c}^\star@f$ + @return `"c1 ... ci ... cn k"` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the values of @f$\mathbf{c}^\star@f$ + @param parameters as `"c1 ... ci ... cn k"` + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the linear function @f$f(x)=\sum_i c_iv_i +k@f$, + where @f$v_i@f$ is the value of the input variable @f$i@f$ registered + in the Linear::getEngine() + @param x is not utilized + @return @f$\sum_i c_ix_i +k@f$ + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + + /** + Sets the vector @f$\mathbf{c}^\star@f$ and the Engine from which + vector @f$\mathbf{v}@f$ will be retrieved when necessary + @param coefficients is the vector @f$\mathbf{c}^\star@f$ + @param engine is the engine from which @f$\mathbf{v}@f$ will be + retrieved when necessary + */ + virtual void set(const std::vector<scalar>& coefficients, const Engine* engine); + + /** + Sets the vector @f$\mathbf{c}^\star@f$ of the linear function + @param coefficients is the vector @f$\mathbf{c}^\star@f$ + */ + virtual void setCoefficients(const std::vector<scalar>& coefficients); + /** + Gets the immutable vector @f$\mathbf{c}^\star@f$ + @return the immutable vector @f$\mathbf{c}^\star@f$ + */ + virtual const std::vector<scalar>& coefficients() const; + /** + Gets the mutable vector @f$\mathbf{c}^\star@f$ + @return the mutable vector @f$\mathbf{c}^\star@f$ + */ + virtual std::vector<scalar>& coefficients(); + + /** + Sets the engine from which the vector @f$\mathbf{v}@f$ will be + obtained upon computing the Linear::membership() + @param engine is the engine from which the vector @f$\mathbf{v}@f$ + will be obtained + */ + virtual void setEngine(const Engine* engine); + /** + Gets the engine from which the vector @f$\mathbf{v}@f$ will be + obtained upon computing the Linear::membership() + @return the engine from which the vector @f$\mathbf{v}@f$ will be + obtained + */ + virtual const Engine* getEngine() const; + + virtual Linear* clone() const FL_IOVERRIDE; + + virtual void updateReference(const Engine* engine) FL_IOVERRIDE; + + static Term* constructor(); + + /** + Creates a Linear term from a variadic set of coefficients. + Beware: this method is unsafe and must be used with care by + ensuring: + + - the data type of each variadic arguments is the same, e.g., + @f$(1.0, 2.0, 3.0)@f$ are all fl::scalar%s. You *need* to avoid the + case of @f$(1.0, 2, 3.0)@f$, where the second term will be + considered as an `int` (different from the others) and cause memory + issues due to the difference in size between an `int` and + `fl::scalar`; and + + - the number of variadic arguments is exactly the same as the number + of input variables in the engine plus one in order to match the + size of the vector @f$\mathbf{c}^\star@f$ + + @param name is the name of the term + @param engine is the engine from which the vector @f$\mathbf{v}@f$ + will be obtained + @param firstCoefficient is the coefficient for the first input + variable, namely @f$c_1@f$ + @param ... is a variadic number of coefficients whose type need to be + the same as the first coefficient + @return a new Linear term with the given parameters + */ + template <typename T> + static Linear* create(const std::string& name, const Engine* engine, + T firstCoefficient, ...); + }; +} + +/** + Template implementation + */ + +#include "fl/Engine.h" + +namespace fl { + + template <typename T> + inline Linear* Linear::create(const std::string& name, + const Engine* engine, T firstCoefficient, ...) { + if (not engine) throw Exception("[linear error] cannot create term <" + name + "> " + "without a reference to the engine", FL_AT); + std::vector<scalar> coefficients; + coefficients.push_back((scalar) firstCoefficient); + + va_list args; + va_start(args, firstCoefficient); + for (std::size_t i = 0; i < engine->inputVariables().size(); ++i) { + coefficients.push_back((scalar) va_arg(args, T)); + } + va_end(args); + + return new Linear(name, coefficients, engine); + } +} +#endif /* FL_LINEAR_H */ + diff --git a/fuzzylite/fl/term/PiShape.h b/fuzzylite/fl/term/PiShape.h new file mode 100644 index 0000000..24e98ed --- /dev/null +++ b/fuzzylite/fl/term/PiShape.h @@ -0,0 +1,137 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_PISHAPE_H +#define FL_PISHAPE_H + +#include "fl/term/Term.h" + +namespace fl { + + /** + The PiShape class is an extended Term that represents the Pi-shaped curve + membership function. + + @image html piShape.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ + class FL_API PiShape : public Term { + private: + scalar _bottomLeft; + scalar _topLeft; + scalar _topRight; + scalar _bottomRight; + + public: + explicit PiShape(const std::string& name = "", + scalar bottomLeft = fl::nan, + scalar topLeft = fl::nan, + scalar topRight = fl::nan, + scalar bottomRight = fl::nan, + scalar height = 1.0); + virtual ~PiShape() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(PiShape) + + virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"bottomLeft topLeft topRight bottomRight [height]"` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"bottomLeft topLeft topRight bottomRight + [height]"` + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$\begin{cases} + 0h & \mbox{if $x \leq b_l$}\cr + 2h \left((x - b_l) / (t_l-b_l)\right)^2 & \mbox{if $x \leq 0.5(a+b)$}\cr + h (1 - 2 \left((x - t_l) / (t_l-b_l)\right)^2) & \mbox{if $ x < t_l$}\cr + h & \mbox{if $x \leq t_r$}\cr + h (1 - 2\left((x - t_r) / (b_r - t_r)\right)^2) & \mbox{if $x \leq 0.5(t_r + b_r)$}\cr + 2h \left((x - b_r) / (b_r-t_r)\right)^2 & \mbox{if $x < b_r$} \cr + 0h & \mbox{otherwise} + \end{cases}@f$ + + where @f$h@f$ is the height of the Term, + @f$b_l@f$ is the bottom left of the PiShape, + @f$t_l@f$ is the top left of the PiShape, + @f$t_r@f$ is the top right of the PiShape + @f$b_r@f$ is the bottom right of the PiShape, + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + + /** + Sets the bottom-left value of the curve + @param bottomLeft is the bottom-left value of the curve + */ + virtual void setBottomLeft(scalar bottomLeft); + /** + Gets the bottom-left value of the curve + @return the bottom-left value of the curve + */ + virtual scalar getBottomLeft() const; + + /** + Sets the top-left value of the curve + @param topLeft is the top-left value of the curve + */ + virtual void setTopLeft(scalar topLeft); + /** + Gets the top-left value of the curve + @return the top-left value of the curve + */ + virtual scalar getTopLeft() const; + + /** + Sets the top-right value of the curve + @param topRight is the top-right value of the curve + */ + virtual void setTopRight(scalar topRight); + /** + Gets the top-right value of the curve + @return the top-right value of the curve + */ + virtual scalar getTopRight() const; + + /** + Sets the bottom-right value of the curve + @param bottomRight is the bottom-right value of the curve + */ + virtual void setBottomRight(scalar bottomRight); + /** + Gets the bottom-right value of the curve + @return the bottom-right value of the curve + */ + virtual scalar getBottomRight() const; + + virtual PiShape* clone() const FL_IOVERRIDE; + + static Term* constructor(); + }; +} +#endif /* FL_PISHAPE_H */ + diff --git a/fuzzylite/fl/term/Ramp.h b/fuzzylite/fl/term/Ramp.h new file mode 100644 index 0000000..dc49301 --- /dev/null +++ b/fuzzylite/fl/term/Ramp.h @@ -0,0 +1,135 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_RAMP_H +#define FL_RAMP_H + +#include "fl/term/Term.h" + +namespace fl { + + /** + The Ramp class is an edge Term that represents the ramp membership + function. + + @image html ramp.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ + class FL_API Ramp : public Term { + private: + scalar _start, _end; + + public: + + /** + Direction is an enumerator that indicates the direction of the ramp. + */ + enum Direction { + /** `(_/)` increases to the right */ Positive, + /** `(--)` slope is zero */ Zero, + /** `(\\_)` increases to the left */ Negative + }; + explicit Ramp(const std::string& name = "", + scalar start = fl::nan, + scalar end = fl::nan, + scalar height = 1.0); + virtual ~Ramp() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(Ramp) + + virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"start end [height]"` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"start end [height]"` + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return + @f$\begin{cases} + + 0h & \mbox{if $x = e$}\cr + + \begin{cases} + 0h & \mbox{if $x \leq s$}\cr + 1h & \mbox{if $x \geq e$}\cr + h (x - s) / (e - s) & \mbox{otherwise}\cr + \end{cases} & \mbox{if $s < e$}\cr + + \begin{cases} + 0h & \mbox{if $x \geq s$}\cr + 1h & \mbox{if $x \leq e$}\cr + h (s - x) / (s - e) & \mbox{otherwise} + \end{cases} & \mbox{if $s > e$}\cr + \end{cases}@f$ + + where @f$h@f$ is the height of the Term, + @f$s@f$ is the start of the Ramp, + @f$e@f$ is the end of the Ramp + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + + virtual scalar tsukamoto(scalar activationDegree, + scalar minimum, scalar maximum) const FL_IOVERRIDE; + + virtual bool isMonotonic() const FL_IOVERRIDE; + + /** + Sets the start of the ramp + @param start is the start of the ramp + */ + virtual void setStart(scalar start); + /** + Gets the start of the ramp + @return the start of the ramp + */ + virtual scalar getStart() const; + + /** + Sets the end of the ramp + @param end is the end of the ramp + */ + virtual void setEnd(scalar end); + /** + Gets the end of the ramp + @return the end of the ramp + */ + virtual scalar getEnd() const; + + /** + Returns the direction of the ramp + @return the direction of the ramp + */ + virtual Direction direction() const; + + virtual Ramp* clone() const FL_IOVERRIDE; + + static Term* constructor(); + }; +} +#endif /* FL_RAMP_H */ + diff --git a/fuzzylite/fl/term/Rectangle.h b/fuzzylite/fl/term/Rectangle.h new file mode 100644 index 0000000..0e29cd2 --- /dev/null +++ b/fuzzylite/fl/term/Rectangle.h @@ -0,0 +1,102 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_RECTANGLE_H +#define FL_RECTANGLE_H + +#include "fl/term/Term.h" + +namespace fl { + + /** + The Rectangle class is a basic Term that represents the rectangle + membership function. + + @image html rectangle.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ + class FL_API Rectangle : public Term { + private: + scalar _start, _end; + + public: + explicit Rectangle(const std::string& name = "", + scalar start = fl::nan, + scalar end = fl::nan, + scalar height = 1.0); + virtual ~Rectangle() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(Rectangle) + + virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"start end [height]"` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"start end [height]"` + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$\begin{cases} + 1h & \mbox{if $x \in [s, e]$} \cr + 0h & \mbox{otherwise} + \end{cases}@f$ + + where @f$h@f$ is the height of the Term, + @f$s@f$ is the start of the Rectangle, + @f$e@f$ is the end of the Rectangle. + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + + /** + Sets the start of the rectangle + @param start is the start of the rectangle + */ + virtual void setStart(scalar start); + /** + Gets the start of the rectangle + @return the start of the rectangle + */ + virtual scalar getStart() const; + + /** + Sets the end of the rectangle + @param end is the end of the rectangle + */ + virtual void setEnd(scalar end); + /** + Gets the end of the rectangle + @return the end of the rectangle + */ + virtual scalar getEnd() const; + + virtual Rectangle* clone() const FL_IOVERRIDE; + + static Term* constructor(); + }; +} +#endif /* FL_RECTANGLE_H */ diff --git a/fuzzylite/fl/term/SShape.h b/fuzzylite/fl/term/SShape.h new file mode 100644 index 0000000..1d107a5 --- /dev/null +++ b/fuzzylite/fl/term/SShape.h @@ -0,0 +1,109 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_SSHAPE_H +#define FL_SSHAPE_H + +#include "fl/term/Term.h" + +namespace fl { + + /** + The SShape class is an edge Term that represents the S-shaped membership + function. + + @image html sShape.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ + class FL_API SShape : public Term { + private: + scalar _start, _end; + + public: + explicit SShape(const std::string& name = "", + scalar start = fl::nan, + scalar end = fl::nan, + scalar height = 1.0); + virtual ~SShape() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(SShape) + + virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"start end [height]"` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"start end [height]"` + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$\begin{cases} + 0h & \mbox{if $x \leq s$} \cr + h(2 \left((x - s) / (e-s)\right)^2) & \mbox{if $x \leq 0.5(s+e)$}\cr + h(1 - 2\left((x - e) / (e-s)\right)^2) & \mbox{if $x < e$}\cr + 1h & \mbox{otherwise} + \end{cases}@f$ + + where @f$h@f$ is the height of the Term, + @f$s@f$ is the start of the SShape, + @f$e@f$ is the end of the SShape. + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + + virtual scalar tsukamoto(scalar activationDegree, + scalar minimum, scalar maximum) const FL_IOVERRIDE; + + virtual bool isMonotonic() const FL_IOVERRIDE; + + /** + Sets the start of the edge + @param start is the start of the edge + */ + virtual void setStart(scalar start); + /** + Gets the start of the edge + @return the start of the edge + */ + virtual scalar getStart() const; + + /** + Sets the end of the edge + @param end is the end of the edge + */ + virtual void setEnd(scalar end); + /** + Gets the end of the edge + @return the end of the edge + */ + virtual scalar getEnd() const; + + virtual SShape* clone() const FL_IOVERRIDE; + + static Term* constructor(); + }; +} +#endif /* FL_SSHAPE_H */ + diff --git a/fuzzylite/fl/term/Sigmoid.h b/fuzzylite/fl/term/Sigmoid.h new file mode 100644 index 0000000..bde9c2f --- /dev/null +++ b/fuzzylite/fl/term/Sigmoid.h @@ -0,0 +1,120 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_SIGMOID_H +#define FL_SIGMOID_H + +#include "fl/term/Term.h" + +namespace fl { + + /** + The Sigmoid class is an edge Term that represents the sigmoid membership + function. + + @image html sigmoid.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ + class FL_API Sigmoid : public Term { + private: + scalar _inflection; + scalar _slope; + public: + + /** + Direction is an enumerator that indicates the direction of the + sigmoid. + */ + enum Direction { + /** `(_/)` increases to the right */ Positive, + /** `(--)` slope is zero */ Zero, + /** `(\\_)` increases to the left */ Negative + }; + explicit Sigmoid(const std::string& name = "", + scalar inflection = fl::nan, + scalar slope = fl::nan, + scalar height = 1.0); + virtual ~Sigmoid() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(Sigmoid) + + virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"inflection slope [height]"` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"inflection slope [height]"` + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$ h / (1 + \exp(-s(x-i)))@f$ + + where @f$h@f$ is the height of the Term, + @f$s@f$ is the slope of the Sigmoid, + @f$i@f$ is the inflection of the Sigmoid + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + + virtual scalar tsukamoto(scalar activationDegree, + scalar minimum, scalar maximum) const FL_IOVERRIDE; + + virtual bool isMonotonic() const FL_IOVERRIDE; + + /** + Sets the inflection of the sigmoid + @param inflection is the inflection of the sigmoid + */ + virtual void setInflection(scalar inflection); + /** + Gets the inflection of the sigmoid + @return the inflection of the sigmoid + */ + virtual scalar getInflection() const; + + /** + Sets the slope of the sigmoid + @param slope is the slope of the sigmoid + */ + virtual void setSlope(scalar slope); + /** + Gets the slope of the sigmoid + @return the slope of the sigmoid + */ + virtual scalar getSlope() const; + + /** + Returns the direction of the sigmoid + @return the direction of the sigmoid + */ + virtual Direction direction() const; + + virtual Sigmoid* clone() const FL_IOVERRIDE; + + static Term* constructor(); + }; +} +#endif /* FL_SIGMOID_H */ diff --git a/fuzzylite/fl/term/SigmoidDifference.h b/fuzzylite/fl/term/SigmoidDifference.h new file mode 100644 index 0000000..4861584 --- /dev/null +++ b/fuzzylite/fl/term/SigmoidDifference.h @@ -0,0 +1,131 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_SIGMOIDDIFFERENCE_H +#define FL_SIGMOIDDIFFERENCE_H + +#include "fl/term/Term.h" + +namespace fl { + + /** + The SigmoidDifference class is an extended Term that represents the + difference between two sigmoidal membership functions. + + @image html sigmoidDifference.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ + class FL_API SigmoidDifference : public Term { + private: + scalar _left; + scalar _rising; + scalar _falling; + scalar _right; + + public: + explicit SigmoidDifference(const std::string& name = "", + scalar left = fl::nan, + scalar rising = fl::nan, + scalar falling = fl::nan, + scalar right = fl::nan, + scalar height = 1.0); + virtual ~SigmoidDifference() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(SigmoidDifference) + + virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"left rising falling right [height]"` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"left rising falling right [height]"` + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$ h (a-b)@f$ + + where @f$h@f$ is the height of the Term, + @f$a= 1 / (1 + \exp(-s_l \times (x - i_l))) @f$, + @f$b = 1 / (1 + \exp(-s_r \times (x - i_r)))@f$, + @f$i_l@f$ is the left inflection of the SigmoidDifference, + @f$s_l@f$ is the left slope of the SigmoidDifference, + @f$i_r@f$ is the right inflection of the SigmoidDifference, + @f$s_r@f$ is the right slope of the SigmoidDifference + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + + /** + Sets the inflection of the left sigmoidal curve + @param leftInflection is the inflection of the left sigmoidal curve + */ + virtual void setLeft(scalar leftInflection); + /** + Gets the inflection of the left sigmoidal curve + @return the inflection of the left sigmoidal curve + */ + virtual scalar getLeft() const; + + /** + Sets the slope of the left sigmoidal curve + @param risingSlope is the slope of the left sigmoidal curve + */ + virtual void setRising(scalar risingSlope); + /** + Gets the slope of the left sigmoidal curve + @return the slope of the left sigmoidal curve + */ + virtual scalar getRising() const; + + /** + Sets the slope of the right sigmoidal curve + @param fallingSlope is the slope of the right sigmoidal curve + */ + virtual void setFalling(scalar fallingSlope); + /** + Gets the slope of the right sigmoidal curve + @return the slope of the right sigmoidal curve + */ + virtual scalar getFalling() const; + + /** + Sets the inflection of the right sigmoidal curve + @param rightInflection is the inflection of the right sigmoidal curve + */ + virtual void setRight(scalar rightInflection); + /** + Gets the inflection of the right sigmoidal curve + @return the inflection of the right sigmoidal curve + */ + virtual scalar getRight() const; + + virtual SigmoidDifference* clone() const FL_IOVERRIDE; + + static Term* constructor(); + }; +} +#endif /* FL_SIGMOIDDIFFERENCE_H */ + diff --git a/fuzzylite/fl/term/SigmoidProduct.h b/fuzzylite/fl/term/SigmoidProduct.h new file mode 100644 index 0000000..19cd4d7 --- /dev/null +++ b/fuzzylite/fl/term/SigmoidProduct.h @@ -0,0 +1,132 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_SIGMOIDPRODUCT_H +#define FL_SIGMOIDPRODUCT_H + +#include "fl/term/Term.h" + +namespace fl { + + /** + The SigmoidProduct class is an extended Term that represents the product + of two sigmoidal membership functions. + + @image html sigmoidProduct.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ + class FL_API SigmoidProduct : public Term { + private: + scalar _left; + scalar _rising; + scalar _falling; + scalar _right; + + public: + explicit SigmoidProduct(const std::string& name = "", + scalar left = fl::nan, + scalar rising = fl::nan, + scalar falling = fl::nan, + scalar right = fl::nan, + scalar height = 1.0); + virtual ~SigmoidProduct() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(SigmoidProduct) + + virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"left rising falling right [height]"` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"left rising falling right [height]"` + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Complexity complexity() const FL_IOVERRIDE; + + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$ h (a \times b)@f$ + + where @f$h@f$ is the height, + @f$a= 1 / (1 + \exp(-s_l *\times (x - i_l))) @f$, + @f$b = 1 / (1 + \exp(-s_r \times (x - i_r)))@f$, + @f$i_l@f$ is the left inflection of the SigmoidProduct, + @f$s_l@f$ is the left slope of the SigmoidProduct, + @f$i_r@f$ is the right inflection of the SigmoidProduct, + @f$s_r@f$ is the right slope of the SigmoidProduct + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + + /** + Sets the inflection of the left sigmoidal curve + @param leftInflection is the inflection of the left sigmoidal curve + */ + virtual void setLeft(scalar leftInflection); + /** + Gets the inflection of the left sigmoidal curve + @return the inflection of the left sigmoidal curve + */ + virtual scalar getLeft() const; + + /** + Sets the slope of the left sigmoidal curve + @param risingSlope is the slope of the left sigmoidal curve + */ + virtual void setRising(scalar risingSlope); + /** + Gets the slope of the left sigmoidal curve + @return the slope of the left sigmoidal curve + */ + virtual scalar getRising() const; + + /** + Sets the slope of the right sigmoidal curve + @param fallingSlope is the slope of the right sigmoidal curve + */ + virtual void setFalling(scalar fallingSlope); + /** + Gets the slope of the right sigmoidal curve + @return the slope of the right sigmoidal curve + */ + virtual scalar getFalling() const; + + /** + Sets the inflection of the right sigmoidal curve + @param rightInflection is the inflection of the right sigmoidal curve + */ + virtual void setRight(scalar rightInflection); + /** + Gets the inflection of the right sigmoidal curve + @return the inflection of the right sigmoidal curve + */ + virtual scalar getRight() const; + + + virtual SigmoidProduct* clone() const FL_IOVERRIDE; + + static Term* constructor(); + }; +} +#endif /* FL_SIGMOIDPRODUCT_H */ + diff --git a/fuzzylite/fl/term/Spike.h b/fuzzylite/fl/term/Spike.h new file mode 100644 index 0000000..7349307 --- /dev/null +++ b/fuzzylite/fl/term/Spike.h @@ -0,0 +1,98 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_SPIKE_H +#define FL_SPIKE_H + +#include "fl/term/Term.h" + +namespace fl { + + /** + The Spike class is an extended Term that represents the spike membership + function. + + @image html spike.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 5.0 + */ + class FL_API Spike : public Term { + private: + scalar _center, _width; + public: + explicit Spike(const std::string& name = "", + scalar center = fl::nan, + scalar width = fl::nan, + scalar height = 1.0); + virtual ~Spike() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(Spike) + + virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"center width [height]"` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"center width [height]"` + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$h \times \exp(-|10 / w (x - c)|)@f$ + + where @f$h@f$ is the height of the Term, + @f$w@f$ is the width of the Spike, + @f$c@f$ is the center of the Spike + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + + /** + Sets the center of the spike + @param center is the center of the spike + */ + virtual void setCenter(scalar center); + /** + Gets the center of the spike + @return the center of the spike + */ + virtual scalar getCenter() const; + + /** + Sets the width of the spike + @param width is the width of the spike + */ + virtual void setWidth(scalar width); + /** + Gets the width of the spike + @return the width of the spike + */ + virtual scalar getWidth() const; + + virtual Spike* clone() const FL_IOVERRIDE; + + static Term* constructor(); + }; +} +#endif /* FL_SPIKE_H */ + diff --git a/fuzzylite/fl/term/Term.h b/fuzzylite/fl/term/Term.h new file mode 100644 index 0000000..31dcabd --- /dev/null +++ b/fuzzylite/fl/term/Term.h @@ -0,0 +1,165 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_TERM_H +#define FL_TERM_H + +#include "fl/fuzzylite.h" + +#include "fl/Operation.h" +#include "fl/Complexity.h" + +#include <cmath> +#include <string> +#include <vector> + +namespace fl { + class Engine; + + /** + The Term class is the abstract class for linguistic terms. The linguistic + terms in this library can be divided in four groups as: `basic`, + `extended`, `edge`, and `function`. The `basic` terms are Triangle, + Trapezoid, Rectangle, and Discrete. The `extended` terms are Bell, + Binary, Cosine, Gaussian, GaussianProduct, PiShape, SigmoidDifference, + SigmoidProduct, and Spike. The `edge` terms are Concave, Ramp, Sigmoid, + SShape, and ZShape. The `function` terms are Constant, Linear, and + Function. + + In the figure below, the `basic` terms are represented in the first + column, and the `extended` terms in the second and third columns. The + `edge` terms are represented in the fifth and sixth rows, and the + `function` terms in the last row. + + @image html terms.svg + + @author Juan Rada-Vilela, Ph.D. + @see Variable + @see InputVariable + @see OutputVariable + @since 4.0 + */ + class FL_API Term { + private: + std::string _name; + protected: + scalar _height; + public: + + explicit Term(const std::string& name = "", scalar height = 1.0); + virtual ~Term(); + FL_DEFAULT_COPY_AND_MOVE(Term) + + /** + Sets the name of the term + @param name is the name of term + */ + virtual void setName(const std::string& name); + /** + Gets the name of the term + @return the name of the term + */ + virtual std::string getName() const; + + /** + Sets the height of the term + @param height is the height of the term + */ + virtual void setHeight(scalar height); + /** + Gets the height of the term + @return the height of the term + */ + virtual scalar getHeight() const; + + /** + Returns the representation of the term in the FuzzyLite Language + @return the representation of the term in FuzzyLite Language + @see FllExporter + */ + virtual std::string toString() const; + + /** + Returns the name of the class of the term + @return the name of the class of the term + */ + virtual std::string className() const = 0; + /** + Returns the parameters to configure the term. The parameters are + separated by spaces. If there is one additional parameter, the + parameter will be considered as the height of the term; otherwise, + the height will be set to @f$1.0@f$ + @return the parameters to configure the term (@see Term::configure()) + */ + virtual std::string parameters() const = 0; + /** + Configures the term with the given parameters. The parameters are + separated by spaces. If there is one additional parameter, the + parameter will be considered as the height of the term; otherwise, + the height will be set to @f$1.0@f$ + @param parameters is the parameters to configure the term + */ + virtual void configure(const std::string& parameters) = 0; + + /** + Computes the estimated complexity of evaluating the membership function + @return the estimated complexity of evaluating the membership function + */ + virtual Complexity complexity() const = 0; + + /** + Computes the membership function value at @f$x@f$ + @param x + @return the membership function value @f$\mu(x)@f$ + */ + virtual scalar membership(scalar x) const = 0; + + /** + Creates a clone of the term + @return a clone of the term + */ + virtual Term* clone() const = 0; + + /** + Updates the references (if any) to point to the current engine (useful + when cloning engines or creating terms within Importer objects + @param engine is the engine to which this term belongs to + */ + virtual void updateReference(const Engine* engine); + + /** + For monotonic terms, computes the tsukamoto value of the term for the + given activation degree @f$\alpha@f$, that is, + @f$ g_j(\alpha) = \{ z \in\mathbb{R} : \mu_j(z) = \alpha \} $@f. If + the term is not monotonic (or does not override this method) the + method computes the membership function @f$\mu(\alpha)@f$. + @param activationDegree is the activationDegree + @param minimum is the minimum value of the range of the term + @param maximum is the maximum value of the range of the term + @return the tsukamoto value of the term for the given activation degree + if the term is monotonic (or overrides this method), or + the membership function for the activation degree otherwise. + */ + virtual scalar tsukamoto(scalar activationDegree, scalar minimum, scalar maximum) const; + + /** + Indicates whether the term is monotonic. + @return whether the term is monotonic. + */ + virtual bool isMonotonic() const; + }; +} +#endif /* FL_TERM_H */ diff --git a/fuzzylite/fl/term/Trapezoid.h b/fuzzylite/fl/term/Trapezoid.h new file mode 100644 index 0000000..ebf5ed8 --- /dev/null +++ b/fuzzylite/fl/term/Trapezoid.h @@ -0,0 +1,129 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_TRAPEZOID_H +#define FL_TRAPEZOID_H + +#include "fl/term/Term.h" + +namespace fl { + + /** + The Trapezoid class is a basic Term that represents the trapezoidal + membership function. + + @image html trapezoid.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ + class FL_API Trapezoid : public Term { + private: + scalar _vertexA, _vertexB, _vertexC, _vertexD; + public: + explicit Trapezoid(const std::string& name = "", + scalar vertexA = fl::nan, + scalar vertexB = fl::nan, + scalar vertexC = fl::nan, + scalar vertexD = fl::nan, + scalar height = 1.0); + virtual ~Trapezoid() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(Trapezoid) + + virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"vertexA vertexB vertexC vertexD [height]"` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"vertexA vertexB vertexC vertexD [height]"` + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$\begin{cases} + 0h & \mbox{if $x \not\in[a,d]$}\cr + h \times \min(1, (x - a) / (b - a)) & \mbox{if $x < b$}\cr + 1h & \mbox{if $x \leq c$}\cr + h (d - x) / (d - c) & \mbox{if $x < d$}\cr + 0h & \mbox{otherwise} + \end{cases}@f$ + + where @f$h@f$ is the height of the Term, + @f$a@f$ is the first vertex of the Trapezoid, + @f$b@f$ is the second vertex of the Trapezoid, + @f$c@f$ is the third vertex of the Trapezoid, + @f$d@f$ is the fourth vertex of the Trapezoid + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + + /** + Sets the first vertex of the trapezoid + @param a is the first vertex of the trapezoid + */ + virtual void setVertexA(scalar a); + /** + Gets the first vertex of the trapezoid + @return the first vertex of the trapezoid + */ + virtual scalar getVertexA() const; + + /** + Sets the second vertex of the trapezoid + @param b is the second vertex of the trapezoid + */ + virtual void setVertexB(scalar b); + /** + Gets the second vertex of the trapezoid + @return the second vertex of the trapezoid + */ + virtual scalar getVertexB() const; + + /** + Sets the third vertex of the trapezoid + @param c is the third vertex of the trapezoid + */ + virtual void setVertexC(scalar c); + /** + Gets the third vertex of the trapezoid + @return the third vertex of the trapezoid + */ + virtual scalar getVertexC() const; + + /** + Sets the fourth vertex of the trapezoid + @param d is the fourth vertex of the trapezoid + */ + virtual void setVertexD(scalar d); + /** + Gets the fourth vertex of the trapezoid + @return the fourth vertex of the trapezoid + */ + virtual scalar getVertexD() const; + + virtual Trapezoid* clone() const FL_IOVERRIDE; + + static Term* constructor(); + }; +} +#endif /* FL_TRAPEZOID_H */ diff --git a/fuzzylite/fl/term/Triangle.h b/fuzzylite/fl/term/Triangle.h new file mode 100644 index 0000000..8b16fa9 --- /dev/null +++ b/fuzzylite/fl/term/Triangle.h @@ -0,0 +1,118 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_TRIANGLE_H +#define FL_TRIANGLE_H + +#include "fl/term/Term.h" + +namespace fl { + + /** + The Triangle class is a basic Term that represents the triangular + membership function. + + @image html triangle.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ + class FL_API Triangle : public Term { + private: + scalar _vertexA; + scalar _vertexB; + scalar _vertexC; + public: + explicit Triangle(const std::string& name = "", + scalar vertexA = fl::nan, + scalar vertexB = fl::nan, + scalar vertexC = fl::nan, + scalar height = 1.0); + virtual ~Triangle() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(Triangle) + + virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"vertexA vertexB vertexC [height]"` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"vertexA vertexB vertexC [height]"` + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$\begin{cases} + 0h & \mbox{if $x \not\in [a,c]$}\cr + 1h & \mbox{if $x = b$}\cr + h (x - a) / (b - a) & \mbox{if $x < b$} \cr + h (c - x) / (c - b) & \mbox{otherwise} + \end{cases}@f$ + + where @f$h@f$ is the height of the Term, + @f$a@f$ is the first vertex of the Triangle, + @f$b@f$ is the second vertex of the Triangle, + @f$c@f$ is the third vertex of the Triangle + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + + /** + Sets the first vertex of the triangle + @param a is the first vertex of the triangle + */ + virtual void setVertexA(scalar a); + /** + Gets the first vertex of the triangle + @return the first vertex of the triangle + */ + virtual scalar getVertexA() const; + + /** + Sets the second vertex of the triangle + @param b is the second vertex of the triangle + */ + virtual void setVertexB(scalar b); + /** + Gets the second vertex of the triangle + @return the second vertex of the triangle + */ + virtual scalar getVertexB() const; + + /** + Sets the third vertex of the triangle + @param c is the third vertex of the triangle + */ + virtual void setVertexC(scalar c); + /** + Gets the third vertex of the triangle + @return the third vertex of the triangle + */ + virtual scalar getVertexC() const; + + virtual Triangle* clone() const FL_IOVERRIDE; + + static Term* constructor(); + + }; +} +#endif /* FL_TRIANGLE_H */ diff --git a/fuzzylite/fl/term/ZShape.h b/fuzzylite/fl/term/ZShape.h new file mode 100644 index 0000000..1737891 --- /dev/null +++ b/fuzzylite/fl/term/ZShape.h @@ -0,0 +1,111 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_ZSHAPE_H +#define FL_ZSHAPE_H + +#include "fl/term/Term.h" + +namespace fl { + + /** + The ZShape class is an edge Term that represents the Z-shaped membership + function. + + @image html zShape.svg + + @author Juan Rada-Vilela, Ph.D. + @see Term + @see Variable + @since 4.0 + */ + + class FL_API ZShape : public Term { + private: + scalar _start, _end; + + public: + explicit ZShape(const std::string& name = "", + scalar _start = fl::nan, + scalar _end = fl::nan, + scalar _height = 1.0); + virtual ~ZShape() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(ZShape) + + virtual std::string className() const FL_IOVERRIDE; + /** + Returns the parameters of the term + @return `"start end [height]"` + */ + virtual std::string parameters() const FL_IOVERRIDE; + /** + Configures the term with the parameters + @param parameters as `"start end [height]"` + */ + virtual void configure(const std::string& parameters) FL_IOVERRIDE; + + virtual Complexity complexity() const FL_IOVERRIDE; + /** + Computes the membership function evaluated at @f$x@f$ + @param x + @return @f$ \begin{cases} + 1h & \mbox{if $x \leq s$} \cr + h(1 - 2\left((x - s) / (e-s)\right)^2) & \mbox{if $x \leq 0.5(s+e)$}\cr + h(2 \left((x - e) / (e-s)\right)^2) & \mbox{if $x < e$}\cr + 0h & \mbox{otherwise} + \end{cases}@f$ + + where @f$h@f$ is the height of the Term, + @f$s@f$ is the start of the ZShape, + @f$e@f$ is the end of the ZShape. + */ + virtual scalar membership(scalar x) const FL_IOVERRIDE; + + virtual scalar tsukamoto(scalar activationDegree, + scalar minimum, scalar maximum) const FL_IOVERRIDE; + + virtual bool isMonotonic() const FL_IOVERRIDE; + + /** + Sets the start of the edge + @param start is the start of the edge + */ + virtual void setStart(scalar start); + /** + Gets the start of the edge + @return the start of the edge + */ + virtual scalar getStart() const; + + /** + Sets the end of the edge + @param end is the end of the edge + */ + virtual void setEnd(scalar end); + /** + Gets the end of the edge + @return the end of the edge + */ + virtual scalar getEnd() const; + + virtual ZShape* clone() const FL_IOVERRIDE; + + static Term* constructor(); + + }; +} +#endif /* ZSHAPE_H */ + diff --git a/fuzzylite/fl/variable/InputVariable.h b/fuzzylite/fl/variable/InputVariable.h new file mode 100644 index 0000000..3201e3d --- /dev/null +++ b/fuzzylite/fl/variable/InputVariable.h @@ -0,0 +1,60 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_INPUTVARIABLE_H +#define FL_INPUTVARIABLE_H + +#include "fl/variable/Variable.h" + +namespace fl { + + /** + The InputVariable class is a Variable that represents an input of the + fuzzy logic controller. + + @author Juan Rada-Vilela, Ph.D. + @see Variable + @see OutputVariable + @see Term + @since 4.0 + */ + class FL_API InputVariable : public Variable { + public: + explicit InputVariable(const std::string& name = "", + scalar minimum = -fl::inf, + scalar maximum = fl::inf); + virtual ~InputVariable() FL_IOVERRIDE; + FL_DEFAULT_COPY_AND_MOVE(InputVariable) + + /** + Evaluates the membership function of the current input value @f$x@f$ + for each term @f$i@f$, resulting in a fuzzy input value in the form + @f$\tilde{x}=\sum_i{\mu_i(x)/i}@f$. This is equivalent to a call to + Variable::fuzzify() passing @f$x@f$ as input value + + @return the fuzzy input value expressed as @f$\sum_i{\mu_i(x)/i}@f$ + */ + virtual std::string fuzzyInputValue() const; + + virtual Variable::Type type() const FL_IOVERRIDE; + + virtual std::string toString() const FL_IOVERRIDE; + + virtual InputVariable* clone() const FL_IOVERRIDE; + + }; +} +#endif /* FL_INPUTVARIABLE_H */ diff --git a/fuzzylite/fl/variable/OutputVariable.h b/fuzzylite/fl/variable/OutputVariable.h new file mode 100644 index 0000000..b79b13c --- /dev/null +++ b/fuzzylite/fl/variable/OutputVariable.h @@ -0,0 +1,226 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_OUTPUTVARIABLE_H +#define FL_OUTPUTVARIABLE_H + +#include "fl/variable/Variable.h" + +#include "fl/term/Activated.h" +#include "fl/term/Aggregated.h" + +#include "fl/defuzzifier/Defuzzifier.h" + +namespace fl { + + /** + The OutputVariable class is a Variable that represents an output of the + fuzzy logic controller. During the activation of a RuleBlock, the + Activated terms of each Rule will be Aggregated in the + OutputVariable::fuzzyOutput(), which represents a fuzzy set hereinafter + referred to as @f$\tilde{y}@f$. The defuzzification of @f$\tilde{y}@f$ + translates the fuzzy output value @f$\tilde{y}@f$ into a crisp output + value @f$y@f$, which can be retrieved using Variable::getValue(). The + value of the OutputVariable is computed and automatically stored when + calling OutputVariable::defuzzify(), but the value depends on the + following properties (expressed in the FuzzyLite Language): + + - Property `default: scalar` overrides the output value @f$y@f$ with + the given fl::scalar whenever the defuzzification process results in + a non-finite value (i.e., fl::nan and fl::inf). For example, + considering `default: 0.0`, if RuleBlock::activate() does not + activate any rules whose Consequent contribute to the OutputVariable, + then the fuzzy output value is empty, the Defuzzifier does not + operate, and hence @f$y=0.0@f$. By default, `default: NaN`. Relevant + methods are OutputVariable::getDefaultValue() and + OutputVariable::setDefaultValue(). + + - Property `lock-previous: boolean`, if enabled, overrides the output + value @f$y^t@f$ at time @f$t@f$ with the previously defuzzified valid + output value @f$y^{t-1}@f$ if defuzzification process results in a + non-finite value (i.e., fl::nan and fl::inf). When enabled, the + property takes precedence over `default` if @f$y^{t-1}@f$ is a finite + value. By default, `lock-previous: false`, @f$y^{t-1}=\mbox{NaN}@f$ + for @f$t=0@f$, and @f$y^{t-1}=\mbox{NaN}@f$ when + OutputVariable::clear(). Relevant methods are + OutputVariable::lockPreviousValue(), + OutputVariable::isLockPreviousValue, + OutputVariable::getPreviousValue(), and + OutputVariable::setPreviousValue(). + + - Property `lock-range: boolean` overrides the output value @f$y@f$ to + enforce it lies within the range of the variable determined by + Variable::getMinimum() and Variable::getMaximum(). When enabled, this + property takes precedence over `lock-previous` and `default`. For + example, considering `range: -1.0 1.0` and `lock-range: true`, + @f$y=-1.0@f$ if the result from the Defuzzifier is smaller than + `-1.0`, and @f$y=1.0@f$ if the result from the Defuzzifier is greater + than `1.0`. The property `lock-range` was introduced in version 5.0 + to substitute the property `lock-valid` in version 4.0. By default, + `lock-range: false`. Relevant methods are + Variable::lockValueInRange(), Variable::isLockValueInRange(), + Variable::getMinimum(), and Variable::getMaximum() + + + @author Juan Rada-Vilela, Ph.D. + @see Variable + @see InputVariable + @see RuleBlock::activate() + @see Term + @since 4.0 + */ + class FL_API OutputVariable : public Variable { + private: + FL_unique_ptr<Aggregated> _fuzzyOutput; + FL_unique_ptr<Defuzzifier> _defuzzifier; + scalar _previousValue; + scalar _defaultValue; + bool _lockPreviousValue; + + void copyFrom(const OutputVariable& other); + + public: + explicit OutputVariable(const std::string& name = "", + scalar minimum = -fl::inf, scalar maximum = fl::inf); + explicit OutputVariable(const OutputVariable& other); + OutputVariable& operator=(const OutputVariable& other); + virtual ~OutputVariable() FL_IOVERRIDE; + FL_DEFAULT_MOVE(OutputVariable) + + /** + Gets the fuzzy output value @f$\tilde{y}@f$ + @return the fuzzy output value @f$\tilde{y}@f$ + @todo rename to fuzzyValue + */ + virtual Aggregated* fuzzyOutput() const; + + virtual void setName(const std::string& name) FL_IOVERRIDE; + + virtual void setMinimum(scalar minimum) FL_IOVERRIDE; + virtual void setMaximum(scalar maximum) FL_IOVERRIDE; + + /** + Sets the defuzzifier of the output variable + @param defuzzifier is the defuzzifier of the output variable + */ + virtual void setDefuzzifier(Defuzzifier* defuzzifier); + /** + Gets the defuzzifier of the output variable + @return the defuzzifier of the output variable + */ + virtual Defuzzifier* getDefuzzifier() const; + + /** + Sets the aggregation operator + @param aggregation is the aggregation + */ + virtual void setAggregation(SNorm* aggregation); + + /** + Gets the aggregation operator + @return the aggregation operator + */ + virtual SNorm* getAggregation() const; + + /** + Sets the previous value of the output variable + @param previousValue is the previous value of the output variable + */ + virtual void setPreviousValue(scalar previousValue); + /** + Gets the previous value of the output variable + @return the previous value of the output variable + */ + virtual scalar getPreviousValue() const; + + /** + Sets the default value of the output variable + @param defaultValue is the default value of the output variable + */ + virtual void setDefaultValue(scalar defaultValue); + /** + Gets the default value of the output variable + @return the default value of the output variable + */ + virtual scalar getDefaultValue() const; + + /** + Sets whether to lock the previous value of the output variable + @param lockPreviousValue indicates whether to lock the previous value + of the output variable + */ + virtual void setLockPreviousValue(bool lockPreviousValue); + /** + Gets whether to lock the previous value of the output variable + @return whether the previous output value of the output variable is + locked + */ + virtual bool isLockPreviousValue() const; + + using Variable::complexity; + /** + Computes the estimated complexity of defuzzifying the activated term + with the current configuration of the variable (namely aggregation and + defuzzifier + @param term is the activated term + @return the estimated complexity of defuzzifying the activated term + with the current configuration of the variable + */ + virtual Complexity complexity(const Activated& term) const; + /** + Computes the estimated complexity of aggregating and defuzzifying all + the terms in the variable. + @return the estimated complexity of aggregating and defuzzifying all + the terms in the variable. + */ + virtual Complexity complexityOfDefuzzification() const; + + /** + Computes the estimated complexity of aggregating and defuzzifying the + terms currently found in the fuzzy output variable. + @return the estimated complexity of aggregating and defuzzifying the + terms currently found in the fuzzy output variable + */ + virtual Complexity currentComplexity() const; + + /** + Defuzzifies the output variable and stores the output value and the + previous output value + */ + virtual void defuzzify(); + + /** + Gets a string representation of the fuzzy output value @f$\tilde{y}@f$ + @return a string representation of the fuzzy output value + @f$\tilde{y}@f$ + */ + virtual std::string fuzzyOutputValue() const; + + /** + Clears the output variable by setting @f$\tilde{y}=\{\}@f$, + @f$y^{t}=\mbox{NaN}@f$, @f$y^{t-1}=\mbox{NaN}@f$ + */ + virtual void clear(); + + virtual Variable::Type type() const FL_IOVERRIDE; + + virtual std::string toString() const FL_IOVERRIDE; + + virtual OutputVariable* clone() const FL_IOVERRIDE; + + }; +} +#endif /* FL_OUTPUTVARIABLE_H */ diff --git a/fuzzylite/fl/variable/Variable.h b/fuzzylite/fl/variable/Variable.h new file mode 100644 index 0000000..47530c0 --- /dev/null +++ b/fuzzylite/fl/variable/Variable.h @@ -0,0 +1,298 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#ifndef FL_VARIABLE_H +#define FL_VARIABLE_H + +#include "fl/fuzzylite.h" +#include "fl/defuzzifier/Centroid.h" + +#include <string> +#include <vector> + +namespace fl { + + class Term; + + /** + The Variable class is the base class for linguistic variables. + + @author Juan Rada-Vilela, Ph.D. + @see InputVariable + @see OutputVariable + @see Term + @since 4.0 + */ + class FL_API Variable { + public: + + /** + Indicates the type of the variable to avoid costly `dynamic_casts` + */ + enum Type { + None, + Input, + Output + }; + protected: + std::string _name; + std::string _description; + std::vector<Term*> _terms; + scalar _value; + scalar _minimum, _maximum; + bool _enabled; + bool _lockValueInRange; + + private: + void copyFrom(const Variable& source); + + public: + explicit Variable(const std::string& name = "", + scalar minimum = -fl::inf, + scalar maximum = fl::inf); + explicit Variable(const Variable& other); + Variable& operator=(const Variable& other); + virtual ~Variable(); + FL_DEFAULT_MOVE(Variable) + + /** + Sets the name of the variable + @param name is the name of the variable + */ + virtual void setName(const std::string& name); + + /** + Gets the name of the variable + @return the name of the variable + */ + virtual std::string getName() const; + + /** + Gets the description of the variable + @return the description of the variable + */ + virtual std::string getDescription() const; + + /** + Sets the description of the variable + @param description is the description of the variable + */ + virtual void setDescription(const std::string& description); + + /** + Sets the value of the variable + @param value is the input value of an InputVariable, or the output + value of an OutputVariable + */ + virtual void setValue(scalar value); + + /** + Gets the value of the variable + @return the input value of an InputVariable, or the output value of + an OutputVariable + */ + virtual scalar getValue() const; + + /** + Sets the range of the variable between `[minimum, maximum]` + @param minimum is the minimum value in range + @param maximum is the maximum value in range + */ + virtual void setRange(scalar minimum, scalar maximum); + /** + Gets the magnitude of the range of the variable + @return `maximum - minimum` + */ + virtual scalar range() const; + + /** + Sets the minimum value of the range of the variable + @param minimum is the minimum value of the range + */ + virtual void setMinimum(scalar minimum); + /** + Gets the minimum value of the range of the variable + @return the minimum value of the range of the variable + */ + virtual scalar getMinimum() const; + + /** + Sets the maximum value of the range of the variable + @param maximum is the maximum value of the range + */ + virtual void setMaximum(scalar maximum); + /** + Gets the maximum value of the range of the variable + @return the maximum value of the range of the variable + */ + virtual scalar getMaximum() const; + + /** + Sets whether the variable is enabled + @param enabled determines whether to enable the variable + */ + virtual void setEnabled(bool enabled); + /** + Gets whether the variable is enabled + @return whether the variable is enabled + */ + virtual bool isEnabled() const; + + /** + Sets whether the variable locks the current value to the range of the + variable. + + If enabled in an InputVariable @f$i@f$, the input value @f$x_i@f$ + will be used when computing the Antecedent::activationDegree() as + long as @f$x_i \in [\mbox{min}, \mbox{max}]@f$. Else, for the case of + @f$x_i \not\in [\mbox{min}, \mbox{max}]@f$, the range values will be + used instead but without changing the input value @f$x_i@f$. + + If enabled in an OutputVariable @f$j@f$, the output value @f$y_j@f$ + will be overriden by the range values when @f$y_j \not\in + [\mbox{min}, \mbox{max}]@f$. See OutputVariable for more information. + + @param lockValueInRange indicates whether to lock the value to the + range of the variable + */ + virtual void setLockValueInRange(bool lockValueInRange); + + /** + Gets whether the variable locks the current value to the + range of the variable + + If enabled in an InputVariable @f$i@f$, the input value @f$x_i@f$ + will be used when computing the Antecedent::activationDegree() as + long as @f$x_i \in [\mbox{min}, \mbox{max}]@f$. Else, for the case of + @f$x_i \not\in [\mbox{min}, \mbox{max}]@f$, the range values will be + used instead but without changing the input value @f$x_i@f$. + + If enabled in an OutputVariable @f$j@f$, the output value @f$y_j@f$ + will be overriden by the range values when @f$y_j \not\in + [\mbox{min}, \mbox{max}]@f$. See OutputVariable for more information. + + @return whether the variable locks the current value to the range of + the variable + */ + virtual bool isLockValueInRange() const; + + /** + Computes the aggregated complexity of the underlying terms + @return the aggregated complexity of the underlying terms + */ + virtual Complexity complexity() const; + + /** + Evaluates the membership function of value @f$x@f$ for each + term @f$i@f$, resulting in a fuzzy value in the form + @f$\tilde{x}=\sum_i{\mu_i(x)/i}@f$ + @param x is the value to fuzzify + @return the fuzzy value expressed as @f$\sum_i{\mu_i(x)/i}@f$ + */ + virtual std::string fuzzify(scalar x) const; + + /** + Gets the term which has the highest membership function value for + @f$x@f$. + @param x is the value of interest + @param[out] yhighest is a pointer where the highest membership + function value will be stored + @return the term @f$i@f$ which maximimizes @f$\mu_i(x)@f$ + */ + virtual Term* highestMembership(scalar x, scalar* yhighest = fl::null) const; + + /** + Returns the type of the variable + @return the type of the variable + */ + virtual Type type() const; + + /** + Gets a string representation of the variable in the FuzzyLite Language + @return a string representation of the variable in the FuzzyLite + Language + @see FllExporter + */ + virtual std::string toString() const; + + /** + Sorts the terms in ascending order according to their centroids + */ + virtual void sort(); + /** + Adds a term to the variable + @param term is the term to add + */ + virtual void addTerm(Term* term); + /** + Inserts the term in the variable + @param term is the term to insert + @param index is the index where the term will be inserted + */ + virtual void insertTerm(Term* term, std::size_t index); + /** + Gets the term at the given index + @param index is the position of the term in the vector + @return the term at the given index + */ + virtual Term* getTerm(std::size_t index) const; + /** + Gets the term of the given name. + @param name is the name of the term to retrieve + @return the term of the given name + @throws Exception if the term is not found + */ + virtual Term* getTerm(const std::string& name) const; + /** + Gets whether a term of the given name has been added + @param name the name of the term + @return whether the term of the given name is found + */ + virtual bool hasTerm(const std::string& name) const; + /** + Removes the term at the given index + @param index the index of the term to remove + @return the removed term + */ + virtual Term* removeTerm(std::size_t index); + /** + Gets the number of terms added to the variable + @return the number of terms in the variable + */ + virtual std::size_t numberOfTerms() const; + /** + Sets the terms of the variable + @param terms is a vector of terms + */ + virtual void setTerms(const std::vector<Term*>& terms); + /** + Gets an immutable vector of the terms + @return an immutable vector of terms + */ + virtual const std::vector<Term*>& terms() const; + /** + Gets a mutable vector of the terms + @return a mutable vector of terms + */ + virtual std::vector<Term*>& terms(); + /** + Creates a clone of the variable + @return a clone of the variable + */ + virtual Variable* clone() const; + }; +} +#endif /* FL_VARIABLE_H */ diff --git a/fuzzylite/fuzzylite.1 b/fuzzylite/fuzzylite.1 new file mode 100644 index 0000000..22d08d0 --- /dev/null +++ b/fuzzylite/fuzzylite.1 @@ -0,0 +1,39 @@ +.TH fuzzylite 1 "October 26, 2014" "version 5.0" "USER COMMANDS" +.SH NAME +fuzzylite \- a fuzzy logic control library +.SH SYNOPSIS +.B fuzzylite +[\-i inputfile] [\-if format] [\-o outputfile] [\-of format] [\-example letter] [\-decimals number] [\-d datafile] [\-dmaximum number] [\-dheader boolean] [\-dinputs boolean] +.SH OPTIONS +.TP +\-i inputfile +file to import your engine from +.TP +\-if format +format of the file to import (fll | fis | fcl) +.TP +\-o outputfile +file to export your engine to +.TP +\-of format +format of the file to export (fll | fld | cpp | java | fis | fcl) +.TP +\-example letter +if not inputfile, built\-in example to use as engine: (m)amdani or (t)akagi\-sugeno +.TP +\-decimals number +number of decimals to write floating\-poing values +.TP +\-d datafile +if exporting to fld, file of input values to evaluate your engine on +.TP +\-dmaximum number +if exporting to fld without datafile, maximum number of results to export +.TP +\-dheader boolean +if true and exporting to fld, include headers +.TP +\-dinputs boolean +if true and exporting to fld, include input values +.SH AUTHOR +Juan Rada\-Vilela diff --git a/fuzzylite/fuzzylite.pc.in b/fuzzylite/fuzzylite.pc.in new file mode 100644 index 0000000..f6f7bee --- /dev/null +++ b/fuzzylite/fuzzylite.pc.in @@ -0,0 +1,10 @@ +prefix=@CMAKE_INSTALL_PREFIX@ +exec_prefix=${prefix} +includedir=${exec_prefix}/include +libdir=${exec_prefix}/lib + +Name: fuzzylite +Description: A fuzzy logic control library in C++ +Version: @FL_VERSION@ +Libs: -L${libdir} -lfuzzylite +Cflags: -I${includedir} diff --git a/fuzzylite/src/Benchmark.cpp b/fuzzylite/src/Benchmark.cpp new file mode 100644 index 0000000..9d318df --- /dev/null +++ b/fuzzylite/src/Benchmark.cpp @@ -0,0 +1,460 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/Benchmark.h" + +#include "fl/Engine.h" +#include "fl/Operation.h" +#include "fl/rule/Rule.h" +#include "fl/rule/RuleBlock.h" +#include "fl/variable/InputVariable.h" +#include "fl/variable/OutputVariable.h" + +#ifdef FL_CPP98 +//timing is only available in C++11 +#else +#include <chrono> +#endif + +namespace fl { + + Benchmark::Benchmark(const std::string& name, Engine* engine, scalar tolerance) + : _name(name), _engine(engine), _tolerance(tolerance) { } + + Benchmark::~Benchmark() { } + + void Benchmark::setName(const std::string& name) { + this->_name = name; + } + + std::string Benchmark::getName() const { + return this->_name; + } + + void Benchmark::setEngine(Engine* engine) { + this->_engine = engine; + } + + Engine* Benchmark::getEngine() const { + return this->_engine; + } + + void Benchmark::setExpected(const std::vector<std::vector<scalar> >& expected) { + this->_expected = expected; + } + + const std::vector<std::vector<scalar> >& Benchmark::getExpected() const { + return this->_expected; + } + + void Benchmark::setObtained(const std::vector<std::vector<scalar> >& obtained) { + this->_obtained = obtained; + } + + const std::vector<std::vector<scalar> >& Benchmark::getObtained() const { + return this->_obtained; + } + + void Benchmark::setTimes(const std::vector<scalar> nanoSeconds) { + this->_times = nanoSeconds; + } + + const std::vector<scalar>& Benchmark::getTimes() const { + return this->_times; + } + + void Benchmark::setTolerance(scalar tolerance) { + this->_tolerance = tolerance; + } + + scalar Benchmark::getTolerance() const { + return this->_tolerance; + } + + void Benchmark::prepare(int values, FldExporter::ScopeOfValues scope) { + if (not _engine) { + throw Exception("[benchmark error] engine not set before preparing for values and scope", FL_AT); + } + int resolution; + if (scope == FldExporter::AllVariables) + resolution = -1 + (int) std::max(1.0, std::pow( + values, 1.0 / _engine->numberOfInputVariables())); + else //if (scope == EachVariable) + resolution = values - 1; + + std::vector<int> sampleValues, minSampleValues, maxSampleValues; + for (std::size_t i = 0; i < _engine->numberOfInputVariables(); ++i) { + sampleValues.push_back(0); + minSampleValues.push_back(0); + maxSampleValues.push_back(resolution); + } + + _expected = std::vector<std::vector<scalar> >(); + do { + std::vector<scalar> expectedValues(_engine->numberOfInputVariables()); + for (std::size_t i = 0; i < _engine->numberOfInputVariables(); ++i) { + InputVariable* inputVariable = _engine->getInputVariable(i); + expectedValues.at(i) = inputVariable->getMinimum() + + sampleValues.at(i) * inputVariable->range() / std::max(1, resolution); + } + _expected.push_back(expectedValues); + } while (Op::increment(sampleValues, minSampleValues, maxSampleValues)); + } + + void Benchmark::prepare(std::istream& reader, long numberOfLines) { + _expected = std::vector<std::vector<scalar> >(); + std::string line; + int lineNumber = 0; + while (lineNumber != numberOfLines and std::getline(reader, line)) { + ++lineNumber; + line = Op::trim(line); + if (line.empty() or line.at(0) == '#') + continue; //comments are ignored, blank lines are retained + std::vector<scalar> expectedValues; + if (lineNumber == 1) { //automatic detection of header. + try { + expectedValues = Op::toScalars(line); + } catch (std::exception&) { + continue; + } + } else { + expectedValues = Op::toScalars(line); + } + _expected.push_back(expectedValues); + } + } + + scalar Benchmark::runOnce() { + return run(1).front(); + } + + std::vector<scalar> Benchmark::run(int times) { + if (not _engine) { + throw Exception("[benchmark error] engine not set for benchmark", FL_AT); + } + std::vector<scalar> runTimes(times, fl::nan); + const std::size_t offset(_engine->inputVariables().size()); + for (int t = 0; t < times; ++t) { + _obtained = std::vector<std::vector<scalar> >(_expected.size(), + std::vector<scalar>(_engine->variables().size())); + _engine->restart(); + +#ifdef FL_CPP98 + //ignore timing +#else + auto start = std::chrono::high_resolution_clock::now(); +#endif + + for (std::size_t evaluation = 0; evaluation < _expected.size(); ++evaluation) { + const std::vector<scalar>& expectedValues = _expected[evaluation]; + std::vector<scalar>& obtainedValues = _obtained[evaluation]; + + if (expectedValues.size() < _engine->inputVariables().size()) { + std::ostringstream ex; + ex << "[benchmark error] the number of input values given <" << + expectedValues.size() << "> at line <" << (evaluation + 1) << "> " + "must be at least the same number of input variables " + "<" << _engine->inputVariables().size() << "> in the engine"; + throw Exception(ex.str()); + } + for (std::size_t i = 0; i < _engine->inputVariables().size(); ++i) { + _engine->getInputVariable(i)->setValue(expectedValues[i]); + obtainedValues[i] = expectedValues[i]; + } + + _engine->process(); + + for (std::size_t i = 0; i < _engine->outputVariables().size(); ++i) { + obtainedValues[i + offset] = _engine->getOutputVariable(i)->getValue(); + } + } + +#ifdef FL_CPP98 + //ignore timing +#else + auto end = std::chrono::high_resolution_clock::now(); + runTimes.at(t) = std::chrono::duration<scalar, std::nano>(end - start).count(); +#endif + } + _times.insert(_times.end(), runTimes.begin(), runTimes.end()); + return runTimes; + } + + void Benchmark::reset() { + this->_obtained.clear(); + this->_times.clear(); + } + + bool Benchmark::canComputeErrors() const { + return not (_engine == fl::null or _expected.empty() or _obtained.empty() + or _expected.size() != _obtained.size() + or _expected.front().size() != _obtained.front().size() + or _expected.front().size() != _engine->variables().size()); + } + + scalar Benchmark::meanSquaredError() const { + return meanSquaredError(fl::null); + } + + scalar Benchmark::meanSquaredError(const OutputVariable* outputVariable) const { + if (not canComputeErrors()) { + return fl::nan; + } + + scalar mse = 0.0; + int errors = 0; + const std::size_t offset = _engine->numberOfInputVariables(); + for (std::size_t i = 0; i < _expected.size(); ++i) { + const std::vector<scalar>& e = _expected.at(i); + const std::vector<scalar>& o = _obtained.at(i); + + for (std::size_t y = 0; y < _engine->numberOfOutputVariables(); ++y) { + if (outputVariable == fl::null + or outputVariable == _engine->getOutputVariable(y)) { + scalar difference = e.at(offset + y) - o.at(offset + y); + if (Op::isFinite(difference) + and not Op::isEq(difference, 0.0, _tolerance)) { + mse += difference * difference; + ++errors; + } + } + } + } + + if (errors > 0) { + mse /= errors; + } + return mse; + } + + int Benchmark::allErrors() const { + return allErrors(fl::null); + } + + int Benchmark::allErrors(const OutputVariable* outputVariable) const { + return numberOfErrors(All, outputVariable); + } + + int Benchmark::nonFiniteErrors() const { + return nonFiniteErrors(fl::null); + } + + int Benchmark::nonFiniteErrors(const OutputVariable* outputVariable) const { + return numberOfErrors(NonFinite, outputVariable); + } + + int Benchmark::accuracyErrors() const { + return accuracyErrors(fl::null); + } + + int Benchmark::accuracyErrors(const OutputVariable* outputVariable) const { + return numberOfErrors(Accuracy, outputVariable); + } + + int Benchmark::numberOfErrors(ErrorType errorType) const { + return numberOfErrors(errorType, fl::null); + } + + int Benchmark::numberOfErrors(ErrorType errorType, + const OutputVariable* outputVariable) const { + if (not canComputeErrors()) { + return -1; + } + + int errors = 0; + const std::size_t offset = _engine->numberOfInputVariables(); + for (std::size_t i = 0; i < _expected.size(); ++i) { + const std::vector<scalar>& e = _expected.at(i); + const std::vector<scalar>& o = _obtained.at(i); + + for (std::size_t y = 0; y < _engine->numberOfOutputVariables(); ++y) { + if (outputVariable == fl::null + or outputVariable == _engine->getOutputVariable(y)) { + if (not Op::isEq(e.at(y + offset), o.at(y + offset), _tolerance)) { + scalar difference = e.at(y + offset) - o.at(y + offset); + if (errorType == Accuracy and Op::isFinite(difference)) { + ++errors; + } else if (errorType == NonFinite and not Op::isFinite(difference)) { + ++errors; + } else if (errorType == All) { + ++errors; + } + } + } + } + } + + return errors; + } + + std::string Benchmark::stringOf(TimeUnit unit) { + if (unit == NanoSeconds) return "nanoseconds"; + if (unit == MicroSeconds) return "microseconds"; + if (unit == MilliSeconds) return "milliseconds"; + if (unit == Seconds) return "seconds"; + if (unit == Minutes) return "minutes"; + if (unit == Hours) return "hours"; + return "undefined"; + } + + scalar Benchmark::factorOf(TimeUnit unit) { + if (unit == NanoSeconds) return 1.0; + else if (unit == MicroSeconds) return 1.0e-3; + else if (unit == MilliSeconds) return 1.0e-6; + else if (unit == Seconds) return 1.0e-9; + else if (unit == Minutes) return 1.0e-9 / 60; + else if (unit == Hours) return 1.0e-9 / 3600; + return fl::nan; + } + + scalar Benchmark::convert(scalar x, TimeUnit from, TimeUnit to) { + return x * factorOf(to) / factorOf(from); + } + + std::vector<std::string> Benchmark::header(int runs, bool includeErrors) { + Benchmark result; + + Engine dummy; + dummy.addOutputVariable(new OutputVariable); //canCompute() == true + result.setEngine(&dummy); + + result.setTimes(std::vector<scalar>(runs, fl::nan)); + + if (includeErrors) { + std::vector<std::vector<scalar> > dummyVector(1, + std::vector<scalar>(1, fl::nan)); + result.setExpected(dummyVector); + result.setObtained(dummyVector); + } + std::vector<Benchmark::Result> dummyResults = result.results(); + + std::vector<std::string> names; + for (std::size_t i = 0; i < dummyResults.size(); ++i) { + names.push_back(dummyResults.at(i).first); + } + return names; + } + + std::vector<Benchmark::Result> Benchmark::results(TimeUnit timeUnit, bool includeTimes) const { + return results(fl::null, timeUnit, includeTimes); + } + + std::vector<Benchmark::Result> Benchmark::results( + const OutputVariable* outputVariable, TimeUnit unit, bool includeTimes) const { + if (not _engine) { + throw Exception("[benchmark error] engine not set for benchmark", FL_AT); + } + + std::vector<scalar> time = _times; + + std::vector<Result> result; + result.push_back(Result("library", fuzzylite::library())); + result.push_back(Result("name", _name)); + result.push_back(Result("inputs", Op::str(_engine->numberOfInputVariables()))); + result.push_back(Result("outputs", Op::str(_engine->numberOfOutputVariables()))); + result.push_back(Result("ruleBlocks", Op::str(_engine->numberOfRuleBlocks()))); + std::size_t rules = 0; + for (std::size_t i = 0; i < _engine->ruleBlocks().size(); ++i) { + rules += _engine->ruleBlocks().at(i)->rules().size(); + } + result.push_back(Result("rules", Op::str(rules))); + result.push_back(Result("runs", Op::str(_times.size()))); + result.push_back(Result("evaluations", Op::str(_expected.size()))); + if (canComputeErrors()) { + std::vector<std::string> names; + scalar meanRange = 0.0; + scalar rmse = std::sqrt(meanSquaredError(outputVariable)); + scalar nrmse = 0.0; + scalar weights = 0.0; + for (std::size_t i = 0; i < _engine->outputVariables().size(); ++i) { + const OutputVariable* y = _engine->outputVariables().at(i); + if (outputVariable == fl::null or outputVariable == y) { + names.push_back(y->getName()); + meanRange += y->range(); + nrmse += std::sqrt(meanSquaredError(y)) * 1.0 / y->range(); + weights += 1.0 / y->range(); + } + } + meanRange /= names.size(); + nrmse /= weights; + + result.push_back(Result("outputVariable", Op::join(names, ","))); + result.push_back(Result("range", Op::str(meanRange))); + + result.push_back(Result("tolerance", Op::str(getTolerance(), -1, std::ios_base::fmtflags(0x0)))); + result.push_back(Result("errors", Op::str(allErrors(outputVariable)))); + + result.push_back(Result("nfErrors", Op::str(nonFiniteErrors(outputVariable)))); + result.push_back(Result("accErrors", Op::str(accuracyErrors(outputVariable)))); + + result.push_back(Result("rmse", Op::str(rmse, 6, std::ios_base::scientific))); + result.push_back(Result("nrmse", Op::str(nrmse, 6, std::ios_base::scientific))); + } + result.push_back(Result("units", stringOf(unit))); + result.push_back(Result("sum(t)", Op::str(convert(Op::sum(time), NanoSeconds, unit), + unit == NanoSeconds ? 0 : fuzzylite::decimals()))); + result.push_back(Result("mean(t)", Op::str(convert(Op::mean(time), NanoSeconds, unit)))); + result.push_back(Result("sd(t)", Op::str(convert(Op::standardDeviation(time), NanoSeconds, unit)))); + + if (includeTimes) { + for (std::size_t i = 0; i < time.size(); ++i) { + result.push_back(Result("t" + Op::str(i + 1), + Op::str(time.at(i), unit == NanoSeconds ? 0 : fuzzylite::decimals()))); + } + } + return result; + } + + std::string Benchmark::format(std::vector<Result> results, TableShape shape, + TableContents contents, const std::string& delimiter) const { + std::ostringstream os; + + if (shape == Vertical) { + for (std::size_t i = 0; i < results.size(); ++i) { + Result pair = results.at(i); + if (contents bitand Header) { + os << pair.first; + } + if (contents == HeaderAndBody) { + os << delimiter; + } + if (contents bitand Body) { + os << pair.second; + } + if (i + 1 < results.size()) os << "\n"; + } + + } else if (shape == Horizontal) { + std::ostringstream header; + std::ostringstream body; + for (std::size_t i = 0; i < results.size(); ++i) { + Result pair = results.at(i); + if (contents bitand Header) { + header << pair.first; + if (i + 1 < results.size()) header << delimiter; + } + if (contents bitand Body) { + body << pair.second; + if (i + 1 < results.size()) body << delimiter; + } + } + if (contents bitand Header) os << header.str(); + if (contents == HeaderAndBody) os << "\n"; + if (contents bitand Body) os << body.str(); + } + return os.str(); + } +} diff --git a/fuzzylite/src/Complexity.cpp b/fuzzylite/src/Complexity.cpp new file mode 100644 index 0000000..8a91c60 --- /dev/null +++ b/fuzzylite/src/Complexity.cpp @@ -0,0 +1,283 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/Complexity.h" + +#include "fl/Engine.h" + +#include "fl/variable/InputVariable.h" +#include "fl/variable/OutputVariable.h" + +#include "fl/rule/RuleBlock.h" +#include "fl/rule/Rule.h" + +namespace fl { + + Complexity::Complexity(scalar all) : + _comparison(all), _arithmetic(all), _function(all) { } + + Complexity::Complexity(scalar comparison, scalar arithmetic, + scalar function) + : _comparison(comparison), _arithmetic(arithmetic), _function(function) { } + + Complexity::~Complexity() { } + + Complexity& Complexity::operator+=(const Complexity& other) { + return this->plus(other); + } + + Complexity& Complexity::operator-=(const Complexity& other) { + return this->minus(other); + } + + Complexity& Complexity::operator*=(const Complexity& other) { + return this->multiply(other); + } + + Complexity& Complexity::operator/=(const Complexity& other) { + return this->divide(other); + } + + Complexity Complexity::operator+(const Complexity& rhs) const { + return Complexity(*this).plus(rhs); + } + + Complexity Complexity::operator-(const Complexity& rhs) const { + return Complexity(*this).minus(rhs); + } + + Complexity Complexity::operator*(const Complexity& rhs) const { + return Complexity(*this).multiply(rhs); + } + + Complexity Complexity::operator/(const Complexity& rhs) const { + return Complexity(*this).divide(rhs); + } + + bool Complexity::operator==(const Complexity& rhs) const { + return equals(rhs); + } + + bool Complexity::operator!=(const Complexity& rhs) const { + return not equals(rhs); + } + + bool Complexity::operator<(const Complexity& rhs) const { + return lessThan(rhs); + } + + bool Complexity::operator<=(const Complexity& rhs) const { + return lessThanOrEqualsTo(rhs); + } + + bool Complexity::operator>(const Complexity& rhs) const { + return greaterThan(rhs); + } + + bool Complexity::operator>=(const Complexity& rhs) const { + return greaterThanOrEqualsTo(rhs); + } + + Complexity& Complexity::plus(const Complexity& other) { + this->_arithmetic += other._arithmetic; + this->_comparison += other._comparison; + this->_function += other._function; + return *this; + } + + Complexity& Complexity::plus(scalar x) { + return this->plus(Complexity().arithmetic(x).comparison(x).function(x)); + } + + Complexity& Complexity::minus(const Complexity& other) { + this->_comparison -= other._comparison; + this->_arithmetic -= other._arithmetic; + this->_function -= other._function; + return *this; + } + + Complexity& Complexity::minus(scalar x) { + return this->minus(Complexity().arithmetic(x).comparison(x).function(x)); + } + + Complexity& Complexity::multiply(const Complexity& other) { + this->_comparison *= other._comparison; + this->_arithmetic *= other._arithmetic; + this->_function *= other._function; + return *this; + } + + Complexity& Complexity::multiply(scalar x) { + return this->multiply(Complexity().arithmetic(x).comparison(x).function(x)); + } + + Complexity& Complexity::divide(const Complexity& other) { + this->_comparison /= other._comparison; + this->_arithmetic /= other._arithmetic; + this->_function /= other._function; + return *this; + } + + Complexity& Complexity::divide(scalar x) { + return this->divide(Complexity().arithmetic(x).comparison(x).function(x)); + } + + bool Complexity::equals(const Complexity& x, scalar macheps) const { + return Op::isEq(_comparison, x._comparison, macheps) and + Op::isEq(_arithmetic, x._arithmetic, macheps) and + Op::isEq(_function, x._function, macheps); + } + + bool Complexity::lessThan(const Complexity& x, scalar macheps) const { + return Op::isLt(_comparison, x._comparison, macheps) and + Op::isLt(_arithmetic, x._arithmetic, macheps) and + Op::isLt(_function, x._function, macheps); + } + + bool Complexity::lessThanOrEqualsTo(const Complexity& x, scalar macheps) const { + return Op::isLE(_comparison, x._comparison, macheps) and + Op::isLE(_arithmetic, x._arithmetic, macheps) and + Op::isLE(_function, x._function, macheps); + } + + bool Complexity::greaterThan(const Complexity& x, scalar macheps) const { + return Op::isGt(_comparison, x._comparison, macheps) and + Op::isGt(_arithmetic, x._arithmetic, macheps) and + Op::isGt(_function, x._function, macheps); + } + + bool Complexity::greaterThanOrEqualsTo(const Complexity& x, scalar macheps) const { + return Op::isGE(_comparison, x._comparison, macheps) and + Op::isGE(_arithmetic, x._arithmetic, macheps) and + Op::isGE(_function, x._function, macheps); + } + + Complexity& Complexity::comparison(scalar comparison) { + this->_comparison += comparison; + return *this; + } + + void Complexity::setComparison(scalar comparison) { + this->_comparison = comparison; + } + + scalar Complexity::getComparison() const { + return _comparison; + } + + Complexity& Complexity::arithmetic(scalar arithmetic) { + this->_arithmetic += arithmetic; + return *this; + } + + void Complexity::setArithmetic(scalar arithmetic) { + this->_arithmetic = arithmetic; + } + + scalar Complexity::getArithmetic() const { + return _arithmetic; + } + + Complexity& Complexity::function(scalar trigonometric) { + this->_function += trigonometric; + return *this; + } + + void Complexity::setFunction(scalar trigonometric) { + this->_function = trigonometric; + } + + scalar Complexity::getFunction() const { + return _function; + } + + std::vector<Complexity::Measure> Complexity::measures() const { + std::vector<Measure> result; + result.push_back(Measure("arithmetic", _arithmetic)); + result.push_back(Measure("comparison", _comparison)); + result.push_back(Measure("function", _function)); + return result; + } + + scalar Complexity::sum() const { + return _arithmetic + _comparison + _function; + } + + scalar Complexity::norm() const { + return std::sqrt(Complexity(*this).multiply(*this).sum()); + } + + std::string Complexity::toString() const { + std::vector<std::string> result; + result.push_back("a=" + Op::str(_arithmetic)); + result.push_back("c=" + Op::str(_comparison)); + result.push_back("f=" + Op::str(_function)); + return "C[" + Op::join(result, ", ") + "]"; + } + + Complexity Complexity::compute(const Engine* engine) const { + return engine->complexity(); + } + + Complexity Complexity::compute(const InputVariable* inputVariable) const { + return inputVariable->complexity(); + } + + Complexity Complexity::compute(const OutputVariable* outputVariable) const { + return outputVariable->complexity(); + } + + Complexity Complexity::compute(const RuleBlock* ruleBlock) const { + return ruleBlock->complexity(); + } + + Complexity Complexity::compute(const std::vector<InputVariable*>& inputVariables) const { + Complexity result; + for (std::size_t i = 0; i < inputVariables.size(); ++i) { + result += inputVariables.at(i)->complexity(); + } + return result; + } + + Complexity Complexity::compute(const std::vector<OutputVariable*>& outputVariables, + bool complexityOfDefuzzification) const { + Complexity result; + for (std::size_t i = 0; i < outputVariables.size(); ++i) { + if (complexityOfDefuzzification) + result += outputVariables.at(i)->complexityOfDefuzzification(); + else + result += outputVariables.at(i)->complexity(); + } + return result; + } + + Complexity Complexity::compute(const std::vector<Variable*>& variables) const { + Complexity result; + for (std::size_t i = 0; i < variables.size(); ++i) { + result += variables.at(i)->complexity(); + } + return result; + } + + Complexity Complexity::compute(const std::vector<RuleBlock*>& ruleBlocks) const { + Complexity result; + for (std::size_t i = 0; i < ruleBlocks.size(); ++i) { + result += ruleBlocks.at(i)->complexity(); + } + return result; + } + +} diff --git a/fuzzylite/src/Console.cpp b/fuzzylite/src/Console.cpp new file mode 100644 index 0000000..5a00c50 --- /dev/null +++ b/fuzzylite/src/Console.cpp @@ -0,0 +1,1023 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/Console.h" + +#include "fl/Headers.h" + +#include <fstream> + +#ifdef FL_UNIX +#include <termios.h> +#include <unistd.h> +#elif defined(FL_WINDOWS) +#include <conio.h> +#endif + + +namespace fl { + const std::string Console::KW_INPUT_FILE = "-i"; + const std::string Console::KW_INPUT_FORMAT = "-if"; + const std::string Console::KW_OUTPUT_FILE = "-o"; + const std::string Console::KW_OUTPUT_FORMAT = "-of"; + const std::string Console::KW_EXAMPLE = "-example"; + const std::string Console::KW_DECIMALS = "-decimals"; + const std::string Console::KW_DATA_INPUT_FILE = "-d"; + const std::string Console::KW_DATA_VALUES = "-values"; + const std::string Console::KW_DATA_VALUES_SCOPE = "-scope"; + + const std::string Console::KW_DATA_EXPORT_HEADER = "-dheader"; + const std::string Console::KW_DATA_EXPORT_INPUTS = "-dinputs"; + + Console::Option::Option(const std::string& key, const std::string& value, const std::string& description) : + key(key), value(value), description(description) { } + + std::vector<Console::Option> Console::availableOptions() { + std::vector<Console::Option> options; + options.push_back(Option(KW_INPUT_FILE, "inputfile", "file to import your engine from")); + options.push_back(Option(KW_INPUT_FORMAT, "format", "format of the file to import (fll | fis | fcl)")); + options.push_back(Option(KW_OUTPUT_FILE, "outputfile", "file to export your engine to")); + options.push_back(Option(KW_OUTPUT_FORMAT, "format", "format of the file to export (fll | fld | cpp | java | fis | fcl)")); + options.push_back(Option(KW_EXAMPLE, "letter", "if not inputfile, built-in example to use as engine: (m)amdani or (t)akagi-sugeno")); + options.push_back(Option(KW_DECIMALS, "number", "number of decimals to write floating-poing values")); + options.push_back(Option(KW_DATA_INPUT_FILE, "file", "if exporting to fld, FLD file of input values to evaluate your engine on")); + options.push_back(Option(KW_DATA_VALUES, "number", "if exporting to fld without datafile, number of results to export within scope (default: EachVariable)")); + options.push_back(Option(KW_DATA_VALUES_SCOPE, "scope", "if exporting to fld without datafile, scope of " + KW_DATA_VALUES + ": [EachVariable|AllVariables]")); + options.push_back(Option(KW_DATA_EXPORT_HEADER, "boolean", "if true and exporting to fld, include headers")); + options.push_back(Option(KW_DATA_EXPORT_INPUTS, "boolean", "if true and exporting to fld, include input values")); + return options; + } + + std::string Console::usage() { + std::vector<Console::Option> options = availableOptions(); + std::ostringstream ss; + + ss << "========================================\n"; + ss << "fuzzylite: a fuzzy logic control library\n"; + ss << "version: " << fuzzylite::version() << "\n"; + ss << "author: " << fuzzylite::author() << "\n"; + ss << "license: " << fuzzylite::license() << "\n"; + ss << "========================================\n\n"; + ss << "usage: fuzzylite inputfile outputfile\n"; + ss << " or: fuzzylite benchmark engine.fll input.fld runs [output.tsv]\n"; + ss << " or: fuzzylite benchmarks fllFiles.txt fldFiles.txt runs [output.tsv]\n"; + ss << " or: fuzzylite "; + for (std::size_t i = 0; i < options.size(); ++i) { + ss << "[" << options.at(i).key << " " << options.at(i).value << "] "; + } + ss << "\n\nwhere:\n"; + for (std::size_t i = 0; i < options.size(); ++i) { + std::string spacedKey(12, ' '); + std::string key = options.at(i).key; + std::copy(key.begin(), key.end(), spacedKey.begin()); + + std::string spacedValue(13, ' '); + std::string value = options.at(i).value; + std::copy(value.begin(), value.end(), spacedValue.begin()); + + std::string description = options.at(i).description; + + ss << spacedKey << spacedValue << description << "\n"; + } + + ss << "\n"; + ss << "Visit " << fuzzylite::website() << " for more information.\n\n"; + ss << "Copyright (C) 2010-2017 by FuzzyLite Limited.\n"; + ss << "All rights reserved."; + + return ss.str(); + } + + std::map<std::string, std::string> Console::parse(int argc, const char* argv[]) { + if ((argc - 1) % 2 != 0) { + throw Exception("[option error] incomplete number of parameters [key value]", FL_AT); + } + std::map<std::string, std::string> options; + for (int i = 1; i < argc - 1; i += 2) { + std::string key = std::string(argv[i]); + std::string value = std::string(argv[i + 1]); + options[key] = value; + } + if (options.size() == 1) { + std::map<std::string, std::string>::const_iterator it = options.begin(); + if (it->first.at(0) != '-') { + options[KW_INPUT_FILE] = it->first; + options[KW_OUTPUT_FILE] = it->second; + } + } else { + std::vector<Console::Option> validOptions = availableOptions(); + + for (std::map<std::string, std::string>::const_iterator it = options.begin(); + it != options.end(); ++it) { + bool isValid = false; + for (std::size_t i = 0; i < validOptions.size(); ++i) { + std::string key = validOptions.at(i).key; + if (key == it->first) { + isValid = true; + break; + } + } + if (not isValid) { + throw Exception("[option error] option <" + it->first + "> not recognized", FL_AT); + } + } + } + return options; + } + + void Console::process(const std::map<std::string, std::string>& options) { + std::map<std::string, std::string>::const_iterator it; + + it = options.find(KW_DECIMALS); + if (it != options.end()) { + fuzzylite::setDecimals((int) Op::toScalar(it->second)); + } + + std::string example; + std::string inputFormat; + std::ostringstream textEngine; + + it = options.find(KW_EXAMPLE); + + bool isExample = (it != options.end()); + + if (isExample) { + example = it->second; + Engine* engine; + if (example == "m" or example == "mamdani") { + engine = mamdani(); + } else if (example == "t" or example == "ts" or example == "takagi-sugeno") { + engine = takagiSugeno(); + } else { + throw Exception("[option error] example <" + example + "> not available", FL_AT); + } + inputFormat = "fll"; + textEngine << FllExporter().toString(engine); + delete engine; + + } else { + it = options.find(KW_INPUT_FILE); + if (it == options.end()) { + throw Exception("[option error] no input file specified", FL_AT); + } + std::string inputFilename = it->second; + std::ifstream inputFile(inputFilename.c_str()); + if (not inputFile.is_open()) { + throw Exception("[file error] file <" + inputFilename + "> could not be opened", FL_AT); + } + std::string line; + while (std::getline(inputFile, line)) { + textEngine << line << std::endl; + } + inputFile.close(); + + it = options.find(KW_INPUT_FORMAT); + if (it != options.end()) { + inputFormat = it->second; + } else { + std::size_t extensionIndex = inputFilename.find_last_of("."); + if (extensionIndex != std::string::npos) { + inputFormat = inputFilename.substr(extensionIndex + 1); + } else { + throw Exception("[format error] unspecified format of input file", FL_AT); + } + } + } + + std::string outputFilename; + it = options.find(KW_OUTPUT_FILE); + if (it != options.end()) { + outputFilename = it->second; + } + + std::string outputFormat; + it = options.find(KW_OUTPUT_FORMAT); + if (it != options.end()) { + outputFormat = it->second; + } else { + std::size_t extensionIndex = outputFilename.find_last_of("."); + if (extensionIndex != std::string::npos) { + outputFormat = outputFilename.substr(extensionIndex + 1); + } else { + throw Exception("[format error] unspecified format of output file", FL_AT); + } + } + + + if (outputFilename.empty()) { + process(textEngine.str(), std::cout, inputFormat, outputFormat, options); + } else { + std::ofstream writer(outputFilename.c_str()); + if (not writer.is_open()) { + throw Exception("[file error] file <" + outputFilename + "> could not be created", FL_AT); + } + process(textEngine.str(), writer, inputFormat, outputFormat, options); + writer.flush(); + writer.close(); + } + } + + void Console::process(const std::string& input, std::ostream& writer, + const std::string& inputFormat, const std::string& outputFormat, + const std::map<std::string, std::string>& options) { + FL_unique_ptr<Importer> importer; + FL_unique_ptr<Exporter> exporter; + FL_unique_ptr<Engine> engine; + + if ("fll" == inputFormat) { + importer.reset(new FllImporter); + } else if ("fcl" == inputFormat) { + importer.reset(new FclImporter); + } else if ("fis" == inputFormat) { + importer.reset(new FisImporter); + } else { + throw Exception("[import error] format <" + inputFormat + "> " + "not supported", FL_AT); + } + + engine.reset(importer->fromString(input)); + + if ("fld" == outputFormat) { + std::map<std::string, std::string>::const_iterator it; + + FldExporter fldExporter; + fldExporter.setSeparator(" "); + bool exportHeaders = true; + if ((it = options.find(KW_DATA_EXPORT_HEADER)) != options.end()) { + exportHeaders = ("true" == it->second); + } + fldExporter.setExportHeader(exportHeaders); + bool exportInputValues = true; + if ((it = options.find(KW_DATA_EXPORT_INPUTS)) != options.end()) { + exportInputValues = ("true" == it->second); + } + fldExporter.setExportInputValues(exportInputValues); + if ((it = options.find(KW_DATA_INPUT_FILE)) != options.end()) { + std::ifstream dataFile(it->second.c_str()); + if (not dataFile.is_open()) { + throw Exception("[export error] file <" + it->second + "> could not be opened", FL_AT); + } + try { + fldExporter.write(engine.get(), writer, dataFile); + } catch (std::exception& ex) { + FL_IUNUSED(ex); + dataFile.close(); + throw; + } + + } else { + if ((it = options.find(KW_DATA_VALUES)) != options.end()) { + int values = (int) Op::toScalar(it->second); + FldExporter::ScopeOfValues scope = FldExporter::EachVariable; + if ((it = options.find(KW_DATA_VALUES_SCOPE)) != options.end()) { + if ("AllVariables" == it->second) + scope = FldExporter::AllVariables; + else if ("EachVariable" == it->second) + scope = FldExporter::EachVariable; + else throw Exception("[export error] unknown scope of values <" + + it->second + ">", FL_AT); + } + fldExporter.write(engine.get(), writer, values, scope); + } else { + std::ostringstream buffer; + buffer << "#FuzzyLite Interactive Console (press H for help)\n"; + buffer << fldExporter.header(engine.get()) << "\n"; + bool showCout = &writer != &std::cout; + writer << buffer.str(); + if (showCout) std::cout << buffer.str(); + else writer.flush(); + interactive(writer, engine.get()); + } + } + } else { + if ("fll" == outputFormat) { + exporter.reset(new FllExporter); + } else if ("fcl" == outputFormat) { + exporter.reset(new FclExporter); + } else if ("fis" == outputFormat) { + exporter.reset(new FisExporter); + } else if ("cpp" == outputFormat) { + exporter.reset(new CppExporter); + } else if ("java" == outputFormat) { + exporter.reset(new JavaExporter); + } else throw Exception("[export error] format <" + outputFormat + "> " + "not supported", FL_AT); + writer << exporter->toString(engine.get()); + } + } + + int Console::readCharacter() { + int ch = 0; +#ifdef FL_UNIX + struct termios oldt, newt; + ::tcgetattr(STDIN_FILENO, &oldt); + newt = oldt; + newt.c_lflag &= ~(ICANON | ECHO); + ::tcsetattr(STDIN_FILENO, TCSANOW, &newt); + ch = ::getchar(); + ::tcsetattr(STDIN_FILENO, TCSANOW, &oldt); +#elif defined(FL_WINDOWS) + ch = ::_getch(); +#endif + return ch; + } + + void Console::interactive(std::ostream& writer, Engine* engine) { + std::ostringstream buffer; + buffer << ">"; + bool showCout = &writer != &std::cout; + const std::string space("\t"); + std::vector<scalar> inputValues; + std::ostringstream inputValue; + int ch = 0; + do { + writer << buffer.str(); + if (showCout) std::cout << buffer.str(); + else writer.flush(); + buffer.str(""); + + ch = readCharacter(); + + if (ch == EOF) { + break; + } + + if (std::isspace(ch)) { + scalar value = engine->getInputVariable(inputValues.size())->getValue(); + try { + value = Op::toScalar(inputValue.str()); + } catch (std::exception& ex) { + FL_IUNUSED(ex); + buffer << "[" << Op::str(value) << "]"; + } + buffer << space; + inputValue.str(""); + inputValues.push_back(value); + if (inputValues.size() == engine->inputVariables().size()) { + ch = 'P'; //fall through to process; + } else continue; + } + + if (not std::isgraph(ch)) continue; + + switch (ch) { + default: + inputValue << char(ch); + buffer << char(ch); + break; + case 'r': + case 'R': engine->restart(); + buffer << "#[Restart]"; + continue; //fall through + case 'd': + case 'D': inputValues.clear(); + buffer << "#[Discard]\n>"; + inputValue.str(""); + break; + case 'p': + case 'P': //Process + { + inputValue.str(""); + + for (std::size_t i = 0; i < inputValues.size(); ++i) { + InputVariable* inputVariable = engine->inputVariables().at(i); + inputVariable->setValue(inputValues.at(i)); + } + std::vector<scalar> missingInputs; + for (std::size_t i = inputValues.size(); i < engine->inputVariables().size(); ++i) { + InputVariable* inputVariable = engine->inputVariables().at(i); + missingInputs.push_back(inputVariable->getValue()); + } + inputValues.clear(); + buffer << Op::join(missingInputs, space); + if (not missingInputs.empty()) buffer << space; + buffer << "=" << space; + try { + engine->process(); + std::vector<scalar> outputValues; + for (std::size_t i = 0; i < engine->outputVariables().size(); ++i) { + OutputVariable* outputVariable = engine->outputVariables().at(i); + outputVariable->defuzzify(); + outputValues.push_back(outputVariable->getValue()); + } + buffer << Op::join(outputValues, space) << "\n>"; + + } catch (std::exception& ex) { + buffer << "#[Error: " << ex.what() << "]"; + } + break; + } + case 'q': + case 'Q': buffer << "#[Quit]\n"; + break; + case 'h': + case 'H': buffer << "\n>" << interactiveHelp() << "\n>"; + inputValue.str(""); + break; + } + } while (not (ch == 'Q' or ch == 'q' or ch == 4)); + writer << std::endl; + } + + std::string Console::interactiveHelp() { + return + "#Special Keys\n" + "#=============\n" + "#\tR\tRestart engine and discard current inputs\n" + "#\tD\tDiscard current inputs\n" + "#\tP\tProcess engine\n" + "#\tQ\tQuit interactive console\n" + "#\tH\tShow this help\n" + "#=============\n"; + } + + Engine* Console::mamdani() { + Engine* engine = new Engine; + engine->setName("simple-dimmer"); + engine->setDescription(""); + + InputVariable* ambient = new InputVariable; + ambient->setName("ambient"); + ambient->setDescription(""); + ambient->setEnabled(true); + ambient->setRange(0.000, 1.000); + ambient->setLockValueInRange(false); + ambient->addTerm(new Triangle("DARK", 0.000, 0.250, 0.500)); + ambient->addTerm(new Triangle("MEDIUM", 0.250, 0.500, 0.750)); + ambient->addTerm(new Triangle("BRIGHT", 0.500, 0.750, 1.000)); + engine->addInputVariable(ambient); + + OutputVariable* power = new OutputVariable; + power->setName("power"); + power->setDescription(""); + power->setEnabled(true); + power->setRange(0.000, 2.000); + power->setLockValueInRange(false); + power->setAggregation(new Maximum); + power->setDefuzzifier(new Centroid(200)); + power->setDefaultValue(fl::nan); + power->setLockPreviousValue(false); + power->addTerm(new Triangle("LOW", 0.000, 0.500, 1.000)); + power->addTerm(new Triangle("MEDIUM", 0.500, 1.000, 1.500)); + power->addTerm(new Triangle("HIGH", 1.000, 1.500, 2.000)); + engine->addOutputVariable(power); + + RuleBlock* ruleBlock = new RuleBlock; + ruleBlock->setName(""); + ruleBlock->setDescription(""); + ruleBlock->setEnabled(true); + ruleBlock->setConjunction(fl::null); + ruleBlock->setDisjunction(fl::null); + ruleBlock->setImplication(new Minimum); + ruleBlock->setActivation(new General); + ruleBlock->addRule(Rule::parse("if ambient is DARK then power is HIGH", engine)); + ruleBlock->addRule(Rule::parse("if ambient is MEDIUM then power is MEDIUM", engine)); + ruleBlock->addRule(Rule::parse("if ambient is BRIGHT then power is LOW", engine)); + engine->addRuleBlock(ruleBlock); + + return engine; + } + + Engine* Console::takagiSugeno() { + Engine* engine = new Engine; + engine->setName("approximation"); + engine->setDescription("approximation of sin(x)/x"); + + InputVariable* inputX = new InputVariable; + inputX->setName("inputX"); + inputX->setDescription("value of x"); + inputX->setEnabled(true); + inputX->setRange(0.000, 10.000); + inputX->setLockValueInRange(false); + inputX->addTerm(new Triangle("NEAR_1", 0.000, 1.000, 2.000)); + inputX->addTerm(new Triangle("NEAR_2", 1.000, 2.000, 3.000)); + inputX->addTerm(new Triangle("NEAR_3", 2.000, 3.000, 4.000)); + inputX->addTerm(new Triangle("NEAR_4", 3.000, 4.000, 5.000)); + inputX->addTerm(new Triangle("NEAR_5", 4.000, 5.000, 6.000)); + inputX->addTerm(new Triangle("NEAR_6", 5.000, 6.000, 7.000)); + inputX->addTerm(new Triangle("NEAR_7", 6.000, 7.000, 8.000)); + inputX->addTerm(new Triangle("NEAR_8", 7.000, 8.000, 9.000)); + inputX->addTerm(new Triangle("NEAR_9", 8.000, 9.000, 10.000)); + engine->addInputVariable(inputX); + + OutputVariable* outputFx = new OutputVariable; + outputFx->setName("outputFx"); + outputFx->setDescription("value of the approximation of x"); + outputFx->setEnabled(true); + outputFx->setRange(-1.000, 1.000); + outputFx->setLockValueInRange(false); + outputFx->setAggregation(fl::null); + outputFx->setDefuzzifier(new WeightedAverage("Automatic")); + outputFx->setDefaultValue(fl::nan); + outputFx->setLockPreviousValue(true); + outputFx->addTerm(new Constant("f1", 0.840)); + outputFx->addTerm(new Constant("f2", 0.450)); + outputFx->addTerm(new Constant("f3", 0.040)); + outputFx->addTerm(new Constant("f4", -0.180)); + outputFx->addTerm(new Constant("f5", -0.190)); + outputFx->addTerm(new Constant("f6", -0.040)); + outputFx->addTerm(new Constant("f7", 0.090)); + outputFx->addTerm(new Constant("f8", 0.120)); + outputFx->addTerm(new Constant("f9", 0.040)); + engine->addOutputVariable(outputFx); + + OutputVariable* trueValue = new OutputVariable; + trueValue->setName("trueValue"); + trueValue->setDescription("value of f(x)=sin(x)/x"); + trueValue->setEnabled(true); + trueValue->setRange(-1.060, 1.000); + trueValue->setLockValueInRange(false); + trueValue->setAggregation(fl::null); + trueValue->setDefuzzifier(new WeightedAverage("Automatic")); + trueValue->setDefaultValue(fl::nan); + trueValue->setLockPreviousValue(true); + trueValue->addTerm(Function::create("fx", "sin(inputX)/inputX", engine)); + engine->addOutputVariable(trueValue); + + OutputVariable* difference = new OutputVariable; + difference->setName("difference"); + difference->setDescription("error e=f(x) - f'(x)"); + difference->setEnabled(true); + difference->setRange(-1.000, 1.000); + difference->setLockValueInRange(false); + difference->setAggregation(fl::null); + difference->setDefuzzifier(new WeightedAverage("Automatic")); + difference->setDefaultValue(fl::nan); + difference->setLockPreviousValue(false); + difference->addTerm(Function::create("error", "outputFx-trueValue", engine)); + engine->addOutputVariable(difference); + + RuleBlock* ruleBlock = new RuleBlock; + ruleBlock->setName(""); + ruleBlock->setDescription(""); + ruleBlock->setEnabled(true); + ruleBlock->setConjunction(fl::null); + ruleBlock->setDisjunction(fl::null); + ruleBlock->setImplication(new AlgebraicProduct); + ruleBlock->setActivation(new General); + ruleBlock->addRule(Rule::parse("if inputX is NEAR_1 then outputFx is f1", engine)); + ruleBlock->addRule(Rule::parse("if inputX is NEAR_2 then outputFx is f2", engine)); + ruleBlock->addRule(Rule::parse("if inputX is NEAR_3 then outputFx is f3", engine)); + ruleBlock->addRule(Rule::parse("if inputX is NEAR_4 then outputFx is f4", engine)); + ruleBlock->addRule(Rule::parse("if inputX is NEAR_5 then outputFx is f5", engine)); + ruleBlock->addRule(Rule::parse("if inputX is NEAR_6 then outputFx is f6", engine)); + ruleBlock->addRule(Rule::parse("if inputX is NEAR_7 then outputFx is f7", engine)); + ruleBlock->addRule(Rule::parse("if inputX is NEAR_8 then outputFx is f8", engine)); + ruleBlock->addRule(Rule::parse("if inputX is NEAR_9 then outputFx is f9", engine)); + ruleBlock->addRule(Rule::parse("if inputX is any then trueValue is fx and difference is error", engine)); + engine->addRuleBlock(ruleBlock); + + return engine; + } + + Engine* Console::hybrid() { + Engine* engine = new Engine; + engine->setName("tipper"); + engine->setDescription("(service and food) -> (tip)"); + + InputVariable* service = new InputVariable; + service->setName("service"); + service->setDescription("quality of service"); + service->setEnabled(true); + service->setRange(0.000, 10.000); + service->setLockValueInRange(true); + service->addTerm(new Trapezoid("poor", 0.000, 0.000, 2.500, 5.000)); + service->addTerm(new Triangle("good", 2.500, 5.000, 7.500)); + service->addTerm(new Trapezoid("excellent", 5.000, 7.500, 10.000, 10.000)); + engine->addInputVariable(service); + + InputVariable* food = new InputVariable; + food->setName("food"); + food->setDescription("quality of food"); + food->setEnabled(true); + food->setRange(0.000, 10.000); + food->setLockValueInRange(true); + food->addTerm(new Trapezoid("rancid", 0.000, 0.000, 2.500, 7.500)); + food->addTerm(new Trapezoid("delicious", 2.500, 7.500, 10.000, 10.000)); + engine->addInputVariable(food); + + OutputVariable* mTip = new OutputVariable; + mTip->setName("mTip"); + mTip->setDescription("tip based on Mamdani inference"); + mTip->setEnabled(true); + mTip->setRange(0.000, 30.000); + mTip->setLockValueInRange(false); + mTip->setAggregation(new Maximum); + mTip->setDefuzzifier(new Centroid(100)); + mTip->setDefaultValue(fl::nan); + mTip->setLockPreviousValue(false); + mTip->addTerm(new Triangle("cheap", 0.000, 5.000, 10.000)); + mTip->addTerm(new Triangle("average", 10.000, 15.000, 20.000)); + mTip->addTerm(new Triangle("generous", 20.000, 25.000, 30.000)); + engine->addOutputVariable(mTip); + + OutputVariable* tsTip = new OutputVariable; + tsTip->setName("tsTip"); + tsTip->setDescription("tip based on Takagi-Sugeno inference"); + tsTip->setEnabled(true); + tsTip->setRange(0.000, 30.000); + tsTip->setLockValueInRange(false); + tsTip->setAggregation(fl::null); + tsTip->setDefuzzifier(new WeightedAverage("TakagiSugeno")); + tsTip->setDefaultValue(fl::nan); + tsTip->setLockPreviousValue(false); + tsTip->addTerm(new Constant("cheap", 5.000)); + tsTip->addTerm(new Constant("average", 15.000)); + tsTip->addTerm(new Constant("generous", 25.000)); + engine->addOutputVariable(tsTip); + + RuleBlock* mamdaniRuleBlock = new RuleBlock; + mamdaniRuleBlock->setName("mamdani"); + mamdaniRuleBlock->setDescription("Mamdani inference"); + mamdaniRuleBlock->setEnabled(true); + mamdaniRuleBlock->setConjunction(new AlgebraicProduct); + mamdaniRuleBlock->setDisjunction(new AlgebraicSum); + mamdaniRuleBlock->setImplication(new Minimum); + mamdaniRuleBlock->setActivation(new General); + mamdaniRuleBlock->addRule(Rule::parse("if service is poor or food is rancid then mTip is cheap", engine)); + mamdaniRuleBlock->addRule(Rule::parse("if service is good then mTip is average", engine)); + mamdaniRuleBlock->addRule(Rule::parse("if service is excellent or food is delicious then mTip is generous with 0.5", engine)); + mamdaniRuleBlock->addRule(Rule::parse("if service is excellent and food is delicious then mTip is generous with 1.0", engine)); + engine->addRuleBlock(mamdaniRuleBlock); + + RuleBlock* takagiSugenoRuleBlock = new RuleBlock; + takagiSugenoRuleBlock->setName("takagiSugeno"); + takagiSugenoRuleBlock->setDescription("Takagi-Sugeno inference"); + takagiSugenoRuleBlock->setEnabled(true); + takagiSugenoRuleBlock->setConjunction(new AlgebraicProduct); + takagiSugenoRuleBlock->setDisjunction(new AlgebraicSum); + takagiSugenoRuleBlock->setImplication(fl::null); + takagiSugenoRuleBlock->setActivation(new General); + takagiSugenoRuleBlock->addRule(Rule::parse("if service is poor or food is rancid then tsTip is cheap", engine)); + takagiSugenoRuleBlock->addRule(Rule::parse("if service is good then tsTip is average", engine)); + takagiSugenoRuleBlock->addRule(Rule::parse("if service is excellent or food is delicious then tsTip is generous with 0.5", engine)); + takagiSugenoRuleBlock->addRule(Rule::parse("if service is excellent and food is delicious then tsTip is generous with 1.0", engine)); + engine->addRuleBlock(takagiSugenoRuleBlock); + + return engine; + } + + void Console::exportAllExamples(const std::string& from, const std::string& to) { + Console::exportAllExamples(from, to, "./", "/tmp/"); + } + + void Console::exportAllExamples(const std::string& from, const std::string& to, + const std::string& sourcePath, const std::string& targetPath) { + std::vector<std::string> examples; + examples.push_back("mamdani/AllTerms"); + examples.push_back("mamdani/SimpleDimmer"); + examples.push_back("mamdani/Laundry"); + examples.push_back("mamdani/ObstacleAvoidance"); + examples.push_back("mamdani/SimpleDimmerChained"); + examples.push_back("mamdani/SimpleDimmerInverse"); + examples.push_back("mamdani/matlab/mam21"); + examples.push_back("mamdani/matlab/mam22"); + examples.push_back("mamdani/matlab/shower"); + examples.push_back("mamdani/matlab/tank"); + examples.push_back("mamdani/matlab/tank2"); + examples.push_back("mamdani/matlab/tipper"); + examples.push_back("mamdani/matlab/tipper1"); + examples.push_back("mamdani/octave/investment_portfolio"); + examples.push_back("mamdani/octave/mamdani_tip_calculator"); + examples.push_back("takagi-sugeno/approximation"); + examples.push_back("takagi-sugeno/ObstacleAvoidance"); + examples.push_back("takagi-sugeno/SimpleDimmer"); + examples.push_back("takagi-sugeno/matlab/fpeaks"); + examples.push_back("takagi-sugeno/matlab/invkine1"); + examples.push_back("takagi-sugeno/matlab/invkine2"); + examples.push_back("takagi-sugeno/matlab/juggler"); + examples.push_back("takagi-sugeno/matlab/membrn1"); + examples.push_back("takagi-sugeno/matlab/membrn2"); + examples.push_back("takagi-sugeno/matlab/slbb"); + examples.push_back("takagi-sugeno/matlab/slcp"); + examples.push_back("takagi-sugeno/matlab/slcp1"); + examples.push_back("takagi-sugeno/matlab/slcpp1"); + examples.push_back("takagi-sugeno/matlab/sltbu_fl"); + examples.push_back("takagi-sugeno/matlab/sugeno1"); + examples.push_back("takagi-sugeno/matlab/tanksg"); + examples.push_back("takagi-sugeno/matlab/tippersg"); + examples.push_back("takagi-sugeno/octave/cubic_approximator"); + examples.push_back("takagi-sugeno/octave/heart_disease_risk"); + examples.push_back("takagi-sugeno/octave/linear_tip_calculator"); + examples.push_back("takagi-sugeno/octave/sugeno_tip_calculator"); + examples.push_back("tsukamoto/tsukamoto"); + examples.push_back("hybrid/tipper"); + examples.push_back("hybrid/ObstacleAvoidance"); + + FL_unique_ptr<Importer> importer; + if (from == "fll") importer.reset(new FllImporter); + else if (from == "fis") importer.reset(new FisImporter); + else if (from == "fcl") importer.reset(new FclImporter); + else throw Exception("[examples error] unrecognized format <" + from + "> to import", FL_AT); + + FL_unique_ptr<Exporter> exporter; + if (to == "fll") exporter.reset(new FllExporter); + else if (to == "fld") exporter.reset(new FldExporter(" ")); + else if (to == "fcl") exporter.reset(new FclExporter); + else if (to == "fis") exporter.reset(new FisExporter); + else if (to == "cpp") exporter.reset(new CppExporter); + else if (to == "java") exporter.reset(new JavaExporter); + else if (to == "R") exporter.reset(new RScriptExporter()); + else throw Exception("[examples error] unrecognized format <" + to + "> to export", FL_AT); + + std::vector<std::pair<Exporter*, Importer*> > tests; + tests.push_back(std::pair<Exporter*, Importer*>(new FllExporter, new FllImporter)); + tests.push_back(std::pair<Exporter*, Importer*>(new FisExporter, new FisImporter)); + tests.push_back(std::pair<Exporter*, Importer*>(new FclExporter, new FclImporter)); + for (std::size_t i = 0; i < examples.size(); ++i) { + std::string example = examples.at(i); + FL_LOG((i + 1) << "/" << examples.size()); + FL_LOG("Importing from: " << sourcePath << "/" << example << "." << from); + std::ostringstream ss; + std::string input = sourcePath + "/" + example + "." + from; + std::ifstream source(input.c_str()); + if (source.is_open()) { + std::string line; + while (source.good()) { + std::getline(source, line); + ss << line << "\n"; + } + source.close(); + } else throw Exception("[examples error] file not found: " + input, FL_AT); + + FL_unique_ptr<Engine> engine(importer->fromString(ss.str())); + + for (std::size_t t = 0; t < tests.size(); ++t) { + if ("mamdani/Laundry" == example + or "mamdani/SimpleDimmerInverse" == example + or "mamdani/SimpleDimmerChained" == example + or "hybrid/tipper" == example + or "hybrid/ObstacleAvoidance" == example) { + if (tests.at(t).second->name() == FisImporter().name()) { + continue; + } + } + + std::string exported = tests.at(t).first->toString(engine.get()); + FL_unique_ptr<Engine> engineFromExport(tests.at(t).second->fromString(exported)); + std::string imported = tests.at(t).first->toString(engineFromExport.get()); + + if (exported != imported) { + std::ostringstream msg; + msg << "[imex error] different results <" + << tests.at(t).first->name() << "," << tests.at(t).second->name() << "> " + "at " << example << "." << from << ":\n"; + msg << "<Engine A>\n" << exported << "\n\n" << + "================================\n\n" << + "<Engine B>\n" << imported; + throw Exception(msg.str(), FL_AT); + } + } + + std::string output = targetPath + "/" + example + "." + to; + std::ofstream target(output.c_str()); + FL_LOG("Exporting to: " << output << "\n"); + if (target.is_open()) { + if (to == "cpp") { + target << "#include <fl/Headers.h>\n\n" + << "int main(int argc, char** argv){\n" + << exporter->toString(engine.get()) + << "\n}\n"; + } else if (to == "java") { + std::string className = example.substr(example.find_last_of('/') + 1); + target << "import com.fuzzylite.*;\n" + << "import com.fuzzylite.activation.*\n" + << "import com.fuzzylite.defuzzifier.*;\n" + << "import com.fuzzylite.factory.*;\n" + << "import com.fuzzylite.hedge.*;\n" + << "import com.fuzzylite.imex.*;\n" + << "import com.fuzzylite.norm.*;\n" + << "import com.fuzzylite.norm.s.*;\n" + << "import com.fuzzylite.norm.t.*;\n" + << "import com.fuzzylite.rule.*;\n" + << "import com.fuzzylite.term.*;\n" + << "import com.fuzzylite.variable.*;\n\n" + << "public class " << Op::validName(className) << "{\n" + << "public static void main(String[] args){\n" + << exporter->toString(engine.get()) + << "\n}\n}\n"; + } else if (to == "R") { + RScriptExporter* rScript = dynamic_cast<RScriptExporter*> (exporter.get()); + InputVariable* a = engine->getInputVariable(0); + InputVariable* b = engine->getInputVariable(1 % engine->numberOfInputVariables()); + std::string pathToDF = example.substr(example.find_last_of('/') + 1) + ".fld"; + rScript->writeScriptImportingDataFrame(engine.get(), target, + a, b, pathToDF, engine->outputVariables()); + } else { + target << exporter->toString(engine.get()); + } + target.close(); + } + Engine copyConstructor(*engine.get()); + FL_IUNUSED(copyConstructor); + Engine assignmentOperator = *engine.get(); + FL_IUNUSED(assignmentOperator); + } + FL_LOG("Please, make sure the output contains the following structure:\n" + "mkdir -p " << targetPath << "mamdani/matlab; " + "mkdir -p " << targetPath << "mamdani/octave; " + "mkdir -p " << targetPath << "takagi-sugeno/matlab; " + "mkdir -p " << targetPath << "takagi-sugeno/octave; " + "mkdir -p " << targetPath << "tsukamoto; " + "mkdir -p " << targetPath << "hybrid;"); + for (std::size_t i = 0; i < tests.size(); ++i) { + delete tests.at(i).first; + delete tests.at(i).second; + } + } + + void Console::benchmark(const std::string& fllFile, const std::string& fldFile, + int runs, std::ofstream* writer) const { + FL_unique_ptr<Engine> engine(FllImporter().fromFile(fllFile)); + std::ifstream reader(fldFile.c_str()); + if (not reader.is_open()) { + throw Exception("File <" + fldFile + "> could not be opened"); + } + Benchmark benchmark(engine->getName(), engine.get()); + benchmark.prepare(reader); + if (writer) { + FL_LOG("\tEvaluating on " << benchmark.getExpected().size() << + " values read from " << fldFile << " ..."); + } + for (int i = 0; i < runs; ++i) { + benchmark.runOnce(); + } + if (writer) { + FL_LOG("\tMean(t)=" << Op::mean(benchmark.getTimes()) << " nanoseconds"); + *writer << benchmark.format(benchmark.results(), + Benchmark::Horizontal, Benchmark::Body) << "\n"; + } else { + FL_LOGP(benchmark.format(benchmark.results(), + Benchmark::Horizontal, Benchmark::Body)); + } + } + + void Console::benchmarks(const std::string& fllFileList, + const std::string& fldFileList, int runs, std::ofstream* writer) const { + std::vector<std::string> fllFiles, fldFiles; + + { + std::ifstream fllReader(fllFileList.c_str()); + if (not fllReader.is_open()) { + throw Exception("[error] file <" + fllFileList + "> could not be opened"); + } + std::ifstream fldReader(fldFileList.c_str()); + if (not fldReader.is_open()) { + throw Exception("[error] file <" + fldFileList + "> could not be opened"); + } + + std::string fllLine, fldLine; + while (std::getline(fllReader, fllLine) and std::getline(fldReader, fldLine)) { + fllLine = Op::trim(fllLine); + fldLine = Op::trim(fldLine); + if (fllLine.empty() or fllLine[0] == '#') + continue; + fllFiles.push_back(fllLine); + fldFiles.push_back(fldLine); + } + } + + if (writer) { + *writer << Op::join(Benchmark().header(runs, true), "\t") << "\n"; + } else { + FL_LOGP(Op::join(Benchmark().header(runs, true), "\t")); + } + + for (std::size_t i = 0; i < fllFiles.size(); ++i) { + if (writer) { + FL_LOG("Benchmark " << (i + 1) << "/" << fllFiles.size() << ": " + << fllFiles.at(i)); + } + benchmark(fllFiles.at(i), fldFiles.at(i), runs, writer); + } + } + + int Console::main(int argc, const char* argv[]) { + fuzzylite::setLogging(true); + + Console console; + if (argc <= 2) { + FL_LOGP(console.usage() << "\n"); + return EXIT_SUCCESS; + } + + const std::string firstArgument(argv[1]); + if (firstArgument == "export-examples") { + std::string path = "."; + if (argc > 2) { + path = std::string(argv[2]); + } + std::string outputPath = "/tmp/"; + if (argc > 3) { + outputPath = std::string(argv[3]); + } + FL_LOG("Origin=" << path); + FL_LOG("Target=" << outputPath); + fuzzylite::setDecimals(3); + try { + FL_LOG("Processing fll->fll"); + console.exportAllExamples("fll", "fll", path, outputPath); + FL_LOG("Processing fll->fcl"); + console.exportAllExamples("fll", "fcl", path, outputPath); + FL_LOG("Processing fll->fis"); + console.exportAllExamples("fll", "fis", path, outputPath); + FL_LOG("Processing fll->cpp"); + console.exportAllExamples("fll", "cpp", path, outputPath); + FL_LOG("Processing fll->java"); + console.exportAllExamples("fll", "java", path, outputPath); + FL_LOG("Processing fll->R"); + console.exportAllExamples("fll", "R", path, outputPath); + fuzzylite::setDecimals(9); + FL_LOG("Processing fll->fld"); + console.exportAllExamples("fll", "fld", path, outputPath); + FL_LOG("Origin=" << path); + FL_LOG("Target=" << outputPath); + } catch (std::exception& ex) { + FL_LOGP(ex.what() << "\n"); + return EXIT_FAILURE; + } + return EXIT_SUCCESS; + + } else if (firstArgument == "benchmark") { + if (argc < 5) { + FL_LOG("[error] not enough parameters"); + return EXIT_FAILURE; + } + std::string fllFile(argv[2]); + std::string fldFile(argv[3]); + try { + int runs = (int) Op::toScalar(argv[4]); + if (argc > 5) { + std::string filename(argv[5]); + std::ofstream outputFile; + outputFile.open(filename.c_str()); + if (not outputFile.is_open()) { + FL_LOG("[error] cannot create file <" << filename << ">"); + return EXIT_FAILURE; + } + outputFile << Op::join(Benchmark().header(runs, true), "\t") << "\n"; + console.benchmark(fllFile, fldFile, runs, &outputFile); + } else { + FL_LOGP(Op::join(Benchmark().header(runs, true), "\t")); + console.benchmark(fllFile, fldFile, runs, fl::null); + } + } catch (std::exception& ex) { + FL_LOGP(ex.what() << "\n"); + return EXIT_FAILURE; + } + return EXIT_SUCCESS; + + } else if (firstArgument == "benchmarks") { + if (argc < 5) { + FL_LOG("[error] not enough parameters"); + return EXIT_FAILURE; + } + std::string fllFiles(argv[2]); + std::string fldFiles(argv[3]); + try { + int runs = (int) Op::toScalar(argv[4]); + if (argc > 5) { + std::string filename(argv[5]); + std::ofstream outputFile; + outputFile.open(filename.c_str()); + if (not outputFile.is_open()) { + FL_LOG("[error] cannot create file <" << filename << ">"); + return EXIT_FAILURE; + } + console.benchmarks(fllFiles, fldFiles, runs, &outputFile); + } else { + console.benchmarks(fllFiles, fldFiles, runs, fl::null); + } + } catch (std::exception& ex) { + FL_LOGP(ex.what() << "\n"); + return EXIT_FAILURE; + } + return EXIT_SUCCESS; + } + + //MAIN: + try { + std::map<std::string, std::string> options = console.parse(argc, argv); + console.process(options); + + } catch (std::exception& ex) { + FL_LOGP(ex.what() << "\n"); + return EXIT_FAILURE; + } + return EXIT_SUCCESS; + } + +} diff --git a/fuzzylite/src/Engine.cpp b/fuzzylite/src/Engine.cpp new file mode 100644 index 0000000..dbe4b17 --- /dev/null +++ b/fuzzylite/src/Engine.cpp @@ -0,0 +1,716 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/Engine.h" + +#include "fl/activation/General.h" +#include "fl/defuzzifier/WeightedAverage.h" +#include "fl/defuzzifier/WeightedSum.h" +#include "fl/factory/DefuzzifierFactory.h" +#include "fl/factory/FactoryManager.h" +#include "fl/imex/FllExporter.h" +#include "fl/norm/t/AlgebraicProduct.h" +#include "fl/rule/Consequent.h" +#include "fl/rule/Expression.h" +#include "fl/rule/Rule.h" +#include "fl/rule/RuleBlock.h" +#include "fl/term/Aggregated.h" +#include "fl/term/Constant.h" +#include "fl/term/Linear.h" +#include "fl/term/Ramp.h" +#include "fl/term/Sigmoid.h" +#include "fl/term/SShape.h" +#include "fl/term/ZShape.h" +#include "fl/variable/InputVariable.h" +#include "fl/variable/OutputVariable.h" + +namespace fl { + + Engine::Engine(const std::string& name) : _name(name) { } + + Engine::Engine(const Engine& other) : _name(""), _description("") { + copyFrom(other); + } + + Engine& Engine::operator=(const Engine& other) { + if (this != &other) { + for (std::size_t i = 0; i < _ruleBlocks.size(); ++i) + delete _ruleBlocks.at(i); + _ruleBlocks.clear(); + for (std::size_t i = 0; i < _outputVariables.size(); ++i) + delete _outputVariables.at(i); + _outputVariables.clear(); + for (std::size_t i = 0; i < _inputVariables.size(); ++i) + delete _inputVariables.at(i); + _inputVariables.clear(); + + copyFrom(other); + } + return *this; + } + + void Engine::copyFrom(const Engine& other) { + _name = other._name; + _description = other._description; + for (std::size_t i = 0; i < other._inputVariables.size(); ++i) + _inputVariables.push_back(new InputVariable(*other._inputVariables.at(i))); + for (std::size_t i = 0; i < other._outputVariables.size(); ++i) + _outputVariables.push_back(new OutputVariable(*other._outputVariables.at(i))); + + updateReferences(); + + for (std::size_t i = 0; i < other._ruleBlocks.size(); ++i) { + RuleBlock* ruleBlock = new RuleBlock(*other._ruleBlocks.at(i)); + try { + ruleBlock->loadRules(this); + } catch (...) { + //ignore + } + _ruleBlocks.push_back(ruleBlock); + } + } + + void Engine::updateReferences() const { + std::vector<Variable*> myVariables = variables(); + for (std::size_t i = 0; i < myVariables.size(); ++i) { + Variable* variable = myVariables.at(i); + for (std::size_t t = 0; t < variable->numberOfTerms(); ++t) { + variable->getTerm(t)->updateReference(this); + } + } + } + + Engine::~Engine() { + for (std::size_t i = 0; i < _ruleBlocks.size(); ++i) + delete _ruleBlocks.at(i); + for (std::size_t i = 0; i < _outputVariables.size(); ++i) + delete _outputVariables.at(i); + for (std::size_t i = 0; i < _inputVariables.size(); ++i) + delete _inputVariables.at(i); + } + + void Engine::configure(const std::string& conjunction, const std::string& disjunction, + const std::string& implication, const std::string& aggregation, + const std::string& defuzzifier, const std::string& activation) { + TNormFactory* tnormFactory = FactoryManager::instance()->tnorm(); + SNormFactory* snormFactory = FactoryManager::instance()->snorm(); + DefuzzifierFactory* defuzzFactory = FactoryManager::instance()->defuzzifier(); + ActivationFactory* activationFactory = FactoryManager::instance()->activation(); + + TNorm* conjunctionObject = tnormFactory->constructObject(conjunction); + SNorm* disjunctionObject = snormFactory->constructObject(disjunction); + TNorm* implicationObject = tnormFactory->constructObject(implication); + SNorm* aggregationObject = snormFactory->constructObject(aggregation); + Defuzzifier* defuzzifierObject = defuzzFactory->constructObject(defuzzifier); + Activation* activationObject = activationFactory->constructObject(activation); + + configure(conjunctionObject, disjunctionObject, + implicationObject, aggregationObject, defuzzifierObject, + activationObject); + } + + void Engine::configure(TNorm* conjunction, SNorm* disjunction, + TNorm* implication, SNorm* aggregation, Defuzzifier* defuzzifier, + Activation* activation) { + for (std::size_t i = 0; i < numberOfRuleBlocks(); ++i) { + RuleBlock* ruleBlock = ruleBlocks().at(i); + ruleBlock->setConjunction(conjunction ? conjunction->clone() : fl::null); + ruleBlock->setDisjunction(disjunction ? disjunction->clone() : fl::null); + ruleBlock->setImplication(implication ? implication->clone() : fl::null); + ruleBlock->setActivation(activation ? activation->clone() : new General); + } + + for (std::size_t i = 0; i < numberOfOutputVariables(); ++i) { + OutputVariable* outputVariable = getOutputVariable(i); + outputVariable->setDefuzzifier(defuzzifier ? defuzzifier->clone() : fl::null); + outputVariable->setAggregation(aggregation ? aggregation->clone() : fl::null); + } + if (defuzzifier) delete defuzzifier; + if (aggregation) delete aggregation; + if (implication) delete implication; + if (disjunction) delete disjunction; + if (conjunction) delete conjunction; + if (activation) delete activation; + } + + bool Engine::isReady(std::string* status) const { + std::ostringstream ss; + if (inputVariables().empty()) { + ss << "- Engine <" << getName() << "> has no input variables\n"; + } + for (std::size_t i = 0; i < inputVariables().size(); ++i) { + InputVariable* inputVariable = inputVariables().at(i); + if (not inputVariable) { + ss << "- Engine <" << getName() << "> has a fl::null input variable at index <" << i << ">\n"; + } + /*else if (inputVariable->terms().empty()) { + ignore because sometimes inputs can be empty: takagi-sugeno/matlab/slcpp1.fis + ss << "- Input variable <" << _inputVariables.at(i)->getName() << ">" + << " has no terms\n"; + }*/ + } + + if (outputVariables().empty()) { + ss << "- Engine <" << _name << "> has no output variables\n"; + } + for (std::size_t i = 0; i < outputVariables().size(); ++i) { + OutputVariable* outputVariable = outputVariables().at(i); + if (not outputVariable) { + ss << "- Engine <" << getName() << "> has a fl::null output variable at index <" << i << ">\n"; + } else { + if (outputVariable->terms().empty()) { + ss << "- Output variable <" << outputVariable->getName() << ">" + << " has no terms\n"; + } + Defuzzifier* defuzzifier = outputVariable->getDefuzzifier(); + if (not defuzzifier) { + ss << "- Output variable <" << outputVariable->getName() << ">" + << " has no defuzzifier\n"; + } + SNorm* aggregation = outputVariable->fuzzyOutput()->getAggregation(); + if (not aggregation and dynamic_cast<IntegralDefuzzifier*> (defuzzifier)) { + ss << "- Output variable <" << outputVariable->getName() << ">" + << " has no aggregation operator\n"; + } + } + } + + if (ruleBlocks().empty()) { + ss << "- Engine <" << getName() << "> has no rule blocks\n"; + } + for (std::size_t i = 0; i < ruleBlocks().size(); ++i) { + RuleBlock* ruleblock = ruleBlocks().at(i); + if (not ruleblock) { + ss << "- Engine <" << getName() << "> has a fl::null rule block at index <" << i << ">\n"; + } else { + if (ruleblock->rules().empty()) { + ss << "- Rule block " << (i + 1) << " <" << ruleblock->getName() << "> has no rules\n"; + } + int requiresConjunction = 0; + int requiresDisjunction = 0; + int requiresImplication = 0; + for (std::size_t r = 0; r < ruleblock->numberOfRules(); ++r) { + Rule* rule = ruleblock->getRule(r); + if (not rule) { + ss << "- Rule block " << (i + 1) << " <" << ruleblock->getName() + << "> has a fl::null rule at index <" << r << ">\n"; + } else { + std::size_t thenIndex = rule->getText().find(" " + Rule::thenKeyword() + " "); + std::size_t andIndex = rule->getText().find(" " + Rule::andKeyword() + " "); + std::size_t orIndex = rule->getText().find(" " + Rule::orKeyword() + " "); + if (andIndex != std::string::npos and andIndex < thenIndex) { + ++requiresConjunction; + } + if (orIndex != std::string::npos and orIndex < thenIndex) { + ++requiresDisjunction; + } + if (rule->isLoaded()) { + Consequent* consequent = rule->getConsequent(); + for (std::size_t c = 0; c < consequent->conclusions().size(); ++c) { + Proposition* proposition = consequent->conclusions().at(c); + const OutputVariable* outputVariable = + dynamic_cast<const OutputVariable*> (proposition->variable); + if (outputVariable and dynamic_cast<IntegralDefuzzifier*> (outputVariable->getDefuzzifier())) { + ++requiresImplication; + break; + } + } + } + } + } + const TNorm* conjunction = ruleblock->getConjunction(); + if (requiresConjunction > 0 and not conjunction) { + ss << "- Rule block " << (i + 1) << " <" << ruleblock->getName() << "> has no conjunction operator\n"; + ss << "- Rule block " << (i + 1) << " <" << ruleblock->getName() << "> has " + << requiresConjunction << " rules that require conjunction operator\n"; + } + const SNorm* disjunction = ruleblock->getDisjunction(); + if (requiresDisjunction > 0 and not disjunction) { + ss << "- Rule block " << (i + 1) << " <" << ruleblock->getName() << "> has no disjunction operator\n"; + ss << "- Rule block " << (i + 1) << " <" << ruleblock->getName() << "> has " + << requiresDisjunction << " rules that require disjunction operator\n"; + } + const TNorm* implication = ruleblock->getImplication(); + if (requiresImplication > 0 and not implication) { + ss << "- Rule block " << (i + 1) << " <" << ruleblock->getName() << "> has no implication operator\n"; + ss << "- Rule block " << (i + 1) << " <" << ruleblock->getName() << "> has " + << requiresImplication << " rules that require implication operator\n"; + } + } + } + if (status) *status = ss.str(); + return ss.str().empty(); + } + + void Engine::restart() { + for (std::size_t i = 0; i < inputVariables().size(); ++i) { + inputVariables().at(i)->setValue(fl::nan); + } + for (std::size_t i = 0; i < outputVariables().size(); ++i) { + outputVariables().at(i)->clear(); + } + } + + Complexity Engine::complexity() const { + Complexity result; + for (std::size_t i = 0; i < _ruleBlocks.size(); ++i) { + const RuleBlock* ruleBlock = _ruleBlocks.at(i); + if (ruleBlock->isEnabled()) { + result += ruleBlock->complexity(); + } + } + return result; + } + + void Engine::process() { + for (std::size_t i = 0; i < _outputVariables.size(); ++i) { + _outputVariables.at(i)->fuzzyOutput()->clear(); + } + + FL_DEBUG_BEGIN; + FL_DBG("==============="); + FL_DBG("CURRENT INPUTS:"); + for (std::size_t i = 0; i < _inputVariables.size(); ++i) { + InputVariable* inputVariable = _inputVariables.at(i); + scalar inputValue = inputVariable->getValue(); + if (inputVariable->isEnabled()) { + FL_DBG(inputVariable->getName() << ".input = " << Op::str(inputValue)); + FL_DBG(inputVariable->getName() << ".fuzzy = " << inputVariable->fuzzify(inputValue)); + } else { + FL_DBG(inputVariable->getName() << ".enabled = false"); + } + } + FL_DEBUG_END; + + + for (std::size_t i = 0; i < _ruleBlocks.size(); ++i) { + RuleBlock* ruleBlock = _ruleBlocks.at(i); + if (ruleBlock->isEnabled()) { + FL_DBG("==============="); + FL_DBG("RULE BLOCK: " << ruleBlock->getName()); + ruleBlock->activate(); + } + } + + for (std::size_t i = 0; i < _outputVariables.size(); ++i) { + _outputVariables.at(i)->defuzzify(); + } + + FL_DEBUG_BEGIN; + FL_DBG("==============="); + FL_DBG("CURRENT OUTPUTS:"); + for (std::size_t i = 0; i < _outputVariables.size(); ++i) { + OutputVariable* outputVariable = _outputVariables.at(i); + if (outputVariable->isEnabled()) { + FL_DBG(outputVariable->getName() << ".default = " + << outputVariable->getDefaultValue()); + + FL_DBG(outputVariable->getName() << ".lockValueInRange = " + << outputVariable->isLockValueInRange()); + + FL_DBG(outputVariable->getName() << ".lockPreviousValue= " + << outputVariable->isLockPreviousValue()); + + scalar output = outputVariable->getValue(); + FL_DBG(outputVariable->getName() << ".output = " << output); + FL_DBG(outputVariable->getName() << ".fuzzy = " << + outputVariable->fuzzify(output)); + FL_DBG(outputVariable->fuzzyOutput()->toString()); + } else { + FL_DBG(outputVariable->getName() << ".enabled = false"); + } + } + FL_DBG("=============="); + FL_DEBUG_END; + } + + void Engine::setName(const std::string& name) { + this->_name = name; + } + + std::string Engine::getName() const { + return this->_name; + } + + void Engine::setDescription(const std::string& description) { + this->_description = description; + } + + std::string Engine::getDescription() const { + return this->_description; + } + + std::string Engine::toString() const { + return FllExporter().toString(this); + } + + Engine::Type Engine::type(std::string* name, std::string* reason) const { + if (outputVariables().empty()) { + if (name) *name = "Unknown"; + if (reason) *reason = "- Engine has no output variables"; + return Engine::Unknown; + } + + //Mamdani + bool mamdani = true; + for (std::size_t i = 0; mamdani and i < outputVariables().size(); ++i) { + OutputVariable* outputVariable = outputVariables().at(i); + //Defuzzifier must be integral + mamdani = mamdani and dynamic_cast<IntegralDefuzzifier*> (outputVariable->getDefuzzifier()); + } + //Larsen + bool larsen = mamdani and not ruleBlocks().empty(); + //Larsen is Mamdani with AlgebraicProduct as Implication + if (mamdani) { + for (std::size_t i = 0; larsen and i < ruleBlocks().size(); ++i) { + RuleBlock* ruleBlock = ruleBlocks().at(i); + larsen = larsen and dynamic_cast<const AlgebraicProduct*> (ruleBlock->getImplication()); + } + } + if (larsen) { + if (name) *name = "Larsen"; + if (reason) *reason = "- Output variables have integral defuzzifiers\n" + "- Implication in rule blocks is the algebraic product T-Norm"; + return Engine::Larsen; + } + if (mamdani) { + if (name) *name = "Mamdani"; + if (reason) *reason = "-Output variables have integral defuzzifiers"; + return Engine::Mamdani; + } + //Else, keep checking + + //TakagiSugeno + bool takagiSugeno = true; + for (std::size_t i = 0; takagiSugeno and i < outputVariables().size(); ++i) { + OutputVariable* outputVariable = outputVariables().at(i); + //Defuzzifier is Weighted + WeightedDefuzzifier* weightedDefuzzifier = + dynamic_cast<WeightedDefuzzifier*> (outputVariable->getDefuzzifier()); + + takagiSugeno = takagiSugeno and weightedDefuzzifier and + (weightedDefuzzifier->getType() == WeightedDefuzzifier::Automatic or + weightedDefuzzifier->getType() == WeightedDefuzzifier::TakagiSugeno); + + if (takagiSugeno) { + //Takagi-Sugeno has only Constant, Linear or Function terms + for (std::size_t t = 0; takagiSugeno and t < outputVariable->numberOfTerms(); ++t) { + Term* term = outputVariable->getTerm(t); + takagiSugeno = takagiSugeno and + weightedDefuzzifier->inferType(term) == WeightedDefuzzifier::TakagiSugeno; + } + } + } + if (takagiSugeno) { + if (name) *name = "Takagi-Sugeno"; + if (reason) *reason = "- Output variables have weighted defuzzifiers\n" + "- Output variables have constant, linear or function terms"; + return Engine::TakagiSugeno; + } + + //Tsukamoto + bool tsukamoto = true; + for (std::size_t i = 0; tsukamoto and i < outputVariables().size(); ++i) { + OutputVariable* outputVariable = outputVariables().at(i); + //Defuzzifier is Weighted + WeightedDefuzzifier* weightedDefuzzifier = + dynamic_cast<WeightedDefuzzifier*> (outputVariable->getDefuzzifier()); + + tsukamoto = tsukamoto and weightedDefuzzifier and + (weightedDefuzzifier->getType() == WeightedDefuzzifier::Automatic or + weightedDefuzzifier->getType() == WeightedDefuzzifier::Tsukamoto); + if (tsukamoto) { + //Tsukamoto has only monotonic terms: Concave, Ramp, Sigmoid, SShape, or ZShape + for (std::size_t t = 0; tsukamoto and t < outputVariable->numberOfTerms(); ++t) { + Term* term = outputVariable->getTerm(t); + tsukamoto = tsukamoto and term->isMonotonic(); + } + } + } + if (tsukamoto) { + if (name) *name = "Tsukamoto"; + if (reason) *reason = "- Output variables have weighted defuzzifiers\n" + "- Output variables only have monotonic terms"; + return Engine::Tsukamoto; + } + + //Inverse Tsukamoto + bool inverseTsukamoto = true; + for (std::size_t i = 0; inverseTsukamoto and i < outputVariables().size(); ++i) { + OutputVariable* outputVariable = outputVariables().at(i); + //Defuzzifier cannot be integral + WeightedDefuzzifier* weightedDefuzzifier = + dynamic_cast<WeightedDefuzzifier*> (outputVariable->getDefuzzifier()); + inverseTsukamoto = inverseTsukamoto and weightedDefuzzifier; + } + if (inverseTsukamoto) { + if (name) *name = "Inverse Tsukamoto"; + if (reason) *reason = "- Output variables have weighted defuzzifiers\n" + "- Output variables do not only have constant, linear or function terms\n" + "- Output variables do not only have monotonic terms"; + return Engine::InverseTsukamoto; + } + + bool hybrid = true; + for (std::size_t i = 0; i < outputVariables().size(); ++i) { + OutputVariable* outputVariable = outputVariables().at(i); + //Output variables have non-fl::null defuzzifiers + hybrid = hybrid and outputVariable->getDefuzzifier(); + } + if (hybrid) { + if (name) *name = "Hybrid"; + if (reason) *reason = "- Output variables have different types of defuzzifiers"; + return Engine::Hybrid; + } + + if (name) *name = "Unknown"; + if (reason) *reason = "- One or more output variables do not have a defuzzifier"; + return Engine::Unknown; + } + + Engine* Engine::clone() const { + return new Engine(*this); + } + + std::vector<Variable*> Engine::variables() const { + std::vector<Variable*> result; + result.reserve(inputVariables().size() + outputVariables().size()); + result.insert(result.end(), inputVariables().begin(), inputVariables().end()); + result.insert(result.end(), outputVariables().begin(), outputVariables().end()); + return result; + } + + /** + * Operations for InputVariables + */ + void Engine::setInputValue(const std::string& name, scalar value) { + InputVariable* inputVariable = getInputVariable(name); + inputVariable->setValue(value); + } + + void Engine::addInputVariable(InputVariable* inputVariable) { + inputVariables().push_back(inputVariable); + } + + InputVariable* Engine::setInputVariable(InputVariable* inputVariable, std::size_t index) { + InputVariable* result = inputVariables().at(index); + inputVariables().at(index) = inputVariable; + return result; + } + + void Engine::insertInputVariable(InputVariable* inputVariable, std::size_t index) { + inputVariables().insert(inputVariables().begin() + index, inputVariable); + } + + InputVariable* Engine::getInputVariable(std::size_t index) const { + return inputVariables().at(index); + } + + InputVariable* Engine::getInputVariable(const std::string& name) const { + for (std::size_t i = 0; i < inputVariables().size(); ++i) { + if (inputVariables().at(i)->getName() == name) + return inputVariables().at(i); + } + throw Exception("[engine error] input variable <" + name + "> not found", FL_AT); + } + + bool Engine::hasInputVariable(const std::string& name) const { + for (std::size_t i = 0; i < inputVariables().size(); ++i) { + if (inputVariables().at(i)->getName() == name) + return true; + } + return false; + } + + InputVariable* Engine::removeInputVariable(std::size_t index) { + InputVariable* result = inputVariables().at(index); + inputVariables().erase(inputVariables().begin() + index); + return result; + } + + InputVariable* Engine::removeInputVariable(const std::string& name) { + for (std::size_t i = 0; i < inputVariables().size(); ++i) { + if (inputVariables().at(i)->getName() == name) { + InputVariable* result = inputVariables().at(i); + inputVariables().erase(inputVariables().begin() + i); + return result; + } + } + throw Exception("[engine error] input variable <" + name + "> not found", FL_AT); + } + + std::size_t Engine::numberOfInputVariables() const { + return inputVariables().size(); + } + + const std::vector<InputVariable*>& Engine::inputVariables() const { + return this->_inputVariables; + } + + void Engine::setInputVariables(const std::vector<InputVariable*>& inputVariables) { + this->_inputVariables = inputVariables; + } + + std::vector<InputVariable*>& Engine::inputVariables() { + return this->_inputVariables; + } + + /** + * Operations for OutputVariables + */ + scalar Engine::getOutputValue(const std::string& name) { + OutputVariable* outputVariable = getOutputVariable(name); + return outputVariable->getValue(); + } + + void Engine::addOutputVariable(OutputVariable* outputVariable) { + outputVariables().push_back(outputVariable); + } + + OutputVariable* Engine::setOutputVariable(OutputVariable* outputVariable, std::size_t index) { + OutputVariable* result = outputVariables().at(index); + outputVariables().at(index) = outputVariable; + return result; + } + + void Engine::insertOutputVariable(OutputVariable* outputVariable, std::size_t index) { + outputVariables().insert(outputVariables().begin() + index, outputVariable); + } + + OutputVariable* Engine::getOutputVariable(std::size_t index) const { + return outputVariables().at(index); + } + + OutputVariable* Engine::getOutputVariable(const std::string& name) const { + for (std::size_t i = 0; i < outputVariables().size(); ++i) { + if (outputVariables().at(i)->getName() == name) + return outputVariables().at(i); + } + throw Exception("[engine error] output variable <" + name + "> not found", FL_AT); + } + + bool Engine::hasOutputVariable(const std::string& name) const { + for (std::size_t i = 0; i < outputVariables().size(); ++i) { + if (outputVariables().at(i)->getName() == name) + return true; + } + return false; + } + + OutputVariable* Engine::removeOutputVariable(std::size_t index) { + OutputVariable* result = outputVariables().at(index); + outputVariables().erase(outputVariables().begin() + index); + return result; + } + + OutputVariable* Engine::removeOutputVariable(const std::string& name) { + for (std::size_t i = 0; i < outputVariables().size(); ++i) { + if (outputVariables().at(i)->getName() == name) { + OutputVariable* result = outputVariables().at(i); + outputVariables().erase(outputVariables().begin() + i); + return result; + } + } + throw Exception("[engine error] output variable <" + name + "> not found", FL_AT); + } + + std::size_t Engine::numberOfOutputVariables() const { + return outputVariables().size(); + } + + const std::vector<OutputVariable*>& Engine::outputVariables() const { + return this->_outputVariables; + } + + void Engine::setOutputVariables(const std::vector<OutputVariable*>& outputVariables) { + this->_outputVariables = outputVariables; + } + + std::vector<OutputVariable*>& Engine::outputVariables() { + return this->_outputVariables; + } + + /** + * Operations for iterable datatype _ruleblocks + */ + void Engine::addRuleBlock(RuleBlock* ruleblock) { + ruleBlocks().push_back(ruleblock); + } + + RuleBlock* Engine::setRuleBlock(RuleBlock* ruleBlock, std::size_t index) { + RuleBlock* result = ruleBlocks().at(index); + ruleBlocks().at(index) = ruleBlock; + return result; + } + + void Engine::insertRuleBlock(RuleBlock* ruleblock, std::size_t index) { + ruleBlocks().insert(ruleBlocks().begin() + index, ruleblock); + } + + RuleBlock* Engine::getRuleBlock(std::size_t index) const { + return ruleBlocks().at(index); + } + + RuleBlock* Engine::getRuleBlock(const std::string& name) const { + for (std::size_t i = 0; i < ruleBlocks().size(); ++i) { + if (ruleBlocks().at(i)->getName() == name) + return ruleBlocks().at(i); + } + throw Exception("[engine error] rule block <" + name + "> not found", FL_AT); + } + + bool Engine::hasRuleBlock(const std::string& name) const { + for (std::size_t i = 0; i < ruleBlocks().size(); ++i) { + if (ruleBlocks().at(i)->getName() == name) + return true; + } + return false; + } + + RuleBlock* Engine::removeRuleBlock(std::size_t index) { + RuleBlock* result = ruleBlocks().at(index); + ruleBlocks().erase(ruleBlocks().begin() + index); + return result; + } + + RuleBlock* Engine::removeRuleBlock(const std::string& name) { + for (std::size_t i = 0; i < ruleBlocks().size(); ++i) { + if (ruleBlocks().at(i)->getName() == name) { + RuleBlock* result = ruleBlocks().at(i); + ruleBlocks().erase(ruleBlocks().begin() + i); + return result; + } + } + throw Exception("[engine error] rule block <" + name + "> not found", FL_AT); + } + + std::size_t Engine::numberOfRuleBlocks() const { + return ruleBlocks().size(); + } + + const std::vector<RuleBlock*>& Engine::ruleBlocks() const { + return this->_ruleBlocks; + } + + void Engine::setRuleBlocks(const std::vector<RuleBlock*>& ruleBlocks) { + this->_ruleBlocks = ruleBlocks; + } + + std::vector<RuleBlock*>& Engine::ruleBlocks() { + return this->_ruleBlocks; + } + +} diff --git a/fuzzylite/src/Exception.cpp b/fuzzylite/src/Exception.cpp new file mode 100644 index 0000000..1c27e70 --- /dev/null +++ b/fuzzylite/src/Exception.cpp @@ -0,0 +1,184 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/Exception.h" + + +#ifdef FL_BACKTRACE + +#ifdef FL_UNIX +#include <execinfo.h> + +#elif defined FL_WINDOWS +#include <windows.h> +#include <winbase.h> + +#ifndef __MINGW32__ +/*Disable warning 8.1\Include\um\dbghelp.h(1544): +warning C4091: 'typedef ': ignored on left of '' when no variable is declared*/ +#pragma warning (push) +#pragma warning (disable:4091) +#include <dbghelp.h> +#pragma warning (pop) +#endif + +#endif + +#endif + + +#include <csignal> +#include <cstring> + +namespace fl { + + Exception::Exception(const std::string& what) + : std::exception(), _what(what) { + FL_DBG(this->what()); + } + + Exception::Exception(const std::string& what, const std::string& file, int line, + const std::string& function) + : std::exception(), _what(what) { + append(file, line, function); + FL_DBG(this->what()); + } + + Exception::~Exception() FL_INOEXCEPT { } + + void Exception::setWhat(const std::string& what) { + this->_what = what; + } + + std::string Exception::getWhat() const { + return this->_what; + } + + const char* Exception::what() const FL_INOEXCEPT { + return this->_what.c_str(); + } + + void Exception::append(const std::string& whatElse) { + this->_what += whatElse + "\n"; + } + + void Exception::append(const std::string& file, int line, const std::string& function) { + std::ostringstream ss; + ss << "\n{at " << file << "::" << function << "() [line:" << line << "]}"; + _what += ss.str(); + } + + void Exception::append(const std::string& whatElse, + const std::string& file, int line, const std::string& function) { + append(whatElse); + append(file, line, function); + } + + std::string Exception::btCallStack() { +#ifndef FL_BACKTRACE + return "[backtrace disabled] fuzzylite was built without -DFL_BACKTRACE"; +#elif defined FL_UNIX + std::ostringstream btStream; + const int bufferSize = 30; + void* buffer[bufferSize]; + int backtraceSize = ::backtrace(buffer, bufferSize); + char **btSymbols = ::backtrace_symbols(buffer, backtraceSize); + if (btSymbols == fl::null) { + btStream << "[backtrace error] no symbols could be retrieved"; + } else { + if (backtraceSize == 0) { + btStream << "[backtrace is empty]"; + } + for (int i = 0; i < backtraceSize; ++i) { + btStream << btSymbols[i] << "\n"; + } + } + ::free(btSymbols); + return btStream.str(); + + +#elif defined FL_WINDOWS && ! defined __MINGW32__ + std::ostringstream btStream; + const int bufferSize = 30; + void* buffer[bufferSize]; + SymInitialize(GetCurrentProcess(), fl::null, TRUE); + + int backtraceSize = CaptureStackBackTrace(0, bufferSize, buffer, fl::null); + SYMBOL_INFO* btSymbol = (SYMBOL_INFO *) calloc(sizeof ( SYMBOL_INFO) + 256 * sizeof ( char), 1); + if (not btSymbol) { + btStream << "[backtrace error] no symbols could be retrieved"; + } else { + btSymbol->MaxNameLen = 255; + btSymbol->SizeOfStruct = sizeof ( SYMBOL_INFO); + if (backtraceSize == 0) { + btStream << "[backtrace is empty]"; + } + for (int i = 0; i < backtraceSize; ++i) { + SymFromAddr(GetCurrentProcess(), (DWORD64) (buffer[ i ]), 0, btSymbol); + btStream << (backtraceSize - i - 1) << ": " << + btSymbol->Name << " at 0x" << btSymbol->Address << "\n"; + } + } + ::free(btSymbol); + return btStream.str(); +#else + return "[backtrace missing] supported only in Unix and Windows platforms"; +#endif + } + + void Exception::signalHandler(int unixSignal) { + std::ostringstream ex; + ex << "[unexpected signal " << unixSignal << "] "; +#ifdef FL_UNIX + ex << ::strsignal(unixSignal); +#endif + ex << "\nBACKTRACE:\n" << btCallStack(); + Exception::catchException(Exception(ex.str(), FL_AT)); + ::exit(EXIT_FAILURE); + } + + void Exception::convertToException(int unixSignal) { + std::string signalDescription; +#ifdef FL_UNIX + //Unblock the signal + sigset_t empty; + sigemptyset(&empty); + sigaddset(&empty, unixSignal); + sigprocmask(SIG_UNBLOCK, &empty, fl::null); + signalDescription = ::strsignal(unixSignal); +#endif + std::ostringstream ex; + ex << "[signal " << unixSignal << "] " << signalDescription << "\n"; + ex << "BACKTRACE:\n" << btCallStack(); + throw Exception(ex.str(), FL_AT); + } + + void Exception::terminate() { + Exception::catchException(Exception("[unexpected exception] BACKTRACE:\n" + btCallStack(), FL_AT)); + ::exit(EXIT_FAILURE); + } + + void Exception::catchException(const std::exception& exception) { + std::ostringstream ss; + ss << exception.what(); + std::string backtrace = btCallStack(); + if (not backtrace.empty()) { + ss << "\n\nBACKTRACE:\n" << backtrace; + } + FL_LOG(ss.str()); + } + +} diff --git a/fuzzylite/src/activation/First.cpp b/fuzzylite/src/activation/First.cpp new file mode 100644 index 0000000..fcf7484 --- /dev/null +++ b/fuzzylite/src/activation/First.cpp @@ -0,0 +1,122 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/activation/First.h" + +#include "fl/rule/RuleBlock.h" +#include "fl/rule/Rule.h" +#include "fl/Operation.h" + +namespace fl { + + First::First(int numberOfRules, scalar threshold) : Activation(), + _numberOfRules(numberOfRules), _threshold(threshold) { } + + First::~First() { } + + std::string First::className() const { + return "First"; + } + + std::string First::parameters() const { + return Op::str(getNumberOfRules()) + " " + Op::str(getThreshold()); + } + + void First::configure(const std::string& parameters) { + if (parameters.empty()) return; + std::vector<std::string> values = Op::split(parameters, " "); + std::size_t required = 2; + if (values.size() < required) { + std::ostringstream ex; + ex << "[configuration error] activation <" << className() << ">" + << " requires <" << required << "> parameters"; + throw Exception(ex.str(), FL_AT); + } + setNumberOfRules((int) Op::toScalar(values.at(0))); + setThreshold(Op::toScalar(values.at(1))); + } + + Complexity First::complexity(const RuleBlock* ruleBlock) const { + Complexity result; + + const TNorm* conjunction = ruleBlock->getConjunction(); + const SNorm* disjunction = ruleBlock->getDisjunction(); + const TNorm* implication = ruleBlock->getImplication(); + + Complexity meanFiring; + for (std::size_t i = 0; i < ruleBlock->numberOfRules(); ++i) { + result.comparison(1 + 3); + const Rule* rule = ruleBlock->getRule(i); + result += rule->complexityOfActivation(conjunction, disjunction); + meanFiring += rule->complexityOfFiring(implication); + } + meanFiring.divide(scalar(ruleBlock->numberOfRules())); + + result += meanFiring.multiply(getNumberOfRules()); + result += Complexity().arithmetic(1).multiply(getNumberOfRules()); + return result; + } + + void First::activate(RuleBlock* ruleBlock) { + FL_DBG("Activation: " << className() << " " << parameters()); + const TNorm* conjunction = ruleBlock->getConjunction(); + const SNorm* disjunction = ruleBlock->getDisjunction(); + const TNorm* implication = ruleBlock->getImplication(); + + int activated = 0; + for (std::vector<Rule*>::const_iterator it = ruleBlock->rules().begin(); + it != ruleBlock->rules().end(); ++it) { + Rule* rule = (*it); + rule->deactivate(); + + if (rule->isLoaded()) { + scalar activationDegree = rule->activateWith(conjunction, disjunction); + if (activated < _numberOfRules + and Op::isGt(activationDegree, 0.0) + and Op::isGE(activationDegree, _threshold)) { + rule->trigger(implication); + ++activated; + } + } + } + } + + void First::setNumberOfRules(int numberOfRules) { + this->_numberOfRules = numberOfRules; + } + + int First::getNumberOfRules() const { + return this->_numberOfRules; + } + + void First::setThreshold(scalar threshold) { + this->_threshold = threshold; + } + + scalar First::getThreshold() const { + return this->_threshold; + } + + First* First::clone() const { + return new First(*this); + } + + Activation* First::constructor() { + return new First; + } + +} + diff --git a/fuzzylite/src/activation/General.cpp b/fuzzylite/src/activation/General.cpp new file mode 100644 index 0000000..a42c588 --- /dev/null +++ b/fuzzylite/src/activation/General.cpp @@ -0,0 +1,77 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/activation/General.h" + +#include "fl/rule/RuleBlock.h" +#include "fl/rule/Rule.h" +#include "fl/Operation.h" + +namespace fl { + + General::General() : Activation() { } + + General::~General() { } + + std::string General::className() const { + return "General"; + } + + std::string General::parameters() const { + return ""; + } + + void General::configure(const std::string& parameters) { + FL_IUNUSED(parameters); + } + + Complexity General::complexity(const RuleBlock* ruleBlock) const { + Complexity result; + for (std::size_t i = 0; i < ruleBlock->numberOfRules(); ++i) { + result.comparison(1); + result += ruleBlock->getRule(i)->complexity( + ruleBlock->getConjunction(), ruleBlock->getDisjunction(), + ruleBlock->getImplication()); + } + return result; + } + + void General::activate(RuleBlock* ruleBlock) { + FL_DBG("Activation: " << className() << " " << parameters()); + const TNorm* conjunction = ruleBlock->getConjunction(); + const SNorm* disjunction = ruleBlock->getDisjunction(); + const TNorm* implication = ruleBlock->getImplication(); + + const std::size_t numberOfRules = ruleBlock->numberOfRules(); + for (std::size_t i = 0; i < numberOfRules; ++i) { + Rule* rule = ruleBlock->getRule(i); + rule->deactivate(); + if (rule->isLoaded()) { + rule->activateWith(conjunction, disjunction); + rule->trigger(implication); + } + } + } + + General* General::clone() const { + return new General(*this); + } + + Activation* General::constructor() { + return new General; + } + +} diff --git a/fuzzylite/src/activation/Highest.cpp b/fuzzylite/src/activation/Highest.cpp new file mode 100644 index 0000000..612c16b --- /dev/null +++ b/fuzzylite/src/activation/Highest.cpp @@ -0,0 +1,120 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/activation/Highest.h" + +#include "fl/rule/RuleBlock.h" +#include "fl/rule/Rule.h" +#include "fl/Operation.h" + +#include <queue> + +namespace fl { + + Highest::Highest(int numberOfRules) : Activation(), _numberOfRules(numberOfRules) { } + + Highest::~Highest() { } + + std::string Highest::className() const { + return "Highest"; + } + + std::string Highest::parameters() const { + return Op::str(getNumberOfRules()); + } + + void Highest::configure(const std::string& parameters) { + setNumberOfRules((int) Op::toScalar(parameters)); + } + + int Highest::getNumberOfRules() const { + return this->_numberOfRules; + } + + void Highest::setNumberOfRules(int numberOfRules) { + this->_numberOfRules = numberOfRules; + } + + Complexity Highest::complexity(const RuleBlock* ruleBlock) const { + //Cost of priority_queue: + //http://stackoverflow.com/questions/2974470/efficiency-of-the-stl-priority-queue + Complexity result; + + const TNorm* conjunction = ruleBlock->getConjunction(); + const SNorm* disjunction = ruleBlock->getDisjunction(); + const TNorm* implication = ruleBlock->getImplication(); + + Complexity meanFiring; + for (std::size_t i = 0; i < ruleBlock->numberOfRules(); ++i) { + const Rule* rule = ruleBlock->getRule(i); + result += rule->complexityOfActivation(conjunction, disjunction); + meanFiring += rule->complexityOfFiring(implication); + } + meanFiring.divide(scalar(ruleBlock->numberOfRules())); + + //Complexity of push is O(log n) + result += Complexity().function(1).multiply(ruleBlock->numberOfRules() + * std::log(scalar(ruleBlock->numberOfRules()))); + + result += Complexity().comparison(2).arithmetic(1).multiply(getNumberOfRules()); + result += meanFiring.multiply(getNumberOfRules()); + //Complexity of pop is 2 * O(log n) + result += Complexity().function(1).multiply(getNumberOfRules() * + 2 * std::log(scalar(ruleBlock->numberOfRules()))); + return result; + } + + struct Descending { + + bool operator()(const Rule* a, const Rule* b) const { + return a->getActivationDegree() < b->getActivationDegree(); + } + }; + + void Highest::activate(RuleBlock* ruleBlock) { + const TNorm* conjunction = ruleBlock->getConjunction(); + const SNorm* disjunction = ruleBlock->getDisjunction(); + const TNorm* implication = ruleBlock->getImplication(); + + std::priority_queue<Rule*, std::vector<Rule*>, Descending> rulesToActivate; + + for (std::size_t i = 0; i < ruleBlock->numberOfRules(); ++i) { + Rule* rule = ruleBlock->getRule(i); + rule->deactivate(); + if (rule->isLoaded()) { + scalar activationDegree = rule->activateWith(conjunction, disjunction); + if (Op::isGt(activationDegree, 0.0)) + rulesToActivate.push(rule); + } + } + + int activated = 0; + while (rulesToActivate.size() > 0 and activated++ < _numberOfRules) { + Rule* rule = rulesToActivate.top(); + rule->trigger(implication); + rulesToActivate.pop(); + } + } + + Highest* Highest::clone() const { + return new Highest(*this); + } + + Activation* Highest::constructor() { + return new Highest; + } + +} diff --git a/fuzzylite/src/activation/Last.cpp b/fuzzylite/src/activation/Last.cpp new file mode 100644 index 0000000..90b5dc9 --- /dev/null +++ b/fuzzylite/src/activation/Last.cpp @@ -0,0 +1,121 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/activation/Last.h" + +#include "fl/rule/RuleBlock.h" +#include "fl/rule/Rule.h" +#include "fl/Operation.h" + +namespace fl { + + Last::Last(int numberOfRules, scalar threshold) : Activation(), + _numberOfRules(numberOfRules), _threshold(threshold) { } + + Last::~Last() { } + + std::string Last::className() const { + return "Last"; + } + + std::string Last::parameters() const { + return Op::str(getNumberOfRules()) + " " + Op::str(getThreshold()); + } + + void Last::configure(const std::string& parameters) { + if (parameters.empty()) return; + std::vector<std::string> values = Op::split(parameters, " "); + std::size_t required = 2; + if (values.size() < required) { + std::ostringstream ex; + ex << "[configuration error] activation <" << className() << ">" + << " requires <" << required << "> parameters"; + throw Exception(ex.str(), FL_AT); + } + setNumberOfRules((int) Op::toScalar(values.at(0))); + setThreshold(Op::toScalar(values.at(1))); + } + + void Last::setNumberOfRules(int numberOfRules) { + this->_numberOfRules = numberOfRules; + } + + int Last::getNumberOfRules() const { + return this->_numberOfRules; + } + + void Last::setThreshold(scalar threshold) { + this->_threshold = threshold; + } + + scalar Last::getThreshold() const { + return this->_threshold; + } + + Complexity Last::complexity(const RuleBlock* ruleBlock) const { + Complexity result; + + const TNorm* conjunction = ruleBlock->getConjunction(); + const SNorm* disjunction = ruleBlock->getDisjunction(); + const TNorm* implication = ruleBlock->getImplication(); + + Complexity meanFiring; + for (std::size_t i = 0; i < ruleBlock->numberOfRules(); ++i) { + result.comparison(1 + 3); + const Rule* rule = ruleBlock->getRule(i); + result += rule->complexityOfActivation(conjunction, disjunction); + meanFiring += rule->complexityOfFiring(implication); + } + meanFiring.divide(scalar(ruleBlock->numberOfRules())); + + result += meanFiring.multiply(getNumberOfRules()); + result += Complexity().arithmetic(1).multiply(getNumberOfRules()); + return result; + } + + void Last::activate(RuleBlock* ruleBlock) { + FL_DBG("Activation: " << className() << " " << parameters()); + const TNorm* conjunction = ruleBlock->getConjunction(); + const SNorm* disjunction = ruleBlock->getDisjunction(); + const TNorm* implication = ruleBlock->getImplication(); + + int activated = 0; + for (std::vector<Rule*>::const_reverse_iterator it = ruleBlock->rules().rbegin(); + it != ruleBlock->rules().rend(); ++it) { + Rule* rule = (*it); + rule->deactivate(); + + if (rule->isLoaded()) { + scalar activationDegree = rule->activateWith(conjunction, disjunction); + if (activated < _numberOfRules + and Op::isGt(activationDegree, 0.0) + and Op::isGE(activationDegree, _threshold)) { + rule->trigger(implication); + ++activated; + } + } + } + } + + Last* Last::clone() const { + return new Last(*this); + } + + Activation* Last::constructor() { + return new Last; + } + +} diff --git a/fuzzylite/src/activation/Lowest.cpp b/fuzzylite/src/activation/Lowest.cpp new file mode 100644 index 0000000..52e7e85 --- /dev/null +++ b/fuzzylite/src/activation/Lowest.cpp @@ -0,0 +1,122 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/activation/Lowest.h" + +#include "fl/rule/RuleBlock.h" +#include "fl/rule/Rule.h" +#include "fl/Operation.h" + +#include <queue> + +namespace fl { + + Lowest::Lowest(int numberOfRules) : Activation(), _numberOfRules(numberOfRules) { } + + Lowest::~Lowest() { } + + std::string Lowest::className() const { + return "Lowest"; + } + + std::string Lowest::parameters() const { + return Op::str(getNumberOfRules()); + } + + void Lowest::configure(const std::string& parameters) { + setNumberOfRules((int) Op::toScalar(parameters)); + } + + int Lowest::getNumberOfRules() const { + return this->_numberOfRules; + } + + void Lowest::setNumberOfRules(int activatedRules) { + this->_numberOfRules = activatedRules; + } + + Complexity Lowest::complexity(const RuleBlock* ruleBlock) const { + //Cost of priority_queue: + //http://stackoverflow.com/questions/2974470/efficiency-of-the-stl-priority-queue + Complexity result; + + const TNorm* conjunction = ruleBlock->getConjunction(); + const SNorm* disjunction = ruleBlock->getDisjunction(); + const TNorm* implication = ruleBlock->getImplication(); + + Complexity meanFiring; + for (std::size_t i = 0; i < ruleBlock->numberOfRules(); ++i) { + const Rule* rule = ruleBlock->getRule(i); + result.comparison(2); + result += rule->complexityOfActivation(conjunction, disjunction); + meanFiring += rule->complexityOfFiring(implication); + } + meanFiring.divide(scalar(ruleBlock->numberOfRules())); + + //Complexity of push is O(log n) + result += Complexity().function(1).multiply(ruleBlock->numberOfRules() + * std::log(scalar(ruleBlock->numberOfRules()))); + + result += Complexity().comparison(2).arithmetic(1).multiply(getNumberOfRules()); + result += meanFiring.multiply(getNumberOfRules()); + //Complexity of pop is 2 * O(log n) + result += Complexity().function(1).multiply(getNumberOfRules() * + 2 * std::log(scalar(ruleBlock->numberOfRules()))); + return result; + } + + struct Ascending { + + bool operator()(const Rule* a, const Rule* b) { + return a->getActivationDegree() > b->getActivationDegree(); + } + }; + + void Lowest::activate(RuleBlock* ruleBlock) { + FL_DBG("Activation: " << className() << " " << parameters()); + const TNorm* conjunction = ruleBlock->getConjunction(); + const SNorm* disjunction = ruleBlock->getDisjunction(); + const TNorm* implication = ruleBlock->getImplication(); + + std::priority_queue<Rule*, std::vector<Rule*>, Ascending> rulesToActivate; + + for (std::size_t i = 0; i < ruleBlock->numberOfRules(); ++i) { + Rule* rule = ruleBlock->getRule(i); + rule->deactivate(); + if (rule->isLoaded()) { + scalar activationDegree = rule->activateWith(conjunction, disjunction); + if (Op::isGt(activationDegree, 0.0)) + rulesToActivate.push(rule); + } + } + + int activated = 0; + while (rulesToActivate.size() > 0 and activated++ < _numberOfRules) { + Rule* rule = rulesToActivate.top(); + rule->trigger(implication); + rulesToActivate.pop(); + } + } + + Lowest* Lowest::clone() const { + return new Lowest(*this); + } + + Activation* Lowest::constructor() { + return new Lowest; + } + +} diff --git a/fuzzylite/src/activation/Proportional.cpp b/fuzzylite/src/activation/Proportional.cpp new file mode 100644 index 0000000..07fa2aa --- /dev/null +++ b/fuzzylite/src/activation/Proportional.cpp @@ -0,0 +1,91 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/activation/Proportional.h" + +#include "fl/rule/RuleBlock.h" +#include "fl/rule/Rule.h" +#include "fl/Operation.h" + +namespace fl { + + Proportional::Proportional() : Activation() { } + + Proportional::~Proportional() { } + + std::string Proportional::className() const { + return "Proportional"; + } + + std::string Proportional::parameters() const { + return ""; + } + + void Proportional::configure(const std::string& parameters) { + FL_IUNUSED(parameters); + } + + Complexity Proportional::complexity(const RuleBlock* ruleBlock) const { + Complexity result; + for (std::size_t i = 0; i < ruleBlock->numberOfRules(); ++i) { + result.comparison(1).arithmetic(1); + result += ruleBlock->getRule(i)->complexityOfActivation( + ruleBlock->getConjunction(), ruleBlock->getDisjunction()); + } + + for (std::size_t i = 0; i < ruleBlock->numberOfRules(); ++i) { + result.arithmetic(1); + result += ruleBlock->getRule(i)->complexityOfFiring(ruleBlock->getImplication()); + } + return result; + } + + void Proportional::activate(RuleBlock* ruleBlock) { + FL_DBG("Activation: " << className() << " " << parameters()); + const TNorm* conjunction = ruleBlock->getConjunction(); + const SNorm* disjunction = ruleBlock->getDisjunction(); + const TNorm* implication = ruleBlock->getImplication(); + + scalar sumActivationDegrees = 0.0; + std::vector<Rule*> rulesToActivate; + const std::size_t numberOfRules = ruleBlock->numberOfRules(); + for (std::size_t i = 0; i < numberOfRules; ++i) { + Rule* rule = ruleBlock->getRule(i); + rule->deactivate(); + if (rule->isLoaded()) { + scalar activationDegree = rule->activateWith(conjunction, disjunction); + rulesToActivate.push_back(rule); + sumActivationDegrees += activationDegree; + } + } + + for (std::size_t i = 0; i < rulesToActivate.size(); ++i) { + Rule* rule = rulesToActivate.at(i); + scalar activationDegree = rule->getActivationDegree() / sumActivationDegrees; + rule->setActivationDegree(activationDegree); + rule->trigger(implication); + } + } + + Proportional* Proportional::clone() const { + return new Proportional(*this); + } + + Activation* Proportional::constructor() { + return new Proportional; + } + +} diff --git a/fuzzylite/src/activation/Threshold.cpp b/fuzzylite/src/activation/Threshold.cpp new file mode 100644 index 0000000..cd1f034 --- /dev/null +++ b/fuzzylite/src/activation/Threshold.cpp @@ -0,0 +1,170 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/activation/Threshold.h" + +#include "fl/rule/RuleBlock.h" +#include "fl/rule/Rule.h" +#include "fl/Operation.h" + +namespace fl { + + Threshold::Threshold(Comparison comparison, scalar threshold) : Activation(), + _comparison(comparison), _value(threshold) { } + + Threshold::Threshold(const std::string& comparison, scalar threshold) : Activation(), + _comparison(parseComparison(comparison)), _value(threshold) { } + + Threshold::~Threshold() { } + + std::string Threshold::className() const { + return "Threshold"; + } + + std::string Threshold::parameters() const { + std::ostringstream ss; + ss << comparisonOperator() << " " << Op::str(getValue()); + return ss.str(); + } + + void Threshold::configure(const std::string& parameters) { + if (parameters.empty()) return; + std::vector<std::string> values = Op::split(parameters, " "); + std::size_t required = 2; + if (values.size() < required) { + std::ostringstream ex; + ex << "[configuration error] activation <" << className() << ">" + << " requires <" << required << "> parameters"; + throw Exception(ex.str(), FL_AT); + } + setComparison(parseComparison(values.at(0))); + setValue(Op::toScalar(values.at(1))); + } + + void Threshold::setComparison(Comparison comparison) { + this->_comparison = comparison; + } + + Threshold::Comparison Threshold::getComparison() const { + return this->_comparison; + } + + std::string Threshold::comparisonOperator() const { + return comparisonOperator(getComparison()); + } + + std::string Threshold::comparisonOperator(Comparison comparison) const { + switch (comparison) { + case LessThan: return "<"; + case LessThanOrEqualTo: return "<="; + case EqualTo: return "=="; + case NotEqualTo: return "!="; + case GreaterThanOrEqualTo: return ">="; + case GreaterThan: return ">"; + default: return "?"; + } + } + + std::vector<std::string> Threshold::availableComparisonOperators() const { + std::vector<std::string> result; + result.push_back("<"); + result.push_back("<="); + result.push_back("=="); + result.push_back("!="); + result.push_back(">="); + result.push_back(">"); + return result; + } + + Threshold::Comparison Threshold::parseComparison(const std::string& name) const { + if (name == "<") return LessThan; + if (name == "<=") return LessThanOrEqualTo; + if (name == "==") return EqualTo; + if (name == "!=") return NotEqualTo; + if (name == ">=") return GreaterThanOrEqualTo; + if (name == ">") return GreaterThan; + throw Exception("[syntax error] invalid threshold type by name <" + name + ">", FL_AT); + } + + void Threshold::setValue(scalar value) { + this->_value = value; + } + + scalar Threshold::getValue() const { + return this->_value; + } + + void Threshold::setThreshold(Comparison comparison, scalar threshold) { + setComparison(comparison); + setValue(threshold); + } + + void Threshold::setThreshold(const std::string& comparison, scalar value) { + setComparison(parseComparison(comparison)); + setValue(value); + } + + bool Threshold::activatesWith(scalar activationDegree) const { + switch (getComparison()) { + case LessThan: return Op::isLt(activationDegree, getValue()); + case LessThanOrEqualTo: return Op::isLE(activationDegree, getValue()); + case EqualTo: return Op::isEq(activationDegree, getValue()); + case NotEqualTo: return not Op::isEq(activationDegree, getValue()); + case GreaterThanOrEqualTo: return Op::isGE(activationDegree, getValue()); + case GreaterThan: return Op::isGt(activationDegree, getValue()); + default: return false; + } + } + + Complexity Threshold::complexity(const RuleBlock* ruleBlock) const { + Complexity result; + for (std::size_t i = 0; i < ruleBlock->rules().size(); ++i) { + result.comparison(2); + result += ruleBlock->rules().at(i)->complexity( + ruleBlock->getConjunction(), ruleBlock->getDisjunction(), + ruleBlock->getImplication()); + } + return result; + } + + void Threshold::activate(RuleBlock* ruleBlock) { + FL_DBG("Activation: " << className() << " " << parameters()); + const TNorm* conjunction = ruleBlock->getConjunction(); + const SNorm* disjunction = ruleBlock->getDisjunction(); + const TNorm* implication = ruleBlock->getImplication(); + + for (std::size_t i = 0; i < ruleBlock->numberOfRules(); ++i) { + Rule* rule = ruleBlock->getRule(i); + rule->deactivate(); + if (rule->isLoaded()) { + scalar activationDegree = rule->activateWith(conjunction, disjunction); + if (activatesWith(activationDegree)) { + rule->trigger(implication); + } + } + } + } + + Threshold* Threshold::clone() const { + return new Threshold(*this); + } + + Activation* Threshold::constructor() { + return new Threshold; + } + +} + diff --git a/fuzzylite/src/defuzzifier/Bisector.cpp b/fuzzylite/src/defuzzifier/Bisector.cpp new file mode 100644 index 0000000..ed61b6f --- /dev/null +++ b/fuzzylite/src/defuzzifier/Bisector.cpp @@ -0,0 +1,68 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/defuzzifier/Bisector.h" + +#include "fl/term/Term.h" + +namespace fl { + + Bisector::Bisector(int resolution) + : IntegralDefuzzifier(resolution) { } + + Bisector::~Bisector() { } + + std::string Bisector::className() const { + return "Bisector"; + } + + Complexity Bisector::complexity(const Term* term) const { + return Complexity().comparison(1).arithmetic(1 + 2 + 5) + + term->complexity().comparison(1).arithmetic(1 + 5).multiply(getResolution()); + } + + scalar Bisector::defuzzify(const Term* term, scalar minimum, scalar maximum) const { + if (not Op::isFinite(minimum + maximum)) return fl::nan; + + const scalar dx = (maximum - minimum) / getResolution(); + int counter = getResolution(); + int left = 0, right = 0; + scalar leftArea = 0, rightArea = 0; + scalar xLeft = minimum, xRight = maximum; + while (counter-- > 0) { + if (Op::isLE(leftArea, rightArea)) { + xLeft = minimum + (left + 0.5) * dx; + leftArea += term->membership(xLeft); + ++left; + } else { + xRight = maximum - (right + 0.5) * dx; + rightArea += term->membership(xRight); + ++right; + } + } + //Inverse weighted average to compensate + return (leftArea * xRight + rightArea * xLeft) / (leftArea + rightArea); + } + + Bisector* Bisector::clone() const { + return new Bisector(*this); + } + + Defuzzifier* Bisector::constructor() { + return new Bisector; + } + +} diff --git a/fuzzylite/src/defuzzifier/Centroid.cpp b/fuzzylite/src/defuzzifier/Centroid.cpp new file mode 100644 index 0000000..177da26 --- /dev/null +++ b/fuzzylite/src/defuzzifier/Centroid.cpp @@ -0,0 +1,68 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/defuzzifier/Centroid.h" + +#include "fl/term/Term.h" + +namespace fl { + + Centroid::Centroid(int resolution) + : IntegralDefuzzifier(resolution) { } + + Centroid::~Centroid() { } + + std::string Centroid::className() const { + return "Centroid"; + } + + Complexity Centroid::complexity(const Term* term) const { + return Complexity().comparison(1).arithmetic(1 + 2 + 1) + + term->complexity().arithmetic(6).multiply(getResolution()); + } + + scalar Centroid::defuzzify(const Term* term, scalar minimum, scalar maximum) const { + if (not Op::isFinite(minimum + maximum)) return fl::nan; + + const int resolution = getResolution(); + const scalar dx = (maximum - minimum) / resolution; + scalar x, y; + scalar area = 0, xcentroid = 0; + //scalar ycentroid = 0; + for (int i = 0; i < resolution; ++i) { + x = minimum + (i + 0.5) * dx; + y = term->membership(x); + + xcentroid += y * x; + //ycentroid += y * y; + area += y; + } + //Final results not computed for efficiency + //xcentroid /= area; + //ycentroid /= 2 * area; + //area *= dx; + return xcentroid / area; + } + + Centroid* Centroid::clone() const { + return new Centroid(*this); + } + + Defuzzifier* Centroid::constructor() { + return new Centroid; + } + +} diff --git a/fuzzylite/src/defuzzifier/IntegralDefuzzifier.cpp b/fuzzylite/src/defuzzifier/IntegralDefuzzifier.cpp new file mode 100644 index 0000000..3b06b42 --- /dev/null +++ b/fuzzylite/src/defuzzifier/IntegralDefuzzifier.cpp @@ -0,0 +1,44 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/defuzzifier/IntegralDefuzzifier.h" + +namespace fl { + + int IntegralDefuzzifier::_defaultResolution = 100; + + void IntegralDefuzzifier::setDefaultResolution(int defaultResolution) { + _defaultResolution = defaultResolution; + } + + int IntegralDefuzzifier::defaultResolution() { + return _defaultResolution; + } + + IntegralDefuzzifier::IntegralDefuzzifier(int resolution) + : Defuzzifier(), _resolution(resolution) { } + + IntegralDefuzzifier::~IntegralDefuzzifier() { } + + void IntegralDefuzzifier::setResolution(int resolution) { + this->_resolution = resolution; + } + + int IntegralDefuzzifier::getResolution() const { + return this->_resolution; + } + +} diff --git a/fuzzylite/src/defuzzifier/LargestOfMaximum.cpp b/fuzzylite/src/defuzzifier/LargestOfMaximum.cpp new file mode 100644 index 0000000..ff05707 --- /dev/null +++ b/fuzzylite/src/defuzzifier/LargestOfMaximum.cpp @@ -0,0 +1,65 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/defuzzifier/LargestOfMaximum.h" + +#include "fl/Exception.h" +#include "fl/term/Term.h" + +namespace fl { + + LargestOfMaximum::LargestOfMaximum(int resolution) + : IntegralDefuzzifier(resolution) { } + + LargestOfMaximum::~LargestOfMaximum() { } + + std::string LargestOfMaximum::className() const { + return "LargestOfMaximum"; + } + + Complexity LargestOfMaximum::complexity(const Term* term) const { + return Complexity().comparison(1).arithmetic(1 + 2) + + term->complexity().comparison(1).arithmetic(3).multiply(getResolution()); + } + + scalar LargestOfMaximum::defuzzify(const Term* term, scalar minimum, scalar maximum) const { + if (not Op::isFinite(minimum + maximum)) return fl::nan; + + const int resolution = getResolution(); + const scalar dx = (maximum - minimum) / resolution; + scalar x, y; + scalar ymax = -1.0, xlargest = maximum; + for (int i = 0; i < resolution; ++i) { + x = minimum + (i + 0.5) * dx; + y = term->membership(x); + + if (Op::isGE(y, ymax)) { + ymax = y; + xlargest = x; + } + } + return xlargest; + } + + LargestOfMaximum* LargestOfMaximum::clone() const { + return new LargestOfMaximum(*this); + } + + Defuzzifier* LargestOfMaximum::constructor() { + return new LargestOfMaximum; + } + +} diff --git a/fuzzylite/src/defuzzifier/MeanOfMaximum.cpp b/fuzzylite/src/defuzzifier/MeanOfMaximum.cpp new file mode 100644 index 0000000..961e505 --- /dev/null +++ b/fuzzylite/src/defuzzifier/MeanOfMaximum.cpp @@ -0,0 +1,77 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/defuzzifier/MeanOfMaximum.h" + +#include "fl/Exception.h" +#include "fl/term/Term.h" + +namespace fl { + + MeanOfMaximum::MeanOfMaximum(int resolution) + : IntegralDefuzzifier(resolution) { } + + MeanOfMaximum::~MeanOfMaximum() { } + + std::string MeanOfMaximum::className() const { + return "MeanOfMaximum"; + } + + Complexity MeanOfMaximum::complexity(const Term* term) const { + return Complexity().comparison(1).arithmetic(1 + 2 + 2) + + term->complexity().comparison(4).arithmetic(3).multiply(getResolution()); + } + + scalar MeanOfMaximum::defuzzify(const Term* term, scalar minimum, scalar maximum) const { + if (not Op::isFinite(minimum + maximum)) return fl::nan; + + const int resolution = getResolution(); + const scalar dx = (maximum - minimum) / resolution; + scalar x, y; + scalar ymax = -1.0; + scalar xsmallest = minimum; + scalar xlargest = maximum; + bool samePlateau = false; + for (int i = 0; i < resolution; ++i) { + x = minimum + (i + 0.5) * dx; + y = term->membership(x); + + if (Op::isGt(y, ymax)) { + ymax = y; + + xsmallest = x; + xlargest = x; + + samePlateau = true; + } else if (samePlateau and Op::isEq(y, ymax)) { + xlargest = x; + } else if (Op::isLt(y, ymax)) { + samePlateau = false; + } + } + + return 0.5 * (xlargest + xsmallest); + } + + MeanOfMaximum* MeanOfMaximum::clone() const { + return new MeanOfMaximum(*this); + } + + Defuzzifier* MeanOfMaximum::constructor() { + return new MeanOfMaximum; + } + +} diff --git a/fuzzylite/src/defuzzifier/SmallestOfMaximum.cpp b/fuzzylite/src/defuzzifier/SmallestOfMaximum.cpp new file mode 100644 index 0000000..7333702 --- /dev/null +++ b/fuzzylite/src/defuzzifier/SmallestOfMaximum.cpp @@ -0,0 +1,65 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/defuzzifier/SmallestOfMaximum.h" + +#include "fl/Exception.h" +#include "fl/term/Term.h" + +namespace fl { + + SmallestOfMaximum::SmallestOfMaximum(int resolution) + : IntegralDefuzzifier(resolution) { } + + SmallestOfMaximum::~SmallestOfMaximum() { } + + std::string SmallestOfMaximum::className() const { + return "SmallestOfMaximum"; + } + + Complexity SmallestOfMaximum::complexity(const Term* term) const { + return Complexity().comparison(1).arithmetic(1 + 2) + + term->complexity().comparison(1).arithmetic(3).multiply(getResolution()); + } + + scalar SmallestOfMaximum::defuzzify(const Term* term, scalar minimum, scalar maximum) const { + if (not Op::isFinite(minimum + maximum)) return fl::nan; + + const int resolution = getResolution(); + const scalar dx = (maximum - minimum) / resolution; + scalar x, y; + scalar ymax = -1.0, xsmallest = minimum; + for (int i = 0; i < resolution; ++i) { + x = minimum + (i + 0.5) * dx; + y = term->membership(x); + + if (Op::isGt(y, ymax)) { + xsmallest = x; + ymax = y; + } + } + return xsmallest; + } + + SmallestOfMaximum* SmallestOfMaximum::clone() const { + return new SmallestOfMaximum(*this); + } + + Defuzzifier* SmallestOfMaximum::constructor() { + return new SmallestOfMaximum; + } + +} diff --git a/fuzzylite/src/defuzzifier/WeightedAverage.cpp b/fuzzylite/src/defuzzifier/WeightedAverage.cpp new file mode 100644 index 0000000..34af8c9 --- /dev/null +++ b/fuzzylite/src/defuzzifier/WeightedAverage.cpp @@ -0,0 +1,100 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/defuzzifier/WeightedAverage.h" + +#include "fl/term/Aggregated.h" + +#include <map> + +namespace fl { + + WeightedAverage::WeightedAverage(Type type) : WeightedDefuzzifier(type) { } + + WeightedAverage::WeightedAverage(const std::string& type) : WeightedDefuzzifier(type) { } + + WeightedAverage::~WeightedAverage() { } + + std::string WeightedAverage::className() const { + return "WeightedAverage"; + } + + Complexity WeightedAverage::complexity(const Term* term) const { + Complexity result; + result.comparison(4).function(1); //for dynamic_cast + const Aggregated* fuzzyOutput = dynamic_cast<const Aggregated*> (term); + if (fuzzyOutput) { + result += term->complexity().arithmetic(3).multiply(scalar(fuzzyOutput->numberOfTerms())); + } + return result; + } + + scalar WeightedAverage::defuzzify(const Term* term, + scalar minimum, scalar maximum) const { + const Aggregated* fuzzyOutput = dynamic_cast<const Aggregated*> (term); + if (not fuzzyOutput) { + std::ostringstream ss; + ss << "[defuzzification error]" + << "expected an Aggregated term instead of" + << "<" << (term ? term->toString() : "null") << ">"; + throw Exception(ss.str(), FL_AT); + } + + if (fuzzyOutput->isEmpty()) return fl::nan; + + minimum = fuzzyOutput->getMinimum(); + maximum = fuzzyOutput->getMaximum(); + + Type type = getType(); + if (type == Automatic) { + type = inferType(&(fuzzyOutput->terms().front())); + } + + scalar sum = 0.0; + scalar weights = 0.0; + const std::size_t numberOfTerms = fuzzyOutput->numberOfTerms(); + if (type == TakagiSugeno) { + //Provides Takagi-Sugeno and Inverse Tsukamoto of Functions + scalar w, z; + for (std::size_t i = 0; i < numberOfTerms; ++i) { + const Activated& activated = fuzzyOutput->getTerm(i); + w = activated.getDegree(); + z = activated.getTerm()->membership(w); + sum += w * z; + weights += w; + } + } else { + scalar w, z; + for (std::size_t i = 0; i < numberOfTerms; ++i) { + const Activated& activated = fuzzyOutput->getTerm(i); + w = activated.getDegree(); + z = activated.getTerm()->tsukamoto(w, minimum, maximum); + sum += w * z; + weights += w; + } + } + return sum / weights; + } + + WeightedAverage* WeightedAverage::clone() const { + return new WeightedAverage(*this); + } + + Defuzzifier* WeightedAverage::constructor() { + return new WeightedAverage; + } + +} diff --git a/fuzzylite/src/defuzzifier/WeightedAverageCustom.cpp b/fuzzylite/src/defuzzifier/WeightedAverageCustom.cpp new file mode 100644 index 0000000..0beb722 --- /dev/null +++ b/fuzzylite/src/defuzzifier/WeightedAverageCustom.cpp @@ -0,0 +1,117 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/defuzzifier/WeightedAverageCustom.h" + +#include "fl/term/Aggregated.h" + +#include <map> + +namespace fl { + + WeightedAverageCustom::WeightedAverageCustom(Type type) : WeightedDefuzzifier(type) { } + + WeightedAverageCustom::WeightedAverageCustom(const std::string& type) : WeightedDefuzzifier(type) { } + + WeightedAverageCustom::~WeightedAverageCustom() { } + + std::string WeightedAverageCustom::className() const { + return "WeightedAverageCustom"; + } + + Complexity WeightedAverageCustom::complexity(const Term* term) const { + Complexity result; + result.comparison(3).arithmetic(1).function(1); + const Aggregated* fuzzyOutput = dynamic_cast<const Aggregated*> (term); + if (fuzzyOutput) { + result += term->complexity().arithmetic(3).comparison(2) + .multiply(scalar(fuzzyOutput->numberOfTerms())); + } + return result; + } + + scalar WeightedAverageCustom::defuzzify(const Term* term, + scalar minimum, scalar maximum) const { + const Aggregated* fuzzyOutput = dynamic_cast<const Aggregated*> (term); + if (not fuzzyOutput) { + std::ostringstream ss; + ss << "[defuzzification error]" + << "expected an Aggregated term instead of" + << "<" << (term ? term->toString() : "null") << ">"; + throw Exception(ss.str(), FL_AT); + } + + if (fuzzyOutput->isEmpty()) return fl::nan; + + minimum = fuzzyOutput->getMinimum(); + maximum = fuzzyOutput->getMaximum(); + + SNorm* aggregation = fuzzyOutput->getAggregation(); + + Type type = getType(); + if (type == Automatic) { + type = inferType(&(fuzzyOutput->terms().front())); + } + + scalar sum = 0.0; + scalar weights = 0.0; + const std::size_t numberOfTerms = fuzzyOutput->numberOfTerms(); + if (type == TakagiSugeno) { + //Provides Takagi-Sugeno and Inverse Tsukamoto of Functions + scalar w, z, wz; + for (std::size_t i = 0; i < numberOfTerms; ++i) { + const Activated& activated = fuzzyOutput->getTerm(i); + w = activated.getDegree(); + z = activated.getTerm()->membership(w); + const TNorm* implication = activated.getImplication(); + wz = implication ? implication->compute(w, z) : (w * z); + if (aggregation) { + sum = aggregation->compute(sum, wz); + weights = aggregation->compute(weights, w); + } else { + sum += wz; + weights += w; + } + } + } else { + scalar w, z, wz; + for (std::size_t i = 0; i < numberOfTerms; ++i) { + const Activated& activated = fuzzyOutput->getTerm(i); + w = activated.getDegree(); + z = activated.getTerm()->tsukamoto(w, minimum, maximum); + const TNorm* implication = activated.getImplication(); + wz = implication ? implication->compute(w, z) : (w * z); + if (aggregation) { + sum = aggregation->compute(sum, wz); + weights = aggregation->compute(weights, w); + } else { + sum += wz; + weights += w; + } + } + } + return sum / weights; + } + + WeightedAverageCustom* WeightedAverageCustom::clone() const { + return new WeightedAverageCustom(*this); + } + + Defuzzifier* WeightedAverageCustom::constructor() { + return new WeightedAverageCustom; + } + +} diff --git a/fuzzylite/src/defuzzifier/WeightedDefuzzifier.cpp b/fuzzylite/src/defuzzifier/WeightedDefuzzifier.cpp new file mode 100644 index 0000000..743c59f --- /dev/null +++ b/fuzzylite/src/defuzzifier/WeightedDefuzzifier.cpp @@ -0,0 +1,76 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/defuzzifier/WeightedDefuzzifier.h" + +#include "fl/term/Activated.h" +#include "fl/term/Concave.h" +#include "fl/term/Constant.h" +#include "fl/term/Function.h" +#include "fl/term/Linear.h" +#include "fl/term/Ramp.h" +#include "fl/term/Sigmoid.h" +#include "fl/term/SShape.h" +#include "fl/term/ZShape.h" + +namespace fl { + + WeightedDefuzzifier::WeightedDefuzzifier(Type type) : _type(type) { } + + WeightedDefuzzifier::WeightedDefuzzifier(const std::string& type) { + if (type == "Automatic") setType(Automatic); + else if (type == "TakagiSugeno") setType(TakagiSugeno); + else if (type == "Tsukamoto") setType(Tsukamoto); + else { + setType(Automatic); + FL_LOG("[warning] incorrect type <" + type + "> of WeightedDefuzzifier" + + " has been defaulted to <Automatic>"); + } + } + + WeightedDefuzzifier::~WeightedDefuzzifier() { } + + std::string WeightedDefuzzifier::typeName(Type type) { + switch (type) { + case Automatic: return "Automatic"; + case TakagiSugeno: return "TakagiSugeno"; + case Tsukamoto: return "Tsukamoto"; + default: return ""; + } + } + + void WeightedDefuzzifier::setType(Type type) { + this->_type = type; + } + + WeightedDefuzzifier::Type WeightedDefuzzifier::getType() const { + return this->_type; + } + + std::string WeightedDefuzzifier::getTypeName() const { + return typeName(getType()); + } + + WeightedDefuzzifier::Type WeightedDefuzzifier::inferType(const Term* term) const { + if (dynamic_cast<const Constant*> (term) + or dynamic_cast<const Linear*> (term) + or dynamic_cast<const Function*> (term)) { + return TakagiSugeno; + } + return Tsukamoto; + } + +} diff --git a/fuzzylite/src/defuzzifier/WeightedSum.cpp b/fuzzylite/src/defuzzifier/WeightedSum.cpp new file mode 100644 index 0000000..6c2343d --- /dev/null +++ b/fuzzylite/src/defuzzifier/WeightedSum.cpp @@ -0,0 +1,97 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/defuzzifier/WeightedSum.h" + +#include "fl/term/Aggregated.h" + +#include <map> + +namespace fl { + + WeightedSum::WeightedSum(Type type) : WeightedDefuzzifier(type) { } + + WeightedSum::WeightedSum(const std::string& type) : WeightedDefuzzifier(type) { } + + WeightedSum::~WeightedSum() { } + + std::string WeightedSum::className() const { + return "WeightedSum"; + } + + Complexity WeightedSum::complexity(const Term* term) const { + Complexity result; + result.comparison(4).function(1); + const Aggregated* fuzzyOutput = dynamic_cast<const Aggregated*> (term); + if (fuzzyOutput) { + result += term->complexity().arithmetic(2).multiply(scalar(fuzzyOutput->numberOfTerms())); + } + return result; + } + + scalar WeightedSum::defuzzify(const Term* term, + scalar minimum, scalar maximum) const { + const Aggregated* fuzzyOutput = dynamic_cast<const Aggregated*> (term); + if (not fuzzyOutput) { + std::ostringstream ss; + ss << "[defuzzification error]" + << "expected an Aggregated term instead of" + << "<" << (term ? term->toString() : "null") << ">"; + throw Exception(ss.str(), FL_AT); + } + + if (fuzzyOutput->isEmpty()) return fl::nan; + + minimum = fuzzyOutput->getMinimum(); + maximum = fuzzyOutput->getMaximum(); + + Type type = getType(); + if (type == Automatic) { + type = inferType(&(fuzzyOutput->terms().front())); + } + + scalar sum = 0.0; + const std::size_t numberOfTerms = fuzzyOutput->numberOfTerms(); + if (type == TakagiSugeno) { + //Provides Takagi-Sugeno and Inverse Tsukamoto of Functions + scalar w, z; + for (std::size_t i = 0; i < numberOfTerms; ++i) { + const Activated& activated = fuzzyOutput->getTerm(i); + w = activated.getDegree(); + z = activated.getTerm()->membership(w); + sum += w * z; + } + } else { + scalar w, z; + for (std::size_t i = 0; i < numberOfTerms; ++i) { + const Activated& activated = fuzzyOutput->getTerm(i); + w = activated.getDegree(); + z = activated.getTerm()->tsukamoto(w, minimum, maximum); + sum += w * z; + } + } + return sum; + } + + WeightedSum* WeightedSum::clone() const { + return new WeightedSum(*this); + } + + Defuzzifier* WeightedSum::constructor() { + return new WeightedSum; + } + +} diff --git a/fuzzylite/src/defuzzifier/WeightedSumCustom.cpp b/fuzzylite/src/defuzzifier/WeightedSumCustom.cpp new file mode 100644 index 0000000..5a9084c --- /dev/null +++ b/fuzzylite/src/defuzzifier/WeightedSumCustom.cpp @@ -0,0 +1,112 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/defuzzifier/WeightedSumCustom.h" + +#include "fl/term/Aggregated.h" + +#include <map> + +namespace fl { + + WeightedSumCustom::WeightedSumCustom(Type type) : WeightedDefuzzifier(type) { } + + WeightedSumCustom::WeightedSumCustom(const std::string& type) : WeightedDefuzzifier(type) { } + + WeightedSumCustom::~WeightedSumCustom() { } + + std::string WeightedSumCustom::className() const { + return "WeightedSumCustom"; + } + + Complexity WeightedSumCustom::complexity(const Term* term) const { + Complexity result; + result.comparison(4).function(1); + const Aggregated* fuzzyOutput = dynamic_cast<const Aggregated*> (term); + if (fuzzyOutput) { + result += term->complexity().arithmetic(2).comparison(2) + .multiply(scalar(fuzzyOutput->numberOfTerms())); + } + return result; + } + + scalar WeightedSumCustom::defuzzify(const Term* term, + scalar minimum, scalar maximum) const { + const Aggregated* fuzzyOutput = dynamic_cast<const Aggregated*> (term); + if (not fuzzyOutput) { + std::ostringstream ss; + ss << "[defuzzification error]" + << "expected an Aggregated term instead of" + << "<" << (term ? term->toString() : "null") << ">"; + throw Exception(ss.str(), FL_AT); + } + + if (fuzzyOutput->isEmpty()) return fl::nan; + + minimum = fuzzyOutput->getMinimum(); + maximum = fuzzyOutput->getMaximum(); + + Type type = getType(); + if (type == Automatic) { + type = inferType(&(fuzzyOutput->terms().front())); + } + + SNorm* aggregation = fuzzyOutput->getAggregation(); + + scalar sum = 0.0; + const std::size_t numberOfTerms = fuzzyOutput->numberOfTerms(); + if (type == TakagiSugeno) { + //Provides Takagi-Sugeno and Inverse Tsukamoto of Functions + scalar w, z, wz; + for (std::size_t i = 0; i < numberOfTerms; ++i) { + const Activated& activated = fuzzyOutput->getTerm(i); + w = activated.getDegree(); + z = activated.getTerm()->membership(w); + const TNorm* implication = activated.getImplication(); + wz = implication ? implication->compute(w, z) : (w * z); + if (aggregation) { + sum = aggregation->compute(sum, wz); + } else { + sum += wz; + } + } + } else { + scalar w, z, wz; + for (std::size_t i = 0; i < numberOfTerms; ++i) { + const Activated& activated = fuzzyOutput->getTerm(i); + w = activated.getDegree(); + z = activated.getTerm()->tsukamoto(w, minimum, maximum); + const TNorm* implication = activated.getImplication(); + wz = implication ? implication->compute(w, z) : (w * z); + if (aggregation) { + sum = aggregation->compute(sum, wz); + } else { + sum += wz; + } + } + } + return sum; + } + + WeightedSumCustom* WeightedSumCustom::clone() const { + return new WeightedSumCustom(*this); + } + + Defuzzifier* WeightedSumCustom::constructor() { + return new WeightedSumCustom; + } + +} diff --git a/fuzzylite/src/factory/ActivationFactory.cpp b/fuzzylite/src/factory/ActivationFactory.cpp new file mode 100644 index 0000000..d000667 --- /dev/null +++ b/fuzzylite/src/factory/ActivationFactory.cpp @@ -0,0 +1,42 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/factory/ActivationFactory.h" + +#include "fl/activation/First.h" +#include "fl/activation/General.h" +#include "fl/activation/Highest.h" +#include "fl/activation/Last.h" +#include "fl/activation/Lowest.h" +#include "fl/activation/Proportional.h" +#include "fl/activation/Threshold.h" + +namespace fl { + + ActivationFactory::ActivationFactory() : ConstructionFactory<Activation*>("Activation") { + registerConstructor("", fl::null); + registerConstructor(First().className(), &(First::constructor)); + registerConstructor(General().className(), &(General::constructor)); + registerConstructor(Highest().className(), &(Highest::constructor)); + registerConstructor(Last().className(), &(Last::constructor)); + registerConstructor(Lowest().className(), &(Lowest::constructor)); + registerConstructor(Proportional().className(), &(Proportional::constructor)); + registerConstructor(Threshold().className(), &(Threshold::constructor)); + } + + ActivationFactory::~ActivationFactory() { } + +} diff --git a/fuzzylite/src/factory/DefuzzifierFactory.cpp b/fuzzylite/src/factory/DefuzzifierFactory.cpp new file mode 100644 index 0000000..3b0bfb1 --- /dev/null +++ b/fuzzylite/src/factory/DefuzzifierFactory.cpp @@ -0,0 +1,73 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/factory/DefuzzifierFactory.h" + +#include "fl/defuzzifier/Bisector.h" +#include "fl/defuzzifier/Centroid.h" +#include "fl/defuzzifier/SmallestOfMaximum.h" +#include "fl/defuzzifier/LargestOfMaximum.h" +#include "fl/defuzzifier/MeanOfMaximum.h" +#include "fl/defuzzifier/WeightedAverage.h" +#include "fl/defuzzifier/WeightedAverageCustom.h" +#include "fl/defuzzifier/WeightedSum.h" +#include "fl/defuzzifier/WeightedSumCustom.h" + +namespace fl { + + DefuzzifierFactory::DefuzzifierFactory() : ConstructionFactory<Defuzzifier*>("Defuzzifier") { + registerConstructor("", fl::null); + registerConstructor(Bisector().className(), &(Bisector::constructor)); + registerConstructor(Centroid().className(), &(Centroid::constructor)); + registerConstructor(LargestOfMaximum().className(), &(LargestOfMaximum::constructor)); + registerConstructor(MeanOfMaximum().className(), &(MeanOfMaximum::constructor)); + registerConstructor(SmallestOfMaximum().className(), &(SmallestOfMaximum::constructor)); + registerConstructor(WeightedAverage().className(), &(WeightedAverage::constructor)); +// registerConstructor(WeightedAverageCustom().className(), &(WeightedAverageCustom::constructor)); + registerConstructor(WeightedSum().className(), &(WeightedSum::constructor)); +// registerConstructor(WeightedSumCustom().className(), &(WeightedSumCustom::constructor)); + } + + DefuzzifierFactory::~DefuzzifierFactory() { } + + Defuzzifier* DefuzzifierFactory::constructDefuzzifier(const std::string& key, + int resolution, WeightedDefuzzifier::Type type) const { + Defuzzifier* result = constructObject(key); + if (IntegralDefuzzifier * integralDefuzzifier = dynamic_cast<IntegralDefuzzifier*> (result)) { + integralDefuzzifier->setResolution(resolution); + } else if (WeightedDefuzzifier * weightedDefuzzifier = dynamic_cast<WeightedDefuzzifier*> (result)) { + weightedDefuzzifier->setType(type); + } + return result; + } + + Defuzzifier* DefuzzifierFactory::constructDefuzzifier(const std::string& key, int resolution) const { + Defuzzifier* result = constructObject(key); + if (IntegralDefuzzifier * integralDefuzzifier = dynamic_cast<IntegralDefuzzifier*> (result)) { + integralDefuzzifier->setResolution(resolution); + } + return result; + } + + Defuzzifier* DefuzzifierFactory::constructDefuzzifier(const std::string& key, WeightedDefuzzifier::Type type) { + Defuzzifier* result = constructObject(key); + if (WeightedDefuzzifier * weightedDefuzzifier = dynamic_cast<WeightedDefuzzifier*> (result)) { + weightedDefuzzifier->setType(type); + } + return result; + } + +} diff --git a/fuzzylite/src/factory/FactoryManager.cpp b/fuzzylite/src/factory/FactoryManager.cpp new file mode 100644 index 0000000..367d982 --- /dev/null +++ b/fuzzylite/src/factory/FactoryManager.cpp @@ -0,0 +1,129 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/factory/FactoryManager.h" + +namespace fl { + + FactoryManager* FactoryManager::instance() { + static FL_ITHREAD_LOCAL FactoryManager _instance; + return &_instance; + } + + FactoryManager::FactoryManager() : + _tnorm(new TNormFactory), _snorm(new SNormFactory), _activation(new ActivationFactory), + _defuzzifier(new DefuzzifierFactory), _term(new TermFactory), + _hedge(new HedgeFactory), _function(new FunctionFactory) { } + + FactoryManager::FactoryManager(TNormFactory* tnorm, SNormFactory* snorm, + ActivationFactory* activation, DefuzzifierFactory* defuzzifier, + TermFactory* term, HedgeFactory* hedge, FunctionFactory* function) : + _tnorm(tnorm), _snorm(snorm), _activation(activation), + _defuzzifier(defuzzifier), _term(term), _hedge(hedge), _function(function) { } + + FactoryManager::FactoryManager(const FactoryManager& other) + : _tnorm(fl::null), _snorm(fl::null), _activation(fl::null), + _defuzzifier(fl::null), _term(fl::null), _hedge(fl::null), + _function(fl::null) { + if (other._tnorm.get()) this->_tnorm.reset(new TNormFactory(*other._tnorm.get())); + if (other._snorm.get()) this->_snorm.reset(new SNormFactory(*other._snorm.get())); + if (other._activation.get()) this->_activation.reset(new ActivationFactory(*other._activation.get())); + if (other._defuzzifier.get()) this->_defuzzifier.reset(new DefuzzifierFactory(*other._defuzzifier.get())); + if (other._term.get()) this->_term.reset(new TermFactory(*other._term.get())); + if (other._hedge.get()) this->_hedge.reset(new HedgeFactory(*other._hedge.get())); + if (other._function.get()) this->_function.reset(new FunctionFactory(*other._function.get())); + } + + FactoryManager& FactoryManager::operator=(const FactoryManager& other) { + if (this != &other) { + _tnorm.reset(fl::null); + _snorm.reset(fl::null); + _activation.reset(fl::null); + _defuzzifier.reset(fl::null); + _term.reset(fl::null); + _hedge.reset(fl::null); + _function.reset(fl::null); + + if (other._tnorm.get()) this->_tnorm.reset(new TNormFactory(*other._tnorm.get())); + if (other._snorm.get()) this->_snorm.reset(new SNormFactory(*other._snorm.get())); + if (other._activation.get()) this->_activation.reset(new ActivationFactory(*other._activation.get())); + if (other._defuzzifier.get()) this->_defuzzifier.reset(new DefuzzifierFactory(*other._defuzzifier.get())); + if (other._term.get()) this->_term.reset(new TermFactory(*other._term.get())); + if (other._hedge.get()) this->_hedge.reset(new HedgeFactory(*other._hedge.get())); + if (other._function.get()) this->_function.reset(new FunctionFactory(*other._function.get())); + } + return *this; + } + + FactoryManager::~FactoryManager() { } + + void FactoryManager::setTnorm(TNormFactory* tnorm) { + this->_tnorm.reset(tnorm); + } + + TNormFactory* FactoryManager::tnorm() const { + return this->_tnorm.get(); + } + + void FactoryManager::setSnorm(SNormFactory* snorm) { + this->_snorm.reset(snorm); + } + + SNormFactory* FactoryManager::snorm() const { + return this->_snorm.get(); + } + + void FactoryManager::setActivation(ActivationFactory* activation) { + this->_activation.reset(activation); + } + + ActivationFactory* FactoryManager::activation() const { + return this->_activation.get(); + } + + void FactoryManager::setDefuzzifier(DefuzzifierFactory* defuzzifier) { + this->_defuzzifier.reset(defuzzifier); + } + + DefuzzifierFactory* FactoryManager::defuzzifier() const { + return this->_defuzzifier.get(); + } + + void FactoryManager::setTerm(TermFactory* term) { + this->_term.reset(term); + } + + TermFactory* FactoryManager::term() const { + return this->_term.get(); + } + + void FactoryManager::setHedge(HedgeFactory* hedge) { + this->_hedge.reset(hedge); + } + + HedgeFactory* FactoryManager::hedge() const { + return this->_hedge.get(); + } + + void FactoryManager::setFunction(FunctionFactory* function) { + this->_function.reset(function); + } + + FunctionFactory* FactoryManager::function() const { + return this->_function.get(); + } + +} diff --git a/fuzzylite/src/factory/FunctionFactory.cpp b/fuzzylite/src/factory/FunctionFactory.cpp new file mode 100644 index 0000000..8e52ddf --- /dev/null +++ b/fuzzylite/src/factory/FunctionFactory.cpp @@ -0,0 +1,169 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/factory/FunctionFactory.h" + +#include "fl/rule/Rule.h" + +namespace fl { + + FunctionFactory::FunctionFactory() : CloningFactory<Function::Element*>("Function::Element") { + registerOperators(); + registerFunctions(); + } + + FunctionFactory::~FunctionFactory() { } + + void FunctionFactory::registerOperators() { + //OPERATORS: + int p = 100; + //First order: not, negate: + registerObject("!", new Function::Element("!", "Logical NOT", + Function::Element::Operator, &(Op::logicalNot), p, 1)); //logical not + registerObject("~", new Function::Element("~", "Negation", + Function::Element::Operator, &(Op::negate), p, 1)); // ~ negates a number + + p -= 10; + //Second order: power + registerObject("^", new Function::Element("^", "Power", + Function::Element::Operator, &(std::pow), p, 1)); + + p -= 10; + //Third order: multiplication, division, modulo + registerObject("*", new Function::Element("*", "Multiplication", + Function::Element::Operator, &(Op::multiply), p)); + registerObject("/", new Function::Element("/", "Division", + Function::Element::Operator, &(Op::divide), p)); + registerObject("%", new Function::Element("%", "Modulo", + Function::Element::Operator, &(Op::modulo), p)); + + p -= 10; + //Fourth order: addition, subtraction + registerObject("+", new Function::Element("+", "Addition", + Function::Element::Operator, &(Op::add), p)); + registerObject("-", new Function::Element("-", "Subtraction", + Function::Element::Operator, &(Op::subtract), p)); + + //Fifth order: logical and, logical or + p -= 10; //Logical AND + registerObject(Rule::andKeyword(), new Function::Element(Rule::andKeyword(), "Logical AND", + Function::Element::Operator, &(Op::logicalAnd), p)); + p -= 10; //Logical OR + registerObject(Rule::orKeyword(), new Function::Element(Rule::orKeyword(), "Logical OR", + Function::Element::Operator, &(Op::logicalOr), p)); + } + + void FunctionFactory::registerFunctions() { + //FUNCTIONS + registerObject("gt", new Function::Element("gt", "Greater than (>)", + Function::Element::Function, &(Op::gt))); + registerObject("ge", new Function::Element("ge", "Greater than or equal to (>=)", + Function::Element::Function, &(Op::ge))); + registerObject("eq", new Function::Element("eq", "Equal to (==)", + Function::Element::Function, &(Op::eq))); + registerObject("neq", new Function::Element("neq", "Not equal to (!=)", + Function::Element::Function, &(Op::neq))); + registerObject("le", new Function::Element("le", "Less than or equal to (<=)", + Function::Element::Function, &(Op::le))); + registerObject("lt", new Function::Element("lt", "Less than (<)", + Function::Element::Function, &(Op::lt))); + + registerObject("min", new Function::Element("min", "Minimum", + Function::Element::Function, &(Op::min))); + registerObject("max", new Function::Element("max", "Maximum", + Function::Element::Function, &(Op::max))); + + registerObject("acos", new Function::Element("acos", "Inverse cosine", + Function::Element::Function, &(std::acos))); + registerObject("asin", new Function::Element("asin", "Inverse sine", + Function::Element::Function, &(std::asin))); + registerObject("atan", new Function::Element("atan", "Inverse tangent", + Function::Element::Function, &(std::atan))); + + registerObject("ceil", new Function::Element("ceil", "Ceiling", + Function::Element::Function, &(std::ceil))); + registerObject("cos", new Function::Element("cos", "Cosine", + Function::Element::Function, &(std::cos))); + registerObject("cosh", new Function::Element("cosh", "Hyperbolic cosine", + Function::Element::Function, &(std::cosh))); + registerObject("exp", new Function::Element("exp", "Exponential", + Function::Element::Function, &(std::exp))); + registerObject("abs", new Function::Element("abs", "Absolute", + Function::Element::Function, &(std::abs))); + registerObject("fabs", new Function::Element("fabs", "Absolute", + Function::Element::Function, &(std::fabs))); + registerObject("floor", new Function::Element("floor", "Floor", + Function::Element::Function, &(std::floor))); + registerObject("log", new Function::Element("log", "Natural logarithm", + Function::Element::Function, &(std::log))); + registerObject("log10", new Function::Element("log10", "Common logarithm", + Function::Element::Function, &(std::log10))); + registerObject("round", new Function::Element("round", "Round", + Function::Element::Function, &(Op::round))); + registerObject("sin", new Function::Element("sin", "Sine", + Function::Element::Function, &(std::sin))); + registerObject("sinh", new Function::Element("sinh", "Hyperbolic sine", + Function::Element::Function, &(std::sinh))); + registerObject("sqrt", new Function::Element("sqrt", "Square root", + Function::Element::Function, &(std::sqrt))); + registerObject("tan", new Function::Element("tan", "Tangent", + Function::Element::Function, &(std::tan))); + registerObject("tanh", new Function::Element("tanh", "Hyperbolic tangent", + Function::Element::Function, &(std::tanh))); + +#if defined(FL_UNIX) && !defined(FL_USE_FLOAT) + //found in Unix when using double precision. not found in Windows. + registerObject("log1p", new Function::Element("log1p", "Natural logarithm plus one", + Function::Element::Function, &(log1p))); + registerObject("acosh", new Function::Element("acosh", "Inverse hyperbolic cosine", + Function::Element::Function, &(acosh))); + registerObject("asinh", new Function::Element("asinh", "Inverse hyperbolic sine", + Function::Element::Function, &(asinh))); + registerObject("atanh", new Function::Element("atanh", "Inverse hyperbolic tangent", + Function::Element::Function, &(atanh))); +#endif + + registerObject("pow", new Function::Element("pow", "Power", + Function::Element::Function, &(std::pow))); + registerObject("atan2", new Function::Element("atan2", "Inverse tangent (y,x)", + Function::Element::Function, &(std::atan2))); + registerObject("fmod", new Function::Element("fmod", "Floating-point remainder", + Function::Element::Function, &(std::fmod))); + } + + std::vector<std::string> FunctionFactory::availableOperators() const { + std::vector<std::string> result; + std::map<std::string, Function::Element*>::const_iterator it = this->objects().begin(); + while (it != this->objects().end()) { + if (it->second and it->second->type == Function::Element::Operator) + result.push_back(it->first); + ++it; + } + return result; + } + + std::vector<std::string> FunctionFactory::availableFunctions() const { + std::vector<std::string> result; + std::map<std::string, Function::Element*>::const_iterator it = this->objects().begin(); + while (it != this->objects().end()) { + if (it->second and it->second->type == Function::Element::Function) + result.push_back(it->first); + ++it; + } + return result; + } + +} diff --git a/fuzzylite/src/factory/HedgeFactory.cpp b/fuzzylite/src/factory/HedgeFactory.cpp new file mode 100644 index 0000000..1a6452c --- /dev/null +++ b/fuzzylite/src/factory/HedgeFactory.cpp @@ -0,0 +1,40 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/factory/HedgeFactory.h" + +#include "fl/hedge/Any.h" +#include "fl/hedge/Extremely.h" +#include "fl/hedge/Not.h" +#include "fl/hedge/Seldom.h" +#include "fl/hedge/Somewhat.h" +#include "fl/hedge/Very.h" + +namespace fl { + + HedgeFactory::HedgeFactory() : ConstructionFactory<Hedge*>("Hedge") { + registerConstructor("", fl::null); + registerConstructor(Any().name(), &(Any::constructor)); + registerConstructor(Extremely().name(), &(Extremely::constructor)); + registerConstructor(Not().name(), &(Not::constructor)); + registerConstructor(Seldom().name(), &(Seldom::constructor)); + registerConstructor(Somewhat().name(), &(Somewhat::constructor)); + registerConstructor(Very().name(), &(Very::constructor)); + } + + HedgeFactory::~HedgeFactory() { } + +} diff --git a/fuzzylite/src/factory/SNormFactory.cpp b/fuzzylite/src/factory/SNormFactory.cpp new file mode 100644 index 0000000..fdaef87 --- /dev/null +++ b/fuzzylite/src/factory/SNormFactory.cpp @@ -0,0 +1,46 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/factory/SNormFactory.h" + +#include "fl/norm/s/AlgebraicSum.h" +#include "fl/norm/s/BoundedSum.h" +#include "fl/norm/s/DrasticSum.h" +#include "fl/norm/s/EinsteinSum.h" +#include "fl/norm/s/HamacherSum.h" +#include "fl/norm/s/Maximum.h" +#include "fl/norm/s/NilpotentMaximum.h" +#include "fl/norm/s/NormalizedSum.h" +#include "fl/norm/s/UnboundedSum.h" + +namespace fl { + + SNormFactory::SNormFactory() : ConstructionFactory<SNorm*>("SNorm") { + registerConstructor("", fl::null); + registerConstructor(AlgebraicSum().className(), &(AlgebraicSum::constructor)); + registerConstructor(BoundedSum().className(), &(BoundedSum::constructor)); + registerConstructor(DrasticSum().className(), &(DrasticSum::constructor)); + registerConstructor(EinsteinSum().className(), &(EinsteinSum::constructor)); + registerConstructor(HamacherSum().className(), &(HamacherSum::constructor)); + registerConstructor(Maximum().className(), &(Maximum::constructor)); + registerConstructor(NilpotentMaximum().className(), &(NilpotentMaximum::constructor)); + registerConstructor(NormalizedSum().className(), &(NormalizedSum::constructor)); + registerConstructor(UnboundedSum().className(), &(UnboundedSum::constructor)); + } + + SNormFactory::~SNormFactory() { } + +} diff --git a/fuzzylite/src/factory/TNormFactory.cpp b/fuzzylite/src/factory/TNormFactory.cpp new file mode 100644 index 0000000..0dda02e --- /dev/null +++ b/fuzzylite/src/factory/TNormFactory.cpp @@ -0,0 +1,42 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/factory/TNormFactory.h" + +#include "fl/norm/t/AlgebraicProduct.h" +#include "fl/norm/t/BoundedDifference.h" +#include "fl/norm/t/DrasticProduct.h" +#include "fl/norm/t/EinsteinProduct.h" +#include "fl/norm/t/HamacherProduct.h" +#include "fl/norm/t/Minimum.h" +#include "fl/norm/t/NilpotentMinimum.h" + +namespace fl { + + TNormFactory::TNormFactory() : ConstructionFactory<TNorm*>("TNorm") { + registerConstructor("", fl::null); + registerConstructor(AlgebraicProduct().className(), &(AlgebraicProduct::constructor)); + registerConstructor(BoundedDifference().className(), &(BoundedDifference::constructor)); + registerConstructor(DrasticProduct().className(), &(DrasticProduct::constructor)); + registerConstructor(EinsteinProduct().className(), &(EinsteinProduct::constructor)); + registerConstructor(HamacherProduct().className(), &(HamacherProduct::constructor)); + registerConstructor(Minimum().className(), &(Minimum::constructor)); + registerConstructor(NilpotentMinimum().className(), &(NilpotentMinimum::constructor)); + } + + TNormFactory::~TNormFactory() { } + +} diff --git a/fuzzylite/src/factory/TermFactory.cpp b/fuzzylite/src/factory/TermFactory.cpp new file mode 100644 index 0000000..e135704 --- /dev/null +++ b/fuzzylite/src/factory/TermFactory.cpp @@ -0,0 +1,70 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/factory/TermFactory.h" + +#include "fl/term/Bell.h" +#include "fl/term/Binary.h" +#include "fl/term/Concave.h" +#include "fl/term/Constant.h" +#include "fl/term/Cosine.h" +#include "fl/term/Discrete.h" +#include "fl/term/Function.h" +#include "fl/term/Gaussian.h" +#include "fl/term/GaussianProduct.h" +#include "fl/term/Linear.h" +#include "fl/term/PiShape.h" +#include "fl/term/Ramp.h" +#include "fl/term/Rectangle.h" +#include "fl/term/SShape.h" +#include "fl/term/Sigmoid.h" +#include "fl/term/SigmoidDifference.h" +#include "fl/term/SigmoidProduct.h" +#include "fl/term/Spike.h" +#include "fl/term/Trapezoid.h" +#include "fl/term/Triangle.h" +#include "fl/term/ZShape.h" + +namespace fl { + + TermFactory::TermFactory() : ConstructionFactory<Term*>("Term") { + registerConstructor("", fl::null); + registerConstructor(Bell().className(), &(Bell::constructor)); + registerConstructor(Binary().className(), &(Binary::constructor)); + registerConstructor(Concave().className(), &(Concave::constructor)); + registerConstructor(Constant().className(), &(Constant::constructor)); + registerConstructor(Cosine().className(), &(Cosine::constructor)); + registerConstructor(Discrete().className(), &(Discrete::constructor)); + registerConstructor(Function().className(), &(Function::constructor)); + registerConstructor(Gaussian().className(), &(Gaussian::constructor)); + registerConstructor(GaussianProduct().className(), &(GaussianProduct::constructor)); + registerConstructor(Linear().className(), &(Linear::constructor)); + registerConstructor(PiShape().className(), &(PiShape::constructor)); + registerConstructor(Ramp().className(), &(Ramp::constructor)); + registerConstructor(Rectangle().className(), &(Rectangle::constructor)); + registerConstructor(SShape().className(), &(SShape::constructor)); + registerConstructor(Sigmoid().className(), &(Sigmoid::constructor)); + registerConstructor(SigmoidDifference().className(), &(SigmoidDifference::constructor)); + registerConstructor(SigmoidProduct().className(), &(SigmoidProduct::constructor)); + registerConstructor(Spike().className(), &(Spike::constructor)); + registerConstructor(Trapezoid().className(), &(Trapezoid::constructor)); + registerConstructor(Triangle().className(), &(Triangle::constructor)); + registerConstructor(ZShape().className(), &(ZShape::constructor)); + } + + TermFactory::~TermFactory() { } + +} diff --git a/fuzzylite/src/fuzzylite.cpp b/fuzzylite/src/fuzzylite.cpp new file mode 100644 index 0000000..db17fec --- /dev/null +++ b/fuzzylite/src/fuzzylite.cpp @@ -0,0 +1,53 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/fuzzylite.h" + +namespace fl { + + + int fuzzylite::_decimals = 3; + std::ios_base::fmtflags fuzzylite::_scalarFormat = std::ios_base::fixed; + scalar fuzzylite::_macheps = 1e-6; + bool fuzzylite::_debugging = false; + bool fuzzylite::_logging = true; + + std::string platform() { +#ifdef FL_UNIX + return "Unix"; +#elif defined FL_WINDOWS + return "Windows"; +#else + return "?"; +#endif + } + + std::string floatingPoint() { + scalar someScalar = 0; + FL_IUNUSED(someScalar); + std::string type; + + std::ostringstream ss; +#ifdef FL_USE_FLOAT + type = "float"; +#else + type = "double"; +#endif + ss << "fl::scalar is defined as \'" << type << "\' using " << + sizeof (someScalar) << " bytes"; + return ss.str(); + } +} diff --git a/fuzzylite/src/hedge/Any.cpp b/fuzzylite/src/hedge/Any.cpp new file mode 100644 index 0000000..9d04262 --- /dev/null +++ b/fuzzylite/src/hedge/Any.cpp @@ -0,0 +1,47 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/hedge/Any.h" + +namespace fl { + + Any::Any() { } + + Any::~Any() { } + + std::string Any::name() const { + return "any"; + } + + Complexity Any::complexity() const { + return Complexity(); + } + + scalar Any::hedge(scalar x) const { + FL_IUNUSED(x); + return 1.0; + } + + Any* Any::clone() const { + return new Any(*this); + } + + Hedge* Any::constructor() { + return new Any; + } + +} + diff --git a/fuzzylite/src/hedge/Extremely.cpp b/fuzzylite/src/hedge/Extremely.cpp new file mode 100644 index 0000000..60d3abd --- /dev/null +++ b/fuzzylite/src/hedge/Extremely.cpp @@ -0,0 +1,46 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/hedge/Extremely.h" + +#include "fl/Operation.h" + +namespace fl { + + std::string Extremely::name() const { + return "extremely"; + } + + Complexity Extremely::complexity() const { + return Complexity().comparison(1).arithmetic(5); + } + + scalar Extremely::hedge(scalar x) const { + return Op::isLE(x, 0.5) + ? 2.0 * x * x + : (1.0 - 2.0 * (1.0 - x) * (1.0 - x)); + } + + Extremely* Extremely::clone() const { + return new Extremely(*this); + } + + Hedge* Extremely::constructor() { + return new Extremely; + } + +} + diff --git a/fuzzylite/src/hedge/HedgeFunction.cpp b/fuzzylite/src/hedge/HedgeFunction.cpp new file mode 100644 index 0000000..c31c72c --- /dev/null +++ b/fuzzylite/src/hedge/HedgeFunction.cpp @@ -0,0 +1,63 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/hedge/HedgeFunction.h" + +namespace fl { + + HedgeFunction::HedgeFunction(const std::string& formula) : Hedge() { + _function.variables["x"] = fl::nan; + if (not formula.empty()) { + _function.load(formula); + } + } + + std::string HedgeFunction::name() const { + return "HedgeFunction"; + } + + Complexity HedgeFunction::complexity() const { + if (_function.root()) + return _function.complexity().function(2 * std::log(scalar(_function.variables.size()))); + return _function.complexity(); + } + + scalar HedgeFunction::hedge(scalar x) const { + _function.variables["x"] = x; + return _function.membership(x); + } + + Function& HedgeFunction::function() { + return this->_function; + } + + void HedgeFunction::setFormula(const std::string& formula) { + _function.load(formula); + } + + std::string HedgeFunction::getFormula() const { + return _function.getFormula(); + } + + HedgeFunction* HedgeFunction::clone() const { + return new HedgeFunction(*this); + } + + Hedge* HedgeFunction::constructor() { + return new HedgeFunction; + } + +} diff --git a/fuzzylite/src/hedge/Not.cpp b/fuzzylite/src/hedge/Not.cpp new file mode 100644 index 0000000..defbabd --- /dev/null +++ b/fuzzylite/src/hedge/Not.cpp @@ -0,0 +1,41 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/hedge/Not.h" + +namespace fl { + + std::string Not::name() const { + return "not"; + } + + Complexity Not::complexity() const { + return Complexity().arithmetic(1); + } + + scalar Not::hedge(scalar x) const { + return 1.0 - x; + } + + Not* Not::clone() const { + return new Not(*this); + } + + Hedge* Not::constructor() { + return new Not; + } + +} diff --git a/fuzzylite/src/hedge/Seldom.cpp b/fuzzylite/src/hedge/Seldom.cpp new file mode 100644 index 0000000..092e873 --- /dev/null +++ b/fuzzylite/src/hedge/Seldom.cpp @@ -0,0 +1,45 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/hedge/Seldom.h" + +#include "fl/Operation.h" + +namespace fl { + + std::string Seldom::name() const { + return "seldom"; + } + + Complexity Seldom::complexity() const { + return Complexity().comparison(1).function(1).arithmetic(3); + } + + scalar Seldom::hedge(scalar x) const { + return Op::isLE(x, 0.5) + ? std::sqrt(0.5 * x) + : (1.0 - std::sqrt(0.5 * (1.0 - x))); + } + + Seldom* Seldom::clone() const { + return new Seldom(*this); + } + + Hedge* Seldom::constructor() { + return new Seldom; + } + +} diff --git a/fuzzylite/src/hedge/Somewhat.cpp b/fuzzylite/src/hedge/Somewhat.cpp new file mode 100644 index 0000000..4d68c20 --- /dev/null +++ b/fuzzylite/src/hedge/Somewhat.cpp @@ -0,0 +1,41 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/hedge/Somewhat.h" + +namespace fl { + + std::string Somewhat::name() const { + return "somewhat"; + } + + Complexity Somewhat::complexity() const { + return Complexity().function(1); + } + + scalar Somewhat::hedge(scalar x) const { + return std::sqrt(x); + } + + Somewhat* Somewhat::clone() const { + return new Somewhat(*this); + } + + Hedge* Somewhat::constructor() { + return new Somewhat; + } + +} diff --git a/fuzzylite/src/hedge/Very.cpp b/fuzzylite/src/hedge/Very.cpp new file mode 100644 index 0000000..5ac7a86 --- /dev/null +++ b/fuzzylite/src/hedge/Very.cpp @@ -0,0 +1,41 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/hedge/Very.h" + +namespace fl { + + std::string Very::name() const { + return "very"; + } + + Complexity Very::complexity() const { + return Complexity().arithmetic(1); + } + + scalar Very::hedge(scalar x) const { + return x * x; + } + + Very* Very::clone() const { + return new Very(*this); + } + + Hedge* Very::constructor() { + return new Very; + } + +} diff --git a/fuzzylite/src/imex/CppExporter.cpp b/fuzzylite/src/imex/CppExporter.cpp new file mode 100644 index 0000000..8a77c0c --- /dev/null +++ b/fuzzylite/src/imex/CppExporter.cpp @@ -0,0 +1,267 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/imex/CppExporter.h" + +#include "fl/Headers.h" + +namespace fl { + + CppExporter::CppExporter(bool prefixNamespace, bool usingVariableNames) : Exporter(), + _usingNamespace(prefixNamespace), _usingVariableNames(usingVariableNames) { } + + CppExporter::~CppExporter() { } + + std::string CppExporter::name() const { + return "CppExporter"; + } + + std::string CppExporter::fl(const std::string& clazz) const { + return _usingNamespace ? "fl::" + clazz : clazz; + } + + void CppExporter::setUsingNamespace(bool usingNamespace) { + this->_usingNamespace = usingNamespace; + } + + bool CppExporter::isUsingNamespace() const { + return this->_usingNamespace; + } + + void CppExporter::setUsingVariableNames(bool usingVariableNames) { + this->_usingVariableNames = usingVariableNames; + } + + bool CppExporter::isUsingVariableNames() const { + return this->_usingVariableNames; + } + + std::string CppExporter::toString(const Engine* engine) const { + std::ostringstream cpp; + cpp << "//Code automatically generated with " << fuzzylite::library() << ".\n\n"; + if (not isUsingNamespace()) cpp << "using namespace fl;\n\n"; + cpp << fl("Engine* ") << "engine = new " << fl("Engine;\n"); + cpp << "engine->setName(\"" << engine->getName() << "\");\n"; + cpp << "engine->setDescription(\"" << engine->getDescription() << "\");\n"; + + cpp << "\n"; + + for (std::size_t i = 0; i < engine->numberOfInputVariables(); ++i) { + cpp << toString(engine->getInputVariable(i), engine) << "\n"; + } + + for (std::size_t i = 0; i < engine->numberOfOutputVariables(); ++i) { + cpp << toString(engine->getOutputVariable(i), engine) << "\n"; + } + + for (std::size_t i = 0; i < engine->numberOfRuleBlocks(); ++i) { + cpp << toString(engine->getRuleBlock(i), engine) << "\n"; + } + + return cpp.str(); + } + + std::string CppExporter::toString(const InputVariable* inputVariable, const Engine* engine) const { + std::string name; + if (isUsingVariableNames()) { + name = Op::validName(inputVariable->getName()); + } else { + name = "inputVariable"; + if (engine->numberOfInputVariables() > 1) { + std::size_t index = std::distance(engine->inputVariables().begin(), + std::find(engine->inputVariables().begin(), + engine->inputVariables().end(), inputVariable)); + name += Op::str(index + 1); + } + } + std::ostringstream ss; + ss << fl("InputVariable* ") << name << " = new " << fl("InputVariable;\n"); + ss << name << "->setName(\"" << inputVariable->getName() << "\");\n"; + ss << name << "->setDescription(\"" << inputVariable->getDescription() << "\");\n"; + ss << name << "->setEnabled(" << (inputVariable->isEnabled() ? "true" : "false") << ");\n"; + ss << name << "->setRange(" << + toString(inputVariable->getMinimum()) << ", " << + toString(inputVariable->getMaximum()) << ");\n"; + ss << name << "->setLockValueInRange(" << (inputVariable->isLockValueInRange() ? "true" : "false") << ");\n"; + for (std::size_t t = 0; t < inputVariable->numberOfTerms(); ++t) { + ss << name << "->addTerm(" << toString(inputVariable->getTerm(t)) << ");\n"; + } + ss << "engine->addInputVariable(" << name << ");\n"; + return ss.str(); + } + + std::string CppExporter::toString(const OutputVariable* outputVariable, const Engine* engine) const { + std::string name; + if (isUsingVariableNames()) { + name = Op::validName(outputVariable->getName()); + } else { + name = "outputVariable"; + if (engine->numberOfOutputVariables() > 1) { + std::size_t index = std::distance(engine->outputVariables().begin(), + std::find(engine->outputVariables().begin(), + engine->outputVariables().end(), outputVariable)); + name += Op::str(index + 1); + } + } + std::ostringstream ss; + ss << fl("OutputVariable* ") << name << " = new " << fl("OutputVariable;\n"); + ss << name << "->setName(\"" << outputVariable->getName() << "\");\n"; + ss << name << "->setDescription(\"" << outputVariable->getDescription() << "\");\n"; + ss << name << "->setEnabled(" << (outputVariable->isEnabled() ? "true" : "false") << ");\n"; + ss << name << "->setRange(" << + toString(outputVariable->getMinimum()) << ", " << + toString(outputVariable->getMaximum()) << ");\n"; + ss << name << "->setLockValueInRange(" << + (outputVariable->isLockValueInRange() ? "true" : "false") << ");\n"; + ss << name << "->setAggregation(" << + toString(outputVariable->fuzzyOutput()->getAggregation()) << ");\n"; + ss << name << "->setDefuzzifier(" << + toString(outputVariable->getDefuzzifier()) << ");\n"; + ss << name << "->setDefaultValue(" << + toString(outputVariable->getDefaultValue()) << ");\n"; + ss << name << "->setLockPreviousValue(" << + (outputVariable->isLockPreviousValue() ? "true" : "false") << ");\n"; + for (std::size_t t = 0; t < outputVariable->numberOfTerms(); ++t) { + ss << name << "->addTerm(" << toString(outputVariable->getTerm(t)) << ");\n"; + } + ss << "engine->addOutputVariable(" << name << ");\n"; + return ss.str(); + } + + std::string CppExporter::toString(const RuleBlock* ruleBlock, const Engine* engine) const { + std::string name; + + if (isUsingVariableNames() and not ruleBlock->getName().empty()) { + name = Op::validName(ruleBlock->getName()); + } else { + name = "ruleBlock"; + if (engine->numberOfRuleBlocks() > 1) { + std::size_t index = std::distance(engine->ruleBlocks().begin(), + std::find(engine->ruleBlocks().begin(), + engine->ruleBlocks().end(), ruleBlock)); + name += Op::str(index + 1); + } + } + + std::ostringstream ss; + ss << fl("RuleBlock* ") << name << " = new " << fl("RuleBlock;\n"); + ss << name << "->setName(\"" << ruleBlock->getName() << "\");\n"; + ss << name << "->setDescription(\"" << ruleBlock->getDescription() << "\");\n"; + ss << name << "->setEnabled(" << (ruleBlock->isEnabled() ? "true" : "false") << ");\n"; + ss << name << "->setConjunction(" << + toString(ruleBlock->getConjunction()) << ");\n"; + ss << name << "->setDisjunction(" + << toString(ruleBlock->getDisjunction()) << ");\n"; + ss << name << "->setImplication(" + << toString(ruleBlock->getImplication()) << ");\n"; + ss << name << "->setActivation(" + << toString(ruleBlock->getActivation()) << ");\n"; + for (std::size_t r = 0; r < ruleBlock->numberOfRules(); ++r) { + ss << name << "->addRule(" << fl("Rule") << "::parse(\"" << + ruleBlock->getRule(r)->getText() << "\", engine));\n"; + } + ss << "engine->addRuleBlock(" << name << ");\n"; + return ss.str(); + } + + std::string CppExporter::toString(scalar value) const { + if (Op::isNaN(value)) + return "fl::nan"; + if (Op::isInf(value)) { + return (value > 0 ? "fl::inf" : "-fl::inf"); + } + return Op::str(value); + } + + std::string CppExporter::toString(const Term* term) const { + if (not term) return "fl::null"; + + if (const Discrete * discrete = dynamic_cast<const Discrete*> (term)) { + std::ostringstream ss; + ss << fl(term->className()) << "::create(\"" << term->getName() << "\", " + << (discrete->xy().size() * 2) << ", " + << Op::join(Discrete::toVector(discrete->xy()), ", ") << ")"; + return ss.str(); + } + + if (const Function * function = dynamic_cast<const Function*> (term)) { + std::ostringstream ss; + ss << fl(term->className()) << "::create(\"" << term->getName() << "\", " + << "\"" << function->getFormula() << "\", engine)"; + return ss.str(); + } + + if (const Linear * linear = dynamic_cast<const Linear*> (term)) { + std::ostringstream ss; + ss << fl(term->className()) << "::create(\"" << term->getName() << "\", " + << "engine, " << Op::join(linear->coefficients(), ", ") << ")"; + return ss.str(); + } + + std::ostringstream ss; + ss << "new " << fl(term->className()) << "(\"" << term->getName() << "\", " + << Op::findReplace(term->parameters(), " ", ", ") << ")"; + return ss.str(); + } + + std::string CppExporter::toString(const Hedge * hedge) const { + if (hedge->name() == Any().name()) return "new " + fl("Any"); + if (hedge->name() == Extremely().name()) return "new " + fl("Extremely"); + if (hedge->name() == Not().name()) return "new " + fl("Not"); + if (hedge->name() == Seldom().name()) return "new " + fl("Seldom"); + if (hedge->name() == Somewhat().name()) return "new " + fl("Somewhat"); + if (hedge->name() == Very().name()) return "new " + fl("Very"); + return "new " + fl(hedge->name()); + } + + std::string CppExporter::toString(const Norm* op) const { + if (not op) return "fl::null"; + return "new " + fl(op->className()); + } + + std::string CppExporter::toString(const Defuzzifier* defuzzifier) const { + if (not defuzzifier) return "fl::null"; + if (const IntegralDefuzzifier * integralDefuzzifier = + dynamic_cast<const IntegralDefuzzifier*> (defuzzifier)) { + return "new " + fl(integralDefuzzifier->className()) + "(" + + Op::str(integralDefuzzifier->getResolution()) + ")"; + } + if (const WeightedDefuzzifier * weightedDefuzzifier = + dynamic_cast<const WeightedDefuzzifier*> (defuzzifier)) { + return "new " + weightedDefuzzifier->className() + + "(\"" + weightedDefuzzifier->getTypeName() + "\")"; + } + return "new " + fl(defuzzifier->className()); + } + + std::string CppExporter::toString(const Activation* activation) const { + if (not activation) return "fl::null"; + std::string parameters = Op::trim(activation->parameters()); + if (parameters.empty()) return "new " + fl(activation->className()); + + std::vector<std::string> values = Op::split(parameters, " "); + for (std::size_t i = 0; i < values.size(); ++i) { + std::string parameter = values.at(i); + values.at(i) = (Op::isNumeric(parameter) ? parameter : ("\"" + parameter + "\"")); + } + return "new " + fl(activation->className()) + "(" + Op::join(values, ", ") + ")"; + } + + CppExporter* CppExporter::clone() const { + return new CppExporter(*this); + } + +} diff --git a/fuzzylite/src/imex/Exporter.cpp b/fuzzylite/src/imex/Exporter.cpp new file mode 100644 index 0000000..1aaf24e --- /dev/null +++ b/fuzzylite/src/imex/Exporter.cpp @@ -0,0 +1,37 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/imex/Exporter.h" +#include "fl/Exception.h" + +#include <fstream> + +namespace fl { + + Exporter::Exporter() { } + + Exporter::~Exporter() { } + + void Exporter::toFile(const std::string& path, const Engine* engine) const { + std::ofstream writer(path.c_str()); + if (not writer.is_open()) { + throw Exception("[file error] file <" + path + "> could not be created", FL_AT); + } + writer << toString(engine) << std::endl; + writer.close(); + } + +} diff --git a/fuzzylite/src/imex/FclExporter.cpp b/fuzzylite/src/imex/FclExporter.cpp new file mode 100644 index 0000000..98f1177 --- /dev/null +++ b/fuzzylite/src/imex/FclExporter.cpp @@ -0,0 +1,200 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/imex/FclExporter.h" + +#include "fl/Headers.h" + +namespace fl { + + FclExporter::FclExporter(const std::string& indent) : Exporter(), _indent(indent) { } + + FclExporter::~FclExporter() { } + + void FclExporter::setIndent(const std::string& indent) { + this->_indent = indent; + } + + std::string FclExporter::getIndent() const { + return this->_indent; + } + + std::string FclExporter::name() const { + return "FclExporter"; + } + + std::string FclExporter::toString(const Engine* engine) const { + std::ostringstream fcl; + fcl << "//Code automatically generated with " << fuzzylite::library() << ".\n\n"; + fcl << "FUNCTION_BLOCK " << engine->getName() << "\n\n"; + + fcl << "VAR_INPUT\n"; + for (std::size_t i = 0; i < engine->numberOfInputVariables(); ++i) { + fcl << _indent << Op::validName(engine->getInputVariable(i)->getName()) << ": REAL;\n"; + } + fcl << "END_VAR\n\n"; + + fcl << "VAR_OUTPUT\n"; + for (std::size_t i = 0; i < engine->numberOfOutputVariables(); ++i) { + fcl << _indent << Op::validName(engine->getOutputVariable(i)->getName()) << ": REAL;\n"; + } + fcl << "END_VAR\n\n"; + + for (std::size_t i = 0; i < engine->numberOfInputVariables(); ++i) { + InputVariable* inputVariable = engine->getInputVariable(i); + fcl << toString(inputVariable) << "\n"; + } + + for (std::size_t i = 0; i < engine->numberOfOutputVariables(); ++i) { + OutputVariable* outputVariable = engine->getOutputVariable(i); + fcl << toString(outputVariable) << "\n"; + } + + for (std::size_t i = 0; i < engine->numberOfRuleBlocks(); ++i) { + RuleBlock* ruleblock = engine->getRuleBlock(i); + fcl << toString(ruleblock) << "\n"; + } + + fcl << "END_FUNCTION_BLOCK\n"; + return fcl.str(); + } + + std::string FclExporter::toString(const InputVariable* inputVariable) const { + std::ostringstream fcl; + fcl << "FUZZIFY " << Op::validName(inputVariable->getName()) << "\n"; + fcl << _indent << "RANGE := (" << Op::join(2, " .. ", + inputVariable->getMinimum(), inputVariable->getMaximum()) + << ");\n"; + + for (std::size_t t = 0; t < inputVariable->numberOfTerms(); ++t) { + Term* term = inputVariable->getTerm(t); + fcl << _indent << "TERM " << Op::validName(term->getName()) << " := " << toString(term) + << ";\n"; + } + fcl << "END_FUZZIFY\n"; + return fcl.str(); + } + + std::string FclExporter::toString(const OutputVariable* outputVariable) const { + std::ostringstream fcl; + fcl << "DEFUZZIFY " << Op::validName(outputVariable->getName()) << "\n"; + fcl << _indent << "RANGE := (" << Op::join(2, " .. ", + outputVariable->getMinimum(), outputVariable->getMaximum()) + << ");\n"; + + for (std::size_t t = 0; t < outputVariable->numberOfTerms(); ++t) { + Term* term = outputVariable->getTerm(t); + fcl << _indent << "TERM " << Op::validName(term->getName()) << " := " << toString(term) + << ";\n"; + } + if (outputVariable->getDefuzzifier()) { + fcl << _indent << "METHOD : " << toString(outputVariable->getDefuzzifier()) << ";\n"; + } + if (outputVariable->fuzzyOutput()->getAggregation()) + fcl << _indent << "ACCU : " << toString(outputVariable->fuzzyOutput()->getAggregation()) << ";\n"; + + fcl << _indent << "DEFAULT := " << Op::str(outputVariable->getDefaultValue()); + if (outputVariable->isLockPreviousValue()) { + fcl << " | NC"; + } + fcl << ";\n"; + + fcl << "END_DEFUZZIFY\n"; + return fcl.str(); + } + + std::string FclExporter::toString(const RuleBlock* ruleBlock) const { + std::ostringstream fcl; + fcl << "RULEBLOCK " << ruleBlock->getName() << "\n"; + if (ruleBlock->getConjunction()) + fcl << _indent << "AND : " << toString(ruleBlock->getConjunction()) << ";\n"; + if (ruleBlock->getDisjunction()) + fcl << _indent << "OR : " << toString(ruleBlock->getDisjunction()) << ";\n"; + if (ruleBlock->getImplication()) + fcl << _indent << "ACT : " << toString(ruleBlock->getImplication()) << ";\n"; + + for (std::size_t r = 0; r < ruleBlock->numberOfRules(); ++r) { + fcl << _indent << "RULE " << (r + 1) << " : " << + ruleBlock->getRule(r)->getText() << "\n"; + } + fcl << "END_RULEBLOCK\n"; + return fcl.str(); + } + + std::string FclExporter::toString(const Norm* norm) const { + if (not norm) return "NONE"; + + std::string name = norm->className(); + //TNorms + if (name == Minimum().className()) return "MIN"; + if (name == AlgebraicProduct().className()) return "PROD"; + if (name == BoundedDifference().className()) return "BDIF"; + if (name == DrasticProduct().className()) return "DPROD"; + if (name == EinsteinProduct().className()) return "EPROD"; + if (name == HamacherProduct().className()) return "HPROD"; + if (name == NilpotentMinimum().className()) return "NMIN"; + + //SNorms + if (name == Maximum().className()) return "MAX"; + if (name == AlgebraicSum().className()) return "ASUM"; + if (name == NormalizedSum().className()) return "NSUM"; + if (name == BoundedSum().className()) return "BSUM"; + if (name == DrasticSum().className()) return "DSUM"; + if (name == EinsteinSum().className()) return "ESUM"; + if (name == HamacherSum().className()) return "HSUM"; + if (name == NilpotentMaximum().className()) return "NMAX"; + + return norm->className(); + } + + std::string FclExporter::toString(const Defuzzifier* defuzzifier) const { + if (not defuzzifier) return "NONE"; + if (defuzzifier->className() == Centroid().className()) return "COG"; + if (defuzzifier->className() == Bisector().className()) return "COA"; + if (defuzzifier->className() == SmallestOfMaximum().className()) return "LM"; + if (defuzzifier->className() == LargestOfMaximum().className()) return "RM"; + if (defuzzifier->className() == MeanOfMaximum().className()) return "MM"; + if (defuzzifier->className() == WeightedAverage().className()) return "COGS"; + if (defuzzifier->className() == WeightedSum().className()) return "COGSS"; + return defuzzifier->className(); + } + + std::string FclExporter::toString(const Term* term) const { + if (not term) return ""; + if (const Discrete * discrete = dynamic_cast<const Discrete*> (term)) { + std::ostringstream ss; + for (std::size_t i = 0; i < discrete->xy().size(); ++i) { + ss << "(" << Op::str(discrete->xy(i).first) << ", " + << Op::str(discrete->xy(i).second) << ")"; + if (i + 1 < discrete->xy().size()) ss << " "; + } + return ss.str(); + } + + if (const Constant * constant = dynamic_cast<const Constant*> (term)) { + return Op::str(constant->getValue()); + } + + std::ostringstream ss; + ss << term->className() << " " << term->parameters(); + return ss.str(); + } + + FclExporter* FclExporter::clone() const { + return new FclExporter(*this); + } + +} diff --git a/fuzzylite/src/imex/FclImporter.cpp b/fuzzylite/src/imex/FclImporter.cpp new file mode 100644 index 0000000..06879cd --- /dev/null +++ b/fuzzylite/src/imex/FclImporter.cpp @@ -0,0 +1,587 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/imex/FclImporter.h" + +#include "fl/Headers.h" + +namespace fl { + + FclImporter::FclImporter() : Importer() { } + + FclImporter::~FclImporter() { } + + std::string FclImporter::name() const { + return "FclImporter"; + } + + Engine* FclImporter::fromString(const std::string& fcl) const { + FL_unique_ptr<Engine> engine(new Engine); + + std::map<std::string, std::string> tags; + tags["VAR_INPUT"] = "END_VAR"; + tags["VAR_OUTPUT"] = "END_VAR"; + tags["FUZZIFY"] = "END_FUZZIFY"; + tags["DEFUZZIFY"] = "END_DEFUZZIFY"; + tags["RULEBLOCK"] = "END_RULEBLOCK"; + std::map<std::string, std::string>::const_iterator tagFinder; + + std::string currentTag = "", closingTag = ""; + std::ostringstream block; + std::istringstream fclReader(fcl); + std::string line; + + std::size_t lineNumber = 0; + while (std::getline(fclReader, line)) { + ++lineNumber; + line = Op::split(line, "//", false).front(); + line = Op::split(line, "#", false).front(); + line = Op::trim(Op::findReplace(line, ";", "")); + if (line.empty() or line.at(0) == '%') { + continue; + } + + std::istringstream tokenizer(line); + std::string firstToken; + tokenizer >> firstToken; + + if (firstToken == "FUNCTION_BLOCK") { + if (tokenizer.rdbuf()->in_avail() > 0) { + std::ostringstream name; + std::string token; + tokenizer >> token; + name << token; + while (tokenizer >> token) { + name << " " << token; + } + engine->setName(name.str()); + } + continue; + } + if (firstToken == "END_FUNCTION_BLOCK") { + break; + } + + if (currentTag.empty()) { + tagFinder = tags.find(firstToken); + if (tagFinder == tags.end()) { + std::ostringstream ex; + ex << "[syntax error] unknown block definition <" << firstToken + << "> " << " in line " << lineNumber << ": " << line; + throw Exception(ex.str(), FL_AT); + } + currentTag = tagFinder->first; + closingTag = tagFinder->second; + block.str(""); + block.clear(); + block << line << "\n"; + continue; + } + + if (not currentTag.empty()) { + if (firstToken == closingTag) { + processBlock(currentTag, block.str(), engine.get()); + currentTag = ""; + closingTag = ""; + } else if (tags.find(firstToken) != tags.end()) { + //if opening new block without closing the previous one + std::ostringstream ex; + ex << "[syntax error] expected <" << closingTag << "> before <" + << firstToken << "> in line: " << line; + throw Exception(ex.str(), FL_AT); + } else { + block << line << "\n"; + } + continue; + } + } + + if (not currentTag.empty()) { + std::ostringstream ex; + ex << "[syntax error] "; + if (block.rdbuf()->in_avail() > 0) { + ex << "expected <" << closingTag << "> for block:\n" << block.str(); + } else { + ex << "expected <" << closingTag << ">, but not found"; + } + throw Exception(ex.str(), FL_AT); + } + return engine.release(); + } + + void FclImporter::processBlock(const std::string& tag, const std::string& block, Engine* engine) const { + if (tag == "VAR_INPUT" or tag == "VAR_OUTPUT") { + processVar(tag, block, engine); + } else if (tag == "FUZZIFY") { + processFuzzify(block, engine); + } else if (tag == "DEFUZZIFY") { + processDefuzzify(block, engine); + } else if (tag == "RULEBLOCK") { + processRuleBlock(block, engine); + } else { + std::ostringstream ex; + ex << "[syntax error] unexpected tag <" << tag << "> for block:\n" << block; + throw Exception(ex.str(), FL_AT); + } + } + + void FclImporter::processVar(const std::string& tag, const std::string& block, Engine* engine)const { + std::istringstream blockReader(block); + std::string line; + + std::getline(blockReader, line); //discard first line as it is VAR_INPUT + while (std::getline(blockReader, line)) { + std::vector<std::string> token = Op::split(line, ":"); + if (token.size() != 2) { + std::ostringstream ex; + ex << "[syntax error] expected property of type (key : value) in line: " << line; + throw Exception(ex.str(), FL_AT); + } + std::string name = Op::validName(token.at(0)); + if (tag == "VAR_INPUT") + engine->addInputVariable(new InputVariable(name)); + else if (tag == "VAR_OUTPUT") + engine->addOutputVariable(new OutputVariable(name)); + else { + std::ostringstream ex; + ex << "[syntax error] unexpected tag <" << tag << "> in line: " << line; + throw Exception(ex.str(), FL_AT); + } + } + } + + void FclImporter::processFuzzify(const std::string& block, Engine* engine)const { + std::istringstream blockReader(block); + std::string line; + + std::getline(blockReader, line); + std::string name; + std::size_t index = line.find_first_of(' '); + if (index != std::string::npos) { + name = Op::validName(line.substr(index + 1)); + } else { + std::ostringstream ex; + ex << "[syntax error] expected name of input variable in line: " << line; + throw Exception(ex.str(), FL_AT); + } + if (not engine->hasInputVariable(name)) { + std::ostringstream ex; + ex << "[syntax error] engine does not contain " + "input variable <" << name << "> from line: " << line; + throw Exception(ex.str(), FL_AT); + } + + InputVariable* inputVariable = engine->getInputVariable(name); + while (std::getline(blockReader, line)) { + std::istringstream ss(line); + std::string firstToken; + ss >> firstToken; + try { + if (firstToken == "RANGE") { + std::pair<scalar, scalar> minmax = parseRange(line); + inputVariable->setMinimum(minmax.first); + inputVariable->setMaximum(minmax.second); + } else if (firstToken == "ENABLED") { + inputVariable->setEnabled(parseEnabled(line)); + } else if (firstToken == "TERM") { + inputVariable->addTerm(parseTerm(line, engine)); + } else throw Exception("[syntax error] unexpected token " + "<" + firstToken + ">" + line, FL_AT); + } catch (Exception& ex) { + ex.append("At line: <" + line + ">"); + throw; + } + } + + } + + void FclImporter::processDefuzzify(const std::string& block, Engine* engine) const { + std::istringstream blockReader(block); + std::string line; + + std::getline(blockReader, line); + std::string name; + std::size_t index = line.find_first_of(' '); + if (index != std::string::npos) { + name = Op::validName(line.substr(index + 1)); + } else { + std::ostringstream ex; + ex << "[syntax error] expected an output variable name in line: " << line; + throw Exception(ex.str(), FL_AT); + } + if (not engine->hasOutputVariable(name)) { + std::ostringstream ex; + ex << "[syntax error] output variable <" << name + << "> not registered in engine. " + << "Line: " << line; + throw Exception(ex.str(), FL_AT); + } + + OutputVariable* outputVariable = engine->getOutputVariable(name); + while (std::getline(blockReader, line)) { + line = Op::trim(line); + std::istringstream tokenizer(line); + std::string firstToken; + tokenizer >> firstToken; + if (firstToken == "TERM") { + outputVariable->addTerm(parseTerm(line, engine)); + } else if (firstToken == "METHOD") { + outputVariable->setDefuzzifier(parseDefuzzifier(line)); + } else if (firstToken == "ACCU") { + outputVariable->fuzzyOutput()->setAggregation(parseSNorm(line)); + } else if (firstToken == "DEFAULT") { + std::pair<scalar, bool> defaultAndLock = parseDefaultValue(line); + outputVariable->setDefaultValue(defaultAndLock.first); + outputVariable->setLockPreviousValue(defaultAndLock.second or + outputVariable->isLockPreviousValue()); + } else if (firstToken == "RANGE") { + std::pair<scalar, scalar> minmax = parseRange(line); + outputVariable->setMinimum(minmax.first); + outputVariable->setMaximum(minmax.second); + } else if (firstToken == "LOCK") { + std::pair<bool, bool> output_range = parseLocks(line); + outputVariable->setLockPreviousValue(output_range.first); + outputVariable->setLockValueInRange(output_range.second); + } else if (firstToken == "ENABLED") { + outputVariable->setEnabled(parseEnabled(line)); + } else { + std::ostringstream ex; + ex << "[syntax error] unexpected token <" << firstToken << + "> in line: " << line; + throw Exception(ex.str(), FL_AT); + } + } + + } + + void FclImporter::processRuleBlock(const std::string& block, Engine* engine)const { + std::istringstream blockReader(block); + std::string line; + + std::string name; + std::getline(blockReader, line); + std::size_t index = line.find_last_of(' '); + if (index != std::string::npos) name = line.substr(index + 1); + RuleBlock * ruleblock = new RuleBlock(name); + ruleblock->setActivation(new General); + engine->addRuleBlock(ruleblock); + + while (std::getline(blockReader, line)) { + std::string firstToken = line.substr(0, line.find_first_of(' ')); + if (firstToken == "AND") { + ruleblock->setConjunction(parseTNorm(line)); + } else if (firstToken == "OR") { + ruleblock->setDisjunction(parseSNorm(line)); + } else if (firstToken == "ACT") { + ruleblock->setImplication(parseTNorm(line)); + } else if (firstToken == "ENABLED") { + ruleblock->setEnabled(parseEnabled(line)); + } else if (firstToken == "RULE") { + std::size_t ruleStart = line.find_first_of(':'); + if (ruleStart == std::string::npos) ruleStart = 4; // "RULE".size() + std::string ruleText = line.substr(ruleStart + 1); + ruleText = Op::trim(ruleText); + Rule* rule = new Rule(ruleText); + try { + rule->load(engine); + } catch (...) { + //ignore + } + ruleblock->addRule(rule); + } else { + std::ostringstream ex; + ex << "[syntax error] keyword <" << firstToken + << "> not recognized in line: " << line; + throw Exception(ex.str(), FL_AT); + } + } + } + + TNorm* FclImporter::parseTNorm(const std::string& line) const { + std::vector<std::string> token = Op::split(line, ":"); + if (token.size() != 2) { + std::ostringstream ex; + ex << "[syntax error] expected property of type (key : value) in line: " + << line; + throw Exception(ex.str(), FL_AT); + } + std::string name = Op::trim(token.at(1)); + std::string className = name; + if (name == "NONE") className = ""; + else if (name == "MIN") className = Minimum().className(); + else if (name == "PROD") className = AlgebraicProduct().className(); + else if (name == "BDIF") className = BoundedDifference().className(); + else if (name == "DPROD") className = DrasticProduct().className(); + else if (name == "EPROD") className = EinsteinProduct().className(); + else if (name == "HPROD") className = HamacherProduct().className(); + else if (name == "NMIN") className = NilpotentMinimum().className(); + + return FactoryManager::instance()->tnorm()->constructObject(className); + } + + SNorm* FclImporter::parseSNorm(const std::string& line) const { + std::vector<std::string> token = Op::split(line, ":"); + if (token.size() != 2) { + std::ostringstream ex; + ex << "[syntax error] expected property of type (key : value) in line: " + << line; + throw Exception(ex.str(), FL_AT); + } + std::string name = Op::trim(token.at(1)); + std::string className = name; + if (name == "NONE") className = ""; + else if (name == "MAX") className = Maximum().className(); + else if (name == "ASUM") className = AlgebraicSum().className(); + else if (name == "BSUM") className = BoundedSum().className(); + else if (name == "NSUM") className = NormalizedSum().className(); + else if (name == "DSUM") className = DrasticSum().className(); + else if (name == "ESUM") className = EinsteinSum().className(); + else if (name == "HSUM") className = HamacherSum().className(); + else if (name == "NMAX") className = NilpotentMaximum().className(); + + return FactoryManager::instance()->snorm()->constructObject(className); + } + + Term* FclImporter::parseTerm(const std::string& line, const Engine* engine) const { + std::ostringstream spacer; + for (std::size_t i = 0; i < line.size(); ++i) { + if (line.at(i) == '(' or line.at(i) == ')' or line.at(i) == ',') { + spacer << " " << line.at(i) << " "; + } else if (line.at(i) == ':') { + spacer << " :"; + } else if (line.at(i) == '=') { + spacer << "= "; + } else + spacer << line.at(i); + } + std::string spacedLine = spacer.str(); + + enum FSM { + S_KWTERM, S_NAME, S_ASSIGN, S_TERMCLASS, S_PARAMETERS + }; + FSM state = S_KWTERM; + std::istringstream tokenizer(spacedLine); + std::string token; + std::string name, termClass; + std::vector<std::string> parameters; + while (tokenizer >> token) { + if (state == S_KWTERM and token == "TERM") { + state = S_NAME; + continue; + } + if (state == S_NAME) { + name = token; + state = S_ASSIGN; + continue; + } + if (state == S_ASSIGN and token == ":=") { + state = S_TERMCLASS; + continue; + } + if (state == S_TERMCLASS) { + if (Op::isNumeric(token)) { + termClass = Constant().className(); + parameters.push_back(token); + } else if (token == "(") { + termClass = Discrete().className(); + } else { + termClass = token; + } + state = S_PARAMETERS; + continue; + } + if (state == S_PARAMETERS) { + if (termClass != Function().className() and + (token == "(" or token == ")" or token == ",")) { + continue; + } + if (token == ";") break; + parameters.push_back(Op::trim(token)); + } + } + if (state <= S_TERMCLASS) + throw Exception("[syntax error] malformed term in line: " + line, FL_AT); + + FL_unique_ptr<Term> term; + term.reset(FactoryManager::instance()->term()->constructObject(termClass)); + term->updateReference(engine); + term->setName(Op::validName(name)); + std::string separator; + if (not dynamic_cast<Function*> (term.get())) { + separator = " "; + } + term->configure(Op::join(parameters, separator)); //remove spaces for text of function + return term.release(); + } + + Defuzzifier* FclImporter::parseDefuzzifier(const std::string& line) const { + std::vector<std::string> token = Op::split(line, ":"); + if (token.size() != 2) { + std::ostringstream ex; + ex << "[syntax error] expected property of type (key : value) in " + << "line: " << line; + throw Exception(ex.str(), FL_AT); + } + + std::string name = Op::trim(token.at(1)); + std::string className = name; + if (name == "NONE") className = ""; + else if (name == "COG") className = Centroid().className(); + else if (name == "COA") className = Bisector().className(); + else if (name == "LM") className = SmallestOfMaximum().className(); + else if (name == "RM") className = LargestOfMaximum().className(); + else if (name == "MM") className = MeanOfMaximum().className(); + else if (name == "COGS") className = WeightedAverage().className(); + else if (name == "COGSS") className = WeightedSum().className(); + + return FactoryManager::instance()->defuzzifier()->constructObject(className); + } + + std::pair<scalar, bool> FclImporter::parseDefaultValue(const std::string& line) const { + std::vector<std::string> token = Op::split(line, ":="); + if (token.size() != 2) { + std::ostringstream ex; + ex << "[syntax error] expected property of type (key := value) in line: " + << line; + throw Exception(ex.str(), FL_AT); + } + + std::vector<std::string> values = Op::split(token.at(1), "|"); + + std::string defaultValue = values.front(); + std::string nc; + if (values.size() == 2) nc = values.back(); + + defaultValue = Op::trim(defaultValue); + nc = Op::trim(nc); + + scalar value; + try { + value = Op::toScalar(defaultValue); + } catch (...) { + std::ostringstream ex; + ex << "[syntax error] expected numeric value, " + << "but found <" << defaultValue << "> in line: " + << line; + throw Exception(ex.str(), FL_AT); + } + + bool lockPreviousOutput = (nc == "NC"); + + if (not (lockPreviousOutput or nc.empty())) { + throw Exception("[syntax error] expected keyword <NC>, " + "but found <" + nc + "> in line: " + line, FL_AT); + } + + return std::pair<scalar, bool>(value, lockPreviousOutput); + } + + std::pair<scalar, scalar> FclImporter::parseRange(const std::string& line) const { + std::vector<std::string> token = Op::split(line, ":="); + if (token.size() != 2) { + std::ostringstream ex; + ex << "[syntax error] expected property of type (key := value) in line: " + << line; + throw Exception(ex.str(), FL_AT); + } + + std::string rangeToken = token.at(1); + + std::ostringstream range; + for (std::size_t i = 0; i < rangeToken.size(); ++i) { + char character = rangeToken.at(i); + if (character == '(' or character == ')' or character == ' ' or character == ';') + continue; + range << character; + } + token = Op::split(range.str(), ".."); + if (token.size() != 2) { + std::ostringstream ex; + ex << "[syntax error] expected property of type 'start .. end', " + << "but found <" << range.str() << "> in line: " << line; + throw Exception(ex.str(), FL_AT); + } + scalar minimum, maximum; + int index; + try { + minimum = Op::toScalar(token.at(index = 0)); + maximum = Op::toScalar(token.at(index = 1)); + } catch (std::exception& ex) { + FL_IUNUSED(ex); + std::ostringstream ss; + ss << "[syntax error] expected numeric value, but found <" << token.at(index) << "> in " + << "line: " << line; + throw Exception(ss.str(), FL_AT); + } + return std::pair<scalar, scalar>(minimum, maximum); + } + + std::pair<bool, bool> FclImporter::parseLocks(const std::string& line) const { + std::size_t index = line.find_first_of(":"); + if (index == std::string::npos) { + throw Exception("[syntax error] expected property of type " + "'key : value' in line: " + line, FL_AT); + } + bool output, range; + std::string value = line.substr(index + 1); + std::vector<std::string> flags = Op::split(value, "|"); + if (flags.size() == 1) { + std::string flag = Op::trim(flags.front()); + output = (flag == "PREVIOUS"); + range = (flag == "RANGE"); + if (not (output or range)) { + throw Exception("[syntax error] expected locking flags " + "<PREVIOUS|RANGE>, but found <" + flag + "> in line: " + line, FL_AT); + } + } else if (flags.size() == 2) { + std::string flagA = Op::trim(flags.front()); + std::string flagB = Op::trim(flags.back()); + output = (flagA == "PREVIOUS" or flagB == "PREVIOUS"); + range = (flagA == "RANGE" or flagB == "RANGE"); + if (not (output and range)) { + throw Exception("[syntax error] expected locking flags " + "<PREVIOUS|RANGE>, but found " + "<" + flags.front() + "|" + flags.back() + "> in line: " + line, FL_AT); + } + } else { + throw Exception("[syntax error] expected locking flags " + "<PREVIOUS|RANGE>, but found " + "<" + value + "> in line: " + line, FL_AT); + } + return std::pair<bool, bool>(output, range); + } + + bool FclImporter::parseEnabled(const std::string& line) const { + std::vector<std::string> tokens = Op::split(line, ":"); + if (tokens.size() != 2) { + std::ostringstream ex; + ex << "[syntax error] expected property of type (key : value) in " + << "line: " << line; + throw Exception(ex.str(), FL_AT); + } + + std::string boolean = Op::trim(tokens.at(1)); + if (boolean == "TRUE") return true; + if (boolean == "FALSE") return false; + throw Exception("[syntax error] expected boolean <TRUE|FALSE>, but found <" + line + ">", FL_AT); + } + + FclImporter* FclImporter::clone() const { + return new FclImporter(*this); + } + + +} diff --git a/fuzzylite/src/imex/FisExporter.cpp b/fuzzylite/src/imex/FisExporter.cpp new file mode 100644 index 0000000..2daac9a --- /dev/null +++ b/fuzzylite/src/imex/FisExporter.cpp @@ -0,0 +1,423 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/imex/FisExporter.h" + +#include "fl/Headers.h" + +#include <queue> + +namespace fl { + + FisExporter::FisExporter() : Exporter() { } + + FisExporter::~FisExporter() { } + + std::string FisExporter::name() const { + return "FisExporter"; + } + + std::string FisExporter::toString(const Engine* engine) const { + std::ostringstream fis; + fis << exportSystem(engine) << "\n"; + + fis << exportInputs(engine); + + fis << exportOutputs(engine); + + fis << exportRules(engine); + + return fis.str(); + } + + std::string FisExporter::exportSystem(const Engine* engine) const { + std::ostringstream fis; + fis << "#Code automatically generated with " << fuzzylite::library() << ".\n\n"; + fis << "[System]\n"; + fis << "Name='" << engine->getName() << "'\n"; + std::string type; + if (engine->type() == Engine::Mamdani or engine->type() == Engine::Larsen) { + type = "mamdani"; + } else if (engine->type() == Engine::TakagiSugeno) { + type = "sugeno"; + } else if (engine->type() == Engine::Tsukamoto) { + type = "tsukamoto"; + } else if (engine->type() == Engine::InverseTsukamoto) { + type = "inverse tsukamoto"; + } else if (engine->type() == Engine::Hybrid) { + type = "hybrid"; + } else { + type = "unknown"; + } + fis << "Type='" << type << "'\n"; + fis << "Version=" << fuzzylite::version() << "\n"; + fis << "NumInputs=" << engine->numberOfInputVariables() << "\n"; + fis << "NumOutputs=" << engine->numberOfOutputVariables() << "\n"; + + std::size_t numberOfRules = 0; + const TNorm* conjunction = fl::null; + const SNorm* disjunction = fl::null; + const TNorm* implication = fl::null; + for (std::size_t i = 0; i < engine->numberOfRuleBlocks(); ++i) { + RuleBlock* rb = engine->getRuleBlock(i); + numberOfRules += rb->numberOfRules(); + if (not conjunction) conjunction = rb->getConjunction(); + if (not disjunction) disjunction = rb->getDisjunction(); + if (not implication) implication = rb->getImplication(); + } + fis << "NumRules=" << numberOfRules << "\n"; + fis << "AndMethod='" << (conjunction ? toString(conjunction) : "min") << "'\n"; + fis << "OrMethod='" << (disjunction ? toString(disjunction) : "max") << "'\n"; + fis << "ImpMethod='" << (implication ? toString(implication) : "min") << "'\n"; + + const SNorm* aggregation = fl::null; + Defuzzifier* defuzzifier = fl::null; + for (std::size_t i = 0; i < engine->numberOfOutputVariables(); ++i) { + OutputVariable* outputVariable = engine->getOutputVariable(i); + if (not aggregation) aggregation = outputVariable->fuzzyOutput()->getAggregation(); + if (not defuzzifier) defuzzifier = outputVariable->getDefuzzifier(); + } + + fis << "AggMethod='" << (aggregation ? toString(aggregation) : "max") << "'\n"; + fis << "DefuzzMethod='" << toString(defuzzifier) << "'\n"; + return fis.str(); + } + + std::string FisExporter::exportInputs(const Engine* engine) const { + std::ostringstream fis; + for (std::size_t ixVar = 0; ixVar < engine->numberOfInputVariables(); ++ixVar) { + InputVariable* var = engine->getInputVariable(ixVar); + fis << "[Input" << (ixVar + 1) << "]\n"; + fis << "Name='" << Op::validName(var->getName()) << "'\n"; + fis << "Range=[" << Op::join(2, " ", var->getMinimum(), var->getMaximum()) << "]\n"; + fis << "NumMFs=" << var->numberOfTerms() << "\n"; + for (std::size_t ixTerm = 0; ixTerm < var->numberOfTerms(); ++ixTerm) { + fis << "MF" << (ixTerm + 1) << "='" << Op::validName(var->getTerm(ixTerm)->getName()) << "':" + << toString(var->getTerm(ixTerm)) << "\n"; + } + fis << "\n"; + } + return fis.str(); + } + + std::string FisExporter::exportOutputs(const Engine* engine) const { + std::ostringstream fis; + for (std::size_t ixVar = 0; ixVar < engine->numberOfOutputVariables(); ++ixVar) { + OutputVariable* var = engine->getOutputVariable(ixVar); + fis << "[Output" << (ixVar + 1) << "]\n"; + fis << "Name='" << Op::validName(var->getName()) << "'\n"; + fis << "Range=[" << Op::join(2, " ", var->getMinimum(), var->getMaximum()) << "]\n"; + fis << "NumMFs=" << var->numberOfTerms() << "\n"; + for (std::size_t ixTerm = 0; ixTerm < var->numberOfTerms(); ++ixTerm) { + fis << "MF" << (ixTerm + 1) << "='" << Op::validName(var->getTerm(ixTerm)->getName()) << "':" + << toString(var->getTerm(ixTerm)) << "\n"; + } + fis << "\n"; + } + return fis.str(); + } + + std::string FisExporter::exportRules(const Engine* engine) const { + std::ostringstream fis; + fis << "[Rules]\n"; + for (std::size_t ixRuleBlock = 0; ixRuleBlock < engine->numberOfRuleBlocks(); ++ixRuleBlock) { + RuleBlock* rb = engine->getRuleBlock(ixRuleBlock); + if (engine->numberOfRuleBlocks() > 1) fis << "# RuleBlock " << rb->getName() << "\n"; + for (std::size_t ixRule = 0; ixRule < rb->numberOfRules(); ++ixRule) { + Rule* rule = rb->getRule(ixRule); + if (rule->isLoaded()) { + fis << exportRule(rule, engine) << "\n"; + } + } + } + return fis.str(); + } + + std::string FisExporter::exportRule(const Rule* rule, const Engine* engine) const { + if (not rule) return ""; + std::vector<Proposition*> propositions; + std::vector<Operator*> operators; + + std::queue<Expression*> bfsQueue; + bfsQueue.push(rule->getAntecedent()->getExpression()); + while (not bfsQueue.empty()) { + Expression* front = bfsQueue.front(); + bfsQueue.pop(); + Operator* op = dynamic_cast<Operator*> (front); + if (op) { + bfsQueue.push(op->left); + bfsQueue.push(op->right); + operators.push_back(op); + } else { + propositions.push_back(dynamic_cast<Proposition*> (front)); + } + } + + bool equalOperators = true; + for (std::size_t i = 0; i + 1 < operators.size(); ++i) { + if (operators.at(i)->name != operators.at(i + 1)->name) { + equalOperators = false; + break; + } + } + if (not equalOperators) { + throw Exception("[exporter error] " + "fis files do not support rules with different connectors " + "(i.e. ['and', 'or']). All connectors within a rule must be the same", FL_AT); + } + std::ostringstream fis; + std::vector<Variable*> inputVariables, outputVariables; + for (std::size_t i = 0; i < engine->numberOfInputVariables(); ++i) + inputVariables.push_back(engine->getInputVariable(i)); + for (std::size_t i = 0; i < engine->numberOfOutputVariables(); ++i) + outputVariables.push_back(engine->getOutputVariable(i)); + + fis << translate(propositions, inputVariables) << ", "; + fis << translate(rule->getConsequent()->conclusions(), outputVariables); + fis << "(" << Op::str(rule->getWeight()) << ") : "; + if (operators.size() == 0) fis << "1"; //does not matter + else { + if (operators.at(0)->name == Rule::andKeyword()) fis << "1"; + else if (operators.at(0)->name == Rule::orKeyword()) fis << "2"; + else fis << operators.at(0)->name; + } + return fis.str(); + } + + std::string FisExporter::translate(const std::vector<Proposition*>& propositions, + const std::vector<Variable*> variables) const { + std::ostringstream ss; + for (std::size_t ixVariable = 0; ixVariable < variables.size(); ++ixVariable) { + Variable* variable = variables.at(ixVariable); + std::size_t termIndexPlusOne = 0; + scalar plusHedge = 0; + int negated = 1; + for (std::size_t ixProposition = 0; ixProposition < propositions.size(); ++ixProposition) { + Proposition* proposition = propositions.at(ixProposition); + if (proposition->variable != variable) continue; + + for (std::size_t termIndex = 0; termIndex < variable->numberOfTerms(); ++termIndex) { + if (variable->getTerm(termIndex) == proposition->term) { + termIndexPlusOne = termIndex + 1; + break; + } + } + + std::vector<Hedge*> hedges = proposition->hedges; + if (hedges.size() > 1) { + FL_DBG("[exporter warning] only a few combinations of multiple " + "hedges are supported in fis files"); + } + for (std::size_t ixHedge = 0; ixHedge < hedges.size(); ++ixHedge) { + Hedge* hedge = hedges.at(ixHedge); + if (hedge->name() == Not().name()) negated *= -1; + else if (hedge->name() == Extremely().name()) plusHedge += 0.3; + else if (hedge->name() == Very().name()) plusHedge += 0.2; + else if (hedge->name() == Somewhat().name()) plusHedge += 0.05; + else if (hedge->name() == Seldom().name()) plusHedge += 0.01; + else if (hedge->name() == Any().name()) plusHedge += 0.99; + else plusHedge = fl::nan; //Unreconized hedge combination (e.g. Any) + } + + break; + } + if (negated < 0) ss << "-"; + if (not Op::isNaN(plusHedge)) { + ss << Op::str(termIndexPlusOne + plusHedge); + } else { + ss << termIndexPlusOne << ".?"; // Unreconized hedge combination + } + ss << " "; + } + return ss.str(); + } + + std::string FisExporter::toString(const TNorm * tnorm) const { + if (not tnorm) return ""; + if (tnorm->className() == Minimum().className()) return "min"; + if (tnorm->className() == AlgebraicProduct().className()) return "prod"; + if (tnorm->className() == BoundedDifference().className()) return "bounded_difference"; + if (tnorm->className() == DrasticProduct().className()) return "drastic_product"; + if (tnorm->className() == EinsteinProduct().className()) return "einstein_product"; + if (tnorm->className() == HamacherProduct().className()) return "hamacher_product"; + if (tnorm->className() == NilpotentMinimum().className()) return "nilpotent_minimum"; + return tnorm->className(); + } + + std::string FisExporter::toString(const SNorm* snorm) const { + if (not snorm) return ""; + if (snorm->className() == Maximum().className()) return "max"; + if (snorm->className() == AlgebraicSum().className()) return "probor"; + if (snorm->className() == BoundedSum().className()) return "bounded_sum"; + if (snorm->className() == NormalizedSum().className()) return "normalized_sum"; + if (snorm->className() == DrasticSum().className()) return "drastic_sum"; + if (snorm->className() == EinsteinSum().className()) return "einstein_sum"; + if (snorm->className() == HamacherSum().className()) return "hamacher_sum"; + if (snorm->className() == NilpotentMaximum().className()) return "nilpotent_maximum"; + if (snorm->className() == UnboundedSum().className()) return "sum"; + return snorm->className(); + } + + std::string FisExporter::toString(const Defuzzifier * defuzzifier) const { + if (not defuzzifier) return ""; + if (defuzzifier->className() == Centroid().className()) return "centroid"; + if (defuzzifier->className() == Bisector().className()) return "bisector"; + if (defuzzifier->className() == LargestOfMaximum().className()) return "lom"; + if (defuzzifier->className() == MeanOfMaximum().className()) return "mom"; + if (defuzzifier->className() == SmallestOfMaximum().className()) return "som"; + if (defuzzifier->className() == WeightedAverage().className()) return "wtaver"; + if (defuzzifier->className() == WeightedSum().className()) return "wtsum"; + return defuzzifier->className(); + } + + std::string FisExporter::toString(const Term * term) const { + std::ostringstream ss; + if (const Bell * x = dynamic_cast<const Bell*> (term)) { + ss << "'gbellmf',[" << Op::join(3, " ", + x->getWidth(), x->getSlope(), x->getCenter()) << "]"; + return ss.str(); + } + + if (const Binary * x = dynamic_cast<const Binary*> (term)) { + ss << "'binarymf,[" << Op::join(2, " ", + x->getStart(), x->getDirection()) << "]"; + return ss.str(); + } + + if (const Concave * x = dynamic_cast<const Concave*> (term)) { + ss << "'concavemf',[" << Op::join(2, " ", + x->getInflection(), x->getEnd()) << "]"; + return ss.str(); + } + + if (const Constant * x = dynamic_cast<const Constant*> (term)) { + ss << "'constant',[" << Op::str(x->getValue()) << "]"; + return ss.str(); + } + + if (const Cosine * x = dynamic_cast<const Cosine*> (term)) { + ss << "'cosinemf',[" << Op::join(2, " ", + x->getCenter(), x->getWidth()) << "]"; + return ss.str(); + } + + if (const Discrete * x = dynamic_cast<const Discrete*> (term)) { + ss << "'discretemf',[" << Op::join(Discrete::toVector(x->xy()), " ") << "]"; + return ss.str(); + } + + if (const Function * x = dynamic_cast<const Function*> (term)) { + ss << "'function',[" << x->getFormula() << "]"; + return ss.str(); + } + + if (const Gaussian * x = dynamic_cast<const Gaussian*> (term)) { + ss << "'gaussmf',[" << Op::join(2, " ", + x->getStandardDeviation(), x->getMean()) << "]"; + return ss.str(); + } + + if (const GaussianProduct * x = dynamic_cast<const GaussianProduct*> (term)) { + ss << "'gauss2mf',[" << Op::join(4, " ", + x->getStandardDeviationA(), x->getMeanA(), + x->getStandardDeviationB(), x->getMeanB()) << "]"; + return ss.str(); + } + + if (const Linear * x = dynamic_cast<const Linear*> (term)) { + ss << "'linear',[" << Op::join<scalar>(x->coefficients(), " ") << "]"; + return ss.str(); + } + + + if (const PiShape * x = dynamic_cast<const PiShape*> (term)) { + ss << "'pimf',[" << Op::join(4, " ", + x->getBottomLeft(), x->getTopLeft(), + x->getTopRight(), x->getBottomRight()) << "]"; + return ss.str(); + } + + if (const Ramp * x = dynamic_cast<const Ramp*> (term)) { + ss << "'rampmf',[" << Op::join(2, " ", + x->getStart(), x->getEnd()) << "]"; + return ss.str(); + } + + if (const Rectangle * x = dynamic_cast<const Rectangle*> (term)) { + ss << "'rectmf',[" << Op::join(2, " ", + x->getStart(), x->getEnd()) << "]"; + return ss.str(); + } + + if (const SigmoidDifference * x = dynamic_cast<const SigmoidDifference*> (term)) { + ss << "'dsigmf',[" << Op::join(4, " ", + x->getRising(), x->getLeft(), + x->getFalling(), x->getRight()) << "]"; + return ss.str(); + } + + if (const Sigmoid * x = dynamic_cast<const Sigmoid*> (term)) { + ss << "'sigmf',[" << Op::join(2, " ", + x->getSlope(), x->getInflection()) << "]"; + return ss.str(); + } + + if (const SigmoidProduct * x = dynamic_cast<const SigmoidProduct*> (term)) { + ss << "'psigmf',[" << Op::join(4, " ", + x->getRising(), x->getLeft(), + x->getFalling(), x->getRight()) << "]"; + return ss.str(); + } + + if (const SShape * x = dynamic_cast<const SShape*> (term)) { + ss << "'smf',[" << Op::join(2, " ", + x->getStart(), x->getEnd()) << "]"; + return ss.str(); + } + + if (const Spike * x = dynamic_cast<const Spike*> (term)) { + ss << "'spikemf',[" << Op::join(2, " ", + x->getCenter(), x->getWidth()) << "]"; + return ss.str(); + } + + if (const Trapezoid * x = dynamic_cast<const Trapezoid*> (term)) { + ss << "'trapmf',[" << Op::join(4, " ", + x->getVertexA(), x->getVertexB(), x->getVertexC(), x->getVertexD()) << "]"; + return ss.str(); + } + + if (const Triangle * x = dynamic_cast<const Triangle*> (term)) { + ss << "'trimf',[" << Op::join(3, " ", + x->getVertexA(), x->getVertexB(), x->getVertexC()) << "]"; + return ss.str(); + } + + if (const ZShape * x = dynamic_cast<const ZShape*> (term)) { + ss << "'zmf',[" << Op::join(2, " ", + x->getStart(), x->getEnd()) << "]"; + return ss.str(); + } + + ss << "[exporter error] term of class <" << (term ? term->className() : "null") << "> not supported"; + throw Exception(ss.str(), FL_AT); + } + + FisExporter* FisExporter::clone() const { + return new FisExporter(*this); + } + +} diff --git a/fuzzylite/src/imex/FisImporter.cpp b/fuzzylite/src/imex/FisImporter.cpp new file mode 100644 index 0000000..f846469 --- /dev/null +++ b/fuzzylite/src/imex/FisImporter.cpp @@ -0,0 +1,484 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/imex/FisImporter.h" + +#include "fl/Headers.h" + +namespace fl { + + FisImporter::FisImporter() : Importer() { } + + FisImporter::~FisImporter() { } + + std::string FisImporter::name() const { + return "FisImporter"; + } + + Engine* FisImporter::fromString(const std::string& fis) const { + FL_unique_ptr<Engine> engine(new Engine); + + std::istringstream fisReader(fis); + std::string line; + std::size_t lineNumber = 0; + + std::vector<std::string> sections; + while (std::getline(fisReader, line)) { + ++lineNumber; + //remove comments + line = Op::split(line, "//", false).front(); + line = Op::split(line, "#", false).front(); + line = Op::trim(line); + if (line.empty() or line.at(0) == '%') { + continue; + } + + line = Op::findReplace(line, "'", ""); + + if ("[System]" == line.substr(0, std::string("[System]").size()) + or "[Input" == line.substr(0, std::string("[Input").size()) + or "[Output" == line.substr(0, std::string("[Output").size()) + or "[Rules]" == line.substr(0, std::string("[Rules]").size())) { + sections.push_back(line); + } else { + if (not sections.empty()) { + sections.at(sections.size() - 1) += "\n" + line; + } else { + std::ostringstream ss; + ss << "[import error] line " << lineNumber << " <" << line << "> " + "does not belong to any section"; + throw Exception(ss.str(), FL_AT); + } + } + } + std::string andMethod, orMethod, impMethod, aggMethod, defuzzMethod; + for (std::size_t i = 0; i < sections.size(); ++i) { + if ("[System]" == sections.at(i).substr(0, std::string("[System]").size())) + importSystem(sections.at(i), engine.get(), + andMethod, orMethod, impMethod, aggMethod, defuzzMethod); + else if ("[Input" == sections.at(i).substr(0, std::string("[Input").size())) + importInput(sections.at(i), engine.get()); + else if ("[Output" == sections.at(i).substr(0, std::string("[Output").size())) + importOutput(sections.at(i), engine.get()); + else if ("[Rules]" == sections.at(i).substr(0, std::string("[Rules]").size())) + importRules(sections.at(i), engine.get()); + else throw Exception("[import error] section <" + + sections.at(i) + "> not recognized", FL_AT); + } + engine->configure(translateTNorm(andMethod), translateSNorm(orMethod), + translateTNorm(impMethod), translateSNorm(aggMethod), + translateDefuzzifier(defuzzMethod), General().className()); + return engine.release(); + } + + void FisImporter::importSystem(const std::string& section, Engine * engine, + std::string& andMethod, std::string& orMethod, + std::string& impMethod, std::string& aggMethod, + std::string& defuzzMethod) const { + std::istringstream reader(section); + std::string line; + std::getline(reader, line); //ignore first line [System] + while (std::getline(reader, line)) { + std::vector<std::string> keyValue = Op::split(line, "="); + + std::string key = Op::trim(keyValue.at(0)); + std::string value; + for (std::size_t i = 1; i < keyValue.size(); ++i) { + value += keyValue.at(i); + } + value = Op::trim(value); + if (key == "Name") engine->setName(value); + else if (key == "AndMethod") andMethod = value; + else if (key == "OrMethod") orMethod = value; + else if (key == "ImpMethod") impMethod = value; + else if (key == "AggMethod") aggMethod = value; + else if (key == "DefuzzMethod") defuzzMethod = value; + else if (key == "Type" or key == "Version" + or key == "NumInputs" or key == "NumOutputs" + or key == "NumRules" or key == "NumMFs") { + //ignore because are redundant. + } else throw Exception("[import error] token <" + key + "> not recognized", FL_AT); + } + } + + void FisImporter::importInput(const std::string& section, Engine* engine) const { + std::istringstream reader(section); + std::string line; + std::getline(reader, line); //ignore first line [Input#] + + InputVariable* input = new InputVariable; + engine->addInputVariable(input); + + while (std::getline(reader, line)) { + std::vector<std::string> keyValue = Op::split(line, "="); + if (keyValue.size() != 2) + throw Exception("[syntax error] expected a property of type " + "'key=value', but found <" + line + ">", FL_AT); + std::string key = Op::trim(keyValue.at(0)); + std::string value = Op::trim(keyValue.at(1)); + + if (key == "Name") { + input->setName(Op::validName(value)); + } else if (key == "Enabled") { + input->setEnabled(Op::isEq(Op::toScalar(value), 1.0)); + } else if (key == "Range") { + std::pair<scalar, scalar> minmax = parseRange(value); + input->setMinimum(minmax.first); + input->setMaximum(minmax.second); + } else if (key.substr(0, 2) == "MF") { + input->addTerm(parseTerm(value, engine)); + } else if (key == "NumMFs") { + //ignore + } else { + throw Exception("[import error] token <" + key + "> not recognized", FL_AT); + } + } + } + + void FisImporter::importOutput(const std::string& section, Engine* engine) const { + std::istringstream reader(section); + std::string line; + std::getline(reader, line); //ignore first line [Output#] + + OutputVariable* output = new OutputVariable; + engine->addOutputVariable(output); + + + while (std::getline(reader, line)) { + std::vector<std::string> keyValue = Op::split(line, "="); + if (keyValue.size() != 2) + throw Exception("[syntax error] expected a property of type " + "'key=value', but found < " + line + ">", FL_AT); + std::string key = Op::trim(keyValue.at(0)); + std::string value = Op::trim(keyValue.at(1)); + + if (key == "Name") { + output->setName(Op::validName(value)); + } else if (key == "Enabled") { + output->setEnabled(Op::isEq(Op::toScalar(value), 1.0)); + } else if (key == "Range") { + std::pair<scalar, scalar> minmax = parseRange(value); + output->setMinimum(minmax.first); + output->setMaximum(minmax.second); + } else if (key.substr(0, 2) == "MF") { + output->addTerm(parseTerm(value, engine)); + } else if (key == "Default") { + output->setDefaultValue(Op::toScalar(value)); + } else if (key == "LockPrevious") { + output->setLockPreviousValue(Op::isEq(Op::toScalar(value), 1.0)); + } else if (key == "LockRange") { + output->setLockValueInRange(Op::isEq(Op::toScalar(value), 1.0)); + } else if (key == "NumMFs") { + //ignore + } else { + throw Exception("[import error] token <" + key + "> not recognized", FL_AT); + } + } + } + + void FisImporter::importRules(const std::string& section, Engine* engine) const { + std::istringstream reader(section); + std::string line; + std::getline(reader, line); //ignore first line [Rules] + + RuleBlock* ruleblock = new RuleBlock; + engine->addRuleBlock(ruleblock); + + while (std::getline(reader, line)) { + std::vector<std::string> inputsAndRest = Op::split(line, ","); + if (inputsAndRest.size() != 2) + throw Exception("[syntax error] expected rule to match pattern " + "<'i '+, 'o '+ (w) : '1|2'>, but found instead <" + line + ">", FL_AT); + + std::vector <std::string> outputsAndRest = Op::split(inputsAndRest.at(1), ":"); + if (outputsAndRest.size() != 2) + throw Exception("[syntax error] expected rule to match pattern " + "<'i '+, 'o '+ (w) : '1|2'>, but found instead <" + line + ">", FL_AT); + + std::vector<std::string> inputs = Op::split(inputsAndRest.at(0), " "); + std::vector<std::string> outputs = Op::split(outputsAndRest.at(0), " "); + std::string weightInParenthesis = outputs.at(outputs.size() - 1); + outputs.erase(outputs.begin() + outputs.size() - 1); + std::string connector = Op::trim(outputsAndRest.at(1)); + + if (inputs.size() != engine->numberOfInputVariables()) { + std::ostringstream ss; + ss << "[syntax error] expected <" << engine->numberOfInputVariables() << ">" + " input variables, but found <" << inputs.size() << ">" + " input variables in rule <" << line << ">"; + throw Exception(ss.str(), FL_AT); + } + if (outputs.size() != engine->numberOfOutputVariables()) { + std::ostringstream ss; + ss << "[syntax error] expected <" << engine->numberOfOutputVariables() << ">" + " output variables, but found <" << outputs.size() << ">" + " output variables in rule <" << line << ">"; + throw Exception(ss.str(), FL_AT); + } + + std::vector<std::string> antecedent, consequent; + + for (std::size_t i = 0; i < inputs.size(); ++i) { + scalar inputCode = Op::toScalar(inputs.at(i)); + if (Op::isEq(inputCode, 0.0)) continue; + std::ostringstream ss; + ss << engine->getInputVariable(i)->getName() << " " + << Rule::isKeyword() << " " + << translateProposition(inputCode, engine->getInputVariable(i)); + antecedent.push_back(ss.str()); + } + + for (std::size_t i = 0; i < outputs.size(); ++i) { + scalar outputCode = Op::toScalar(outputs.at(i)); + if (Op::isEq(outputCode, 0.0)) continue; + std::ostringstream ss; + ss << engine->getOutputVariable(i)->getName() << " " + << Rule::isKeyword() << " " + << translateProposition(outputCode, engine->getOutputVariable(i)); + consequent.push_back(ss.str()); + } + + std::ostringstream ruleText; + + ruleText << Rule::ifKeyword() << " "; + for (std::size_t i = 0; i < antecedent.size(); ++i) { + ruleText << antecedent.at(i); + if (i + 1 < antecedent.size()) { + ruleText << " "; + if (connector == "1") ruleText << Rule::andKeyword() << " "; + else if (connector == "2") ruleText << Rule::orKeyword() << " "; + else throw Exception("[syntax error] connector <" + + connector + "> not recognized", FL_AT); + } + } + + ruleText << " " << Rule::thenKeyword() << " "; + for (std::size_t i = 0; i < consequent.size(); ++i) { + ruleText << consequent.at(i); + if (i + 1 < consequent.size()) { + ruleText << " " << Rule::andKeyword() << " "; + } + } + + std::ostringstream ss; + for (std::size_t i = 0; i < weightInParenthesis.size(); ++i) { + if (weightInParenthesis.at(i) == '(' + or weightInParenthesis.at(i) == ')' + or weightInParenthesis.at(i) == ' ') continue; + ss << weightInParenthesis.at(i); + } + + scalar weight = Op::toScalar(ss.str()); + if (not Op::isEq(weight, 1.0)) + ruleText << " " << Rule::withKeyword() << " " << Op::str(weight); + Rule* rule = new Rule(ruleText.str()); + try { + rule->load(engine); + } catch (...) { + //ignore + } + ruleblock->addRule(rule); + } + } + + std::string FisImporter::translateProposition(scalar code, Variable* variable) const { + int intPart = (int) std::floor(std::abs(code)) - 1; + scalar fracPart = std::fmod(std::abs(code), scalar(1.0)); + if (intPart >= static_cast<int> (variable->numberOfTerms())) { + std::ostringstream ex; + ex << "[syntax error] the code <" << code << "> refers to a term " + "out of range from variable <" << variable->getName() << ">"; + throw Exception(ex.str(), FL_AT); + } + + bool isAny = intPart < 0; + std::ostringstream ss; + if (code < 0) ss << Not().name() << " "; + if (Op::isEq(fracPart, 0.01)) ss << Seldom().name() << " "; + else if (Op::isEq(fracPart, 0.05)) ss << Somewhat().name() << " "; + else if (Op::isEq(fracPart, 0.2)) ss << Very().name() << " "; + else if (Op::isEq(fracPart, 0.3)) ss << Extremely().name() << " "; + else if (Op::isEq(fracPart, 0.4)) ss << Very().name() << " " << Very().name() << " "; + else if (Op::isEq(fracPart, 0.99)) ss << Any().name() << " "; + else if (not Op::isEq(fracPart, 0.0)) + throw Exception("[syntax error] no hedge defined in FIS format for <" + + Op::str(fracPart) + ">", FL_AT); + if (not isAny) { + ss << variable->getTerm(intPart)->getName(); + } + return ss.str(); + } + + std::string FisImporter::translateTNorm(const std::string& name) const { + if (name.empty()) return ""; + if (name == "min") return Minimum().className(); + if (name == "prod") return AlgebraicProduct().className(); + if (name == "bounded_difference") return BoundedDifference().className(); + if (name == "drastic_product") return DrasticProduct().className(); + if (name == "einstein_product") return EinsteinProduct().className(); + if (name == "hamacher_product") return HamacherProduct().className(); + if (name == "nilpotent_minimum") return NilpotentMinimum().className(); + return name; + } + + std::string FisImporter::translateSNorm(const std::string& name) const { + if (name.empty()) return ""; + if (name == "max") return Maximum().className(); + if (name == "probor") return AlgebraicSum().className(); + if (name == "bounded_sum") return BoundedSum().className(); + if (name == "normalized_sum") return NormalizedSum().className(); + if (name == "drastic_sum") return DrasticSum().className(); + if (name == "einstein_sum") return EinsteinSum().className(); + if (name == "hamacher_sum") return HamacherSum().className(); + if (name == "nilpotent_maximum") return NilpotentMaximum().className(); + if (name == "sum") return UnboundedSum().className(); + return name; + } + + std::string FisImporter::translateDefuzzifier(const std::string& name) const { + if (name.empty()) return ""; + if (name == "centroid") return Centroid().className(); + if (name == "bisector") return Bisector().className(); + if (name == "lom") return LargestOfMaximum().className(); + if (name == "mom") return MeanOfMaximum().className(); + if (name == "som") return SmallestOfMaximum().className(); + if (name == "wtaver") return WeightedAverage().className(); + if (name == "wtsum") return WeightedSum().className(); + return name; + } + + std::pair<scalar, scalar> FisImporter::parseRange(const std::string& range) const { + std::vector<std::string> parts = Op::split(range, " "); + if (parts.size() != 2) + throw Exception("[syntax error] expected range in format '[begin end]'," + " but found <" + range + ">", FL_AT); + std::string begin = parts.at(0), end = parts.at(1); + if (begin.at(0) != '[' or end.at(end.size() - 1) != ']') + throw Exception("[syntax error] expected range in format '[begin end]'," + " but found <" + range + ">", FL_AT); + std::pair<scalar, scalar> result; + result.first = Op::toScalar(begin.substr(1)); + result.second = Op::toScalar(end.substr(0, end.size() - 1)); + return result; + } + + Term * FisImporter::parseTerm(const std::string& fis, const Engine* engine) const { + std::ostringstream ss; + for (std::size_t i = 0; i < fis.size(); ++i) { + if (not (fis.at(i) == '[' or fis.at(i) == ']')) { + ss << fis.at(i); + } + } + std::string line = ss.str(); + + std::vector<std::string> nameTerm = Op::split(line, ":"); + if (nameTerm.size() != 2) { + throw Exception("[syntax error] expected term in format 'name':'class',[params], " + "but found <" + line + ">", FL_AT); + } + std::vector<std::string> termParams = Op::split(nameTerm.at(1), ","); + if (termParams.size() != 2) { + throw Exception("[syntax error] expected term in format 'name':'class',[params], " + "but found " + line, FL_AT); + } + + std::vector<std::string> parameters = Op::split(termParams.at(1), " "); + for (std::size_t i = 0; i < parameters.size(); ++i) { + parameters.at(i) = Op::trim(parameters.at(i)); + } + return createInstance( + Op::trim(termParams.at(0)), + Op::trim(nameTerm.at(0)), + parameters, engine); + } + + Term* FisImporter::createInstance(const std::string& mClass, + const std::string& name, const std::vector<std::string>& params, + const Engine* engine) const { + std::map<std::string, std::string> mapping; + mapping["binarymf"] = Binary().className(); + mapping["concavemf"] = Concave().className(); + mapping["constant"] = Constant().className(); + mapping["cosinemf"] = Cosine().className(); + mapping["discretemf"] = Discrete().className(); + mapping["function"] = Function().className(); + mapping["gbellmf"] = Bell().className(); + mapping["gaussmf"] = Gaussian().className(); + mapping["gauss2mf"] = GaussianProduct().className(); + mapping["linear"] = Linear().className(); + mapping["pimf"] = PiShape().className(); + mapping["rampmf"] = Ramp().className(); + mapping["rectmf"] = Rectangle().className(); + mapping["smf"] = SShape().className(); + mapping["sigmf"] = Sigmoid().className(); + mapping["dsigmf"] = SigmoidDifference().className(); + mapping["psigmf"] = SigmoidProduct().className(); + mapping["spikemf"] = Spike().className(); + mapping["trapmf"] = Trapezoid().className(); + mapping["trimf"] = Triangle().className(); + mapping["zmf"] = ZShape().className(); + + std::vector<std::string> sortedParams = params; + + if (mClass == "gbellmf" and params.size() >= 3) { + sortedParams.at(0) = params.at(2); + sortedParams.at(1) = params.at(0); + sortedParams.at(2) = params.at(1); + } else if (mClass == "gaussmf" and params.size() >= 2) { + sortedParams.at(0) = params.at(1); + sortedParams.at(1) = params.at(0); + } else if (mClass == "gauss2mf" and params.size() >= 4) { + sortedParams.at(0) = params.at(1); + sortedParams.at(1) = params.at(0); + sortedParams.at(2) = params.at(3); + sortedParams.at(3) = params.at(2); + } else if (mClass == "sigmf" and params.size() >= 2) { + sortedParams.at(0) = params.at(1); + sortedParams.at(1) = params.at(0); + } else if (mClass == "dsigmf" and params.size() >= 4) { + sortedParams.at(0) = params.at(1); + sortedParams.at(1) = params.at(0); + sortedParams.at(2) = params.at(2); + sortedParams.at(3) = params.at(3); + } else if (mClass == "psigmf" and params.size() >= 4) { + sortedParams.at(0) = params.at(1); + sortedParams.at(1) = params.at(0); + sortedParams.at(2) = params.at(2); + sortedParams.at(3) = params.at(3); + } + + std::string flClass; + std::map<std::string, std::string>::const_iterator it = mapping.find(mClass); + if (it != mapping.end()) flClass = it->second; + else flClass = mClass; + + FL_unique_ptr<Term> term; + term.reset(FactoryManager::instance()->term()->constructObject(flClass)); + term->updateReference(engine); + term->setName(Op::validName(name)); + std::string separator; + if (not dynamic_cast<Function*> (term.get())) { + separator = " "; + } + term->configure(Op::join(sortedParams, separator)); + return term.release(); + } + + FisImporter* FisImporter::clone() const { + return new FisImporter(*this); + } + +} diff --git a/fuzzylite/src/imex/FldExporter.cpp b/fuzzylite/src/imex/FldExporter.cpp new file mode 100644 index 0000000..e25af1b --- /dev/null +++ b/fuzzylite/src/imex/FldExporter.cpp @@ -0,0 +1,312 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/imex/FldExporter.h" + +#include "fl/Engine.h" +#include "fl/Operation.h" +#include "fl/variable/Variable.h" +#include "fl/variable/InputVariable.h" +#include "fl/variable/OutputVariable.h" + +#include <fstream> + +namespace fl { + + FldExporter::FldExporter(const std::string& separator) : Exporter(), + _separator(separator), _exportHeaders(true), + _exportInputValues(true), _exportOutputValues(true) { } + + FldExporter::~FldExporter() { } + + std::string FldExporter::name() const { + return "FldExporter"; + } + + void FldExporter::setSeparator(const std::string& separator) { + this->_separator = separator; + } + + std::string FldExporter::getSeparator() const { + return this->_separator; + } + + void FldExporter::setExportHeader(bool exportHeaders) { + this->_exportHeaders = exportHeaders; + } + + bool FldExporter::exportsHeader() const { + return this->_exportHeaders; + } + + void FldExporter::setExportInputValues(bool exportInputValues) { + this->_exportInputValues = exportInputValues; + } + + bool FldExporter::exportsInputValues() const { + return this->_exportInputValues; + } + + void FldExporter::setExportOutputValues(bool exportOutputValues) { + this->_exportOutputValues = exportOutputValues; + } + + bool FldExporter::exportsOutputValues() const { + return this->_exportOutputValues; + } + + std::string FldExporter::header(const Engine* engine) const { + std::vector<std::string> result; + if (_exportInputValues) { + for (std::size_t i = 0; i < engine->numberOfInputVariables(); ++i) { + InputVariable* inputVariable = engine->getInputVariable(i); + result.push_back(inputVariable->getName()); + } + } + if (_exportOutputValues) { + for (std::size_t i = 0; i < engine->numberOfOutputVariables(); ++i) { + OutputVariable* outputVariable = engine->getOutputVariable(i); + result.push_back(outputVariable->getName()); + } + } + return Op::join(result, _separator); + } + + std::string FldExporter::toString(const Engine* engine) const { + return toString(const_cast<Engine*> (engine), 1024, AllVariables); + } + + std::string FldExporter::toString(Engine* engine, int values, ScopeOfValues scope) const { + return toString(engine, values, scope, engine->inputVariables()); + } + + std::string FldExporter::toString(Engine* engine, int values, ScopeOfValues scope, + const std::vector<InputVariable*>& activeVariables) const { + std::ostringstream result; + write(engine, result, values, scope, activeVariables); + return result.str(); + } + + std::string FldExporter::toString(Engine* engine, std::istream& reader) const { + std::ostringstream writer; + if (_exportHeaders) writer << header(engine) << "\n"; + std::string line; + int lineNumber = 0; + while (std::getline(reader, line)) { + ++lineNumber; + line = Op::trim(line); + if (not line.empty() and line.at(0) == '#') + continue; //comments are ignored, blank lines are retained + std::vector<scalar> inputValues; + if (lineNumber == 1) { //automatic detection of header. + try { + inputValues = parse(line); + } catch (std::exception&) { + continue; + } + } else { + inputValues = parse(line); + } + + write(engine, writer, inputValues, engine->inputVariables()); + } + return writer.str(); + } + + void FldExporter::toFile(const std::string& path, Engine* engine, int values, ScopeOfValues scope) const { + toFile(path, engine, values, scope, engine->inputVariables()); + } + + void FldExporter::toFile(const std::string& path, Engine* engine, int values, ScopeOfValues scope, + const std::vector<InputVariable*>& activeVariables) const { + std::ofstream writer(path.c_str()); + if (not writer.is_open()) { + throw Exception("[file error] file <" + path + "> could not be created", FL_AT); + } + write(engine, writer, values, scope, activeVariables); + writer.close(); + } + + void FldExporter::toFile(const std::string& path, Engine* engine, std::istream& reader) const { + std::ofstream writer(path.c_str()); + if (not writer.is_open()) { + throw Exception("[file error] file <" + path + "> could not be created", FL_AT); + } + if (_exportHeaders) writer << header(engine) << "\n"; + + std::string line; + int lineNumber = 0; + while (std::getline(reader, line)) { + ++lineNumber; + line = Op::trim(line); + if (not line.empty() and line.at(0) == '#') + continue; //comments are ignored, blank lines are retained + std::vector<scalar> inputValues; + if (lineNumber == 1) { //automatic detection of header. + try { + inputValues = parse(line); + } catch (std::exception&) { + continue; + } + } else { + inputValues = parse(line); + } + + write(engine, writer, inputValues, engine->inputVariables()); + } + writer.close(); + } + + std::vector<scalar> FldExporter::parse(const std::string& values) const { + std::vector<scalar> inputValues; + if (not (values.empty() or values.at(0) == '#')) { + inputValues = Op::toScalars(values); + } + return inputValues; + } + + void FldExporter::write(Engine* engine, std::ostream& writer, int values, ScopeOfValues scope) const { + write(engine, writer, values, scope, engine->inputVariables()); + } + + void FldExporter::write(Engine* engine, std::ostream& writer, + int values, ScopeOfValues scope, + const std::vector<InputVariable*>& activeVariables) const { + if (_exportHeaders) writer << header(engine) << "\n"; + + if (activeVariables.size() != engine->inputVariables().size()) { + std::ostringstream ex; + ex << "[exporter error] number of active variables " + "<" << activeVariables.size() << ">" + << "must match the number of input variables in the engine " + "<" << engine->inputVariables().size() << ">"; + throw Exception(ex.str(), FL_AT); + } + + int resolution; + if (scope == AllVariables) + resolution = -1 + (int) std::max(1.0, std::pow( + values, 1.0 / engine->numberOfInputVariables())); + else //if (scope == EachVariable) + resolution = values - 1; + + std::vector<int> sampleValues, minSampleValues, maxSampleValues; + for (std::size_t i = 0; i < engine->numberOfInputVariables(); ++i) { + sampleValues.push_back(0); + minSampleValues.push_back(0); + if (engine->inputVariables().at(i) == activeVariables.at(i)) + maxSampleValues.push_back(resolution); + else maxSampleValues.push_back(0); + } + + std::vector<scalar> inputValues(engine->numberOfInputVariables()); + do { + for (std::size_t i = 0; i < engine->numberOfInputVariables(); ++i) { + InputVariable* inputVariable = engine->getInputVariable(i); + if (inputVariable == activeVariables.at(i)) { + inputValues.at(i) = inputVariable->getMinimum() + + sampleValues.at(i) * inputVariable->range() / std::max(1, resolution); + } else { + inputValues.at(i) = inputVariable->getValue(); + } + } + write(engine, writer, inputValues, activeVariables); + } while (Op::increment(sampleValues, minSampleValues, maxSampleValues)); + } + + void FldExporter::write(Engine* engine, std::ostream& writer, std::istream& reader) const { + if (_exportHeaders) writer << header(engine) << "\n"; + + std::string line; + std::size_t lineNumber = 0; + while (std::getline(reader, line)) { + ++lineNumber; + line = Op::trim(line); + if (not line.empty() and line.at(0) == '#') + continue; //comments are ignored, blank lines are retained + std::vector<scalar> inputValues; + if (lineNumber == 1) { //automatic detection of header. + try { + inputValues = parse(line); + } catch (std::exception&) { + continue; + } + } else { + inputValues = parse(line); + } + try { + write(engine, writer, inputValues, engine->inputVariables()); + } catch (Exception& ex) { + ex.append(" writing line <" + Op::str(lineNumber) + ">"); + throw; + } + } + } + + void FldExporter::write(Engine* engine, std::ostream& writer, + const std::vector<scalar>& inputValues) const { + write(engine, writer, inputValues, engine->inputVariables()); + } + + void FldExporter::write(Engine* engine, std::ostream& writer, + const std::vector<scalar>& inputValues, + const std::vector<InputVariable*>& activeVariables) const { + if (inputValues.empty()) { + writer << "\n"; + return; + } + if (inputValues.size() < engine->numberOfInputVariables()) { + std::ostringstream ex; + ex << "[export error] engine has <" << engine->numberOfInputVariables() << "> " + "input variables, but input data provides <" << inputValues.size() << "> values"; + throw Exception(ex.str(), FL_AT); + } + if (activeVariables.size() != engine->inputVariables().size()) { + std::ostringstream ex; + ex << "[exporter error] number of active variables <" << activeVariables.size() << "> " + "must match the number of input variables in the engine <" << engine->inputVariables().size() << ">"; + throw Exception(ex.str(), FL_AT); + } + + std::vector<scalar> values; + for (std::size_t i = 0; i < engine->numberOfInputVariables(); ++i) { + InputVariable* inputVariable = engine->getInputVariable(i); + scalar inputValue; + if (inputVariable == activeVariables.at(i)) { + inputValue = inputValues.at(i); + } else { + inputValue = inputVariable->getValue(); + } + inputVariable->setValue(inputValue); + if (_exportInputValues) values.push_back(inputValue); + } + + engine->process(); + + for (std::size_t i = 0; i < engine->numberOfOutputVariables(); ++i) { + OutputVariable* outputVariable = engine->getOutputVariable(i); + if (_exportOutputValues) + values.push_back(outputVariable->getValue()); + } + + writer << Op::join(values, _separator) << "\n"; + } + + FldExporter* FldExporter::clone() const { + return new FldExporter(*this); + } + +} diff --git a/fuzzylite/src/imex/FllExporter.cpp b/fuzzylite/src/imex/FllExporter.cpp new file mode 100644 index 0000000..6a86152 --- /dev/null +++ b/fuzzylite/src/imex/FllExporter.cpp @@ -0,0 +1,210 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/imex/FllExporter.h" + +#include "fl/Headers.h" + +namespace fl { + + FllExporter::FllExporter(const std::string& indent, const std::string& separator) + : Exporter(), _indent(indent), _separator(separator) { } + + FllExporter::~FllExporter() { } + + std::string FllExporter::name() const { + return "FllExporter"; + } + + void FllExporter::setIndent(const std::string& indent) { + this->_indent = indent; + } + + std::string FllExporter::getIndent() const { + return this->_indent; + } + + void FllExporter::setSeparator(const std::string& separator) { + this->_separator = separator; + } + + std::string FllExporter::getSeparator() const { + return this->_separator; + } + + std::string FllExporter::toString(const Engine* engine) const { + std::vector<std::string> result; + result.push_back("Engine: " + engine->getName()); + if (not engine->getDescription().empty()) + result.push_back("description: " + engine->getDescription()); + for (std::size_t i = 0 ; i < engine->numberOfInputVariables(); ++i){ + result.push_back(toString(engine->getInputVariable(i))); + } + for (std::size_t i = 0 ; i < engine->numberOfOutputVariables(); ++i){ + result.push_back(toString(engine->getOutputVariable(i))); + } + for (std::size_t i = 0 ; i < engine->numberOfRuleBlocks(); ++i){ + result.push_back(toString(engine->getRuleBlock(i))); + } + return Op::join(result, _separator); + } + + std::string FllExporter::toString(const std::vector<Variable*>& variables) const { + std::vector<std::string> result; + for (std::size_t i = 0; i < variables.size(); ++i) { + result.push_back(toString(variables.at(i))); + } + return Op::join(result, _separator); + } + + std::string FllExporter::toString(const std::vector<InputVariable*>& variables) const { + std::vector<std::string> result; + for (std::size_t i = 0; i < variables.size(); ++i) { + result.push_back(toString(variables.at(i))); + } + return Op::join(result, _separator); + } + + std::string FllExporter::toString(const std::vector<OutputVariable*>& variables) const { + std::vector<std::string> result; + for (std::size_t i = 0; i < variables.size(); ++i) { + result.push_back(toString(variables.at(i))); + } + return Op::join(result, _separator); + } + + std::string FllExporter::toString(const std::vector<RuleBlock*>& ruleBlocks) const { + std::vector<std::string> result; + for (std::size_t i = 0; i < ruleBlocks.size(); ++i) { + result.push_back(toString(ruleBlocks.at(i))); + } + return Op::join(result, _separator); + } + + std::string FllExporter::toString(const Variable* variable) const { + std::vector<std::string> result; + result.push_back("Variable: " + Op::validName(variable->getName())); + if (not variable->getDescription().empty()) { + result.push_back(_indent + "description: " + variable->getDescription()); + } + result.push_back(_indent + "enabled: " + (variable->isEnabled() ? "true" : "false")); + result.push_back(_indent + "range: " + Op::join(2, " ", + variable->getMinimum(), variable->getMaximum())); + result.push_back(_indent + "lock-range: " + + (variable->isLockValueInRange() ? "true" : "false")); + for (std::size_t i = 0; i < variable->numberOfTerms(); ++i) { + result.push_back(_indent + toString(variable->getTerm(i))); + } + return Op::join(result, _separator); + } + + std::string FllExporter::toString(const InputVariable* inputVariable) const { + std::vector<std::string> result; + result.push_back("InputVariable: " + Op::validName(inputVariable->getName())); + if (not inputVariable->getDescription().empty()) { + result.push_back(_indent + "description: " + inputVariable->getDescription()); + } + result.push_back(_indent + "enabled: " + (inputVariable->isEnabled() ? "true" : "false")); + result.push_back(_indent + "range: " + Op::join(2, " ", + inputVariable->getMinimum(), inputVariable->getMaximum())); + result.push_back(_indent + "lock-range: " + + (inputVariable->isLockValueInRange() ? "true" : "false")); + for (std::size_t i = 0; i < inputVariable->numberOfTerms(); ++i) { + result.push_back(_indent + toString(inputVariable->getTerm(i))); + } + return Op::join(result, _separator); + } + + std::string FllExporter::toString(const OutputVariable* outputVariable) const { + std::vector<std::string> result; + result.push_back("OutputVariable: " + Op::validName(outputVariable->getName())); + if (not outputVariable->getDescription().empty()) { + result.push_back(_indent + "description: " + outputVariable->getDescription()); + } + result.push_back(_indent + "enabled: " + (outputVariable->isEnabled() ? "true" : "false")); + result.push_back(_indent + "range: " + Op::join(2, " ", + outputVariable->getMinimum(), outputVariable->getMaximum())); + result.push_back(_indent + "lock-range: " + + (outputVariable->isLockValueInRange() ? "true" : "false")); + result.push_back(_indent + "aggregation: " + + toString(outputVariable->fuzzyOutput()->getAggregation())); + result.push_back(_indent + "defuzzifier: " + + toString(outputVariable->getDefuzzifier())); + result.push_back(_indent + "default: " + Op::str(outputVariable->getDefaultValue())); + result.push_back(_indent + "lock-previous: " + + (outputVariable->isLockPreviousValue() ? "true" : "false")); + for (std::size_t i = 0; i < outputVariable->numberOfTerms(); ++i) { + result.push_back(_indent + toString(outputVariable->getTerm(i))); + } + return Op::join(result, _separator); + } + + std::string FllExporter::toString(const RuleBlock* ruleBlock) const { + std::vector<std::string> result; + result.push_back("RuleBlock: " + ruleBlock->getName()); + if (not ruleBlock->getDescription().empty()) { + result.push_back(_indent + "description: " + ruleBlock->getDescription()); + } + result.push_back(_indent + "enabled: " + + (ruleBlock->isEnabled() ? "true" : "false")); + result.push_back(_indent + "conjunction: " + toString(ruleBlock->getConjunction())); + result.push_back(_indent + "disjunction: " + toString(ruleBlock->getDisjunction())); + result.push_back(_indent + "implication: " + toString(ruleBlock->getImplication())); + result.push_back(_indent + "activation: " + toString(ruleBlock->getActivation())); + for (std::size_t i = 0; i < ruleBlock->numberOfRules(); ++i) { + result.push_back(_indent + toString(ruleBlock->getRule(i))); + } + return Op::join(result, _separator); + } + + std::string FllExporter::toString(const Rule* rule) const { + return "rule: " + rule->getText(); + } + + std::string FllExporter::toString(const Term* term) const { + return "term: " + Op::validName(term->getName()) + " " + term->className() + + " " + term->parameters(); + } + + std::string FllExporter::toString(const Norm* norm) const { + if (norm) return norm->className(); + return "none"; + } + + std::string FllExporter::toString(const Activation* activation) const { + if (not activation) return "none"; + if (activation->parameters().empty()) return activation->className(); + return activation->className() + " " + activation->parameters(); + } + + std::string FllExporter::toString(const Defuzzifier* defuzzifier) const { + if (not defuzzifier) return "none"; + if (const IntegralDefuzzifier * integralDefuzzifier = + dynamic_cast<const IntegralDefuzzifier*> (defuzzifier)) { + return defuzzifier->className() + " " + Op::str(integralDefuzzifier->getResolution()); + + } else if (const WeightedDefuzzifier * weightedDefuzzifier = + dynamic_cast<const WeightedDefuzzifier*> (defuzzifier)) { + return weightedDefuzzifier->className() + " " + weightedDefuzzifier->getTypeName(); + } + return defuzzifier->className(); + } + + FllExporter* FllExporter::clone() const { + return new FllExporter(*this); + } + +} diff --git a/fuzzylite/src/imex/FllImporter.cpp b/fuzzylite/src/imex/FllImporter.cpp new file mode 100644 index 0000000..ac9d1fc --- /dev/null +++ b/fuzzylite/src/imex/FllImporter.cpp @@ -0,0 +1,316 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/imex/FllImporter.h" + +#include "fl/Headers.h" + +#include <queue> + +namespace fl { + + FllImporter::FllImporter(const std::string& separator) : Importer(), + _separator(separator) { } + + FllImporter::~FllImporter() { } + + std::string FllImporter::name() const { + return "FllImporter"; + } + + void FllImporter::setSeparator(const std::string& separator) { + this->_separator = separator; + } + + std::string FllImporter::getSeparator() const { + return this->_separator; + } + + Engine* FllImporter::fromString(const std::string& code) const { + FL_unique_ptr<Engine> engine(new Engine); + + const std::string fll = Op::join(Op::split(code, _separator), "\n"); + std::string tag; + std::vector<std::string> block; + std::istringstream fllReader(fll); + std::string line; + + while (std::getline(fllReader, line)) { + line = Op::trim(Op::split(line, "#", false).front()); //remove comments + if (line.empty()) continue; + std::size_t colon = line.find_first_of(':'); + if (colon == std::string::npos) { + throw Exception("[import error] expected a colon here: " + line, FL_AT); + } + std::string key = Op::trim(line.substr(0, colon)); + std::string value = Op::trim(line.substr(colon + 1)); + if ("Engine" == key) { + engine->setName(value); + continue; + } else if (key == "description" and block.empty()){ + engine->setDescription(value); + continue; + } else if (key == "InputVariable" + or key == "OutputVariable" + or key == "RuleBlock") { + process(tag, Op::join(block, "\n"), engine.get()); + block.clear(); //clear error flags + tag = key; + } else if (tag.empty()) { + throw Exception("[import error] unexpected block: " + line, FL_AT); + } + block.push_back(key + ":" + value); + } + process(tag, Op::join(block, "\n"), engine.get()); + return engine.release(); + } + + void FllImporter::process(const std::string& tag, const std::string& block, Engine* engine) const { + if (tag.empty()) return; + // FL_LOG("Processing " << tag << "\n" << block); + if ("InputVariable" == tag) { + processInputVariable(block, engine); + } else if ("OutputVariable" == tag) { + processOutputVariable(block, engine); + } else if ("RuleBlock" == tag) { + processRuleBlock(block, engine); + } else { + throw Exception("[import error] block tag <" + tag + "> not recognized", FL_AT); + } + } + + void FllImporter::processInputVariable(const std::string& block, Engine* engine) const { + std::istringstream reader(block); + std::string line; + FL_unique_ptr<InputVariable> inputVariable(new InputVariable); + while (std::getline(reader, line)) { + std::pair<std::string, std::string> keyValue = parseKeyValue(line, ':'); + if ("InputVariable" == keyValue.first) { + inputVariable->setName(Op::validName(keyValue.second)); + } else if ("description" == keyValue.first) { + inputVariable->setDescription(keyValue.second); + } else if ("enabled" == keyValue.first) { + inputVariable->setEnabled(parseBoolean(keyValue.second)); + } else if ("range" == keyValue.first) { + std::pair<scalar, scalar> range = parseRange(keyValue.second); + inputVariable->setRange(range.first, range.second); + } else if ("lock-range" == keyValue.first) { + inputVariable->setLockValueInRange(parseBoolean(keyValue.second)); + } else if ("term" == keyValue.first) { + inputVariable->addTerm(parseTerm(keyValue.second, engine)); + } else { + throw Exception("[import error] key <" + keyValue.first + "> not " + "recognized in pair <" + keyValue.first + ":" + keyValue.second + ">", FL_AT); + } + } + engine->addInputVariable(inputVariable.release()); + } + + void FllImporter::processOutputVariable(const std::string& block, Engine* engine) const { + std::istringstream reader(block); + std::string line; + FL_unique_ptr<OutputVariable> outputVariable(new OutputVariable); + while (std::getline(reader, line)) { + std::pair<std::string, std::string> keyValue = parseKeyValue(line, ':'); + if ("OutputVariable" == keyValue.first) { + outputVariable->setName(Op::validName(keyValue.second)); + } else if ("description" == keyValue.first) { + outputVariable->setDescription(keyValue.second); + } else if ("enabled" == keyValue.first) { + outputVariable->setEnabled(parseBoolean(keyValue.second)); + } else if ("range" == keyValue.first) { + std::pair<scalar, scalar> range = parseRange(keyValue.second); + outputVariable->setRange(range.first, range.second); + } else if ("default" == keyValue.first) { + outputVariable->setDefaultValue(Op::toScalar(keyValue.second)); + } else if ("lock-previous" == keyValue.first or "lock-valid" == keyValue.first) { + outputVariable->setLockPreviousValue(parseBoolean(keyValue.second)); + } else if ("lock-range" == keyValue.first) { + outputVariable->setLockValueInRange(parseBoolean(keyValue.second)); + } else if ("defuzzifier" == keyValue.first) { + outputVariable->setDefuzzifier(parseDefuzzifier(keyValue.second)); + } else if ("aggregation" == keyValue.first) { + outputVariable->fuzzyOutput()->setAggregation(parseSNorm(keyValue.second)); + } else if ("accumulation" == keyValue.first) { + outputVariable->fuzzyOutput()->setAggregation(parseSNorm(keyValue.second)); + FL_LOG("[warning] obsolete usage of identifier <accumulation: SNorm> in OutputVariable"); + FL_LOG("[information] from version 6.0, the identifier <aggregation: SNorm> should be used"); + FL_LOG("[backward compatibility] assumed " + "<aggregation: " << keyValue.second << "> " + "instead of <accumulation: " << keyValue.second << ">"); + } else if ("term" == keyValue.first) { + outputVariable->addTerm(parseTerm(keyValue.second, engine)); + } else { + throw Exception("[import error] key <" + keyValue.first + "> not " + "recognized in pair <" + keyValue.first + ":" + keyValue.second + ">", FL_AT); + } + } + engine->addOutputVariable(outputVariable.release()); + } + + void FllImporter::processRuleBlock(const std::string& block, Engine* engine) const { + std::istringstream reader(block); + std::string line; + FL_unique_ptr<RuleBlock> ruleBlock(new RuleBlock); + while (std::getline(reader, line)) { + std::pair<std::string, std::string> keyValue = parseKeyValue(line, ':'); + if ("RuleBlock" == keyValue.first) { + ruleBlock->setName(keyValue.second); + } else if ("description" == keyValue.first) { + ruleBlock->setDescription(keyValue.second); + } else if ("enabled" == keyValue.first) { + ruleBlock->setEnabled(parseBoolean(keyValue.second)); + } else if ("conjunction" == keyValue.first) { + ruleBlock->setConjunction(parseTNorm(keyValue.second)); + } else if ("disjunction" == keyValue.first) { + ruleBlock->setDisjunction(parseSNorm(keyValue.second)); + } else if ("implication" == keyValue.first) { + ruleBlock->setImplication(parseTNorm(keyValue.second)); + } else if ("activation" == keyValue.first) { + TNormFactory* tnorm = FactoryManager::instance()->tnorm(); + //@todo remove backwards compatibility in version 7.0 + if (tnorm->hasConstructor(keyValue.second)) { + ruleBlock->setImplication(parseTNorm(keyValue.second)); + FL_LOG("[warning] obsolete usage of identifier <activation: TNorm> " + "in RuleBlock"); + FL_LOG("[information] from version 6.0, the identifiers are " + "<activation: Activation> for Activation methods " + "and <implication: TNorm> for T-Norms"); + FL_LOG("[backward compatibility] assumed " + "<implication: " << keyValue.second << "> " + "instead of <activation: " << keyValue.second << ">"); + } else { + ruleBlock->setActivation(parseActivation(keyValue.second)); + } + } else if ("rule" == keyValue.first) { + Rule* rule = new Rule; + rule->setText(keyValue.second); + try { + rule->load(engine); + } catch (std::exception& ex) { + FL_LOG(ex.what()); + } + ruleBlock->addRule(rule); + } else { + throw Exception("[import error] key <" + keyValue.first + "> not " + "recognized in pair <" + keyValue.first + ":" + keyValue.second + ">", FL_AT); + } + } + if (not ruleBlock->getActivation()){ + ruleBlock->setActivation(new General); + } + engine->addRuleBlock(ruleBlock.release()); + } + + Term* FllImporter::parseTerm(const std::string& text, Engine* engine) const { + std::vector<std::string> tokens = Op::split(text, " "); + + //MEDIUM Triangle 0.500 1.000 1.500 + + if (tokens.size() < 2) { + throw Exception("[syntax error] expected a term in format <name class parameters>, " + "but found <" + text + ">", FL_AT); + } + FL_unique_ptr<Term> term; + term.reset(FactoryManager::instance()->term()->constructObject(tokens.at(1))); + term->updateReference(engine); + term->setName(Op::validName(tokens.at(0))); + std::ostringstream parameters; + for (std::size_t i = 2; i < tokens.size(); ++i) { + parameters << tokens.at(i); + if (i + 1 < tokens.size()) parameters << " "; + } + term->configure(parameters.str()); + return term.release(); + } + + TNorm* FllImporter::parseTNorm(const std::string& name) const { + if (name == "none") return FactoryManager::instance()->tnorm()->constructObject(""); + return FactoryManager::instance()->tnorm()->constructObject(name); + } + + SNorm* FllImporter::parseSNorm(const std::string& name) const { + if (name == "none") return FactoryManager::instance()->snorm()->constructObject(""); + return FactoryManager::instance()->snorm()->constructObject(name); + } + + Activation* FllImporter::parseActivation(const std::string& name) const { + if (name == "none") return FactoryManager::instance()->activation()->constructObject(""); + std::vector<std::string> tokens = Op::split(name, " "); + Activation* result = FactoryManager::instance()->activation()->constructObject(tokens.front()); + + std::ostringstream parameters; + for (std::size_t i = 1; i < tokens.size(); ++i) { + parameters << tokens.at(i); + if (i + 1 < tokens.size()) parameters << " "; + } + result->configure(parameters.str()); + return result; + } + + Defuzzifier* FllImporter::parseDefuzzifier(const std::string& text) const { + std::vector<std::string> parameters = Op::split(text, " "); + std::string name = parameters.at(0); + if (name == "none") return FactoryManager::instance()->defuzzifier()->constructObject(""); + Defuzzifier* defuzzifier = FactoryManager::instance()->defuzzifier()->constructObject(name); + if (parameters.size() > 1) { + std::string parameter(parameters.at(1)); + if (IntegralDefuzzifier * integralDefuzzifier = dynamic_cast<IntegralDefuzzifier*> (defuzzifier)) { + integralDefuzzifier->setResolution((int) Op::toScalar(parameter)); + } else if (WeightedDefuzzifier * weightedDefuzzifier = dynamic_cast<WeightedDefuzzifier*> (defuzzifier)) { + WeightedDefuzzifier::Type type = WeightedDefuzzifier::Automatic; + if (parameter == "Automatic") type = WeightedDefuzzifier::Automatic; + else if (parameter == "TakagiSugeno") type = WeightedDefuzzifier::TakagiSugeno; + else if (parameter == "Tsukamoto") type = WeightedDefuzzifier::Tsukamoto; + else throw Exception("[syntax error] unknown parameter of WeightedDefuzzifier <" + parameter + ">", FL_AT); + weightedDefuzzifier->setType(type); + } + } + return defuzzifier; + } + + std::pair<scalar, scalar> FllImporter::parseRange(const std::string& text) const { + std::pair<std::string, std::string> range = parseKeyValue(text, ' '); + return std::pair<scalar, scalar>(Op::toScalar(range.first), Op::toScalar(range.second)); + } + + bool FllImporter::parseBoolean(const std::string& boolean) const { + if ("true" == boolean) return true; + if ("false" == boolean) return false; + throw Exception("[syntax error] expected boolean <true|false>, " + "but found <" + boolean + ">", FL_AT); + } + + std::pair<std::string, std::string> FllImporter::parseKeyValue(const std::string& text, + char separator) const { + std::size_t half = text.find_first_of(separator); + if (half == std::string::npos) { + std::ostringstream ex; + ex << "[syntax error] expected pair in the form " + "<key" << separator << "value>, but found <" << text << ">"; + throw Exception(ex.str(), FL_AT); + } + std::pair<std::string, std::string> result; + result.first = text.substr(0, half); + result.second = text.substr(half + 1); + return result; + } + + FllImporter* FllImporter::clone() const { + return new FllImporter(*this); + } + +} diff --git a/fuzzylite/src/imex/Importer.cpp b/fuzzylite/src/imex/Importer.cpp new file mode 100644 index 0000000..c0be7f5 --- /dev/null +++ b/fuzzylite/src/imex/Importer.cpp @@ -0,0 +1,42 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/imex/Importer.h" +#include "fl/Exception.h" + +#include <fstream> + +namespace fl { + + Importer::Importer() { } + + Importer::~Importer() { } + + Engine* Importer::fromFile(const std::string& path) const { + std::ifstream reader(path.c_str()); + if (not reader.is_open()) { + throw Exception("[file error] file <" + path + "> could not be opened", FL_AT); + } + std::ostringstream textEngine; + std::string line; + while (std::getline(reader, line)) { + textEngine << line << std::endl; + } + reader.close(); + return fromString(textEngine.str()); + } + +} diff --git a/fuzzylite/src/imex/JavaExporter.cpp b/fuzzylite/src/imex/JavaExporter.cpp new file mode 100644 index 0000000..6981dae --- /dev/null +++ b/fuzzylite/src/imex/JavaExporter.cpp @@ -0,0 +1,251 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/imex/JavaExporter.h" + +#include "fl/Headers.h" + +namespace fl { + + JavaExporter::JavaExporter(bool usingVariableNames) : Exporter(), + _usingVariableNames(usingVariableNames) { } + + JavaExporter::~JavaExporter() { } + + std::string JavaExporter::name() const { + return "JavaExporter"; + } + + void JavaExporter::setUsingVariableNames(bool usingVariableNames) { + this->_usingVariableNames = usingVariableNames; + } + + bool JavaExporter::isUsingVariableNames() const { + return this->_usingVariableNames; + } + + std::string JavaExporter::toString(const Engine* engine) const { + std::ostringstream ss; + ss << "//Code automatically generated with " << fuzzylite::library() << ".\n\n"; + ss << "Engine engine = new Engine();\n"; + ss << "engine.setName(\"" << engine->getName() << "\");\n"; + ss << "engine.setDescription(\"" << engine->getDescription() << "\");\n"; + ss << "\n"; + + for (std::size_t i = 0; i < engine->numberOfInputVariables(); ++i) { + ss << toString(engine->getInputVariable(i), engine) << "\n"; + } + + for (std::size_t i = 0; i < engine->numberOfOutputVariables(); ++i) { + ss << toString(engine->getOutputVariable(i), engine) << "\n"; + } + + for (std::size_t i = 0; i < engine->numberOfRuleBlocks(); ++i) { + ss << toString(engine->getRuleBlock(i), engine) << "\n"; + } + + return ss.str(); + } + + std::string JavaExporter::toString(const InputVariable* inputVariable, const Engine* engine) const { + std::ostringstream ss; + std::string name; + if (isUsingVariableNames()) { + name = Op::validName(inputVariable->getName()); + } else { + name = "inputVariable"; + if (engine->numberOfInputVariables() > 1) { + std::size_t index = std::distance(engine->inputVariables().begin(), + std::find(engine->inputVariables().begin(), + engine->inputVariables().end(), inputVariable)); + name += Op::str(index + 1); + } + } + ss << "InputVariable " << name << " = new InputVariable();\n"; + ss << name << ".setName(\"" << inputVariable->getName() << "\");\n"; + ss << name << ".setDescription(\"" << inputVariable->getDescription() << "\");\n"; + ss << name << ".setEnabled(" << (inputVariable->isEnabled() ? "true" : "false") << ");\n"; + ss << name << ".setRange(" + << toString(inputVariable->getMinimum()) << ", " + << toString(inputVariable->getMaximum()) << ");\n"; + ss << name << ".setLockValueInRange(" + << (inputVariable->isLockValueInRange() ? "true" : "false") << ");\n"; + for (std::size_t i = 0; i < inputVariable->numberOfTerms(); ++i) { + ss << name << ".addTerm(" << + toString(inputVariable->getTerm(i)) << ");\n"; + } + ss << "engine.addInputVariable(" << name << ");\n"; + return ss.str(); + } + + std::string JavaExporter::toString(const OutputVariable* outputVariable, const Engine* engine) const { + std::ostringstream ss; + std::string name; + if (isUsingVariableNames()) { + name = Op::validName(outputVariable->getName()); + } else { + name = "outputVariable"; + if (engine->numberOfOutputVariables() > 1) { + std::size_t index = std::distance(engine->outputVariables().begin(), + std::find(engine->outputVariables().begin(), + engine->outputVariables().end(), outputVariable)); + name += Op::str(index + 1); + } + } + ss << "OutputVariable " << name << " = new OutputVariable();\n"; + ss << name << ".setName(\"" << outputVariable->getName() << "\");\n"; + ss << name << ".setDescription(\"" << outputVariable->getDescription() << "\");\n"; + ss << name << ".setEnabled(" << (outputVariable->isEnabled() ? "true" : "false") << ");\n"; + ss << name << ".setRange(" + << toString(outputVariable->getMinimum()) << ", " + << toString(outputVariable->getMaximum()) << ");\n"; + ss << name << ".setLockValueInRange(" << + (outputVariable->isLockValueInRange() ? "true" : "false") << ");\n"; + ss << name << ".setAggregation(" << + toString(outputVariable->fuzzyOutput()->getAggregation()) << ");\n"; + ss << name << ".setDefuzzifier(" << + toString(outputVariable->getDefuzzifier()) << ");\n"; + ss << name << ".setDefaultValue(" << + toString(outputVariable->getDefaultValue()) << ");\n"; + ss << name << ".setLockPreviousValue(" << + (outputVariable->isLockPreviousValue() ? "true" : "false") << ");\n"; + for (std::size_t i = 0; i < outputVariable->numberOfTerms(); ++i) { + ss << name << ".addTerm(" << + toString(outputVariable->getTerm(i)) << ");\n"; + } + ss << "engine.addOutputVariable(" << name << ");\n"; + return ss.str(); + } + + std::string JavaExporter::toString(const RuleBlock* ruleBlock, const Engine* engine) const { + std::ostringstream ss; + std::string name; + + if (isUsingVariableNames() and not ruleBlock->getName().empty()) { + name = Op::validName(ruleBlock->getName()); + } else { + name = "ruleBlock"; + if (engine->numberOfRuleBlocks() > 1) { + std::size_t index = std::distance(engine->ruleBlocks().begin(), + std::find(engine->ruleBlocks().begin(), + engine->ruleBlocks().end(), ruleBlock)); + name += Op::str(index + 1); + } + } + + ss << "RuleBlock " << name << " = new RuleBlock();\n"; + ss << name << ".setName(\"" << ruleBlock->getName() << "\");\n"; + ss << name << ".setDescription(\"" << ruleBlock->getDescription() << "\");\n"; + ss << name << ".setEnabled(" << (ruleBlock->isEnabled() ? "true" : "false") << ");\n"; + ss << name << ".setConjunction(" + << toString(ruleBlock->getConjunction()) << ");\n"; + ss << name << ".setDisjunction(" + << toString(ruleBlock->getDisjunction()) << ");\n"; + ss << name << ".setImplication(" + << toString(ruleBlock->getImplication()) << ");\n"; + ss << name << ".setActivation(" + << toString(ruleBlock->getActivation()) << ");\n"; + for (std::size_t i = 0; i < ruleBlock->numberOfRules(); ++i) { + Rule* rule = ruleBlock->getRule(i); + ss << name << ".addRule(Rule.parse(\"" << rule->getText() << "\", engine));\n"; + } + ss << "engine.addRuleBlock(" << name << ");\n"; + return ss.str(); + } + + std::string JavaExporter::toString(const Term* term) const { + if (not term) { + return "null"; + } + + if (const Discrete * discrete = dynamic_cast<const Discrete*> (term)) { + std::ostringstream ss; + std::vector<scalar> xy; + ss << term->className() << ".create(\"" << term->getName() << "\", " + << Op::join(Discrete::toVector(discrete->xy()), ", ") << ")"; + return ss.str(); + } + + if (const Function * function = dynamic_cast<const Function*> (term)) { + std::ostringstream ss; + ss << term->className() << ".create(\"" << term->getName() << "\", " + << "\"" << function->getFormula() << "\", engine)"; + return ss.str(); + } + + if (const Linear * linear = dynamic_cast<const Linear*> (term)) { + std::ostringstream ss; + ss << term->className() << ".create(\"" << term->getName() << "\", " + << "engine, " << Op::join(linear->coefficients(), ", ") << ")"; + return ss.str(); + } + + std::ostringstream ss; + ss << "new " << term->className() << "(\"" << term->getName() << "\", " + << Op::findReplace(term->parameters(), " ", ", ") << ")"; + return ss.str(); + } + + std::string JavaExporter::toString(const Defuzzifier* defuzzifier) const { + if (not defuzzifier) return "null"; + + if (const IntegralDefuzzifier * integralDefuzzifier = + dynamic_cast<const IntegralDefuzzifier*> (defuzzifier)) { + return "new " + integralDefuzzifier->className() + "(" + + Op::str(integralDefuzzifier->getResolution()) + ")"; + } + if (const WeightedDefuzzifier * weightedDefuzzifier = + dynamic_cast<const WeightedDefuzzifier*> (defuzzifier)) { + return "new " + weightedDefuzzifier->className() + + "(\"" + weightedDefuzzifier->getTypeName() + "\")"; + } + return "new " + defuzzifier->className() + "()"; + } + + std::string JavaExporter::toString(const Norm* norm) const { + if (not norm) return "null"; + return "new " + norm->className() + "()"; + } + + std::string JavaExporter::toString(const Activation* activation) const { + if (not activation) return "null"; + std::string parameters = Op::trim(activation->parameters()); + if (parameters.empty()) return "new " + activation->className() + "()"; + + std::vector<std::string> values = Op::split(parameters, " "); + for (std::size_t i = 0; i < values.size(); ++i) { + std::string parameter = values.at(i); + values.at(i) = (Op::isNumeric(parameter) ? parameter : ("\"" + parameter + "\"")); + } + return "new " + activation->className() + "(" + Op::join(values, ", ") + ")"; + } + + std::string JavaExporter::toString(scalar value) const { + if (Op::isNaN(value)) { + return "Double.NaN"; + } else if (Op::isInf(value)) { + return (value > 0 + ? "Double.POSITIVE_INFINITY" + : "Double.NEGATIVE_INFINITY"); + } + return Op::str(value); + } + + JavaExporter* JavaExporter::clone() const { + return new JavaExporter(*this); + } + +} diff --git a/fuzzylite/src/imex/RScriptExporter.cpp b/fuzzylite/src/imex/RScriptExporter.cpp new file mode 100644 index 0000000..5fe53c7 --- /dev/null +++ b/fuzzylite/src/imex/RScriptExporter.cpp @@ -0,0 +1,234 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/imex/RScriptExporter.h" + +#include "fl/Engine.h" +#include "fl/imex/FllExporter.h" +#include "fl/variable/InputVariable.h" +#include "fl/variable/OutputVariable.h" + +#include <fstream> +namespace fl { + + RScriptExporter::RScriptExporter() : Exporter(), + _minimumColor("yellow"), _maximumColor("red"), _contourColor("black") { } + + RScriptExporter::~RScriptExporter() { } + + std::string RScriptExporter::name() const { + return "RScriptExporter"; + } + + void RScriptExporter::setMinimumColor(const std::string& minimumColor) { + this->_minimumColor = minimumColor; + } + + std::string RScriptExporter::getMinimumColor() const { + return this->_minimumColor; + } + + void RScriptExporter::setMaximumColor(const std::string& maximumColor) { + this->_maximumColor = maximumColor; + } + + std::string RScriptExporter::getMaximumColor() const { + return _maximumColor; + } + + void RScriptExporter::setContourColor(const std::string& contourColor) { + this->_contourColor = contourColor; + } + + std::string RScriptExporter::getContourColor() const { + return this->_contourColor; + } + + RScriptExporter* RScriptExporter::clone() const { + return new RScriptExporter(*this); + } + + std::string RScriptExporter::toString(const Engine* engine) const { + if (engine->inputVariables().empty()) { + throw Exception("[exporter error] engine has no input variables to export the surface", FL_AT); + } + if (engine->outputVariables().empty()) { + throw Exception("[exporter error] engine has no output variables to export the surface", FL_AT); + } + InputVariable* a = engine->inputVariables().at(0); + InputVariable* b = engine->inputVariables().at(1 % engine->numberOfInputVariables()); + return toString(const_cast<Engine*> (engine), a, b, + 1024, FldExporter::AllVariables, engine->outputVariables()); + } + + std::string RScriptExporter::toString(Engine* engine, InputVariable* a, InputVariable* b, + int values, FldExporter::ScopeOfValues scope, + const std::vector<OutputVariable*>& outputVariables) const { + std::ostringstream writer; + writeScriptExportingDataFrame(engine, writer, a, b, values, scope, outputVariables); + return writer.str(); + } + + std::string RScriptExporter::toString(Engine* engine, InputVariable* a, InputVariable* b, + std::istream& reader, const std::vector<OutputVariable*>& outputVariables) const { + std::ostringstream writer; + writeScriptExportingDataFrame(engine, writer, a, b, reader, outputVariables); + return writer.str(); + } + + void RScriptExporter::toFile(const std::string& filePath, const Engine* engine) const { + if (engine->inputVariables().empty()) { + throw Exception("[exporter error] engine has no input variables to export the surface", FL_AT); + } + if (engine->outputVariables().empty()) { + throw Exception("[exporter error] engine has no output variables to export the surface", FL_AT); + } + InputVariable* a = engine->inputVariables().at(0); + InputVariable* b = engine->inputVariables().at(1 % engine->numberOfInputVariables()); + + toFile(filePath, const_cast<Engine*> (engine), a, b, + 1024, FldExporter::AllVariables, engine->outputVariables()); + } + + void RScriptExporter::toFile(const std::string& filePath, Engine* engine, + InputVariable* a, InputVariable* b, int values, FldExporter::ScopeOfValues scope, + const std::vector<OutputVariable*>& outputVariables) const { + std::ofstream writer(filePath.c_str()); + if (not writer.is_open()) { + throw Exception("[file error] file <" + filePath + "> could not be created", FL_AT); + } + writeScriptExportingDataFrame(engine, writer, a, b, values, scope, outputVariables); + writer.close(); + } + + void RScriptExporter::toFile(const std::string& filePath, Engine* engine, + InputVariable* a, InputVariable* b, std::istream& reader, + const std::vector<OutputVariable*>& outputVariables) const { + std::ofstream writer(filePath.c_str()); + if (not writer.is_open()) { + throw Exception("[file error] file <" + filePath + "> could not be created", FL_AT); + } + writeScriptExportingDataFrame(engine, writer, a, b, reader, outputVariables); + writer.close(); + } + + void RScriptExporter::writeScriptImportingDataFrame(const Engine* engine, std::ostream& writer, + InputVariable* a, InputVariable* b, const std::string& dfPath, + const std::vector<OutputVariable*>& outputVariables) const { + writeScriptHeader(writer, engine); + + writer << "engine.fldFile = \"" << dfPath << "\"\n"; + writer << "if (require(data.table)) {\n" + << " engine.df = data.table::fread(engine.fldFile, sep=\"auto\", header=\"auto\")\n" + << "} else {\n" + << " engine.df = read.table(engine.fldFile, header=TRUE)\n" + << "}\n"; + writer << "\n"; + + writeScriptPlots(writer, a, b, outputVariables); + } + + void RScriptExporter::writeScriptExportingDataFrame(Engine* engine, std::ostream& writer, + InputVariable* a, InputVariable* b, int values, FldExporter::ScopeOfValues scope, + const std::vector<OutputVariable*>& outputVariables) const { + writeScriptHeader(writer, engine); + + std::vector<InputVariable*> activeVariables = engine->inputVariables(); + for (std::size_t i = 0; i < activeVariables.size(); ++i) { + if (activeVariables.at(i) != a and activeVariables.at(i) != b) { + activeVariables.at(i) = fl::null; + } + } + writer << "engine.fld = \""; + FldExporter().write(engine, writer, values, scope, activeVariables); + writer << "\"\n\n"; + writer << "engine.df = read.delim(textConnection(engine.fld), header=TRUE, " + "sep=\" \", strip.white=TRUE)\n\n"; + + writeScriptPlots(writer, a, b, outputVariables); + } + + void RScriptExporter::writeScriptExportingDataFrame(Engine* engine, std::ostream& writer, + InputVariable* a, InputVariable* b, std::istream& reader, + const std::vector<OutputVariable*>& outputVariables) const { + writeScriptHeader(writer, engine); + + writer << "engine.fld = \""; + FldExporter().write(engine, writer, reader); + writer << "\"\n\n"; + + writer << "engine.df = read.delim(textConnection(engine.fld), header=TRUE, " + "sep=\" \", strip.white=TRUE)\n\n"; + + writeScriptPlots(writer, a, b, outputVariables); + } + + void RScriptExporter::writeScriptHeader(std::ostream& writer, const Engine* engine) const { + writer << "#Code automatically generated with " << fuzzylite::library() << ".\n\n" + << "library(ggplot2);\n" + << "\n"; + writer << "engine.name = \"" << engine->getName() << "\"\n"; + if (not engine->getDescription().empty()) + writer << "engine.description = \"" << engine->getDescription() << "\"\n"; + writer << "engine.fll = \"" << FllExporter().toString(engine) << "\"\n\n"; + } + + void RScriptExporter::writeScriptPlots(std::ostream& writer, + InputVariable* a, InputVariable* b, + const std::vector<OutputVariable*>& outputVariables) const { + std::ostringstream arrangeGrob; + arrangeGrob << "arrangeGrob("; + for (std::size_t i = 0; i < outputVariables.size(); ++i) { + OutputVariable* z = outputVariables.at(i); + if (a != b) { + writer << "engine.plot.i1i2_o" << (i + 1) << " = ggplot(engine.df, aes(" << a->getName() << ", " << b->getName() << ")) + \n" + << " geom_tile(aes(fill=" << z->getName() << ")) + \n" + << " scale_fill_gradient(low=\"" << _minimumColor << "\", high=\"" << _maximumColor << "\") + \n" + << " stat_contour(aes(x=" << a->getName() << ", y=" << b->getName() << ", z=" << z->getName() << "), color=\"" << _contourColor << "\") + \n" + << " ggtitle(\"(" << a->getName() << ", " << b->getName() << ") = " << z->getName() << "\")\n\n"; + + writer << "engine.plot.i2i1_o" << (i + 1) << " = ggplot(engine.df, aes(" << b->getName() << ", " << a->getName() << ")) + \n" + << " geom_tile(aes(fill=" << z->getName() << ")) + \n" + << " scale_fill_gradient(low=\"" << _minimumColor << "\", high=\"" << _maximumColor << "\") + \n" + << " stat_contour(aes(x=" << b->getName() << ", y=" << a->getName() << ", z=" << z->getName() << "), color=\"" << _contourColor << "\") + \n" + << " ggtitle(\"(" << b->getName() << ", " << a->getName() << ") = " << z->getName() << "\")\n\n"; + arrangeGrob << "engine.plot.i1i2_o" << (i + 1) << ", " << "engine.plot.i2i1_o" << (i + 1) << ", "; + } else { + writer << "engine.plot.i1_o" << (i + 1) << " = ggplot(engine.df, aes(" << a->getName() << ", " << z->getName() << ")) + \n" + << " geom_line(aes(color=" << z->getName() << "), size=3, lineend=\"round\", linejoin=\"mitre\") + \n" + << " scale_color_gradient(low=\"" << _minimumColor << "\", high=\"" << _maximumColor << "\") + \n" + << " ggtitle(\"" << a->getName() << " vs " << z->getName() << "\")\n\n"; + + writer << "engine.plot.o" << (i + 1) << "_i1 = ggplot(engine.df, aes(" << a->getName() << ", " << z->getName() << ")) + \n" + << " geom_line(aes(color=" << z->getName() << "), size=3, lineend=\"round\", linejoin=\"mitre\") + \n" + << " scale_color_gradient(low=\"" << _minimumColor << "\", high=\"" << _maximumColor << "\") + \n" + << " coord_flip() + \n" + << " ggtitle(\"" << z->getName() << " vs " << a->getName() << "\")\n\n"; + arrangeGrob << "engine.plot.i1_o" << (i + 1) << ", " << "engine.plot.o" << (i + 1) << "_i1, "; + } + + } + arrangeGrob << "ncol=2, top=engine.name)"; + writer << "if (require(gridExtra)) {\n" + << " engine.plots = " << arrangeGrob.str() << "\n" + << " ggsave(paste0(engine.name, \".pdf\"), engine.plots)\n" + << " if (require(grid)) {\n" + << " grid.newpage()\n" + << " grid.draw(engine.plots)\n" + << " }\n" + << "}\n"; + } +} diff --git a/fuzzylite/src/main.cpp b/fuzzylite/src/main.cpp new file mode 100644 index 0000000..06ca139 --- /dev/null +++ b/fuzzylite/src/main.cpp @@ -0,0 +1,50 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/Headers.h" + +#include <fstream> +#include <csignal> + +int main(int argc, const char* argv[]) { + std::set_terminate(fl::Exception::terminate); + std::set_unexpected(fl::Exception::terminate); + ::signal(SIGSEGV, fl::Exception::signalHandler); + ::signal(SIGABRT, fl::Exception::signalHandler); + ::signal(SIGILL, fl::Exception::signalHandler); + ::signal(SIGSEGV, fl::Exception::signalHandler); + ::signal(SIGFPE, fl::Exception::signalHandler); +#ifdef FL_UNIX + ::signal(SIGBUS, fl::Exception::signalHandler); + ::signal(SIGPIPE, fl::Exception::signalHandler); +#endif +#ifdef FL_WINDOWS + //SetConsoleCtrlHandler(flSignalHandler, TRUE); +#endif + fl::fuzzylite::setDebugging(false); + + try { + fl::Console::main(argc, argv); + } catch (std::exception& ex) { + std::cout << ex.what() << "\nBACKTRACE:\n" << + fl::Exception::btCallStack() << std::endl; + return EXIT_FAILURE; + } + return EXIT_SUCCESS; +} + + + diff --git a/fuzzylite/src/norm/s/AlgebraicSum.cpp b/fuzzylite/src/norm/s/AlgebraicSum.cpp new file mode 100644 index 0000000..0497f23 --- /dev/null +++ b/fuzzylite/src/norm/s/AlgebraicSum.cpp @@ -0,0 +1,41 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/norm/s/AlgebraicSum.h" + +namespace fl { + + std::string AlgebraicSum::className() const { + return "AlgebraicSum"; + } + + Complexity AlgebraicSum::complexity() const { + return Complexity().arithmetic(3); + } + + scalar AlgebraicSum::compute(scalar a, scalar b) const { + return a + b - (a * b); + } + + AlgebraicSum* AlgebraicSum::clone() const { + return new AlgebraicSum(*this); + } + + SNorm* AlgebraicSum::constructor() { + return new AlgebraicSum; + } + +} diff --git a/fuzzylite/src/norm/s/BoundedSum.cpp b/fuzzylite/src/norm/s/BoundedSum.cpp new file mode 100644 index 0000000..b1cb42b --- /dev/null +++ b/fuzzylite/src/norm/s/BoundedSum.cpp @@ -0,0 +1,43 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/norm/s/BoundedSum.h" + +#include "fl/Operation.h" + +namespace fl { + + std::string BoundedSum::className() const { + return "BoundedSum"; + } + + Complexity BoundedSum::complexity() const { + return Complexity().arithmetic(1).function(1); + } + + scalar BoundedSum::compute(scalar a, scalar b) const { + return Op::min(scalar(1.0), a + b); + } + + BoundedSum* BoundedSum::clone() const { + return new BoundedSum(*this); + } + + SNorm* BoundedSum::constructor() { + return new BoundedSum; + } + +} diff --git a/fuzzylite/src/norm/s/DrasticSum.cpp b/fuzzylite/src/norm/s/DrasticSum.cpp new file mode 100644 index 0000000..b0d9f1e --- /dev/null +++ b/fuzzylite/src/norm/s/DrasticSum.cpp @@ -0,0 +1,46 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/norm/s/DrasticSum.h" + +#include "fl/Operation.h" + +namespace fl { + + std::string DrasticSum::className() const { + return "DrasticSum"; + } + + Complexity DrasticSum::complexity() const { + return Complexity().comparison(1).function(2); + } + + scalar DrasticSum::compute(scalar a, scalar b) const { + if (Op::isEq(Op::min(a, b), 0.0)) { + return Op::max(a, b); + } + return 1.0; + } + + DrasticSum* DrasticSum::clone() const { + return new DrasticSum(*this); + } + + SNorm* DrasticSum::constructor() { + return new DrasticSum; + } + +} diff --git a/fuzzylite/src/norm/s/EinsteinSum.cpp b/fuzzylite/src/norm/s/EinsteinSum.cpp new file mode 100644 index 0000000..a638c7b --- /dev/null +++ b/fuzzylite/src/norm/s/EinsteinSum.cpp @@ -0,0 +1,41 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/norm/s/EinsteinSum.h" + +namespace fl { + + std::string EinsteinSum::className() const { + return "EinsteinSum"; + } + + Complexity EinsteinSum::complexity() const { + return Complexity().arithmetic(4); + } + + scalar EinsteinSum::compute(scalar a, scalar b) const { + return (a + b) / (1.0 + a * b); + } + + EinsteinSum* EinsteinSum::clone() const { + return new EinsteinSum(*this); + } + + SNorm* EinsteinSum::constructor() { + return new EinsteinSum; + } + +} diff --git a/fuzzylite/src/norm/s/HamacherSum.cpp b/fuzzylite/src/norm/s/HamacherSum.cpp new file mode 100644 index 0000000..41242ee --- /dev/null +++ b/fuzzylite/src/norm/s/HamacherSum.cpp @@ -0,0 +1,44 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/norm/s/HamacherSum.h" + +#include "fl/Operation.h" + +namespace fl { + + std::string HamacherSum::className() const { + return "HamacherSum"; + } + + Complexity HamacherSum::complexity() const { + return Complexity().arithmetic(7); + } + + scalar HamacherSum::compute(scalar a, scalar b) const { + if (Op::isEq(a * b, 1.0)) return 1.0; + return (a + b - 2.0 * a * b) / (1.0 - a * b); + } + + HamacherSum* HamacherSum::clone() const { + return new HamacherSum(*this); + } + + SNorm* HamacherSum::constructor() { + return new HamacherSum; + } + +} diff --git a/fuzzylite/src/norm/s/Maximum.cpp b/fuzzylite/src/norm/s/Maximum.cpp new file mode 100644 index 0000000..5277d49 --- /dev/null +++ b/fuzzylite/src/norm/s/Maximum.cpp @@ -0,0 +1,43 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/norm/s/Maximum.h" + +#include "fl/Operation.h" + +namespace fl { + + std::string Maximum::className() const { + return "Maximum"; + } + + Complexity Maximum::complexity() const { + return Complexity().function(1); + } + + scalar Maximum::compute(scalar a, scalar b) const { + return Op::max(a, b); + } + + Maximum* Maximum::clone() const { + return new Maximum(*this); + } + + SNorm* Maximum::constructor() { + return new Maximum; + } + +} diff --git a/fuzzylite/src/norm/s/NilpotentMaximum.cpp b/fuzzylite/src/norm/s/NilpotentMaximum.cpp new file mode 100644 index 0000000..7c63999 --- /dev/null +++ b/fuzzylite/src/norm/s/NilpotentMaximum.cpp @@ -0,0 +1,46 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/norm/s/NilpotentMaximum.h" + +#include "fl/Operation.h" + +namespace fl { + + std::string NilpotentMaximum::className() const { + return "NilpotentMaximum"; + } + + Complexity NilpotentMaximum::complexity() const { + return Complexity().comparison(1).arithmetic(1).function(1); + } + + scalar NilpotentMaximum::compute(scalar a, scalar b) const { + if (Op::isLt(a + b, 1.0)) { + return Op::max(a, b); + } + return 1.0; + } + + NilpotentMaximum* NilpotentMaximum::clone() const { + return new NilpotentMaximum(*this); + } + + SNorm* NilpotentMaximum::constructor() { + return new NilpotentMaximum; + } + +} diff --git a/fuzzylite/src/norm/s/NormalizedSum.cpp b/fuzzylite/src/norm/s/NormalizedSum.cpp new file mode 100644 index 0000000..c420ed1 --- /dev/null +++ b/fuzzylite/src/norm/s/NormalizedSum.cpp @@ -0,0 +1,43 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/norm/s/NormalizedSum.h" + +#include "fl/Operation.h" + +namespace fl { + + std::string NormalizedSum::className() const { + return "NormalizedSum"; + } + + Complexity NormalizedSum::complexity() const { + return Complexity().arithmetic(3).function(1); + } + + scalar NormalizedSum::compute(scalar a, scalar b) const { + return (a + b) / Op::max(scalar(1.0), a + b); + } + + NormalizedSum* NormalizedSum::clone() const { + return new NormalizedSum(*this); + } + + SNorm* NormalizedSum::constructor() { + return new NormalizedSum; + } + +} diff --git a/fuzzylite/src/norm/s/SNormFunction.cpp b/fuzzylite/src/norm/s/SNormFunction.cpp new file mode 100644 index 0000000..f8d9e42 --- /dev/null +++ b/fuzzylite/src/norm/s/SNormFunction.cpp @@ -0,0 +1,65 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/norm/s/SNormFunction.h" + +namespace fl { + + SNormFunction::SNormFunction(const std::string& formula) : SNorm() { + _function.variables["a"] = fl::nan; + _function.variables["b"] = fl::nan; + if (not formula.empty()) { + _function.load(formula); + } + } + + std::string SNormFunction::className() const { + return "SNormFunction"; + } + + Complexity SNormFunction::complexity() const { + if (_function.root()) + return _function.complexity().function(2 * std::log(scalar(_function.variables.size()))); + return _function.complexity(); + } + + scalar SNormFunction::compute(scalar a, scalar b) const { + _function.variables["a"] = a; + _function.variables["b"] = b; + return _function.evaluate(); + } + + Function& SNormFunction::function() { + return this->_function; + } + + void SNormFunction::setFormula(const std::string& formula) { + this->_function.load(formula); + } + + std::string SNormFunction::getFormula() const { + return _function.getFormula(); + } + + SNormFunction* SNormFunction::clone() const { + return new SNormFunction(*this); + } + + SNorm* SNormFunction::constructor() { + return new SNormFunction; + } + +} diff --git a/fuzzylite/src/norm/s/UnboundedSum.cpp b/fuzzylite/src/norm/s/UnboundedSum.cpp new file mode 100644 index 0000000..adee6ad --- /dev/null +++ b/fuzzylite/src/norm/s/UnboundedSum.cpp @@ -0,0 +1,43 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/norm/s/UnboundedSum.h" + +#include "fl/Operation.h" + +namespace fl { + + std::string UnboundedSum::className() const { + return "UnboundedSum"; + } + + Complexity UnboundedSum::complexity() const { + return Complexity().arithmetic(1); + } + + scalar UnboundedSum::compute(scalar a, scalar b) const { + return a + b; + } + + UnboundedSum* UnboundedSum::clone() const { + return new UnboundedSum(*this); + } + + SNorm* UnboundedSum::constructor() { + return new UnboundedSum; + } + +} diff --git a/fuzzylite/src/norm/t/AlgebraicProduct.cpp b/fuzzylite/src/norm/t/AlgebraicProduct.cpp new file mode 100644 index 0000000..eefe309 --- /dev/null +++ b/fuzzylite/src/norm/t/AlgebraicProduct.cpp @@ -0,0 +1,41 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/norm/t/AlgebraicProduct.h" + +namespace fl { + + std::string AlgebraicProduct::className() const { + return "AlgebraicProduct"; + } + + Complexity AlgebraicProduct::complexity() const { + return Complexity().arithmetic(1); + } + + scalar AlgebraicProduct::compute(scalar a, scalar b) const { + return a * b; + } + + AlgebraicProduct* AlgebraicProduct::clone() const { + return new AlgebraicProduct(*this); + } + + TNorm* AlgebraicProduct::constructor() { + return new AlgebraicProduct; + } + +} diff --git a/fuzzylite/src/norm/t/BoundedDifference.cpp b/fuzzylite/src/norm/t/BoundedDifference.cpp new file mode 100644 index 0000000..3775aa4 --- /dev/null +++ b/fuzzylite/src/norm/t/BoundedDifference.cpp @@ -0,0 +1,43 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/norm/t/BoundedDifference.h" + +#include "fl/Operation.h" + +namespace fl { + + std::string BoundedDifference::className() const { + return "BoundedDifference"; + } + + Complexity BoundedDifference::complexity() const { + return Complexity().arithmetic(2).function(1); + } + + scalar BoundedDifference::compute(scalar a, scalar b) const { + return Op::max(scalar(0.0), a + b - scalar(1.0)); + } + + BoundedDifference* BoundedDifference::clone() const { + return new BoundedDifference(*this); + } + + TNorm* BoundedDifference::constructor() { + return new BoundedDifference; + } + +} diff --git a/fuzzylite/src/norm/t/DrasticProduct.cpp b/fuzzylite/src/norm/t/DrasticProduct.cpp new file mode 100644 index 0000000..5bfdc87 --- /dev/null +++ b/fuzzylite/src/norm/t/DrasticProduct.cpp @@ -0,0 +1,46 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/norm/t/DrasticProduct.h" + +#include "fl/Operation.h" + +namespace fl { + + std::string DrasticProduct::className() const { + return "DrasticProduct"; + } + + Complexity DrasticProduct::complexity() const { + return Complexity().comparison(1).function(2); + } + + scalar DrasticProduct::compute(scalar a, scalar b) const { + if (Op::isEq(Op::max(a, b), 1.0)) { + return Op::min(a, b); + } + return 0.0; + } + + DrasticProduct* DrasticProduct::clone() const { + return new DrasticProduct(*this); + } + + TNorm* DrasticProduct::constructor() { + return new DrasticProduct; + } + +} diff --git a/fuzzylite/src/norm/t/EinsteinProduct.cpp b/fuzzylite/src/norm/t/EinsteinProduct.cpp new file mode 100644 index 0000000..bbb3966 --- /dev/null +++ b/fuzzylite/src/norm/t/EinsteinProduct.cpp @@ -0,0 +1,41 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/norm/t/EinsteinProduct.h" + +namespace fl { + + std::string EinsteinProduct::className() const { + return "EinsteinProduct"; + } + + Complexity EinsteinProduct::complexity() const { + return Complexity().arithmetic(6); + } + + scalar EinsteinProduct::compute(scalar a, scalar b) const { + return (a * b) / (2.0 - (a + b - a * b)); + } + + EinsteinProduct* EinsteinProduct::clone() const { + return new EinsteinProduct(*this); + } + + TNorm* EinsteinProduct::constructor() { + return new EinsteinProduct; + } + +} diff --git a/fuzzylite/src/norm/t/HamacherProduct.cpp b/fuzzylite/src/norm/t/HamacherProduct.cpp new file mode 100644 index 0000000..9416084 --- /dev/null +++ b/fuzzylite/src/norm/t/HamacherProduct.cpp @@ -0,0 +1,44 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/norm/t/HamacherProduct.h" + +#include "fl/Operation.h" + +namespace fl { + + std::string HamacherProduct::className() const { + return "HamacherProduct"; + } + + Complexity HamacherProduct::complexity() const { + return Complexity().arithmetic(5); + } + + scalar HamacherProduct::compute(scalar a, scalar b) const { + if (Op::isEq(a + b, 0.0)) return 0.0; + return (a * b) / (a + b - a * b); + } + + HamacherProduct* HamacherProduct::clone() const { + return new HamacherProduct(*this); + } + + TNorm* HamacherProduct::constructor() { + return new HamacherProduct; + } + +} diff --git a/fuzzylite/src/norm/t/Minimum.cpp b/fuzzylite/src/norm/t/Minimum.cpp new file mode 100644 index 0000000..1a63658 --- /dev/null +++ b/fuzzylite/src/norm/t/Minimum.cpp @@ -0,0 +1,44 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/norm/t/Minimum.h" + +#include "fl/Operation.h" + +namespace fl { + + std::string Minimum::className() const { + return "Minimum"; + } + + Complexity Minimum::complexity() const { + return Complexity().function(1); + } + + scalar Minimum::compute(scalar a, scalar b) const { + return Op::min(a, b); + } + + Minimum* Minimum::clone() const { + return new Minimum(*this); + } + + TNorm* Minimum::constructor() { + return new Minimum; + } + + +} diff --git a/fuzzylite/src/norm/t/NilpotentMinimum.cpp b/fuzzylite/src/norm/t/NilpotentMinimum.cpp new file mode 100644 index 0000000..7ab906b --- /dev/null +++ b/fuzzylite/src/norm/t/NilpotentMinimum.cpp @@ -0,0 +1,47 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/norm/t/NilpotentMinimum.h" + +#include "fl/Operation.h" + +namespace fl { + + std::string NilpotentMinimum::className() const { + return "NilpotentMinimum"; + } + + Complexity NilpotentMinimum::complexity() const { + return Complexity().comparison(1).arithmetic(1).function(1); + } + + scalar NilpotentMinimum::compute(scalar a, scalar b) const { + if (Op::isGt(a + b, 1.0)) { + return Op::min(a, b); + } + return 0.0; + } + + NilpotentMinimum* NilpotentMinimum::clone() const { + return new NilpotentMinimum(*this); + } + + TNorm* NilpotentMinimum::constructor() { + return new NilpotentMinimum; + } + + +} diff --git a/fuzzylite/src/norm/t/TNormFunction.cpp b/fuzzylite/src/norm/t/TNormFunction.cpp new file mode 100644 index 0000000..1e0563e --- /dev/null +++ b/fuzzylite/src/norm/t/TNormFunction.cpp @@ -0,0 +1,66 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/norm/t/TNormFunction.h" + +namespace fl { + + TNormFunction::TNormFunction(const std::string& formula) : TNorm() { + _function.variables["a"] = fl::nan; + _function.variables["b"] = fl::nan; + if (not formula.empty()) { + _function.load(formula); + } + } + + std::string TNormFunction::className() const { + return "TNormFunction"; + } + + Complexity TNormFunction::complexity() const { + if (_function.root()) + return _function.complexity().function(2 * std::log(scalar(_function.variables.size()))); + return _function.complexity(); + } + + scalar TNormFunction::compute(scalar a, scalar b) const { + _function.variables["a"] = a; + _function.variables["b"] = b; + return _function.evaluate(); + } + + Function& TNormFunction::function() { + return this->_function; + } + + void TNormFunction::setFormula(const std::string& formula) { + this->_function.load(formula); + } + + std::string TNormFunction::getFormula() const { + return _function.getFormula(); + } + + TNormFunction* TNormFunction::clone() const { + return new TNormFunction(*this); + } + + TNorm* TNormFunction::constructor() { + return new TNormFunction; + } + + +} diff --git a/fuzzylite/src/rule/Antecedent.cpp b/fuzzylite/src/rule/Antecedent.cpp new file mode 100644 index 0000000..5f0b0be --- /dev/null +++ b/fuzzylite/src/rule/Antecedent.cpp @@ -0,0 +1,447 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/rule/Antecedent.h" + +#include "fl/Engine.h" +#include "fl/factory/HedgeFactory.h" +#include "fl/factory/FactoryManager.h" +#include "fl/hedge/Any.h" +#include "fl/rule/Expression.h" +#include "fl/rule/Rule.h" +#include "fl/term/Aggregated.h" +#include "fl/variable/InputVariable.h" +#include "fl/variable/OutputVariable.h" + +#include <stack> + +namespace fl { + + Antecedent::Antecedent() + : _text(""), _expression(fl::null) { } + + Antecedent::~Antecedent() { + _expression.reset(fl::null); + } + + void Antecedent::setText(const std::string& text) { + this->_text = text; + } + + std::string Antecedent::getText() const { + return this->_text; + } + + Expression* Antecedent::getExpression() const { + return this->_expression.get(); + } + + void Antecedent::setExpression(Expression* expression) { + this->_expression.reset(expression); + } + + bool Antecedent::isLoaded() const { + return _expression.get() != fl::null; + } + + scalar Antecedent::activationDegree(const TNorm* conjunction, const SNorm* disjunction) const { + return this->activationDegree(conjunction, disjunction, _expression.get()); + } + + scalar Antecedent::activationDegree(const TNorm* conjunction, const SNorm* disjunction, + const Expression* node) const { + if (not isLoaded()) { + throw Exception("[antecedent error] antecedent <" + getText() + "> is not loaded", FL_AT); + } + const Expression::Type expression = node->type(); + if (expression == Expression::Proposition) { + const Proposition* proposition = static_cast<const Proposition*> (node); + if (not proposition->variable->isEnabled()) { + return 0.0; + } + + if (not proposition->hedges.empty()) { + //if last hedge is "Any", apply hedges in reverse order and return degree + std::vector<Hedge*>::const_reverse_iterator rit = proposition->hedges.rbegin(); + if (dynamic_cast<Any*> (*rit)) { + scalar result = (*rit)->hedge(fl::nan); + while (++rit != proposition->hedges.rend()) { + result = (*rit)->hedge(result); + } + return result; + } + } + scalar result = fl::nan; + Variable::Type variableType = proposition->variable->type(); + if (variableType == Variable::Input) { + result = proposition->term->membership(proposition->variable->getValue()); + } else if (variableType == Variable::Output) { + result = static_cast<OutputVariable*> (proposition->variable) + ->fuzzyOutput()->activationDegree(proposition->term); + } + + if (not proposition->hedges.empty()) { + for (std::vector<Hedge*>::const_reverse_iterator rit = proposition->hedges.rbegin(); + rit != proposition->hedges.rend(); ++rit) { + result = (*rit)->hedge(result); + } + } + return result; + } + //if node is an operator + if (expression == Expression::Operator) { + const Operator* fuzzyOperator = static_cast<const Operator*> (node); + if (not (fuzzyOperator->left and fuzzyOperator->right)) { + std::ostringstream ex; + ex << "[syntax error] left and right operands must exist"; + throw Exception(ex.str(), FL_AT); + } + if (fuzzyOperator->name == Rule::andKeyword()) { + if (not conjunction) throw Exception("[conjunction error] " + "the following rule requires a conjunction operator:\n" + _text, FL_AT); + return conjunction->compute( + this->activationDegree(conjunction, disjunction, fuzzyOperator->left), + this->activationDegree(conjunction, disjunction, fuzzyOperator->right)); + } + + if (fuzzyOperator->name == Rule::orKeyword()) { + if (not disjunction) throw Exception("[disjunction error] " + "the following rule requires a disjunction operator:\n" + _text, FL_AT); + return disjunction->compute( + this->activationDegree(conjunction, disjunction, fuzzyOperator->left), + this->activationDegree(conjunction, disjunction, fuzzyOperator->right)); + } + std::ostringstream ex; + ex << "[syntax error] operator <" << fuzzyOperator->name << "> not recognized"; + throw Exception(ex.str(), FL_AT); + + } else { + std::ostringstream ss; + ss << "[antecedent error] expected a Proposition or Operator, but found <"; + if (node) ss << node->toString(); + ss << ">"; + throw Exception(ss.str(), FL_AT); + } + } + + + Complexity Antecedent::complexity(const TNorm* conjunction, const SNorm* disjunction) const { + return complexity(conjunction, disjunction, _expression.get()); + } + + Complexity Antecedent::complexity(const TNorm* conjunction, const SNorm* disjunction, + const Expression* node) const { + if (not isLoaded()) { + return Complexity(); + } + + Complexity result; + const Expression::Type expression = node->type(); + if (expression == Expression::Proposition) { + const Proposition* proposition = static_cast<const Proposition*> (node); + if (not proposition->variable->isEnabled()) { + return result; + } + + if (not proposition->hedges.empty()) { + //if last hedge is "Any", apply hedges in reverse order and return degree + std::vector<Hedge*>::const_reverse_iterator rit = proposition->hedges.rbegin(); + if (dynamic_cast<Any*> (*rit)) { + result += (*rit)->complexity(); + while (++rit != proposition->hedges.rend()) { + result = (*rit)->complexity(); + } + return result; + } + } + Variable::Type variableType = proposition->variable->type(); + if (variableType == Variable::Input) { + result += proposition->term->complexity(); + } else if (variableType == Variable::Output) { + OutputVariable* outputVariable = static_cast<OutputVariable*> (proposition->variable); + result += outputVariable->fuzzyOutput()->complexityOfActivationDegree(); + } + + if (not proposition->hedges.empty()) { + for (std::vector<Hedge*>::const_reverse_iterator rit = proposition->hedges.rbegin(); + rit != proposition->hedges.rend(); ++rit) { + result += (*rit)->complexity(); + } + } + return result; + } + //if node is an operator + if (expression == Expression::Operator) { + const Operator* fuzzyOperator = static_cast<const Operator*> (node); + if (not (fuzzyOperator->left and fuzzyOperator->right)) { + std::ostringstream ex; + ex << "[syntax error] left and right operands must exist"; + throw Exception(ex.str(), FL_AT); + } + if (fuzzyOperator->name == Rule::andKeyword()) { + if (conjunction) { + result += conjunction->complexity(); + } + result += complexity(conjunction, disjunction, fuzzyOperator->left) + + complexity(conjunction, disjunction, fuzzyOperator->right); + return result; + } + + if (fuzzyOperator->name == Rule::orKeyword()) { + if (disjunction) { + result += disjunction->complexity(); + } + result += complexity(conjunction, disjunction, fuzzyOperator->left) + + complexity(conjunction, disjunction, fuzzyOperator->right); + return result; + } + } + return Complexity(); + } + + void Antecedent::unload() { + _expression.reset(fl::null); + } + + void Antecedent::load(const Engine* engine) { + load(getText(), engine); + } + + void Antecedent::load(const std::string& antecedent, const Engine* engine) { + FL_DBG("Antecedent: " << antecedent); + unload(); + setText(antecedent); + if (Op::trim(antecedent).empty()) { + throw Exception("[syntax error] antecedent is empty", FL_AT); + } + /* + Builds an proposition tree from the antecedent of a fuzzy rule. + The rules are: + 1) After a variable comes 'is', + 2) After 'is' comes a hedge or a term + 3) After a hedge comes a hedge or a term + 4) After a term comes a variable or an operator + */ + + Function function; + + std::string postfix = function.toPostfix(antecedent); + FL_DBG("Postfix: " << postfix); + std::stringstream tokenizer(postfix); + std::string token; + + enum FSM { + S_VARIABLE = 1, S_IS = 2, S_HEDGE = 4, S_TERM = 8, S_AND_OR = 16 + }; + int state = S_VARIABLE; + std::stack<Expression*> expressionStack; + Proposition* proposition = fl::null; + try { + while (tokenizer >> token) { + if (state bitand S_VARIABLE) { + Variable* variable = fl::null; + if (engine->hasInputVariable(token)) + variable = engine->getInputVariable(token); + else if (engine->hasOutputVariable(token)) + variable = engine->getOutputVariable(token); + if (variable) { + proposition = new Proposition; + proposition->variable = variable; + expressionStack.push(proposition); + + state = S_IS; + FL_DBG("Token <" << token << "> is variable"); + continue; + } + } + + if (state bitand S_IS) { + if (token == Rule::isKeyword()) { + state = S_HEDGE bitor S_TERM; + FL_DBG("Token <" << token << "> is keyword"); + continue; + } + } + + if (state bitand S_HEDGE) { + HedgeFactory* factory = FactoryManager::instance()->hedge(); + if (factory->hasConstructor(token)) { + Hedge* hedge = factory->constructObject(token); + proposition->hedges.push_back(hedge); + if (dynamic_cast<Any*> (hedge)) { + state = S_VARIABLE bitor S_AND_OR; + } else { + state = S_HEDGE bitor S_TERM; + } + FL_DBG("Token <" << token << "> is hedge"); + continue; + } + } + + if (state bitand S_TERM) { + if (proposition->variable->hasTerm(token)) { + proposition->term = proposition->variable->getTerm(token); + state = S_VARIABLE bitor S_AND_OR; + FL_DBG("Token <" << token << "> is term"); + continue; + } + } + + if (state bitand S_AND_OR) { + if (token == Rule::andKeyword() or token == Rule::orKeyword()) { + if (expressionStack.size() < 2) { + std::ostringstream ex; + ex << "[syntax error] logical operator <" << token << "> expects two operands," + << "but found <" << expressionStack.size() << "> in antecedent"; + throw Exception(ex.str(), FL_AT); + } + Operator* fuzzyOperator = new Operator; + fuzzyOperator->name = token; + fuzzyOperator->right = expressionStack.top(); + expressionStack.pop(); + fuzzyOperator->left = expressionStack.top(); + expressionStack.pop(); + expressionStack.push(fuzzyOperator); + + state = S_VARIABLE bitor S_AND_OR; + FL_DBG("Subtree: " << fuzzyOperator->toString() << + "(" << fuzzyOperator->left->toString() << ") " << + "(" << fuzzyOperator->right->toString() << ")"); + continue; + } + } + + //If reached this point, there was an error + if ((state bitand S_VARIABLE) or (state bitand S_AND_OR)) { + std::ostringstream ex; + ex << "[syntax error] antecedent expected variable or logical operator, but found <" << token << ">"; + throw Exception(ex.str(), FL_AT); + } + if (state bitand S_IS) { + std::ostringstream ex; + ex << "[syntax error] antecedent expected keyword <" << Rule::isKeyword() << ">, but found <" << token << ">"; + throw Exception(ex.str(), FL_AT); + } + if ((state bitand S_HEDGE) or (state bitand S_TERM)) { + std::ostringstream ex; + ex << "[syntax error] antecedent expected hedge or term, but found <" << token << ">"; + throw Exception(ex.str(), FL_AT); + } + std::ostringstream ex; + ex << "[syntax error] unexpected token <" << token << "> in antecedent"; + throw Exception(ex.str(), FL_AT); + } + + if (not ((state bitand S_VARIABLE) or (state bitand S_AND_OR))) { //only acceptable final state + if (state bitand S_IS) { + std::ostringstream ex; + ex << "[syntax error] antecedent expected keyword <" << Rule::isKeyword() << "> after <" << token << ">"; + throw Exception(ex.str(), FL_AT); + } + if ((state bitand S_HEDGE) or (state bitand S_TERM)) { + std::ostringstream ex; + ex << "[syntax error] antecedent expected hedge or term after <" << token << ">"; + throw Exception(ex.str(), FL_AT); + } + } + + if (expressionStack.size() != 1) { + std::vector<std::string> errors; + while (expressionStack.size() > 1) { + Expression* expression = expressionStack.top(); + expressionStack.pop(); + errors.push_back(expression->toString()); + delete expression; + } + std::ostringstream ex; + ex << "[syntax error] unable to parse the following expressions in antecedent <" + << Op::join(errors, " ") << ">"; + throw Exception(ex.str(), FL_AT); + } + } catch (...) { + for (std::size_t i = 0; i < expressionStack.size(); ++i) { + delete expressionStack.top(); + expressionStack.pop(); + } + throw; + } + setExpression(expressionStack.top()); + } + + std::string Antecedent::toString() const { + return toInfix(getExpression()); + } + + std::string Antecedent::toPrefix(const Expression* node) const { + if (not isLoaded()) { + throw Exception("[antecedent error] antecedent <" + _text + "> is not loaded", FL_AT); + } + if (not node) node = getExpression(); + + if (dynamic_cast<const Proposition*> (node)) { + return node->toString(); + } + std::stringstream ss; + if (const Operator * fuzzyOperator = dynamic_cast<const Operator*> (node)) { + ss << fuzzyOperator->toString() << " " + << toPrefix(fuzzyOperator->left) << " " + << toPrefix(fuzzyOperator->right) << " "; + } else { + ss << "[antecedent error] unknown class of Expression <" << (node ? node->toString() : "null") << ">"; + } + return ss.str(); + } + + std::string Antecedent::toInfix(const Expression* node) const { + if (not isLoaded()) { + throw Exception("[antecedent error] antecedent <" + _text + "> is not loaded", FL_AT); + } + if (not node) node = getExpression(); + if (dynamic_cast<const Proposition*> (node)) { + return node->toString(); + } + std::stringstream ss; + if (const Operator * fuzzyOperator = dynamic_cast<const Operator*> (node)) { + ss << toInfix(fuzzyOperator->left) << " " + << fuzzyOperator->toString() << " " + << toInfix(fuzzyOperator->right) << " "; + } else { + ss << "[antecedent error] unknown class of Expression <" << (node ? node->toString() : "null") << ">"; + } + return ss.str(); + } + + std::string Antecedent::toPostfix(const Expression* node) const { + if (not isLoaded()) { + throw Exception("[antecedent error] antecedent <" + _text + "> is not loaded", FL_AT); + } + if (not node) node = getExpression(); + if (dynamic_cast<const Proposition*> (node)) { + return node->toString(); + } + std::stringstream ss; + if (const Operator * fuzzyOperator = dynamic_cast<const Operator*> (node)) { + ss << toPostfix(fuzzyOperator->left) << " " + << toPostfix(fuzzyOperator->right) << " " + << fuzzyOperator->toString() << " "; + } else { + ss << "[antecedent error] unknown class of Expression <" << (node ? node->toString() : "null") << ">"; + } + return ss.str(); + } + + +} diff --git a/fuzzylite/src/rule/Consequent.cpp b/fuzzylite/src/rule/Consequent.cpp new file mode 100644 index 0000000..a90b1a9 --- /dev/null +++ b/fuzzylite/src/rule/Consequent.cpp @@ -0,0 +1,244 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/rule/Consequent.h" + +#include "fl/Engine.h" +#include "fl/factory/HedgeFactory.h" +#include "fl/factory/FactoryManager.h" +#include "fl/hedge/Any.h" +#include "fl/rule/Expression.h" +#include "fl/rule/Rule.h" +#include "fl/term/Aggregated.h" +#include "fl/variable/OutputVariable.h" + +namespace fl { + + Consequent::Consequent() { } + + Consequent::~Consequent() { + for (std::size_t i = 0; i < _conclusions.size(); ++i) { + delete _conclusions.at(i); + } + _conclusions.clear(); + } + + std::string Consequent::getText() const { + return this->_text; + } + + void Consequent::setText(const std::string& text) { + this->_text = text; + } + + const std::vector<Proposition*>& Consequent::conclusions() const { + return this->_conclusions; + } + + std::vector<Proposition*>& Consequent::conclusions() { + return this->_conclusions; + } + + Complexity Consequent::complexity(const TNorm* implication) const { + Complexity result; + result.comparison(1); + + for (std::size_t i = 0; i < _conclusions.size(); ++i) { + Proposition* proposition = _conclusions.at(i); + result.comparison(2); + for (std::size_t h = 0; h < proposition->hedges.size(); ++h) { + result += proposition->hedges.at(h)->complexity(); + } + result += static_cast<OutputVariable*> (proposition->variable) + ->complexity(Activated(proposition->term, fl::nan, implication)); + } + return result; + } + + void Consequent::modify(scalar activationDegree, const TNorm* implication) { + if (not isLoaded()) { + throw Exception("[consequent error] consequent <" + getText() + "> is not loaded", FL_AT); + } + for (std::size_t i = 0; i < _conclusions.size(); ++i) { + Proposition* proposition = _conclusions.at(i); + if (proposition->variable->isEnabled()) { + if (not proposition->hedges.empty()) { + for (std::vector<Hedge*>::const_reverse_iterator rit = proposition->hedges.rbegin(); + rit != proposition->hedges.rend(); ++rit) { + activationDegree = (*rit)->hedge(activationDegree); + } + } + + static_cast<OutputVariable*> (proposition->variable)->fuzzyOutput() + ->addTerm(proposition->term, activationDegree, implication); + } + } + } + + bool Consequent::isLoaded() { + return not _conclusions.empty(); + } + + void Consequent::unload() { + for (std::size_t i = 0; i < _conclusions.size(); ++i) { + delete _conclusions.at(i); + } + _conclusions.clear(); + } + + void Consequent::load(const Engine* engine) { + load(getText(), engine); + } + + void Consequent::load(const std::string& consequent, const Engine* engine) { + unload(); + setText(consequent); + + if (Op::trim(consequent).empty()) { + throw Exception("[syntax error] consequent is empty", FL_AT); + } + + /** + Extracts the list of propositions from the consequent + The rules are: + 1) After a variable comes 'is' or '=', + 2) After 'is' comes a hedge or a term + 3) After a hedge comes a hedge or a term + 4) After a term comes operators 'and' or 'with' + 5) After operator 'and' comes a variable + 6) After operator 'with' comes a float + */ + enum FSM { + S_VARIABLE = 1, S_IS = 2, S_HEDGE = 4, S_TERM = 8, + S_AND = 16, S_WITH = 32 + }; + int state = S_VARIABLE; + + Proposition* proposition = fl::null; + + std::stringstream tokenizer(consequent); + std::string token; + try { + while (tokenizer >> token) { + if (state bitand S_VARIABLE) { + if (engine->hasOutputVariable(token)) { + proposition = new Proposition; + proposition->variable = engine->getOutputVariable(token); + conclusions().push_back(proposition); + state = S_IS; + continue; + } + } + + if (state bitand S_IS) { + if (token == Rule::isKeyword()) { + state = S_HEDGE bitor S_TERM; + continue; + } + } + + if (state bitand S_HEDGE) { + HedgeFactory* factory = FactoryManager::instance()->hedge(); + if (factory->hasConstructor(token)) { + Hedge* hedge = factory->constructObject(token); + proposition->hedges.push_back(hedge); + state = S_HEDGE bitor S_TERM; + continue; + } + } + + if (state bitand S_TERM) { + if (proposition->variable->hasTerm(token)) { + proposition->term = proposition->variable->getTerm(token); + state = S_AND bitor S_WITH; + continue; + } + } + + if (state bitand S_AND) { + if (token == Rule::andKeyword()) { + state = S_VARIABLE; + continue; + } + } + + //if reached this point, there was an error: + if (state bitand S_VARIABLE) { + std::ostringstream ex; + ex << "[syntax error] consequent expected output variable, but found <" << token << ">"; + throw Exception(ex.str(), FL_AT); + } + if (state bitand S_IS) { + std::ostringstream ex; + ex << "[syntax error] consequent expected keyword <" << Rule::isKeyword() << ">, " + "but found <" << token << ">"; + throw Exception(ex.str(), FL_AT); + } + + if ((state bitand S_HEDGE) or (state bitand S_TERM)) { + std::ostringstream ex; + ex << "[syntax error] consequent expected hedge or term, but found <" << token << ">"; + throw Exception(ex.str(), FL_AT); + } + + if ((state bitand S_AND) or (state bitand S_WITH)) { + std::ostringstream ex; + ex << "[syntax error] consequent expected operator <" << Rule::andKeyword() << "> " + << "or keyword <" << Rule::withKeyword() << ">, " + << "but found <" << token << ">"; + throw Exception(ex.str(), FL_AT); + } + + std::ostringstream ex; + ex << "[syntax error] unexpected token <" << token << "> in consequent"; + throw Exception(ex.str(), FL_AT); + } + + if (not ((state bitand S_AND) or (state bitand S_WITH))) { //only acceptable final state + if (state bitand S_VARIABLE) { + std::ostringstream ex; + ex << "[syntax error] consequent expected output variable after <" << token << ">"; + throw Exception(ex.str(), FL_AT); + } + if (state bitand S_IS) { + std::ostringstream ex; + ex << "[syntax error] consequent expected keyword <" << Rule::isKeyword() << "> " + "after <" << token << ">"; + throw Exception(ex.str(), FL_AT); + } + if ((state bitand S_HEDGE) or (state bitand S_TERM)) { + std::ostringstream ex; + ex << "[syntax error] consequent expected hedge or term after <" << token << ">"; + throw Exception(ex.str(), FL_AT); + } + } + } catch (...) { + unload(); + throw; + } + } + + std::string Consequent::toString() const { + std::stringstream ss; + for (std::size_t i = 0; i < conclusions().size(); ++i) { + ss << conclusions().at(i)->toString(); + if (i + 1 < conclusions().size()) + ss << " " << Rule::andKeyword() << " "; + } + return ss.str(); + } + +} diff --git a/fuzzylite/src/rule/Expression.cpp b/fuzzylite/src/rule/Expression.cpp new file mode 100644 index 0000000..9b0ab81 --- /dev/null +++ b/fuzzylite/src/rule/Expression.cpp @@ -0,0 +1,83 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/rule/Expression.h" + +#include "fl/hedge/Hedge.h" +#include "fl/term/Term.h" +#include "fl/rule/Rule.h" +#include "fl/variable/Variable.h" + +namespace fl { + + Expression::Expression() { } + + Expression::~Expression() { } + + Proposition::Proposition() : Expression(), + variable(fl::null), term(fl::null) { } + + Proposition::~Proposition() { + for (std::size_t i = 0; i < hedges.size(); ++i) { + delete hedges.at(i); + } + hedges.clear(); + } + + Expression::Type Proposition::type() const { + return Expression::Proposition; + } + + std::string Proposition::toString() const { + std::ostringstream ss; + if (variable) { + ss << variable->getName(); + } else { + ss << "?"; + } + if (not hedges.empty()) { + ss << " " << Rule::isKeyword() << " "; + for (std::size_t i = 0; i < hedges.size(); ++i) { + ss << hedges.at(i)->name() << " "; + } + } + + if (term) { //term is fl::null if hedge is any + if (hedges.empty()) { + ss << " " << Rule::isKeyword() << " "; + } + ss << term->getName(); + } + return ss.str(); + } + + Operator::Operator() : Expression(), + name(""), left(fl::null), right(fl::null) { } + + Operator::~Operator() { + if (left) delete left; + if (right) delete right; + } + + Expression::Type Operator::type() const { + return Expression::Operator; + } + + std::string Operator::toString() const { + return name; + } + +} diff --git a/fuzzylite/src/rule/Rule.cpp b/fuzzylite/src/rule/Rule.cpp new file mode 100644 index 0000000..0338cd9 --- /dev/null +++ b/fuzzylite/src/rule/Rule.cpp @@ -0,0 +1,264 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/rule/Rule.h" + +#include "fl/Exception.h" +#include "fl/imex/FllExporter.h" +#include "fl/norm/Norm.h" +#include "fl/Operation.h" + +namespace fl { + + Rule::Rule(const std::string& text, scalar weight) + : _enabled(true), _text(text), _weight(weight), _activationDegree(0.0), _triggered(false), + _antecedent(new Antecedent), _consequent(new Consequent) { } + + Rule::Rule(const Rule& other) : _enabled(other._enabled), _text(other._text), + _weight(other._weight), _activationDegree(other._activationDegree), _triggered(false), + _antecedent(new Antecedent), _consequent(new Consequent) { } + + Rule& Rule::operator=(const Rule& other) { + if (this != &other) { + _enabled = other._enabled; + _text = other._text; + _weight = other._weight; + _activationDegree = other._activationDegree; + _triggered = other._triggered; + _antecedent.reset(new Antecedent); + _consequent.reset(new Consequent); + } + return *this; + } + + Rule::~Rule() { + if (_antecedent.get()) _antecedent->unload(); + if (_consequent.get()) _consequent->unload(); + } + + void Rule::setText(const std::string& text) { + this->_text = text; + } + + std::string Rule::getText() const { + return this->_text; + } + + void Rule::setWeight(scalar weight) { + this->_weight = weight; + } + + scalar Rule::getWeight() const { + return this->_weight; + } + + void Rule::setAntecedent(Antecedent* antecedent) { + this->_antecedent.reset(antecedent); + } + + Antecedent* Rule::getAntecedent() const { + return this->_antecedent.get(); + } + + void Rule::setConsequent(Consequent* consequent) { + this->_consequent.reset(consequent); + } + + Consequent* Rule::getConsequent() const { + return this->_consequent.get(); + } + + void Rule::setEnabled(bool active) { + this->_enabled = active; + } + + bool Rule::isEnabled() const { + return this->_enabled; + } + + void Rule::setActivationDegree(scalar activationDegree) { + this->_activationDegree = activationDegree; + } + + scalar Rule::getActivationDegree() const { + return this->_activationDegree; + } + + void Rule::deactivate() { + _activationDegree = 0.0; + _triggered = false; + } + + scalar Rule::activateWith(const TNorm* conjunction, const SNorm* disjunction) { + if (not isLoaded()) { + throw Exception("[rule error] the following rule is not loaded: " + getText(), FL_AT); + } + _activationDegree = _weight * _antecedent->activationDegree(conjunction, disjunction); + return _activationDegree; + } + + void Rule::trigger(const TNorm* implication) { + if (not isLoaded()) { + throw Exception("[rule error] the following rule is not loaded: " + getText(), FL_AT); + } + if (_enabled and Op::isGt(_activationDegree, 0.0)) { + FL_DBG("[firing with " << Op::str(_activationDegree) << "] " << toString()); + _consequent->modify(_activationDegree, implication); + _triggered = true; + } + } + + bool Rule::isTriggered() const { + return this->_triggered; + } + + Complexity Rule::complexityOfActivation(const TNorm* conjunction, const SNorm* disjunction) const { + Complexity result; + result.comparison(1).arithmetic(1); + if (isLoaded()) { + result += _antecedent->complexity(conjunction, disjunction); + } + return result; + } + + Complexity Rule::complexityOfFiring(const TNorm* implication) const { + Complexity result; + result.comparison(3); + if (isLoaded()) { + result += _consequent->complexity(implication); + } + return result; + } + + Complexity Rule::complexity(const TNorm* conjunction, const SNorm* disjunction, + const TNorm* implication) const { + return complexityOfActivation(conjunction, disjunction) + + complexityOfFiring(implication); + } + + bool Rule::isLoaded() const { + return _antecedent.get() and _consequent.get() + and _antecedent->isLoaded() and _consequent->isLoaded(); + } + + void Rule::unload() { + deactivate(); + if (getAntecedent()) getAntecedent()->unload(); + if (getConsequent()) getConsequent()->unload(); + } + + void Rule::load(const Engine* engine) { + load(getText(), engine); + } + + void Rule::load(const std::string& rule, const Engine* engine) { + deactivate(); + setEnabled(true); + setText(rule); + std::istringstream tokenizer(rule.substr(0, rule.find_first_of('#'))); + std::string token; + std::ostringstream ossAntecedent, ossConsequent; + scalar weight = 1.0; + + enum FSM { + S_NONE, S_IF, S_THEN, S_WITH, S_END + }; + FSM state = S_NONE; + try { + while (tokenizer >> token) { + + switch (state) { + case S_NONE: + if (token == Rule::ifKeyword()) state = S_IF; + else { + std::ostringstream ex; + ex << "[syntax error] expected keyword <" << Rule::ifKeyword() << + ">, but found <" << token << "> in rule: " << rule; + throw Exception(ex.str(), FL_AT); + } + break; + case S_IF: + if (token == Rule::thenKeyword()) state = S_THEN; + else ossAntecedent << token << " "; + break; + case S_THEN: + if (token == Rule::withKeyword()) state = S_WITH; + else ossConsequent << token << " "; + break; + case S_WITH: + try { + weight = Op::toScalar(token); + state = S_END; + } catch (Exception& e) { + std::ostringstream ex; + ex << "[syntax error] expected a numeric value as the weight of the rule: " + << rule; + e.append(ex.str(), FL_AT); + throw; + } + break; + case S_END: + { + std::ostringstream ex; + ex << "[syntax error] unexpected token <" << token << "> at the end of rule"; + throw Exception(ex.str(), FL_AT); + } + + default: + std::ostringstream ex; + ex << "[syntax error] unexpected state <" << state << ">"; + throw Exception(ex.str(), FL_AT); + } + } + if (state == S_NONE) { + std::ostringstream ex; + ex << "[syntax error] " << (rule.empty() ? "empty rule" : ("ignored rule: " + rule)); + throw Exception(ex.str(), FL_AT); + } else if (state == S_IF) { + std::ostringstream ex; + ex << "[syntax error] keyword <" << Rule::thenKeyword() << "> not found in rule: " << rule; + throw Exception(ex.str(), FL_AT); + } else if (state == S_WITH) { + std::ostringstream ex; + ex << "[syntax error] expected a numeric value as the weight of the rule: " << rule; + throw Exception(ex.str(), FL_AT); + } + + getAntecedent()->load(ossAntecedent.str(), engine); + getConsequent()->load(ossConsequent.str(), engine); + setWeight(weight); + + } catch (...) { + unload(); + throw; + } + } + + std::string Rule::toString() const { + return FllExporter().toString(this); + } + + Rule* Rule::clone() const { + return new Rule(*this); + } + + Rule* Rule::parse(const std::string& rule, const Engine* engine) { + FL_unique_ptr<Rule> result(new Rule); + result->load(rule, engine); + return result.release(); + } + +} diff --git a/fuzzylite/src/rule/RuleBlock.cpp b/fuzzylite/src/rule/RuleBlock.cpp new file mode 100644 index 0000000..d4e2a82 --- /dev/null +++ b/fuzzylite/src/rule/RuleBlock.cpp @@ -0,0 +1,227 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/rule/RuleBlock.h" + +#include "fl/activation/General.h" +#include "fl/imex/FllExporter.h" +#include "fl/norm/TNorm.h" +#include "fl/norm/SNorm.h" +#include "fl/rule/Rule.h" +#include "fl/Operation.h" + +namespace fl { + + RuleBlock::RuleBlock(const std::string& name) + : _enabled(true), _name(name), _description("") { } + + RuleBlock::RuleBlock(const RuleBlock& other) : _enabled(true), _name(other._name), + _description(other._description) { + copyFrom(other); + } + + RuleBlock& RuleBlock::operator=(const RuleBlock& other) { + if (this != &other) { + for (std::size_t i = 0; i < _rules.size(); ++i) { + delete _rules.at(i); + } + _rules.clear(); + _conjunction.reset(fl::null); + _disjunction.reset(fl::null); + _implication.reset(fl::null); + _activation.reset(fl::null); + + copyFrom(other); + } + return *this; + } + + void RuleBlock::copyFrom(const RuleBlock& source) { + _enabled = source._enabled; + _name = source._name; + _description = source._description; + if (source._conjunction.get()) _conjunction.reset(source._conjunction->clone()); + if (source._disjunction.get()) _disjunction.reset(source._disjunction->clone()); + if (source._implication.get()) _implication.reset(source._implication->clone()); + if (source._activation.get()) _activation.reset(source._activation->clone()); + for (std::size_t i = 0; i < source._rules.size(); ++i) { + _rules.push_back(source._rules.at(i)->clone()); + } + } + + RuleBlock::~RuleBlock() { + for (std::size_t i = 0; i < _rules.size(); ++i) { + delete _rules.at(i); + } + _rules.clear(); + } + + Complexity RuleBlock::complexity() const { + Complexity result; + result.comparison(1); + if (_activation.get()) { + result += _activation->complexity(this); + } else { + for (std::size_t i = 0; i < _rules.size(); ++i) { + result += _rules.at(i)->complexity( + _conjunction.get(), _disjunction.get(), _implication.get()); + } + } + return result; + } + + void RuleBlock::activate() { + if (not _activation.get()) { + _activation.reset(new General); + } + _activation->activate(this); + } + + void RuleBlock::unloadRules() const { + for (std::size_t i = 0; i < _rules.size(); ++i) { + _rules.at(i)->unload(); + } + } + + void RuleBlock::loadRules(const Engine* engine) { + std::ostringstream exceptions; + bool throwException = false; + for (std::size_t i = 0; i < _rules.size(); ++i) { + Rule* rule = _rules.at(i); + if (rule->isLoaded()) { + rule->unload(); + } + try { + rule->load(engine); + } catch (std::exception& ex) { + throwException = true; + exceptions << ex.what() << "\n"; + } + } + if (throwException) { + Exception exception("[ruleblock error] the following " + "rules could not be loaded:\n" + exceptions.str(), FL_AT); + throw exception; + } + } + + void RuleBlock::reloadRules(const Engine* engine) { + unloadRules(); + loadRules(engine); + } + + void RuleBlock::setName(std::string name) { + this->_name = name; + } + + std::string RuleBlock::getName() const { + return this->_name; + } + + void RuleBlock::setDescription(const std::string& description) { + this->_description = description; + } + + std::string RuleBlock::getDescription() const { + return this->_description; + } + + void RuleBlock::setConjunction(TNorm* tnorm) { + this->_conjunction.reset(tnorm); + } + + TNorm* RuleBlock::getConjunction() const { + return this->_conjunction.get(); + } + + void RuleBlock::setDisjunction(SNorm* snorm) { + this->_disjunction.reset(snorm); + } + + SNorm* RuleBlock::getDisjunction() const { + return this->_disjunction.get(); + } + + void RuleBlock::setImplication(TNorm* implication) { + this->_implication.reset(implication); + } + + TNorm* RuleBlock::getImplication() const { + return this->_implication.get(); + } + + void RuleBlock::setActivation(Activation* activation) { + this->_activation.reset(activation); + } + + Activation* RuleBlock::getActivation() const { + return this->_activation.get(); + } + + void RuleBlock::setEnabled(bool enabled) { + this->_enabled = enabled; + } + + bool RuleBlock::isEnabled() const { + return this->_enabled; + } + + std::string RuleBlock::toString() const { + return FllExporter().toString(this); + } + + /** + * Operations for std::vector _rules + */ + void RuleBlock::addRule(Rule* rule) { + _rules.push_back(rule); + } + + void RuleBlock::insertRule(Rule* rule, std::size_t index) { + _rules.insert(_rules.begin() + index, rule); + } + + Rule* RuleBlock::getRule(std::size_t index) const { + return _rules.at(index); + } + + Rule* RuleBlock::removeRule(std::size_t index) { + Rule* result = _rules.at(index); + _rules.erase(_rules.begin() + index); + return result; + } + + std::size_t RuleBlock::numberOfRules() const { + return _rules.size(); + } + + const std::vector<Rule*>& RuleBlock::rules() const { + return this->_rules; + } + + void RuleBlock::setRules(const std::vector<Rule*>& rules) { + this->_rules = rules; + } + + std::vector<Rule*>& RuleBlock::rules() { + return this->_rules; + } + + RuleBlock* RuleBlock::clone() const { + return new RuleBlock(*this); + } + +} diff --git a/fuzzylite/src/term/Activated.cpp b/fuzzylite/src/term/Activated.cpp new file mode 100644 index 0000000..80eda6c --- /dev/null +++ b/fuzzylite/src/term/Activated.cpp @@ -0,0 +1,110 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/Activated.h" + +#include "fl/imex/FllExporter.h" + +namespace fl { + + Activated::Activated(const Term* term, scalar degree, const TNorm* implication) + : Term(""), _term(term), _degree(degree), _implication(implication) { + if (term) setName(term->getName()); + } + + Activated::~Activated() { } + + std::string Activated::className() const { + return "Activated"; + } + + Complexity Activated::complexity() const { + Complexity result; + result.comparison(3); + if (_implication) { + result += _implication->complexity(); + } + if (_term) { + result += _term->complexity(); + } + return result; + } + + scalar Activated::membership(scalar x) const { + if (Op::isNaN(x)) return fl::nan; + if (not _term) + throw Exception("[activation error] no term available to activate", FL_AT); + if (not _implication) + throw Exception("[implication error] implication operator needed " + "to activate " + getTerm()->toString(), FL_AT); + return _implication->compute(_term->membership(x), _degree); + } + + std::string Activated::parameters() const { + FllExporter exporter; + std::ostringstream ss; + ss << Op::str(getDegree()) << " " << exporter.toString(getImplication()) << " " + << exporter.toString(getTerm()); + return ss.str(); + } + + void Activated::configure(const std::string& parameters) { + FL_IUNUSED(parameters); + } + + std::string Activated::toString() const { + FllExporter exporter; + std::ostringstream ss; + if (getImplication()) { + ss << exporter.toString(getImplication()) << "(" + << Op::str(getDegree()) << "," + << getTerm()->getName() << ")"; + } else { + ss << "(" << Op::str(getDegree()) << "*" //"\u2297: (*)" + << getTerm()->getName() << ")"; + } + return ss.str(); + } + + void Activated::setTerm(const Term* term) { + this->_term = term; + } + + const Term* Activated::getTerm() const { + return this->_term; + } + + void Activated::setDegree(scalar degree) { + this->_degree = degree; + } + + scalar Activated::getDegree() const { + return this->_degree; + } + + void Activated::setImplication(const TNorm* implication) { + this->_implication = implication; + } + + const TNorm* Activated::getImplication() const { + return this->_implication; + } + + Activated* Activated::clone() const { + return new Activated(*this); + } + +} diff --git a/fuzzylite/src/term/Aggregated.cpp b/fuzzylite/src/term/Aggregated.cpp new file mode 100644 index 0000000..d1e2c89 --- /dev/null +++ b/fuzzylite/src/term/Aggregated.cpp @@ -0,0 +1,247 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/Aggregated.h" + +#include "fl/imex/FllExporter.h" +#include "fl/norm/s/Maximum.h" + +namespace fl { + + Aggregated::Aggregated(const std::string& name, scalar minimum, scalar maximum, + SNorm* aggregation) + : Term(name), _minimum(minimum), _maximum(maximum), _aggregation(aggregation) { } + + Aggregated::Aggregated(const Aggregated& other) : Term(other) { + copyFrom(other); + } + + Aggregated& Aggregated::operator=(const Aggregated& other) { + if (this != &other) { + clear(); + _aggregation.reset(fl::null); + + Term::operator=(other); + copyFrom(other); + } + return *this; + } + + Aggregated::~Aggregated() { } + + void Aggregated::copyFrom(const Aggregated& source) { + _minimum = source._minimum; + _maximum = source._maximum; + + if (source._aggregation.get()) + _aggregation.reset(source._aggregation->clone()); + + for (std::size_t i = 0; i < source._terms.size(); ++i) { + _terms.push_back(source._terms.at(i)); + } + } + + std::string Aggregated::className() const { + return "Aggregated"; + } + + Complexity Aggregated::complexity() const { + return complexityOfMembership(); + } + + Complexity Aggregated::complexityOfMembership() const { + Complexity result; + result.comparison(3); + if (_aggregation.get()) { + result += _aggregation->complexity().multiply(scalar(_terms.size())); + } + for (std::size_t i = 0; i < _terms.size(); ++i) { + result += _terms.at(i).complexity(); + } + return result; + } + + scalar Aggregated::membership(scalar x) const { + if (Op::isNaN(x)) return fl::nan; + if (not (_terms.empty() or _aggregation.get())) { //Exception for IntegralDefuzzifiers + throw Exception("[aggregation error] " + "aggregation operator needed to aggregate variable " + "<" + getName() + ">", FL_AT); + } + scalar mu = 0.0; + for (std::size_t i = 0; i < _terms.size(); ++i) { + mu = _aggregation->compute(mu, _terms.at(i).membership(x)); + } + return mu; + } + + Complexity Aggregated::complexityOfActivationDegree() const { + Complexity result; + result.comparison(2); + if (_aggregation.get()) { + result += _aggregation->complexity(); + } else result.arithmetic(1); + result.multiply(scalar(_terms.size())); + return result; + } + + scalar Aggregated::activationDegree(const Term* forTerm) const { + scalar result = 0.0; + for (std::size_t i = 0; i < _terms.size(); ++i) { + const Activated& activatedTerm = _terms.at(i); + if (activatedTerm.getTerm() == forTerm) { + if (_aggregation.get()) + result = _aggregation->compute(result, activatedTerm.getDegree()); + else + result += activatedTerm.getDegree(); //Default for WeightDefuzzifier + } + } + return result; + } + + const Activated* Aggregated::highestActivatedTerm() const { + const Activated* maximumTerm = fl::null; + scalar maximumActivation = -fl::inf; + for (std::size_t i = 0; i < _terms.size(); ++i) { + const Activated& activated = _terms.at(i); + if (Op::isGt(activated.getDegree(), maximumActivation)) { + maximumActivation = activated.getDegree(); + maximumTerm = &activated; + } + } + return maximumTerm; + } + + std::string Aggregated::parameters() const { + FllExporter exporter; + std::ostringstream ss; + ss << exporter.toString(getAggregation()); + ss << " " << Op::str(getMinimum()) << " " << Op::str(getMaximum()) << " "; + for (std::size_t i = 0; i < terms().size(); ++i) { + ss << " " << exporter.toString(&terms().at(i)); + } + return ss.str(); + } + + void Aggregated::configure(const std::string& parameters) { + FL_IUNUSED(parameters); + } + + Aggregated* Aggregated::clone() const { + return new Aggregated(*this); + } + + std::string Aggregated::toString() const { + std::vector<std::string> aggregate; + for (std::size_t i = 0; i < terms().size(); ++i) { + aggregate.push_back(terms().at(i).toString()); + } + FllExporter exporter; + std::ostringstream ss; + if (getAggregation()) { + ss << getName() << ": " << className() << " " + << exporter.toString(getAggregation()) << "[" + << Op::join(aggregate, ",") << "]"; + } else { + ss << getName() << ": " << className() << " " << "[" + << Op::join(aggregate, "+") << "]"; //\u2295: (+) + } + return ss.str(); + } + + void Aggregated::setMinimum(scalar minimum) { + this->_minimum = minimum; + } + + scalar Aggregated::getMinimum() const { + return this->_minimum; + } + + void Aggregated::setMaximum(scalar maximum) { + this->_maximum = maximum; + } + + scalar Aggregated::getMaximum() const { + return this->_maximum; + } + + void Aggregated::setRange(scalar minimum, scalar maximum) { + setMinimum(minimum); + setMaximum(maximum); + } + + scalar Aggregated::range() const { + return getMaximum() - getMinimum(); + } + + void Aggregated::setAggregation(SNorm* aggregation) { + this->_aggregation.reset(aggregation); + } + + SNorm* Aggregated::getAggregation() const { + return this->_aggregation.get(); + } + + /** + * Operations for std::vector _terms + */ + + + void Aggregated::addTerm(const Term* term, scalar degree, const TNorm* implication) { + _terms.push_back(Activated(term, degree, implication)); + FL_DBG("Aggregating " << _terms.back().toString()); + } + + void Aggregated::addTerm(const Activated& term) { + _terms.push_back(term); + FL_DBG("Aggregating " << _terms.back().toString()); + } + + const Activated& Aggregated::removeTerm(std::size_t index) { + const Activated& term = _terms.at(index); + _terms.erase(_terms.begin() + index); + return term; + } + + void Aggregated::clear() { + _terms.clear(); + } + + const Activated& Aggregated::getTerm(std::size_t index) const { + return _terms.at(index); + } + + void Aggregated::setTerms(const std::vector<Activated>& terms) { + this->_terms = terms; + } + + const std::vector<Activated>& Aggregated::terms() const { + return this->_terms; + } + + std::vector<Activated>& Aggregated::terms() { + return this->_terms; + } + + std::size_t Aggregated::numberOfTerms() const { + return _terms.size(); + } + + bool Aggregated::isEmpty() const { + return _terms.empty(); + } + +} diff --git a/fuzzylite/src/term/Bell.cpp b/fuzzylite/src/term/Bell.cpp new file mode 100644 index 0000000..bd63753 --- /dev/null +++ b/fuzzylite/src/term/Bell.cpp @@ -0,0 +1,93 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/Bell.h" + +namespace fl { + + Bell::Bell(const std::string& name, scalar center, scalar width, scalar slope, scalar height) + : Term(name, height), _center(center), _width(width), _slope(slope) { } + + Bell::~Bell() { } + + std::string Bell::className() const { + return "Bell"; + } + + Complexity Bell::complexity() const { + return Complexity().comparison(1).arithmetic(6).function(2); + } + + scalar Bell::membership(scalar x) const { + if (Op::isNaN(x)) return fl::nan; + return Term::_height * (1.0 / (1.0 + std::pow(std::abs((x - _center) / _width), 2.0 * _slope))); + } + + std::string Bell::parameters() const { + return Op::join(3, " ", _center, _width, _slope) + + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); + } + + void Bell::configure(const std::string& parameters) { + if (parameters.empty()) return; + std::vector<std::string> values = Op::split(parameters, " "); + std::size_t required = 3; + if (values.size() < required) { + std::ostringstream ex; + ex << "[configuration error] term <" << className() << ">" + << " requires <" << required << "> parameters"; + throw Exception(ex.str(), FL_AT); + } + setCenter(Op::toScalar(values.at(0))); + setWidth(Op::toScalar(values.at(1))); + setSlope(Op::toScalar(values.at(2))); + if (values.size() > required) + setHeight(Op::toScalar(values.at(required))); + } + + void Bell::setWidth(scalar a) { + this->_width = a; + } + + scalar Bell::getWidth() const { + return this->_width; + } + + void Bell::setSlope(scalar b) { + this->_slope = b; + } + + scalar Bell::getSlope() const { + return this->_slope; + } + + void Bell::setCenter(scalar c) { + this->_center = c; + } + + scalar Bell::getCenter() const { + return this->_center; + } + + Bell* Bell::clone() const { + return new Bell(*this); + } + + Term* Bell::constructor() { + return new Bell; + } + +} diff --git a/fuzzylite/src/term/Binary.cpp b/fuzzylite/src/term/Binary.cpp new file mode 100644 index 0000000..368530b --- /dev/null +++ b/fuzzylite/src/term/Binary.cpp @@ -0,0 +1,96 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/Binary.h" + +namespace fl { + + Binary::Binary(const std::string& name, scalar start, scalar direction, scalar height) + : Term(name, height), _start(start), _direction(direction) { } + + Binary::~Binary() { } + + std::string Binary::className() const { + return "Binary"; + } + + Complexity Binary::complexity() const { + return Complexity().comparison(5).arithmetic(1); + } + + scalar Binary::membership(scalar x) const { + if (Op::isNaN(x)) return fl::nan; + if (_direction > _start and Op::isGE(x, _start)) { + return Term::_height * 1.0; + } + if (_direction < _start and Op::isLE(x, _start)) { + return Term::_height * 1.0; + } + return Term::_height * 0.0; + } + + std::string Binary::parameters() const { + return Op::join(2, " ", _start, _direction) + + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); + } + + void Binary::configure(const std::string& parameters) { + if (parameters.empty()) return; + std::vector<std::string> values = Op::split(parameters, " "); + std::size_t required = 2; + if (values.size() < required) { + std::ostringstream ex; + ex << "[configuration error] term <" << className() << ">" + << " requires <" << required << "> parameters"; + throw Exception(ex.str(), FL_AT); + } + setStart(Op::toScalar(values.at(0))); + setDirection(Op::toScalar(values.at(1))); + if (values.size() > required) + setHeight(Op::toScalar(values.at(required))); + } + + void Binary::setStart(scalar minimum) { + this->_start = minimum; + } + + scalar Binary::getStart() const { + return this->_start; + } + + void Binary::setDirection(scalar direction) { + this->_direction = direction; + } + + scalar Binary::getDirection() const { + return this->_direction; + } + + Binary::Direction Binary::direction() const { + if (this->_direction > _start) return Positive; + if (this->_direction < _start) return Negative; + return Undefined; + } + + Binary* Binary::clone() const { + return new Binary(*this); + } + + Term* Binary::constructor() { + return new Binary; + } + +} diff --git a/fuzzylite/src/term/Concave.cpp b/fuzzylite/src/term/Concave.cpp new file mode 100644 index 0000000..c585edc --- /dev/null +++ b/fuzzylite/src/term/Concave.cpp @@ -0,0 +1,107 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/Concave.h" + +namespace fl { + + Concave::Concave(const std::string& name, scalar inflection, scalar end, scalar height) + : Term(name, height), _inflection(inflection), _end(end) { } + + Concave::~Concave() { } + + std::string Concave::className() const { + return "Concave"; + } + + Complexity Concave::complexity() const { + return Complexity().comparison(1 + 3).arithmetic(1 + 5); + } + + scalar Concave::membership(scalar x) const { + if (Op::isNaN(x)) return fl::nan; + if (Op::isLE(_inflection, _end)) { //Concave increasing + if (Op::isLt(x, _end)) { + return Term::_height * (_end - _inflection) / (2.0 * _end - _inflection - x); + } + } else { //Concave decreasing + if (Op::isGt(x, _end)) { + return Term::_height * (_inflection - _end) / (_inflection - 2.0 * _end + x); + } + } + return Term::_height * 1.0; + } + + scalar Concave::tsukamoto(scalar activationDegree, scalar minimum, scalar maximum) const { + FL_IUNUSED(minimum); + FL_IUNUSED(maximum); + scalar i = _inflection; + scalar e = _end; + return (i - e) / membership(activationDegree) + 2 * e - i; + } + + bool Concave::isMonotonic() const { + return true; + } + + std::string Concave::parameters() const { + return Op::join(2, " ", _inflection, _end) + + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); + + } + + void Concave::configure(const std::string& parameters) { + if (parameters.empty()) return; + std::vector<std::string> values = Op::split(parameters, " "); + std::size_t required = 2; + if (values.size() < required) { + std::ostringstream ex; + ex << "[configuration error] term <" << className() << ">" + << " requires <" << required << "> parameters"; + throw Exception(ex.str(), FL_AT); + } + setInflection(Op::toScalar(values.at(0))); + setEnd(Op::toScalar(values.at(1))); + if (values.size() > required) + setHeight(Op::toScalar(values.at(required))); + + } + + void Concave::setInflection(scalar start) { + this->_inflection = start; + } + + scalar Concave::getInflection() const { + return this->_inflection; + } + + void Concave::setEnd(scalar end) { + this->_end = end; + } + + scalar Concave::getEnd() const { + return this->_end; + } + + Concave* Concave::clone() const { + return new Concave(*this); + } + + Term* Concave::constructor() { + return new Concave; + } + +} diff --git a/fuzzylite/src/term/Constant.cpp b/fuzzylite/src/term/Constant.cpp new file mode 100644 index 0000000..7c0422e --- /dev/null +++ b/fuzzylite/src/term/Constant.cpp @@ -0,0 +1,64 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/Constant.h" + +namespace fl { + + Constant::Constant(const std::string& name, scalar value) + : Term(name), _value(value) { } + + Constant::~Constant() { } + + std::string Constant::className() const { + return "Constant"; + } + + Complexity Constant::complexity() const { + return Complexity(); + } + + scalar Constant::membership(scalar x) const { + FL_IUNUSED(x); + return this->_value; + } + + std::string Constant::parameters() const { + return Op::str(_value); + } + + void Constant::configure(const std::string& parameters) { + if (parameters.empty()) return; + setValue(Op::toScalar(parameters)); + } + + void Constant::setValue(scalar value) { + this->_value = value; + } + + scalar Constant::getValue() const { + return this->_value; + } + + Constant* Constant::clone() const { + return new Constant(*this); + } + + Term* Constant::constructor() { + return new Constant; + } + +} diff --git a/fuzzylite/src/term/Cosine.cpp b/fuzzylite/src/term/Cosine.cpp new file mode 100644 index 0000000..78162ea --- /dev/null +++ b/fuzzylite/src/term/Cosine.cpp @@ -0,0 +1,89 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/Cosine.h" + +namespace fl { + + Cosine::Cosine(const std::string& name, scalar center, scalar width, scalar height) + : Term(name, height), _center(center), _width(width) { } + + Cosine::~Cosine() { } + + std::string Cosine::className() const { + return "Cosine"; + } + + std::string Cosine::parameters() const { + return Op::join(2, " ", _center, _width) + + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); + } + + void Cosine::configure(const std::string& parameters) { + if (parameters.empty()) return; + std::vector<std::string> values = Op::split(parameters, " "); + std::size_t required = 2; + if (values.size() < required) { + std::ostringstream ex; + ex << "[configuration error] term <" << className() << ">" + << " requires <" << required << "> parameters"; + throw Exception(ex.str(), FL_AT); + } + setCenter(Op::toScalar(values.at(0))); + setWidth(Op::toScalar(values.at(1))); + if (values.size() > required) + setHeight(Op::toScalar(values.at(required))); + + } + + Complexity Cosine::complexity() const { + return Complexity().comparison(3).arithmetic(4 + 1 + 7).function(2); + } + + scalar Cosine::membership(scalar x) const { + if (Op::isNaN(x)) return fl::nan; + if (Op::isLt(x, _center - 0.5 * _width) + or Op::isGt(x, _center + 0.5 * _width)) + return Term::_height * 0.0; + const scalar pi = 4.0 * std::atan(1.0); + return Term::_height * (0.5 * (1.0 + std::cos(2.0 / _width * pi * (x - _center)))); + } + + void Cosine::setCenter(scalar center) { + this->_center = center; + } + + scalar Cosine::getCenter() const { + return this->_center; + } + + void Cosine::setWidth(scalar width) { + this->_width = width; + } + + scalar Cosine::getWidth() const { + return this->_width; + } + + Cosine* Cosine::clone() const { + return new Cosine(*this); + } + + Term* Cosine::constructor() { + return new Cosine; + } + +} diff --git a/fuzzylite/src/term/Discrete.cpp b/fuzzylite/src/term/Discrete.cpp new file mode 100644 index 0000000..9951867 --- /dev/null +++ b/fuzzylite/src/term/Discrete.cpp @@ -0,0 +1,230 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/Discrete.h" + +namespace fl { + + Discrete::Discrete(const std::string& name, const std::vector<Pair>& xy, scalar height) + : Term(name, height), _xy(xy) { } + + Discrete::~Discrete() { } + + std::string Discrete::className() const { + return "Discrete"; + } + + bool compare(const Discrete::Pair& a, const Discrete::Pair& b) { + return a.first < b.first; + } + + void Discrete::sort(std::vector<Pair>& pairs) { + std::sort(pairs.begin(), pairs.end(), compare); + } + + void Discrete::sort() { + std::sort(_xy.begin(), _xy.end(), compare); + } + + Complexity Discrete::complexity() const { + return Complexity().comparison(1 + 4).arithmetic(1 + 1 + 1).function(1) + .function(2 * std::log(scalar(_xy.size()))); + } + + scalar Discrete::membership(scalar x) const { + if (Op::isNaN(x)) return fl::nan; + if (_xy.empty()) + throw Exception("[discrete error] term is empty", FL_AT); + + /* ______________________ + / \ + / \ + ____________/ \____________ + x[0] x[n-1] + */ + + if (Op::isLE(x, _xy.front().first)) + return Term::_height * _xy.front().second; + if (Op::isGE(x, _xy.back().first)) + return Term::_height * _xy.back().second; + + const Pair value(x, fl::nan); + typedef std::vector<Discrete::Pair>::const_iterator Bound; + //std::lower_bound finds the first number greater than or equal to x + Bound lowerBound(std::lower_bound(_xy.begin(), _xy.end(), value, compare)); + + //if the lower bound is equal to x + if (Op::isEq(x, lowerBound->first)) { + return Term::_height * lowerBound->second; + } + //find the upper bound starting from a copy of lowerBound + const Bound upperBound(std::upper_bound(_xy.begin(), _xy.end(), value, compare)); + --lowerBound; //One arithmetic + return Term::_height * Op::scale(x, lowerBound->first, upperBound->first, + lowerBound->second, upperBound->second); + } + + std::string Discrete::parameters() const { + std::ostringstream ss; + for (std::size_t i = 0; i < _xy.size(); ++i) { + ss << Op::str(_xy.at(i).first) << " " << Op::str(_xy.at(i).second); + if (i + 1 < _xy.size()) ss << " "; + } + if (not Op::isEq(getHeight(), 1.0)) ss << " " << Op::str(getHeight()); + return ss.str(); + } + + void Discrete::configure(const std::string& parameters) { + if (parameters.empty()) return; + std::vector<std::string> strValues = Op::split(parameters, " "); + std::vector<scalar> values(strValues.size()); + for (std::size_t i = 0; i < strValues.size(); ++i) { + values.at(i) = Op::toScalar(strValues.at(i)); + } + if (values.size() % 2 == 0) { + setHeight(1.0); + } else { + setHeight(values.back()); + values.pop_back(); + } + this->_xy = toPairs(values); + } + + void Discrete::setXY(const std::vector<Pair>& xy) { + this->_xy = xy; + } + + const std::vector<Discrete::Pair>& Discrete::xy() const { + return this->_xy; + } + + std::vector<Discrete::Pair>& Discrete::xy() { + return this->_xy; + } + + const Discrete::Pair& Discrete::xy(std::size_t index) const { + return this->_xy.at(index); + } + + Discrete::Pair& Discrete::xy(std::size_t index) { + return this->_xy.at(index); + } + + std::vector<scalar> Discrete::x() const { + std::vector<scalar> result(_xy.size()); + for (std::size_t i = 0; i < result.size(); ++i) { + result.at(i) = _xy.at(i).first; + } + return result; + } + + std::vector<scalar> Discrete::y() const { + std::vector<scalar> result(_xy.size()); + for (std::size_t i = 0; i < result.size(); ++i) { + result.at(i) = _xy.at(i).second; + } + return result; + } + + scalar Discrete::x(std::size_t index) const { + return _xy.at(index).first; + } + + scalar& Discrete::x(std::size_t index) { + return _xy.at(index).first; + } + + scalar Discrete::y(std::size_t index) const { + return _xy.at(index).second; + } + + scalar& Discrete::y(std::size_t index) { + return _xy.at(index).second; + } + + std::vector<Discrete::Pair> Discrete::toPairs(const std::vector<scalar>& xy) { + if (xy.size() % 2 != 0) { + std::ostringstream os; + os << "[discrete error] missing value in set of pairs (|xy|=" << xy.size() << ")"; + throw Exception(os.str(), FL_AT); + } + + std::vector<Pair> result((xy.size() + 1) / 2); + for (std::size_t i = 0; i + 1 < xy.size(); i += 2) { + result.at(i / 2).first = xy.at(i); + result.at(i / 2).second = xy.at(i + 1); + } + return result; + } + + std::vector<Discrete::Pair> Discrete::toPairs(const std::vector<scalar>& xy, + scalar missingValue) FL_INOEXCEPT { + std::vector<Pair> result((xy.size() + 1) / 2); + for (std::size_t i = 0; i + 1 < xy.size(); i += 2) { + result.at(i / 2).first = xy.at(i); + result.at(i / 2).second = xy.at(i + 1); + } + if (xy.size() % 2 != 0) { + result.back().first = xy.back(); + result.back().second = missingValue; + } + return result; + } + + std::vector<scalar> Discrete::toVector(const std::vector<Pair>& xy) { + std::vector<scalar> result(xy.size() * 2); + for (std::size_t i = 0; i < xy.size(); ++i) { + result.at(2 * i) = xy.at(i).first; + result.at(2 * i + 1) = xy.at(i).second; + } + return result; + } + + std::string Discrete::formatXY(const std::vector<Pair>& xy, const std::string& prefix, + const std::string& innerSeparator, const std::string& suffix, const std::string& outerSeparator) { + std::ostringstream os; + for (std::size_t i = 0; i < xy.size(); ++i) { + os << prefix << Op::str(xy.at(i).first) << innerSeparator + << Op::str(xy.at(i).second) << suffix; + if (i + 1 < xy.size()) os << outerSeparator; + } + return os.str(); + } + + Discrete* Discrete::discretize(const Term* term, scalar start, scalar end, int resolution, + bool boundedMembershipFunction) { + FL_unique_ptr<Discrete> result(new Discrete(term->getName())); + scalar dx = (end - start) / resolution; + scalar x, y; + for (int i = 0; i <= resolution; ++i) { + x = start + i * dx; + y = term->membership(x); + if (boundedMembershipFunction) + y = Op::bound(y, scalar(0.0), scalar(1.0)); + result->xy().push_back(Discrete::Pair(x, y)); + } + return result.release(); + } + + Discrete* Discrete::clone() const { + return new Discrete(*this); + } + + Term* Discrete::constructor() { + return new Discrete; + } + +} diff --git a/fuzzylite/src/term/Function.cpp b/fuzzylite/src/term/Function.cpp new file mode 100644 index 0000000..42f4aaa --- /dev/null +++ b/fuzzylite/src/term/Function.cpp @@ -0,0 +1,578 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/Function.h" + +#include "fl/Engine.h" +#include "fl/factory/FactoryManager.h" +#include "fl/rule/Rule.h" +#include "fl/variable/InputVariable.h" +#include "fl/variable/OutputVariable.h" + +#include <queue> +#include <stack> + +namespace fl { + + /** + * Parsing elements + */ + + + Function::Element::Element(const std::string& name, const std::string& description, Type type) + : name(name), description(description), type(type), unary(fl::null), binary(fl::null), arity(0), + precedence(0), associativity(-1) { } + + Function::Element::Element(const std::string& name, const std::string& description, + Type type, Unary unary, int precedence, int associativity) + : name(name), description(description), type(type), unary(unary), binary(fl::null), arity(1), + precedence(precedence), associativity(associativity) { } + + Function::Element::Element(const std::string& name, const std::string& description, + Type type, Binary binary, int precedence, int associativity) + : name(name), description(description), type(type), unary(fl::null), binary(binary), arity(2), + precedence(precedence), associativity(associativity) { } + + Function::Element::~Element() { } + + bool Function::Element::isOperator() const { + return type == Operator; + } + + bool Function::Element::isFunction() const { + return type == Function; + } + + Function::Element* Function::Element::clone() const { + return new Element(*this); + } + + std::string Function::Element::toString() const { + std::ostringstream ss; + + if (type == Operator) { + ss << "Operator (name=" << name << ", " + << "description=" << description << ", " + << "precedence=" << precedence << ", " + << "arity=" << arity << ", " + << "associativity=" << associativity << ", "; + if (arity == 1) ss << "pointer=" << unary; + else if (arity == 2) ss << "pointer=" << binary; + else ss << "pointer=error"; + ss << ")"; + } else if (type == Function) { + ss << "Function (name=" << name << ", " + << "description=" << description << ", " + << "arity=" << arity << ", " + << "associativity=" << associativity << ", "; + if (arity == 1) ss << "pointer=" << unary; + else if (arity == 2) ss << "pointer=" << binary; + else ss << "pointer=error"; + ss << ")"; + } + return ss.str(); + } + + /****************************** + * Tree Node Elements + ******************************/ + + Function::Node::Node(Element* element, Node* left, Node* right) + : element(element), left(left), right(right), variable(""), value(fl::nan) { } + + Function::Node::Node(const std::string& variable) + : element(fl::null), left(fl::null), right(fl::null), variable(variable), value(fl::nan) { } + + Function::Node::Node(scalar value) + : element(fl::null), left(fl::null), right(fl::null), variable(""), value(value) { } + + Function::Node::Node(const Node& other) + : element(fl::null), left(fl::null), right(fl::null), variable(""), value(fl::nan) { + copyFrom(other); + } + + Function::Node& Function::Node::operator=(const Node& other) { + if (this != &other) { + element.reset(fl::null); + left.reset(fl::null); + right.reset(fl::null); + + copyFrom(other); + } + return *this; + } + + void Function::Node::copyFrom(const Node& other) { + if (other.element.get()) element.reset(other.element->clone()); + if (other.left.get()) left.reset(other.left->clone()); + if (other.right.get()) right.reset(other.right->clone()); + variable = other.variable; + value = other.value; + } + + Function::Node::~Node() { } + + scalar Function::Node::evaluate(const std::map<std::string, scalar>* variables) const { + scalar result = fl::nan; + if (element.get()) { + if (element->unary) { + result = element->unary(left->evaluate(variables)); + } else if (element->binary) { + result = element->binary(right->evaluate(variables), left->evaluate(variables)); + } else { + std::ostringstream ex; + ex << "[function error] arity <" << element->arity << "> of " + << (element->isOperator() ? "operator" : "function") << + " <" << element->name << "> is fl::null"; + throw Exception(ex.str(), FL_AT); + } + + } else if (not variable.empty()) { + if (not variables) { + throw Exception("[function error] " + "expected a map of variables, but none was provided", FL_AT); + } + std::map<std::string, scalar>::const_iterator it = variables->find(variable); + if (it != variables->end()) result = it->second; + else throw Exception("[function error] " + "unknown variable <" + variable + ">", FL_AT); + } else { + result = value; + } + return result; + } + + std::size_t Function::Node::treeSize(const Node* root) const { + if (not root) root = this; + std::size_t result = 0; + if (root->left.get()) { + result += treeSize(root->left.get()); + } + if (root->right.get()) { + result += treeSize(root->right.get()); + } + if (root->element.get()) { + result += 1; + } + return result; + } + + std::size_t Function::Node::treeSize(Element::Type type, const Node* root) const { + if (not root) root = this; + std::size_t result = 0; + if (root->left.get()) { + result += treeSize(type, root->left.get()); + } + if (root->right.get()) { + result += treeSize(type, root->right.get()); + } + if (root->element.get() and root->element->type == type) { + result += 1; + } + return result; + } + + Function::Node* Function::Node::clone() const { + return new Node(*this); + } + + std::string Function::Node::toString() const { + std::ostringstream ss; + if (element.get()) ss << element->name; + else if (not variable.empty()) ss << variable; + else ss << Op::str(value); + return ss.str(); + } + + std::string Function::Node::toPrefix(const Node* node) const { + if (not node) node = this; + if (not Op::isNaN(node->value)) { //is terminal + return Op::str(node->value); + } + if (not node->variable.empty()) { + return node->variable; + } + + std::ostringstream ss; + ss << node->toString(); + if (node->left.get()) + ss << " " << this->toPrefix(node->left.get()); + if (node->right.get()) + ss << " " << this->toPrefix(node->right.get()); + return ss.str(); + } + + std::string Function::Node::toInfix(const Node* node) const { + if (not node) node = this; + if (not Op::isNaN(node->value)) { //is proposition + return Op::str(node->value); + } + if (not node->variable.empty()) { + return node->variable; + } + + std::ostringstream ss; + if (node->left.get()) + ss << this->toInfix(node->left.get()) << " "; + ss << node->toString(); + if (node->right.get()) + ss << " " << this->toInfix(node->right.get()); + return ss.str(); + } + + std::string Function::Node::toPostfix(const Node* node) const { + if (not node) node = this; + if (not Op::isNaN(node->value)) { //is proposition + return Op::str(node->value); + } + if (not node->variable.empty()) { + return node->variable; + } + + std::ostringstream ss; + if (node->left.get()) + ss << this->toPostfix(node->left.get()) << " "; + if (node->right.get()) + ss << this->toPostfix(node->right.get()) << " "; + ss << node->toString(); + return ss.str(); + } + + /********************************** + * Function class. + **********************************/ + Function::Function(const std::string& name, + const std::string& formula, const Engine* engine) + : Term(name), _root(fl::null), _formula(formula), _engine(engine) { } + + Function::Function(const Function& other) : Term(other), + _root(fl::null), _formula(other._formula), _engine(other._engine) { + if (other._root.get()) _root.reset(other._root->clone()); + variables = other.variables; + } + + Function& Function::operator=(const Function& other) { + if (this != &other) { + _root.reset(fl::null); + + Term::operator=(other); + _formula = other._formula; + _engine = other._engine; + if (other._root.get()) _root.reset(other._root->clone()); + variables = other.variables; + } + return *this; + } + + Function::~Function() { } + + std::string Function::className() const { + return "Function"; + } + + Complexity Function::complexity() const { + Complexity result; + result.comparison(2 + 2); //membership(scalar) + membership(std::map) + if (_engine) { //insert variables in map + const std::size_t engineVariables = _engine->variables().size(); + result.function(engineVariables * std::log(scalar(variables.size() + engineVariables))); + result.function(1 * std::log(scalar(variables.size() + engineVariables))); + } + if (_root.get()) { + //Node::evaluate multiplies by tree size + const scalar treeSize = scalar(_root->treeSize()); + result.comparison(3 * treeSize); //if element, unary, binary + result.function(treeSize * std::log(treeSize)); //only operands in tree + } + return result; + } + + scalar Function::membership(scalar x) const { + if (not _root.get()) { + throw Exception("[function error] function <" + _formula + "> not loaded.", FL_AT); + } + if (_engine) { + for (std::size_t i = 0; i < _engine->numberOfInputVariables(); ++i) { + InputVariable* input = _engine->getInputVariable(i); + this->variables[input->getName()] = input->getValue(); + } + for (std::size_t i = 0; i < _engine->numberOfOutputVariables(); ++i) { + OutputVariable* output = _engine->getOutputVariable(i); + this->variables[output->getName()] = output->getValue(); + } + } + this->variables["x"] = x; + return this->evaluate(&this->variables); + } + + scalar Function::evaluate(const std::map<std::string, scalar>* localVariables) const { + if (not _root.get()) + throw Exception("[function error] evaluation failed because the function is not loaded", FL_AT); + if (localVariables) + return _root->evaluate(localVariables); + return _root->evaluate(&this->variables); + } + + std::string Function::parameters() const { + return getFormula(); + } + + void Function::configure(const std::string& parameters) { + load(parameters); + } + + Function* Function::create(const std::string& name, + const std::string& infix, const Engine* engine) { + FL_unique_ptr<Function> result(new Function(name)); + result->load(infix, engine); + return result.release(); + } + + bool Function::isLoaded() const { + return this->_root.get() != fl::null; + } + + void Function::unload() { + this->_root.reset(fl::null); + this->variables.clear(); + } + + void Function::load() { + load(getFormula()); + } + + void Function::load(const std::string& formula) { + load(formula, getEngine()); + } + + void Function::load(const std::string& formula, + const Engine* engine) { + setFormula(formula); + setEngine(engine); + this->_root.reset(parse(formula)); + membership(0.0); //make sure function evaluates without throwing exception. + } + + void Function::setFormula(const std::string& formula) { + this->_formula = formula; + } + + std::string Function::getFormula() const { + return this->_formula; + } + + void Function::setEngine(const Engine* engine) { + this->_engine = engine; + } + + const Engine* Function::getEngine() const { + return this->_engine; + } + + Function::Node* Function::root() const { + return this->_root.get(); + } + + Function* Function::clone() const { + return new Function(*this); + } + + Term* Function::constructor() { + return new Function; + } + + void Function::updateReference(const Engine* engine) { + setEngine(engine); + try { + load(); + } catch (...) { + //ignore + } + } + + std::string Function::space(const std::string& formula) const { + std::vector<std::string> chars; + chars.push_back("("); + chars.push_back(")"); + chars.push_back(","); + + std::vector<std::string> operators = FactoryManager::instance()->function()->availableOperators(); + for (std::size_t i = 0; i < operators.size(); ++i) { + if (not (operators.at(i) == Rule::andKeyword() or + operators.at(i) == Rule::orKeyword())) { + chars.push_back(operators.at(i)); + } + } + + std::string result = formula; + for (std::size_t i = 0; i < chars.size(); ++i) { + result = Op::findReplace(result, chars.at(i), " " + chars.at(i) + " "); + } + return result; + } + + /**************************************** + * The Glorious Parser + * Shunting-yard algorithm + * @todo: maybe change it for http://en.wikipedia.org/wiki/Operator-precedence_parser + ***************************************/ + + std::string Function::toPostfix(const std::string& formula) const { + std::string spacedFormula = space(formula); + + std::queue<std::string> queue; + std::stack<std::string> stack; + + std::stringstream tokenizer(spacedFormula); + std::string token; + FunctionFactory* factory = FactoryManager::instance()->function(); + while (tokenizer >> token) { + Element* element = factory->getObject(token); + bool isOperand = not element and token != "(" and token != ")" and token != ","; + + if (isOperand) { + queue.push(token); + + } else if (element and element->isFunction()) { + stack.push(token); + + } else if (token == ",") { + while (not stack.empty() and stack.top() != "(") { + queue.push(stack.top()); + stack.pop(); + } + if (stack.empty() or stack.top() != "(") { + std::ostringstream ex; + ex << "[parsing error] mismatching parentheses in: " << formula; + throw Exception(ex.str(), FL_AT); + } + + } else if (element and element->isOperator()) { + Element* op1 = element; + for (;;) { + Element* op2 = fl::null; + if (not stack.empty()) op2 = factory->getObject(stack.top()); + if (not op2) break; + + if ((op1->associativity < 0 and op1->precedence == op2->precedence) + or op1->precedence < op2->precedence) { + queue.push(stack.top()); + stack.pop(); + } else + break; + } + stack.push(token); + + } else if (token == "(") { + stack.push(token); + + } else if (token == ")") { + while (not stack.empty() and stack.top() != "(") { + queue.push(stack.top()); + stack.pop(); + } + if (stack.empty() or stack.top() != "(") { + std::ostringstream ex; + ex << "[parsing error] mismatching parentheses in: " << formula; + throw Exception(ex.str(), FL_AT); + } + stack.pop(); //get rid of "(" + + Element* top = fl::null; + if (not stack.empty()) top = factory->getObject(stack.top()); + if (top and top->isFunction()) { + queue.push(stack.top()); + stack.pop(); + } + } else { + std::ostringstream ex; + ex << "[parsing error] unexpected error with token <" << token << ">"; + throw Exception(ex.str(), FL_AT); + } + } + + while (not stack.empty()) { + if (stack.top() == "(" or stack.top() == ")") { + std::ostringstream ex; + ex << "[parsing error] mismatching parentheses in: " << formula; + throw Exception(ex.str(), FL_AT); + } + queue.push(stack.top()); + stack.pop(); + } + + std::stringstream ssPostfix; + while (not queue.empty()) { + ssPostfix << queue.front(); + queue.pop(); + if (not queue.empty()) ssPostfix << " "; + } + return ssPostfix.str(); + } + + Function::Node* Function::parse(const std::string& formula) { + if (formula.empty()) + throw Exception("[function error] formula is empty", FL_AT); + std::string postfix = toPostfix(formula); + + std::stack<Node*> stack; + + std::istringstream tokenizer(postfix); + std::string token; + FunctionFactory* factory = FactoryManager::instance()->function(); + while (tokenizer >> token) { + Element* element = factory->getObject(token); + bool isOperand = not element and token != "(" and token != ")" and token != ","; + + if (element) { + if (element->arity > static_cast<int> (stack.size())) { + std::ostringstream ss; + ss << "[function error] " << (element->isOperator() ? "operator" : "function") << + " <" << element->name << "> has arity <" << element->arity << ">, " + "but found <" << stack.size() << "> element" << + (stack.size() == 1 ? "" : "s"); + throw Exception(ss.str(), FL_AT); + } + + Node* node = new Node(element->clone()); + node->left.reset(stack.top()); + stack.pop(); + if (element->arity == 2) { + node->right.reset(stack.top()); + stack.pop(); + } + stack.push(node); + + } else if (isOperand) { + Node* node; + try { + scalar value = Op::toScalar(token); + node = new Node(value); + } catch (std::exception& ex) { + FL_IUNUSED(ex); + node = new Node(token); + } + stack.push(node); + } + } + + if (stack.size() != 1) + throw Exception("[function error] ill-formed formula <" + formula + ">", FL_AT); + + return stack.top(); + } + +} diff --git a/fuzzylite/src/term/Gaussian.cpp b/fuzzylite/src/term/Gaussian.cpp new file mode 100644 index 0000000..8413b59 --- /dev/null +++ b/fuzzylite/src/term/Gaussian.cpp @@ -0,0 +1,86 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/Gaussian.h" + +namespace fl { + + Gaussian::Gaussian(const std::string& name, + scalar mean, scalar standardDeviation, scalar height) + : Term(name, height), _mean(mean), _standardDeviation(standardDeviation) { } + + Gaussian::~Gaussian() { } + + std::string Gaussian::className() const { + return "Gaussian"; + } + + Complexity Gaussian::complexity() const { + return Complexity().comparison(1).arithmetic(7).function(1); + } + + scalar Gaussian::membership(scalar x) const { + if (Op::isNaN(x)) return fl::nan; + return Term::_height * std::exp((-(x - _mean) * (x - _mean)) / (2.0 * _standardDeviation * _standardDeviation)); + } + + std::string Gaussian::parameters() const { + return Op::join(2, " ", _mean, _standardDeviation) + + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); + } + + void Gaussian::configure(const std::string& parameters) { + if (parameters.empty()) return; + std::vector<std::string> values = Op::split(parameters, " "); + std::size_t required = 2; + if (values.size() < required) { + std::ostringstream ex; + ex << "[configuration error] term <" << className() << ">" + << " requires <" << required << "> parameters"; + throw Exception(ex.str(), FL_AT); + } + setMean(Op::toScalar(values.at(0))); + setStandardDeviation(Op::toScalar(values.at(1))); + if (values.size() > required) + setHeight(Op::toScalar(values.at(required))); + } + + void Gaussian::setMean(scalar mean) { + this->_mean = mean; + } + + scalar Gaussian::getMean() const { + return this->_mean; + } + + void Gaussian::setStandardDeviation(scalar standardDeviation) { + this->_standardDeviation = standardDeviation; + } + + scalar Gaussian::getStandardDeviation() const { + return this->_standardDeviation; + } + + Gaussian* Gaussian::clone() const { + return new Gaussian(*this); + } + + Term* Gaussian::constructor() { + return new Gaussian; + } + + +} diff --git a/fuzzylite/src/term/GaussianProduct.cpp b/fuzzylite/src/term/GaussianProduct.cpp new file mode 100644 index 0000000..791790c --- /dev/null +++ b/fuzzylite/src/term/GaussianProduct.cpp @@ -0,0 +1,117 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/GaussianProduct.h" + +namespace fl { + + GaussianProduct::GaussianProduct(const std::string& name, + scalar meanA, scalar standardDeviationA, scalar meanB, scalar standardDeviationB, + scalar height) + : Term(name, height), _meanA(meanA), _standardDeviationA(standardDeviationA), + _meanB(meanB), _standardDeviationB(standardDeviationB) { } + + GaussianProduct::~GaussianProduct() { } + + std::string GaussianProduct::className() const { + return "GaussianProduct"; + } + + Complexity GaussianProduct::complexity() const { + return Complexity().comparison(1 + 2).arithmetic(9 + 9 + 2).function(2); + } + + scalar GaussianProduct::membership(scalar x) const { + if (Op::isNaN(x)) return fl::nan; + + scalar a = 1.0, b = 1.0; + if (Op::isLt(x, _meanA)) { + a = std::exp((-(x - _meanA) * (x - _meanA)) / + (2.0 * _standardDeviationA * _standardDeviationA)); + } + if (Op::isGt(x, _meanB)) { + b = std::exp((-(x - _meanB) * (x - _meanB)) / + (2.0 * _standardDeviationB * _standardDeviationB)); + } + + return Term::_height * a * b; + } + + std::string GaussianProduct::parameters() const { + return Op::join(4, " ", _meanA, _standardDeviationA, _meanB, _standardDeviationB) + + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); + } + + void GaussianProduct::configure(const std::string& parameters) { + if (parameters.empty()) return; + std::vector<std::string> values = Op::split(parameters, " "); + std::size_t required = 4; + if (values.size() < required) { + std::ostringstream ex; + ex << "[configuration error] term <" << className() << ">" + << " requires <" << required << "> parameters"; + throw Exception(ex.str(), FL_AT); + } + setMeanA(Op::toScalar(values.at(0))); + setStandardDeviationA(Op::toScalar(values.at(1))); + setMeanB(Op::toScalar(values.at(2))); + setStandardDeviationB(Op::toScalar(values.at(3))); + if (values.size() > required) + setHeight(Op::toScalar(values.at(required))); + } + + void GaussianProduct::setMeanA(scalar meanA) { + this->_meanA = meanA; + } + + scalar GaussianProduct::getMeanA() const { + return this->_meanA; + } + + void GaussianProduct::setStandardDeviationA(scalar sigmaA) { + this->_standardDeviationA = sigmaA; + } + + scalar GaussianProduct::getStandardDeviationA() const { + return this->_standardDeviationA; + } + + void GaussianProduct::setMeanB(scalar meanB) { + this->_meanB = meanB; + } + + scalar GaussianProduct::getMeanB() const { + return this->_meanB; + } + + void GaussianProduct::setStandardDeviationB(scalar sigmaB) { + this->_standardDeviationB = sigmaB; + } + + scalar GaussianProduct::getStandardDeviationB() const { + return this->_standardDeviationB; + } + + GaussianProduct* GaussianProduct::clone() const { + return new GaussianProduct(*this); + } + + Term* GaussianProduct::constructor() { + return new GaussianProduct; + } + + +} diff --git a/fuzzylite/src/term/Linear.cpp b/fuzzylite/src/term/Linear.cpp new file mode 100644 index 0000000..e6efb43 --- /dev/null +++ b/fuzzylite/src/term/Linear.cpp @@ -0,0 +1,115 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/Linear.h" + +#include "fl/variable/InputVariable.h" + +namespace fl { + + Linear::Linear(const std::string& name, + const std::vector<scalar>& coefficients, + const Engine* engine) + : Term(name), _coefficients(coefficients), _engine(engine) { } + + Linear::~Linear() { } + + std::string Linear::className() const { + return "Linear"; + } + + Complexity Linear::complexity() const { + Complexity result; + result.comparison(1 + 1); + if (_engine) { + result.arithmetic(scalar(_engine->variables().size())); + result.comparison(scalar(_engine->variables().size())); //if (i < coefficients) + } + return result; + } + + scalar Linear::membership(scalar x) const { + FL_IUNUSED(x); + if (not _engine) + throw Exception("[linear error] term <" + getName() + "> " + "is missing a reference to the engine", FL_AT); + + scalar result = 0.0; + const std::size_t numberOfInputVariables = _engine->inputVariables().size(); + const std::size_t numberOfCoefficients = _coefficients.size(); + for (std::size_t i = 0; i < numberOfInputVariables; ++i) { + if (i < numberOfCoefficients) + result += _coefficients.at(i) * _engine->inputVariables().at(i)->getValue(); + } + if (numberOfCoefficients > numberOfInputVariables) { + result += _coefficients.back(); + } + return result; + } + + void Linear::set(const std::vector<scalar>& coefficients, const Engine* engine) { + setCoefficients(coefficients); + setEngine(engine); + } + + void Linear::setCoefficients(const std::vector<scalar>& coefficients) { + this->_coefficients = coefficients; + } + + const std::vector<scalar>& Linear::coefficients() const { + return this->_coefficients; + } + + std::vector<scalar>& Linear::coefficients() { + return this->_coefficients; + } + + void Linear::setEngine(const Engine* engine) { + this->_engine = engine; + } + + const Engine* Linear::getEngine() const { + return this->_engine; + } + + std::string Linear::parameters() const { + return Op::join(this->_coefficients, " "); + } + + void Linear::configure(const std::string& parameters) { + this->_coefficients.clear(); + if (parameters.empty()) return; + std::vector<std::string> strValues = Op::split(parameters, " "); + std::vector<scalar> values; + for (std::size_t i = 0; i < strValues.size(); ++i) { + values.push_back(Op::toScalar(strValues.at(i))); + } + this->_coefficients = values; + } + + Linear* Linear::clone() const { + return new Linear(*this); + } + + void Linear::updateReference(const Engine* engine) { + setEngine(engine); + } + + Term* Linear::constructor() { + return new Linear; + } + +} diff --git a/fuzzylite/src/term/PiShape.cpp b/fuzzylite/src/term/PiShape.cpp new file mode 100644 index 0000000..6c91f54 --- /dev/null +++ b/fuzzylite/src/term/PiShape.cpp @@ -0,0 +1,123 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/PiShape.h" + +namespace fl { + + PiShape::PiShape(const std::string& name, scalar bottomLeft, scalar topLeft, + scalar topRight, scalar bottomRight, scalar height) + : Term(name, height), _bottomLeft(bottomLeft), _topLeft(topLeft), + _topRight(topRight), _bottomRight(bottomRight) { } + + PiShape::~PiShape() { } + + std::string PiShape::className() const { + return "PiShape"; + } + + Complexity PiShape::complexity() const { + return Complexity().comparison(1 + 6).arithmetic(1 + 5 + 5).function(1 + 1); + } + + scalar PiShape::membership(scalar x) const { + if (Op::isNaN(x)) return fl::nan; + + scalar sshape; + if (Op::isLE(x, _bottomLeft)) + sshape = 0.0; + else if (Op::isLE(x, 0.5 * (_bottomLeft + _topLeft))) + sshape = 2.0 * std::pow((x - _bottomLeft) / (_topLeft - _bottomLeft), 2); + else if (Op::isLt(x, _topLeft)) + sshape = 1.0 - 2.0 * std::pow((x - _topLeft) / (_topLeft - _bottomLeft), 2); + else sshape = 1.0; + + scalar zshape; + if (Op::isLE(x, _topRight)) + zshape = 1.0; + else if (Op::isLE(x, 0.5 * (_topRight + _bottomRight))) + zshape = 1.0 - 2.0 * std::pow((x - _topRight) / (_bottomRight - _topRight), 2); + else if (Op::isLt(x, _bottomRight)) + zshape = 2.0 * std::pow((x - _bottomRight) / (_bottomRight - _topRight), 2); + else zshape = 0.0; + + return Term::_height * sshape * zshape; + } + + std::string PiShape::parameters() const { + return Op::join(4, " ", _bottomLeft, _topLeft, _topRight, _bottomRight) + + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); + } + + void PiShape::configure(const std::string& parameters) { + if (parameters.empty()) return; + std::vector<std::string> values = Op::split(parameters, " "); + std::size_t required = 4; + if (values.size() < required) { + std::ostringstream ex; + ex << "[configuration error] term <" << className() << ">" + << " requires <" << required << "> parameters"; + throw Exception(ex.str(), FL_AT); + } + setBottomLeft(Op::toScalar(values.at(0))); + setTopLeft(Op::toScalar(values.at(1))); + setTopRight(Op::toScalar(values.at(2))); + setBottomRight(Op::toScalar(values.at(3))); + if (values.size() > required) + setHeight(Op::toScalar(values.at(required))); + } + + void PiShape::setBottomLeft(scalar a) { + this->_bottomLeft = a; + } + + scalar PiShape::getBottomLeft() const { + return this->_bottomLeft; + } + + void PiShape::setTopLeft(scalar b) { + this->_topLeft = b; + } + + scalar PiShape::getTopLeft() const { + return this->_topLeft; + } + + void PiShape::setTopRight(scalar d) { + this->_topRight = d; + } + + scalar PiShape::getTopRight() const { + return this->_topRight; + } + + void PiShape::setBottomRight(scalar c) { + this->_bottomRight = c; + } + + scalar PiShape::getBottomRight() const { + return this->_bottomRight; + } + + PiShape* PiShape::clone() const { + return new PiShape(*this); + } + + Term* PiShape::constructor() { + return new PiShape; + } + +} diff --git a/fuzzylite/src/term/Ramp.cpp b/fuzzylite/src/term/Ramp.cpp new file mode 100644 index 0000000..d29233d --- /dev/null +++ b/fuzzylite/src/term/Ramp.cpp @@ -0,0 +1,119 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/Ramp.h" + +namespace fl { + + Ramp::Ramp(const std::string& name, scalar start, scalar end, scalar height) + : Term(name, height), _start(start), _end(end) { } + + Ramp::~Ramp() { } + + std::string Ramp::className() const { + return "Ramp"; + } + + Complexity Ramp::complexity() const { + return Complexity().comparison(1 + 4).arithmetic(1 + 3); + } + + scalar Ramp::membership(scalar x) const { + if (Op::isNaN(x)) return fl::nan; + + if (Op::isEq(_start, _end)) + return Term::_height * 0.0; + + if (Op::isLt(_start, _end)) { + if (Op::isLE(x, _start)) + return Term::_height * 0.0; + if (Op::isGE(x, _end)) + return Term::_height * 1.0; + return Term::_height * (x - _start) / (_end - _start); + } else { + if (Op::isGE(x, _start)) + return Term::_height * 0.0; + if (Op::isLE(x, _end)) + return Term::_height * 1.0; + return Term::_height * (_start - x) / (_start - _end); + } + } + + scalar Ramp::tsukamoto(scalar activationDegree, scalar minimum, scalar maximum) const { + FL_IUNUSED(minimum); + FL_IUNUSED(maximum); + return Op::scale(activationDegree, 0, 1, _start, _end); + } + + bool Ramp::isMonotonic() const { + return true; + } + + std::string Ramp::parameters() const { + return Op::join(2, " ", _start, _end) + + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); + } + + void Ramp::configure(const std::string& parameters) { + if (parameters.empty()) return; + std::vector<std::string> values = Op::split(parameters, " "); + std::size_t required = 2; + if (values.size() < required) { + std::ostringstream ex; + ex << "[configuration error] term <" << className() << ">" + << " requires <" << required << "> parameters"; + throw Exception(ex.str(), FL_AT); + } + setStart(Op::toScalar(values.at(0))); + setEnd(Op::toScalar(values.at(1))); + if (values.size() > required) + setHeight(Op::toScalar(values.at(required))); + } + + void Ramp::setStart(scalar start) { + this->_start = start; + } + + scalar Ramp::getStart() const { + return this->_start; + } + + void Ramp::setEnd(scalar end) { + this->_end = end; + } + + scalar Ramp::getEnd() const { + return this->_end; + } + + Ramp::Direction Ramp::direction() const { + scalar range = this->_end - this->_start; + if (not Op::isFinite(range) or Op::isEq(range, 0.0)) return Zero; + + if (Op::isGt(range, 0.0)) return Positive; + + return Negative; + } + + Ramp* Ramp::clone() const { + return new Ramp(*this); + } + + Term* Ramp::constructor() { + return new Ramp; + } + +} diff --git a/fuzzylite/src/term/Rectangle.cpp b/fuzzylite/src/term/Rectangle.cpp new file mode 100644 index 0000000..4f975d0 --- /dev/null +++ b/fuzzylite/src/term/Rectangle.cpp @@ -0,0 +1,87 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/Rectangle.h" + +namespace fl { + + Rectangle::Rectangle(const std::string& name, scalar start, scalar end, scalar height) + : Term(name, height), _start(start), _end(end) { } + + Rectangle::~Rectangle() { } + + std::string Rectangle::className() const { + return "Rectangle"; + } + + Complexity Rectangle::complexity() const { + return Complexity().comparison(1 + 2).arithmetic(1); + } + + scalar Rectangle::membership(scalar x) const { + if (Op::isNaN(x)) return fl::nan; + if (Op::isGE(x, _start) and Op::isLE(x, _end)) + return Term::_height * 1.0; + return Term::_height * 0.0; + } + + std::string Rectangle::parameters() const { + return Op::join(2, " ", _start, _end) + + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); + } + + void Rectangle::configure(const std::string& parameters) { + if (parameters.empty()) return; + std::vector<std::string> values = Op::split(parameters, " "); + std::size_t required = 2; + if (values.size() < required) { + std::ostringstream ex; + ex << "[configuration error] term <" << className() << ">" + << " requires <" << required << "> parameters"; + throw Exception(ex.str(), FL_AT); + } + setStart(Op::toScalar(values.at(0))); + setEnd(Op::toScalar(values.at(1))); + if (values.size() > required) + setHeight(Op::toScalar(values.at(required))); + } + + void Rectangle::setStart(scalar minimum) { + this->_start = minimum; + } + + scalar Rectangle::getStart() const { + return this->_start; + } + + void Rectangle::setEnd(scalar maximum) { + this->_end = maximum; + } + + scalar Rectangle::getEnd() const { + return this->_end; + } + + Rectangle* Rectangle::clone() const { + return new Rectangle(*this); + } + + Term* Rectangle::constructor() { + return new Rectangle; + } + + +} diff --git a/fuzzylite/src/term/SShape.cpp b/fuzzylite/src/term/SShape.cpp new file mode 100644 index 0000000..65dfd2b --- /dev/null +++ b/fuzzylite/src/term/SShape.cpp @@ -0,0 +1,116 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/SShape.h" + +namespace fl { + + SShape::SShape(const std::string& name, scalar start, scalar end, scalar height) + : Term(name, height), _start(start), _end(end) { } + + SShape::~SShape() { } + + std::string SShape::className() const { + return "SShape"; + } + + Complexity SShape::complexity() const { + return Complexity().comparison(1 + 3).arithmetic(1 + 3 + 4).function(1); + } + + scalar SShape::membership(scalar x) const { + if (Op::isNaN(x)) return fl::nan; + + if (Op::isLE(x, _start)) + return Term::_height * 0.0; + + if (Op::isLE(x, 0.5 * (_start + _end))) + return Term::_height * (2.0 * std::pow((x - _start) / (_end - _start), 2)); + + if (Op::isLt(x, _end)) + return Term::_height * (1.0 - 2.0 * std::pow((x - _end) / (_end - _start), 2)); + + return Term::_height * 1.0; + } + + scalar SShape::tsukamoto(scalar activationDegree, scalar minimum, scalar maximum) const { + FL_IUNUSED(minimum); + FL_IUNUSED(maximum); + + scalar w = activationDegree; + scalar z = fl::nan; + + scalar difference = _end - _start; + scalar a = _start + std::sqrt(0.5 * w * difference * difference); + scalar b = _end + std::sqrt(-0.5 * (w - 1.0) * difference * difference); + if (std::abs(w - membership(a)) < std::abs(w - membership(b))) { + z = a; + } else { + z = b; + } + return z; + } + + bool SShape::isMonotonic() const { + return true; + } + + std::string SShape::parameters() const { + return Op::join(2, " ", _start, _end) + + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); + } + + void SShape::configure(const std::string& parameters) { + if (parameters.empty()) return; + std::vector<std::string> values = Op::split(parameters, " "); + std::size_t required = 2; + if (values.size() < required) { + std::ostringstream ex; + ex << "[configuration error] term <" << className() << ">" + << " requires <" << required << "> parameters"; + throw Exception(ex.str(), FL_AT); + } + setStart(Op::toScalar(values.at(0))); + setEnd(Op::toScalar(values.at(1))); + if (values.size() > required) + setHeight(Op::toScalar(values.at(required))); + } + + void SShape::setStart(scalar start) { + this->_start = start; + } + + scalar SShape::getStart() const { + return this->_start; + } + + void SShape::setEnd(scalar end) { + this->_end = end; + } + + scalar SShape::getEnd() const { + return this->_end; + } + + SShape* SShape::clone() const { + return new SShape(*this); + } + + Term* SShape::constructor() { + return new SShape; + } + +} diff --git a/fuzzylite/src/term/Sigmoid.cpp b/fuzzylite/src/term/Sigmoid.cpp new file mode 100644 index 0000000..fcf165e --- /dev/null +++ b/fuzzylite/src/term/Sigmoid.cpp @@ -0,0 +1,124 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/Sigmoid.h" + +namespace fl { + + Sigmoid::Sigmoid(const std::string& name, scalar inflection, scalar slope, scalar height) + : Term(name, height), _inflection(inflection), _slope(slope) { } + + Sigmoid::~Sigmoid() { } + + std::string Sigmoid::className() const { + return "Sigmoid"; + } + + Complexity Sigmoid::complexity() const { + return Complexity().comparison(1).arithmetic(1 + 4).function(1); + } + + scalar Sigmoid::membership(scalar x) const { + if (Op::isNaN(x)) return fl::nan; + return Term::_height * 1.0 / (1.0 + std::exp(-_slope * (x - _inflection))); + } + + scalar Sigmoid::tsukamoto(scalar activationDegree, + scalar minimum, scalar maximum) const { + + scalar w = activationDegree; + scalar z = fl::nan; + + if (Op::isEq(w, 1.0)) { + if (Op::isGE(_slope, 0.0)) { + z = maximum; + } else { + z = minimum; + } + + } else if (Op::isEq(w, 0.0)) { + if (Op::isGE(_slope, 0.0)) { + z = minimum; + } else { + z = maximum; + } + } else { + scalar a = _slope; + scalar b = _inflection; + z = b + (std::log(1.0 / w - 1.0) / -a); + } + + return z; + } + + bool Sigmoid::isMonotonic() const { + return true; + } + + std::string Sigmoid::parameters() const { + return Op::join(2, " ", _inflection, _slope) + + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); + } + + void Sigmoid::configure(const std::string& parameters) { + if (parameters.empty()) return; + std::vector<std::string> values = Op::split(parameters, " "); + std::size_t required = 2; + if (values.size() < required) { + std::ostringstream ex; + ex << "[configuration error] term <" << className() << ">" + << " requires <" << required << "> parameters"; + throw Exception(ex.str(), FL_AT); + } + setInflection(Op::toScalar(values.at(0))); + setSlope(Op::toScalar(values.at(1))); + if (values.size() > required) + setHeight(Op::toScalar(values.at(required))); + } + + void Sigmoid::setSlope(scalar a) { + this->_slope = a; + } + + scalar Sigmoid::getSlope() const { + return this->_slope; + } + + void Sigmoid::setInflection(scalar c) { + this->_inflection = c; + } + + scalar Sigmoid::getInflection() const { + return this->_inflection; + } + + Sigmoid::Direction Sigmoid::direction() const { + if (not Op::isFinite(_slope) or Op::isEq(_slope, 0.0)) return Zero; + + if (Op::isGt(_slope, 0.0)) return Positive; + + return Negative; + } + + Sigmoid* Sigmoid::clone() const { + return new Sigmoid(*this); + } + + Term* Sigmoid::constructor() { + return new Sigmoid; + } + +} diff --git a/fuzzylite/src/term/SigmoidDifference.cpp b/fuzzylite/src/term/SigmoidDifference.cpp new file mode 100644 index 0000000..8f7ee80 --- /dev/null +++ b/fuzzylite/src/term/SigmoidDifference.cpp @@ -0,0 +1,108 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/SigmoidDifference.h" + +namespace fl { + + SigmoidDifference::SigmoidDifference(const std::string& name, + scalar left, scalar rising, + scalar falling, scalar right, scalar height) + : Term(name, height), _left(left), _rising(rising), _falling(falling), _right(right) { } + + SigmoidDifference::~SigmoidDifference() { } + + std::string SigmoidDifference::className() const { + return "SigmoidDifference"; + } + + Complexity SigmoidDifference::complexity() const { + return Complexity().comparison(1).arithmetic(2 + 4 + 4).function(2 + 1); + } + + scalar SigmoidDifference::membership(scalar x) const { + if (Op::isNaN(x)) return fl::nan; + + const scalar a = 1.0 / (1.0 + std::exp(-_rising * (x - _left))); + const scalar b = 1.0 / (1.0 + std::exp(-_falling * (x - _right))); + return Term::_height * std::abs(a - b); + } + + std::string SigmoidDifference::parameters() const { + return Op::join(4, " ", _left, _rising, _falling, _right) + + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); + } + + void SigmoidDifference::configure(const std::string& parameters) { + if (parameters.empty()) return; + std::vector<std::string> values = Op::split(parameters, " "); + std::size_t required = 4; + if (values.size() < required) { + std::ostringstream ex; + ex << "[configuration error] term <" << className() << ">" + << " requires <" << required << "> parameters"; + throw Exception(ex.str(), FL_AT); + } + setLeft(Op::toScalar(values.at(0))); + setRising(Op::toScalar(values.at(1))); + setFalling(Op::toScalar(values.at(2))); + setRight(Op::toScalar(values.at(3))); + if (values.size() > required) + setHeight(Op::toScalar(values.at(required))); + } + + void SigmoidDifference::setLeft(scalar leftInflection) { + this->_left = leftInflection; + } + + scalar SigmoidDifference::getLeft() const { + return this->_left; + } + + void SigmoidDifference::setRising(scalar risingSlope) { + this->_rising = risingSlope; + } + + scalar SigmoidDifference::getRising() const { + return this->_rising; + } + + void SigmoidDifference::setFalling(scalar fallingSlope) { + this->_falling = fallingSlope; + } + + scalar SigmoidDifference::getFalling() const { + return this->_falling; + } + + void SigmoidDifference::setRight(scalar rightInflection) { + this->_right = rightInflection; + } + + scalar SigmoidDifference::getRight() const { + return this->_right; + } + + SigmoidDifference* SigmoidDifference::clone() const { + return new SigmoidDifference(*this); + } + + Term* SigmoidDifference::constructor() { + return new SigmoidDifference; + } + + +} diff --git a/fuzzylite/src/term/SigmoidProduct.cpp b/fuzzylite/src/term/SigmoidProduct.cpp new file mode 100644 index 0000000..5f35750 --- /dev/null +++ b/fuzzylite/src/term/SigmoidProduct.cpp @@ -0,0 +1,107 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/SigmoidProduct.h" + +namespace fl { + + SigmoidProduct::SigmoidProduct(const std::string& name, + scalar left, scalar rising, + scalar falling, scalar right, scalar height) + : Term(name, height), _left(left), _rising(rising), _falling(falling), _right(right) { } + + SigmoidProduct::~SigmoidProduct() { } + + std::string SigmoidProduct::className() const { + return "SigmoidProduct"; + } + + Complexity SigmoidProduct::complexity() const { + return Complexity().comparison(1).arithmetic(2 + 4 + 4).function(2); + } + + scalar SigmoidProduct::membership(scalar x) const { + if (Op::isNaN(x)) return fl::nan; + const scalar a = 1.0 + std::exp(-_rising * (x - _left)); + const scalar b = 1.0 + std::exp(-_falling * (x - _right)); + return Term::_height * 1.0 / (a * b); + } + + std::string SigmoidProduct::parameters() const { + return Op::join(4, " ", _left, _rising, _falling, _right) + + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); + } + + void SigmoidProduct::configure(const std::string& parameters) { + if (parameters.empty()) return; + std::vector<std::string> values = Op::split(parameters, " "); + std::size_t required = 4; + if (values.size() < required) { + std::ostringstream ex; + ex << "[configuration error] term <" << className() << ">" + << " requires <" << required << "> parameters"; + throw Exception(ex.str(), FL_AT); + } + setLeft(Op::toScalar(values.at(0))); + setRising(Op::toScalar(values.at(1))); + setFalling(Op::toScalar(values.at(2))); + setRight(Op::toScalar(values.at(3))); + if (values.size() > required) + setHeight(Op::toScalar(values.at(required))); + + } + + void SigmoidProduct::setRising(scalar risingSlope) { + this->_rising = risingSlope; + } + + scalar SigmoidProduct::getRising() const { + return this->_rising; + } + + void SigmoidProduct::setLeft(scalar leftInflection) { + this->_left = leftInflection; + } + + scalar SigmoidProduct::getLeft() const { + return this->_left; + } + + void SigmoidProduct::setRight(scalar rightInflection) { + this->_right = rightInflection; + } + + scalar SigmoidProduct::getRight() const { + return this->_right; + } + + void SigmoidProduct::setFalling(scalar fallingSlope) { + this->_falling = fallingSlope; + } + + scalar SigmoidProduct::getFalling() const { + return this->_falling; + } + + SigmoidProduct* SigmoidProduct::clone() const { + return new SigmoidProduct(*this); + } + + Term* SigmoidProduct::constructor() { + return new SigmoidProduct; + } + +} diff --git a/fuzzylite/src/term/Spike.cpp b/fuzzylite/src/term/Spike.cpp new file mode 100644 index 0000000..a19810d --- /dev/null +++ b/fuzzylite/src/term/Spike.cpp @@ -0,0 +1,84 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/Spike.h" + +namespace fl { + + Spike::Spike(const std::string& name, scalar center, scalar width, scalar height) + : Term(name, height), _center(center), _width(width) { } + + Spike::~Spike() { } + + std::string Spike::className() const { + return "Spike"; + } + + Complexity Spike::complexity() const { + return Complexity().comparison(1).arithmetic(1 + 3).function(2); + } + + scalar Spike::membership(scalar x) const { + if (Op::isNaN(x)) return fl::nan; + return Term::_height * std::exp(-std::abs(10.0 / _width * (x - _center))); + } + + std::string Spike::parameters() const { + return Op::join(2, " ", _center, _width) + + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); + } + + void Spike::configure(const std::string& parameters) { + if (parameters.empty()) return; + std::vector<std::string> values = Op::split(parameters, " "); + std::size_t required = 2; + if (values.size() < required) { + std::ostringstream ex; + ex << "[configuration error] term <" << className() << ">" + << " requires <" << required << "> parameters"; + throw Exception(ex.str(), FL_AT); + } + setCenter(Op::toScalar(values.at(0))); + setWidth(Op::toScalar(values.at(1))); + if (values.size() > required) + setHeight(Op::toScalar(values.at(required))); + } + + void Spike::setCenter(scalar center) { + this->_center = center; + } + + scalar Spike::getCenter() const { + return this->_center; + } + + void Spike::setWidth(scalar width) { + this->_width = width; + } + + scalar Spike::getWidth() const { + return this->_width; + } + + Spike* Spike::clone() const { + return new Spike(*this); + } + + Term* Spike::constructor() { + return new Spike; + } + +} diff --git a/fuzzylite/src/term/Term.cpp b/fuzzylite/src/term/Term.cpp new file mode 100644 index 0000000..bf7520d --- /dev/null +++ b/fuzzylite/src/term/Term.cpp @@ -0,0 +1,65 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/Term.h" + +#include "fl/imex/FllExporter.h" +#include "fl/term/Linear.h" +#include "fl/term/Function.h" + +namespace fl { + + Term::Term(const std::string& name, scalar height) : _name(name), _height(height) { } + + Term::~Term() { } + + void Term::setName(const std::string& name) { + this->_name = name; + } + + std::string Term::getName() const { + return this->_name; + } + + void Term::setHeight(scalar height) { + this->_height = height; + } + + scalar Term::getHeight() const { + return this->_height; + } + + std::string Term::toString() const { + return FllExporter().toString(this); + } + + void Term::updateReference(const Engine* engine) { + FL_IUNUSED(engine); + //do nothing + } + + scalar Term::tsukamoto(scalar activationDegree, scalar minimum, scalar maximum) const { + FL_IUNUSED(minimum); + FL_IUNUSED(maximum); + return membership(activationDegree); + } + + bool Term::isMonotonic() const { + return false; + } + + +} diff --git a/fuzzylite/src/term/Trapezoid.cpp b/fuzzylite/src/term/Trapezoid.cpp new file mode 100644 index 0000000..4773dbb --- /dev/null +++ b/fuzzylite/src/term/Trapezoid.cpp @@ -0,0 +1,128 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/Trapezoid.h" + +namespace fl { + + Trapezoid::Trapezoid(const std::string& name, + scalar vertexA, scalar vertexB, scalar vertexC, scalar vertexD, scalar height) + : Term(name, height), _vertexA(vertexA), _vertexB(vertexB), _vertexC(vertexC), _vertexD(vertexD) { + if (Op::isNaN(vertexC) and Op::isNaN(vertexD)) { + this->_vertexD = _vertexB; + scalar range = _vertexD - _vertexA; + this->_vertexB = _vertexA + range * 1.0 / 5.0; + this->_vertexC = _vertexA + range * 4.0 / 5.0; + } + } + + Trapezoid::~Trapezoid() { } + + std::string Trapezoid::className() const { + return "Trapezoid"; + } + + Complexity Trapezoid::complexity() const { + return Complexity().comparison(1 + 6).arithmetic(1 + 3).function(1); + } + + scalar Trapezoid::membership(scalar x) const { + if (Op::isNaN(x)) return fl::nan; + + if (Op::isLt(x, _vertexA) or Op::isGt(x, _vertexD)) + return Term::_height * 0.0; + + if (Op::isLt(x, _vertexB)) { + if (_vertexA == -fl::inf) return Term::_height * 1.0; + return Term::_height * Op::min(scalar(1.0), (x - _vertexA) / (_vertexB - _vertexA)); + } + if (Op::isLE(x, _vertexC)) + return Term::_height * 1.0; + + if (Op::isLt(x, _vertexD)) { + if (_vertexD == fl::inf) return Term::_height * 1.0; + return Term::_height * (_vertexD - x) / (_vertexD - _vertexC); + } + + if (_vertexD == fl::inf) return Term::_height * 1.0; + return Term::_height * 0.0; + } + + std::string Trapezoid::parameters() const { + return Op::join(4, " ", _vertexA, _vertexB, _vertexC, _vertexD)+ + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); + } + + void Trapezoid::configure(const std::string& parameters) { + if (parameters.empty()) return; + std::vector<std::string> values = Op::split(parameters, " "); + std::size_t required = 4; + if (values.size() < required) { + std::ostringstream ex; + ex << "[configuration error] term <" << className() << ">" + << " requires <" << required << "> parameters"; + throw Exception(ex.str(), FL_AT); + } + setVertexA(Op::toScalar(values.at(0))); + setVertexB(Op::toScalar(values.at(1))); + setVertexC(Op::toScalar(values.at(2))); + setVertexD(Op::toScalar(values.at(3))); + if (values.size() > required) + setHeight(Op::toScalar(values.at(required))); + } + + void Trapezoid::setVertexA(scalar a) { + this->_vertexA = a; + } + + scalar Trapezoid::getVertexA() const { + return this->_vertexA; + } + + void Trapezoid::setVertexB(scalar b) { + this->_vertexB = b; + } + + scalar Trapezoid::getVertexB() const { + return this->_vertexB; + } + + void Trapezoid::setVertexC(scalar c) { + this->_vertexC = c; + } + + scalar Trapezoid::getVertexC() const { + return this->_vertexC; + } + + void Trapezoid::setVertexD(scalar d) { + this->_vertexD = d; + } + + scalar Trapezoid::getVertexD() const { + return this->_vertexD; + } + + Trapezoid* Trapezoid::clone() const { + return new Trapezoid(*this); + } + + Term* Trapezoid::constructor() { + return new Trapezoid; + } + + +} diff --git a/fuzzylite/src/term/Triangle.cpp b/fuzzylite/src/term/Triangle.cpp new file mode 100644 index 0000000..9d1835c --- /dev/null +++ b/fuzzylite/src/term/Triangle.cpp @@ -0,0 +1,113 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/Triangle.h" + +namespace fl { + + Triangle::Triangle(const std::string& name, scalar vertexA, scalar vertexB, scalar vertexC, scalar height) + : Term(name, height), _vertexA(vertexA), _vertexB(vertexB), _vertexC(vertexC) { + if (Op::isNaN(vertexC)) { + this->_vertexC = _vertexB; + this->_vertexB = 0.5 * (_vertexA + _vertexB); + } + } + + Triangle::~Triangle() { } + + std::string Triangle::className() const { + return "Triangle"; + } + + Complexity Triangle::complexity() const { + return Complexity().comparison(1 + 5).arithmetic(4); + } + + scalar Triangle::membership(scalar x) const { + if (Op::isNaN(x)) return fl::nan; + + if (Op::isLt(x, _vertexA) or Op::isGt(x, _vertexC)) + return Term::_height * 0.0; + + if (Op::isEq(x, _vertexB)) + return Term::_height * 1.0; + + if (Op::isLt(x, _vertexB)) { + if (_vertexA == -fl::inf) + return Term::_height * 1.0; + return Term::_height * (x - _vertexA) / (_vertexB - _vertexA); + } + if (_vertexC == fl::inf) + return Term::_height * 1.0; + return Term::_height * (_vertexC - x) / (_vertexC - _vertexB); + } + + std::string Triangle::parameters() const { + return Op::join(3, " ", _vertexA, _vertexB, _vertexC) + + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); + } + + void Triangle::configure(const std::string& parameters) { + if (parameters.empty()) return; + std::vector<std::string> values = Op::split(parameters, " "); + std::size_t required = 3; + if (values.size() < required) { + std::ostringstream ex; + ex << "[configuration error] term <" << className() << ">" + << " requires <" << required << "> parameters"; + throw Exception(ex.str(), FL_AT); + } + setVertexA(Op::toScalar(values.at(0))); + setVertexB(Op::toScalar(values.at(1))); + setVertexC(Op::toScalar(values.at(2))); + if (values.size() > required) + setHeight(Op::toScalar(values.at(required))); + } + + void Triangle::setVertexA(scalar a) { + this->_vertexA = a; + } + + scalar Triangle::getVertexA() const { + return this->_vertexA; + } + + void Triangle::setVertexB(scalar b) { + this->_vertexB = b; + } + + scalar Triangle::getVertexB() const { + return this->_vertexB; + } + + void Triangle::setVertexC(scalar c) { + this->_vertexC = c; + } + + scalar Triangle::getVertexC() const { + return this->_vertexC; + } + + Triangle* Triangle::clone() const { + return new Triangle(*this); + } + + Term* Triangle::constructor() { + return new Triangle; + } + + +} diff --git a/fuzzylite/src/term/ZShape.cpp b/fuzzylite/src/term/ZShape.cpp new file mode 100644 index 0000000..9054b20 --- /dev/null +++ b/fuzzylite/src/term/ZShape.cpp @@ -0,0 +1,116 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/term/ZShape.h" + +namespace fl { + + ZShape::ZShape(const std::string& name, scalar start, scalar end, scalar height) + : Term(name, height), _start(start), _end(end) { } + + ZShape::~ZShape() { } + + std::string ZShape::className() const { + return "ZShape"; + } + + Complexity ZShape::complexity() const { + return Complexity().comparison(1 + 3).arithmetic(3 + 4).function(1); + } + + scalar ZShape::membership(scalar x) const { + if (Op::isNaN(x)) return fl::nan; + + if (Op::isLE(x, _start)) + return Term::_height * 1.0; + + if (Op::isLE(x, 0.5 * (_start + _end))) + return Term::_height * (1.0 - 2.0 * std::pow((x - _start) / (_end - _start), 2)); + + if (Op::isLt(x, _end)) + return Term::_height * (2.0 * std::pow((x - _end) / (_end - _start), 2)); + + return Term::_height * 0.0; + } + + scalar ZShape::tsukamoto(scalar activationDegree, scalar minimum, scalar maximum) const { + FL_IUNUSED(minimum); + FL_IUNUSED(maximum); + + scalar w = activationDegree; + scalar z = fl::nan; + + scalar difference = _end - _start; + scalar a = _start + std::sqrt(-0.5 * (w - 1.0) * difference * difference); + scalar b = _end + std::sqrt(0.5 * w * difference * difference); + if (std::abs(w - membership(a)) < std::abs(w - membership(b))) { + z = a; + } else { + z = b; + } + return z; + } + + bool ZShape::isMonotonic() const { + return true; + } + + std::string ZShape::parameters() const { + return Op::join(2, " ", _start, _end) + + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); + } + + void ZShape::configure(const std::string& parameters) { + if (parameters.empty()) return; + std::vector<std::string> values = Op::split(parameters, " "); + std::size_t required = 2; + if (values.size() < required) { + std::ostringstream ex; + ex << "[configuration error] term <" << className() << ">" + << " requires <" << required << "> parameters"; + throw Exception(ex.str(), FL_AT); + } + setStart(Op::toScalar(values.at(0))); + setEnd(Op::toScalar(values.at(1))); + if (values.size() > required) + setHeight(Op::toScalar(values.at(required))); + } + + void ZShape::setStart(scalar start) { + this->_start = start; + } + + scalar ZShape::getStart() const { + return this->_start; + } + + void ZShape::setEnd(scalar end) { + this->_end = end; + } + + scalar ZShape::getEnd() const { + return this->_end; + } + + ZShape* ZShape::clone() const { + return new ZShape(*this); + } + + Term* ZShape::constructor() { + return new ZShape; + } + +} diff --git a/fuzzylite/src/variable/InputVariable.cpp b/fuzzylite/src/variable/InputVariable.cpp new file mode 100644 index 0000000..1364d8f --- /dev/null +++ b/fuzzylite/src/variable/InputVariable.cpp @@ -0,0 +1,44 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/variable/InputVariable.h" + +#include "fl/imex/FllExporter.h" + +namespace fl { + + InputVariable::InputVariable(const std::string& name, scalar minimum, scalar maximum) + : Variable(name, minimum, maximum) { } + + InputVariable::~InputVariable() { } + + std::string InputVariable::fuzzyInputValue() const { + return fuzzify(getValue()); + } + + Variable::Type InputVariable::type() const { + return Variable::Input; + } + + std::string InputVariable::toString() const { + return FllExporter().toString(this); + } + + InputVariable* InputVariable::clone() const { + return new InputVariable(*this); + } + +} diff --git a/fuzzylite/src/variable/OutputVariable.cpp b/fuzzylite/src/variable/OutputVariable.cpp new file mode 100644 index 0000000..0f85dc4 --- /dev/null +++ b/fuzzylite/src/variable/OutputVariable.cpp @@ -0,0 +1,226 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/variable/OutputVariable.h" + +#include "fl/imex/FllExporter.h" + +namespace fl { + + OutputVariable::OutputVariable(const std::string& name, + scalar minimum, scalar maximum) + : Variable(name, minimum, maximum), + _fuzzyOutput(new Aggregated(name, minimum, maximum)), + _previousValue(fl::nan), _defaultValue(fl::nan), + _lockPreviousValue(false) { } + + OutputVariable::OutputVariable(const OutputVariable& other) : Variable(other) { + copyFrom(other); + } + + OutputVariable& OutputVariable::operator=(const OutputVariable& other) { + if (this != &other) { + _fuzzyOutput.reset(fl::null); + _defuzzifier.reset(fl::null); + + Variable::operator=(other); + copyFrom(other); + } + return *this; + } + + OutputVariable::~OutputVariable() { } + + void OutputVariable::copyFrom(const OutputVariable& other) { + _fuzzyOutput.reset(other._fuzzyOutput->clone()); + if (other._defuzzifier.get()) _defuzzifier.reset(other._defuzzifier->clone()); + _previousValue = other._previousValue; + _defaultValue = other._defaultValue; + _lockPreviousValue = other._lockPreviousValue; + } + + void OutputVariable::setName(const std::string& name) { + Variable::setName(name); + _fuzzyOutput->setName(name); + } + + Aggregated* OutputVariable::fuzzyOutput() const { + return this->_fuzzyOutput.get(); + } + + void OutputVariable::setMinimum(scalar minimum) { + Variable::setMinimum(minimum); + _fuzzyOutput->setMinimum(minimum); + } + + void OutputVariable::setMaximum(scalar maximum) { + Variable::setMaximum(maximum); + _fuzzyOutput->setMaximum(maximum); + } + + void OutputVariable::setDefuzzifier(Defuzzifier* defuzzifier) { + this->_defuzzifier.reset(defuzzifier); + } + + Defuzzifier* OutputVariable::getDefuzzifier() const { + return this->_defuzzifier.get(); + } + + void OutputVariable::setAggregation(SNorm* aggregation) { + this->_fuzzyOutput->setAggregation(aggregation); + } + + SNorm* OutputVariable::getAggregation() const { + return this->_fuzzyOutput->getAggregation(); + } + + void OutputVariable::setPreviousValue(scalar previousOutputValue) { + this->_previousValue = previousOutputValue; + } + + scalar OutputVariable::getPreviousValue() const { + return this->_previousValue; + } + + void OutputVariable::setDefaultValue(scalar defaultValue) { + this->_defaultValue = defaultValue; + } + + scalar OutputVariable::getDefaultValue() const { + return this->_defaultValue; + } + + void OutputVariable::setLockPreviousValue(bool lockPreviousValue) { + this->_lockPreviousValue = lockPreviousValue; + } + + bool OutputVariable::isLockPreviousValue() const { + return this->_lockPreviousValue; + } + + Variable::Type OutputVariable::type() const { + return Variable::Output; + } + + Complexity OutputVariable::complexity(const Activated& term) const { + Aggregated aggregated; + if (_fuzzyOutput->getAggregation()) { + aggregated.setAggregation(_fuzzyOutput->getAggregation()->clone()); + } + aggregated.addTerm(term); + if (_defuzzifier.get()) { + return _defuzzifier->complexity(&aggregated); + } + return aggregated.complexityOfMembership(); + } + + Complexity OutputVariable::complexityOfDefuzzification() const { + Aggregated term; + for (std::size_t i = 0; i < _terms.size(); ++i) { + term.addTerm(_terms.at(i), fl::nan, fl::null); + } + if (_defuzzifier.get()) { + return _defuzzifier->complexity(&term); + } + return term.complexityOfMembership(); + } + + Complexity OutputVariable::currentComplexity() const { + if (_defuzzifier.get()) + return _defuzzifier->complexity(_fuzzyOutput.get()); + return _fuzzyOutput->complexity(); + } + + void OutputVariable::defuzzify() { + if (not _enabled) return; + + if (Op::isFinite(_value)) { + _previousValue = _value; + } + + std::string exception; + scalar result = fl::nan; + bool isValid = not _fuzzyOutput->isEmpty(); + if (isValid) { + /* Checks whether the variable can be defuzzified without exceptions. + * If it cannot be defuzzified, be that due to a missing defuzzifier + * or aggregation operator, the expected behaviour is to leave the + * variable in a state that reflects an invalid defuzzification, + * that is, apply logic of default values and previous values.*/ + isValid = false; + if (_defuzzifier.get()) { + try { + result = _defuzzifier->defuzzify(_fuzzyOutput.get(), _minimum, _maximum); + isValid = true; + } catch (std::exception& ex) { + exception = ex.what(); + } + } else { + exception = "[defuzzifier error] " + "defuzzifier needed to defuzzify output variable <" + getName() + ">"; + } + } + + if (not isValid) { + //if a previous defuzzification was successfully performed and + //and the output value is supposed not to change when the output is empty + if (_lockPreviousValue and not Op::isNaN(_previousValue)) { + result = _previousValue; + } else { + result = _defaultValue; + } + } + + setValue(result); + + if (not exception.empty()) { + throw Exception(exception, FL_AT); + } + } + + std::string OutputVariable::fuzzyOutputValue() const { + std::ostringstream ss; + if (not _terms.empty()) { + Term* first = _terms.front(); + ss << Op::str(fuzzyOutput()->activationDegree(first)) + << "/" << first->getName(); + } + for (std::size_t i = 1; i < _terms.size(); ++i) { + scalar degree = fuzzyOutput()->activationDegree(_terms.at(i)); + if (Op::isNaN(degree) or Op::isGE(degree, 0.0)) + ss << " + " << Op::str(degree); + else + ss << " - " << Op::str(std::abs(degree)); + ss << "/" << terms().at(i)->getName(); + } + return ss.str(); + } + + void OutputVariable::clear() { + fuzzyOutput()->clear(); + setValue(fl::nan); + setPreviousValue(fl::nan); + } + + std::string OutputVariable::toString() const { + return FllExporter().toString(this); + } + + OutputVariable* OutputVariable::clone() const { + return new OutputVariable(*this); + } + +} diff --git a/fuzzylite/src/variable/Variable.cpp b/fuzzylite/src/variable/Variable.cpp new file mode 100644 index 0000000..516b466 --- /dev/null +++ b/fuzzylite/src/variable/Variable.cpp @@ -0,0 +1,295 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/variable/Variable.h" + +#include "fl/imex/FllExporter.h" +#include "fl/norm/Norm.h" +#include "fl/term/Constant.h" +#include "fl/term/Linear.h" + +#include <queue> + +namespace fl { + + Variable::Variable(const std::string& name, scalar minimum, scalar maximum) + : _name(name), _description(""), + _value(fl::nan), _minimum(minimum), _maximum(maximum), + _enabled(true), _lockValueInRange(false) { } + + Variable::Variable(const Variable& other) { + copyFrom(other); + } + + Variable& Variable::operator=(const Variable& other) { + if (this != &other) { + for (std::size_t i = 0; i < _terms.size(); ++i) { + delete _terms.at(i); + } + _terms.clear(); + copyFrom(other); + } + return *this; + } + + void Variable::copyFrom(const Variable& other) { + _name = other._name; + _description = other._description; + _value = other._value; + _minimum = other._minimum; + _maximum = other._maximum; + _enabled = other._enabled; + _lockValueInRange = other._lockValueInRange; + for (std::size_t i = 0; i < other._terms.size(); ++i) { + _terms.push_back(other._terms.at(i)->clone()); + } + } + + Variable::~Variable() { + for (std::size_t i = 0; i < _terms.size(); ++i) { + delete _terms.at(i); + } + } + + void Variable::setName(const std::string& name) { + this->_name = name; + } + + std::string Variable::getName() const { + return this->_name; + } + + void Variable::setDescription(const std::string& description) { + this->_description = description; + } + + std::string Variable::getDescription() const { + return this->_description; + } + + void Variable::setValue(scalar value) { + this->_value = _lockValueInRange + ? Op::bound(value, _minimum, _maximum) + : value; + } + + scalar Variable::getValue() const { + return this->_value; + } + + void Variable::setRange(scalar minimum, scalar maximum) { + setMinimum(minimum); + setMaximum(maximum); + } + + scalar Variable::range() const { + return getMaximum() - getMinimum(); + } + + void Variable::setMinimum(scalar minimum) { + this->_minimum = minimum; + } + + scalar Variable::getMinimum() const { + return this->_minimum; + } + + void Variable::setMaximum(scalar maximum) { + this->_maximum = maximum; + } + + scalar Variable::getMaximum() const { + return this->_maximum; + } + + void Variable::setEnabled(bool enabled) { + this->_enabled = enabled; + } + + bool Variable::isEnabled() const { + return this->_enabled; + } + + void Variable::setLockValueInRange(bool lockValueInRange) { + this->_lockValueInRange = lockValueInRange; + } + + bool Variable::isLockValueInRange() const { + return this->_lockValueInRange; + } + + Variable::Type Variable::type() const { + return None; + } + + Complexity Variable::complexity() const { + Complexity result; + if (isEnabled()) { + for (std::size_t i = 0; i < _terms.size(); ++i) { + result += _terms.at(i)->complexity(); + } + } + return result; + } + + std::string Variable::fuzzify(scalar x) const { + std::ostringstream ss; + for (std::size_t i = 0; i < terms().size(); ++i) { + Term* term = _terms.at(i); + scalar fx = fl::nan; + try { + fx = term->membership(x); + } catch (...) { + //ignore + } + if (i == 0) { + ss << Op::str(fx); + } else { + if (Op::isNaN(fx) or Op::isGE(fx, 0.0)) + ss << " + " << Op::str(fx); + else + ss << " - " << Op::str(std::abs(fx)); + } + ss << "/" << term->getName(); + } + return ss.str(); + } + + Term* Variable::highestMembership(scalar x, scalar* yhighest) const { + Term* result = fl::null; + scalar ymax = 0.0; + for (std::size_t i = 0; i < _terms.size(); ++i) { + scalar y = fl::nan; + Term* term = _terms.at(i); + try { + y = term->membership(x); + } catch (...) { + //ignore + } + if (Op::isGt(y, ymax)) { + ymax = y; + result = term; + } + } + if (yhighest) *yhighest = ymax; + return result; + } + + std::string Variable::toString() const { + return FllExporter().toString(this); + } + + /** + * Operations for datatype _terms + */ + + typedef std::pair<Term*, scalar> TermCentroid; + + struct Ascending { + + bool operator()(const TermCentroid& a, const TermCentroid& b) const { + return a.second > b.second; + } + }; + + void Variable::sort() { + std::priority_queue <TermCentroid, std::vector<TermCentroid>, Ascending> termCentroids; + Centroid defuzzifier; + FL_DBG("Sorting..."); + for (std::size_t i = 0; i < _terms.size(); ++i) { + Term* term = _terms.at(i); + scalar centroid = fl::inf; + try { + if (dynamic_cast<const Constant*> (term) or dynamic_cast<const Linear*> (term)) { + centroid = term->membership(0); + } else { + centroid = defuzzifier.defuzzify(term, getMinimum(), getMaximum()); + } + } catch (...) { //ignore error possibly due to Function not loaded + centroid = fl::inf; + } + termCentroids.push(TermCentroid(term, centroid)); + FL_DBG(term->toString() << " -> " << centroid) + } + + std::vector<Term*> sortedTerms; + while (termCentroids.size() > 0) { + sortedTerms.push_back(termCentroids.top().first); + FL_DBG(termCentroids.top().first->toString() << " -> " << termCentroids.top().second); + termCentroids.pop(); + } + setTerms(sortedTerms); + } + + void Variable::addTerm(Term* term) { + _terms.push_back(term); + } + + void Variable::insertTerm(Term* term, std::size_t index) { + _terms.insert(_terms.begin() + index, term); + } + + Term* Variable::getTerm(std::size_t index) const { + return _terms.at(index); + } + + Term* Variable::getTerm(const std::string& name) const { + for (std::size_t i = 0; i < terms().size(); ++i) { + if (_terms.at(i)->getName() == name) { + return terms().at(i); + } + } + throw Exception("[variable error] term <" + name + "> " + "not found in variable <" + getName() + ">", FL_AT); + } + + bool Variable::hasTerm(const std::string& name) const { + for (std::size_t i = 0; i < _terms.size(); ++i) { + if (_terms.at(i)->getName() == name) { + return true; + } + } + return false; + } + + Term* Variable::removeTerm(std::size_t index) { + Term* result = _terms.at(index); + _terms.erase(_terms.begin() + index); + return result; + } + + std::size_t Variable::numberOfTerms() const { + return _terms.size(); + } + + const std::vector<Term*>& Variable::terms() const { + return this->_terms; + } + + void Variable::setTerms(const std::vector<Term*>& terms) { + this->_terms = terms; + } + + std::vector<Term*>& Variable::terms() { + return this->_terms; + } + + Variable* Variable::clone() const { + return new Variable(*this); + } + +} + diff --git a/fuzzylite/test/BenchmarkTest.cpp b/fuzzylite/test/BenchmarkTest.cpp new file mode 100644 index 0000000..f868ec2 --- /dev/null +++ b/fuzzylite/test/BenchmarkTest.cpp @@ -0,0 +1,131 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "fl/Benchmark.h" + +#include "test/catch.hpp" +#include "fl/Headers.h" + +#include <vector> +#include <fstream> + +namespace fl { + + TEST_CASE("Benchmarks run from Console ", "[benchmark][console]") { + // const char* args[] = {"dummy-command", "benchmarks", "../../examples/", "1"}; + // Console::main(4, args); + } + + TEST_CASE("Benchmarks from FLD files", "[benchmark][fld]") { + std::string path = "../../examples/"; + typedef std::pair<std::string, int > Example; + std::vector<Example> examples; + examples.push_back(Example("mamdani/AllTerms", int(1e4))); + examples.push_back(Example("mamdani/SimpleDimmer", int(1e5))); + examples.push_back(Example("mamdani/matlab/mam21", 128)); + examples.push_back(Example("mamdani/matlab/mam22", 128)); + examples.push_back(Example("mamdani/matlab/shower", 256)); + examples.push_back(Example("mamdani/matlab/tank", 256)); + examples.push_back(Example("mamdani/matlab/tank2", 512)); + examples.push_back(Example("mamdani/matlab/tipper", 256)); + examples.push_back(Example("mamdani/matlab/tipper1", int(1e5))); + examples.push_back(Example("mamdani/octave/investment_portfolio", 256)); + examples.push_back(Example("mamdani/octave/mamdani_tip_calculator", 256)); + examples.push_back(Example("takagi-sugeno/approximation", int(1e6))); + examples.push_back(Example("takagi-sugeno/SimpleDimmer", int(2e6))); + examples.push_back(Example("takagi-sugeno/matlab/fpeaks", 512)); + examples.push_back(Example("takagi-sugeno/matlab/invkine1", 256)); + examples.push_back(Example("takagi-sugeno/matlab/invkine2", 256)); + examples.push_back(Example("takagi-sugeno/matlab/juggler", 512)); + examples.push_back(Example("takagi-sugeno/matlab/membrn1", 1024)); + examples.push_back(Example("takagi-sugeno/matlab/membrn2", 512)); + examples.push_back(Example("takagi-sugeno/matlab/slbb", 20)); + examples.push_back(Example("takagi-sugeno/matlab/slcp", 20)); + examples.push_back(Example("takagi-sugeno/matlab/slcp1", 15)); + examples.push_back(Example("takagi-sugeno/matlab/slcpp1", 9)); + examples.push_back(Example("takagi-sugeno/matlab/sltbu_fl", 128)); + examples.push_back(Example("takagi-sugeno/matlab/sugeno1", int(2e6))); + examples.push_back(Example("takagi-sugeno/matlab/tanksg", 1024)); + examples.push_back(Example("takagi-sugeno/matlab/tippersg", 1024)); + examples.push_back(Example("takagi-sugeno/octave/cubic_approximator", int(2e6))); + examples.push_back(Example("takagi-sugeno/octave/heart_disease_risk", 1024)); + examples.push_back(Example("takagi-sugeno/octave/linear_tip_calculator", 1024)); + examples.push_back(Example("takagi-sugeno/octave/sugeno_tip_calculator", 512)); + examples.push_back(Example("tsukamoto/tsukamoto", int(1e6))); + + std::ostringstream writer; + std::vector<int> errors = std::vector<int>(examples.size(), 0); + for (std::size_t i = 0; i < examples.size(); ++i) { + Example example = examples.at(i); + FL_LOG("Benchmark " << (i + 1) << "/" << examples.size() << ": " + << example.first << ".fll (" << example.second << " values)"); + + FL_unique_ptr<Engine> engine(FllImporter().fromFile(path + example.first + ".fll")); + +#ifdef FL_USE_FLOAT + scalar tolerance = 1e-3; +#else + scalar tolerance = fuzzylite::macheps(); +#endif + Benchmark benchmark(example.first, engine.get(), tolerance); + + std::ifstream reader(std::string(path + example.first + ".fld").c_str()); + if (not reader.is_open()) { + throw Exception("File not found: " + path + example.first + ".fld"); + } + benchmark.prepare(reader, 1024); + benchmark.run(1); + CHECK(benchmark.canComputeErrors() == true); + errors.at(i) = benchmark.accuracyErrors(); + + if (i == 0) { + writer << "\n" << benchmark.format(benchmark.results(), + Benchmark::Horizontal, Benchmark::HeaderAndBody) << "\n"; + } else { + writer << benchmark.format(benchmark.results(), + Benchmark::Horizontal, Benchmark::Body) << "\n"; + } + } + FL_LOG(writer.str()); + for (std::size_t i = 0; i < errors.size(); ++i) { + FL_LOG("Checking for errors in: " << examples.at(i).first); + CHECK(errors.at(i) == 0); + } + } + + TEST_CASE("Time conversions", "[benchmark][time]") { + CHECK(Op::isEq(1.0, Benchmark::convert(3600, Benchmark::Seconds, Benchmark::Hours))); + FL_LOG(Benchmark::convert(3600, Benchmark::Seconds, Benchmark::Hours)); + CHECK(Op::isEq(3600, Benchmark::convert(1, Benchmark::Hours, Benchmark::Seconds))); + FL_LOG(Benchmark::convert(1, Benchmark::Hours, Benchmark::Seconds)); + + CHECK(Op::isEq(1000.0, Benchmark::convert(1.0, Benchmark::Seconds, Benchmark::MilliSeconds))); + FL_LOG(Benchmark::convert(1.0, Benchmark::Seconds, Benchmark::MilliSeconds)); + CHECK(Op::isEq(1.0, Benchmark::convert(1000.0, Benchmark::MilliSeconds, Benchmark::Seconds))); + FL_LOG(Benchmark::convert(1000.0, Benchmark::MilliSeconds, Benchmark::Seconds)); + + CHECK(Op::isEq(35e9, Benchmark::convert(35, Benchmark::Seconds, Benchmark::NanoSeconds))); + CHECK(Op::isEq(35, Benchmark::convert(35e9, Benchmark::NanoSeconds, Benchmark::Seconds))); + } + + TEST_CASE("Benchmark headers", "[benchmark][header]") { + FL_LOG(Op::join(Benchmark().header(10, true), "\t")); + CHECK(Benchmark().header(10).size() == 30); + + FL_LOG(Op::join(Benchmark().header(10, false), "\t")); + CHECK(Benchmark().header(10, false).size() == 30 - 8); + } +} diff --git a/fuzzylite/test/Catch.License b/fuzzylite/test/Catch.License new file mode 100644 index 0000000..36b7cd9 --- /dev/null +++ b/fuzzylite/test/Catch.License @@ -0,0 +1,23 @@ +Boost Software License - Version 1.0 - August 17th, 2003 + +Permission is hereby granted, free of charge, to any person or organization +obtaining a copy of the software and accompanying documentation covered by +this license (the "Software") to use, reproduce, display, distribute, +execute, and transmit the Software, and to prepare derivative works of the +Software, and to permit third-parties to whom the Software is furnished to +do so, all subject to the following: + +The copyright notices in the Software and this entire statement, including +the above license grant, this restriction and the following disclaimer, +must be included in all copies of the Software, in whole or in part, and +all derivative works of the Software, unless such copies or derivative +works are solely in the form of machine-executable object code generated by +a source language processor. + +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, TITLE AND NON-INFRINGEMENT. IN NO EVENT +SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/fuzzylite/test/MainTest.cpp b/fuzzylite/test/MainTest.cpp new file mode 100644 index 0000000..c18740a --- /dev/null +++ b/fuzzylite/test/MainTest.cpp @@ -0,0 +1,34 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#define CATCH_CONFIG_RUNNER + +#include "test/catch.hpp" + +#include "fl/Headers.h" + +int main(int argc, char** argv) { + + // global setup... + fl::fuzzylite::setDebugging(false); + fl::fuzzylite::setLogging(true); + + int result = Catch::Session().run(argc, argv); + + // global clean-up... + + return result; +} diff --git a/fuzzylite/test/QuickTest.cpp b/fuzzylite/test/QuickTest.cpp new file mode 100644 index 0000000..9108fde --- /dev/null +++ b/fuzzylite/test/QuickTest.cpp @@ -0,0 +1,128 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "test/catch.hpp" +#include "fl/Headers.h" + +namespace fl { + + TEST_CASE("Increment ", "[op][increment]") { + std::vector<int> sampleValues, minSampleValues, maxSampleValues; + for (std::size_t i = 0; i < 2; ++i) { + sampleValues.push_back(0); + minSampleValues.push_back(0); + if (i == 0) + maxSampleValues.push_back(10); + else maxSampleValues.push_back(0); + } + int times = 0; + do { + times++; + FL_LOG(times << " " << Op::join(sampleValues, ",")); + } while (Op::increment(sampleValues, minSampleValues, maxSampleValues)); + + CHECK(times == 10 + 1); + } + + TEST_CASE("Op::split splits multiline", "[op][split]") { + std::string multiline = "1\n2\n3\n4"; + std::vector<std::string> split = Op::split(multiline, "\n"); + CHECK(split.size() == 4); + } + + TEST_CASE("Op::str produces correct numbers", "[op][str]") { + fuzzylite::setLogging(true); + fuzzylite::setDecimals(3); + + FL_LOG(Op::str(1.0)); + FL_LOG(Op::str((long) 5000)); + FL_LOG(Op::str((int) 6000)); + FL_LOG(Op::str(std::size_t(6000))); + FL_LOG(Op::str(scalar(0.333333))); + FL_LOG(Op::str(float(0.333333))); + FL_LOG(Op::str(double(0.333333))); + FL_LOG(Op::str(double(0.333333), 9)); + + CHECK(Op::str(0) == "0"); + CHECK(Op::str(1) == "1"); + CHECK(Op::str(1.0) == "1.000"); + CHECK(Op::str((long) 5000) == "5000"); + CHECK(Op::str((int) 6000) == "6000"); + CHECK(Op::str(std::size_t(6000)) == "6000"); + CHECK(Op::str(scalar(0.333333)) == "0.333"); + CHECK(Op::str(float(0.333333)) == "0.333"); + CHECK(Op::str(double(0.333333)) == "0.333"); + CHECK(Op::str(double(0.0000333), 9) == "0.000033300"); + + CHECK(Op::str(fuzzylite::macheps()) == "0.000"); + CHECK(Op::str(fuzzylite::macheps(), 6) == "0.000001"); + CHECK(Op::str(1e-7) == "0.000"); + CHECK(Op::str(1e-7, 6) == "0.000000"); + CHECK(Op::str(1e-7, 7) == "0.0000001"); + + FL_LOG("scientific"); + fuzzylite::setScalarFormat(std::ios_base::scientific); + FL_LOG(Op::str(1.0)); + FL_LOG(Op::str((long) 5000)); + FL_LOG(Op::str((int) 6000)); + FL_LOG(Op::str(std::size_t(6000))); + FL_LOG(Op::str(scalar(0.333333))); + FL_LOG(Op::str(float(0.333333))); + FL_LOG(Op::str(double(0.333333))); + FL_LOG(Op::str(double(0.0000333), 9)); + + CHECK(Op::str(0) == "0"); + CHECK(Op::str(1.0) == "1.000e+00"); + CHECK(Op::str((long) 5000) == "5000"); + CHECK(Op::str((int) 6000) == "6000"); + CHECK(Op::str(std::size_t(6000)) == "6000"); + CHECK(Op::str(scalar(0.333333)) == "3.333e-01"); + CHECK(Op::str(float(0.333333)) == "3.333e-01"); + CHECK(Op::str(double(0.333333)) == "3.333e-01"); + CHECK(Op::str(double(0.0000333), 9) == "3.330000000e-05"); + + CHECK(Op::isEq(fuzzylite::macheps(), 0.0) == false); + CHECK(Op::isEq(fuzzylite::macheps(), 0.0, std::pow(10.0, -6)) == false); + CHECK(Op::str(fuzzylite::macheps()) == "0.000e+00"); + CHECK(Op::str(fuzzylite::macheps(), -1) == "1.000000e-06"); + CHECK(Op::str(fuzzylite::macheps(), -1, std::ios_base::fmtflags(0x0)) == "1e-06"); + CHECK(Op::str(1e-7, 6) == "0.000000e+00"); + CHECK(Op::str(1e-7, 7) == "1.0000000e-07"); + CHECK(Op::str(1e-7, 7, std::ios_base::fmtflags(0x0)) == "1e-07"); + + + fuzzylite::setScalarFormat(std::ios_base::fixed); + + CHECK(Op::str(0.000001, 6, std::ios_base::fmtflags(0x0)) == "1e-06"); + CHECK(Op::str(1e-6, 6, std::ios_base::fmtflags(0x0)) == "1e-06"); + CHECK(Op::str(1e6, 3, std::ios_base::fmtflags(0x0)) == "1e+06"); + CHECK(Op::str(1000000, 3, std::ios_base::fmtflags(0x0)) == "1000000"); + CHECK(Op::str(1000000.0, 3, std::ios_base::fmtflags(0x0)) == "1e+06"); + } + + TEST_CASE("macro expansion does not evaluate parameter before expansion", "[op]") { + std::ostringstream os; +#define FL_MACRO1(x) os << x * 5; + FL_MACRO1(4 + 10); + CHECK(os.str() == "54"); + +#define xstr(s) str(s) +#define str(s) #s + CHECK(xstr(4 + 10) == "4 + 10"); + } + + +} diff --git a/fuzzylite/test/activation/ThresholdTest.cpp b/fuzzylite/test/activation/ThresholdTest.cpp new file mode 100644 index 0000000..9f6a9e6 --- /dev/null +++ b/fuzzylite/test/activation/ThresholdTest.cpp @@ -0,0 +1,65 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "test/catch.hpp" +#include "fl/Headers.h" + +namespace fl { + + /** + * Tests: term/Function + * + * @author Juan Rada-Vilela, Ph.D. + * + */ + + TEST_CASE("Treshold can be clone ", "[activation][threshold]") { + Threshold* t = new Threshold("<", 1.0); + REQUIRE(t->getComparison() == Threshold::LessThan); + REQUIRE(Op::isEq(t->getValue(), 1.0)); + FL_DBG(FllExporter().toString(t)); + + Threshold* clone = t->clone(); + REQUIRE(clone->getComparison() == Threshold::LessThan); + REQUIRE(Op::isEq(clone->getValue(), 1.0)); + FL_DBG(FllExporter().toString(clone)); + } + + TEST_CASE("Treshold can be copy-constructed", "[activation][threshold]") { + Threshold* t = new Threshold(">=", 1.0); + REQUIRE(t->getComparison() == Threshold::GreaterThanOrEqualTo); + REQUIRE(Op::isEq(t->getValue(), 1.0)); + FL_DBG(FllExporter().toString(t)); + + Threshold clone(*t); + REQUIRE(clone.getComparison() == Threshold::GreaterThanOrEqualTo); + REQUIRE(Op::isEq(clone.getValue(), 1.0)); + FL_DBG(FllExporter().toString(&clone)); + } + + TEST_CASE("Treshold can be assigned", "[activation][threshold]") { + Threshold* t = new Threshold(">=", 1.0); + REQUIRE(t->getComparison() == Threshold::GreaterThanOrEqualTo); + REQUIRE(Op::isEq(t->getValue(), 1.0)); + FL_DBG(FllExporter().toString(t)); + + Threshold clone = *t; + REQUIRE(clone.getComparison() == Threshold::GreaterThanOrEqualTo); + REQUIRE(Op::isEq(clone.getValue(), 1.0)); + FL_DBG(FllExporter().toString(&clone)); + } + +} diff --git a/fuzzylite/test/catch.hpp b/fuzzylite/test/catch.hpp new file mode 100644 index 0000000..6f9334b --- /dev/null +++ b/fuzzylite/test/catch.hpp @@ -0,0 +1,11282 @@ +/* + * Catch v1.8.1 + * Generated: 2017-03-01 16:04:19.016511 + * ---------------------------------------------------------- + * This file has been merged from multiple headers. Please don't edit it directly + * Copyright (c) 2012 Two Blue Cubes Ltd. All rights reserved. + * + * Distributed under the Boost Software License, Version 1.0. (See accompanying + * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + */ +#ifndef TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED +#define TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED + +#define TWOBLUECUBES_CATCH_HPP_INCLUDED + +#ifdef __clang__ +# pragma clang system_header +#elif defined __GNUC__ +# pragma GCC system_header +#endif + +// #included from: internal/catch_suppress_warnings.h + +#ifdef __clang__ +# ifdef __ICC // icpc defines the __clang__ macro +# pragma warning(push) +# pragma warning(disable: 161 1682) +# else // __ICC +# pragma clang diagnostic ignored "-Wglobal-constructors" +# pragma clang diagnostic ignored "-Wvariadic-macros" +# pragma clang diagnostic ignored "-Wc99-extensions" +# pragma clang diagnostic ignored "-Wunused-variable" +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wpadded" +# pragma clang diagnostic ignored "-Wc++98-compat" +# pragma clang diagnostic ignored "-Wc++98-compat-pedantic" +# pragma clang diagnostic ignored "-Wswitch-enum" +# pragma clang diagnostic ignored "-Wcovered-switch-default" +# endif +#elif defined __GNUC__ +# pragma GCC diagnostic ignored "-Wvariadic-macros" +# pragma GCC diagnostic ignored "-Wunused-variable" + + // For newer version we can use __Pragma to disable the warnings locally +# if __GNUC__ == 4 && __GNUC_MINOR__ >= 4 && __GNUC_MINOR__ <= 7 +# pragma GCC diagnostic ignored "-Wparentheses" +# endif + +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wpadded" +#endif +#if defined(CATCH_CONFIG_MAIN) || defined(CATCH_CONFIG_RUNNER) +# define CATCH_IMPL +#endif + +#ifdef CATCH_IMPL +# ifndef CLARA_CONFIG_MAIN +# define CLARA_CONFIG_MAIN_NOT_DEFINED +# define CLARA_CONFIG_MAIN +# endif +#endif + +// #included from: internal/catch_notimplemented_exception.h +#define TWOBLUECUBES_CATCH_NOTIMPLEMENTED_EXCEPTION_H_INCLUDED + +// #included from: catch_common.h +#define TWOBLUECUBES_CATCH_COMMON_H_INCLUDED + +// #included from: catch_compiler_capabilities.h +#define TWOBLUECUBES_CATCH_COMPILER_CAPABILITIES_HPP_INCLUDED + +// Detect a number of compiler features - mostly C++11/14 conformance - by compiler +// The following features are defined: +// +// CATCH_CONFIG_CPP11_NULLPTR : is nullptr supported? +// CATCH_CONFIG_CPP11_NOEXCEPT : is noexcept supported? +// CATCH_CONFIG_CPP11_GENERATED_METHODS : The delete and default keywords for compiler generated methods +// CATCH_CONFIG_CPP11_IS_ENUM : std::is_enum is supported? +// CATCH_CONFIG_CPP11_TUPLE : std::tuple is supported +// CATCH_CONFIG_CPP11_LONG_LONG : is long long supported? +// CATCH_CONFIG_CPP11_OVERRIDE : is override supported? +// CATCH_CONFIG_CPP11_UNIQUE_PTR : is unique_ptr supported (otherwise use auto_ptr) +// CATCH_CONFIG_CPP11_SHUFFLE : is std::shuffle supported? +// CATCH_CONFIG_CPP11_TYPE_TRAITS : are type_traits and enable_if supported? + +// CATCH_CONFIG_CPP11_OR_GREATER : Is C++11 supported? + +// CATCH_CONFIG_VARIADIC_MACROS : are variadic macros supported? +// CATCH_CONFIG_COUNTER : is the __COUNTER__ macro supported? +// CATCH_CONFIG_WINDOWS_SEH : is Windows SEH supported? +// CATCH_CONFIG_POSIX_SIGNALS : are POSIX signals supported? +// **************** +// Note to maintainers: if new toggles are added please document them +// in configuration.md, too +// **************** + +// In general each macro has a _NO_<feature name> form +// (e.g. CATCH_CONFIG_CPP11_NO_NULLPTR) which disables the feature. +// Many features, at point of detection, define an _INTERNAL_ macro, so they +// can be combined, en-mass, with the _NO_ forms later. + +// All the C++11 features can be disabled with CATCH_CONFIG_NO_CPP11 + +#ifdef __cplusplus + +# if __cplusplus >= 201103L +# define CATCH_CPP11_OR_GREATER +# endif + +# if __cplusplus >= 201402L +# define CATCH_CPP14_OR_GREATER +# endif + +#endif + +#ifdef __clang__ + +# if __has_feature(cxx_nullptr) +# define CATCH_INTERNAL_CONFIG_CPP11_NULLPTR +# endif + +# if __has_feature(cxx_noexcept) +# define CATCH_INTERNAL_CONFIG_CPP11_NOEXCEPT +# endif + +# if defined(CATCH_CPP11_OR_GREATER) +# define CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS \ + _Pragma( "clang diagnostic push" ) \ + _Pragma( "clang diagnostic ignored \"-Wparentheses\"" ) +# define CATCH_INTERNAL_UNSUPPRESS_PARENTHESES_WARNINGS \ + _Pragma( "clang diagnostic pop" ) +# endif + +#endif // __clang__ + +//////////////////////////////////////////////////////////////////////////////// +// Cygwin +#ifdef __CYGWIN__ + +# if !defined(CATCH_CONFIG_POSIX_SIGNALS) +# define CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS +# endif + +// Required for some versions of Cygwin to declare gettimeofday +// see: http://stackoverflow.com/questions/36901803/gettimeofday-not-declared-in-this-scope-cygwin +# define _BSD_SOURCE + +#endif // __CYGWIN__ + +//////////////////////////////////////////////////////////////////////////////// +// Borland +#ifdef __BORLANDC__ + +#endif // __BORLANDC__ + +//////////////////////////////////////////////////////////////////////////////// +// EDG +#ifdef __EDG_VERSION__ + +#endif // __EDG_VERSION__ + +//////////////////////////////////////////////////////////////////////////////// +// Digital Mars +#ifdef __DMC__ + +#endif // __DMC__ + +//////////////////////////////////////////////////////////////////////////////// +// GCC +#ifdef __GNUC__ + +# if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) +# define CATCH_GCC_HAS_NEW_PRAGMA +# endif + +# if __GNUC__ == 4 && __GNUC_MINOR__ >= 6 && defined(__GXX_EXPERIMENTAL_CXX0X__) +# define CATCH_INTERNAL_CONFIG_CPP11_NULLPTR +# endif + +# if !defined(CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS) && defined(CATCH_GCC_HAS_NEW_PRAGMA) +# define CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS \ + _Pragma( "GCC diagnostic push" ) \ + _Pragma( "GCC diagnostic ignored \"-Wparentheses\"" ) +# define CATCH_INTERNAL_UNSUPPRESS_PARENTHESES_WARNINGS \ + _Pragma( "GCC diagnostic pop" ) +# endif + +// - otherwise more recent versions define __cplusplus >= 201103L +// and will get picked up below + +#endif // __GNUC__ + +//////////////////////////////////////////////////////////////////////////////// +// Visual C++ +#ifdef _MSC_VER + +#define CATCH_INTERNAL_CONFIG_WINDOWS_SEH + +#if (_MSC_VER >= 1600) +# define CATCH_INTERNAL_CONFIG_CPP11_NULLPTR +# define CATCH_INTERNAL_CONFIG_CPP11_UNIQUE_PTR +#endif + +#if (_MSC_VER >= 1900 ) // (VC++ 13 (VS2015)) +#define CATCH_INTERNAL_CONFIG_CPP11_NOEXCEPT +#define CATCH_INTERNAL_CONFIG_CPP11_GENERATED_METHODS +#define CATCH_INTERNAL_CONFIG_CPP11_SHUFFLE +#define CATCH_INTERNAL_CONFIG_CPP11_TYPE_TRAITS +#endif + +#endif // _MSC_VER + +//////////////////////////////////////////////////////////////////////////////// + +// Use variadic macros if the compiler supports them +#if ( defined _MSC_VER && _MSC_VER > 1400 && !defined __EDGE__) || \ + ( defined __WAVE__ && __WAVE_HAS_VARIADICS ) || \ + ( defined __GNUC__ && __GNUC__ >= 3 ) || \ + ( !defined __cplusplus && __STDC_VERSION__ >= 199901L || __cplusplus >= 201103L ) + +#define CATCH_INTERNAL_CONFIG_VARIADIC_MACROS + +#endif + +// Use __COUNTER__ if the compiler supports it +#if ( defined _MSC_VER && _MSC_VER >= 1300 ) || \ + ( defined __GNUC__ && __GNUC__ >= 4 && __GNUC_MINOR__ >= 3 ) || \ + ( defined __clang__ && __clang_major__ >= 3 ) + +#define CATCH_INTERNAL_CONFIG_COUNTER + +#endif + +//////////////////////////////////////////////////////////////////////////////// +// C++ language feature support + +// catch all support for C++11 +#if defined(CATCH_CPP11_OR_GREATER) + +# if !defined(CATCH_INTERNAL_CONFIG_CPP11_NULLPTR) +# define CATCH_INTERNAL_CONFIG_CPP11_NULLPTR +# endif + +# ifndef CATCH_INTERNAL_CONFIG_CPP11_NOEXCEPT +# define CATCH_INTERNAL_CONFIG_CPP11_NOEXCEPT +# endif + +# ifndef CATCH_INTERNAL_CONFIG_CPP11_GENERATED_METHODS +# define CATCH_INTERNAL_CONFIG_CPP11_GENERATED_METHODS +# endif + +# ifndef CATCH_INTERNAL_CONFIG_CPP11_IS_ENUM +# define CATCH_INTERNAL_CONFIG_CPP11_IS_ENUM +# endif + +# ifndef CATCH_INTERNAL_CONFIG_CPP11_TUPLE +# define CATCH_INTERNAL_CONFIG_CPP11_TUPLE +# endif + +# ifndef CATCH_INTERNAL_CONFIG_VARIADIC_MACROS +# define CATCH_INTERNAL_CONFIG_VARIADIC_MACROS +# endif + +# if !defined(CATCH_INTERNAL_CONFIG_CPP11_LONG_LONG) +# define CATCH_INTERNAL_CONFIG_CPP11_LONG_LONG +# endif + +# if !defined(CATCH_INTERNAL_CONFIG_CPP11_OVERRIDE) +# define CATCH_INTERNAL_CONFIG_CPP11_OVERRIDE +# endif +# if !defined(CATCH_INTERNAL_CONFIG_CPP11_UNIQUE_PTR) +# define CATCH_INTERNAL_CONFIG_CPP11_UNIQUE_PTR +# endif +# if !defined(CATCH_INTERNAL_CONFIG_CPP11_SHUFFLE) +# define CATCH_INTERNAL_CONFIG_CPP11_SHUFFLE +# endif +# if !defined(CATCH_INTERNAL_CONFIG_CPP11_TYPE_TRAITS) +# define CATCH_INTERNAL_CONFIG_CPP11_TYPE_TRAITS +# endif + +#endif // __cplusplus >= 201103L + +// Now set the actual defines based on the above + anything the user has configured +#if defined(CATCH_INTERNAL_CONFIG_CPP11_NULLPTR) && !defined(CATCH_CONFIG_CPP11_NO_NULLPTR) && !defined(CATCH_CONFIG_CPP11_NULLPTR) && !defined(CATCH_CONFIG_NO_CPP11) +# define CATCH_CONFIG_CPP11_NULLPTR +#endif +#if defined(CATCH_INTERNAL_CONFIG_CPP11_NOEXCEPT) && !defined(CATCH_CONFIG_CPP11_NO_NOEXCEPT) && !defined(CATCH_CONFIG_CPP11_NOEXCEPT) && !defined(CATCH_CONFIG_NO_CPP11) +# define CATCH_CONFIG_CPP11_NOEXCEPT +#endif +#if defined(CATCH_INTERNAL_CONFIG_CPP11_GENERATED_METHODS) && !defined(CATCH_CONFIG_CPP11_NO_GENERATED_METHODS) && !defined(CATCH_CONFIG_CPP11_GENERATED_METHODS) && !defined(CATCH_CONFIG_NO_CPP11) +# define CATCH_CONFIG_CPP11_GENERATED_METHODS +#endif +#if defined(CATCH_INTERNAL_CONFIG_CPP11_IS_ENUM) && !defined(CATCH_CONFIG_CPP11_NO_IS_ENUM) && !defined(CATCH_CONFIG_CPP11_IS_ENUM) && !defined(CATCH_CONFIG_NO_CPP11) +# define CATCH_CONFIG_CPP11_IS_ENUM +#endif +#if defined(CATCH_INTERNAL_CONFIG_CPP11_TUPLE) && !defined(CATCH_CONFIG_CPP11_NO_TUPLE) && !defined(CATCH_CONFIG_CPP11_TUPLE) && !defined(CATCH_CONFIG_NO_CPP11) +# define CATCH_CONFIG_CPP11_TUPLE +#endif +#if defined(CATCH_INTERNAL_CONFIG_VARIADIC_MACROS) && !defined(CATCH_CONFIG_NO_VARIADIC_MACROS) && !defined(CATCH_CONFIG_VARIADIC_MACROS) +# define CATCH_CONFIG_VARIADIC_MACROS +#endif +#if defined(CATCH_INTERNAL_CONFIG_CPP11_LONG_LONG) && !defined(CATCH_CONFIG_CPP11_NO_LONG_LONG) && !defined(CATCH_CONFIG_CPP11_LONG_LONG) && !defined(CATCH_CONFIG_NO_CPP11) +# define CATCH_CONFIG_CPP11_LONG_LONG +#endif +#if defined(CATCH_INTERNAL_CONFIG_CPP11_OVERRIDE) && !defined(CATCH_CONFIG_CPP11_NO_OVERRIDE) && !defined(CATCH_CONFIG_CPP11_OVERRIDE) && !defined(CATCH_CONFIG_NO_CPP11) +# define CATCH_CONFIG_CPP11_OVERRIDE +#endif +#if defined(CATCH_INTERNAL_CONFIG_CPP11_UNIQUE_PTR) && !defined(CATCH_CONFIG_CPP11_NO_UNIQUE_PTR) && !defined(CATCH_CONFIG_CPP11_UNIQUE_PTR) && !defined(CATCH_CONFIG_NO_CPP11) +# define CATCH_CONFIG_CPP11_UNIQUE_PTR +#endif +// Use of __COUNTER__ is suppressed if __JETBRAINS_IDE__ is #defined (meaning we're being parsed by a JetBrains IDE for +// analytics) because, at time of writing, __COUNTER__ is not properly handled by it. +// This does not affect compilation +#if defined(CATCH_INTERNAL_CONFIG_COUNTER) && !defined(CATCH_CONFIG_NO_COUNTER) && !defined(CATCH_CONFIG_COUNTER) && !defined(__JETBRAINS_IDE__) +# define CATCH_CONFIG_COUNTER +#endif +#if defined(CATCH_INTERNAL_CONFIG_CPP11_SHUFFLE) && !defined(CATCH_CONFIG_CPP11_NO_SHUFFLE) && !defined(CATCH_CONFIG_CPP11_SHUFFLE) && !defined(CATCH_CONFIG_NO_CPP11) +# define CATCH_CONFIG_CPP11_SHUFFLE +#endif +# if defined(CATCH_INTERNAL_CONFIG_CPP11_TYPE_TRAITS) && !defined(CATCH_CONFIG_CPP11_NO_TYPE_TRAITS) && !defined(CATCH_CONFIG_CPP11_TYPE_TRAITS) && !defined(CATCH_CONFIG_NO_CPP11) +# define CATCH_CONFIG_CPP11_TYPE_TRAITS +# endif +#if defined(CATCH_INTERNAL_CONFIG_WINDOWS_SEH) && !defined(CATCH_CONFIG_NO_WINDOWS_SEH) && !defined(CATCH_CONFIG_WINDOWS_SEH) +# define CATCH_CONFIG_WINDOWS_SEH +#endif +// This is set by default, because we assume that unix compilers are posix-signal-compatible by default. +#if !defined(CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS) && !defined(CATCH_CONFIG_NO_POSIX_SIGNALS) && !defined(CATCH_CONFIG_POSIX_SIGNALS) +# define CATCH_CONFIG_POSIX_SIGNALS +#endif + +#if !defined(CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS) +# define CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS +# define CATCH_INTERNAL_UNSUPPRESS_PARENTHESES_WARNINGS +#endif + +// noexcept support: +#if defined(CATCH_CONFIG_CPP11_NOEXCEPT) && !defined(CATCH_NOEXCEPT) +# define CATCH_NOEXCEPT noexcept +# define CATCH_NOEXCEPT_IS(x) noexcept(x) +#else +# define CATCH_NOEXCEPT throw() +# define CATCH_NOEXCEPT_IS(x) +#endif + +// nullptr support +#ifdef CATCH_CONFIG_CPP11_NULLPTR +# define CATCH_NULL nullptr +#else +# define CATCH_NULL NULL +#endif + +// override support +#ifdef CATCH_CONFIG_CPP11_OVERRIDE +# define CATCH_OVERRIDE override +#else +# define CATCH_OVERRIDE +#endif + +// unique_ptr support +#ifdef CATCH_CONFIG_CPP11_UNIQUE_PTR +# define CATCH_AUTO_PTR( T ) std::unique_ptr<T> +#else +# define CATCH_AUTO_PTR( T ) std::auto_ptr<T> +#endif + +#define INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line ) name##line +#define INTERNAL_CATCH_UNIQUE_NAME_LINE( name, line ) INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line ) +#ifdef CATCH_CONFIG_COUNTER +# define INTERNAL_CATCH_UNIQUE_NAME( name ) INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __COUNTER__ ) +#else +# define INTERNAL_CATCH_UNIQUE_NAME( name ) INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __LINE__ ) +#endif + +#define INTERNAL_CATCH_STRINGIFY2( expr ) #expr +#define INTERNAL_CATCH_STRINGIFY( expr ) INTERNAL_CATCH_STRINGIFY2( expr ) + +#include <sstream> +#include <algorithm> + +namespace Catch { + + struct IConfig; + + struct CaseSensitive { enum Choice { + Yes, + No + }; }; + + class NonCopyable { +#ifdef CATCH_CONFIG_CPP11_GENERATED_METHODS + NonCopyable( NonCopyable const& ) = delete; + NonCopyable( NonCopyable && ) = delete; + NonCopyable& operator = ( NonCopyable const& ) = delete; + NonCopyable& operator = ( NonCopyable && ) = delete; +#else + NonCopyable( NonCopyable const& info ); + NonCopyable& operator = ( NonCopyable const& ); +#endif + + protected: + NonCopyable() {} + virtual ~NonCopyable(); + }; + + class SafeBool { + public: + typedef void (SafeBool::*type)() const; + + static type makeSafe( bool value ) { + return value ? &SafeBool::trueValue : 0; + } + private: + void trueValue() const {} + }; + + template<typename ContainerT> + inline void deleteAll( ContainerT& container ) { + typename ContainerT::const_iterator it = container.begin(); + typename ContainerT::const_iterator itEnd = container.end(); + for(; it != itEnd; ++it ) + delete *it; + } + template<typename AssociativeContainerT> + inline void deleteAllValues( AssociativeContainerT& container ) { + typename AssociativeContainerT::const_iterator it = container.begin(); + typename AssociativeContainerT::const_iterator itEnd = container.end(); + for(; it != itEnd; ++it ) + delete it->second; + } + + bool startsWith( std::string const& s, std::string const& prefix ); + bool startsWith( std::string const& s, char prefix ); + bool endsWith( std::string const& s, std::string const& suffix ); + bool endsWith( std::string const& s, char suffix ); + bool contains( std::string const& s, std::string const& infix ); + void toLowerInPlace( std::string& s ); + std::string toLower( std::string const& s ); + std::string trim( std::string const& str ); + bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ); + + struct pluralise { + pluralise( std::size_t count, std::string const& label ); + + friend std::ostream& operator << ( std::ostream& os, pluralise const& pluraliser ); + + std::size_t m_count; + std::string m_label; + }; + + struct SourceLineInfo { + + SourceLineInfo(); + SourceLineInfo( char const* _file, std::size_t _line ); +# ifdef CATCH_CONFIG_CPP11_GENERATED_METHODS + SourceLineInfo(SourceLineInfo const& other) = default; + SourceLineInfo( SourceLineInfo && ) = default; + SourceLineInfo& operator = ( SourceLineInfo const& ) = default; + SourceLineInfo& operator = ( SourceLineInfo && ) = default; +# endif + bool empty() const; + bool operator == ( SourceLineInfo const& other ) const; + bool operator < ( SourceLineInfo const& other ) const; + + char const* file; + std::size_t line; + }; + + std::ostream& operator << ( std::ostream& os, SourceLineInfo const& info ); + + // This is just here to avoid compiler warnings with macro constants and boolean literals + inline bool isTrue( bool value ){ return value; } + inline bool alwaysTrue() { return true; } + inline bool alwaysFalse() { return false; } + + void throwLogicError( std::string const& message, SourceLineInfo const& locationInfo ); + + void seedRng( IConfig const& config ); + unsigned int rngSeed(); + + // Use this in variadic streaming macros to allow + // >> +StreamEndStop + // as well as + // >> stuff +StreamEndStop + struct StreamEndStop { + std::string operator+() { + return std::string(); + } + }; + template<typename T> + T const& operator + ( T const& value, StreamEndStop ) { + return value; + } +} + +#define CATCH_INTERNAL_LINEINFO ::Catch::SourceLineInfo( __FILE__, static_cast<std::size_t>( __LINE__ ) ) +#define CATCH_INTERNAL_ERROR( msg ) ::Catch::throwLogicError( msg, CATCH_INTERNAL_LINEINFO ); + +namespace Catch { + + class NotImplementedException : public std::exception + { + public: + NotImplementedException( SourceLineInfo const& lineInfo ); + NotImplementedException( NotImplementedException const& ) {} + + virtual ~NotImplementedException() CATCH_NOEXCEPT {} + + virtual const char* what() const CATCH_NOEXCEPT; + + private: + std::string m_what; + SourceLineInfo m_lineInfo; + }; + +} // end namespace Catch + +/////////////////////////////////////////////////////////////////////////////// +#define CATCH_NOT_IMPLEMENTED throw Catch::NotImplementedException( CATCH_INTERNAL_LINEINFO ) + +// #included from: internal/catch_context.h +#define TWOBLUECUBES_CATCH_CONTEXT_H_INCLUDED + +// #included from: catch_interfaces_generators.h +#define TWOBLUECUBES_CATCH_INTERFACES_GENERATORS_H_INCLUDED + +#include <string> + +namespace Catch { + + struct IGeneratorInfo { + virtual ~IGeneratorInfo(); + virtual bool moveNext() = 0; + virtual std::size_t getCurrentIndex() const = 0; + }; + + struct IGeneratorsForTest { + virtual ~IGeneratorsForTest(); + + virtual IGeneratorInfo& getGeneratorInfo( std::string const& fileInfo, std::size_t size ) = 0; + virtual bool moveNext() = 0; + }; + + IGeneratorsForTest* createGeneratorsForTest(); + +} // end namespace Catch + +// #included from: catch_ptr.hpp +#define TWOBLUECUBES_CATCH_PTR_HPP_INCLUDED + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" +#endif + +namespace Catch { + + // An intrusive reference counting smart pointer. + // T must implement addRef() and release() methods + // typically implementing the IShared interface + template<typename T> + class Ptr { + public: + Ptr() : m_p( CATCH_NULL ){} + Ptr( T* p ) : m_p( p ){ + if( m_p ) + m_p->addRef(); + } + Ptr( Ptr const& other ) : m_p( other.m_p ){ + if( m_p ) + m_p->addRef(); + } + ~Ptr(){ + if( m_p ) + m_p->release(); + } + void reset() { + if( m_p ) + m_p->release(); + m_p = CATCH_NULL; + } + Ptr& operator = ( T* p ){ + Ptr temp( p ); + swap( temp ); + return *this; + } + Ptr& operator = ( Ptr const& other ){ + Ptr temp( other ); + swap( temp ); + return *this; + } + void swap( Ptr& other ) { std::swap( m_p, other.m_p ); } + T* get() const{ return m_p; } + T& operator*() const { return *m_p; } + T* operator->() const { return m_p; } + bool operator !() const { return m_p == CATCH_NULL; } + operator SafeBool::type() const { return SafeBool::makeSafe( m_p != CATCH_NULL ); } + + private: + T* m_p; + }; + + struct IShared : NonCopyable { + virtual ~IShared(); + virtual void addRef() const = 0; + virtual void release() const = 0; + }; + + template<typename T = IShared> + struct SharedImpl : T { + + SharedImpl() : m_rc( 0 ){} + + virtual void addRef() const { + ++m_rc; + } + virtual void release() const { + if( --m_rc == 0 ) + delete this; + } + + mutable unsigned int m_rc; + }; + +} // end namespace Catch + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +namespace Catch { + + class TestCase; + class Stream; + struct IResultCapture; + struct IRunner; + struct IGeneratorsForTest; + struct IConfig; + + struct IContext + { + virtual ~IContext(); + + virtual IResultCapture* getResultCapture() = 0; + virtual IRunner* getRunner() = 0; + virtual size_t getGeneratorIndex( std::string const& fileInfo, size_t totalSize ) = 0; + virtual bool advanceGeneratorsForCurrentTest() = 0; + virtual Ptr<IConfig const> getConfig() const = 0; + }; + + struct IMutableContext : IContext + { + virtual ~IMutableContext(); + virtual void setResultCapture( IResultCapture* resultCapture ) = 0; + virtual void setRunner( IRunner* runner ) = 0; + virtual void setConfig( Ptr<IConfig const> const& config ) = 0; + }; + + IContext& getCurrentContext(); + IMutableContext& getCurrentMutableContext(); + void cleanUpContext(); + Stream createStream( std::string const& streamName ); + +} + +// #included from: internal/catch_test_registry.hpp +#define TWOBLUECUBES_CATCH_TEST_REGISTRY_HPP_INCLUDED + +// #included from: catch_interfaces_testcase.h +#define TWOBLUECUBES_CATCH_INTERFACES_TESTCASE_H_INCLUDED + +#include <vector> + +namespace Catch { + + class TestSpec; + + struct ITestCase : IShared { + virtual void invoke () const = 0; + protected: + virtual ~ITestCase(); + }; + + class TestCase; + struct IConfig; + + struct ITestCaseRegistry { + virtual ~ITestCaseRegistry(); + virtual std::vector<TestCase> const& getAllTests() const = 0; + virtual std::vector<TestCase> const& getAllTestsSorted( IConfig const& config ) const = 0; + }; + + bool matchTest( TestCase const& testCase, TestSpec const& testSpec, IConfig const& config ); + std::vector<TestCase> filterTests( std::vector<TestCase> const& testCases, TestSpec const& testSpec, IConfig const& config ); + std::vector<TestCase> const& getAllTestCasesSorted( IConfig const& config ); + +} + +namespace Catch { + +template<typename C> +class MethodTestCase : public SharedImpl<ITestCase> { + +public: + MethodTestCase( void (C::*method)() ) : m_method( method ) {} + + virtual void invoke() const { + C obj; + (obj.*m_method)(); + } + +private: + virtual ~MethodTestCase() {} + + void (C::*m_method)(); +}; + +typedef void(*TestFunction)(); + +struct NameAndDesc { + NameAndDesc( const char* _name = "", const char* _description= "" ) + : name( _name ), description( _description ) + {} + + const char* name; + const char* description; +}; + +void registerTestCase + ( ITestCase* testCase, + char const* className, + NameAndDesc const& nameAndDesc, + SourceLineInfo const& lineInfo ); + +struct AutoReg { + + AutoReg + ( TestFunction function, + SourceLineInfo const& lineInfo, + NameAndDesc const& nameAndDesc ); + + template<typename C> + AutoReg + ( void (C::*method)(), + char const* className, + NameAndDesc const& nameAndDesc, + SourceLineInfo const& lineInfo ) { + + registerTestCase + ( new MethodTestCase<C>( method ), + className, + nameAndDesc, + lineInfo ); + } + + ~AutoReg(); + +private: + AutoReg( AutoReg const& ); + void operator= ( AutoReg const& ); +}; + +void registerTestCaseFunction + ( TestFunction function, + SourceLineInfo const& lineInfo, + NameAndDesc const& nameAndDesc ); + +} // end namespace Catch + +#ifdef CATCH_CONFIG_VARIADIC_MACROS + /////////////////////////////////////////////////////////////////////////////// + #define INTERNAL_CATCH_TESTCASE2( TestName, ... ) \ + static void TestName(); \ + namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &TestName, CATCH_INTERNAL_LINEINFO, Catch::NameAndDesc( __VA_ARGS__ ) ); }\ + static void TestName() + #define INTERNAL_CATCH_TESTCASE( ... ) \ + INTERNAL_CATCH_TESTCASE2( INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), __VA_ARGS__ ) + + /////////////////////////////////////////////////////////////////////////////// + #define INTERNAL_CATCH_METHOD_AS_TEST_CASE( QualifiedMethod, ... ) \ + namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &QualifiedMethod, "&" #QualifiedMethod, Catch::NameAndDesc( __VA_ARGS__ ), CATCH_INTERNAL_LINEINFO ); } + + /////////////////////////////////////////////////////////////////////////////// + #define INTERNAL_CATCH_TEST_CASE_METHOD2( TestName, ClassName, ... )\ + namespace{ \ + struct TestName : ClassName{ \ + void test(); \ + }; \ + Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar ) ( &TestName::test, #ClassName, Catch::NameAndDesc( __VA_ARGS__ ), CATCH_INTERNAL_LINEINFO ); \ + } \ + void TestName::test() + #define INTERNAL_CATCH_TEST_CASE_METHOD( ClassName, ... ) \ + INTERNAL_CATCH_TEST_CASE_METHOD2( INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), ClassName, __VA_ARGS__ ) + + /////////////////////////////////////////////////////////////////////////////// + #define INTERNAL_CATCH_REGISTER_TESTCASE( Function, ... ) \ + Catch::AutoReg( Function, CATCH_INTERNAL_LINEINFO, Catch::NameAndDesc( __VA_ARGS__ ) ); + +#else + /////////////////////////////////////////////////////////////////////////////// + #define INTERNAL_CATCH_TESTCASE2( TestName, Name, Desc ) \ + static void TestName(); \ + namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &TestName, CATCH_INTERNAL_LINEINFO, Catch::NameAndDesc( Name, Desc ) ); }\ + static void TestName() + #define INTERNAL_CATCH_TESTCASE( Name, Desc ) \ + INTERNAL_CATCH_TESTCASE2( INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), Name, Desc ) + + /////////////////////////////////////////////////////////////////////////////// + #define INTERNAL_CATCH_METHOD_AS_TEST_CASE( QualifiedMethod, Name, Desc ) \ + namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &QualifiedMethod, "&" #QualifiedMethod, Catch::NameAndDesc( Name, Desc ), CATCH_INTERNAL_LINEINFO ); } + + /////////////////////////////////////////////////////////////////////////////// + #define INTERNAL_CATCH_TEST_CASE_METHOD2( TestCaseName, ClassName, TestName, Desc )\ + namespace{ \ + struct TestCaseName : ClassName{ \ + void test(); \ + }; \ + Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar ) ( &TestCaseName::test, #ClassName, Catch::NameAndDesc( TestName, Desc ), CATCH_INTERNAL_LINEINFO ); \ + } \ + void TestCaseName::test() + #define INTERNAL_CATCH_TEST_CASE_METHOD( ClassName, TestName, Desc )\ + INTERNAL_CATCH_TEST_CASE_METHOD2( INTERNAL_CATCH_UNIQUE_NAME( ____C_A_T_C_H____T_E_S_T____ ), ClassName, TestName, Desc ) + + /////////////////////////////////////////////////////////////////////////////// + #define INTERNAL_CATCH_REGISTER_TESTCASE( Function, Name, Desc ) \ + Catch::AutoReg( Function, CATCH_INTERNAL_LINEINFO, Catch::NameAndDesc( Name, Desc ) ); +#endif + +// #included from: internal/catch_capture.hpp +#define TWOBLUECUBES_CATCH_CAPTURE_HPP_INCLUDED + +// #included from: catch_result_builder.h +#define TWOBLUECUBES_CATCH_RESULT_BUILDER_H_INCLUDED + +// #included from: catch_result_type.h +#define TWOBLUECUBES_CATCH_RESULT_TYPE_H_INCLUDED + +namespace Catch { + + // ResultWas::OfType enum + struct ResultWas { enum OfType { + Unknown = -1, + Ok = 0, + Info = 1, + Warning = 2, + + FailureBit = 0x10, + + ExpressionFailed = FailureBit | 1, + ExplicitFailure = FailureBit | 2, + + Exception = 0x100 | FailureBit, + + ThrewException = Exception | 1, + DidntThrowException = Exception | 2, + + FatalErrorCondition = 0x200 | FailureBit + + }; }; + + inline bool isOk( ResultWas::OfType resultType ) { + return ( resultType & ResultWas::FailureBit ) == 0; + } + inline bool isJustInfo( int flags ) { + return flags == ResultWas::Info; + } + + // ResultDisposition::Flags enum + struct ResultDisposition { enum Flags { + Normal = 0x01, + + ContinueOnFailure = 0x02, // Failures fail test, but execution continues + FalseTest = 0x04, // Prefix expression with ! + SuppressFail = 0x08 // Failures are reported but do not fail the test + }; }; + + inline ResultDisposition::Flags operator | ( ResultDisposition::Flags lhs, ResultDisposition::Flags rhs ) { + return static_cast<ResultDisposition::Flags>( static_cast<int>( lhs ) | static_cast<int>( rhs ) ); + } + + inline bool shouldContinueOnFailure( int flags ) { return ( flags & ResultDisposition::ContinueOnFailure ) != 0; } + inline bool isFalseTest( int flags ) { return ( flags & ResultDisposition::FalseTest ) != 0; } + inline bool shouldSuppressFailure( int flags ) { return ( flags & ResultDisposition::SuppressFail ) != 0; } + +} // end namespace Catch + +// #included from: catch_assertionresult.h +#define TWOBLUECUBES_CATCH_ASSERTIONRESULT_H_INCLUDED + +#include <string> + +namespace Catch { + + struct STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison; + + struct DecomposedExpression + { + virtual ~DecomposedExpression() {} + virtual bool isBinaryExpression() const { + return false; + } + virtual void reconstructExpression( std::string& dest ) const = 0; + + // Only simple binary comparisons can be decomposed. + // If more complex check is required then wrap sub-expressions in parentheses. + template<typename T> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator + ( T const& ); + template<typename T> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator - ( T const& ); + template<typename T> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator * ( T const& ); + template<typename T> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator / ( T const& ); + template<typename T> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator % ( T const& ); + template<typename T> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator && ( T const& ); + template<typename T> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator || ( T const& ); + + private: + DecomposedExpression& operator = (DecomposedExpression const&); + }; + + struct AssertionInfo + { + AssertionInfo() {} + AssertionInfo( std::string const& _macroName, + SourceLineInfo const& _lineInfo, + std::string const& _capturedExpression, + ResultDisposition::Flags _resultDisposition ); + + std::string macroName; + SourceLineInfo lineInfo; + std::string capturedExpression; + ResultDisposition::Flags resultDisposition; + }; + + struct AssertionResultData + { + AssertionResultData() : decomposedExpression( CATCH_NULL ) + , resultType( ResultWas::Unknown ) + , negated( false ) + , parenthesized( false ) {} + + void negate( bool parenthesize ) { + negated = !negated; + parenthesized = parenthesize; + if( resultType == ResultWas::Ok ) + resultType = ResultWas::ExpressionFailed; + else if( resultType == ResultWas::ExpressionFailed ) + resultType = ResultWas::Ok; + } + + std::string const& reconstructExpression() const { + if( decomposedExpression != CATCH_NULL ) { + decomposedExpression->reconstructExpression( reconstructedExpression ); + if( parenthesized ) { + reconstructedExpression.insert( 0, 1, '(' ); + reconstructedExpression.append( 1, ')' ); + } + if( negated ) { + reconstructedExpression.insert( 0, 1, '!' ); + } + decomposedExpression = CATCH_NULL; + } + return reconstructedExpression; + } + + mutable DecomposedExpression const* decomposedExpression; + mutable std::string reconstructedExpression; + std::string message; + ResultWas::OfType resultType; + bool negated; + bool parenthesized; + }; + + class AssertionResult { + public: + AssertionResult(); + AssertionResult( AssertionInfo const& info, AssertionResultData const& data ); + ~AssertionResult(); +# ifdef CATCH_CONFIG_CPP11_GENERATED_METHODS + AssertionResult( AssertionResult const& ) = default; + AssertionResult( AssertionResult && ) = default; + AssertionResult& operator = ( AssertionResult const& ) = default; + AssertionResult& operator = ( AssertionResult && ) = default; +# endif + + bool isOk() const; + bool succeeded() const; + ResultWas::OfType getResultType() const; + bool hasExpression() const; + bool hasMessage() const; + std::string getExpression() const; + std::string getExpressionInMacro() const; + bool hasExpandedExpression() const; + std::string getExpandedExpression() const; + std::string getMessage() const; + SourceLineInfo getSourceInfo() const; + std::string getTestMacroName() const; + void discardDecomposedExpression() const; + void expandDecomposedExpression() const; + + protected: + AssertionInfo m_info; + AssertionResultData m_resultData; + }; + +} // end namespace Catch + +// #included from: catch_matchers.hpp +#define TWOBLUECUBES_CATCH_MATCHERS_HPP_INCLUDED + +namespace Catch { +namespace Matchers { + namespace Impl { + + template<typename ArgT> struct MatchAllOf; + template<typename ArgT> struct MatchAnyOf; + template<typename ArgT> struct MatchNotOf; + + class MatcherUntypedBase { + public: + std::string toString() const { + if( m_cachedToString.empty() ) + m_cachedToString = describe(); + return m_cachedToString; + } + + protected: + virtual std::string describe() const = 0; + mutable std::string m_cachedToString; + private: + MatcherUntypedBase& operator = ( MatcherUntypedBase const& ); + }; + + template<typename ObjectT, typename ComparatorT = ObjectT> + struct MatcherBase : MatcherUntypedBase { + + virtual bool match( ObjectT const& arg ) const = 0; + + MatchAllOf<ComparatorT> operator && ( MatcherBase const& other ) const; + MatchAnyOf<ComparatorT> operator || ( MatcherBase const& other ) const; + MatchNotOf<ComparatorT> operator ! () const; + }; + + template<typename ArgT> + struct MatchAllOf : MatcherBase<ArgT> { + virtual bool match( ArgT const& arg ) const CATCH_OVERRIDE { + for( std::size_t i = 0; i < m_matchers.size(); ++i ) { + if (!m_matchers[i]->match(arg)) + return false; + } + return true; + } + virtual std::string describe() const CATCH_OVERRIDE { + std::string description; + description.reserve( 4 + m_matchers.size()*32 ); + description += "( "; + for( std::size_t i = 0; i < m_matchers.size(); ++i ) { + if( i != 0 ) + description += " and "; + description += m_matchers[i]->toString(); + } + description += " )"; + return description; + } + + MatchAllOf<ArgT>& operator && ( MatcherBase<ArgT> const& other ) { + m_matchers.push_back( &other ); + return *this; + } + + std::vector<MatcherBase<ArgT> const*> m_matchers; + }; + template<typename ArgT> + struct MatchAnyOf : MatcherBase<ArgT> { + + virtual bool match( ArgT const& arg ) const CATCH_OVERRIDE { + for( std::size_t i = 0; i < m_matchers.size(); ++i ) { + if (m_matchers[i]->match(arg)) + return true; + } + return false; + } + virtual std::string describe() const CATCH_OVERRIDE { + std::string description; + description.reserve( 4 + m_matchers.size()*32 ); + description += "( "; + for( std::size_t i = 0; i < m_matchers.size(); ++i ) { + if( i != 0 ) + description += " or "; + description += m_matchers[i]->toString(); + } + description += " )"; + return description; + } + + MatchAnyOf<ArgT>& operator || ( MatcherBase<ArgT> const& other ) { + m_matchers.push_back( &other ); + return *this; + } + + std::vector<MatcherBase<ArgT> const*> m_matchers; + }; + + template<typename ArgT> + struct MatchNotOf : MatcherBase<ArgT> { + + MatchNotOf( MatcherBase<ArgT> const& underlyingMatcher ) : m_underlyingMatcher( underlyingMatcher ) {} + + virtual bool match( ArgT const& arg ) const CATCH_OVERRIDE { + return !m_underlyingMatcher.match( arg ); + } + + virtual std::string describe() const CATCH_OVERRIDE { + return "not " + m_underlyingMatcher.toString(); + } + MatcherBase<ArgT> const& m_underlyingMatcher; + }; + + template<typename ObjectT, typename ComparatorT> + MatchAllOf<ComparatorT> MatcherBase<ObjectT, ComparatorT>::operator && ( MatcherBase const& other ) const { + return MatchAllOf<ComparatorT>() && *this && other; + } + template<typename ObjectT, typename ComparatorT> + MatchAnyOf<ComparatorT> MatcherBase<ObjectT, ComparatorT>::operator || ( MatcherBase const& other ) const { + return MatchAnyOf<ComparatorT>() || *this || other; + } + template<typename ObjectT, typename ComparatorT> + MatchNotOf<ComparatorT> MatcherBase<ObjectT, ComparatorT>::operator ! () const { + return MatchNotOf<ComparatorT>( *this ); + } + + } // namespace Impl + + // The following functions create the actual matcher objects. + // This allows the types to be inferred + // - deprecated: prefer ||, && and ! + template<typename T> + inline Impl::MatchNotOf<T> Not( Impl::MatcherBase<T> const& underlyingMatcher ) { + return Impl::MatchNotOf<T>( underlyingMatcher ); + } + template<typename T> + inline Impl::MatchAllOf<T> AllOf( Impl::MatcherBase<T> const& m1, Impl::MatcherBase<T> const& m2 ) { + return Impl::MatchAllOf<T>() && m1 && m2; + } + template<typename T> + inline Impl::MatchAllOf<T> AllOf( Impl::MatcherBase<T> const& m1, Impl::MatcherBase<T> const& m2, Impl::MatcherBase<T> const& m3 ) { + return Impl::MatchAllOf<T>() && m1 && m2 && m3; + } + template<typename T> + inline Impl::MatchAnyOf<T> AnyOf( Impl::MatcherBase<T> const& m1, Impl::MatcherBase<T> const& m2 ) { + return Impl::MatchAnyOf<T>() || m1 || m2; + } + template<typename T> + inline Impl::MatchAnyOf<T> AnyOf( Impl::MatcherBase<T> const& m1, Impl::MatcherBase<T> const& m2, Impl::MatcherBase<T> const& m3 ) { + return Impl::MatchAnyOf<T>() || m1 || m2 || m3; + } + +} // namespace Matchers + +using namespace Matchers; +using Matchers::Impl::MatcherBase; + +} // namespace Catch + +namespace Catch { + + struct TestFailureException{}; + + template<typename T> class ExpressionLhs; + + struct CopyableStream { + CopyableStream() {} + CopyableStream( CopyableStream const& other ) { + oss << other.oss.str(); + } + CopyableStream& operator=( CopyableStream const& other ) { + oss.str(std::string()); + oss << other.oss.str(); + return *this; + } + std::ostringstream oss; + }; + + class ResultBuilder : public DecomposedExpression { + public: + ResultBuilder( char const* macroName, + SourceLineInfo const& lineInfo, + char const* capturedExpression, + ResultDisposition::Flags resultDisposition, + char const* secondArg = "" ); + + template<typename T> + ExpressionLhs<T const&> operator <= ( T const& operand ); + ExpressionLhs<bool> operator <= ( bool value ); + + template<typename T> + ResultBuilder& operator << ( T const& value ) { + m_stream.oss << value; + return *this; + } + + ResultBuilder& setResultType( ResultWas::OfType result ); + ResultBuilder& setResultType( bool result ); + + void endExpression( DecomposedExpression const& expr ); + + virtual void reconstructExpression( std::string& dest ) const CATCH_OVERRIDE; + + AssertionResult build() const; + AssertionResult build( DecomposedExpression const& expr ) const; + + void useActiveException( ResultDisposition::Flags resultDisposition = ResultDisposition::Normal ); + void captureResult( ResultWas::OfType resultType ); + void captureExpression(); + void captureExpectedException( std::string const& expectedMessage ); + void captureExpectedException( Matchers::Impl::MatcherBase<std::string> const& matcher ); + void handleResult( AssertionResult const& result ); + void react(); + bool shouldDebugBreak() const; + bool allowThrows() const; + + template<typename ArgT, typename MatcherT> + void captureMatch( ArgT const& arg, MatcherT const& matcher, char const* matcherString ); + + private: + AssertionInfo m_assertionInfo; + AssertionResultData m_data; + CopyableStream m_stream; + + bool m_shouldDebugBreak; + bool m_shouldThrow; + }; + +} // namespace Catch + +// Include after due to circular dependency: +// #included from: catch_expression_lhs.hpp +#define TWOBLUECUBES_CATCH_EXPRESSION_LHS_HPP_INCLUDED + +// #included from: catch_evaluate.hpp +#define TWOBLUECUBES_CATCH_EVALUATE_HPP_INCLUDED + +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable:4389) // '==' : signed/unsigned mismatch +#pragma warning(disable:4312) // Converting int to T* using reinterpret_cast (issue on x64 platform) +#endif + +#include <cstddef> + +namespace Catch { +namespace Internal { + + enum Operator { + IsEqualTo, + IsNotEqualTo, + IsLessThan, + IsGreaterThan, + IsLessThanOrEqualTo, + IsGreaterThanOrEqualTo + }; + + template<Operator Op> struct OperatorTraits { static const char* getName(){ return "*error*"; } }; + template<> struct OperatorTraits<IsEqualTo> { static const char* getName(){ return "=="; } }; + template<> struct OperatorTraits<IsNotEqualTo> { static const char* getName(){ return "!="; } }; + template<> struct OperatorTraits<IsLessThan> { static const char* getName(){ return "<"; } }; + template<> struct OperatorTraits<IsGreaterThan> { static const char* getName(){ return ">"; } }; + template<> struct OperatorTraits<IsLessThanOrEqualTo> { static const char* getName(){ return "<="; } }; + template<> struct OperatorTraits<IsGreaterThanOrEqualTo>{ static const char* getName(){ return ">="; } }; + + template<typename T> + inline T& opCast(T const& t) { return const_cast<T&>(t); } + +// nullptr_t support based on pull request #154 from Konstantin Baumann +#ifdef CATCH_CONFIG_CPP11_NULLPTR + inline std::nullptr_t opCast(std::nullptr_t) { return nullptr; } +#endif // CATCH_CONFIG_CPP11_NULLPTR + + // So the compare overloads can be operator agnostic we convey the operator as a template + // enum, which is used to specialise an Evaluator for doing the comparison. + template<typename T1, typename T2, Operator Op> + class Evaluator{}; + + template<typename T1, typename T2> + struct Evaluator<T1, T2, IsEqualTo> { + static bool evaluate( T1 const& lhs, T2 const& rhs) { + return bool( opCast( lhs ) == opCast( rhs ) ); + } + }; + template<typename T1, typename T2> + struct Evaluator<T1, T2, IsNotEqualTo> { + static bool evaluate( T1 const& lhs, T2 const& rhs ) { + return bool( opCast( lhs ) != opCast( rhs ) ); + } + }; + template<typename T1, typename T2> + struct Evaluator<T1, T2, IsLessThan> { + static bool evaluate( T1 const& lhs, T2 const& rhs ) { + return bool( opCast( lhs ) < opCast( rhs ) ); + } + }; + template<typename T1, typename T2> + struct Evaluator<T1, T2, IsGreaterThan> { + static bool evaluate( T1 const& lhs, T2 const& rhs ) { + return bool( opCast( lhs ) > opCast( rhs ) ); + } + }; + template<typename T1, typename T2> + struct Evaluator<T1, T2, IsGreaterThanOrEqualTo> { + static bool evaluate( T1 const& lhs, T2 const& rhs ) { + return bool( opCast( lhs ) >= opCast( rhs ) ); + } + }; + template<typename T1, typename T2> + struct Evaluator<T1, T2, IsLessThanOrEqualTo> { + static bool evaluate( T1 const& lhs, T2 const& rhs ) { + return bool( opCast( lhs ) <= opCast( rhs ) ); + } + }; + + template<Operator Op, typename T1, typename T2> + bool applyEvaluator( T1 const& lhs, T2 const& rhs ) { + return Evaluator<T1, T2, Op>::evaluate( lhs, rhs ); + } + + // This level of indirection allows us to specialise for integer types + // to avoid signed/ unsigned warnings + + // "base" overload + template<Operator Op, typename T1, typename T2> + bool compare( T1 const& lhs, T2 const& rhs ) { + return Evaluator<T1, T2, Op>::evaluate( lhs, rhs ); + } + + // unsigned X to int + template<Operator Op> bool compare( unsigned int lhs, int rhs ) { + return applyEvaluator<Op>( lhs, static_cast<unsigned int>( rhs ) ); + } + template<Operator Op> bool compare( unsigned long lhs, int rhs ) { + return applyEvaluator<Op>( lhs, static_cast<unsigned int>( rhs ) ); + } + template<Operator Op> bool compare( unsigned char lhs, int rhs ) { + return applyEvaluator<Op>( lhs, static_cast<unsigned int>( rhs ) ); + } + + // unsigned X to long + template<Operator Op> bool compare( unsigned int lhs, long rhs ) { + return applyEvaluator<Op>( lhs, static_cast<unsigned long>( rhs ) ); + } + template<Operator Op> bool compare( unsigned long lhs, long rhs ) { + return applyEvaluator<Op>( lhs, static_cast<unsigned long>( rhs ) ); + } + template<Operator Op> bool compare( unsigned char lhs, long rhs ) { + return applyEvaluator<Op>( lhs, static_cast<unsigned long>( rhs ) ); + } + + // int to unsigned X + template<Operator Op> bool compare( int lhs, unsigned int rhs ) { + return applyEvaluator<Op>( static_cast<unsigned int>( lhs ), rhs ); + } + template<Operator Op> bool compare( int lhs, unsigned long rhs ) { + return applyEvaluator<Op>( static_cast<unsigned int>( lhs ), rhs ); + } + template<Operator Op> bool compare( int lhs, unsigned char rhs ) { + return applyEvaluator<Op>( static_cast<unsigned int>( lhs ), rhs ); + } + + // long to unsigned X + template<Operator Op> bool compare( long lhs, unsigned int rhs ) { + return applyEvaluator<Op>( static_cast<unsigned long>( lhs ), rhs ); + } + template<Operator Op> bool compare( long lhs, unsigned long rhs ) { + return applyEvaluator<Op>( static_cast<unsigned long>( lhs ), rhs ); + } + template<Operator Op> bool compare( long lhs, unsigned char rhs ) { + return applyEvaluator<Op>( static_cast<unsigned long>( lhs ), rhs ); + } + + // pointer to long (when comparing against NULL) + template<Operator Op, typename T> bool compare( long lhs, T* rhs ) { + return Evaluator<T*, T*, Op>::evaluate( reinterpret_cast<T*>( lhs ), rhs ); + } + template<Operator Op, typename T> bool compare( T* lhs, long rhs ) { + return Evaluator<T*, T*, Op>::evaluate( lhs, reinterpret_cast<T*>( rhs ) ); + } + + // pointer to int (when comparing against NULL) + template<Operator Op, typename T> bool compare( int lhs, T* rhs ) { + return Evaluator<T*, T*, Op>::evaluate( reinterpret_cast<T*>( lhs ), rhs ); + } + template<Operator Op, typename T> bool compare( T* lhs, int rhs ) { + return Evaluator<T*, T*, Op>::evaluate( lhs, reinterpret_cast<T*>( rhs ) ); + } + +#ifdef CATCH_CONFIG_CPP11_LONG_LONG + // long long to unsigned X + template<Operator Op> bool compare( long long lhs, unsigned int rhs ) { + return applyEvaluator<Op>( static_cast<unsigned long>( lhs ), rhs ); + } + template<Operator Op> bool compare( long long lhs, unsigned long rhs ) { + return applyEvaluator<Op>( static_cast<unsigned long>( lhs ), rhs ); + } + template<Operator Op> bool compare( long long lhs, unsigned long long rhs ) { + return applyEvaluator<Op>( static_cast<unsigned long>( lhs ), rhs ); + } + template<Operator Op> bool compare( long long lhs, unsigned char rhs ) { + return applyEvaluator<Op>( static_cast<unsigned long>( lhs ), rhs ); + } + + // unsigned long long to X + template<Operator Op> bool compare( unsigned long long lhs, int rhs ) { + return applyEvaluator<Op>( static_cast<long>( lhs ), rhs ); + } + template<Operator Op> bool compare( unsigned long long lhs, long rhs ) { + return applyEvaluator<Op>( static_cast<long>( lhs ), rhs ); + } + template<Operator Op> bool compare( unsigned long long lhs, long long rhs ) { + return applyEvaluator<Op>( static_cast<long>( lhs ), rhs ); + } + template<Operator Op> bool compare( unsigned long long lhs, char rhs ) { + return applyEvaluator<Op>( static_cast<long>( lhs ), rhs ); + } + + // pointer to long long (when comparing against NULL) + template<Operator Op, typename T> bool compare( long long lhs, T* rhs ) { + return Evaluator<T*, T*, Op>::evaluate( reinterpret_cast<T*>( lhs ), rhs ); + } + template<Operator Op, typename T> bool compare( T* lhs, long long rhs ) { + return Evaluator<T*, T*, Op>::evaluate( lhs, reinterpret_cast<T*>( rhs ) ); + } +#endif // CATCH_CONFIG_CPP11_LONG_LONG + +#ifdef CATCH_CONFIG_CPP11_NULLPTR + // pointer to nullptr_t (when comparing against nullptr) + template<Operator Op, typename T> bool compare( std::nullptr_t, T* rhs ) { + return Evaluator<T*, T*, Op>::evaluate( nullptr, rhs ); + } + template<Operator Op, typename T> bool compare( T* lhs, std::nullptr_t ) { + return Evaluator<T*, T*, Op>::evaluate( lhs, nullptr ); + } +#endif // CATCH_CONFIG_CPP11_NULLPTR + +} // end of namespace Internal +} // end of namespace Catch + +#ifdef _MSC_VER +#pragma warning(pop) +#endif + +// #included from: catch_tostring.h +#define TWOBLUECUBES_CATCH_TOSTRING_H_INCLUDED + +#include <sstream> +#include <iomanip> +#include <limits> +#include <vector> +#include <cstddef> + +#ifdef __OBJC__ +// #included from: catch_objc_arc.hpp +#define TWOBLUECUBES_CATCH_OBJC_ARC_HPP_INCLUDED + +#import <Foundation/Foundation.h> + +#ifdef __has_feature +#define CATCH_ARC_ENABLED __has_feature(objc_arc) +#else +#define CATCH_ARC_ENABLED 0 +#endif + +void arcSafeRelease( NSObject* obj ); +id performOptionalSelector( id obj, SEL sel ); + +#if !CATCH_ARC_ENABLED +inline void arcSafeRelease( NSObject* obj ) { + [obj release]; +} +inline id performOptionalSelector( id obj, SEL sel ) { + if( [obj respondsToSelector: sel] ) + return [obj performSelector: sel]; + return nil; +} +#define CATCH_UNSAFE_UNRETAINED +#define CATCH_ARC_STRONG +#else +inline void arcSafeRelease( NSObject* ){} +inline id performOptionalSelector( id obj, SEL sel ) { +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Warc-performSelector-leaks" +#endif + if( [obj respondsToSelector: sel] ) + return [obj performSelector: sel]; +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + return nil; +} +#define CATCH_UNSAFE_UNRETAINED __unsafe_unretained +#define CATCH_ARC_STRONG __strong +#endif + +#endif + +#ifdef CATCH_CONFIG_CPP11_TUPLE +#include <tuple> +#endif + +#ifdef CATCH_CONFIG_CPP11_IS_ENUM +#include <type_traits> +#endif + +namespace Catch { + +// Why we're here. +template<typename T> +std::string toString( T const& value ); + +// Built in overloads + +std::string toString( std::string const& value ); +std::string toString( std::wstring const& value ); +std::string toString( const char* const value ); +std::string toString( char* const value ); +std::string toString( const wchar_t* const value ); +std::string toString( wchar_t* const value ); +std::string toString( int value ); +std::string toString( unsigned long value ); +std::string toString( unsigned int value ); +std::string toString( const double value ); +std::string toString( const float value ); +std::string toString( bool value ); +std::string toString( char value ); +std::string toString( signed char value ); +std::string toString( unsigned char value ); + +#ifdef CATCH_CONFIG_CPP11_LONG_LONG +std::string toString( long long value ); +std::string toString( unsigned long long value ); +#endif + +#ifdef CATCH_CONFIG_CPP11_NULLPTR +std::string toString( std::nullptr_t ); +#endif + +#ifdef __OBJC__ + std::string toString( NSString const * const& nsstring ); + std::string toString( NSString * CATCH_ARC_STRONG const& nsstring ); + std::string toString( NSObject* const& nsObject ); +#endif + +namespace Detail { + + extern const std::string unprintableString; + + struct BorgType { + template<typename T> BorgType( T const& ); + }; + + struct TrueType { char sizer[1]; }; + struct FalseType { char sizer[2]; }; + + TrueType& testStreamable( std::ostream& ); + FalseType testStreamable( FalseType ); + + FalseType operator<<( std::ostream const&, BorgType const& ); + + template<typename T> + struct IsStreamInsertable { + static std::ostream &s; + static T const&t; + enum { value = sizeof( testStreamable(s << t) ) == sizeof( TrueType ) }; + }; + +#if defined(CATCH_CONFIG_CPP11_IS_ENUM) + template<typename T, + bool IsEnum = std::is_enum<T>::value + > + struct EnumStringMaker + { + static std::string convert( T const& ) { return unprintableString; } + }; + + template<typename T> + struct EnumStringMaker<T,true> + { + static std::string convert( T const& v ) + { + return ::Catch::toString( + static_cast<typename std::underlying_type<T>::type>(v) + ); + } + }; +#endif + template<bool C> + struct StringMakerBase { +#if defined(CATCH_CONFIG_CPP11_IS_ENUM) + template<typename T> + static std::string convert( T const& v ) + { + return EnumStringMaker<T>::convert( v ); + } +#else + template<typename T> + static std::string convert( T const& ) { return unprintableString; } +#endif + }; + + template<> + struct StringMakerBase<true> { + template<typename T> + static std::string convert( T const& _value ) { + std::ostringstream oss; + oss << _value; + return oss.str(); + } + }; + + std::string rawMemoryToString( const void *object, std::size_t size ); + + template<typename T> + inline std::string rawMemoryToString( const T& object ) { + return rawMemoryToString( &object, sizeof(object) ); + } + +} // end namespace Detail + +template<typename T> +struct StringMaker : + Detail::StringMakerBase<Detail::IsStreamInsertable<T>::value> {}; + +template<typename T> +struct StringMaker<T*> { + template<typename U> + static std::string convert( U* p ) { + if( !p ) + return "NULL"; + else + return Detail::rawMemoryToString( p ); + } +}; + +template<typename R, typename C> +struct StringMaker<R C::*> { + static std::string convert( R C::* p ) { + if( !p ) + return "NULL"; + else + return Detail::rawMemoryToString( p ); + } +}; + +namespace Detail { + template<typename InputIterator> + std::string rangeToString( InputIterator first, InputIterator last ); +} + +//template<typename T, typename Allocator> +//struct StringMaker<std::vector<T, Allocator> > { +// static std::string convert( std::vector<T,Allocator> const& v ) { +// return Detail::rangeToString( v.begin(), v.end() ); +// } +//}; + +template<typename T, typename Allocator> +std::string toString( std::vector<T,Allocator> const& v ) { + return Detail::rangeToString( v.begin(), v.end() ); +} + +#ifdef CATCH_CONFIG_CPP11_TUPLE + +// toString for tuples +namespace TupleDetail { + template< + typename Tuple, + std::size_t N = 0, + bool = (N < std::tuple_size<Tuple>::value) + > + struct ElementPrinter { + static void print( const Tuple& tuple, std::ostream& os ) + { + os << ( N ? ", " : " " ) + << Catch::toString(std::get<N>(tuple)); + ElementPrinter<Tuple,N+1>::print(tuple,os); + } + }; + + template< + typename Tuple, + std::size_t N + > + struct ElementPrinter<Tuple,N,false> { + static void print( const Tuple&, std::ostream& ) {} + }; + +} + +template<typename ...Types> +struct StringMaker<std::tuple<Types...>> { + + static std::string convert( const std::tuple<Types...>& tuple ) + { + std::ostringstream os; + os << '{'; + TupleDetail::ElementPrinter<std::tuple<Types...>>::print( tuple, os ); + os << " }"; + return os.str(); + } +}; +#endif // CATCH_CONFIG_CPP11_TUPLE + +namespace Detail { + template<typename T> + std::string makeString( T const& value ) { + return StringMaker<T>::convert( value ); + } +} // end namespace Detail + +/// \brief converts any type to a string +/// +/// The default template forwards on to ostringstream - except when an +/// ostringstream overload does not exist - in which case it attempts to detect +/// that and writes {?}. +/// Overload (not specialise) this template for custom typs that you don't want +/// to provide an ostream overload for. +template<typename T> +std::string toString( T const& value ) { + return StringMaker<T>::convert( value ); +} + + namespace Detail { + template<typename InputIterator> + std::string rangeToString( InputIterator first, InputIterator last ) { + std::ostringstream oss; + oss << "{ "; + if( first != last ) { + oss << Catch::toString( *first ); + for( ++first ; first != last ; ++first ) + oss << ", " << Catch::toString( *first ); + } + oss << " }"; + return oss.str(); + } +} + +} // end namespace Catch + +namespace Catch { + +template<typename LhsT, Internal::Operator Op, typename RhsT> +class BinaryExpression; + +template<typename ArgT, typename MatcherT> +class MatchExpression; + +// Wraps the LHS of an expression and overloads comparison operators +// for also capturing those and RHS (if any) +template<typename T> +class ExpressionLhs : public DecomposedExpression { +public: + ExpressionLhs( ResultBuilder& rb, T lhs ) : m_rb( rb ), m_lhs( lhs ), m_truthy(false) {} + + ExpressionLhs& operator = ( const ExpressionLhs& ); + + template<typename RhsT> + BinaryExpression<T, Internal::IsEqualTo, RhsT const&> + operator == ( RhsT const& rhs ) { + return captureExpression<Internal::IsEqualTo>( rhs ); + } + + template<typename RhsT> + BinaryExpression<T, Internal::IsNotEqualTo, RhsT const&> + operator != ( RhsT const& rhs ) { + return captureExpression<Internal::IsNotEqualTo>( rhs ); + } + + template<typename RhsT> + BinaryExpression<T, Internal::IsLessThan, RhsT const&> + operator < ( RhsT const& rhs ) { + return captureExpression<Internal::IsLessThan>( rhs ); + } + + template<typename RhsT> + BinaryExpression<T, Internal::IsGreaterThan, RhsT const&> + operator > ( RhsT const& rhs ) { + return captureExpression<Internal::IsGreaterThan>( rhs ); + } + + template<typename RhsT> + BinaryExpression<T, Internal::IsLessThanOrEqualTo, RhsT const&> + operator <= ( RhsT const& rhs ) { + return captureExpression<Internal::IsLessThanOrEqualTo>( rhs ); + } + + template<typename RhsT> + BinaryExpression<T, Internal::IsGreaterThanOrEqualTo, RhsT const&> + operator >= ( RhsT const& rhs ) { + return captureExpression<Internal::IsGreaterThanOrEqualTo>( rhs ); + } + + BinaryExpression<T, Internal::IsEqualTo, bool> operator == ( bool rhs ) { + return captureExpression<Internal::IsEqualTo>( rhs ); + } + + BinaryExpression<T, Internal::IsNotEqualTo, bool> operator != ( bool rhs ) { + return captureExpression<Internal::IsNotEqualTo>( rhs ); + } + + void endExpression() { + m_truthy = m_lhs ? true : false; + m_rb + .setResultType( m_truthy ) + .endExpression( *this ); + } + + virtual void reconstructExpression( std::string& dest ) const CATCH_OVERRIDE { + dest = Catch::toString( m_truthy ); + } + +private: + template<Internal::Operator Op, typename RhsT> + BinaryExpression<T, Op, RhsT&> captureExpression( RhsT& rhs ) const { + return BinaryExpression<T, Op, RhsT&>( m_rb, m_lhs, rhs ); + } + + template<Internal::Operator Op> + BinaryExpression<T, Op, bool> captureExpression( bool rhs ) const { + return BinaryExpression<T, Op, bool>( m_rb, m_lhs, rhs ); + } + +private: + ResultBuilder& m_rb; + T m_lhs; + bool m_truthy; +}; + +template<typename LhsT, Internal::Operator Op, typename RhsT> +class BinaryExpression : public DecomposedExpression { +public: + BinaryExpression( ResultBuilder& rb, LhsT lhs, RhsT rhs ) + : m_rb( rb ), m_lhs( lhs ), m_rhs( rhs ) {} + + BinaryExpression& operator = ( BinaryExpression& ); + + void endExpression() const { + m_rb + .setResultType( Internal::compare<Op>( m_lhs, m_rhs ) ) + .endExpression( *this ); + } + + virtual bool isBinaryExpression() const CATCH_OVERRIDE { + return true; + } + + virtual void reconstructExpression( std::string& dest ) const CATCH_OVERRIDE { + std::string lhs = Catch::toString( m_lhs ); + std::string rhs = Catch::toString( m_rhs ); + char delim = lhs.size() + rhs.size() < 40 && + lhs.find('\n') == std::string::npos && + rhs.find('\n') == std::string::npos ? ' ' : '\n'; + dest.reserve( 7 + lhs.size() + rhs.size() ); + // 2 for spaces around operator + // 2 for operator + // 2 for parentheses (conditionally added later) + // 1 for negation (conditionally added later) + dest = lhs; + dest += delim; + dest += Internal::OperatorTraits<Op>::getName(); + dest += delim; + dest += rhs; + } + +private: + ResultBuilder& m_rb; + LhsT m_lhs; + RhsT m_rhs; +}; + +template<typename ArgT, typename MatcherT> +class MatchExpression : public DecomposedExpression { +public: + MatchExpression( ArgT arg, MatcherT matcher, char const* matcherString ) + : m_arg( arg ), m_matcher( matcher ), m_matcherString( matcherString ) {} + + virtual bool isBinaryExpression() const CATCH_OVERRIDE { + return true; + } + + virtual void reconstructExpression( std::string& dest ) const CATCH_OVERRIDE { + std::string matcherAsString = m_matcher.toString(); + dest = Catch::toString( m_arg ); + dest += ' '; + if( matcherAsString == Detail::unprintableString ) + dest += m_matcherString; + else + dest += matcherAsString; + } + +private: + ArgT m_arg; + MatcherT m_matcher; + char const* m_matcherString; +}; + +} // end namespace Catch + + +namespace Catch { + + template<typename T> + inline ExpressionLhs<T const&> ResultBuilder::operator <= ( T const& operand ) { + return ExpressionLhs<T const&>( *this, operand ); + } + + inline ExpressionLhs<bool> ResultBuilder::operator <= ( bool value ) { + return ExpressionLhs<bool>( *this, value ); + } + + template<typename ArgT, typename MatcherT> + inline void ResultBuilder::captureMatch( ArgT const& arg, MatcherT const& matcher, + char const* matcherString ) { + MatchExpression<ArgT const&, MatcherT const&> expr( arg, matcher, matcherString ); + setResultType( matcher.match( arg ) ); + endExpression( expr ); + } + +} // namespace Catch + +// #included from: catch_message.h +#define TWOBLUECUBES_CATCH_MESSAGE_H_INCLUDED + +#include <string> + +namespace Catch { + + struct MessageInfo { + MessageInfo( std::string const& _macroName, + SourceLineInfo const& _lineInfo, + ResultWas::OfType _type ); + + std::string macroName; + SourceLineInfo lineInfo; + ResultWas::OfType type; + std::string message; + unsigned int sequence; + + bool operator == ( MessageInfo const& other ) const { + return sequence == other.sequence; + } + bool operator < ( MessageInfo const& other ) const { + return sequence < other.sequence; + } + private: + static unsigned int globalCount; + }; + + struct MessageBuilder { + MessageBuilder( std::string const& macroName, + SourceLineInfo const& lineInfo, + ResultWas::OfType type ) + : m_info( macroName, lineInfo, type ) + {} + + template<typename T> + MessageBuilder& operator << ( T const& value ) { + m_stream << value; + return *this; + } + + MessageInfo m_info; + std::ostringstream m_stream; + }; + + class ScopedMessage { + public: + ScopedMessage( MessageBuilder const& builder ); + ScopedMessage( ScopedMessage const& other ); + ~ScopedMessage(); + + MessageInfo m_info; + }; + +} // end namespace Catch + +// #included from: catch_interfaces_capture.h +#define TWOBLUECUBES_CATCH_INTERFACES_CAPTURE_H_INCLUDED + +#include <string> + +namespace Catch { + + class TestCase; + class AssertionResult; + struct AssertionInfo; + struct SectionInfo; + struct SectionEndInfo; + struct MessageInfo; + class ScopedMessageBuilder; + struct Counts; + + struct IResultCapture { + + virtual ~IResultCapture(); + + virtual void assertionEnded( AssertionResult const& result ) = 0; + virtual bool sectionStarted( SectionInfo const& sectionInfo, + Counts& assertions ) = 0; + virtual void sectionEnded( SectionEndInfo const& endInfo ) = 0; + virtual void sectionEndedEarly( SectionEndInfo const& endInfo ) = 0; + virtual void pushScopedMessage( MessageInfo const& message ) = 0; + virtual void popScopedMessage( MessageInfo const& message ) = 0; + + virtual std::string getCurrentTestName() const = 0; + virtual const AssertionResult* getLastResult() const = 0; + + virtual void handleFatalErrorCondition( std::string const& message ) = 0; + }; + + IResultCapture& getResultCapture(); +} + +// #included from: catch_debugger.h +#define TWOBLUECUBES_CATCH_DEBUGGER_H_INCLUDED + +// #included from: catch_platform.h +#define TWOBLUECUBES_CATCH_PLATFORM_H_INCLUDED + +#if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) +# define CATCH_PLATFORM_MAC +#elif defined(__IPHONE_OS_VERSION_MIN_REQUIRED) +# define CATCH_PLATFORM_IPHONE +#elif defined(linux) || defined(__linux) || defined(__linux__) +# define CATCH_PLATFORM_LINUX +#elif defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) +# define CATCH_PLATFORM_WINDOWS +# if !defined(NOMINMAX) && !defined(CATCH_CONFIG_NO_NOMINMAX) +# define CATCH_DEFINES_NOMINMAX +# endif +# if !defined(WIN32_LEAN_AND_MEAN) && !defined(CATCH_CONFIG_NO_WIN32_LEAN_AND_MEAN) +# define CATCH_DEFINES_WIN32_LEAN_AND_MEAN +# endif +#endif + +#include <string> + +namespace Catch{ + + bool isDebuggerActive(); + void writeToDebugConsole( std::string const& text ); +} + +#ifdef CATCH_PLATFORM_MAC + + // The following code snippet based on: + // http://cocoawithlove.com/2008/03/break-into-debugger.html + #if defined(__ppc64__) || defined(__ppc__) + #define CATCH_TRAP() \ + __asm__("li r0, 20\nsc\nnop\nli r0, 37\nli r4, 2\nsc\nnop\n" \ + : : : "memory","r0","r3","r4" ) + #else + #define CATCH_TRAP() __asm__("int $3\n" : : ) + #endif + +#elif defined(CATCH_PLATFORM_LINUX) + // If we can use inline assembler, do it because this allows us to break + // directly at the location of the failing check instead of breaking inside + // raise() called from it, i.e. one stack frame below. + #if defined(__GNUC__) && (defined(__i386) || defined(__x86_64)) + #define CATCH_TRAP() asm volatile ("int $3") + #else // Fall back to the generic way. + #include <signal.h> + + #define CATCH_TRAP() raise(SIGTRAP) + #endif +#elif defined(_MSC_VER) + #define CATCH_TRAP() __debugbreak() +#elif defined(__MINGW32__) + extern "C" __declspec(dllimport) void __stdcall DebugBreak(); + #define CATCH_TRAP() DebugBreak() +#endif + +#ifdef CATCH_TRAP + #define CATCH_BREAK_INTO_DEBUGGER() if( Catch::isDebuggerActive() ) { CATCH_TRAP(); } +#else + #define CATCH_BREAK_INTO_DEBUGGER() Catch::alwaysTrue(); +#endif + +// #included from: catch_interfaces_runner.h +#define TWOBLUECUBES_CATCH_INTERFACES_RUNNER_H_INCLUDED + +namespace Catch { + class TestCase; + + struct IRunner { + virtual ~IRunner(); + virtual bool aborting() const = 0; + }; +} + +// #included from: catch_type_traits.hpp +#define TWOBLUECUBES_CATCH_TYPE_TRAITS_HPP_INCLUDED + +#if defined(CATCH_CONFIG_CPP11_TYPE_TRAITS) +#include <type_traits> +#endif + +namespace Catch { + +#if defined(CATCH_CONFIG_CPP11_TYPE_TRAITS) + + template <typename T> + using add_lvalue_reference = std::add_lvalue_reference<T>; + + template <typename T> + using add_const = std::add_const<T>; + +#else + + template <typename T> + struct add_const { + typedef const T type; + }; + + template <typename T> + struct add_lvalue_reference { + typedef T& type; + }; + template <typename T> + struct add_lvalue_reference<T&> { + typedef T& type; + }; + // No && overload, because that is C++11, in which case we have + // proper type_traits implementation from the standard library + +#endif + +} + +#if defined(CATCH_CONFIG_FAST_COMPILE) +/////////////////////////////////////////////////////////////////////////////// +// We can speedup compilation significantly by breaking into debugger lower in +// the callstack, because then we don't have to expand CATCH_BREAK_INTO_DEBUGGER +// macro in each assertion +#define INTERNAL_CATCH_REACT( resultBuilder ) \ + resultBuilder.react(); +#else +/////////////////////////////////////////////////////////////////////////////// +// In the event of a failure works out if the debugger needs to be invoked +// and/or an exception thrown and takes appropriate action. +// This needs to be done as a macro so the debugger will stop in the user +// source code rather than in Catch library code +#define INTERNAL_CATCH_REACT( resultBuilder ) \ + if( resultBuilder.shouldDebugBreak() ) CATCH_BREAK_INTO_DEBUGGER(); \ + resultBuilder.react(); +#endif + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_TEST( expr, resultDisposition, macroName ) \ + do { \ + Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, #expr, resultDisposition ); \ + try { \ + CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS \ + ( __catchResult <= expr ).endExpression(); \ + CATCH_INTERNAL_UNSUPPRESS_PARENTHESES_WARNINGS \ + } \ + catch( ... ) { \ + __catchResult.useActiveException( resultDisposition ); \ + } \ + INTERNAL_CATCH_REACT( __catchResult ) \ + } while( Catch::isTrue( false && static_cast<bool>( !!(expr) ) ) ) // expr here is never evaluated at runtime but it forces the compiler to give it a look + // The double negation silences MSVC's C4800 warning, the static_cast forces short-circuit evaluation if the type has overloaded &&. + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_IF( expr, resultDisposition, macroName ) \ + INTERNAL_CATCH_TEST( expr, resultDisposition, macroName ); \ + if( Catch::getResultCapture().getLastResult()->succeeded() ) + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_ELSE( expr, resultDisposition, macroName ) \ + INTERNAL_CATCH_TEST( expr, resultDisposition, macroName ); \ + if( !Catch::getResultCapture().getLastResult()->succeeded() ) + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_NO_THROW( expr, resultDisposition, macroName ) \ + do { \ + Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, #expr, resultDisposition ); \ + try { \ + static_cast<void>(expr); \ + __catchResult.captureResult( Catch::ResultWas::Ok ); \ + } \ + catch( ... ) { \ + __catchResult.useActiveException( resultDisposition ); \ + } \ + INTERNAL_CATCH_REACT( __catchResult ) \ + } while( Catch::alwaysFalse() ) + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_THROWS( expr, resultDisposition, matcher, macroName ) \ + do { \ + Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, #expr, resultDisposition, #matcher ); \ + if( __catchResult.allowThrows() ) \ + try { \ + static_cast<void>(expr); \ + __catchResult.captureResult( Catch::ResultWas::DidntThrowException ); \ + } \ + catch( ... ) { \ + __catchResult.captureExpectedException( matcher ); \ + } \ + else \ + __catchResult.captureResult( Catch::ResultWas::Ok ); \ + INTERNAL_CATCH_REACT( __catchResult ) \ + } while( Catch::alwaysFalse() ) + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_THROWS_AS( expr, exceptionType, resultDisposition, macroName ) \ + do { \ + Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, #expr ", " #exceptionType, resultDisposition ); \ + if( __catchResult.allowThrows() ) \ + try { \ + static_cast<void>(expr); \ + __catchResult.captureResult( Catch::ResultWas::DidntThrowException ); \ + } \ + catch( Catch::add_const<Catch::add_lvalue_reference<exceptionType>::type>::type ) { \ + __catchResult.captureResult( Catch::ResultWas::Ok ); \ + } \ + catch( ... ) { \ + __catchResult.useActiveException( resultDisposition ); \ + } \ + else \ + __catchResult.captureResult( Catch::ResultWas::Ok ); \ + INTERNAL_CATCH_REACT( __catchResult ) \ + } while( Catch::alwaysFalse() ) + +/////////////////////////////////////////////////////////////////////////////// +#ifdef CATCH_CONFIG_VARIADIC_MACROS + #define INTERNAL_CATCH_MSG( messageType, resultDisposition, macroName, ... ) \ + do { \ + Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, "", resultDisposition ); \ + __catchResult << __VA_ARGS__ + ::Catch::StreamEndStop(); \ + __catchResult.captureResult( messageType ); \ + INTERNAL_CATCH_REACT( __catchResult ) \ + } while( Catch::alwaysFalse() ) +#else + #define INTERNAL_CATCH_MSG( messageType, resultDisposition, macroName, log ) \ + do { \ + Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, "", resultDisposition ); \ + __catchResult << log + ::Catch::StreamEndStop(); \ + __catchResult.captureResult( messageType ); \ + INTERNAL_CATCH_REACT( __catchResult ) \ + } while( Catch::alwaysFalse() ) +#endif + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_INFO( log, macroName ) \ + Catch::ScopedMessage INTERNAL_CATCH_UNIQUE_NAME( scopedMessage ) = Catch::MessageBuilder( macroName, CATCH_INTERNAL_LINEINFO, Catch::ResultWas::Info ) << log; + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CHECK_THAT( arg, matcher, resultDisposition, macroName ) \ + do { \ + Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, #arg ", " #matcher, resultDisposition ); \ + try { \ + __catchResult.captureMatch( arg, matcher, #matcher ); \ + } catch( ... ) { \ + __catchResult.useActiveException( resultDisposition | Catch::ResultDisposition::ContinueOnFailure ); \ + } \ + INTERNAL_CATCH_REACT( __catchResult ) \ + } while( Catch::alwaysFalse() ) + +// #included from: internal/catch_section.h +#define TWOBLUECUBES_CATCH_SECTION_H_INCLUDED + +// #included from: catch_section_info.h +#define TWOBLUECUBES_CATCH_SECTION_INFO_H_INCLUDED + +// #included from: catch_totals.hpp +#define TWOBLUECUBES_CATCH_TOTALS_HPP_INCLUDED + +#include <cstddef> + +namespace Catch { + + struct Counts { + Counts() : passed( 0 ), failed( 0 ), failedButOk( 0 ) {} + + Counts operator - ( Counts const& other ) const { + Counts diff; + diff.passed = passed - other.passed; + diff.failed = failed - other.failed; + diff.failedButOk = failedButOk - other.failedButOk; + return diff; + } + Counts& operator += ( Counts const& other ) { + passed += other.passed; + failed += other.failed; + failedButOk += other.failedButOk; + return *this; + } + + std::size_t total() const { + return passed + failed + failedButOk; + } + bool allPassed() const { + return failed == 0 && failedButOk == 0; + } + bool allOk() const { + return failed == 0; + } + + std::size_t passed; + std::size_t failed; + std::size_t failedButOk; + }; + + struct Totals { + + Totals operator - ( Totals const& other ) const { + Totals diff; + diff.assertions = assertions - other.assertions; + diff.testCases = testCases - other.testCases; + return diff; + } + + Totals delta( Totals const& prevTotals ) const { + Totals diff = *this - prevTotals; + if( diff.assertions.failed > 0 ) + ++diff.testCases.failed; + else if( diff.assertions.failedButOk > 0 ) + ++diff.testCases.failedButOk; + else + ++diff.testCases.passed; + return diff; + } + + Totals& operator += ( Totals const& other ) { + assertions += other.assertions; + testCases += other.testCases; + return *this; + } + + Counts assertions; + Counts testCases; + }; +} + +#include <string> + +namespace Catch { + + struct SectionInfo { + SectionInfo + ( SourceLineInfo const& _lineInfo, + std::string const& _name, + std::string const& _description = std::string() ); + + std::string name; + std::string description; + SourceLineInfo lineInfo; + }; + + struct SectionEndInfo { + SectionEndInfo( SectionInfo const& _sectionInfo, Counts const& _prevAssertions, double _durationInSeconds ) + : sectionInfo( _sectionInfo ), prevAssertions( _prevAssertions ), durationInSeconds( _durationInSeconds ) + {} + + SectionInfo sectionInfo; + Counts prevAssertions; + double durationInSeconds; + }; + +} // end namespace Catch + +// #included from: catch_timer.h +#define TWOBLUECUBES_CATCH_TIMER_H_INCLUDED + +#ifdef CATCH_PLATFORM_WINDOWS +typedef unsigned long long uint64_t; +#else +#include <stdint.h> +#endif + +namespace Catch { + + class Timer { + public: + Timer() : m_ticks( 0 ) {} + void start(); + unsigned int getElapsedMicroseconds() const; + unsigned int getElapsedMilliseconds() const; + double getElapsedSeconds() const; + + private: + uint64_t m_ticks; + }; + +} // namespace Catch + +#include <string> + +namespace Catch { + + class Section : NonCopyable { + public: + Section( SectionInfo const& info ); + ~Section(); + + // This indicates whether the section should be executed or not + operator bool() const; + + private: + SectionInfo m_info; + + std::string m_name; + Counts m_assertions; + bool m_sectionIncluded; + Timer m_timer; + }; + +} // end namespace Catch + +#ifdef CATCH_CONFIG_VARIADIC_MACROS + #define INTERNAL_CATCH_SECTION( ... ) \ + if( Catch::Section const& INTERNAL_CATCH_UNIQUE_NAME( catch_internal_Section ) = Catch::SectionInfo( CATCH_INTERNAL_LINEINFO, __VA_ARGS__ ) ) +#else + #define INTERNAL_CATCH_SECTION( name, desc ) \ + if( Catch::Section const& INTERNAL_CATCH_UNIQUE_NAME( catch_internal_Section ) = Catch::SectionInfo( CATCH_INTERNAL_LINEINFO, name, desc ) ) +#endif + +// #included from: internal/catch_generators.hpp +#define TWOBLUECUBES_CATCH_GENERATORS_HPP_INCLUDED + +#include <iterator> +#include <vector> +#include <string> +#include <stdlib.h> + +namespace Catch { + +template<typename T> +struct IGenerator { + virtual ~IGenerator() {} + virtual T getValue( std::size_t index ) const = 0; + virtual std::size_t size () const = 0; +}; + +template<typename T> +class BetweenGenerator : public IGenerator<T> { +public: + BetweenGenerator( T from, T to ) : m_from( from ), m_to( to ){} + + virtual T getValue( std::size_t index ) const { + return m_from+static_cast<int>( index ); + } + + virtual std::size_t size() const { + return static_cast<std::size_t>( 1+m_to-m_from ); + } + +private: + + T m_from; + T m_to; +}; + +template<typename T> +class ValuesGenerator : public IGenerator<T> { +public: + ValuesGenerator(){} + + void add( T value ) { + m_values.push_back( value ); + } + + virtual T getValue( std::size_t index ) const { + return m_values[index]; + } + + virtual std::size_t size() const { + return m_values.size(); + } + +private: + std::vector<T> m_values; +}; + +template<typename T> +class CompositeGenerator { +public: + CompositeGenerator() : m_totalSize( 0 ) {} + + // *** Move semantics, similar to auto_ptr *** + CompositeGenerator( CompositeGenerator& other ) + : m_fileInfo( other.m_fileInfo ), + m_totalSize( 0 ) + { + move( other ); + } + + CompositeGenerator& setFileInfo( const char* fileInfo ) { + m_fileInfo = fileInfo; + return *this; + } + + ~CompositeGenerator() { + deleteAll( m_composed ); + } + + operator T () const { + size_t overallIndex = getCurrentContext().getGeneratorIndex( m_fileInfo, m_totalSize ); + + typename std::vector<const IGenerator<T>*>::const_iterator it = m_composed.begin(); + typename std::vector<const IGenerator<T>*>::const_iterator itEnd = m_composed.end(); + for( size_t index = 0; it != itEnd; ++it ) + { + const IGenerator<T>* generator = *it; + if( overallIndex >= index && overallIndex < index + generator->size() ) + { + return generator->getValue( overallIndex-index ); + } + index += generator->size(); + } + CATCH_INTERNAL_ERROR( "Indexed past end of generated range" ); + return T(); // Suppress spurious "not all control paths return a value" warning in Visual Studio - if you know how to fix this please do so + } + + void add( const IGenerator<T>* generator ) { + m_totalSize += generator->size(); + m_composed.push_back( generator ); + } + + CompositeGenerator& then( CompositeGenerator& other ) { + move( other ); + return *this; + } + + CompositeGenerator& then( T value ) { + ValuesGenerator<T>* valuesGen = new ValuesGenerator<T>(); + valuesGen->add( value ); + add( valuesGen ); + return *this; + } + +private: + + void move( CompositeGenerator& other ) { + std::copy( other.m_composed.begin(), other.m_composed.end(), std::back_inserter( m_composed ) ); + m_totalSize += other.m_totalSize; + other.m_composed.clear(); + } + + std::vector<const IGenerator<T>*> m_composed; + std::string m_fileInfo; + size_t m_totalSize; +}; + +namespace Generators +{ + template<typename T> + CompositeGenerator<T> between( T from, T to ) { + CompositeGenerator<T> generators; + generators.add( new BetweenGenerator<T>( from, to ) ); + return generators; + } + + template<typename T> + CompositeGenerator<T> values( T val1, T val2 ) { + CompositeGenerator<T> generators; + ValuesGenerator<T>* valuesGen = new ValuesGenerator<T>(); + valuesGen->add( val1 ); + valuesGen->add( val2 ); + generators.add( valuesGen ); + return generators; + } + + template<typename T> + CompositeGenerator<T> values( T val1, T val2, T val3 ){ + CompositeGenerator<T> generators; + ValuesGenerator<T>* valuesGen = new ValuesGenerator<T>(); + valuesGen->add( val1 ); + valuesGen->add( val2 ); + valuesGen->add( val3 ); + generators.add( valuesGen ); + return generators; + } + + template<typename T> + CompositeGenerator<T> values( T val1, T val2, T val3, T val4 ) { + CompositeGenerator<T> generators; + ValuesGenerator<T>* valuesGen = new ValuesGenerator<T>(); + valuesGen->add( val1 ); + valuesGen->add( val2 ); + valuesGen->add( val3 ); + valuesGen->add( val4 ); + generators.add( valuesGen ); + return generators; + } + +} // end namespace Generators + +using namespace Generators; + +} // end namespace Catch + +#define INTERNAL_CATCH_LINESTR2( line ) #line +#define INTERNAL_CATCH_LINESTR( line ) INTERNAL_CATCH_LINESTR2( line ) + +#define INTERNAL_CATCH_GENERATE( expr ) expr.setFileInfo( __FILE__ "(" INTERNAL_CATCH_LINESTR( __LINE__ ) ")" ) + +// #included from: internal/catch_interfaces_exception.h +#define TWOBLUECUBES_CATCH_INTERFACES_EXCEPTION_H_INCLUDED + +#include <string> +#include <vector> + +// #included from: catch_interfaces_registry_hub.h +#define TWOBLUECUBES_CATCH_INTERFACES_REGISTRY_HUB_H_INCLUDED + +#include <string> + +namespace Catch { + + class TestCase; + struct ITestCaseRegistry; + struct IExceptionTranslatorRegistry; + struct IExceptionTranslator; + struct IReporterRegistry; + struct IReporterFactory; + + struct IRegistryHub { + virtual ~IRegistryHub(); + + virtual IReporterRegistry const& getReporterRegistry() const = 0; + virtual ITestCaseRegistry const& getTestCaseRegistry() const = 0; + virtual IExceptionTranslatorRegistry& getExceptionTranslatorRegistry() = 0; + }; + + struct IMutableRegistryHub { + virtual ~IMutableRegistryHub(); + virtual void registerReporter( std::string const& name, Ptr<IReporterFactory> const& factory ) = 0; + virtual void registerListener( Ptr<IReporterFactory> const& factory ) = 0; + virtual void registerTest( TestCase const& testInfo ) = 0; + virtual void registerTranslator( const IExceptionTranslator* translator ) = 0; + }; + + IRegistryHub& getRegistryHub(); + IMutableRegistryHub& getMutableRegistryHub(); + void cleanUp(); + std::string translateActiveException(); + +} + +namespace Catch { + + typedef std::string(*exceptionTranslateFunction)(); + + struct IExceptionTranslator; + typedef std::vector<const IExceptionTranslator*> ExceptionTranslators; + + struct IExceptionTranslator { + virtual ~IExceptionTranslator(); + virtual std::string translate( ExceptionTranslators::const_iterator it, ExceptionTranslators::const_iterator itEnd ) const = 0; + }; + + struct IExceptionTranslatorRegistry { + virtual ~IExceptionTranslatorRegistry(); + + virtual std::string translateActiveException() const = 0; + }; + + class ExceptionTranslatorRegistrar { + template<typename T> + class ExceptionTranslator : public IExceptionTranslator { + public: + + ExceptionTranslator( std::string(*translateFunction)( T& ) ) + : m_translateFunction( translateFunction ) + {} + + virtual std::string translate( ExceptionTranslators::const_iterator it, ExceptionTranslators::const_iterator itEnd ) const CATCH_OVERRIDE { + try { + if( it == itEnd ) + throw; + else + return (*it)->translate( it+1, itEnd ); + } + catch( T& ex ) { + return m_translateFunction( ex ); + } + } + + protected: + std::string(*m_translateFunction)( T& ); + }; + + public: + template<typename T> + ExceptionTranslatorRegistrar( std::string(*translateFunction)( T& ) ) { + getMutableRegistryHub().registerTranslator + ( new ExceptionTranslator<T>( translateFunction ) ); + } + }; +} + +/////////////////////////////////////////////////////////////////////////////// +#define INTERNAL_CATCH_TRANSLATE_EXCEPTION2( translatorName, signature ) \ + static std::string translatorName( signature ); \ + namespace{ Catch::ExceptionTranslatorRegistrar INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionRegistrar )( &translatorName ); }\ + static std::string translatorName( signature ) + +#define INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION2( INTERNAL_CATCH_UNIQUE_NAME( catch_internal_ExceptionTranslator ), signature ) + +// #included from: internal/catch_approx.hpp +#define TWOBLUECUBES_CATCH_APPROX_HPP_INCLUDED + +#include <cmath> +#include <limits> + +#if defined(CATCH_CONFIG_CPP11_TYPE_TRAITS) +#include <type_traits> +#endif + +namespace Catch { +namespace Detail { + + class Approx { + public: + explicit Approx ( double value ) + : m_epsilon( std::numeric_limits<float>::epsilon()*100 ), + m_margin( 0.0 ), + m_scale( 1.0 ), + m_value( value ) + {} + + Approx( Approx const& other ) + : m_epsilon( other.m_epsilon ), + m_margin( other.m_margin ), + m_scale( other.m_scale ), + m_value( other.m_value ) + {} + + static Approx custom() { + return Approx( 0 ); + } + + Approx operator()( double value ) { + Approx approx( value ); + approx.epsilon( m_epsilon ); + approx.margin( m_margin ); + approx.scale( m_scale ); + return approx; + } + +#if defined(CATCH_CONFIG_CPP11_TYPE_TRAITS) + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + friend bool operator == ( const T& lhs, Approx const& rhs ) { + // Thanks to Richard Harris for his help refining this formula + auto lhs_v = double(lhs); + bool relativeOK = std::fabs(lhs_v - rhs.m_value) < rhs.m_epsilon * (rhs.m_scale + (std::max)(std::fabs(lhs_v), std::fabs(rhs.m_value))); + if (relativeOK) { + return true; + } + return std::fabs(lhs_v - rhs.m_value) < rhs.m_margin; + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + friend bool operator == ( Approx const& lhs, const T& rhs ) { + return operator==( rhs, lhs ); + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + friend bool operator != ( T lhs, Approx const& rhs ) { + return !operator==( lhs, rhs ); + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + friend bool operator != ( Approx const& lhs, T rhs ) { + return !operator==( rhs, lhs ); + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + friend bool operator <= ( T lhs, Approx const& rhs ) + { + return double(lhs) < rhs.m_value || lhs == rhs; + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + friend bool operator <= ( Approx const& lhs, T rhs ) + { + return lhs.m_value < double(rhs) || lhs == rhs; + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + friend bool operator >= ( T lhs, Approx const& rhs ) + { + return double(lhs) > rhs.m_value || lhs == rhs; + } + + template <typename T, typename = typename std::enable_if<std::is_constructible<double, T>::value>::type> + friend bool operator >= ( Approx const& lhs, T rhs ) + { + return lhs.m_value > double(rhs) || lhs == rhs; + } +#else + friend bool operator == ( double lhs, Approx const& rhs ) { + // Thanks to Richard Harris for his help refining this formula + bool relativeOK = std::fabs( lhs - rhs.m_value ) < rhs.m_epsilon * (rhs.m_scale + (std::max)( std::fabs(lhs), std::fabs(rhs.m_value) ) ); + if (relativeOK) { + return true; + } + return std::fabs(lhs - rhs.m_value) < rhs.m_margin; + } + + friend bool operator == ( Approx const& lhs, double rhs ) { + return operator==( rhs, lhs ); + } + + friend bool operator != ( double lhs, Approx const& rhs ) { + return !operator==( lhs, rhs ); + } + + friend bool operator != ( Approx const& lhs, double rhs ) { + return !operator==( rhs, lhs ); + } + + friend bool operator <= ( double lhs, Approx const& rhs ) + { + return lhs < rhs.m_value || lhs == rhs; + } + + friend bool operator <= ( Approx const& lhs, double rhs ) + { + return lhs.m_value < rhs || lhs == rhs; + } + + friend bool operator >= ( double lhs, Approx const& rhs ) + { + return lhs > rhs.m_value || lhs == rhs; + } + + friend bool operator >= ( Approx const& lhs, double rhs ) + { + return lhs.m_value > rhs || lhs == rhs; + } +#endif + + Approx& epsilon( double newEpsilon ) { + m_epsilon = newEpsilon; + return *this; + } + + Approx& margin( double newMargin ) { + m_margin = newMargin; + return *this; + } + + Approx& scale( double newScale ) { + m_scale = newScale; + return *this; + } + + std::string toString() const { + std::ostringstream oss; + oss << "Approx( " << Catch::toString( m_value ) << " )"; + return oss.str(); + } + + private: + double m_epsilon; + double m_margin; + double m_scale; + double m_value; + }; +} + +template<> +inline std::string toString<Detail::Approx>( Detail::Approx const& value ) { + return value.toString(); +} + +} // end namespace Catch + +// #included from: internal/catch_matchers_string.h +#define TWOBLUECUBES_CATCH_MATCHERS_STRING_H_INCLUDED + +namespace Catch { +namespace Matchers { + + namespace StdString { + + struct CasedString + { + CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity ); + std::string adjustString( std::string const& str ) const; + std::string caseSensitivitySuffix() const; + + CaseSensitive::Choice m_caseSensitivity; + std::string m_str; + }; + + struct StringMatcherBase : MatcherBase<std::string> { + StringMatcherBase( std::string operation, CasedString const& comparator ); + virtual std::string describe() const CATCH_OVERRIDE; + + CasedString m_comparator; + std::string m_operation; + }; + + struct EqualsMatcher : StringMatcherBase { + EqualsMatcher( CasedString const& comparator ); + virtual bool match( std::string const& source ) const CATCH_OVERRIDE; + }; + struct ContainsMatcher : StringMatcherBase { + ContainsMatcher( CasedString const& comparator ); + virtual bool match( std::string const& source ) const CATCH_OVERRIDE; + }; + struct StartsWithMatcher : StringMatcherBase { + StartsWithMatcher( CasedString const& comparator ); + virtual bool match( std::string const& source ) const CATCH_OVERRIDE; + }; + struct EndsWithMatcher : StringMatcherBase { + EndsWithMatcher( CasedString const& comparator ); + virtual bool match( std::string const& source ) const CATCH_OVERRIDE; + }; + + } // namespace StdString + + // The following functions create the actual matcher objects. + // This allows the types to be inferred + + StdString::EqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); + StdString::ContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); + StdString::EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); + StdString::StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ); + +} // namespace Matchers +} // namespace Catch + +// #included from: internal/catch_matchers_vector.h +#define TWOBLUECUBES_CATCH_MATCHERS_VECTOR_H_INCLUDED + +namespace Catch { +namespace Matchers { + + namespace Vector { + + template<typename T> + struct ContainsElementMatcher : MatcherBase<std::vector<T>, T> { + + ContainsElementMatcher(T const &comparator) : m_comparator( comparator) {} + + bool match(std::vector<T> const &v) const CATCH_OVERRIDE { + return std::find(v.begin(), v.end(), m_comparator) != v.end(); + } + + virtual std::string describe() const CATCH_OVERRIDE { + return "Contains: " + Catch::toString( m_comparator ); + } + + T const& m_comparator; + }; + + template<typename T> + struct ContainsMatcher : MatcherBase<std::vector<T>, std::vector<T> > { + + ContainsMatcher(std::vector<T> const &comparator) : m_comparator( comparator ) {} + + bool match(std::vector<T> const &v) const CATCH_OVERRIDE { + // !TBD: see note in EqualsMatcher + if (m_comparator.size() > v.size()) + return false; + for (size_t i = 0; i < m_comparator.size(); ++i) + if (std::find(v.begin(), v.end(), m_comparator[i]) == v.end()) + return false; + return true; + } + virtual std::string describe() const CATCH_OVERRIDE { + return "Contains: " + Catch::toString( m_comparator ); + } + + std::vector<T> const& m_comparator; + }; + + template<typename T> + struct EqualsMatcher : MatcherBase<std::vector<T>, std::vector<T> > { + + EqualsMatcher(std::vector<T> const &comparator) : m_comparator( comparator ) {} + + bool match(std::vector<T> const &v) const CATCH_OVERRIDE { + // !TBD: This currently works if all elements can be compared using != + // - a more general approach would be via a compare template that defaults + // to using !=. but could be specialised for, e.g. std::vector<T> etc + // - then just call that directly + if (m_comparator.size() != v.size()) + return false; + for (size_t i = 0; i < v.size(); ++i) + if (m_comparator[i] != v[i]) + return false; + return true; + } + virtual std::string describe() const CATCH_OVERRIDE { + return "Equals: " + Catch::toString( m_comparator ); + } + std::vector<T> const& m_comparator; + }; + + } // namespace Vector + + // The following functions create the actual matcher objects. + // This allows the types to be inferred + + template<typename T> + Vector::ContainsMatcher<T> Contains( std::vector<T> const& comparator ) { + return Vector::ContainsMatcher<T>( comparator ); + } + + template<typename T> + Vector::ContainsElementMatcher<T> VectorContains( T const& comparator ) { + return Vector::ContainsElementMatcher<T>( comparator ); + } + + template<typename T> + Vector::EqualsMatcher<T> Equals( std::vector<T> const& comparator ) { + return Vector::EqualsMatcher<T>( comparator ); + } + +} // namespace Matchers +} // namespace Catch + +// #included from: internal/catch_interfaces_tag_alias_registry.h +#define TWOBLUECUBES_CATCH_INTERFACES_TAG_ALIAS_REGISTRY_H_INCLUDED + +// #included from: catch_tag_alias.h +#define TWOBLUECUBES_CATCH_TAG_ALIAS_H_INCLUDED + +#include <string> + +namespace Catch { + + struct TagAlias { + TagAlias( std::string _tag, SourceLineInfo _lineInfo ) : tag( _tag ), lineInfo( _lineInfo ) {} + + std::string tag; + SourceLineInfo lineInfo; + }; + + struct RegistrarForTagAliases { + RegistrarForTagAliases( char const* alias, char const* tag, SourceLineInfo const& lineInfo ); + }; + +} // end namespace Catch + +#define CATCH_REGISTER_TAG_ALIAS( alias, spec ) namespace{ Catch::RegistrarForTagAliases INTERNAL_CATCH_UNIQUE_NAME( AutoRegisterTagAlias )( alias, spec, CATCH_INTERNAL_LINEINFO ); } +// #included from: catch_option.hpp +#define TWOBLUECUBES_CATCH_OPTION_HPP_INCLUDED + +namespace Catch { + + // An optional type + template<typename T> + class Option { + public: + Option() : nullableValue( CATCH_NULL ) {} + Option( T const& _value ) + : nullableValue( new( storage ) T( _value ) ) + {} + Option( Option const& _other ) + : nullableValue( _other ? new( storage ) T( *_other ) : CATCH_NULL ) + {} + + ~Option() { + reset(); + } + + Option& operator= ( Option const& _other ) { + if( &_other != this ) { + reset(); + if( _other ) + nullableValue = new( storage ) T( *_other ); + } + return *this; + } + Option& operator = ( T const& _value ) { + reset(); + nullableValue = new( storage ) T( _value ); + return *this; + } + + void reset() { + if( nullableValue ) + nullableValue->~T(); + nullableValue = CATCH_NULL; + } + + T& operator*() { return *nullableValue; } + T const& operator*() const { return *nullableValue; } + T* operator->() { return nullableValue; } + const T* operator->() const { return nullableValue; } + + T valueOr( T const& defaultValue ) const { + return nullableValue ? *nullableValue : defaultValue; + } + + bool some() const { return nullableValue != CATCH_NULL; } + bool none() const { return nullableValue == CATCH_NULL; } + + bool operator !() const { return nullableValue == CATCH_NULL; } + operator SafeBool::type() const { + return SafeBool::makeSafe( some() ); + } + + private: + T* nullableValue; + char storage[sizeof(T)]; + }; + +} // end namespace Catch + +namespace Catch { + + struct ITagAliasRegistry { + virtual ~ITagAliasRegistry(); + virtual Option<TagAlias> find( std::string const& alias ) const = 0; + virtual std::string expandAliases( std::string const& unexpandedTestSpec ) const = 0; + + static ITagAliasRegistry const& get(); + }; + +} // end namespace Catch + +// These files are included here so the single_include script doesn't put them +// in the conditionally compiled sections +// #included from: internal/catch_test_case_info.h +#define TWOBLUECUBES_CATCH_TEST_CASE_INFO_H_INCLUDED + +#include <string> +#include <set> + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" +#endif + +namespace Catch { + + struct ITestCase; + + struct TestCaseInfo { + enum SpecialProperties{ + None = 0, + IsHidden = 1 << 1, + ShouldFail = 1 << 2, + MayFail = 1 << 3, + Throws = 1 << 4, + NonPortable = 1 << 5 + }; + + TestCaseInfo( std::string const& _name, + std::string const& _className, + std::string const& _description, + std::set<std::string> const& _tags, + SourceLineInfo const& _lineInfo ); + + TestCaseInfo( TestCaseInfo const& other ); + + friend void setTags( TestCaseInfo& testCaseInfo, std::set<std::string> const& tags ); + + bool isHidden() const; + bool throws() const; + bool okToFail() const; + bool expectedToFail() const; + + std::string name; + std::string className; + std::string description; + std::set<std::string> tags; + std::set<std::string> lcaseTags; + std::string tagsAsString; + SourceLineInfo lineInfo; + SpecialProperties properties; + }; + + class TestCase : public TestCaseInfo { + public: + + TestCase( ITestCase* testCase, TestCaseInfo const& info ); + TestCase( TestCase const& other ); + + TestCase withName( std::string const& _newName ) const; + + void invoke() const; + + TestCaseInfo const& getTestCaseInfo() const; + + void swap( TestCase& other ); + bool operator == ( TestCase const& other ) const; + bool operator < ( TestCase const& other ) const; + TestCase& operator = ( TestCase const& other ); + + private: + Ptr<ITestCase> test; + }; + + TestCase makeTestCase( ITestCase* testCase, + std::string const& className, + std::string const& name, + std::string const& description, + SourceLineInfo const& lineInfo ); +} + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + + +#ifdef __OBJC__ +// #included from: internal/catch_objc.hpp +#define TWOBLUECUBES_CATCH_OBJC_HPP_INCLUDED + +#import <objc/runtime.h> + +#include <string> + +// NB. Any general catch headers included here must be included +// in catch.hpp first to make sure they are included by the single +// header for non obj-usage + +/////////////////////////////////////////////////////////////////////////////// +// This protocol is really only here for (self) documenting purposes, since +// all its methods are optional. +@protocol OcFixture + +@optional + +-(void) setUp; +-(void) tearDown; + +@end + +namespace Catch { + + class OcMethod : public SharedImpl<ITestCase> { + + public: + OcMethod( Class cls, SEL sel ) : m_cls( cls ), m_sel( sel ) {} + + virtual void invoke() const { + id obj = [[m_cls alloc] init]; + + performOptionalSelector( obj, @selector(setUp) ); + performOptionalSelector( obj, m_sel ); + performOptionalSelector( obj, @selector(tearDown) ); + + arcSafeRelease( obj ); + } + private: + virtual ~OcMethod() {} + + Class m_cls; + SEL m_sel; + }; + + namespace Detail{ + + inline std::string getAnnotation( Class cls, + std::string const& annotationName, + std::string const& testCaseName ) { + NSString* selStr = [[NSString alloc] initWithFormat:@"Catch_%s_%s", annotationName.c_str(), testCaseName.c_str()]; + SEL sel = NSSelectorFromString( selStr ); + arcSafeRelease( selStr ); + id value = performOptionalSelector( cls, sel ); + if( value ) + return [(NSString*)value UTF8String]; + return ""; + } + } + + inline size_t registerTestMethods() { + size_t noTestMethods = 0; + int noClasses = objc_getClassList( CATCH_NULL, 0 ); + + Class* classes = (CATCH_UNSAFE_UNRETAINED Class *)malloc( sizeof(Class) * noClasses); + objc_getClassList( classes, noClasses ); + + for( int c = 0; c < noClasses; c++ ) { + Class cls = classes[c]; + { + u_int count; + Method* methods = class_copyMethodList( cls, &count ); + for( u_int m = 0; m < count ; m++ ) { + SEL selector = method_getName(methods[m]); + std::string methodName = sel_getName(selector); + if( startsWith( methodName, "Catch_TestCase_" ) ) { + std::string testCaseName = methodName.substr( 15 ); + std::string name = Detail::getAnnotation( cls, "Name", testCaseName ); + std::string desc = Detail::getAnnotation( cls, "Description", testCaseName ); + const char* className = class_getName( cls ); + + getMutableRegistryHub().registerTest( makeTestCase( new OcMethod( cls, selector ), className, name.c_str(), desc.c_str(), SourceLineInfo() ) ); + noTestMethods++; + } + } + free(methods); + } + } + return noTestMethods; + } + + namespace Matchers { + namespace Impl { + namespace NSStringMatchers { + + template<typename MatcherT> + struct StringHolder : MatcherImpl<MatcherT, NSString*>{ + StringHolder( NSString* substr ) : m_substr( [substr copy] ){} + StringHolder( StringHolder const& other ) : m_substr( [other.m_substr copy] ){} + StringHolder() { + arcSafeRelease( m_substr ); + } + + NSString* m_substr; + }; + + struct Equals : StringHolder<Equals> { + Equals( NSString* substr ) : StringHolder( substr ){} + + virtual bool match( ExpressionType const& str ) const { + return (str != nil || m_substr == nil ) && + [str isEqualToString:m_substr]; + } + + virtual std::string toString() const { + return "equals string: " + Catch::toString( m_substr ); + } + }; + + struct Contains : StringHolder<Contains> { + Contains( NSString* substr ) : StringHolder( substr ){} + + virtual bool match( ExpressionType const& str ) const { + return (str != nil || m_substr == nil ) && + [str rangeOfString:m_substr].location != NSNotFound; + } + + virtual std::string toString() const { + return "contains string: " + Catch::toString( m_substr ); + } + }; + + struct StartsWith : StringHolder<StartsWith> { + StartsWith( NSString* substr ) : StringHolder( substr ){} + + virtual bool match( ExpressionType const& str ) const { + return (str != nil || m_substr == nil ) && + [str rangeOfString:m_substr].location == 0; + } + + virtual std::string toString() const { + return "starts with: " + Catch::toString( m_substr ); + } + }; + struct EndsWith : StringHolder<EndsWith> { + EndsWith( NSString* substr ) : StringHolder( substr ){} + + virtual bool match( ExpressionType const& str ) const { + return (str != nil || m_substr == nil ) && + [str rangeOfString:m_substr].location == [str length] - [m_substr length]; + } + + virtual std::string toString() const { + return "ends with: " + Catch::toString( m_substr ); + } + }; + + } // namespace NSStringMatchers + } // namespace Impl + + inline Impl::NSStringMatchers::Equals + Equals( NSString* substr ){ return Impl::NSStringMatchers::Equals( substr ); } + + inline Impl::NSStringMatchers::Contains + Contains( NSString* substr ){ return Impl::NSStringMatchers::Contains( substr ); } + + inline Impl::NSStringMatchers::StartsWith + StartsWith( NSString* substr ){ return Impl::NSStringMatchers::StartsWith( substr ); } + + inline Impl::NSStringMatchers::EndsWith + EndsWith( NSString* substr ){ return Impl::NSStringMatchers::EndsWith( substr ); } + + } // namespace Matchers + + using namespace Matchers; + +} // namespace Catch + +/////////////////////////////////////////////////////////////////////////////// +#define OC_TEST_CASE( name, desc )\ ++(NSString*) INTERNAL_CATCH_UNIQUE_NAME( Catch_Name_test ) \ +{\ +return @ name; \ +}\ ++(NSString*) INTERNAL_CATCH_UNIQUE_NAME( Catch_Description_test ) \ +{ \ +return @ desc; \ +} \ +-(void) INTERNAL_CATCH_UNIQUE_NAME( Catch_TestCase_test ) + +#endif + +#ifdef CATCH_IMPL + +// !TBD: Move the leak detector code into a separate header +#ifdef CATCH_CONFIG_WINDOWS_CRTDBG +#include <crtdbg.h> +class LeakDetector { +public: + LeakDetector() { + int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); + flag |= _CRTDBG_LEAK_CHECK_DF; + flag |= _CRTDBG_ALLOC_MEM_DF; + _CrtSetDbgFlag(flag); + _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG); + _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR); + // Change this to leaking allocation's number to break there + _CrtSetBreakAlloc(-1); + } +}; +#else +class LeakDetector {}; +#endif + +LeakDetector leakDetector; + +// #included from: internal/catch_impl.hpp +#define TWOBLUECUBES_CATCH_IMPL_HPP_INCLUDED + +// Collect all the implementation files together here +// These are the equivalent of what would usually be cpp files + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wweak-vtables" +#endif + +// #included from: ../catch_session.hpp +#define TWOBLUECUBES_CATCH_RUNNER_HPP_INCLUDED + +// #included from: internal/catch_commandline.hpp +#define TWOBLUECUBES_CATCH_COMMANDLINE_HPP_INCLUDED + +// #included from: catch_config.hpp +#define TWOBLUECUBES_CATCH_CONFIG_HPP_INCLUDED + +// #included from: catch_test_spec_parser.hpp +#define TWOBLUECUBES_CATCH_TEST_SPEC_PARSER_HPP_INCLUDED + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" +#endif + +// #included from: catch_test_spec.hpp +#define TWOBLUECUBES_CATCH_TEST_SPEC_HPP_INCLUDED + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" +#endif + +// #included from: catch_wildcard_pattern.hpp +#define TWOBLUECUBES_CATCH_WILDCARD_PATTERN_HPP_INCLUDED + +#include <stdexcept> + +namespace Catch +{ + class WildcardPattern { + enum WildcardPosition { + NoWildcard = 0, + WildcardAtStart = 1, + WildcardAtEnd = 2, + WildcardAtBothEnds = WildcardAtStart | WildcardAtEnd + }; + + public: + + WildcardPattern( std::string const& pattern, CaseSensitive::Choice caseSensitivity ) + : m_caseSensitivity( caseSensitivity ), + m_wildcard( NoWildcard ), + m_pattern( adjustCase( pattern ) ) + { + if( startsWith( m_pattern, '*' ) ) { + m_pattern = m_pattern.substr( 1 ); + m_wildcard = WildcardAtStart; + } + if( endsWith( m_pattern, '*' ) ) { + m_pattern = m_pattern.substr( 0, m_pattern.size()-1 ); + m_wildcard = static_cast<WildcardPosition>( m_wildcard | WildcardAtEnd ); + } + } + virtual ~WildcardPattern(); + virtual bool matches( std::string const& str ) const { + switch( m_wildcard ) { + case NoWildcard: + return m_pattern == adjustCase( str ); + case WildcardAtStart: + return endsWith( adjustCase( str ), m_pattern ); + case WildcardAtEnd: + return startsWith( adjustCase( str ), m_pattern ); + case WildcardAtBothEnds: + return contains( adjustCase( str ), m_pattern ); + } + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunreachable-code" +#endif + throw std::logic_error( "Unknown enum" ); +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + } + private: + std::string adjustCase( std::string const& str ) const { + return m_caseSensitivity == CaseSensitive::No ? toLower( str ) : str; + } + CaseSensitive::Choice m_caseSensitivity; + WildcardPosition m_wildcard; + std::string m_pattern; + }; +} + +#include <string> +#include <vector> + +namespace Catch { + + class TestSpec { + struct Pattern : SharedImpl<> { + virtual ~Pattern(); + virtual bool matches( TestCaseInfo const& testCase ) const = 0; + }; + class NamePattern : public Pattern { + public: + NamePattern( std::string const& name ) + : m_wildcardPattern( toLower( name ), CaseSensitive::No ) + {} + virtual ~NamePattern(); + virtual bool matches( TestCaseInfo const& testCase ) const { + return m_wildcardPattern.matches( toLower( testCase.name ) ); + } + private: + WildcardPattern m_wildcardPattern; + }; + + class TagPattern : public Pattern { + public: + TagPattern( std::string const& tag ) : m_tag( toLower( tag ) ) {} + virtual ~TagPattern(); + virtual bool matches( TestCaseInfo const& testCase ) const { + return testCase.lcaseTags.find( m_tag ) != testCase.lcaseTags.end(); + } + private: + std::string m_tag; + }; + + class ExcludedPattern : public Pattern { + public: + ExcludedPattern( Ptr<Pattern> const& underlyingPattern ) : m_underlyingPattern( underlyingPattern ) {} + virtual ~ExcludedPattern(); + virtual bool matches( TestCaseInfo const& testCase ) const { return !m_underlyingPattern->matches( testCase ); } + private: + Ptr<Pattern> m_underlyingPattern; + }; + + struct Filter { + std::vector<Ptr<Pattern> > m_patterns; + + bool matches( TestCaseInfo const& testCase ) const { + // All patterns in a filter must match for the filter to be a match + for( std::vector<Ptr<Pattern> >::const_iterator it = m_patterns.begin(), itEnd = m_patterns.end(); it != itEnd; ++it ) { + if( !(*it)->matches( testCase ) ) + return false; + } + return true; + } + }; + + public: + bool hasFilters() const { + return !m_filters.empty(); + } + bool matches( TestCaseInfo const& testCase ) const { + // A TestSpec matches if any filter matches + for( std::vector<Filter>::const_iterator it = m_filters.begin(), itEnd = m_filters.end(); it != itEnd; ++it ) + if( it->matches( testCase ) ) + return true; + return false; + } + + private: + std::vector<Filter> m_filters; + + friend class TestSpecParser; + }; +} + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +namespace Catch { + + class TestSpecParser { + enum Mode{ None, Name, QuotedName, Tag, EscapedName }; + Mode m_mode; + bool m_exclusion; + std::size_t m_start, m_pos; + std::string m_arg; + std::vector<std::size_t> m_escapeChars; + TestSpec::Filter m_currentFilter; + TestSpec m_testSpec; + ITagAliasRegistry const* m_tagAliases; + + public: + TestSpecParser( ITagAliasRegistry const& tagAliases ) : m_tagAliases( &tagAliases ) {} + + TestSpecParser& parse( std::string const& arg ) { + m_mode = None; + m_exclusion = false; + m_start = std::string::npos; + m_arg = m_tagAliases->expandAliases( arg ); + m_escapeChars.clear(); + for( m_pos = 0; m_pos < m_arg.size(); ++m_pos ) + visitChar( m_arg[m_pos] ); + if( m_mode == Name ) + addPattern<TestSpec::NamePattern>(); + return *this; + } + TestSpec testSpec() { + addFilter(); + return m_testSpec; + } + private: + void visitChar( char c ) { + if( m_mode == None ) { + switch( c ) { + case ' ': return; + case '~': m_exclusion = true; return; + case '[': return startNewMode( Tag, ++m_pos ); + case '"': return startNewMode( QuotedName, ++m_pos ); + case '\\': return escape(); + default: startNewMode( Name, m_pos ); break; + } + } + if( m_mode == Name ) { + if( c == ',' ) { + addPattern<TestSpec::NamePattern>(); + addFilter(); + } + else if( c == '[' ) { + if( subString() == "exclude:" ) + m_exclusion = true; + else + addPattern<TestSpec::NamePattern>(); + startNewMode( Tag, ++m_pos ); + } + else if( c == '\\' ) + escape(); + } + else if( m_mode == EscapedName ) + m_mode = Name; + else if( m_mode == QuotedName && c == '"' ) + addPattern<TestSpec::NamePattern>(); + else if( m_mode == Tag && c == ']' ) + addPattern<TestSpec::TagPattern>(); + } + void startNewMode( Mode mode, std::size_t start ) { + m_mode = mode; + m_start = start; + } + void escape() { + if( m_mode == None ) + m_start = m_pos; + m_mode = EscapedName; + m_escapeChars.push_back( m_pos ); + } + std::string subString() const { return m_arg.substr( m_start, m_pos - m_start ); } + template<typename T> + void addPattern() { + std::string token = subString(); + for( size_t i = 0; i < m_escapeChars.size(); ++i ) + token = token.substr( 0, m_escapeChars[i]-m_start-i ) + token.substr( m_escapeChars[i]-m_start-i+1 ); + m_escapeChars.clear(); + if( startsWith( token, "exclude:" ) ) { + m_exclusion = true; + token = token.substr( 8 ); + } + if( !token.empty() ) { + Ptr<TestSpec::Pattern> pattern = new T( token ); + if( m_exclusion ) + pattern = new TestSpec::ExcludedPattern( pattern ); + m_currentFilter.m_patterns.push_back( pattern ); + } + m_exclusion = false; + m_mode = None; + } + void addFilter() { + if( !m_currentFilter.m_patterns.empty() ) { + m_testSpec.m_filters.push_back( m_currentFilter ); + m_currentFilter = TestSpec::Filter(); + } + } + }; + inline TestSpec parseTestSpec( std::string const& arg ) { + return TestSpecParser( ITagAliasRegistry::get() ).parse( arg ).testSpec(); + } + +} // namespace Catch + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +// #included from: catch_interfaces_config.h +#define TWOBLUECUBES_CATCH_INTERFACES_CONFIG_H_INCLUDED + +#include <iosfwd> +#include <string> +#include <vector> + +namespace Catch { + + struct Verbosity { enum Level { + NoOutput = 0, + Quiet, + Normal + }; }; + + struct WarnAbout { enum What { + Nothing = 0x00, + NoAssertions = 0x01 + }; }; + + struct ShowDurations { enum OrNot { + DefaultForReporter, + Always, + Never + }; }; + struct RunTests { enum InWhatOrder { + InDeclarationOrder, + InLexicographicalOrder, + InRandomOrder + }; }; + struct UseColour { enum YesOrNo { + Auto, + Yes, + No + }; }; + + class TestSpec; + + struct IConfig : IShared { + + virtual ~IConfig(); + + virtual bool allowThrows() const = 0; + virtual std::ostream& stream() const = 0; + virtual std::string name() const = 0; + virtual bool includeSuccessfulResults() const = 0; + virtual bool shouldDebugBreak() const = 0; + virtual bool warnAboutMissingAssertions() const = 0; + virtual int abortAfter() const = 0; + virtual bool showInvisibles() const = 0; + virtual ShowDurations::OrNot showDurations() const = 0; + virtual TestSpec const& testSpec() const = 0; + virtual RunTests::InWhatOrder runOrder() const = 0; + virtual unsigned int rngSeed() const = 0; + virtual UseColour::YesOrNo useColour() const = 0; + virtual std::vector<std::string> const& getSectionsToRun() const = 0; + + }; +} + +// #included from: catch_stream.h +#define TWOBLUECUBES_CATCH_STREAM_H_INCLUDED + +// #included from: catch_streambuf.h +#define TWOBLUECUBES_CATCH_STREAMBUF_H_INCLUDED + +#include <streambuf> + +namespace Catch { + + class StreamBufBase : public std::streambuf { + public: + virtual ~StreamBufBase() CATCH_NOEXCEPT; + }; +} + +#include <streambuf> +#include <ostream> +#include <fstream> +#include <memory> + +namespace Catch { + + std::ostream& cout(); + std::ostream& cerr(); + + struct IStream { + virtual ~IStream() CATCH_NOEXCEPT; + virtual std::ostream& stream() const = 0; + }; + + class FileStream : public IStream { + mutable std::ofstream m_ofs; + public: + FileStream( std::string const& filename ); + virtual ~FileStream() CATCH_NOEXCEPT; + public: // IStream + virtual std::ostream& stream() const CATCH_OVERRIDE; + }; + + class CoutStream : public IStream { + mutable std::ostream m_os; + public: + CoutStream(); + virtual ~CoutStream() CATCH_NOEXCEPT; + + public: // IStream + virtual std::ostream& stream() const CATCH_OVERRIDE; + }; + + class DebugOutStream : public IStream { + CATCH_AUTO_PTR( StreamBufBase ) m_streamBuf; + mutable std::ostream m_os; + public: + DebugOutStream(); + virtual ~DebugOutStream() CATCH_NOEXCEPT; + + public: // IStream + virtual std::ostream& stream() const CATCH_OVERRIDE; + }; +} + +#include <memory> +#include <vector> +#include <string> +#include <stdexcept> + +#ifndef CATCH_CONFIG_CONSOLE_WIDTH +#define CATCH_CONFIG_CONSOLE_WIDTH 80 +#endif + +namespace Catch { + + struct ConfigData { + + ConfigData() + : listTests( false ), + listTags( false ), + listReporters( false ), + listTestNamesOnly( false ), + showSuccessfulTests( false ), + shouldDebugBreak( false ), + noThrow( false ), + showHelp( false ), + showInvisibles( false ), + filenamesAsTags( false ), + abortAfter( -1 ), + rngSeed( 0 ), + verbosity( Verbosity::Normal ), + warnings( WarnAbout::Nothing ), + showDurations( ShowDurations::DefaultForReporter ), + runOrder( RunTests::InDeclarationOrder ), + useColour( UseColour::Auto ) + {} + + bool listTests; + bool listTags; + bool listReporters; + bool listTestNamesOnly; + + bool showSuccessfulTests; + bool shouldDebugBreak; + bool noThrow; + bool showHelp; + bool showInvisibles; + bool filenamesAsTags; + + int abortAfter; + unsigned int rngSeed; + + Verbosity::Level verbosity; + WarnAbout::What warnings; + ShowDurations::OrNot showDurations; + RunTests::InWhatOrder runOrder; + UseColour::YesOrNo useColour; + + std::string outputFilename; + std::string name; + std::string processName; + + std::vector<std::string> reporterNames; + std::vector<std::string> testsOrTags; + std::vector<std::string> sectionsToRun; + }; + + class Config : public SharedImpl<IConfig> { + private: + Config( Config const& other ); + Config& operator = ( Config const& other ); + virtual void dummy(); + public: + + Config() + {} + + Config( ConfigData const& data ) + : m_data( data ), + m_stream( openStream() ) + { + if( !data.testsOrTags.empty() ) { + TestSpecParser parser( ITagAliasRegistry::get() ); + for( std::size_t i = 0; i < data.testsOrTags.size(); ++i ) + parser.parse( data.testsOrTags[i] ); + m_testSpec = parser.testSpec(); + } + } + + virtual ~Config() {} + + std::string const& getFilename() const { + return m_data.outputFilename ; + } + + bool listTests() const { return m_data.listTests; } + bool listTestNamesOnly() const { return m_data.listTestNamesOnly; } + bool listTags() const { return m_data.listTags; } + bool listReporters() const { return m_data.listReporters; } + + std::string getProcessName() const { return m_data.processName; } + + std::vector<std::string> const& getReporterNames() const { return m_data.reporterNames; } + std::vector<std::string> const& getSectionsToRun() const CATCH_OVERRIDE { return m_data.sectionsToRun; } + + virtual TestSpec const& testSpec() const CATCH_OVERRIDE { return m_testSpec; } + + bool showHelp() const { return m_data.showHelp; } + + // IConfig interface + virtual bool allowThrows() const CATCH_OVERRIDE { return !m_data.noThrow; } + virtual std::ostream& stream() const CATCH_OVERRIDE { return m_stream->stream(); } + virtual std::string name() const CATCH_OVERRIDE { return m_data.name.empty() ? m_data.processName : m_data.name; } + virtual bool includeSuccessfulResults() const CATCH_OVERRIDE { return m_data.showSuccessfulTests; } + virtual bool warnAboutMissingAssertions() const CATCH_OVERRIDE { return m_data.warnings & WarnAbout::NoAssertions; } + virtual ShowDurations::OrNot showDurations() const CATCH_OVERRIDE { return m_data.showDurations; } + virtual RunTests::InWhatOrder runOrder() const CATCH_OVERRIDE { return m_data.runOrder; } + virtual unsigned int rngSeed() const CATCH_OVERRIDE { return m_data.rngSeed; } + virtual UseColour::YesOrNo useColour() const CATCH_OVERRIDE { return m_data.useColour; } + virtual bool shouldDebugBreak() const CATCH_OVERRIDE { return m_data.shouldDebugBreak; } + virtual int abortAfter() const CATCH_OVERRIDE { return m_data.abortAfter; } + virtual bool showInvisibles() const CATCH_OVERRIDE { return m_data.showInvisibles; } + + private: + + IStream const* openStream() { + if( m_data.outputFilename.empty() ) + return new CoutStream(); + else if( m_data.outputFilename[0] == '%' ) { + if( m_data.outputFilename == "%debug" ) + return new DebugOutStream(); + else + throw std::domain_error( "Unrecognised stream: " + m_data.outputFilename ); + } + else + return new FileStream( m_data.outputFilename ); + } + ConfigData m_data; + + CATCH_AUTO_PTR( IStream const ) m_stream; + TestSpec m_testSpec; + }; + +} // end namespace Catch + +// #included from: catch_clara.h +#define TWOBLUECUBES_CATCH_CLARA_H_INCLUDED + +// Use Catch's value for console width (store Clara's off to the side, if present) +#ifdef CLARA_CONFIG_CONSOLE_WIDTH +#define CATCH_TEMP_CLARA_CONFIG_CONSOLE_WIDTH CLARA_CONFIG_CONSOLE_WIDTH +#undef CLARA_CONFIG_CONSOLE_WIDTH +#endif +#define CLARA_CONFIG_CONSOLE_WIDTH CATCH_CONFIG_CONSOLE_WIDTH + +// Declare Clara inside the Catch namespace +#define STITCH_CLARA_OPEN_NAMESPACE namespace Catch { +// #included from: ../external/clara.h + +// Version 0.0.2.4 + +// Only use header guard if we are not using an outer namespace +#if !defined(TWOBLUECUBES_CLARA_H_INCLUDED) || defined(STITCH_CLARA_OPEN_NAMESPACE) + +#ifndef STITCH_CLARA_OPEN_NAMESPACE +#define TWOBLUECUBES_CLARA_H_INCLUDED +#define STITCH_CLARA_OPEN_NAMESPACE +#define STITCH_CLARA_CLOSE_NAMESPACE +#else +#define STITCH_CLARA_CLOSE_NAMESPACE } +#endif + +#define STITCH_TBC_TEXT_FORMAT_OPEN_NAMESPACE STITCH_CLARA_OPEN_NAMESPACE + +// ----------- #included from tbc_text_format.h ----------- + +// Only use header guard if we are not using an outer namespace +#if !defined(TBC_TEXT_FORMAT_H_INCLUDED) || defined(STITCH_TBC_TEXT_FORMAT_OUTER_NAMESPACE) +#ifndef STITCH_TBC_TEXT_FORMAT_OUTER_NAMESPACE +#define TBC_TEXT_FORMAT_H_INCLUDED +#endif + +#include <string> +#include <vector> +#include <sstream> +#include <algorithm> + +// Use optional outer namespace +#ifdef STITCH_TBC_TEXT_FORMAT_OUTER_NAMESPACE +namespace STITCH_TBC_TEXT_FORMAT_OUTER_NAMESPACE { +#endif + +namespace Tbc { + +#ifdef TBC_TEXT_FORMAT_CONSOLE_WIDTH + const unsigned int consoleWidth = TBC_TEXT_FORMAT_CONSOLE_WIDTH; +#else + const unsigned int consoleWidth = 80; +#endif + + struct TextAttributes { + TextAttributes() + : initialIndent( std::string::npos ), + indent( 0 ), + width( consoleWidth-1 ), + tabChar( '\t' ) + {} + + TextAttributes& setInitialIndent( std::size_t _value ) { initialIndent = _value; return *this; } + TextAttributes& setIndent( std::size_t _value ) { indent = _value; return *this; } + TextAttributes& setWidth( std::size_t _value ) { width = _value; return *this; } + TextAttributes& setTabChar( char _value ) { tabChar = _value; return *this; } + + std::size_t initialIndent; // indent of first line, or npos + std::size_t indent; // indent of subsequent lines, or all if initialIndent is npos + std::size_t width; // maximum width of text, including indent. Longer text will wrap + char tabChar; // If this char is seen the indent is changed to current pos + }; + + class Text { + public: + Text( std::string const& _str, TextAttributes const& _attr = TextAttributes() ) + : attr( _attr ) + { + std::string wrappableChars = " [({.,/|\\-"; + std::size_t indent = _attr.initialIndent != std::string::npos + ? _attr.initialIndent + : _attr.indent; + std::string remainder = _str; + + while( !remainder.empty() ) { + if( lines.size() >= 1000 ) { + lines.push_back( "... message truncated due to excessive size" ); + return; + } + std::size_t tabPos = std::string::npos; + std::size_t width = (std::min)( remainder.size(), _attr.width - indent ); + std::size_t pos = remainder.find_first_of( '\n' ); + if( pos <= width ) { + width = pos; + } + pos = remainder.find_last_of( _attr.tabChar, width ); + if( pos != std::string::npos ) { + tabPos = pos; + if( remainder[width] == '\n' ) + width--; + remainder = remainder.substr( 0, tabPos ) + remainder.substr( tabPos+1 ); + } + + if( width == remainder.size() ) { + spliceLine( indent, remainder, width ); + } + else if( remainder[width] == '\n' ) { + spliceLine( indent, remainder, width ); + if( width <= 1 || remainder.size() != 1 ) + remainder = remainder.substr( 1 ); + indent = _attr.indent; + } + else { + pos = remainder.find_last_of( wrappableChars, width ); + if( pos != std::string::npos && pos > 0 ) { + spliceLine( indent, remainder, pos ); + if( remainder[0] == ' ' ) + remainder = remainder.substr( 1 ); + } + else { + spliceLine( indent, remainder, width-1 ); + lines.back() += "-"; + } + if( lines.size() == 1 ) + indent = _attr.indent; + if( tabPos != std::string::npos ) + indent += tabPos; + } + } + } + + void spliceLine( std::size_t _indent, std::string& _remainder, std::size_t _pos ) { + lines.push_back( std::string( _indent, ' ' ) + _remainder.substr( 0, _pos ) ); + _remainder = _remainder.substr( _pos ); + } + + typedef std::vector<std::string>::const_iterator const_iterator; + + const_iterator begin() const { return lines.begin(); } + const_iterator end() const { return lines.end(); } + std::string const& last() const { return lines.back(); } + std::size_t size() const { return lines.size(); } + std::string const& operator[]( std::size_t _index ) const { return lines[_index]; } + std::string toString() const { + std::ostringstream oss; + oss << *this; + return oss.str(); + } + + inline friend std::ostream& operator << ( std::ostream& _stream, Text const& _text ) { + for( Text::const_iterator it = _text.begin(), itEnd = _text.end(); + it != itEnd; ++it ) { + if( it != _text.begin() ) + _stream << "\n"; + _stream << *it; + } + return _stream; + } + + private: + std::string str; + TextAttributes attr; + std::vector<std::string> lines; + }; + +} // end namespace Tbc + +#ifdef STITCH_TBC_TEXT_FORMAT_OUTER_NAMESPACE +} // end outer namespace +#endif + +#endif // TBC_TEXT_FORMAT_H_INCLUDED + +// ----------- end of #include from tbc_text_format.h ----------- +// ........... back in clara.h + +#undef STITCH_TBC_TEXT_FORMAT_OPEN_NAMESPACE + +// ----------- #included from clara_compilers.h ----------- + +#ifndef TWOBLUECUBES_CLARA_COMPILERS_H_INCLUDED +#define TWOBLUECUBES_CLARA_COMPILERS_H_INCLUDED + +// Detect a number of compiler features - mostly C++11/14 conformance - by compiler +// The following features are defined: +// +// CLARA_CONFIG_CPP11_NULLPTR : is nullptr supported? +// CLARA_CONFIG_CPP11_NOEXCEPT : is noexcept supported? +// CLARA_CONFIG_CPP11_GENERATED_METHODS : The delete and default keywords for compiler generated methods +// CLARA_CONFIG_CPP11_OVERRIDE : is override supported? +// CLARA_CONFIG_CPP11_UNIQUE_PTR : is unique_ptr supported (otherwise use auto_ptr) + +// CLARA_CONFIG_CPP11_OR_GREATER : Is C++11 supported? + +// CLARA_CONFIG_VARIADIC_MACROS : are variadic macros supported? + +// In general each macro has a _NO_<feature name> form +// (e.g. CLARA_CONFIG_CPP11_NO_NULLPTR) which disables the feature. +// Many features, at point of detection, define an _INTERNAL_ macro, so they +// can be combined, en-mass, with the _NO_ forms later. + +// All the C++11 features can be disabled with CLARA_CONFIG_NO_CPP11 + +#ifdef __clang__ + +#if __has_feature(cxx_nullptr) +#define CLARA_INTERNAL_CONFIG_CPP11_NULLPTR +#endif + +#if __has_feature(cxx_noexcept) +#define CLARA_INTERNAL_CONFIG_CPP11_NOEXCEPT +#endif + +#endif // __clang__ + +//////////////////////////////////////////////////////////////////////////////// +// GCC +#ifdef __GNUC__ + +#if __GNUC__ == 4 && __GNUC_MINOR__ >= 6 && defined(__GXX_EXPERIMENTAL_CXX0X__) +#define CLARA_INTERNAL_CONFIG_CPP11_NULLPTR +#endif + +// - otherwise more recent versions define __cplusplus >= 201103L +// and will get picked up below + +#endif // __GNUC__ + +//////////////////////////////////////////////////////////////////////////////// +// Visual C++ +#ifdef _MSC_VER + +#if (_MSC_VER >= 1600) +#define CLARA_INTERNAL_CONFIG_CPP11_NULLPTR +#define CLARA_INTERNAL_CONFIG_CPP11_UNIQUE_PTR +#endif + +#if (_MSC_VER >= 1900 ) // (VC++ 13 (VS2015)) +#define CLARA_INTERNAL_CONFIG_CPP11_NOEXCEPT +#define CLARA_INTERNAL_CONFIG_CPP11_GENERATED_METHODS +#endif + +#endif // _MSC_VER + +//////////////////////////////////////////////////////////////////////////////// +// C++ language feature support + +// catch all support for C++11 +#if defined(__cplusplus) && __cplusplus >= 201103L + +#define CLARA_CPP11_OR_GREATER + +#if !defined(CLARA_INTERNAL_CONFIG_CPP11_NULLPTR) +#define CLARA_INTERNAL_CONFIG_CPP11_NULLPTR +#endif + +#ifndef CLARA_INTERNAL_CONFIG_CPP11_NOEXCEPT +#define CLARA_INTERNAL_CONFIG_CPP11_NOEXCEPT +#endif + +#ifndef CLARA_INTERNAL_CONFIG_CPP11_GENERATED_METHODS +#define CLARA_INTERNAL_CONFIG_CPP11_GENERATED_METHODS +#endif + +#if !defined(CLARA_INTERNAL_CONFIG_CPP11_OVERRIDE) +#define CLARA_INTERNAL_CONFIG_CPP11_OVERRIDE +#endif +#if !defined(CLARA_INTERNAL_CONFIG_CPP11_UNIQUE_PTR) +#define CLARA_INTERNAL_CONFIG_CPP11_UNIQUE_PTR +#endif + +#endif // __cplusplus >= 201103L + +// Now set the actual defines based on the above + anything the user has configured +#if defined(CLARA_INTERNAL_CONFIG_CPP11_NULLPTR) && !defined(CLARA_CONFIG_CPP11_NO_NULLPTR) && !defined(CLARA_CONFIG_CPP11_NULLPTR) && !defined(CLARA_CONFIG_NO_CPP11) +#define CLARA_CONFIG_CPP11_NULLPTR +#endif +#if defined(CLARA_INTERNAL_CONFIG_CPP11_NOEXCEPT) && !defined(CLARA_CONFIG_CPP11_NO_NOEXCEPT) && !defined(CLARA_CONFIG_CPP11_NOEXCEPT) && !defined(CLARA_CONFIG_NO_CPP11) +#define CLARA_CONFIG_CPP11_NOEXCEPT +#endif +#if defined(CLARA_INTERNAL_CONFIG_CPP11_GENERATED_METHODS) && !defined(CLARA_CONFIG_CPP11_NO_GENERATED_METHODS) && !defined(CLARA_CONFIG_CPP11_GENERATED_METHODS) && !defined(CLARA_CONFIG_NO_CPP11) +#define CLARA_CONFIG_CPP11_GENERATED_METHODS +#endif +#if defined(CLARA_INTERNAL_CONFIG_CPP11_OVERRIDE) && !defined(CLARA_CONFIG_NO_OVERRIDE) && !defined(CLARA_CONFIG_CPP11_OVERRIDE) && !defined(CLARA_CONFIG_NO_CPP11) +#define CLARA_CONFIG_CPP11_OVERRIDE +#endif +#if defined(CLARA_INTERNAL_CONFIG_CPP11_UNIQUE_PTR) && !defined(CLARA_CONFIG_NO_UNIQUE_PTR) && !defined(CLARA_CONFIG_CPP11_UNIQUE_PTR) && !defined(CLARA_CONFIG_NO_CPP11) +#define CLARA_CONFIG_CPP11_UNIQUE_PTR +#endif + +// noexcept support: +#if defined(CLARA_CONFIG_CPP11_NOEXCEPT) && !defined(CLARA_NOEXCEPT) +#define CLARA_NOEXCEPT noexcept +# define CLARA_NOEXCEPT_IS(x) noexcept(x) +#else +#define CLARA_NOEXCEPT throw() +# define CLARA_NOEXCEPT_IS(x) +#endif + +// nullptr support +#ifdef CLARA_CONFIG_CPP11_NULLPTR +#define CLARA_NULL nullptr +#else +#define CLARA_NULL NULL +#endif + +// override support +#ifdef CLARA_CONFIG_CPP11_OVERRIDE +#define CLARA_OVERRIDE override +#else +#define CLARA_OVERRIDE +#endif + +// unique_ptr support +#ifdef CLARA_CONFIG_CPP11_UNIQUE_PTR +# define CLARA_AUTO_PTR( T ) std::unique_ptr<T> +#else +# define CLARA_AUTO_PTR( T ) std::auto_ptr<T> +#endif + +#endif // TWOBLUECUBES_CLARA_COMPILERS_H_INCLUDED + +// ----------- end of #include from clara_compilers.h ----------- +// ........... back in clara.h + +#include <map> +#include <stdexcept> +#include <memory> + +#if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) +#define CLARA_PLATFORM_WINDOWS +#endif + +// Use optional outer namespace +#ifdef STITCH_CLARA_OPEN_NAMESPACE +STITCH_CLARA_OPEN_NAMESPACE +#endif + +namespace Clara { + + struct UnpositionalTag {}; + + extern UnpositionalTag _; + +#ifdef CLARA_CONFIG_MAIN + UnpositionalTag _; +#endif + + namespace Detail { + +#ifdef CLARA_CONSOLE_WIDTH + const unsigned int consoleWidth = CLARA_CONFIG_CONSOLE_WIDTH; +#else + const unsigned int consoleWidth = 80; +#endif + + using namespace Tbc; + + inline bool startsWith( std::string const& str, std::string const& prefix ) { + return str.size() >= prefix.size() && str.substr( 0, prefix.size() ) == prefix; + } + + template<typename T> struct RemoveConstRef{ typedef T type; }; + template<typename T> struct RemoveConstRef<T&>{ typedef T type; }; + template<typename T> struct RemoveConstRef<T const&>{ typedef T type; }; + template<typename T> struct RemoveConstRef<T const>{ typedef T type; }; + + template<typename T> struct IsBool { static const bool value = false; }; + template<> struct IsBool<bool> { static const bool value = true; }; + + template<typename T> + void convertInto( std::string const& _source, T& _dest ) { + std::stringstream ss; + ss << _source; + ss >> _dest; + if( ss.fail() ) + throw std::runtime_error( "Unable to convert " + _source + " to destination type" ); + } + inline void convertInto( std::string const& _source, std::string& _dest ) { + _dest = _source; + } + char toLowerCh(char c) { + return static_cast<char>( ::tolower( c ) ); + } + inline void convertInto( std::string const& _source, bool& _dest ) { + std::string sourceLC = _source; + std::transform( sourceLC.begin(), sourceLC.end(), sourceLC.begin(), toLowerCh ); + if( sourceLC == "y" || sourceLC == "1" || sourceLC == "true" || sourceLC == "yes" || sourceLC == "on" ) + _dest = true; + else if( sourceLC == "n" || sourceLC == "0" || sourceLC == "false" || sourceLC == "no" || sourceLC == "off" ) + _dest = false; + else + throw std::runtime_error( "Expected a boolean value but did not recognise:\n '" + _source + "'" ); + } + + template<typename ConfigT> + struct IArgFunction { + virtual ~IArgFunction() {} +#ifdef CLARA_CONFIG_CPP11_GENERATED_METHODS + IArgFunction() = default; + IArgFunction( IArgFunction const& ) = default; +#endif + virtual void set( ConfigT& config, std::string const& value ) const = 0; + virtual bool takesArg() const = 0; + virtual IArgFunction* clone() const = 0; + }; + + template<typename ConfigT> + class BoundArgFunction { + public: + BoundArgFunction() : functionObj( CLARA_NULL ) {} + BoundArgFunction( IArgFunction<ConfigT>* _functionObj ) : functionObj( _functionObj ) {} + BoundArgFunction( BoundArgFunction const& other ) : functionObj( other.functionObj ? other.functionObj->clone() : CLARA_NULL ) {} + BoundArgFunction& operator = ( BoundArgFunction const& other ) { + IArgFunction<ConfigT>* newFunctionObj = other.functionObj ? other.functionObj->clone() : CLARA_NULL; + delete functionObj; + functionObj = newFunctionObj; + return *this; + } + ~BoundArgFunction() { delete functionObj; } + + void set( ConfigT& config, std::string const& value ) const { + functionObj->set( config, value ); + } + bool takesArg() const { return functionObj->takesArg(); } + + bool isSet() const { + return functionObj != CLARA_NULL; + } + private: + IArgFunction<ConfigT>* functionObj; + }; + + template<typename C> + struct NullBinder : IArgFunction<C>{ + virtual void set( C&, std::string const& ) const {} + virtual bool takesArg() const { return true; } + virtual IArgFunction<C>* clone() const { return new NullBinder( *this ); } + }; + + template<typename C, typename M> + struct BoundDataMember : IArgFunction<C>{ + BoundDataMember( M C::* _member ) : member( _member ) {} + virtual void set( C& p, std::string const& stringValue ) const { + convertInto( stringValue, p.*member ); + } + virtual bool takesArg() const { return !IsBool<M>::value; } + virtual IArgFunction<C>* clone() const { return new BoundDataMember( *this ); } + M C::* member; + }; + template<typename C, typename M> + struct BoundUnaryMethod : IArgFunction<C>{ + BoundUnaryMethod( void (C::*_member)( M ) ) : member( _member ) {} + virtual void set( C& p, std::string const& stringValue ) const { + typename RemoveConstRef<M>::type value; + convertInto( stringValue, value ); + (p.*member)( value ); + } + virtual bool takesArg() const { return !IsBool<M>::value; } + virtual IArgFunction<C>* clone() const { return new BoundUnaryMethod( *this ); } + void (C::*member)( M ); + }; + template<typename C> + struct BoundNullaryMethod : IArgFunction<C>{ + BoundNullaryMethod( void (C::*_member)() ) : member( _member ) {} + virtual void set( C& p, std::string const& stringValue ) const { + bool value; + convertInto( stringValue, value ); + if( value ) + (p.*member)(); + } + virtual bool takesArg() const { return false; } + virtual IArgFunction<C>* clone() const { return new BoundNullaryMethod( *this ); } + void (C::*member)(); + }; + + template<typename C> + struct BoundUnaryFunction : IArgFunction<C>{ + BoundUnaryFunction( void (*_function)( C& ) ) : function( _function ) {} + virtual void set( C& obj, std::string const& stringValue ) const { + bool value; + convertInto( stringValue, value ); + if( value ) + function( obj ); + } + virtual bool takesArg() const { return false; } + virtual IArgFunction<C>* clone() const { return new BoundUnaryFunction( *this ); } + void (*function)( C& ); + }; + + template<typename C, typename T> + struct BoundBinaryFunction : IArgFunction<C>{ + BoundBinaryFunction( void (*_function)( C&, T ) ) : function( _function ) {} + virtual void set( C& obj, std::string const& stringValue ) const { + typename RemoveConstRef<T>::type value; + convertInto( stringValue, value ); + function( obj, value ); + } + virtual bool takesArg() const { return !IsBool<T>::value; } + virtual IArgFunction<C>* clone() const { return new BoundBinaryFunction( *this ); } + void (*function)( C&, T ); + }; + + } // namespace Detail + + inline std::vector<std::string> argsToVector( int argc, char const* const* const argv ) { + std::vector<std::string> args( static_cast<std::size_t>( argc ) ); + for( std::size_t i = 0; i < static_cast<std::size_t>( argc ); ++i ) + args[i] = argv[i]; + + return args; + } + + class Parser { + enum Mode { None, MaybeShortOpt, SlashOpt, ShortOpt, LongOpt, Positional }; + Mode mode; + std::size_t from; + bool inQuotes; + public: + + struct Token { + enum Type { Positional, ShortOpt, LongOpt }; + Token( Type _type, std::string const& _data ) : type( _type ), data( _data ) {} + Type type; + std::string data; + }; + + Parser() : mode( None ), from( 0 ), inQuotes( false ){} + + void parseIntoTokens( std::vector<std::string> const& args, std::vector<Token>& tokens ) { + const std::string doubleDash = "--"; + for( std::size_t i = 1; i < args.size() && args[i] != doubleDash; ++i ) + parseIntoTokens( args[i], tokens); + } + + void parseIntoTokens( std::string const& arg, std::vector<Token>& tokens ) { + for( std::size_t i = 0; i <= arg.size(); ++i ) { + char c = arg[i]; + if( c == '"' ) + inQuotes = !inQuotes; + mode = handleMode( i, c, arg, tokens ); + } + } + Mode handleMode( std::size_t i, char c, std::string const& arg, std::vector<Token>& tokens ) { + switch( mode ) { + case None: return handleNone( i, c ); + case MaybeShortOpt: return handleMaybeShortOpt( i, c ); + case ShortOpt: + case LongOpt: + case SlashOpt: return handleOpt( i, c, arg, tokens ); + case Positional: return handlePositional( i, c, arg, tokens ); + default: throw std::logic_error( "Unknown mode" ); + } + } + + Mode handleNone( std::size_t i, char c ) { + if( inQuotes ) { + from = i; + return Positional; + } + switch( c ) { + case '-': return MaybeShortOpt; +#ifdef CLARA_PLATFORM_WINDOWS + case '/': from = i+1; return SlashOpt; +#endif + default: from = i; return Positional; + } + } + Mode handleMaybeShortOpt( std::size_t i, char c ) { + switch( c ) { + case '-': from = i+1; return LongOpt; + default: from = i; return ShortOpt; + } + } + Mode handleOpt( std::size_t i, char c, std::string const& arg, std::vector<Token>& tokens ) { + if( std::string( ":=\0", 3 ).find( c ) == std::string::npos ) + return mode; + + std::string optName = arg.substr( from, i-from ); + if( mode == ShortOpt ) + for( std::size_t j = 0; j < optName.size(); ++j ) + tokens.push_back( Token( Token::ShortOpt, optName.substr( j, 1 ) ) ); + else if( mode == SlashOpt && optName.size() == 1 ) + tokens.push_back( Token( Token::ShortOpt, optName ) ); + else + tokens.push_back( Token( Token::LongOpt, optName ) ); + return None; + } + Mode handlePositional( std::size_t i, char c, std::string const& arg, std::vector<Token>& tokens ) { + if( inQuotes || std::string( "\0", 1 ).find( c ) == std::string::npos ) + return mode; + + std::string data = arg.substr( from, i-from ); + tokens.push_back( Token( Token::Positional, data ) ); + return None; + } + }; + + template<typename ConfigT> + struct CommonArgProperties { + CommonArgProperties() {} + CommonArgProperties( Detail::BoundArgFunction<ConfigT> const& _boundField ) : boundField( _boundField ) {} + + Detail::BoundArgFunction<ConfigT> boundField; + std::string description; + std::string detail; + std::string placeholder; // Only value if boundField takes an arg + + bool takesArg() const { + return !placeholder.empty(); + } + void validate() const { + if( !boundField.isSet() ) + throw std::logic_error( "option not bound" ); + } + }; + struct OptionArgProperties { + std::vector<std::string> shortNames; + std::string longName; + + bool hasShortName( std::string const& shortName ) const { + return std::find( shortNames.begin(), shortNames.end(), shortName ) != shortNames.end(); + } + bool hasLongName( std::string const& _longName ) const { + return _longName == longName; + } + }; + struct PositionalArgProperties { + PositionalArgProperties() : position( -1 ) {} + int position; // -1 means non-positional (floating) + + bool isFixedPositional() const { + return position != -1; + } + }; + + template<typename ConfigT> + class CommandLine { + + struct Arg : CommonArgProperties<ConfigT>, OptionArgProperties, PositionalArgProperties { + Arg() {} + Arg( Detail::BoundArgFunction<ConfigT> const& _boundField ) : CommonArgProperties<ConfigT>( _boundField ) {} + + using CommonArgProperties<ConfigT>::placeholder; // !TBD + + std::string dbgName() const { + if( !longName.empty() ) + return "--" + longName; + if( !shortNames.empty() ) + return "-" + shortNames[0]; + return "positional args"; + } + std::string commands() const { + std::ostringstream oss; + bool first = true; + std::vector<std::string>::const_iterator it = shortNames.begin(), itEnd = shortNames.end(); + for(; it != itEnd; ++it ) { + if( first ) + first = false; + else + oss << ", "; + oss << "-" << *it; + } + if( !longName.empty() ) { + if( !first ) + oss << ", "; + oss << "--" << longName; + } + if( !placeholder.empty() ) + oss << " <" << placeholder << ">"; + return oss.str(); + } + }; + + typedef CLARA_AUTO_PTR( Arg ) ArgAutoPtr; + + friend void addOptName( Arg& arg, std::string const& optName ) + { + if( optName.empty() ) + return; + if( Detail::startsWith( optName, "--" ) ) { + if( !arg.longName.empty() ) + throw std::logic_error( "Only one long opt may be specified. '" + + arg.longName + + "' already specified, now attempting to add '" + + optName + "'" ); + arg.longName = optName.substr( 2 ); + } + else if( Detail::startsWith( optName, "-" ) ) + arg.shortNames.push_back( optName.substr( 1 ) ); + else + throw std::logic_error( "option must begin with - or --. Option was: '" + optName + "'" ); + } + friend void setPositionalArg( Arg& arg, int position ) + { + arg.position = position; + } + + class ArgBuilder { + public: + ArgBuilder( Arg* arg ) : m_arg( arg ) {} + + // Bind a non-boolean data member (requires placeholder string) + template<typename C, typename M> + void bind( M C::* field, std::string const& placeholder ) { + m_arg->boundField = new Detail::BoundDataMember<C,M>( field ); + m_arg->placeholder = placeholder; + } + // Bind a boolean data member (no placeholder required) + template<typename C> + void bind( bool C::* field ) { + m_arg->boundField = new Detail::BoundDataMember<C,bool>( field ); + } + + // Bind a method taking a single, non-boolean argument (requires a placeholder string) + template<typename C, typename M> + void bind( void (C::* unaryMethod)( M ), std::string const& placeholder ) { + m_arg->boundField = new Detail::BoundUnaryMethod<C,M>( unaryMethod ); + m_arg->placeholder = placeholder; + } + + // Bind a method taking a single, boolean argument (no placeholder string required) + template<typename C> + void bind( void (C::* unaryMethod)( bool ) ) { + m_arg->boundField = new Detail::BoundUnaryMethod<C,bool>( unaryMethod ); + } + + // Bind a method that takes no arguments (will be called if opt is present) + template<typename C> + void bind( void (C::* nullaryMethod)() ) { + m_arg->boundField = new Detail::BoundNullaryMethod<C>( nullaryMethod ); + } + + // Bind a free function taking a single argument - the object to operate on (no placeholder string required) + template<typename C> + void bind( void (* unaryFunction)( C& ) ) { + m_arg->boundField = new Detail::BoundUnaryFunction<C>( unaryFunction ); + } + + // Bind a free function taking a single argument - the object to operate on (requires a placeholder string) + template<typename C, typename T> + void bind( void (* binaryFunction)( C&, T ), std::string const& placeholder ) { + m_arg->boundField = new Detail::BoundBinaryFunction<C, T>( binaryFunction ); + m_arg->placeholder = placeholder; + } + + ArgBuilder& describe( std::string const& description ) { + m_arg->description = description; + return *this; + } + ArgBuilder& detail( std::string const& detail ) { + m_arg->detail = detail; + return *this; + } + + protected: + Arg* m_arg; + }; + + class OptBuilder : public ArgBuilder { + public: + OptBuilder( Arg* arg ) : ArgBuilder( arg ) {} + OptBuilder( OptBuilder& other ) : ArgBuilder( other ) {} + + OptBuilder& operator[]( std::string const& optName ) { + addOptName( *ArgBuilder::m_arg, optName ); + return *this; + } + }; + + public: + + CommandLine() + : m_boundProcessName( new Detail::NullBinder<ConfigT>() ), + m_highestSpecifiedArgPosition( 0 ), + m_throwOnUnrecognisedTokens( false ) + {} + CommandLine( CommandLine const& other ) + : m_boundProcessName( other.m_boundProcessName ), + m_options ( other.m_options ), + m_positionalArgs( other.m_positionalArgs ), + m_highestSpecifiedArgPosition( other.m_highestSpecifiedArgPosition ), + m_throwOnUnrecognisedTokens( other.m_throwOnUnrecognisedTokens ) + { + if( other.m_floatingArg.get() ) + m_floatingArg.reset( new Arg( *other.m_floatingArg ) ); + } + + CommandLine& setThrowOnUnrecognisedTokens( bool shouldThrow = true ) { + m_throwOnUnrecognisedTokens = shouldThrow; + return *this; + } + + OptBuilder operator[]( std::string const& optName ) { + m_options.push_back( Arg() ); + addOptName( m_options.back(), optName ); + OptBuilder builder( &m_options.back() ); + return builder; + } + + ArgBuilder operator[]( int position ) { + m_positionalArgs.insert( std::make_pair( position, Arg() ) ); + if( position > m_highestSpecifiedArgPosition ) + m_highestSpecifiedArgPosition = position; + setPositionalArg( m_positionalArgs[position], position ); + ArgBuilder builder( &m_positionalArgs[position] ); + return builder; + } + + // Invoke this with the _ instance + ArgBuilder operator[]( UnpositionalTag ) { + if( m_floatingArg.get() ) + throw std::logic_error( "Only one unpositional argument can be added" ); + m_floatingArg.reset( new Arg() ); + ArgBuilder builder( m_floatingArg.get() ); + return builder; + } + + template<typename C, typename M> + void bindProcessName( M C::* field ) { + m_boundProcessName = new Detail::BoundDataMember<C,M>( field ); + } + template<typename C, typename M> + void bindProcessName( void (C::*_unaryMethod)( M ) ) { + m_boundProcessName = new Detail::BoundUnaryMethod<C,M>( _unaryMethod ); + } + + void optUsage( std::ostream& os, std::size_t indent = 0, std::size_t width = Detail::consoleWidth ) const { + typename std::vector<Arg>::const_iterator itBegin = m_options.begin(), itEnd = m_options.end(), it; + std::size_t maxWidth = 0; + for( it = itBegin; it != itEnd; ++it ) + maxWidth = (std::max)( maxWidth, it->commands().size() ); + + for( it = itBegin; it != itEnd; ++it ) { + Detail::Text usage( it->commands(), Detail::TextAttributes() + .setWidth( maxWidth+indent ) + .setIndent( indent ) ); + Detail::Text desc( it->description, Detail::TextAttributes() + .setWidth( width - maxWidth - 3 ) ); + + for( std::size_t i = 0; i < (std::max)( usage.size(), desc.size() ); ++i ) { + std::string usageCol = i < usage.size() ? usage[i] : ""; + os << usageCol; + + if( i < desc.size() && !desc[i].empty() ) + os << std::string( indent + 2 + maxWidth - usageCol.size(), ' ' ) + << desc[i]; + os << "\n"; + } + } + } + std::string optUsage() const { + std::ostringstream oss; + optUsage( oss ); + return oss.str(); + } + + void argSynopsis( std::ostream& os ) const { + for( int i = 1; i <= m_highestSpecifiedArgPosition; ++i ) { + if( i > 1 ) + os << " "; + typename std::map<int, Arg>::const_iterator it = m_positionalArgs.find( i ); + if( it != m_positionalArgs.end() ) + os << "<" << it->second.placeholder << ">"; + else if( m_floatingArg.get() ) + os << "<" << m_floatingArg->placeholder << ">"; + else + throw std::logic_error( "non consecutive positional arguments with no floating args" ); + } + // !TBD No indication of mandatory args + if( m_floatingArg.get() ) { + if( m_highestSpecifiedArgPosition > 1 ) + os << " "; + os << "[<" << m_floatingArg->placeholder << "> ...]"; + } + } + std::string argSynopsis() const { + std::ostringstream oss; + argSynopsis( oss ); + return oss.str(); + } + + void usage( std::ostream& os, std::string const& procName ) const { + validate(); + os << "usage:\n " << procName << " "; + argSynopsis( os ); + if( !m_options.empty() ) { + os << " [options]\n\nwhere options are: \n"; + optUsage( os, 2 ); + } + os << "\n"; + } + std::string usage( std::string const& procName ) const { + std::ostringstream oss; + usage( oss, procName ); + return oss.str(); + } + + ConfigT parse( std::vector<std::string> const& args ) const { + ConfigT config; + parseInto( args, config ); + return config; + } + + std::vector<Parser::Token> parseInto( std::vector<std::string> const& args, ConfigT& config ) const { + std::string processName = args[0]; + std::size_t lastSlash = processName.find_last_of( "/\\" ); + if( lastSlash != std::string::npos ) + processName = processName.substr( lastSlash+1 ); + m_boundProcessName.set( config, processName ); + std::vector<Parser::Token> tokens; + Parser parser; + parser.parseIntoTokens( args, tokens ); + return populate( tokens, config ); + } + + std::vector<Parser::Token> populate( std::vector<Parser::Token> const& tokens, ConfigT& config ) const { + validate(); + std::vector<Parser::Token> unusedTokens = populateOptions( tokens, config ); + unusedTokens = populateFixedArgs( unusedTokens, config ); + unusedTokens = populateFloatingArgs( unusedTokens, config ); + return unusedTokens; + } + + std::vector<Parser::Token> populateOptions( std::vector<Parser::Token> const& tokens, ConfigT& config ) const { + std::vector<Parser::Token> unusedTokens; + std::vector<std::string> errors; + for( std::size_t i = 0; i < tokens.size(); ++i ) { + Parser::Token const& token = tokens[i]; + typename std::vector<Arg>::const_iterator it = m_options.begin(), itEnd = m_options.end(); + for(; it != itEnd; ++it ) { + Arg const& arg = *it; + + try { + if( ( token.type == Parser::Token::ShortOpt && arg.hasShortName( token.data ) ) || + ( token.type == Parser::Token::LongOpt && arg.hasLongName( token.data ) ) ) { + if( arg.takesArg() ) { + if( i == tokens.size()-1 || tokens[i+1].type != Parser::Token::Positional ) + errors.push_back( "Expected argument to option: " + token.data ); + else + arg.boundField.set( config, tokens[++i].data ); + } + else { + arg.boundField.set( config, "true" ); + } + break; + } + } + catch( std::exception& ex ) { + errors.push_back( std::string( ex.what() ) + "\n- while parsing: (" + arg.commands() + ")" ); + } + } + if( it == itEnd ) { + if( token.type == Parser::Token::Positional || !m_throwOnUnrecognisedTokens ) + unusedTokens.push_back( token ); + else if( errors.empty() && m_throwOnUnrecognisedTokens ) + errors.push_back( "unrecognised option: " + token.data ); + } + } + if( !errors.empty() ) { + std::ostringstream oss; + for( std::vector<std::string>::const_iterator it = errors.begin(), itEnd = errors.end(); + it != itEnd; + ++it ) { + if( it != errors.begin() ) + oss << "\n"; + oss << *it; + } + throw std::runtime_error( oss.str() ); + } + return unusedTokens; + } + std::vector<Parser::Token> populateFixedArgs( std::vector<Parser::Token> const& tokens, ConfigT& config ) const { + std::vector<Parser::Token> unusedTokens; + int position = 1; + for( std::size_t i = 0; i < tokens.size(); ++i ) { + Parser::Token const& token = tokens[i]; + typename std::map<int, Arg>::const_iterator it = m_positionalArgs.find( position ); + if( it != m_positionalArgs.end() ) + it->second.boundField.set( config, token.data ); + else + unusedTokens.push_back( token ); + if( token.type == Parser::Token::Positional ) + position++; + } + return unusedTokens; + } + std::vector<Parser::Token> populateFloatingArgs( std::vector<Parser::Token> const& tokens, ConfigT& config ) const { + if( !m_floatingArg.get() ) + return tokens; + std::vector<Parser::Token> unusedTokens; + for( std::size_t i = 0; i < tokens.size(); ++i ) { + Parser::Token const& token = tokens[i]; + if( token.type == Parser::Token::Positional ) + m_floatingArg->boundField.set( config, token.data ); + else + unusedTokens.push_back( token ); + } + return unusedTokens; + } + + void validate() const + { + if( m_options.empty() && m_positionalArgs.empty() && !m_floatingArg.get() ) + throw std::logic_error( "No options or arguments specified" ); + + for( typename std::vector<Arg>::const_iterator it = m_options.begin(), + itEnd = m_options.end(); + it != itEnd; ++it ) + it->validate(); + } + + private: + Detail::BoundArgFunction<ConfigT> m_boundProcessName; + std::vector<Arg> m_options; + std::map<int, Arg> m_positionalArgs; + ArgAutoPtr m_floatingArg; + int m_highestSpecifiedArgPosition; + bool m_throwOnUnrecognisedTokens; + }; + +} // end namespace Clara + +STITCH_CLARA_CLOSE_NAMESPACE +#undef STITCH_CLARA_OPEN_NAMESPACE +#undef STITCH_CLARA_CLOSE_NAMESPACE + +#endif // TWOBLUECUBES_CLARA_H_INCLUDED +#undef STITCH_CLARA_OPEN_NAMESPACE + +// Restore Clara's value for console width, if present +#ifdef CATCH_TEMP_CLARA_CONFIG_CONSOLE_WIDTH +#define CLARA_CONFIG_CONSOLE_WIDTH CATCH_TEMP_CLARA_CONFIG_CONSOLE_WIDTH +#undef CATCH_TEMP_CLARA_CONFIG_CONSOLE_WIDTH +#endif + +#include <fstream> +#include <ctime> + +namespace Catch { + + inline void abortAfterFirst( ConfigData& config ) { config.abortAfter = 1; } + inline void abortAfterX( ConfigData& config, int x ) { + if( x < 1 ) + throw std::runtime_error( "Value after -x or --abortAfter must be greater than zero" ); + config.abortAfter = x; + } + inline void addTestOrTags( ConfigData& config, std::string const& _testSpec ) { config.testsOrTags.push_back( _testSpec ); } + inline void addSectionToRun( ConfigData& config, std::string const& sectionName ) { config.sectionsToRun.push_back( sectionName ); } + inline void addReporterName( ConfigData& config, std::string const& _reporterName ) { config.reporterNames.push_back( _reporterName ); } + + inline void addWarning( ConfigData& config, std::string const& _warning ) { + if( _warning == "NoAssertions" ) + config.warnings = static_cast<WarnAbout::What>( config.warnings | WarnAbout::NoAssertions ); + else + throw std::runtime_error( "Unrecognised warning: '" + _warning + '\'' ); + } + inline void setOrder( ConfigData& config, std::string const& order ) { + if( startsWith( "declared", order ) ) + config.runOrder = RunTests::InDeclarationOrder; + else if( startsWith( "lexical", order ) ) + config.runOrder = RunTests::InLexicographicalOrder; + else if( startsWith( "random", order ) ) + config.runOrder = RunTests::InRandomOrder; + else + throw std::runtime_error( "Unrecognised ordering: '" + order + '\'' ); + } + inline void setRngSeed( ConfigData& config, std::string const& seed ) { + if( seed == "time" ) { + config.rngSeed = static_cast<unsigned int>( std::time(0) ); + } + else { + std::stringstream ss; + ss << seed; + ss >> config.rngSeed; + if( ss.fail() ) + throw std::runtime_error( "Argument to --rng-seed should be the word 'time' or a number" ); + } + } + inline void setVerbosity( ConfigData& config, int level ) { + // !TBD: accept strings? + config.verbosity = static_cast<Verbosity::Level>( level ); + } + inline void setShowDurations( ConfigData& config, bool _showDurations ) { + config.showDurations = _showDurations + ? ShowDurations::Always + : ShowDurations::Never; + } + inline void setUseColour( ConfigData& config, std::string const& value ) { + std::string mode = toLower( value ); + + if( mode == "yes" ) + config.useColour = UseColour::Yes; + else if( mode == "no" ) + config.useColour = UseColour::No; + else if( mode == "auto" ) + config.useColour = UseColour::Auto; + else + throw std::runtime_error( "colour mode must be one of: auto, yes or no" ); + } + inline void forceColour( ConfigData& config ) { + config.useColour = UseColour::Yes; + } + inline void loadTestNamesFromFile( ConfigData& config, std::string const& _filename ) { + std::ifstream f( _filename.c_str() ); + if( !f.is_open() ) + throw std::domain_error( "Unable to load input file: " + _filename ); + + std::string line; + while( std::getline( f, line ) ) { + line = trim(line); + if( !line.empty() && !startsWith( line, '#' ) ) { + if( !startsWith( line, '"' ) ) + line = '"' + line + '"'; + addTestOrTags( config, line + ',' ); + } + } + } + + inline Clara::CommandLine<ConfigData> makeCommandLineParser() { + + using namespace Clara; + CommandLine<ConfigData> cli; + + cli.bindProcessName( &ConfigData::processName ); + + cli["-?"]["-h"]["--help"] + .describe( "display usage information" ) + .bind( &ConfigData::showHelp ); + + cli["-l"]["--list-tests"] + .describe( "list all/matching test cases" ) + .bind( &ConfigData::listTests ); + + cli["-t"]["--list-tags"] + .describe( "list all/matching tags" ) + .bind( &ConfigData::listTags ); + + cli["-s"]["--success"] + .describe( "include successful tests in output" ) + .bind( &ConfigData::showSuccessfulTests ); + + cli["-b"]["--break"] + .describe( "break into debugger on failure" ) + .bind( &ConfigData::shouldDebugBreak ); + + cli["-e"]["--nothrow"] + .describe( "skip exception tests" ) + .bind( &ConfigData::noThrow ); + + cli["-i"]["--invisibles"] + .describe( "show invisibles (tabs, newlines)" ) + .bind( &ConfigData::showInvisibles ); + + cli["-o"]["--out"] + .describe( "output filename" ) + .bind( &ConfigData::outputFilename, "filename" ); + + cli["-r"]["--reporter"] +// .placeholder( "name[:filename]" ) + .describe( "reporter to use (defaults to console)" ) + .bind( &addReporterName, "name" ); + + cli["-n"]["--name"] + .describe( "suite name" ) + .bind( &ConfigData::name, "name" ); + + cli["-a"]["--abort"] + .describe( "abort at first failure" ) + .bind( &abortAfterFirst ); + + cli["-x"]["--abortx"] + .describe( "abort after x failures" ) + .bind( &abortAfterX, "no. failures" ); + + cli["-w"]["--warn"] + .describe( "enable warnings" ) + .bind( &addWarning, "warning name" ); + +// - needs updating if reinstated +// cli.into( &setVerbosity ) +// .describe( "level of verbosity (0=no output)" ) +// .shortOpt( "v") +// .longOpt( "verbosity" ) +// .placeholder( "level" ); + + cli[_] + .describe( "which test or tests to use" ) + .bind( &addTestOrTags, "test name, pattern or tags" ); + + cli["-d"]["--durations"] + .describe( "show test durations" ) + .bind( &setShowDurations, "yes|no" ); + + cli["-f"]["--input-file"] + .describe( "load test names to run from a file" ) + .bind( &loadTestNamesFromFile, "filename" ); + + cli["-#"]["--filenames-as-tags"] + .describe( "adds a tag for the filename" ) + .bind( &ConfigData::filenamesAsTags ); + + cli["-c"]["--section"] + .describe( "specify section to run" ) + .bind( &addSectionToRun, "section name" ); + + // Less common commands which don't have a short form + cli["--list-test-names-only"] + .describe( "list all/matching test cases names only" ) + .bind( &ConfigData::listTestNamesOnly ); + + cli["--list-reporters"] + .describe( "list all reporters" ) + .bind( &ConfigData::listReporters ); + + cli["--order"] + .describe( "test case order (defaults to decl)" ) + .bind( &setOrder, "decl|lex|rand" ); + + cli["--rng-seed"] + .describe( "set a specific seed for random numbers" ) + .bind( &setRngSeed, "'time'|number" ); + + cli["--force-colour"] + .describe( "force colourised output (deprecated)" ) + .bind( &forceColour ); + + cli["--use-colour"] + .describe( "should output be colourised" ) + .bind( &setUseColour, "yes|no" ); + + return cli; + } + +} // end namespace Catch + +// #included from: internal/catch_list.hpp +#define TWOBLUECUBES_CATCH_LIST_HPP_INCLUDED + +// #included from: catch_text.h +#define TWOBLUECUBES_CATCH_TEXT_H_INCLUDED + +#define TBC_TEXT_FORMAT_CONSOLE_WIDTH CATCH_CONFIG_CONSOLE_WIDTH + +#define CLICHE_TBC_TEXT_FORMAT_OUTER_NAMESPACE Catch +// #included from: ../external/tbc_text_format.h +// Only use header guard if we are not using an outer namespace +#ifndef CLICHE_TBC_TEXT_FORMAT_OUTER_NAMESPACE +# ifdef TWOBLUECUBES_TEXT_FORMAT_H_INCLUDED +# ifndef TWOBLUECUBES_TEXT_FORMAT_H_ALREADY_INCLUDED +# define TWOBLUECUBES_TEXT_FORMAT_H_ALREADY_INCLUDED +# endif +# else +# define TWOBLUECUBES_TEXT_FORMAT_H_INCLUDED +# endif +#endif +#ifndef TWOBLUECUBES_TEXT_FORMAT_H_ALREADY_INCLUDED +#include <string> +#include <vector> +#include <sstream> + +// Use optional outer namespace +#ifdef CLICHE_TBC_TEXT_FORMAT_OUTER_NAMESPACE +namespace CLICHE_TBC_TEXT_FORMAT_OUTER_NAMESPACE { +#endif + +namespace Tbc { + +#ifdef TBC_TEXT_FORMAT_CONSOLE_WIDTH + const unsigned int consoleWidth = TBC_TEXT_FORMAT_CONSOLE_WIDTH; +#else + const unsigned int consoleWidth = 80; +#endif + + struct TextAttributes { + TextAttributes() + : initialIndent( std::string::npos ), + indent( 0 ), + width( consoleWidth-1 ) + {} + + TextAttributes& setInitialIndent( std::size_t _value ) { initialIndent = _value; return *this; } + TextAttributes& setIndent( std::size_t _value ) { indent = _value; return *this; } + TextAttributes& setWidth( std::size_t _value ) { width = _value; return *this; } + + std::size_t initialIndent; // indent of first line, or npos + std::size_t indent; // indent of subsequent lines, or all if initialIndent is npos + std::size_t width; // maximum width of text, including indent. Longer text will wrap + }; + + class Text { + public: + Text( std::string const& _str, TextAttributes const& _attr = TextAttributes() ) + : attr( _attr ) + { + const std::string wrappableBeforeChars = "[({<\t"; + const std::string wrappableAfterChars = "])}>-,./|\\"; + const std::string wrappableInsteadOfChars = " \n\r"; + std::string indent = _attr.initialIndent != std::string::npos + ? std::string( _attr.initialIndent, ' ' ) + : std::string( _attr.indent, ' ' ); + + typedef std::string::const_iterator iterator; + iterator it = _str.begin(); + const iterator strEnd = _str.end(); + + while( it != strEnd ) { + + if( lines.size() >= 1000 ) { + lines.push_back( "... message truncated due to excessive size" ); + return; + } + + std::string suffix; + std::size_t width = (std::min)( static_cast<size_t>( strEnd-it ), _attr.width-static_cast<size_t>( indent.size() ) ); + iterator itEnd = it+width; + iterator itNext = _str.end(); + + iterator itNewLine = std::find( it, itEnd, '\n' ); + if( itNewLine != itEnd ) + itEnd = itNewLine; + + if( itEnd != strEnd ) { + bool foundWrapPoint = false; + iterator findIt = itEnd; + do { + if( wrappableAfterChars.find( *findIt ) != std::string::npos && findIt != itEnd ) { + itEnd = findIt+1; + itNext = findIt+1; + foundWrapPoint = true; + } + else if( findIt > it && wrappableBeforeChars.find( *findIt ) != std::string::npos ) { + itEnd = findIt; + itNext = findIt; + foundWrapPoint = true; + } + else if( wrappableInsteadOfChars.find( *findIt ) != std::string::npos ) { + itNext = findIt+1; + itEnd = findIt; + foundWrapPoint = true; + } + if( findIt == it ) + break; + else + --findIt; + } + while( !foundWrapPoint ); + + if( !foundWrapPoint ) { + // No good wrap char, so we'll break mid word and add a hyphen + --itEnd; + itNext = itEnd; + suffix = "-"; + } + else { + while( itEnd > it && wrappableInsteadOfChars.find( *(itEnd-1) ) != std::string::npos ) + --itEnd; + } + } + lines.push_back( indent + std::string( it, itEnd ) + suffix ); + + if( indent.size() != _attr.indent ) + indent = std::string( _attr.indent, ' ' ); + it = itNext; + } + } + + typedef std::vector<std::string>::const_iterator const_iterator; + + const_iterator begin() const { return lines.begin(); } + const_iterator end() const { return lines.end(); } + std::string const& last() const { return lines.back(); } + std::size_t size() const { return lines.size(); } + std::string const& operator[]( std::size_t _index ) const { return lines[_index]; } + std::string toString() const { + std::ostringstream oss; + oss << *this; + return oss.str(); + } + + inline friend std::ostream& operator << ( std::ostream& _stream, Text const& _text ) { + for( Text::const_iterator it = _text.begin(), itEnd = _text.end(); + it != itEnd; ++it ) { + if( it != _text.begin() ) + _stream << "\n"; + _stream << *it; + } + return _stream; + } + + private: + std::string str; + TextAttributes attr; + std::vector<std::string> lines; + }; + +} // end namespace Tbc + +#ifdef CLICHE_TBC_TEXT_FORMAT_OUTER_NAMESPACE +} // end outer namespace +#endif + +#endif // TWOBLUECUBES_TEXT_FORMAT_H_ALREADY_INCLUDED +#undef CLICHE_TBC_TEXT_FORMAT_OUTER_NAMESPACE + +namespace Catch { + using Tbc::Text; + using Tbc::TextAttributes; +} + +// #included from: catch_console_colour.hpp +#define TWOBLUECUBES_CATCH_CONSOLE_COLOUR_HPP_INCLUDED + +namespace Catch { + + struct Colour { + enum Code { + None = 0, + + White, + Red, + Green, + Blue, + Cyan, + Yellow, + Grey, + + Bright = 0x10, + + BrightRed = Bright | Red, + BrightGreen = Bright | Green, + LightGrey = Bright | Grey, + BrightWhite = Bright | White, + + // By intention + FileName = LightGrey, + Warning = Yellow, + ResultError = BrightRed, + ResultSuccess = BrightGreen, + ResultExpectedFailure = Warning, + + Error = BrightRed, + Success = Green, + + OriginalExpression = Cyan, + ReconstructedExpression = Yellow, + + SecondaryText = LightGrey, + Headers = White + }; + + // Use constructed object for RAII guard + Colour( Code _colourCode ); + Colour( Colour const& other ); + ~Colour(); + + // Use static method for one-shot changes + static void use( Code _colourCode ); + + private: + bool m_moved; + }; + + inline std::ostream& operator << ( std::ostream& os, Colour const& ) { return os; } + +} // end namespace Catch + +// #included from: catch_interfaces_reporter.h +#define TWOBLUECUBES_CATCH_INTERFACES_REPORTER_H_INCLUDED + +#include <string> +#include <ostream> +#include <map> + +namespace Catch +{ + struct ReporterConfig { + explicit ReporterConfig( Ptr<IConfig const> const& _fullConfig ) + : m_stream( &_fullConfig->stream() ), m_fullConfig( _fullConfig ) {} + + ReporterConfig( Ptr<IConfig const> const& _fullConfig, std::ostream& _stream ) + : m_stream( &_stream ), m_fullConfig( _fullConfig ) {} + + std::ostream& stream() const { return *m_stream; } + Ptr<IConfig const> fullConfig() const { return m_fullConfig; } + + private: + std::ostream* m_stream; + Ptr<IConfig const> m_fullConfig; + }; + + struct ReporterPreferences { + ReporterPreferences() + : shouldRedirectStdOut( false ) + {} + + bool shouldRedirectStdOut; + }; + + template<typename T> + struct LazyStat : Option<T> { + LazyStat() : used( false ) {} + LazyStat& operator=( T const& _value ) { + Option<T>::operator=( _value ); + used = false; + return *this; + } + void reset() { + Option<T>::reset(); + used = false; + } + bool used; + }; + + struct TestRunInfo { + TestRunInfo( std::string const& _name ) : name( _name ) {} + std::string name; + }; + struct GroupInfo { + GroupInfo( std::string const& _name, + std::size_t _groupIndex, + std::size_t _groupsCount ) + : name( _name ), + groupIndex( _groupIndex ), + groupsCounts( _groupsCount ) + {} + + std::string name; + std::size_t groupIndex; + std::size_t groupsCounts; + }; + + struct AssertionStats { + AssertionStats( AssertionResult const& _assertionResult, + std::vector<MessageInfo> const& _infoMessages, + Totals const& _totals ) + : assertionResult( _assertionResult ), + infoMessages( _infoMessages ), + totals( _totals ) + { + if( assertionResult.hasMessage() ) { + // Copy message into messages list. + // !TBD This should have been done earlier, somewhere + MessageBuilder builder( assertionResult.getTestMacroName(), assertionResult.getSourceInfo(), assertionResult.getResultType() ); + builder << assertionResult.getMessage(); + builder.m_info.message = builder.m_stream.str(); + + infoMessages.push_back( builder.m_info ); + } + } + virtual ~AssertionStats(); + +# ifdef CATCH_CONFIG_CPP11_GENERATED_METHODS + AssertionStats( AssertionStats const& ) = default; + AssertionStats( AssertionStats && ) = default; + AssertionStats& operator = ( AssertionStats const& ) = default; + AssertionStats& operator = ( AssertionStats && ) = default; +# endif + + AssertionResult assertionResult; + std::vector<MessageInfo> infoMessages; + Totals totals; + }; + + struct SectionStats { + SectionStats( SectionInfo const& _sectionInfo, + Counts const& _assertions, + double _durationInSeconds, + bool _missingAssertions ) + : sectionInfo( _sectionInfo ), + assertions( _assertions ), + durationInSeconds( _durationInSeconds ), + missingAssertions( _missingAssertions ) + {} + virtual ~SectionStats(); +# ifdef CATCH_CONFIG_CPP11_GENERATED_METHODS + SectionStats( SectionStats const& ) = default; + SectionStats( SectionStats && ) = default; + SectionStats& operator = ( SectionStats const& ) = default; + SectionStats& operator = ( SectionStats && ) = default; +# endif + + SectionInfo sectionInfo; + Counts assertions; + double durationInSeconds; + bool missingAssertions; + }; + + struct TestCaseStats { + TestCaseStats( TestCaseInfo const& _testInfo, + Totals const& _totals, + std::string const& _stdOut, + std::string const& _stdErr, + bool _aborting ) + : testInfo( _testInfo ), + totals( _totals ), + stdOut( _stdOut ), + stdErr( _stdErr ), + aborting( _aborting ) + {} + virtual ~TestCaseStats(); + +# ifdef CATCH_CONFIG_CPP11_GENERATED_METHODS + TestCaseStats( TestCaseStats const& ) = default; + TestCaseStats( TestCaseStats && ) = default; + TestCaseStats& operator = ( TestCaseStats const& ) = default; + TestCaseStats& operator = ( TestCaseStats && ) = default; +# endif + + TestCaseInfo testInfo; + Totals totals; + std::string stdOut; + std::string stdErr; + bool aborting; + }; + + struct TestGroupStats { + TestGroupStats( GroupInfo const& _groupInfo, + Totals const& _totals, + bool _aborting ) + : groupInfo( _groupInfo ), + totals( _totals ), + aborting( _aborting ) + {} + TestGroupStats( GroupInfo const& _groupInfo ) + : groupInfo( _groupInfo ), + aborting( false ) + {} + virtual ~TestGroupStats(); + +# ifdef CATCH_CONFIG_CPP11_GENERATED_METHODS + TestGroupStats( TestGroupStats const& ) = default; + TestGroupStats( TestGroupStats && ) = default; + TestGroupStats& operator = ( TestGroupStats const& ) = default; + TestGroupStats& operator = ( TestGroupStats && ) = default; +# endif + + GroupInfo groupInfo; + Totals totals; + bool aborting; + }; + + struct TestRunStats { + TestRunStats( TestRunInfo const& _runInfo, + Totals const& _totals, + bool _aborting ) + : runInfo( _runInfo ), + totals( _totals ), + aborting( _aborting ) + {} + virtual ~TestRunStats(); + +# ifndef CATCH_CONFIG_CPP11_GENERATED_METHODS + TestRunStats( TestRunStats const& _other ) + : runInfo( _other.runInfo ), + totals( _other.totals ), + aborting( _other.aborting ) + {} +# else + TestRunStats( TestRunStats const& ) = default; + TestRunStats( TestRunStats && ) = default; + TestRunStats& operator = ( TestRunStats const& ) = default; + TestRunStats& operator = ( TestRunStats && ) = default; +# endif + + TestRunInfo runInfo; + Totals totals; + bool aborting; + }; + + class MultipleReporters; + + struct IStreamingReporter : IShared { + virtual ~IStreamingReporter(); + + // Implementing class must also provide the following static method: + // static std::string getDescription(); + + virtual ReporterPreferences getPreferences() const = 0; + + virtual void noMatchingTestCases( std::string const& spec ) = 0; + + virtual void testRunStarting( TestRunInfo const& testRunInfo ) = 0; + virtual void testGroupStarting( GroupInfo const& groupInfo ) = 0; + + virtual void testCaseStarting( TestCaseInfo const& testInfo ) = 0; + virtual void sectionStarting( SectionInfo const& sectionInfo ) = 0; + + virtual void assertionStarting( AssertionInfo const& assertionInfo ) = 0; + + // The return value indicates if the messages buffer should be cleared: + virtual bool assertionEnded( AssertionStats const& assertionStats ) = 0; + + virtual void sectionEnded( SectionStats const& sectionStats ) = 0; + virtual void testCaseEnded( TestCaseStats const& testCaseStats ) = 0; + virtual void testGroupEnded( TestGroupStats const& testGroupStats ) = 0; + virtual void testRunEnded( TestRunStats const& testRunStats ) = 0; + + virtual void skipTest( TestCaseInfo const& testInfo ) = 0; + + virtual MultipleReporters* tryAsMulti() { return CATCH_NULL; } + }; + + struct IReporterFactory : IShared { + virtual ~IReporterFactory(); + virtual IStreamingReporter* create( ReporterConfig const& config ) const = 0; + virtual std::string getDescription() const = 0; + }; + + struct IReporterRegistry { + typedef std::map<std::string, Ptr<IReporterFactory> > FactoryMap; + typedef std::vector<Ptr<IReporterFactory> > Listeners; + + virtual ~IReporterRegistry(); + virtual IStreamingReporter* create( std::string const& name, Ptr<IConfig const> const& config ) const = 0; + virtual FactoryMap const& getFactories() const = 0; + virtual Listeners const& getListeners() const = 0; + }; + + Ptr<IStreamingReporter> addReporter( Ptr<IStreamingReporter> const& existingReporter, Ptr<IStreamingReporter> const& additionalReporter ); + +} + +#include <limits> +#include <algorithm> + +namespace Catch { + + inline std::size_t listTests( Config const& config ) { + + TestSpec testSpec = config.testSpec(); + if( config.testSpec().hasFilters() ) + Catch::cout() << "Matching test cases:\n"; + else { + Catch::cout() << "All available test cases:\n"; + testSpec = TestSpecParser( ITagAliasRegistry::get() ).parse( "*" ).testSpec(); + } + + std::size_t matchedTests = 0; + TextAttributes nameAttr, tagsAttr; + nameAttr.setInitialIndent( 2 ).setIndent( 4 ); + tagsAttr.setIndent( 6 ); + + std::vector<TestCase> matchedTestCases = filterTests( getAllTestCasesSorted( config ), testSpec, config ); + for( std::vector<TestCase>::const_iterator it = matchedTestCases.begin(), itEnd = matchedTestCases.end(); + it != itEnd; + ++it ) { + matchedTests++; + TestCaseInfo const& testCaseInfo = it->getTestCaseInfo(); + Colour::Code colour = testCaseInfo.isHidden() + ? Colour::SecondaryText + : Colour::None; + Colour colourGuard( colour ); + + Catch::cout() << Text( testCaseInfo.name, nameAttr ) << std::endl; + if( !testCaseInfo.tags.empty() ) + Catch::cout() << Text( testCaseInfo.tagsAsString, tagsAttr ) << std::endl; + } + + if( !config.testSpec().hasFilters() ) + Catch::cout() << pluralise( matchedTests, "test case" ) << '\n' << std::endl; + else + Catch::cout() << pluralise( matchedTests, "matching test case" ) << '\n' << std::endl; + return matchedTests; + } + + inline std::size_t listTestsNamesOnly( Config const& config ) { + TestSpec testSpec = config.testSpec(); + if( !config.testSpec().hasFilters() ) + testSpec = TestSpecParser( ITagAliasRegistry::get() ).parse( "*" ).testSpec(); + std::size_t matchedTests = 0; + std::vector<TestCase> matchedTestCases = filterTests( getAllTestCasesSorted( config ), testSpec, config ); + for( std::vector<TestCase>::const_iterator it = matchedTestCases.begin(), itEnd = matchedTestCases.end(); + it != itEnd; + ++it ) { + matchedTests++; + TestCaseInfo const& testCaseInfo = it->getTestCaseInfo(); + if( startsWith( testCaseInfo.name, '#' ) ) + Catch::cout() << '"' << testCaseInfo.name << '"' << std::endl; + else + Catch::cout() << testCaseInfo.name << std::endl; + } + return matchedTests; + } + + struct TagInfo { + TagInfo() : count ( 0 ) {} + void add( std::string const& spelling ) { + ++count; + spellings.insert( spelling ); + } + std::string all() const { + std::string out; + for( std::set<std::string>::const_iterator it = spellings.begin(), itEnd = spellings.end(); + it != itEnd; + ++it ) + out += "[" + *it + "]"; + return out; + } + std::set<std::string> spellings; + std::size_t count; + }; + + inline std::size_t listTags( Config const& config ) { + TestSpec testSpec = config.testSpec(); + if( config.testSpec().hasFilters() ) + Catch::cout() << "Tags for matching test cases:\n"; + else { + Catch::cout() << "All available tags:\n"; + testSpec = TestSpecParser( ITagAliasRegistry::get() ).parse( "*" ).testSpec(); + } + + std::map<std::string, TagInfo> tagCounts; + + std::vector<TestCase> matchedTestCases = filterTests( getAllTestCasesSorted( config ), testSpec, config ); + for( std::vector<TestCase>::const_iterator it = matchedTestCases.begin(), itEnd = matchedTestCases.end(); + it != itEnd; + ++it ) { + for( std::set<std::string>::const_iterator tagIt = it->getTestCaseInfo().tags.begin(), + tagItEnd = it->getTestCaseInfo().tags.end(); + tagIt != tagItEnd; + ++tagIt ) { + std::string tagName = *tagIt; + std::string lcaseTagName = toLower( tagName ); + std::map<std::string, TagInfo>::iterator countIt = tagCounts.find( lcaseTagName ); + if( countIt == tagCounts.end() ) + countIt = tagCounts.insert( std::make_pair( lcaseTagName, TagInfo() ) ).first; + countIt->second.add( tagName ); + } + } + + for( std::map<std::string, TagInfo>::const_iterator countIt = tagCounts.begin(), + countItEnd = tagCounts.end(); + countIt != countItEnd; + ++countIt ) { + std::ostringstream oss; + oss << " " << std::setw(2) << countIt->second.count << " "; + Text wrapper( countIt->second.all(), TextAttributes() + .setInitialIndent( 0 ) + .setIndent( oss.str().size() ) + .setWidth( CATCH_CONFIG_CONSOLE_WIDTH-10 ) ); + Catch::cout() << oss.str() << wrapper << '\n'; + } + Catch::cout() << pluralise( tagCounts.size(), "tag" ) << '\n' << std::endl; + return tagCounts.size(); + } + + inline std::size_t listReporters( Config const& /*config*/ ) { + Catch::cout() << "Available reporters:\n"; + IReporterRegistry::FactoryMap const& factories = getRegistryHub().getReporterRegistry().getFactories(); + IReporterRegistry::FactoryMap::const_iterator itBegin = factories.begin(), itEnd = factories.end(), it; + std::size_t maxNameLen = 0; + for(it = itBegin; it != itEnd; ++it ) + maxNameLen = (std::max)( maxNameLen, it->first.size() ); + + for(it = itBegin; it != itEnd; ++it ) { + Text wrapper( it->second->getDescription(), TextAttributes() + .setInitialIndent( 0 ) + .setIndent( 7+maxNameLen ) + .setWidth( CATCH_CONFIG_CONSOLE_WIDTH - maxNameLen-8 ) ); + Catch::cout() << " " + << it->first + << ':' + << std::string( maxNameLen - it->first.size() + 2, ' ' ) + << wrapper << '\n'; + } + Catch::cout() << std::endl; + return factories.size(); + } + + inline Option<std::size_t> list( Config const& config ) { + Option<std::size_t> listedCount; + if( config.listTests() ) + listedCount = listedCount.valueOr(0) + listTests( config ); + if( config.listTestNamesOnly() ) + listedCount = listedCount.valueOr(0) + listTestsNamesOnly( config ); + if( config.listTags() ) + listedCount = listedCount.valueOr(0) + listTags( config ); + if( config.listReporters() ) + listedCount = listedCount.valueOr(0) + listReporters( config ); + return listedCount; + } + +} // end namespace Catch + +// #included from: internal/catch_run_context.hpp +#define TWOBLUECUBES_CATCH_RUNNER_IMPL_HPP_INCLUDED + +// #included from: catch_test_case_tracker.hpp +#define TWOBLUECUBES_CATCH_TEST_CASE_TRACKER_HPP_INCLUDED + +#include <map> +#include <string> +#include <assert.h> +#include <vector> +#include <iterator> +#include <stdexcept> + +namespace Catch { +namespace TestCaseTracking { + + struct NameAndLocation { + std::string name; + SourceLineInfo location; + + NameAndLocation( std::string const& _name, SourceLineInfo const& _location ) + : name( _name ), + location( _location ) + {} + }; + + struct ITracker : SharedImpl<> { + virtual ~ITracker(); + + // static queries + virtual NameAndLocation const& nameAndLocation() const = 0; + + // dynamic queries + virtual bool isComplete() const = 0; // Successfully completed or failed + virtual bool isSuccessfullyCompleted() const = 0; + virtual bool isOpen() const = 0; // Started but not complete + virtual bool hasChildren() const = 0; + + virtual ITracker& parent() = 0; + + // actions + virtual void close() = 0; // Successfully complete + virtual void fail() = 0; + virtual void markAsNeedingAnotherRun() = 0; + + virtual void addChild( Ptr<ITracker> const& child ) = 0; + virtual ITracker* findChild( NameAndLocation const& nameAndLocation ) = 0; + virtual void openChild() = 0; + + // Debug/ checking + virtual bool isSectionTracker() const = 0; + virtual bool isIndexTracker() const = 0; + }; + + class TrackerContext { + + enum RunState { + NotStarted, + Executing, + CompletedCycle + }; + + Ptr<ITracker> m_rootTracker; + ITracker* m_currentTracker; + RunState m_runState; + + public: + + static TrackerContext& instance() { + static TrackerContext s_instance; + return s_instance; + } + + TrackerContext() + : m_currentTracker( CATCH_NULL ), + m_runState( NotStarted ) + {} + + ITracker& startRun(); + + void endRun() { + m_rootTracker.reset(); + m_currentTracker = CATCH_NULL; + m_runState = NotStarted; + } + + void startCycle() { + m_currentTracker = m_rootTracker.get(); + m_runState = Executing; + } + void completeCycle() { + m_runState = CompletedCycle; + } + + bool completedCycle() const { + return m_runState == CompletedCycle; + } + ITracker& currentTracker() { + return *m_currentTracker; + } + void setCurrentTracker( ITracker* tracker ) { + m_currentTracker = tracker; + } + }; + + class TrackerBase : public ITracker { + protected: + enum CycleState { + NotStarted, + Executing, + ExecutingChildren, + NeedsAnotherRun, + CompletedSuccessfully, + Failed + }; + class TrackerHasName { + NameAndLocation m_nameAndLocation; + public: + TrackerHasName( NameAndLocation const& nameAndLocation ) : m_nameAndLocation( nameAndLocation ) {} + bool operator ()( Ptr<ITracker> const& tracker ) { + return + tracker->nameAndLocation().name == m_nameAndLocation.name && + tracker->nameAndLocation().location == m_nameAndLocation.location; + } + }; + typedef std::vector<Ptr<ITracker> > Children; + NameAndLocation m_nameAndLocation; + TrackerContext& m_ctx; + ITracker* m_parent; + Children m_children; + CycleState m_runState; + public: + TrackerBase( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent ) + : m_nameAndLocation( nameAndLocation ), + m_ctx( ctx ), + m_parent( parent ), + m_runState( NotStarted ) + {} + virtual ~TrackerBase(); + + virtual NameAndLocation const& nameAndLocation() const CATCH_OVERRIDE { + return m_nameAndLocation; + } + virtual bool isComplete() const CATCH_OVERRIDE { + return m_runState == CompletedSuccessfully || m_runState == Failed; + } + virtual bool isSuccessfullyCompleted() const CATCH_OVERRIDE { + return m_runState == CompletedSuccessfully; + } + virtual bool isOpen() const CATCH_OVERRIDE { + return m_runState != NotStarted && !isComplete(); + } + virtual bool hasChildren() const CATCH_OVERRIDE { + return !m_children.empty(); + } + + virtual void addChild( Ptr<ITracker> const& child ) CATCH_OVERRIDE { + m_children.push_back( child ); + } + + virtual ITracker* findChild( NameAndLocation const& nameAndLocation ) CATCH_OVERRIDE { + Children::const_iterator it = std::find_if( m_children.begin(), m_children.end(), TrackerHasName( nameAndLocation ) ); + return( it != m_children.end() ) + ? it->get() + : CATCH_NULL; + } + virtual ITracker& parent() CATCH_OVERRIDE { + assert( m_parent ); // Should always be non-null except for root + return *m_parent; + } + + virtual void openChild() CATCH_OVERRIDE { + if( m_runState != ExecutingChildren ) { + m_runState = ExecutingChildren; + if( m_parent ) + m_parent->openChild(); + } + } + + virtual bool isSectionTracker() const CATCH_OVERRIDE { return false; } + virtual bool isIndexTracker() const CATCH_OVERRIDE { return false; } + + void open() { + m_runState = Executing; + moveToThis(); + if( m_parent ) + m_parent->openChild(); + } + + virtual void close() CATCH_OVERRIDE { + + // Close any still open children (e.g. generators) + while( &m_ctx.currentTracker() != this ) + m_ctx.currentTracker().close(); + + switch( m_runState ) { + case NotStarted: + case CompletedSuccessfully: + case Failed: + throw std::logic_error( "Illogical state" ); + + case NeedsAnotherRun: + break;; + + case Executing: + m_runState = CompletedSuccessfully; + break; + case ExecutingChildren: + if( m_children.empty() || m_children.back()->isComplete() ) + m_runState = CompletedSuccessfully; + break; + + default: + throw std::logic_error( "Unexpected state" ); + } + moveToParent(); + m_ctx.completeCycle(); + } + virtual void fail() CATCH_OVERRIDE { + m_runState = Failed; + if( m_parent ) + m_parent->markAsNeedingAnotherRun(); + moveToParent(); + m_ctx.completeCycle(); + } + virtual void markAsNeedingAnotherRun() CATCH_OVERRIDE { + m_runState = NeedsAnotherRun; + } + private: + void moveToParent() { + assert( m_parent ); + m_ctx.setCurrentTracker( m_parent ); + } + void moveToThis() { + m_ctx.setCurrentTracker( this ); + } + }; + + class SectionTracker : public TrackerBase { + std::vector<std::string> m_filters; + public: + SectionTracker( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent ) + : TrackerBase( nameAndLocation, ctx, parent ) + { + if( parent ) { + while( !parent->isSectionTracker() ) + parent = &parent->parent(); + + SectionTracker& parentSection = static_cast<SectionTracker&>( *parent ); + addNextFilters( parentSection.m_filters ); + } + } + virtual ~SectionTracker(); + + virtual bool isSectionTracker() const CATCH_OVERRIDE { return true; } + + static SectionTracker& acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation ) { + SectionTracker* section = CATCH_NULL; + + ITracker& currentTracker = ctx.currentTracker(); + if( ITracker* childTracker = currentTracker.findChild( nameAndLocation ) ) { + assert( childTracker ); + assert( childTracker->isSectionTracker() ); + section = static_cast<SectionTracker*>( childTracker ); + } + else { + section = new SectionTracker( nameAndLocation, ctx, ¤tTracker ); + currentTracker.addChild( section ); + } + if( !ctx.completedCycle() ) + section->tryOpen(); + return *section; + } + + void tryOpen() { + if( !isComplete() && (m_filters.empty() || m_filters[0].empty() || m_filters[0] == m_nameAndLocation.name ) ) + open(); + } + + void addInitialFilters( std::vector<std::string> const& filters ) { + if( !filters.empty() ) { + m_filters.push_back(""); // Root - should never be consulted + m_filters.push_back(""); // Test Case - not a section filter + std::copy( filters.begin(), filters.end(), std::back_inserter( m_filters ) ); + } + } + void addNextFilters( std::vector<std::string> const& filters ) { + if( filters.size() > 1 ) + std::copy( filters.begin()+1, filters.end(), std::back_inserter( m_filters ) ); + } + }; + + class IndexTracker : public TrackerBase { + int m_size; + int m_index; + public: + IndexTracker( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent, int size ) + : TrackerBase( nameAndLocation, ctx, parent ), + m_size( size ), + m_index( -1 ) + {} + virtual ~IndexTracker(); + + virtual bool isIndexTracker() const CATCH_OVERRIDE { return true; } + + static IndexTracker& acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation, int size ) { + IndexTracker* tracker = CATCH_NULL; + + ITracker& currentTracker = ctx.currentTracker(); + if( ITracker* childTracker = currentTracker.findChild( nameAndLocation ) ) { + assert( childTracker ); + assert( childTracker->isIndexTracker() ); + tracker = static_cast<IndexTracker*>( childTracker ); + } + else { + tracker = new IndexTracker( nameAndLocation, ctx, ¤tTracker, size ); + currentTracker.addChild( tracker ); + } + + if( !ctx.completedCycle() && !tracker->isComplete() ) { + if( tracker->m_runState != ExecutingChildren && tracker->m_runState != NeedsAnotherRun ) + tracker->moveNext(); + tracker->open(); + } + + return *tracker; + } + + int index() const { return m_index; } + + void moveNext() { + m_index++; + m_children.clear(); + } + + virtual void close() CATCH_OVERRIDE { + TrackerBase::close(); + if( m_runState == CompletedSuccessfully && m_index < m_size-1 ) + m_runState = Executing; + } + }; + + inline ITracker& TrackerContext::startRun() { + m_rootTracker = new SectionTracker( NameAndLocation( "{root}", CATCH_INTERNAL_LINEINFO ), *this, CATCH_NULL ); + m_currentTracker = CATCH_NULL; + m_runState = Executing; + return *m_rootTracker; + } + +} // namespace TestCaseTracking + +using TestCaseTracking::ITracker; +using TestCaseTracking::TrackerContext; +using TestCaseTracking::SectionTracker; +using TestCaseTracking::IndexTracker; + +} // namespace Catch + +// #included from: catch_fatal_condition.hpp +#define TWOBLUECUBES_CATCH_FATAL_CONDITION_H_INCLUDED + +namespace Catch { + + // Report the error condition + inline void reportFatal( std::string const& message ) { + IContext& context = Catch::getCurrentContext(); + IResultCapture* resultCapture = context.getResultCapture(); + resultCapture->handleFatalErrorCondition( message ); + } + +} // namespace Catch + +#if defined ( CATCH_PLATFORM_WINDOWS ) ///////////////////////////////////////// +// #included from: catch_windows_h_proxy.h + +#define TWOBLUECUBES_CATCH_WINDOWS_H_PROXY_H_INCLUDED + +#ifdef CATCH_DEFINES_NOMINMAX +# define NOMINMAX +#endif +#ifdef CATCH_DEFINES_WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +#endif + +#ifdef __AFXDLL +#include <AfxWin.h> +#else +#include <windows.h> +#endif + +#ifdef CATCH_DEFINES_NOMINMAX +# undef NOMINMAX +#endif +#ifdef CATCH_DEFINES_WIN32_LEAN_AND_MEAN +# undef WIN32_LEAN_AND_MEAN +#endif + + +# if !defined ( CATCH_CONFIG_WINDOWS_SEH ) + +namespace Catch { + struct FatalConditionHandler { + void reset() {} + }; +} + +# else // CATCH_CONFIG_WINDOWS_SEH is defined + +namespace Catch { + + struct SignalDefs { DWORD id; const char* name; }; + extern SignalDefs signalDefs[]; + // There is no 1-1 mapping between signals and windows exceptions. + // Windows can easily distinguish between SO and SigSegV, + // but SigInt, SigTerm, etc are handled differently. + SignalDefs signalDefs[] = { + { EXCEPTION_ILLEGAL_INSTRUCTION, "SIGILL - Illegal instruction signal" }, + { EXCEPTION_STACK_OVERFLOW, "SIGSEGV - Stack overflow" }, + { EXCEPTION_ACCESS_VIOLATION, "SIGSEGV - Segmentation violation signal" }, + { EXCEPTION_INT_DIVIDE_BY_ZERO, "Divide by zero error" }, + }; + + struct FatalConditionHandler { + + static LONG CALLBACK handleVectoredException(PEXCEPTION_POINTERS ExceptionInfo) { + for (int i = 0; i < sizeof(signalDefs) / sizeof(SignalDefs); ++i) { + if (ExceptionInfo->ExceptionRecord->ExceptionCode == signalDefs[i].id) { + reset(); + reportFatal(signalDefs[i].name); + } + } + // If its not an exception we care about, pass it along. + // This stops us from eating debugger breaks etc. + return EXCEPTION_CONTINUE_SEARCH; + } + + FatalConditionHandler() { + isSet = true; + // 32k seems enough for Catch to handle stack overflow, + // but the value was found experimentally, so there is no strong guarantee + guaranteeSize = 32 * 1024; + exceptionHandlerHandle = CATCH_NULL; + // Register as first handler in current chain + exceptionHandlerHandle = AddVectoredExceptionHandler(1, handleVectoredException); + // Pass in guarantee size to be filled + SetThreadStackGuarantee(&guaranteeSize); + } + + static void reset() { + if (isSet) { + // Unregister handler and restore the old guarantee + RemoveVectoredExceptionHandler(exceptionHandlerHandle); + SetThreadStackGuarantee(&guaranteeSize); + exceptionHandlerHandle = CATCH_NULL; + isSet = false; + } + } + + ~FatalConditionHandler() { + reset(); + } + private: + static bool isSet; + static ULONG guaranteeSize; + static PVOID exceptionHandlerHandle; + }; + + bool FatalConditionHandler::isSet = false; + ULONG FatalConditionHandler::guaranteeSize = 0; + PVOID FatalConditionHandler::exceptionHandlerHandle = CATCH_NULL; + +} // namespace Catch + +# endif // CATCH_CONFIG_WINDOWS_SEH + +#else // Not Windows - assumed to be POSIX compatible ////////////////////////// + +# if !defined(CATCH_CONFIG_POSIX_SIGNALS) + +namespace Catch { + struct FatalConditionHandler { + void reset() {} + }; +} + +# else // CATCH_CONFIG_POSIX_SIGNALS is defined + +#include <signal.h> + +namespace Catch { + + struct SignalDefs { + int id; + const char* name; + }; + extern SignalDefs signalDefs[]; + SignalDefs signalDefs[] = { + { SIGINT, "SIGINT - Terminal interrupt signal" }, + { SIGILL, "SIGILL - Illegal instruction signal" }, + { SIGFPE, "SIGFPE - Floating point error signal" }, + { SIGSEGV, "SIGSEGV - Segmentation violation signal" }, + { SIGTERM, "SIGTERM - Termination request signal" }, + { SIGABRT, "SIGABRT - Abort (abnormal termination) signal" } + }; + + struct FatalConditionHandler { + + static bool isSet; + static struct sigaction oldSigActions [sizeof(signalDefs)/sizeof(SignalDefs)]; + static stack_t oldSigStack; + static char altStackMem[SIGSTKSZ]; + + static void handleSignal( int sig ) { + std::string name = "<unknown signal>"; + for (std::size_t i = 0; i < sizeof(signalDefs) / sizeof(SignalDefs); ++i) { + SignalDefs &def = signalDefs[i]; + if (sig == def.id) { + name = def.name; + break; + } + } + reset(); + reportFatal(name); + raise( sig ); + } + + FatalConditionHandler() { + isSet = true; + stack_t sigStack; + sigStack.ss_sp = altStackMem; + sigStack.ss_size = SIGSTKSZ; + sigStack.ss_flags = 0; + sigaltstack(&sigStack, &oldSigStack); + struct sigaction sa = { 0 }; + + sa.sa_handler = handleSignal; + sa.sa_flags = SA_ONSTACK; + for (std::size_t i = 0; i < sizeof(signalDefs)/sizeof(SignalDefs); ++i) { + sigaction(signalDefs[i].id, &sa, &oldSigActions[i]); + } + } + + ~FatalConditionHandler() { + reset(); + } + static void reset() { + if( isSet ) { + // Set signals back to previous values -- hopefully nobody overwrote them in the meantime + for( std::size_t i = 0; i < sizeof(signalDefs)/sizeof(SignalDefs); ++i ) { + sigaction(signalDefs[i].id, &oldSigActions[i], CATCH_NULL); + } + // Return the old stack + sigaltstack(&oldSigStack, CATCH_NULL); + isSet = false; + } + } + }; + + bool FatalConditionHandler::isSet = false; + struct sigaction FatalConditionHandler::oldSigActions[sizeof(signalDefs)/sizeof(SignalDefs)] = {}; + stack_t FatalConditionHandler::oldSigStack = {}; + char FatalConditionHandler::altStackMem[SIGSTKSZ] = {}; + +} // namespace Catch + +# endif // CATCH_CONFIG_POSIX_SIGNALS + +#endif // not Windows + +#include <set> +#include <string> + +namespace Catch { + + class StreamRedirect { + + public: + StreamRedirect( std::ostream& stream, std::string& targetString ) + : m_stream( stream ), + m_prevBuf( stream.rdbuf() ), + m_targetString( targetString ) + { + stream.rdbuf( m_oss.rdbuf() ); + } + + ~StreamRedirect() { + m_targetString += m_oss.str(); + m_stream.rdbuf( m_prevBuf ); + } + + private: + std::ostream& m_stream; + std::streambuf* m_prevBuf; + std::ostringstream m_oss; + std::string& m_targetString; + }; + + /////////////////////////////////////////////////////////////////////////// + + class RunContext : public IResultCapture, public IRunner { + + RunContext( RunContext const& ); + void operator =( RunContext const& ); + + public: + + explicit RunContext( Ptr<IConfig const> const& _config, Ptr<IStreamingReporter> const& reporter ) + : m_runInfo( _config->name() ), + m_context( getCurrentMutableContext() ), + m_activeTestCase( CATCH_NULL ), + m_config( _config ), + m_reporter( reporter ) + { + m_context.setRunner( this ); + m_context.setConfig( m_config ); + m_context.setResultCapture( this ); + m_reporter->testRunStarting( m_runInfo ); + } + + virtual ~RunContext() { + m_reporter->testRunEnded( TestRunStats( m_runInfo, m_totals, aborting() ) ); + } + + void testGroupStarting( std::string const& testSpec, std::size_t groupIndex, std::size_t groupsCount ) { + m_reporter->testGroupStarting( GroupInfo( testSpec, groupIndex, groupsCount ) ); + } + void testGroupEnded( std::string const& testSpec, Totals const& totals, std::size_t groupIndex, std::size_t groupsCount ) { + m_reporter->testGroupEnded( TestGroupStats( GroupInfo( testSpec, groupIndex, groupsCount ), totals, aborting() ) ); + } + + Totals runTest( TestCase const& testCase ) { + Totals prevTotals = m_totals; + + std::string redirectedCout; + std::string redirectedCerr; + + TestCaseInfo testInfo = testCase.getTestCaseInfo(); + + m_reporter->testCaseStarting( testInfo ); + + m_activeTestCase = &testCase; + + do { + ITracker& rootTracker = m_trackerContext.startRun(); + assert( rootTracker.isSectionTracker() ); + static_cast<SectionTracker&>( rootTracker ).addInitialFilters( m_config->getSectionsToRun() ); + do { + m_trackerContext.startCycle(); + m_testCaseTracker = &SectionTracker::acquire( m_trackerContext, TestCaseTracking::NameAndLocation( testInfo.name, testInfo.lineInfo ) ); + runCurrentTest( redirectedCout, redirectedCerr ); + } + while( !m_testCaseTracker->isSuccessfullyCompleted() && !aborting() ); + } + // !TBD: deprecated - this will be replaced by indexed trackers + while( getCurrentContext().advanceGeneratorsForCurrentTest() && !aborting() ); + + Totals deltaTotals = m_totals.delta( prevTotals ); + if( testInfo.expectedToFail() && deltaTotals.testCases.passed > 0 ) { + deltaTotals.assertions.failed++; + deltaTotals.testCases.passed--; + deltaTotals.testCases.failed++; + } + m_totals.testCases += deltaTotals.testCases; + m_reporter->testCaseEnded( TestCaseStats( testInfo, + deltaTotals, + redirectedCout, + redirectedCerr, + aborting() ) ); + + m_activeTestCase = CATCH_NULL; + m_testCaseTracker = CATCH_NULL; + + return deltaTotals; + } + + Ptr<IConfig const> config() const { + return m_config; + } + + private: // IResultCapture + + virtual void assertionEnded( AssertionResult const& result ) { + if( result.getResultType() == ResultWas::Ok ) { + m_totals.assertions.passed++; + } + else if( !result.isOk() ) { + m_totals.assertions.failed++; + } + + if( m_reporter->assertionEnded( AssertionStats( result, m_messages, m_totals ) ) ) + m_messages.clear(); + + // Reset working state + m_lastAssertionInfo = AssertionInfo( std::string(), m_lastAssertionInfo.lineInfo, "{Unknown expression after the reported line}" , m_lastAssertionInfo.resultDisposition ); + m_lastResult = result; + } + + virtual bool sectionStarted ( + SectionInfo const& sectionInfo, + Counts& assertions + ) + { + ITracker& sectionTracker = SectionTracker::acquire( m_trackerContext, TestCaseTracking::NameAndLocation( sectionInfo.name, sectionInfo.lineInfo ) ); + if( !sectionTracker.isOpen() ) + return false; + m_activeSections.push_back( §ionTracker ); + + m_lastAssertionInfo.lineInfo = sectionInfo.lineInfo; + + m_reporter->sectionStarting( sectionInfo ); + + assertions = m_totals.assertions; + + return true; + } + bool testForMissingAssertions( Counts& assertions ) { + if( assertions.total() != 0 ) + return false; + if( !m_config->warnAboutMissingAssertions() ) + return false; + if( m_trackerContext.currentTracker().hasChildren() ) + return false; + m_totals.assertions.failed++; + assertions.failed++; + return true; + } + + virtual void sectionEnded( SectionEndInfo const& endInfo ) { + Counts assertions = m_totals.assertions - endInfo.prevAssertions; + bool missingAssertions = testForMissingAssertions( assertions ); + + if( !m_activeSections.empty() ) { + m_activeSections.back()->close(); + m_activeSections.pop_back(); + } + + m_reporter->sectionEnded( SectionStats( endInfo.sectionInfo, assertions, endInfo.durationInSeconds, missingAssertions ) ); + m_messages.clear(); + } + + virtual void sectionEndedEarly( SectionEndInfo const& endInfo ) { + if( m_unfinishedSections.empty() ) + m_activeSections.back()->fail(); + else + m_activeSections.back()->close(); + m_activeSections.pop_back(); + + m_unfinishedSections.push_back( endInfo ); + } + + virtual void pushScopedMessage( MessageInfo const& message ) { + m_messages.push_back( message ); + } + + virtual void popScopedMessage( MessageInfo const& message ) { + m_messages.erase( std::remove( m_messages.begin(), m_messages.end(), message ), m_messages.end() ); + } + + virtual std::string getCurrentTestName() const { + return m_activeTestCase + ? m_activeTestCase->getTestCaseInfo().name + : std::string(); + } + + virtual const AssertionResult* getLastResult() const { + return &m_lastResult; + } + + virtual void handleFatalErrorCondition( std::string const& message ) { + ResultBuilder resultBuilder = makeUnexpectedResultBuilder(); + resultBuilder.setResultType( ResultWas::FatalErrorCondition ); + resultBuilder << message; + resultBuilder.captureExpression(); + + handleUnfinishedSections(); + + // Recreate section for test case (as we will lose the one that was in scope) + TestCaseInfo const& testCaseInfo = m_activeTestCase->getTestCaseInfo(); + SectionInfo testCaseSection( testCaseInfo.lineInfo, testCaseInfo.name, testCaseInfo.description ); + + Counts assertions; + assertions.failed = 1; + SectionStats testCaseSectionStats( testCaseSection, assertions, 0, false ); + m_reporter->sectionEnded( testCaseSectionStats ); + + TestCaseInfo testInfo = m_activeTestCase->getTestCaseInfo(); + + Totals deltaTotals; + deltaTotals.testCases.failed = 1; + m_reporter->testCaseEnded( TestCaseStats( testInfo, + deltaTotals, + std::string(), + std::string(), + false ) ); + m_totals.testCases.failed++; + testGroupEnded( std::string(), m_totals, 1, 1 ); + m_reporter->testRunEnded( TestRunStats( m_runInfo, m_totals, false ) ); + } + + public: + // !TBD We need to do this another way! + bool aborting() const { + return m_totals.assertions.failed == static_cast<std::size_t>( m_config->abortAfter() ); + } + + private: + + void runCurrentTest( std::string& redirectedCout, std::string& redirectedCerr ) { + TestCaseInfo const& testCaseInfo = m_activeTestCase->getTestCaseInfo(); + SectionInfo testCaseSection( testCaseInfo.lineInfo, testCaseInfo.name, testCaseInfo.description ); + m_reporter->sectionStarting( testCaseSection ); + Counts prevAssertions = m_totals.assertions; + double duration = 0; + try { + m_lastAssertionInfo = AssertionInfo( "TEST_CASE", testCaseInfo.lineInfo, std::string(), ResultDisposition::Normal ); + + seedRng( *m_config ); + + Timer timer; + timer.start(); + if( m_reporter->getPreferences().shouldRedirectStdOut ) { + StreamRedirect coutRedir( Catch::cout(), redirectedCout ); + StreamRedirect cerrRedir( Catch::cerr(), redirectedCerr ); + invokeActiveTestCase(); + } + else { + invokeActiveTestCase(); + } + duration = timer.getElapsedSeconds(); + } + catch( TestFailureException& ) { + // This just means the test was aborted due to failure + } + catch(...) { + makeUnexpectedResultBuilder().useActiveException(); + } + m_testCaseTracker->close(); + handleUnfinishedSections(); + m_messages.clear(); + + Counts assertions = m_totals.assertions - prevAssertions; + bool missingAssertions = testForMissingAssertions( assertions ); + + if( testCaseInfo.okToFail() ) { + std::swap( assertions.failedButOk, assertions.failed ); + m_totals.assertions.failed -= assertions.failedButOk; + m_totals.assertions.failedButOk += assertions.failedButOk; + } + + SectionStats testCaseSectionStats( testCaseSection, assertions, duration, missingAssertions ); + m_reporter->sectionEnded( testCaseSectionStats ); + } + + void invokeActiveTestCase() { + FatalConditionHandler fatalConditionHandler; // Handle signals + m_activeTestCase->invoke(); + fatalConditionHandler.reset(); + } + + private: + + ResultBuilder makeUnexpectedResultBuilder() const { + return ResultBuilder( m_lastAssertionInfo.macroName.c_str(), + m_lastAssertionInfo.lineInfo, + m_lastAssertionInfo.capturedExpression.c_str(), + m_lastAssertionInfo.resultDisposition ); + } + + void handleUnfinishedSections() { + // If sections ended prematurely due to an exception we stored their + // infos here so we can tear them down outside the unwind process. + for( std::vector<SectionEndInfo>::const_reverse_iterator it = m_unfinishedSections.rbegin(), + itEnd = m_unfinishedSections.rend(); + it != itEnd; + ++it ) + sectionEnded( *it ); + m_unfinishedSections.clear(); + } + + TestRunInfo m_runInfo; + IMutableContext& m_context; + TestCase const* m_activeTestCase; + ITracker* m_testCaseTracker; + ITracker* m_currentSectionTracker; + AssertionResult m_lastResult; + + Ptr<IConfig const> m_config; + Totals m_totals; + Ptr<IStreamingReporter> m_reporter; + std::vector<MessageInfo> m_messages; + AssertionInfo m_lastAssertionInfo; + std::vector<SectionEndInfo> m_unfinishedSections; + std::vector<ITracker*> m_activeSections; + TrackerContext m_trackerContext; + }; + + IResultCapture& getResultCapture() { + if( IResultCapture* capture = getCurrentContext().getResultCapture() ) + return *capture; + else + throw std::logic_error( "No result capture instance" ); + } + +} // end namespace Catch + +// #included from: internal/catch_version.h +#define TWOBLUECUBES_CATCH_VERSION_H_INCLUDED + +namespace Catch { + + // Versioning information + struct Version { + Version( unsigned int _majorVersion, + unsigned int _minorVersion, + unsigned int _patchNumber, + std::string const& _branchName, + unsigned int _buildNumber ); + + unsigned int const majorVersion; + unsigned int const minorVersion; + unsigned int const patchNumber; + + // buildNumber is only used if branchName is not null + std::string const branchName; + unsigned int const buildNumber; + + friend std::ostream& operator << ( std::ostream& os, Version const& version ); + + private: + void operator=( Version const& ); + }; + + extern Version libraryVersion; +} + +#include <fstream> +#include <stdlib.h> +#include <limits> + +namespace Catch { + + Ptr<IStreamingReporter> createReporter( std::string const& reporterName, Ptr<Config> const& config ) { + Ptr<IStreamingReporter> reporter = getRegistryHub().getReporterRegistry().create( reporterName, config.get() ); + if( !reporter ) { + std::ostringstream oss; + oss << "No reporter registered with name: '" << reporterName << "'"; + throw std::domain_error( oss.str() ); + } + return reporter; + } + + Ptr<IStreamingReporter> makeReporter( Ptr<Config> const& config ) { + std::vector<std::string> reporters = config->getReporterNames(); + if( reporters.empty() ) + reporters.push_back( "console" ); + + Ptr<IStreamingReporter> reporter; + for( std::vector<std::string>::const_iterator it = reporters.begin(), itEnd = reporters.end(); + it != itEnd; + ++it ) + reporter = addReporter( reporter, createReporter( *it, config ) ); + return reporter; + } + Ptr<IStreamingReporter> addListeners( Ptr<IConfig const> const& config, Ptr<IStreamingReporter> reporters ) { + IReporterRegistry::Listeners listeners = getRegistryHub().getReporterRegistry().getListeners(); + for( IReporterRegistry::Listeners::const_iterator it = listeners.begin(), itEnd = listeners.end(); + it != itEnd; + ++it ) + reporters = addReporter(reporters, (*it)->create( ReporterConfig( config ) ) ); + return reporters; + } + + Totals runTests( Ptr<Config> const& config ) { + + Ptr<IConfig const> iconfig = config.get(); + + Ptr<IStreamingReporter> reporter = makeReporter( config ); + reporter = addListeners( iconfig, reporter ); + + RunContext context( iconfig, reporter ); + + Totals totals; + + context.testGroupStarting( config->name(), 1, 1 ); + + TestSpec testSpec = config->testSpec(); + if( !testSpec.hasFilters() ) + testSpec = TestSpecParser( ITagAliasRegistry::get() ).parse( "~[.]" ).testSpec(); // All not hidden tests + + std::vector<TestCase> const& allTestCases = getAllTestCasesSorted( *iconfig ); + for( std::vector<TestCase>::const_iterator it = allTestCases.begin(), itEnd = allTestCases.end(); + it != itEnd; + ++it ) { + if( !context.aborting() && matchTest( *it, testSpec, *iconfig ) ) + totals += context.runTest( *it ); + else + reporter->skipTest( *it ); + } + + context.testGroupEnded( iconfig->name(), totals, 1, 1 ); + return totals; + } + + void applyFilenamesAsTags( IConfig const& config ) { + std::vector<TestCase> const& tests = getAllTestCasesSorted( config ); + for(std::size_t i = 0; i < tests.size(); ++i ) { + TestCase& test = const_cast<TestCase&>( tests[i] ); + std::set<std::string> tags = test.tags; + + std::string filename = test.lineInfo.file; + std::string::size_type lastSlash = filename.find_last_of( "\\/" ); + if( lastSlash != std::string::npos ) + filename = filename.substr( lastSlash+1 ); + + std::string::size_type lastDot = filename.find_last_of( "." ); + if( lastDot != std::string::npos ) + filename = filename.substr( 0, lastDot ); + + tags.insert( "#" + filename ); + setTags( test, tags ); + } + } + + class Session : NonCopyable { + static bool alreadyInstantiated; + + public: + + struct OnUnusedOptions { enum DoWhat { Ignore, Fail }; }; + + Session() + : m_cli( makeCommandLineParser() ) { + if( alreadyInstantiated ) { + std::string msg = "Only one instance of Catch::Session can ever be used"; + Catch::cerr() << msg << std::endl; + throw std::logic_error( msg ); + } + alreadyInstantiated = true; + } + ~Session() { + Catch::cleanUp(); + } + + void showHelp( std::string const& processName ) { + Catch::cout() << "\nCatch v" << libraryVersion << "\n"; + + m_cli.usage( Catch::cout(), processName ); + Catch::cout() << "For more detail usage please see the project docs\n" << std::endl; + } + + int applyCommandLine( int argc, char const* const* const argv, OnUnusedOptions::DoWhat unusedOptionBehaviour = OnUnusedOptions::Fail ) { + try { + m_cli.setThrowOnUnrecognisedTokens( unusedOptionBehaviour == OnUnusedOptions::Fail ); + m_unusedTokens = m_cli.parseInto( Clara::argsToVector( argc, argv ), m_configData ); + if( m_configData.showHelp ) + showHelp( m_configData.processName ); + m_config.reset(); + } + catch( std::exception& ex ) { + { + Colour colourGuard( Colour::Red ); + Catch::cerr() + << "\nError(s) in input:\n" + << Text( ex.what(), TextAttributes().setIndent(2) ) + << "\n\n"; + } + m_cli.usage( Catch::cout(), m_configData.processName ); + return (std::numeric_limits<int>::max)(); + } + return 0; + } + + void useConfigData( ConfigData const& _configData ) { + m_configData = _configData; + m_config.reset(); + } + + int run( int argc, char const* const* const argv ) { + + int returnCode = applyCommandLine( argc, argv ); + if( returnCode == 0 ) + returnCode = run(); + return returnCode; + } + + int run() { + if( m_configData.showHelp ) + return 0; + + try + { + config(); // Force config to be constructed + + seedRng( *m_config ); + + if( m_configData.filenamesAsTags ) + applyFilenamesAsTags( *m_config ); + + // Handle list request + if( Option<std::size_t> listed = list( config() ) ) + return static_cast<int>( *listed ); + + return static_cast<int>( runTests( m_config ).assertions.failed ); + } + catch( std::exception& ex ) { + Catch::cerr() << ex.what() << std::endl; + return (std::numeric_limits<int>::max)(); + } + } + + Clara::CommandLine<ConfigData> const& cli() const { + return m_cli; + } + std::vector<Clara::Parser::Token> const& unusedTokens() const { + return m_unusedTokens; + } + ConfigData& configData() { + return m_configData; + } + Config& config() { + if( !m_config ) + m_config = new Config( m_configData ); + return *m_config; + } + private: + Clara::CommandLine<ConfigData> m_cli; + std::vector<Clara::Parser::Token> m_unusedTokens; + ConfigData m_configData; + Ptr<Config> m_config; + }; + + bool Session::alreadyInstantiated = false; + +} // end namespace Catch + +// #included from: catch_registry_hub.hpp +#define TWOBLUECUBES_CATCH_REGISTRY_HUB_HPP_INCLUDED + +// #included from: catch_test_case_registry_impl.hpp +#define TWOBLUECUBES_CATCH_TEST_CASE_REGISTRY_IMPL_HPP_INCLUDED + +#include <vector> +#include <set> +#include <sstream> +#include <algorithm> + +namespace Catch { + + struct RandomNumberGenerator { + typedef std::ptrdiff_t result_type; + + result_type operator()( result_type n ) const { return std::rand() % n; } + +#ifdef CATCH_CONFIG_CPP11_SHUFFLE + static constexpr result_type min() { return 0; } + static constexpr result_type max() { return 1000000; } + result_type operator()() const { return std::rand() % max(); } +#endif + template<typename V> + static void shuffle( V& vector ) { + RandomNumberGenerator rng; +#ifdef CATCH_CONFIG_CPP11_SHUFFLE + std::shuffle( vector.begin(), vector.end(), rng ); +#else + std::random_shuffle( vector.begin(), vector.end(), rng ); +#endif + } + }; + + inline std::vector<TestCase> sortTests( IConfig const& config, std::vector<TestCase> const& unsortedTestCases ) { + + std::vector<TestCase> sorted = unsortedTestCases; + + switch( config.runOrder() ) { + case RunTests::InLexicographicalOrder: + std::sort( sorted.begin(), sorted.end() ); + break; + case RunTests::InRandomOrder: + { + seedRng( config ); + RandomNumberGenerator::shuffle( sorted ); + } + break; + case RunTests::InDeclarationOrder: + // already in declaration order + break; + } + return sorted; + } + bool matchTest( TestCase const& testCase, TestSpec const& testSpec, IConfig const& config ) { + return testSpec.matches( testCase ) && ( config.allowThrows() || !testCase.throws() ); + } + + void enforceNoDuplicateTestCases( std::vector<TestCase> const& functions ) { + std::set<TestCase> seenFunctions; + for( std::vector<TestCase>::const_iterator it = functions.begin(), itEnd = functions.end(); + it != itEnd; + ++it ) { + std::pair<std::set<TestCase>::const_iterator, bool> prev = seenFunctions.insert( *it ); + if( !prev.second ) { + std::ostringstream ss; + + ss << Colour( Colour::Red ) + << "error: TEST_CASE( \"" << it->name << "\" ) already defined.\n" + << "\tFirst seen at " << prev.first->getTestCaseInfo().lineInfo << '\n' + << "\tRedefined at " << it->getTestCaseInfo().lineInfo << std::endl; + + throw std::runtime_error(ss.str()); + } + } + } + + std::vector<TestCase> filterTests( std::vector<TestCase> const& testCases, TestSpec const& testSpec, IConfig const& config ) { + std::vector<TestCase> filtered; + filtered.reserve( testCases.size() ); + for( std::vector<TestCase>::const_iterator it = testCases.begin(), itEnd = testCases.end(); + it != itEnd; + ++it ) + if( matchTest( *it, testSpec, config ) ) + filtered.push_back( *it ); + return filtered; + } + std::vector<TestCase> const& getAllTestCasesSorted( IConfig const& config ) { + return getRegistryHub().getTestCaseRegistry().getAllTestsSorted( config ); + } + + class TestRegistry : public ITestCaseRegistry { + public: + TestRegistry() + : m_currentSortOrder( RunTests::InDeclarationOrder ), + m_unnamedCount( 0 ) + {} + virtual ~TestRegistry(); + + virtual void registerTest( TestCase const& testCase ) { + std::string name = testCase.getTestCaseInfo().name; + if( name.empty() ) { + std::ostringstream oss; + oss << "Anonymous test case " << ++m_unnamedCount; + return registerTest( testCase.withName( oss.str() ) ); + } + m_functions.push_back( testCase ); + } + + virtual std::vector<TestCase> const& getAllTests() const { + return m_functions; + } + virtual std::vector<TestCase> const& getAllTestsSorted( IConfig const& config ) const { + if( m_sortedFunctions.empty() ) + enforceNoDuplicateTestCases( m_functions ); + + if( m_currentSortOrder != config.runOrder() || m_sortedFunctions.empty() ) { + m_sortedFunctions = sortTests( config, m_functions ); + m_currentSortOrder = config.runOrder(); + } + return m_sortedFunctions; + } + + private: + std::vector<TestCase> m_functions; + mutable RunTests::InWhatOrder m_currentSortOrder; + mutable std::vector<TestCase> m_sortedFunctions; + size_t m_unnamedCount; + std::ios_base::Init m_ostreamInit; // Forces cout/ cerr to be initialised + }; + + /////////////////////////////////////////////////////////////////////////// + + class FreeFunctionTestCase : public SharedImpl<ITestCase> { + public: + + FreeFunctionTestCase( TestFunction fun ) : m_fun( fun ) {} + + virtual void invoke() const { + m_fun(); + } + + private: + virtual ~FreeFunctionTestCase(); + + TestFunction m_fun; + }; + + inline std::string extractClassName( std::string const& classOrQualifiedMethodName ) { + std::string className = classOrQualifiedMethodName; + if( startsWith( className, '&' ) ) + { + std::size_t lastColons = className.rfind( "::" ); + std::size_t penultimateColons = className.rfind( "::", lastColons-1 ); + if( penultimateColons == std::string::npos ) + penultimateColons = 1; + className = className.substr( penultimateColons, lastColons-penultimateColons ); + } + return className; + } + + void registerTestCase + ( ITestCase* testCase, + char const* classOrQualifiedMethodName, + NameAndDesc const& nameAndDesc, + SourceLineInfo const& lineInfo ) { + + getMutableRegistryHub().registerTest + ( makeTestCase + ( testCase, + extractClassName( classOrQualifiedMethodName ), + nameAndDesc.name, + nameAndDesc.description, + lineInfo ) ); + } + void registerTestCaseFunction + ( TestFunction function, + SourceLineInfo const& lineInfo, + NameAndDesc const& nameAndDesc ) { + registerTestCase( new FreeFunctionTestCase( function ), "", nameAndDesc, lineInfo ); + } + + /////////////////////////////////////////////////////////////////////////// + + AutoReg::AutoReg + ( TestFunction function, + SourceLineInfo const& lineInfo, + NameAndDesc const& nameAndDesc ) { + registerTestCaseFunction( function, lineInfo, nameAndDesc ); + } + + AutoReg::~AutoReg() {} + +} // end namespace Catch + +// #included from: catch_reporter_registry.hpp +#define TWOBLUECUBES_CATCH_REPORTER_REGISTRY_HPP_INCLUDED + +#include <map> + +namespace Catch { + + class ReporterRegistry : public IReporterRegistry { + + public: + + virtual ~ReporterRegistry() CATCH_OVERRIDE {} + + virtual IStreamingReporter* create( std::string const& name, Ptr<IConfig const> const& config ) const CATCH_OVERRIDE { + FactoryMap::const_iterator it = m_factories.find( name ); + if( it == m_factories.end() ) + return CATCH_NULL; + return it->second->create( ReporterConfig( config ) ); + } + + void registerReporter( std::string const& name, Ptr<IReporterFactory> const& factory ) { + m_factories.insert( std::make_pair( name, factory ) ); + } + void registerListener( Ptr<IReporterFactory> const& factory ) { + m_listeners.push_back( factory ); + } + + virtual FactoryMap const& getFactories() const CATCH_OVERRIDE { + return m_factories; + } + virtual Listeners const& getListeners() const CATCH_OVERRIDE { + return m_listeners; + } + + private: + FactoryMap m_factories; + Listeners m_listeners; + }; +} + +// #included from: catch_exception_translator_registry.hpp +#define TWOBLUECUBES_CATCH_EXCEPTION_TRANSLATOR_REGISTRY_HPP_INCLUDED + +#ifdef __OBJC__ +#import "Foundation/Foundation.h" +#endif + +namespace Catch { + + class ExceptionTranslatorRegistry : public IExceptionTranslatorRegistry { + public: + ~ExceptionTranslatorRegistry() { + deleteAll( m_translators ); + } + + virtual void registerTranslator( const IExceptionTranslator* translator ) { + m_translators.push_back( translator ); + } + + virtual std::string translateActiveException() const { + try { +#ifdef __OBJC__ + // In Objective-C try objective-c exceptions first + @try { + return tryTranslators(); + } + @catch (NSException *exception) { + return Catch::toString( [exception description] ); + } +#else + return tryTranslators(); +#endif + } + catch( TestFailureException& ) { + throw; + } + catch( std::exception& ex ) { + return ex.what(); + } + catch( std::string& msg ) { + return msg; + } + catch( const char* msg ) { + return msg; + } + catch(...) { + return "Unknown exception"; + } + } + + std::string tryTranslators() const { + if( m_translators.empty() ) + throw; + else + return m_translators[0]->translate( m_translators.begin()+1, m_translators.end() ); + } + + private: + std::vector<const IExceptionTranslator*> m_translators; + }; +} + +namespace Catch { + + namespace { + + class RegistryHub : public IRegistryHub, public IMutableRegistryHub { + + RegistryHub( RegistryHub const& ); + void operator=( RegistryHub const& ); + + public: // IRegistryHub + RegistryHub() { + } + virtual IReporterRegistry const& getReporterRegistry() const CATCH_OVERRIDE { + return m_reporterRegistry; + } + virtual ITestCaseRegistry const& getTestCaseRegistry() const CATCH_OVERRIDE { + return m_testCaseRegistry; + } + virtual IExceptionTranslatorRegistry& getExceptionTranslatorRegistry() CATCH_OVERRIDE { + return m_exceptionTranslatorRegistry; + } + + public: // IMutableRegistryHub + virtual void registerReporter( std::string const& name, Ptr<IReporterFactory> const& factory ) CATCH_OVERRIDE { + m_reporterRegistry.registerReporter( name, factory ); + } + virtual void registerListener( Ptr<IReporterFactory> const& factory ) CATCH_OVERRIDE { + m_reporterRegistry.registerListener( factory ); + } + virtual void registerTest( TestCase const& testInfo ) CATCH_OVERRIDE { + m_testCaseRegistry.registerTest( testInfo ); + } + virtual void registerTranslator( const IExceptionTranslator* translator ) CATCH_OVERRIDE { + m_exceptionTranslatorRegistry.registerTranslator( translator ); + } + + private: + TestRegistry m_testCaseRegistry; + ReporterRegistry m_reporterRegistry; + ExceptionTranslatorRegistry m_exceptionTranslatorRegistry; + }; + + // Single, global, instance + inline RegistryHub*& getTheRegistryHub() { + static RegistryHub* theRegistryHub = CATCH_NULL; + if( !theRegistryHub ) + theRegistryHub = new RegistryHub(); + return theRegistryHub; + } + } + + IRegistryHub& getRegistryHub() { + return *getTheRegistryHub(); + } + IMutableRegistryHub& getMutableRegistryHub() { + return *getTheRegistryHub(); + } + void cleanUp() { + delete getTheRegistryHub(); + getTheRegistryHub() = CATCH_NULL; + cleanUpContext(); + } + std::string translateActiveException() { + return getRegistryHub().getExceptionTranslatorRegistry().translateActiveException(); + } + +} // end namespace Catch + +// #included from: catch_notimplemented_exception.hpp +#define TWOBLUECUBES_CATCH_NOTIMPLEMENTED_EXCEPTION_HPP_INCLUDED + +#include <sstream> + +namespace Catch { + + NotImplementedException::NotImplementedException( SourceLineInfo const& lineInfo ) + : m_lineInfo( lineInfo ) { + std::ostringstream oss; + oss << lineInfo << ": function "; + oss << "not implemented"; + m_what = oss.str(); + } + + const char* NotImplementedException::what() const CATCH_NOEXCEPT { + return m_what.c_str(); + } + +} // end namespace Catch + +// #included from: catch_context_impl.hpp +#define TWOBLUECUBES_CATCH_CONTEXT_IMPL_HPP_INCLUDED + +// #included from: catch_stream.hpp +#define TWOBLUECUBES_CATCH_STREAM_HPP_INCLUDED + +#include <stdexcept> +#include <cstdio> +#include <iostream> + +namespace Catch { + + template<typename WriterF, size_t bufferSize=256> + class StreamBufImpl : public StreamBufBase { + char data[bufferSize]; + WriterF m_writer; + + public: + StreamBufImpl() { + setp( data, data + sizeof(data) ); + } + + ~StreamBufImpl() CATCH_NOEXCEPT { + sync(); + } + + private: + int overflow( int c ) { + sync(); + + if( c != EOF ) { + if( pbase() == epptr() ) + m_writer( std::string( 1, static_cast<char>( c ) ) ); + else + sputc( static_cast<char>( c ) ); + } + return 0; + } + + int sync() { + if( pbase() != pptr() ) { + m_writer( std::string( pbase(), static_cast<std::string::size_type>( pptr() - pbase() ) ) ); + setp( pbase(), epptr() ); + } + return 0; + } + }; + + /////////////////////////////////////////////////////////////////////////// + + FileStream::FileStream( std::string const& filename ) { + m_ofs.open( filename.c_str() ); + if( m_ofs.fail() ) { + std::ostringstream oss; + oss << "Unable to open file: '" << filename << '\''; + throw std::domain_error( oss.str() ); + } + } + + std::ostream& FileStream::stream() const { + return m_ofs; + } + + struct OutputDebugWriter { + + void operator()( std::string const&str ) { + writeToDebugConsole( str ); + } + }; + + DebugOutStream::DebugOutStream() + : m_streamBuf( new StreamBufImpl<OutputDebugWriter>() ), + m_os( m_streamBuf.get() ) + {} + + std::ostream& DebugOutStream::stream() const { + return m_os; + } + + // Store the streambuf from cout up-front because + // cout may get redirected when running tests + CoutStream::CoutStream() + : m_os( Catch::cout().rdbuf() ) + {} + + std::ostream& CoutStream::stream() const { + return m_os; + } + +#ifndef CATCH_CONFIG_NOSTDOUT // If you #define this you must implement these functions + std::ostream& cout() { + return std::cout; + } + std::ostream& cerr() { + return std::cerr; + } +#endif +} + +namespace Catch { + + class Context : public IMutableContext { + + Context() : m_config( CATCH_NULL ), m_runner( CATCH_NULL ), m_resultCapture( CATCH_NULL ) {} + Context( Context const& ); + void operator=( Context const& ); + + public: + virtual ~Context() { + deleteAllValues( m_generatorsByTestName ); + } + + public: // IContext + virtual IResultCapture* getResultCapture() { + return m_resultCapture; + } + virtual IRunner* getRunner() { + return m_runner; + } + virtual size_t getGeneratorIndex( std::string const& fileInfo, size_t totalSize ) { + return getGeneratorsForCurrentTest() + .getGeneratorInfo( fileInfo, totalSize ) + .getCurrentIndex(); + } + virtual bool advanceGeneratorsForCurrentTest() { + IGeneratorsForTest* generators = findGeneratorsForCurrentTest(); + return generators && generators->moveNext(); + } + + virtual Ptr<IConfig const> getConfig() const { + return m_config; + } + + public: // IMutableContext + virtual void setResultCapture( IResultCapture* resultCapture ) { + m_resultCapture = resultCapture; + } + virtual void setRunner( IRunner* runner ) { + m_runner = runner; + } + virtual void setConfig( Ptr<IConfig const> const& config ) { + m_config = config; + } + + friend IMutableContext& getCurrentMutableContext(); + + private: + IGeneratorsForTest* findGeneratorsForCurrentTest() { + std::string testName = getResultCapture()->getCurrentTestName(); + + std::map<std::string, IGeneratorsForTest*>::const_iterator it = + m_generatorsByTestName.find( testName ); + return it != m_generatorsByTestName.end() + ? it->second + : CATCH_NULL; + } + + IGeneratorsForTest& getGeneratorsForCurrentTest() { + IGeneratorsForTest* generators = findGeneratorsForCurrentTest(); + if( !generators ) { + std::string testName = getResultCapture()->getCurrentTestName(); + generators = createGeneratorsForTest(); + m_generatorsByTestName.insert( std::make_pair( testName, generators ) ); + } + return *generators; + } + + private: + Ptr<IConfig const> m_config; + IRunner* m_runner; + IResultCapture* m_resultCapture; + std::map<std::string, IGeneratorsForTest*> m_generatorsByTestName; + }; + + namespace { + Context* currentContext = CATCH_NULL; + } + IMutableContext& getCurrentMutableContext() { + if( !currentContext ) + currentContext = new Context(); + return *currentContext; + } + IContext& getCurrentContext() { + return getCurrentMutableContext(); + } + + void cleanUpContext() { + delete currentContext; + currentContext = CATCH_NULL; + } +} + +// #included from: catch_console_colour_impl.hpp +#define TWOBLUECUBES_CATCH_CONSOLE_COLOUR_IMPL_HPP_INCLUDED + +namespace Catch { + namespace { + + struct IColourImpl { + virtual ~IColourImpl() {} + virtual void use( Colour::Code _colourCode ) = 0; + }; + + struct NoColourImpl : IColourImpl { + void use( Colour::Code ) {} + + static IColourImpl* instance() { + static NoColourImpl s_instance; + return &s_instance; + } + }; + + } // anon namespace +} // namespace Catch + +#if !defined( CATCH_CONFIG_COLOUR_NONE ) && !defined( CATCH_CONFIG_COLOUR_WINDOWS ) && !defined( CATCH_CONFIG_COLOUR_ANSI ) +# ifdef CATCH_PLATFORM_WINDOWS +# define CATCH_CONFIG_COLOUR_WINDOWS +# else +# define CATCH_CONFIG_COLOUR_ANSI +# endif +#endif + +#if defined ( CATCH_CONFIG_COLOUR_WINDOWS ) ///////////////////////////////////////// + +namespace Catch { +namespace { + + class Win32ColourImpl : public IColourImpl { + public: + Win32ColourImpl() : stdoutHandle( GetStdHandle(STD_OUTPUT_HANDLE) ) + { + CONSOLE_SCREEN_BUFFER_INFO csbiInfo; + GetConsoleScreenBufferInfo( stdoutHandle, &csbiInfo ); + originalForegroundAttributes = csbiInfo.wAttributes & ~( BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_INTENSITY ); + originalBackgroundAttributes = csbiInfo.wAttributes & ~( FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY ); + } + + virtual void use( Colour::Code _colourCode ) { + switch( _colourCode ) { + case Colour::None: return setTextAttribute( originalForegroundAttributes ); + case Colour::White: return setTextAttribute( FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE ); + case Colour::Red: return setTextAttribute( FOREGROUND_RED ); + case Colour::Green: return setTextAttribute( FOREGROUND_GREEN ); + case Colour::Blue: return setTextAttribute( FOREGROUND_BLUE ); + case Colour::Cyan: return setTextAttribute( FOREGROUND_BLUE | FOREGROUND_GREEN ); + case Colour::Yellow: return setTextAttribute( FOREGROUND_RED | FOREGROUND_GREEN ); + case Colour::Grey: return setTextAttribute( 0 ); + + case Colour::LightGrey: return setTextAttribute( FOREGROUND_INTENSITY ); + case Colour::BrightRed: return setTextAttribute( FOREGROUND_INTENSITY | FOREGROUND_RED ); + case Colour::BrightGreen: return setTextAttribute( FOREGROUND_INTENSITY | FOREGROUND_GREEN ); + case Colour::BrightWhite: return setTextAttribute( FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE ); + + case Colour::Bright: throw std::logic_error( "not a colour" ); + } + } + + private: + void setTextAttribute( WORD _textAttribute ) { + SetConsoleTextAttribute( stdoutHandle, _textAttribute | originalBackgroundAttributes ); + } + HANDLE stdoutHandle; + WORD originalForegroundAttributes; + WORD originalBackgroundAttributes; + }; + + IColourImpl* platformColourInstance() { + static Win32ColourImpl s_instance; + + Ptr<IConfig const> config = getCurrentContext().getConfig(); + UseColour::YesOrNo colourMode = config + ? config->useColour() + : UseColour::Auto; + if( colourMode == UseColour::Auto ) + colourMode = !isDebuggerActive() + ? UseColour::Yes + : UseColour::No; + return colourMode == UseColour::Yes + ? &s_instance + : NoColourImpl::instance(); + } + +} // end anon namespace +} // end namespace Catch + +#elif defined( CATCH_CONFIG_COLOUR_ANSI ) ////////////////////////////////////// + +#include <unistd.h> + +namespace Catch { +namespace { + + // use POSIX/ ANSI console terminal codes + // Thanks to Adam Strzelecki for original contribution + // (http://github.com/nanoant) + // https://github.com/philsquared/Catch/pull/131 + class PosixColourImpl : public IColourImpl { + public: + virtual void use( Colour::Code _colourCode ) { + switch( _colourCode ) { + case Colour::None: + case Colour::White: return setColour( "[0m" ); + case Colour::Red: return setColour( "[0;31m" ); + case Colour::Green: return setColour( "[0;32m" ); + case Colour::Blue: return setColour( "[0;34m" ); + case Colour::Cyan: return setColour( "[0;36m" ); + case Colour::Yellow: return setColour( "[0;33m" ); + case Colour::Grey: return setColour( "[1;30m" ); + + case Colour::LightGrey: return setColour( "[0;37m" ); + case Colour::BrightRed: return setColour( "[1;31m" ); + case Colour::BrightGreen: return setColour( "[1;32m" ); + case Colour::BrightWhite: return setColour( "[1;37m" ); + + case Colour::Bright: throw std::logic_error( "not a colour" ); + } + } + static IColourImpl* instance() { + static PosixColourImpl s_instance; + return &s_instance; + } + + private: + void setColour( const char* _escapeCode ) { + Catch::cout() << '\033' << _escapeCode; + } + }; + + IColourImpl* platformColourInstance() { + Ptr<IConfig const> config = getCurrentContext().getConfig(); + UseColour::YesOrNo colourMode = config + ? config->useColour() + : UseColour::Auto; + if( colourMode == UseColour::Auto ) + colourMode = (!isDebuggerActive() && isatty(STDOUT_FILENO) ) + ? UseColour::Yes + : UseColour::No; + return colourMode == UseColour::Yes + ? PosixColourImpl::instance() + : NoColourImpl::instance(); + } + +} // end anon namespace +} // end namespace Catch + +#else // not Windows or ANSI /////////////////////////////////////////////// + +namespace Catch { + + static IColourImpl* platformColourInstance() { return NoColourImpl::instance(); } + +} // end namespace Catch + +#endif // Windows/ ANSI/ None + +namespace Catch { + + Colour::Colour( Code _colourCode ) : m_moved( false ) { use( _colourCode ); } + Colour::Colour( Colour const& _other ) : m_moved( false ) { const_cast<Colour&>( _other ).m_moved = true; } + Colour::~Colour(){ if( !m_moved ) use( None ); } + + void Colour::use( Code _colourCode ) { + static IColourImpl* impl = platformColourInstance(); + impl->use( _colourCode ); + } + +} // end namespace Catch + +// #included from: catch_generators_impl.hpp +#define TWOBLUECUBES_CATCH_GENERATORS_IMPL_HPP_INCLUDED + +#include <vector> +#include <string> +#include <map> + +namespace Catch { + + struct GeneratorInfo : IGeneratorInfo { + + GeneratorInfo( std::size_t size ) + : m_size( size ), + m_currentIndex( 0 ) + {} + + bool moveNext() { + if( ++m_currentIndex == m_size ) { + m_currentIndex = 0; + return false; + } + return true; + } + + std::size_t getCurrentIndex() const { + return m_currentIndex; + } + + std::size_t m_size; + std::size_t m_currentIndex; + }; + + /////////////////////////////////////////////////////////////////////////// + + class GeneratorsForTest : public IGeneratorsForTest { + + public: + ~GeneratorsForTest() { + deleteAll( m_generatorsInOrder ); + } + + IGeneratorInfo& getGeneratorInfo( std::string const& fileInfo, std::size_t size ) { + std::map<std::string, IGeneratorInfo*>::const_iterator it = m_generatorsByName.find( fileInfo ); + if( it == m_generatorsByName.end() ) { + IGeneratorInfo* info = new GeneratorInfo( size ); + m_generatorsByName.insert( std::make_pair( fileInfo, info ) ); + m_generatorsInOrder.push_back( info ); + return *info; + } + return *it->second; + } + + bool moveNext() { + std::vector<IGeneratorInfo*>::const_iterator it = m_generatorsInOrder.begin(); + std::vector<IGeneratorInfo*>::const_iterator itEnd = m_generatorsInOrder.end(); + for(; it != itEnd; ++it ) { + if( (*it)->moveNext() ) + return true; + } + return false; + } + + private: + std::map<std::string, IGeneratorInfo*> m_generatorsByName; + std::vector<IGeneratorInfo*> m_generatorsInOrder; + }; + + IGeneratorsForTest* createGeneratorsForTest() + { + return new GeneratorsForTest(); + } + +} // end namespace Catch + +// #included from: catch_assertionresult.hpp +#define TWOBLUECUBES_CATCH_ASSERTIONRESULT_HPP_INCLUDED + +namespace Catch { + + AssertionInfo::AssertionInfo( std::string const& _macroName, + SourceLineInfo const& _lineInfo, + std::string const& _capturedExpression, + ResultDisposition::Flags _resultDisposition ) + : macroName( _macroName ), + lineInfo( _lineInfo ), + capturedExpression( _capturedExpression ), + resultDisposition( _resultDisposition ) + {} + + AssertionResult::AssertionResult() {} + + AssertionResult::AssertionResult( AssertionInfo const& info, AssertionResultData const& data ) + : m_info( info ), + m_resultData( data ) + {} + + AssertionResult::~AssertionResult() {} + + // Result was a success + bool AssertionResult::succeeded() const { + return Catch::isOk( m_resultData.resultType ); + } + + // Result was a success, or failure is suppressed + bool AssertionResult::isOk() const { + return Catch::isOk( m_resultData.resultType ) || shouldSuppressFailure( m_info.resultDisposition ); + } + + ResultWas::OfType AssertionResult::getResultType() const { + return m_resultData.resultType; + } + + bool AssertionResult::hasExpression() const { + return !m_info.capturedExpression.empty(); + } + + bool AssertionResult::hasMessage() const { + return !m_resultData.message.empty(); + } + + std::string AssertionResult::getExpression() const { + if( isFalseTest( m_info.resultDisposition ) ) + return '!' + m_info.capturedExpression; + else + return m_info.capturedExpression; + } + std::string AssertionResult::getExpressionInMacro() const { + if( m_info.macroName.empty() ) + return m_info.capturedExpression; + else + return m_info.macroName + "( " + m_info.capturedExpression + " )"; + } + + bool AssertionResult::hasExpandedExpression() const { + return hasExpression() && getExpandedExpression() != getExpression(); + } + + std::string AssertionResult::getExpandedExpression() const { + return m_resultData.reconstructExpression(); + } + + std::string AssertionResult::getMessage() const { + return m_resultData.message; + } + SourceLineInfo AssertionResult::getSourceInfo() const { + return m_info.lineInfo; + } + + std::string AssertionResult::getTestMacroName() const { + return m_info.macroName; + } + + void AssertionResult::discardDecomposedExpression() const { + m_resultData.decomposedExpression = CATCH_NULL; + } + + void AssertionResult::expandDecomposedExpression() const { + m_resultData.reconstructExpression(); + } + +} // end namespace Catch + +// #included from: catch_test_case_info.hpp +#define TWOBLUECUBES_CATCH_TEST_CASE_INFO_HPP_INCLUDED + +#include <cctype> + +namespace Catch { + + inline TestCaseInfo::SpecialProperties parseSpecialTag( std::string const& tag ) { + if( startsWith( tag, '.' ) || + tag == "hide" || + tag == "!hide" ) + return TestCaseInfo::IsHidden; + else if( tag == "!throws" ) + return TestCaseInfo::Throws; + else if( tag == "!shouldfail" ) + return TestCaseInfo::ShouldFail; + else if( tag == "!mayfail" ) + return TestCaseInfo::MayFail; + else if( tag == "!nonportable" ) + return TestCaseInfo::NonPortable; + else + return TestCaseInfo::None; + } + inline bool isReservedTag( std::string const& tag ) { + return parseSpecialTag( tag ) == TestCaseInfo::None && tag.size() > 0 && !std::isalnum( tag[0] ); + } + inline void enforceNotReservedTag( std::string const& tag, SourceLineInfo const& _lineInfo ) { + if( isReservedTag( tag ) ) { + { + Colour colourGuard( Colour::Red ); + Catch::cerr() + << "Tag name [" << tag << "] not allowed.\n" + << "Tag names starting with non alpha-numeric characters are reserved\n"; + } + { + Colour colourGuard( Colour::FileName ); + Catch::cerr() << _lineInfo << std::endl; + } + exit(1); + } + } + + TestCase makeTestCase( ITestCase* _testCase, + std::string const& _className, + std::string const& _name, + std::string const& _descOrTags, + SourceLineInfo const& _lineInfo ) + { + bool isHidden( startsWith( _name, "./" ) ); // Legacy support + + // Parse out tags + std::set<std::string> tags; + std::string desc, tag; + bool inTag = false; + for( std::size_t i = 0; i < _descOrTags.size(); ++i ) { + char c = _descOrTags[i]; + if( !inTag ) { + if( c == '[' ) + inTag = true; + else + desc += c; + } + else { + if( c == ']' ) { + TestCaseInfo::SpecialProperties prop = parseSpecialTag( tag ); + if( prop == TestCaseInfo::IsHidden ) + isHidden = true; + else if( prop == TestCaseInfo::None ) + enforceNotReservedTag( tag, _lineInfo ); + + tags.insert( tag ); + tag.clear(); + inTag = false; + } + else + tag += c; + } + } + if( isHidden ) { + tags.insert( "hide" ); + tags.insert( "." ); + } + + TestCaseInfo info( _name, _className, desc, tags, _lineInfo ); + return TestCase( _testCase, info ); + } + + void setTags( TestCaseInfo& testCaseInfo, std::set<std::string> const& tags ) + { + testCaseInfo.tags = tags; + testCaseInfo.lcaseTags.clear(); + + std::ostringstream oss; + for( std::set<std::string>::const_iterator it = tags.begin(), itEnd = tags.end(); it != itEnd; ++it ) { + oss << '[' << *it << ']'; + std::string lcaseTag = toLower( *it ); + testCaseInfo.properties = static_cast<TestCaseInfo::SpecialProperties>( testCaseInfo.properties | parseSpecialTag( lcaseTag ) ); + testCaseInfo.lcaseTags.insert( lcaseTag ); + } + testCaseInfo.tagsAsString = oss.str(); + } + + TestCaseInfo::TestCaseInfo( std::string const& _name, + std::string const& _className, + std::string const& _description, + std::set<std::string> const& _tags, + SourceLineInfo const& _lineInfo ) + : name( _name ), + className( _className ), + description( _description ), + lineInfo( _lineInfo ), + properties( None ) + { + setTags( *this, _tags ); + } + + TestCaseInfo::TestCaseInfo( TestCaseInfo const& other ) + : name( other.name ), + className( other.className ), + description( other.description ), + tags( other.tags ), + lcaseTags( other.lcaseTags ), + tagsAsString( other.tagsAsString ), + lineInfo( other.lineInfo ), + properties( other.properties ) + {} + + bool TestCaseInfo::isHidden() const { + return ( properties & IsHidden ) != 0; + } + bool TestCaseInfo::throws() const { + return ( properties & Throws ) != 0; + } + bool TestCaseInfo::okToFail() const { + return ( properties & (ShouldFail | MayFail ) ) != 0; + } + bool TestCaseInfo::expectedToFail() const { + return ( properties & (ShouldFail ) ) != 0; + } + + TestCase::TestCase( ITestCase* testCase, TestCaseInfo const& info ) : TestCaseInfo( info ), test( testCase ) {} + + TestCase::TestCase( TestCase const& other ) + : TestCaseInfo( other ), + test( other.test ) + {} + + TestCase TestCase::withName( std::string const& _newName ) const { + TestCase other( *this ); + other.name = _newName; + return other; + } + + void TestCase::swap( TestCase& other ) { + test.swap( other.test ); + name.swap( other.name ); + className.swap( other.className ); + description.swap( other.description ); + tags.swap( other.tags ); + lcaseTags.swap( other.lcaseTags ); + tagsAsString.swap( other.tagsAsString ); + std::swap( TestCaseInfo::properties, static_cast<TestCaseInfo&>( other ).properties ); + std::swap( lineInfo, other.lineInfo ); + } + + void TestCase::invoke() const { + test->invoke(); + } + + bool TestCase::operator == ( TestCase const& other ) const { + return test.get() == other.test.get() && + name == other.name && + className == other.className; + } + + bool TestCase::operator < ( TestCase const& other ) const { + return name < other.name; + } + TestCase& TestCase::operator = ( TestCase const& other ) { + TestCase temp( other ); + swap( temp ); + return *this; + } + + TestCaseInfo const& TestCase::getTestCaseInfo() const + { + return *this; + } + +} // end namespace Catch + +// #included from: catch_version.hpp +#define TWOBLUECUBES_CATCH_VERSION_HPP_INCLUDED + +namespace Catch { + + Version::Version + ( unsigned int _majorVersion, + unsigned int _minorVersion, + unsigned int _patchNumber, + std::string const& _branchName, + unsigned int _buildNumber ) + : majorVersion( _majorVersion ), + minorVersion( _minorVersion ), + patchNumber( _patchNumber ), + branchName( _branchName ), + buildNumber( _buildNumber ) + {} + + std::ostream& operator << ( std::ostream& os, Version const& version ) { + os << version.majorVersion << '.' + << version.minorVersion << '.' + << version.patchNumber; + + if( !version.branchName.empty() ) { + os << '-' << version.branchName + << '.' << version.buildNumber; + } + return os; + } + + Version libraryVersion( 1, 8, 1, "", 0 ); + +} + +// #included from: catch_message.hpp +#define TWOBLUECUBES_CATCH_MESSAGE_HPP_INCLUDED + +namespace Catch { + + MessageInfo::MessageInfo( std::string const& _macroName, + SourceLineInfo const& _lineInfo, + ResultWas::OfType _type ) + : macroName( _macroName ), + lineInfo( _lineInfo ), + type( _type ), + sequence( ++globalCount ) + {} + + // This may need protecting if threading support is added + unsigned int MessageInfo::globalCount = 0; + + //////////////////////////////////////////////////////////////////////////// + + ScopedMessage::ScopedMessage( MessageBuilder const& builder ) + : m_info( builder.m_info ) + { + m_info.message = builder.m_stream.str(); + getResultCapture().pushScopedMessage( m_info ); + } + ScopedMessage::ScopedMessage( ScopedMessage const& other ) + : m_info( other.m_info ) + {} + + ScopedMessage::~ScopedMessage() { + getResultCapture().popScopedMessage( m_info ); + } + +} // end namespace Catch + +// #included from: catch_legacy_reporter_adapter.hpp +#define TWOBLUECUBES_CATCH_LEGACY_REPORTER_ADAPTER_HPP_INCLUDED + +// #included from: catch_legacy_reporter_adapter.h +#define TWOBLUECUBES_CATCH_LEGACY_REPORTER_ADAPTER_H_INCLUDED + +namespace Catch +{ + // Deprecated + struct IReporter : IShared { + virtual ~IReporter(); + + virtual bool shouldRedirectStdout() const = 0; + + virtual void StartTesting() = 0; + virtual void EndTesting( Totals const& totals ) = 0; + virtual void StartGroup( std::string const& groupName ) = 0; + virtual void EndGroup( std::string const& groupName, Totals const& totals ) = 0; + virtual void StartTestCase( TestCaseInfo const& testInfo ) = 0; + virtual void EndTestCase( TestCaseInfo const& testInfo, Totals const& totals, std::string const& stdOut, std::string const& stdErr ) = 0; + virtual void StartSection( std::string const& sectionName, std::string const& description ) = 0; + virtual void EndSection( std::string const& sectionName, Counts const& assertions ) = 0; + virtual void NoAssertionsInSection( std::string const& sectionName ) = 0; + virtual void NoAssertionsInTestCase( std::string const& testName ) = 0; + virtual void Aborted() = 0; + virtual void Result( AssertionResult const& result ) = 0; + }; + + class LegacyReporterAdapter : public SharedImpl<IStreamingReporter> + { + public: + LegacyReporterAdapter( Ptr<IReporter> const& legacyReporter ); + virtual ~LegacyReporterAdapter(); + + virtual ReporterPreferences getPreferences() const; + virtual void noMatchingTestCases( std::string const& ); + virtual void testRunStarting( TestRunInfo const& ); + virtual void testGroupStarting( GroupInfo const& groupInfo ); + virtual void testCaseStarting( TestCaseInfo const& testInfo ); + virtual void sectionStarting( SectionInfo const& sectionInfo ); + virtual void assertionStarting( AssertionInfo const& ); + virtual bool assertionEnded( AssertionStats const& assertionStats ); + virtual void sectionEnded( SectionStats const& sectionStats ); + virtual void testCaseEnded( TestCaseStats const& testCaseStats ); + virtual void testGroupEnded( TestGroupStats const& testGroupStats ); + virtual void testRunEnded( TestRunStats const& testRunStats ); + virtual void skipTest( TestCaseInfo const& ); + + private: + Ptr<IReporter> m_legacyReporter; + }; +} + +namespace Catch +{ + LegacyReporterAdapter::LegacyReporterAdapter( Ptr<IReporter> const& legacyReporter ) + : m_legacyReporter( legacyReporter ) + {} + LegacyReporterAdapter::~LegacyReporterAdapter() {} + + ReporterPreferences LegacyReporterAdapter::getPreferences() const { + ReporterPreferences prefs; + prefs.shouldRedirectStdOut = m_legacyReporter->shouldRedirectStdout(); + return prefs; + } + + void LegacyReporterAdapter::noMatchingTestCases( std::string const& ) {} + void LegacyReporterAdapter::testRunStarting( TestRunInfo const& ) { + m_legacyReporter->StartTesting(); + } + void LegacyReporterAdapter::testGroupStarting( GroupInfo const& groupInfo ) { + m_legacyReporter->StartGroup( groupInfo.name ); + } + void LegacyReporterAdapter::testCaseStarting( TestCaseInfo const& testInfo ) { + m_legacyReporter->StartTestCase( testInfo ); + } + void LegacyReporterAdapter::sectionStarting( SectionInfo const& sectionInfo ) { + m_legacyReporter->StartSection( sectionInfo.name, sectionInfo.description ); + } + void LegacyReporterAdapter::assertionStarting( AssertionInfo const& ) { + // Not on legacy interface + } + + bool LegacyReporterAdapter::assertionEnded( AssertionStats const& assertionStats ) { + if( assertionStats.assertionResult.getResultType() != ResultWas::Ok ) { + for( std::vector<MessageInfo>::const_iterator it = assertionStats.infoMessages.begin(), itEnd = assertionStats.infoMessages.end(); + it != itEnd; + ++it ) { + if( it->type == ResultWas::Info ) { + ResultBuilder rb( it->macroName.c_str(), it->lineInfo, "", ResultDisposition::Normal ); + rb << it->message; + rb.setResultType( ResultWas::Info ); + AssertionResult result = rb.build(); + m_legacyReporter->Result( result ); + } + } + } + m_legacyReporter->Result( assertionStats.assertionResult ); + return true; + } + void LegacyReporterAdapter::sectionEnded( SectionStats const& sectionStats ) { + if( sectionStats.missingAssertions ) + m_legacyReporter->NoAssertionsInSection( sectionStats.sectionInfo.name ); + m_legacyReporter->EndSection( sectionStats.sectionInfo.name, sectionStats.assertions ); + } + void LegacyReporterAdapter::testCaseEnded( TestCaseStats const& testCaseStats ) { + m_legacyReporter->EndTestCase + ( testCaseStats.testInfo, + testCaseStats.totals, + testCaseStats.stdOut, + testCaseStats.stdErr ); + } + void LegacyReporterAdapter::testGroupEnded( TestGroupStats const& testGroupStats ) { + if( testGroupStats.aborting ) + m_legacyReporter->Aborted(); + m_legacyReporter->EndGroup( testGroupStats.groupInfo.name, testGroupStats.totals ); + } + void LegacyReporterAdapter::testRunEnded( TestRunStats const& testRunStats ) { + m_legacyReporter->EndTesting( testRunStats.totals ); + } + void LegacyReporterAdapter::skipTest( TestCaseInfo const& ) { + } +} + +// #included from: catch_timer.hpp + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wc++11-long-long" +#endif + +#ifdef CATCH_PLATFORM_WINDOWS + +#else + +#include <sys/time.h> + +#endif + +namespace Catch { + + namespace { +#ifdef CATCH_PLATFORM_WINDOWS + uint64_t getCurrentTicks() { + static uint64_t hz=0, hzo=0; + if (!hz) { + QueryPerformanceFrequency( reinterpret_cast<LARGE_INTEGER*>( &hz ) ); + QueryPerformanceCounter( reinterpret_cast<LARGE_INTEGER*>( &hzo ) ); + } + uint64_t t; + QueryPerformanceCounter( reinterpret_cast<LARGE_INTEGER*>( &t ) ); + return ((t-hzo)*1000000)/hz; + } +#else + uint64_t getCurrentTicks() { + timeval t; + gettimeofday(&t,CATCH_NULL); + return static_cast<uint64_t>( t.tv_sec ) * 1000000ull + static_cast<uint64_t>( t.tv_usec ); + } +#endif + } + + void Timer::start() { + m_ticks = getCurrentTicks(); + } + unsigned int Timer::getElapsedMicroseconds() const { + return static_cast<unsigned int>(getCurrentTicks() - m_ticks); + } + unsigned int Timer::getElapsedMilliseconds() const { + return static_cast<unsigned int>(getElapsedMicroseconds()/1000); + } + double Timer::getElapsedSeconds() const { + return getElapsedMicroseconds()/1000000.0; + } + +} // namespace Catch + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif +// #included from: catch_common.hpp +#define TWOBLUECUBES_CATCH_COMMON_HPP_INCLUDED + +#include <cstring> +#include <cctype> + +namespace Catch { + + bool startsWith( std::string const& s, std::string const& prefix ) { + return s.size() >= prefix.size() && std::equal(prefix.begin(), prefix.end(), s.begin()); + } + bool startsWith( std::string const& s, char prefix ) { + return !s.empty() && s[0] == prefix; + } + bool endsWith( std::string const& s, std::string const& suffix ) { + return s.size() >= suffix.size() && std::equal(suffix.rbegin(), suffix.rend(), s.rbegin()); + } + bool endsWith( std::string const& s, char suffix ) { + return !s.empty() && s[s.size()-1] == suffix; + } + bool contains( std::string const& s, std::string const& infix ) { + return s.find( infix ) != std::string::npos; + } + char toLowerCh(char c) { + return static_cast<char>( std::tolower( c ) ); + } + void toLowerInPlace( std::string& s ) { + std::transform( s.begin(), s.end(), s.begin(), toLowerCh ); + } + std::string toLower( std::string const& s ) { + std::string lc = s; + toLowerInPlace( lc ); + return lc; + } + std::string trim( std::string const& str ) { + static char const* whitespaceChars = "\n\r\t "; + std::string::size_type start = str.find_first_not_of( whitespaceChars ); + std::string::size_type end = str.find_last_not_of( whitespaceChars ); + + return start != std::string::npos ? str.substr( start, 1+end-start ) : std::string(); + } + + bool replaceInPlace( std::string& str, std::string const& replaceThis, std::string const& withThis ) { + bool replaced = false; + std::size_t i = str.find( replaceThis ); + while( i != std::string::npos ) { + replaced = true; + str = str.substr( 0, i ) + withThis + str.substr( i+replaceThis.size() ); + if( i < str.size()-withThis.size() ) + i = str.find( replaceThis, i+withThis.size() ); + else + i = std::string::npos; + } + return replaced; + } + + pluralise::pluralise( std::size_t count, std::string const& label ) + : m_count( count ), + m_label( label ) + {} + + std::ostream& operator << ( std::ostream& os, pluralise const& pluraliser ) { + os << pluraliser.m_count << ' ' << pluraliser.m_label; + if( pluraliser.m_count != 1 ) + os << 's'; + return os; + } + + SourceLineInfo::SourceLineInfo() : file(""), line( 0 ){} + SourceLineInfo::SourceLineInfo( char const* _file, std::size_t _line ) + : file( _file ), + line( _line ) + {} + bool SourceLineInfo::empty() const { + return file[0] == '\0'; + } + bool SourceLineInfo::operator == ( SourceLineInfo const& other ) const { + return line == other.line && (file == other.file || std::strcmp(file, other.file) == 0); + } + bool SourceLineInfo::operator < ( SourceLineInfo const& other ) const { + return line < other.line || ( line == other.line && (std::strcmp(file, other.file) < 0)); + } + + void seedRng( IConfig const& config ) { + if( config.rngSeed() != 0 ) + std::srand( config.rngSeed() ); + } + unsigned int rngSeed() { + return getCurrentContext().getConfig()->rngSeed(); + } + + std::ostream& operator << ( std::ostream& os, SourceLineInfo const& info ) { +#ifndef __GNUG__ + os << info.file << '(' << info.line << ')'; +#else + os << info.file << ':' << info.line; +#endif + return os; + } + + void throwLogicError( std::string const& message, SourceLineInfo const& locationInfo ) { + std::ostringstream oss; + oss << locationInfo << ": Internal Catch error: '" << message << '\''; + if( alwaysTrue() ) + throw std::logic_error( oss.str() ); + } +} + +// #included from: catch_section.hpp +#define TWOBLUECUBES_CATCH_SECTION_HPP_INCLUDED + +namespace Catch { + + SectionInfo::SectionInfo + ( SourceLineInfo const& _lineInfo, + std::string const& _name, + std::string const& _description ) + : name( _name ), + description( _description ), + lineInfo( _lineInfo ) + {} + + Section::Section( SectionInfo const& info ) + : m_info( info ), + m_sectionIncluded( getResultCapture().sectionStarted( m_info, m_assertions ) ) + { + m_timer.start(); + } + + Section::~Section() { + if( m_sectionIncluded ) { + SectionEndInfo endInfo( m_info, m_assertions, m_timer.getElapsedSeconds() ); + if( std::uncaught_exception() ) + getResultCapture().sectionEndedEarly( endInfo ); + else + getResultCapture().sectionEnded( endInfo ); + } + } + + // This indicates whether the section should be executed or not + Section::operator bool() const { + return m_sectionIncluded; + } + +} // end namespace Catch + +// #included from: catch_debugger.hpp +#define TWOBLUECUBES_CATCH_DEBUGGER_HPP_INCLUDED + +#ifdef CATCH_PLATFORM_MAC + + #include <assert.h> + #include <stdbool.h> + #include <sys/types.h> + #include <unistd.h> + #include <sys/sysctl.h> + + namespace Catch{ + + // The following function is taken directly from the following technical note: + // http://developer.apple.com/library/mac/#qa/qa2004/qa1361.html + + // Returns true if the current process is being debugged (either + // running under the debugger or has a debugger attached post facto). + bool isDebuggerActive(){ + + int mib[4]; + struct kinfo_proc info; + size_t size; + + // Initialize the flags so that, if sysctl fails for some bizarre + // reason, we get a predictable result. + + info.kp_proc.p_flag = 0; + + // Initialize mib, which tells sysctl the info we want, in this case + // we're looking for information about a specific process ID. + + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = KERN_PROC_PID; + mib[3] = getpid(); + + // Call sysctl. + + size = sizeof(info); + if( sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, CATCH_NULL, 0) != 0 ) { + Catch::cerr() << "\n** Call to sysctl failed - unable to determine if debugger is active **\n" << std::endl; + return false; + } + + // We're being debugged if the P_TRACED flag is set. + + return ( (info.kp_proc.p_flag & P_TRACED) != 0 ); + } + } // namespace Catch + +#elif defined(CATCH_PLATFORM_LINUX) + #include <fstream> + #include <string> + + namespace Catch{ + // The standard POSIX way of detecting a debugger is to attempt to + // ptrace() the process, but this needs to be done from a child and not + // this process itself to still allow attaching to this process later + // if wanted, so is rather heavy. Under Linux we have the PID of the + // "debugger" (which doesn't need to be gdb, of course, it could also + // be strace, for example) in /proc/$PID/status, so just get it from + // there instead. + bool isDebuggerActive(){ + std::ifstream in("/proc/self/status"); + for( std::string line; std::getline(in, line); ) { + static const int PREFIX_LEN = 11; + if( line.compare(0, PREFIX_LEN, "TracerPid:\t") == 0 ) { + // We're traced if the PID is not 0 and no other PID starts + // with 0 digit, so it's enough to check for just a single + // character. + return line.length() > PREFIX_LEN && line[PREFIX_LEN] != '0'; + } + } + + return false; + } + } // namespace Catch +#elif defined(_MSC_VER) + extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent(); + namespace Catch { + bool isDebuggerActive() { + return IsDebuggerPresent() != 0; + } + } +#elif defined(__MINGW32__) + extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent(); + namespace Catch { + bool isDebuggerActive() { + return IsDebuggerPresent() != 0; + } + } +#else + namespace Catch { + inline bool isDebuggerActive() { return false; } + } +#endif // Platform + +#ifdef CATCH_PLATFORM_WINDOWS + + namespace Catch { + void writeToDebugConsole( std::string const& text ) { + ::OutputDebugStringA( text.c_str() ); + } + } +#else + namespace Catch { + void writeToDebugConsole( std::string const& text ) { + // !TBD: Need a version for Mac/ XCode and other IDEs + Catch::cout() << text; + } + } +#endif // Platform + +// #included from: catch_tostring.hpp +#define TWOBLUECUBES_CATCH_TOSTRING_HPP_INCLUDED + +namespace Catch { + +namespace Detail { + + const std::string unprintableString = "{?}"; + + namespace { + const int hexThreshold = 255; + + struct Endianness { + enum Arch { Big, Little }; + + static Arch which() { + union _{ + int asInt; + char asChar[sizeof (int)]; + } u; + + u.asInt = 1; + return ( u.asChar[sizeof(int)-1] == 1 ) ? Big : Little; + } + }; + } + + std::string rawMemoryToString( const void *object, std::size_t size ) + { + // Reverse order for little endian architectures + int i = 0, end = static_cast<int>( size ), inc = 1; + if( Endianness::which() == Endianness::Little ) { + i = end-1; + end = inc = -1; + } + + unsigned char const *bytes = static_cast<unsigned char const *>(object); + std::ostringstream os; + os << "0x" << std::setfill('0') << std::hex; + for( ; i != end; i += inc ) + os << std::setw(2) << static_cast<unsigned>(bytes[i]); + return os.str(); + } +} + +std::string toString( std::string const& value ) { + std::string s = value; + if( getCurrentContext().getConfig()->showInvisibles() ) { + for(size_t i = 0; i < s.size(); ++i ) { + std::string subs; + switch( s[i] ) { + case '\n': subs = "\\n"; break; + case '\t': subs = "\\t"; break; + default: break; + } + if( !subs.empty() ) { + s = s.substr( 0, i ) + subs + s.substr( i+1 ); + ++i; + } + } + } + return '"' + s + '"'; +} +std::string toString( std::wstring const& value ) { + + std::string s; + s.reserve( value.size() ); + for(size_t i = 0; i < value.size(); ++i ) + s += value[i] <= 0xff ? static_cast<char>( value[i] ) : '?'; + return Catch::toString( s ); +} + +std::string toString( const char* const value ) { + return value ? Catch::toString( std::string( value ) ) : std::string( "{null string}" ); +} + +std::string toString( char* const value ) { + return Catch::toString( static_cast<const char*>( value ) ); +} + +std::string toString( const wchar_t* const value ) +{ + return value ? Catch::toString( std::wstring(value) ) : std::string( "{null string}" ); +} + +std::string toString( wchar_t* const value ) +{ + return Catch::toString( static_cast<const wchar_t*>( value ) ); +} + +std::string toString( int value ) { + std::ostringstream oss; + oss << value; + if( value > Detail::hexThreshold ) + oss << " (0x" << std::hex << value << ')'; + return oss.str(); +} + +std::string toString( unsigned long value ) { + std::ostringstream oss; + oss << value; + if( value > Detail::hexThreshold ) + oss << " (0x" << std::hex << value << ')'; + return oss.str(); +} + +std::string toString( unsigned int value ) { + return Catch::toString( static_cast<unsigned long>( value ) ); +} + +template<typename T> +std::string fpToString( T value, int precision ) { + std::ostringstream oss; + oss << std::setprecision( precision ) + << std::fixed + << value; + std::string d = oss.str(); + std::size_t i = d.find_last_not_of( '0' ); + if( i != std::string::npos && i != d.size()-1 ) { + if( d[i] == '.' ) + i++; + d = d.substr( 0, i+1 ); + } + return d; +} + +std::string toString( const double value ) { + return fpToString( value, 10 ); +} +std::string toString( const float value ) { + return fpToString( value, 5 ) + 'f'; +} + +std::string toString( bool value ) { + return value ? "true" : "false"; +} + +std::string toString( char value ) { + if ( value == '\r' ) + return "'\\r'"; + if ( value == '\f' ) + return "'\\f'"; + if ( value == '\n' ) + return "'\\n'"; + if ( value == '\t' ) + return "'\\t'"; + if ( '\0' <= value && value < ' ' ) + return toString( static_cast<unsigned int>( value ) ); + char chstr[] = "' '"; + chstr[1] = value; + return chstr; +} + +std::string toString( signed char value ) { + return toString( static_cast<char>( value ) ); +} + +std::string toString( unsigned char value ) { + return toString( static_cast<char>( value ) ); +} + +#ifdef CATCH_CONFIG_CPP11_LONG_LONG +std::string toString( long long value ) { + std::ostringstream oss; + oss << value; + if( value > Detail::hexThreshold ) + oss << " (0x" << std::hex << value << ')'; + return oss.str(); +} +std::string toString( unsigned long long value ) { + std::ostringstream oss; + oss << value; + if( value > Detail::hexThreshold ) + oss << " (0x" << std::hex << value << ')'; + return oss.str(); +} +#endif + +#ifdef CATCH_CONFIG_CPP11_NULLPTR +std::string toString( std::nullptr_t ) { + return "nullptr"; +} +#endif + +#ifdef __OBJC__ + std::string toString( NSString const * const& nsstring ) { + if( !nsstring ) + return "nil"; + return "@" + toString([nsstring UTF8String]); + } + std::string toString( NSString * CATCH_ARC_STRONG const& nsstring ) { + if( !nsstring ) + return "nil"; + return "@" + toString([nsstring UTF8String]); + } + std::string toString( NSObject* const& nsObject ) { + return toString( [nsObject description] ); + } +#endif + +} // end namespace Catch + +// #included from: catch_result_builder.hpp +#define TWOBLUECUBES_CATCH_RESULT_BUILDER_HPP_INCLUDED + +namespace Catch { + + std::string capturedExpressionWithSecondArgument( std::string const& capturedExpression, std::string const& secondArg ) { + return secondArg.empty() || secondArg == "\"\"" + ? capturedExpression + : capturedExpression + ", " + secondArg; + } + ResultBuilder::ResultBuilder( char const* macroName, + SourceLineInfo const& lineInfo, + char const* capturedExpression, + ResultDisposition::Flags resultDisposition, + char const* secondArg ) + : m_assertionInfo( macroName, lineInfo, capturedExpressionWithSecondArgument( capturedExpression, secondArg ), resultDisposition ), + m_shouldDebugBreak( false ), + m_shouldThrow( false ) + {} + + ResultBuilder& ResultBuilder::setResultType( ResultWas::OfType result ) { + m_data.resultType = result; + return *this; + } + ResultBuilder& ResultBuilder::setResultType( bool result ) { + m_data.resultType = result ? ResultWas::Ok : ResultWas::ExpressionFailed; + return *this; + } + + void ResultBuilder::endExpression( DecomposedExpression const& expr ) { + AssertionResult result = build( expr ); + handleResult( result ); + } + + void ResultBuilder::useActiveException( ResultDisposition::Flags resultDisposition ) { + m_assertionInfo.resultDisposition = resultDisposition; + m_stream.oss << Catch::translateActiveException(); + captureResult( ResultWas::ThrewException ); + } + + void ResultBuilder::captureResult( ResultWas::OfType resultType ) { + setResultType( resultType ); + captureExpression(); + } + + void ResultBuilder::captureExpectedException( std::string const& expectedMessage ) { + if( expectedMessage.empty() ) + captureExpectedException( Matchers::Impl::MatchAllOf<std::string>() ); + else + captureExpectedException( Matchers::Equals( expectedMessage ) ); + } + + void ResultBuilder::captureExpectedException( Matchers::Impl::MatcherBase<std::string> const& matcher ) { + + assert( !isFalseTest( m_assertionInfo.resultDisposition ) ); + AssertionResultData data = m_data; + data.resultType = ResultWas::Ok; + data.reconstructedExpression = m_assertionInfo.capturedExpression; + + std::string actualMessage = Catch::translateActiveException(); + if( !matcher.match( actualMessage ) ) { + data.resultType = ResultWas::ExpressionFailed; + data.reconstructedExpression = actualMessage; + } + AssertionResult result( m_assertionInfo, data ); + handleResult( result ); + } + + void ResultBuilder::captureExpression() { + AssertionResult result = build(); + handleResult( result ); + } + + void ResultBuilder::handleResult( AssertionResult const& result ) + { + getResultCapture().assertionEnded( result ); + + if( !result.isOk() ) { + if( getCurrentContext().getConfig()->shouldDebugBreak() ) + m_shouldDebugBreak = true; + if( getCurrentContext().getRunner()->aborting() || (m_assertionInfo.resultDisposition & ResultDisposition::Normal) ) + m_shouldThrow = true; + } + } + + void ResultBuilder::react() { +#if defined(CATCH_CONFIG_FAST_COMPILE) + if (m_shouldDebugBreak) { + /////////////////////////////////////////////////////////////////// + // To inspect the state during test, you need to go one level up the callstack + // To go back to the test and change execution, jump over the throw statement + /////////////////////////////////////////////////////////////////// + CATCH_BREAK_INTO_DEBUGGER(); + } +#endif + if( m_shouldThrow ) + throw Catch::TestFailureException(); + } + + bool ResultBuilder::shouldDebugBreak() const { return m_shouldDebugBreak; } + bool ResultBuilder::allowThrows() const { return getCurrentContext().getConfig()->allowThrows(); } + + AssertionResult ResultBuilder::build() const + { + return build( *this ); + } + + // CAVEAT: The returned AssertionResult stores a pointer to the argument expr, + // a temporary DecomposedExpression, which in turn holds references to + // operands, possibly temporary as well. + // It should immediately be passed to handleResult; if the expression + // needs to be reported, its string expansion must be composed before + // the temporaries are destroyed. + AssertionResult ResultBuilder::build( DecomposedExpression const& expr ) const + { + assert( m_data.resultType != ResultWas::Unknown ); + AssertionResultData data = m_data; + + // Flip bool results if FalseTest flag is set + if( isFalseTest( m_assertionInfo.resultDisposition ) ) { + data.negate( expr.isBinaryExpression() ); + } + + data.message = m_stream.oss.str(); + data.decomposedExpression = &expr; // for lazy reconstruction + return AssertionResult( m_assertionInfo, data ); + } + + void ResultBuilder::reconstructExpression( std::string& dest ) const { + dest = m_assertionInfo.capturedExpression; + } + +} // end namespace Catch + +// #included from: catch_tag_alias_registry.hpp +#define TWOBLUECUBES_CATCH_TAG_ALIAS_REGISTRY_HPP_INCLUDED + +// #included from: catch_tag_alias_registry.h +#define TWOBLUECUBES_CATCH_TAG_ALIAS_REGISTRY_H_INCLUDED + +#include <map> + +namespace Catch { + + class TagAliasRegistry : public ITagAliasRegistry { + public: + virtual ~TagAliasRegistry(); + virtual Option<TagAlias> find( std::string const& alias ) const; + virtual std::string expandAliases( std::string const& unexpandedTestSpec ) const; + void add( char const* alias, char const* tag, SourceLineInfo const& lineInfo ); + static TagAliasRegistry& get(); + + private: + std::map<std::string, TagAlias> m_registry; + }; + +} // end namespace Catch + +namespace Catch { + + TagAliasRegistry::~TagAliasRegistry() {} + + Option<TagAlias> TagAliasRegistry::find( std::string const& alias ) const { + std::map<std::string, TagAlias>::const_iterator it = m_registry.find( alias ); + if( it != m_registry.end() ) + return it->second; + else + return Option<TagAlias>(); + } + + std::string TagAliasRegistry::expandAliases( std::string const& unexpandedTestSpec ) const { + std::string expandedTestSpec = unexpandedTestSpec; + for( std::map<std::string, TagAlias>::const_iterator it = m_registry.begin(), itEnd = m_registry.end(); + it != itEnd; + ++it ) { + std::size_t pos = expandedTestSpec.find( it->first ); + if( pos != std::string::npos ) { + expandedTestSpec = expandedTestSpec.substr( 0, pos ) + + it->second.tag + + expandedTestSpec.substr( pos + it->first.size() ); + } + } + return expandedTestSpec; + } + + void TagAliasRegistry::add( char const* alias, char const* tag, SourceLineInfo const& lineInfo ) { + + if( !startsWith( alias, "[@" ) || !endsWith( alias, ']' ) ) { + std::ostringstream oss; + oss << "error: tag alias, \"" << alias << "\" is not of the form [@alias name].\n" << lineInfo; + throw std::domain_error( oss.str().c_str() ); + } + if( !m_registry.insert( std::make_pair( alias, TagAlias( tag, lineInfo ) ) ).second ) { + std::ostringstream oss; + oss << "error: tag alias, \"" << alias << "\" already registered.\n" + << "\tFirst seen at " << find(alias)->lineInfo << '\n' + << "\tRedefined at " << lineInfo; + throw std::domain_error( oss.str().c_str() ); + } + } + + TagAliasRegistry& TagAliasRegistry::get() { + static TagAliasRegistry instance; + return instance; + + } + + ITagAliasRegistry::~ITagAliasRegistry() {} + ITagAliasRegistry const& ITagAliasRegistry::get() { return TagAliasRegistry::get(); } + + RegistrarForTagAliases::RegistrarForTagAliases( char const* alias, char const* tag, SourceLineInfo const& lineInfo ) { + try { + TagAliasRegistry::get().add( alias, tag, lineInfo ); + } + catch( std::exception& ex ) { + Colour colourGuard( Colour::Red ); + Catch::cerr() << ex.what() << std::endl; + exit(1); + } + } + +} // end namespace Catch + +// #included from: catch_matchers_string.hpp + +namespace Catch { +namespace Matchers { + + namespace StdString { + + CasedString::CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity ) + : m_caseSensitivity( caseSensitivity ), + m_str( adjustString( str ) ) + {} + std::string CasedString::adjustString( std::string const& str ) const { + return m_caseSensitivity == CaseSensitive::No + ? toLower( str ) + : str; + } + std::string CasedString::caseSensitivitySuffix() const { + return m_caseSensitivity == CaseSensitive::No + ? " (case insensitive)" + : std::string(); + } + + StringMatcherBase::StringMatcherBase( std::string operation, CasedString const& comparator ) + : m_comparator( comparator ), + m_operation( operation ) { + } + + std::string StringMatcherBase::describe() const { + std::string description; + description.reserve(5 + m_operation.size() + m_comparator.m_str.size() + + m_comparator.caseSensitivitySuffix().size()); + description += m_operation; + description += ": \""; + description += m_comparator.m_str; + description += "\""; + description += m_comparator.caseSensitivitySuffix(); + return description; + } + + EqualsMatcher::EqualsMatcher( CasedString const& comparator ) : StringMatcherBase( "equals", comparator ) {} + + bool EqualsMatcher::match( std::string const& source ) const { + return m_comparator.adjustString( source ) == m_comparator.m_str; + } + + ContainsMatcher::ContainsMatcher( CasedString const& comparator ) : StringMatcherBase( "contains", comparator ) {} + + bool ContainsMatcher::match( std::string const& source ) const { + return contains( m_comparator.adjustString( source ), m_comparator.m_str ); + } + + StartsWithMatcher::StartsWithMatcher( CasedString const& comparator ) : StringMatcherBase( "starts with", comparator ) {} + + bool StartsWithMatcher::match( std::string const& source ) const { + return startsWith( m_comparator.adjustString( source ), m_comparator.m_str ); + } + + EndsWithMatcher::EndsWithMatcher( CasedString const& comparator ) : StringMatcherBase( "ends with", comparator ) {} + + bool EndsWithMatcher::match( std::string const& source ) const { + return endsWith( m_comparator.adjustString( source ), m_comparator.m_str ); + } + + } // namespace StdString + + StdString::EqualsMatcher Equals( std::string const& str, CaseSensitive::Choice caseSensitivity ) { + return StdString::EqualsMatcher( StdString::CasedString( str, caseSensitivity) ); + } + StdString::ContainsMatcher Contains( std::string const& str, CaseSensitive::Choice caseSensitivity ) { + return StdString::ContainsMatcher( StdString::CasedString( str, caseSensitivity) ); + } + StdString::EndsWithMatcher EndsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) { + return StdString::EndsWithMatcher( StdString::CasedString( str, caseSensitivity) ); + } + StdString::StartsWithMatcher StartsWith( std::string const& str, CaseSensitive::Choice caseSensitivity ) { + return StdString::StartsWithMatcher( StdString::CasedString( str, caseSensitivity) ); + } + +} // namespace Matchers +} // namespace Catch +// #included from: ../reporters/catch_reporter_multi.hpp +#define TWOBLUECUBES_CATCH_REPORTER_MULTI_HPP_INCLUDED + +namespace Catch { + +class MultipleReporters : public SharedImpl<IStreamingReporter> { + typedef std::vector<Ptr<IStreamingReporter> > Reporters; + Reporters m_reporters; + +public: + void add( Ptr<IStreamingReporter> const& reporter ) { + m_reporters.push_back( reporter ); + } + +public: // IStreamingReporter + + virtual ReporterPreferences getPreferences() const CATCH_OVERRIDE { + return m_reporters[0]->getPreferences(); + } + + virtual void noMatchingTestCases( std::string const& spec ) CATCH_OVERRIDE { + for( Reporters::const_iterator it = m_reporters.begin(), itEnd = m_reporters.end(); + it != itEnd; + ++it ) + (*it)->noMatchingTestCases( spec ); + } + + virtual void testRunStarting( TestRunInfo const& testRunInfo ) CATCH_OVERRIDE { + for( Reporters::const_iterator it = m_reporters.begin(), itEnd = m_reporters.end(); + it != itEnd; + ++it ) + (*it)->testRunStarting( testRunInfo ); + } + + virtual void testGroupStarting( GroupInfo const& groupInfo ) CATCH_OVERRIDE { + for( Reporters::const_iterator it = m_reporters.begin(), itEnd = m_reporters.end(); + it != itEnd; + ++it ) + (*it)->testGroupStarting( groupInfo ); + } + + virtual void testCaseStarting( TestCaseInfo const& testInfo ) CATCH_OVERRIDE { + for( Reporters::const_iterator it = m_reporters.begin(), itEnd = m_reporters.end(); + it != itEnd; + ++it ) + (*it)->testCaseStarting( testInfo ); + } + + virtual void sectionStarting( SectionInfo const& sectionInfo ) CATCH_OVERRIDE { + for( Reporters::const_iterator it = m_reporters.begin(), itEnd = m_reporters.end(); + it != itEnd; + ++it ) + (*it)->sectionStarting( sectionInfo ); + } + + virtual void assertionStarting( AssertionInfo const& assertionInfo ) CATCH_OVERRIDE { + for( Reporters::const_iterator it = m_reporters.begin(), itEnd = m_reporters.end(); + it != itEnd; + ++it ) + (*it)->assertionStarting( assertionInfo ); + } + + // The return value indicates if the messages buffer should be cleared: + virtual bool assertionEnded( AssertionStats const& assertionStats ) CATCH_OVERRIDE { + bool clearBuffer = false; + for( Reporters::const_iterator it = m_reporters.begin(), itEnd = m_reporters.end(); + it != itEnd; + ++it ) + clearBuffer |= (*it)->assertionEnded( assertionStats ); + return clearBuffer; + } + + virtual void sectionEnded( SectionStats const& sectionStats ) CATCH_OVERRIDE { + for( Reporters::const_iterator it = m_reporters.begin(), itEnd = m_reporters.end(); + it != itEnd; + ++it ) + (*it)->sectionEnded( sectionStats ); + } + + virtual void testCaseEnded( TestCaseStats const& testCaseStats ) CATCH_OVERRIDE { + for( Reporters::const_iterator it = m_reporters.begin(), itEnd = m_reporters.end(); + it != itEnd; + ++it ) + (*it)->testCaseEnded( testCaseStats ); + } + + virtual void testGroupEnded( TestGroupStats const& testGroupStats ) CATCH_OVERRIDE { + for( Reporters::const_iterator it = m_reporters.begin(), itEnd = m_reporters.end(); + it != itEnd; + ++it ) + (*it)->testGroupEnded( testGroupStats ); + } + + virtual void testRunEnded( TestRunStats const& testRunStats ) CATCH_OVERRIDE { + for( Reporters::const_iterator it = m_reporters.begin(), itEnd = m_reporters.end(); + it != itEnd; + ++it ) + (*it)->testRunEnded( testRunStats ); + } + + virtual void skipTest( TestCaseInfo const& testInfo ) CATCH_OVERRIDE { + for( Reporters::const_iterator it = m_reporters.begin(), itEnd = m_reporters.end(); + it != itEnd; + ++it ) + (*it)->skipTest( testInfo ); + } + + virtual MultipleReporters* tryAsMulti() CATCH_OVERRIDE { + return this; + } + +}; + +Ptr<IStreamingReporter> addReporter( Ptr<IStreamingReporter> const& existingReporter, Ptr<IStreamingReporter> const& additionalReporter ) { + Ptr<IStreamingReporter> resultingReporter; + + if( existingReporter ) { + MultipleReporters* multi = existingReporter->tryAsMulti(); + if( !multi ) { + multi = new MultipleReporters; + resultingReporter = Ptr<IStreamingReporter>( multi ); + if( existingReporter ) + multi->add( existingReporter ); + } + else + resultingReporter = existingReporter; + multi->add( additionalReporter ); + } + else + resultingReporter = additionalReporter; + + return resultingReporter; +} + +} // end namespace Catch + +// #included from: ../reporters/catch_reporter_xml.hpp +#define TWOBLUECUBES_CATCH_REPORTER_XML_HPP_INCLUDED + +// #included from: catch_reporter_bases.hpp +#define TWOBLUECUBES_CATCH_REPORTER_BASES_HPP_INCLUDED + +#include <cstring> +#include <assert.h> + +namespace Catch { + + struct StreamingReporterBase : SharedImpl<IStreamingReporter> { + + StreamingReporterBase( ReporterConfig const& _config ) + : m_config( _config.fullConfig() ), + stream( _config.stream() ) + { + m_reporterPrefs.shouldRedirectStdOut = false; + } + + virtual ReporterPreferences getPreferences() const CATCH_OVERRIDE { + return m_reporterPrefs; + } + + virtual ~StreamingReporterBase() CATCH_OVERRIDE; + + virtual void noMatchingTestCases( std::string const& ) CATCH_OVERRIDE {} + + virtual void testRunStarting( TestRunInfo const& _testRunInfo ) CATCH_OVERRIDE { + currentTestRunInfo = _testRunInfo; + } + virtual void testGroupStarting( GroupInfo const& _groupInfo ) CATCH_OVERRIDE { + currentGroupInfo = _groupInfo; + } + + virtual void testCaseStarting( TestCaseInfo const& _testInfo ) CATCH_OVERRIDE { + currentTestCaseInfo = _testInfo; + } + virtual void sectionStarting( SectionInfo const& _sectionInfo ) CATCH_OVERRIDE { + m_sectionStack.push_back( _sectionInfo ); + } + + virtual void sectionEnded( SectionStats const& /* _sectionStats */ ) CATCH_OVERRIDE { + m_sectionStack.pop_back(); + } + virtual void testCaseEnded( TestCaseStats const& /* _testCaseStats */ ) CATCH_OVERRIDE { + currentTestCaseInfo.reset(); + } + virtual void testGroupEnded( TestGroupStats const& /* _testGroupStats */ ) CATCH_OVERRIDE { + currentGroupInfo.reset(); + } + virtual void testRunEnded( TestRunStats const& /* _testRunStats */ ) CATCH_OVERRIDE { + currentTestCaseInfo.reset(); + currentGroupInfo.reset(); + currentTestRunInfo.reset(); + } + + virtual void skipTest( TestCaseInfo const& ) CATCH_OVERRIDE { + // Don't do anything with this by default. + // It can optionally be overridden in the derived class. + } + + Ptr<IConfig const> m_config; + std::ostream& stream; + + LazyStat<TestRunInfo> currentTestRunInfo; + LazyStat<GroupInfo> currentGroupInfo; + LazyStat<TestCaseInfo> currentTestCaseInfo; + + std::vector<SectionInfo> m_sectionStack; + ReporterPreferences m_reporterPrefs; + }; + + struct CumulativeReporterBase : SharedImpl<IStreamingReporter> { + template<typename T, typename ChildNodeT> + struct Node : SharedImpl<> { + explicit Node( T const& _value ) : value( _value ) {} + virtual ~Node() {} + + typedef std::vector<Ptr<ChildNodeT> > ChildNodes; + T value; + ChildNodes children; + }; + struct SectionNode : SharedImpl<> { + explicit SectionNode( SectionStats const& _stats ) : stats( _stats ) {} + virtual ~SectionNode(); + + bool operator == ( SectionNode const& other ) const { + return stats.sectionInfo.lineInfo == other.stats.sectionInfo.lineInfo; + } + bool operator == ( Ptr<SectionNode> const& other ) const { + return operator==( *other ); + } + + SectionStats stats; + typedef std::vector<Ptr<SectionNode> > ChildSections; + typedef std::vector<AssertionStats> Assertions; + ChildSections childSections; + Assertions assertions; + std::string stdOut; + std::string stdErr; + }; + + struct BySectionInfo { + BySectionInfo( SectionInfo const& other ) : m_other( other ) {} + BySectionInfo( BySectionInfo const& other ) : m_other( other.m_other ) {} + bool operator() ( Ptr<SectionNode> const& node ) const { + return node->stats.sectionInfo.lineInfo == m_other.lineInfo; + } + private: + void operator=( BySectionInfo const& ); + SectionInfo const& m_other; + }; + + typedef Node<TestCaseStats, SectionNode> TestCaseNode; + typedef Node<TestGroupStats, TestCaseNode> TestGroupNode; + typedef Node<TestRunStats, TestGroupNode> TestRunNode; + + CumulativeReporterBase( ReporterConfig const& _config ) + : m_config( _config.fullConfig() ), + stream( _config.stream() ) + { + m_reporterPrefs.shouldRedirectStdOut = false; + } + ~CumulativeReporterBase(); + + virtual ReporterPreferences getPreferences() const CATCH_OVERRIDE { + return m_reporterPrefs; + } + + virtual void testRunStarting( TestRunInfo const& ) CATCH_OVERRIDE {} + virtual void testGroupStarting( GroupInfo const& ) CATCH_OVERRIDE {} + + virtual void testCaseStarting( TestCaseInfo const& ) CATCH_OVERRIDE {} + + virtual void sectionStarting( SectionInfo const& sectionInfo ) CATCH_OVERRIDE { + SectionStats incompleteStats( sectionInfo, Counts(), 0, false ); + Ptr<SectionNode> node; + if( m_sectionStack.empty() ) { + if( !m_rootSection ) + m_rootSection = new SectionNode( incompleteStats ); + node = m_rootSection; + } + else { + SectionNode& parentNode = *m_sectionStack.back(); + SectionNode::ChildSections::const_iterator it = + std::find_if( parentNode.childSections.begin(), + parentNode.childSections.end(), + BySectionInfo( sectionInfo ) ); + if( it == parentNode.childSections.end() ) { + node = new SectionNode( incompleteStats ); + parentNode.childSections.push_back( node ); + } + else + node = *it; + } + m_sectionStack.push_back( node ); + m_deepestSection = node; + } + + virtual void assertionStarting( AssertionInfo const& ) CATCH_OVERRIDE {} + + virtual bool assertionEnded( AssertionStats const& assertionStats ) CATCH_OVERRIDE { + assert( !m_sectionStack.empty() ); + SectionNode& sectionNode = *m_sectionStack.back(); + sectionNode.assertions.push_back( assertionStats ); + // AssertionResult holds a pointer to a temporary DecomposedExpression, + // which getExpandedExpression() calls to build the expression string. + // Our section stack copy of the assertionResult will likely outlive the + // temporary, so it must be expanded or discarded now to avoid calling + // a destroyed object later. + prepareExpandedExpression( sectionNode.assertions.back().assertionResult ); + return true; + } + virtual void sectionEnded( SectionStats const& sectionStats ) CATCH_OVERRIDE { + assert( !m_sectionStack.empty() ); + SectionNode& node = *m_sectionStack.back(); + node.stats = sectionStats; + m_sectionStack.pop_back(); + } + virtual void testCaseEnded( TestCaseStats const& testCaseStats ) CATCH_OVERRIDE { + Ptr<TestCaseNode> node = new TestCaseNode( testCaseStats ); + assert( m_sectionStack.size() == 0 ); + node->children.push_back( m_rootSection ); + m_testCases.push_back( node ); + m_rootSection.reset(); + + assert( m_deepestSection ); + m_deepestSection->stdOut = testCaseStats.stdOut; + m_deepestSection->stdErr = testCaseStats.stdErr; + } + virtual void testGroupEnded( TestGroupStats const& testGroupStats ) CATCH_OVERRIDE { + Ptr<TestGroupNode> node = new TestGroupNode( testGroupStats ); + node->children.swap( m_testCases ); + m_testGroups.push_back( node ); + } + virtual void testRunEnded( TestRunStats const& testRunStats ) CATCH_OVERRIDE { + Ptr<TestRunNode> node = new TestRunNode( testRunStats ); + node->children.swap( m_testGroups ); + m_testRuns.push_back( node ); + testRunEndedCumulative(); + } + virtual void testRunEndedCumulative() = 0; + + virtual void skipTest( TestCaseInfo const& ) CATCH_OVERRIDE {} + + virtual void prepareExpandedExpression( AssertionResult& result ) const { + if( result.isOk() ) + result.discardDecomposedExpression(); + else + result.expandDecomposedExpression(); + } + + Ptr<IConfig const> m_config; + std::ostream& stream; + std::vector<AssertionStats> m_assertions; + std::vector<std::vector<Ptr<SectionNode> > > m_sections; + std::vector<Ptr<TestCaseNode> > m_testCases; + std::vector<Ptr<TestGroupNode> > m_testGroups; + + std::vector<Ptr<TestRunNode> > m_testRuns; + + Ptr<SectionNode> m_rootSection; + Ptr<SectionNode> m_deepestSection; + std::vector<Ptr<SectionNode> > m_sectionStack; + ReporterPreferences m_reporterPrefs; + + }; + + template<char C> + char const* getLineOfChars() { + static char line[CATCH_CONFIG_CONSOLE_WIDTH] = {0}; + if( !*line ) { + std::memset( line, C, CATCH_CONFIG_CONSOLE_WIDTH-1 ); + line[CATCH_CONFIG_CONSOLE_WIDTH-1] = 0; + } + return line; + } + + struct TestEventListenerBase : StreamingReporterBase { + TestEventListenerBase( ReporterConfig const& _config ) + : StreamingReporterBase( _config ) + {} + + virtual void assertionStarting( AssertionInfo const& ) CATCH_OVERRIDE {} + virtual bool assertionEnded( AssertionStats const& ) CATCH_OVERRIDE { + return false; + } + }; + +} // end namespace Catch + +// #included from: ../internal/catch_reporter_registrars.hpp +#define TWOBLUECUBES_CATCH_REPORTER_REGISTRARS_HPP_INCLUDED + +namespace Catch { + + template<typename T> + class LegacyReporterRegistrar { + + class ReporterFactory : public IReporterFactory { + virtual IStreamingReporter* create( ReporterConfig const& config ) const { + return new LegacyReporterAdapter( new T( config ) ); + } + + virtual std::string getDescription() const { + return T::getDescription(); + } + }; + + public: + + LegacyReporterRegistrar( std::string const& name ) { + getMutableRegistryHub().registerReporter( name, new ReporterFactory() ); + } + }; + + template<typename T> + class ReporterRegistrar { + + class ReporterFactory : public SharedImpl<IReporterFactory> { + + // *** Please Note ***: + // - If you end up here looking at a compiler error because it's trying to register + // your custom reporter class be aware that the native reporter interface has changed + // to IStreamingReporter. The "legacy" interface, IReporter, is still supported via + // an adapter. Just use REGISTER_LEGACY_REPORTER to take advantage of the adapter. + // However please consider updating to the new interface as the old one is now + // deprecated and will probably be removed quite soon! + // Please contact me via github if you have any questions at all about this. + // In fact, ideally, please contact me anyway to let me know you've hit this - as I have + // no idea who is actually using custom reporters at all (possibly no-one!). + // The new interface is designed to minimise exposure to interface changes in the future. + virtual IStreamingReporter* create( ReporterConfig const& config ) const { + return new T( config ); + } + + virtual std::string getDescription() const { + return T::getDescription(); + } + }; + + public: + + ReporterRegistrar( std::string const& name ) { + getMutableRegistryHub().registerReporter( name, new ReporterFactory() ); + } + }; + + template<typename T> + class ListenerRegistrar { + + class ListenerFactory : public SharedImpl<IReporterFactory> { + + virtual IStreamingReporter* create( ReporterConfig const& config ) const { + return new T( config ); + } + virtual std::string getDescription() const { + return std::string(); + } + }; + + public: + + ListenerRegistrar() { + getMutableRegistryHub().registerListener( new ListenerFactory() ); + } + }; +} + +#define INTERNAL_CATCH_REGISTER_LEGACY_REPORTER( name, reporterType ) \ + namespace{ Catch::LegacyReporterRegistrar<reporterType> catch_internal_RegistrarFor##reporterType( name ); } + +#define INTERNAL_CATCH_REGISTER_REPORTER( name, reporterType ) \ + namespace{ Catch::ReporterRegistrar<reporterType> catch_internal_RegistrarFor##reporterType( name ); } + +#define INTERNAL_CATCH_REGISTER_LISTENER( listenerType ) \ + namespace{ Catch::ListenerRegistrar<listenerType> catch_internal_RegistrarFor##listenerType; } + +// #included from: ../internal/catch_xmlwriter.hpp +#define TWOBLUECUBES_CATCH_XMLWRITER_HPP_INCLUDED + +#include <sstream> +#include <string> +#include <vector> +#include <iomanip> + +namespace Catch { + + class XmlEncode { + public: + enum ForWhat { ForTextNodes, ForAttributes }; + + XmlEncode( std::string const& str, ForWhat forWhat = ForTextNodes ) + : m_str( str ), + m_forWhat( forWhat ) + {} + + void encodeTo( std::ostream& os ) const { + + // Apostrophe escaping not necessary if we always use " to write attributes + // (see: http://www.w3.org/TR/xml/#syntax) + + for( std::size_t i = 0; i < m_str.size(); ++ i ) { + char c = m_str[i]; + switch( c ) { + case '<': os << "<"; break; + case '&': os << "&"; break; + + case '>': + // See: http://www.w3.org/TR/xml/#syntax + if( i > 2 && m_str[i-1] == ']' && m_str[i-2] == ']' ) + os << ">"; + else + os << c; + break; + + case '\"': + if( m_forWhat == ForAttributes ) + os << """; + else + os << c; + break; + + default: + // Escape control chars - based on contribution by @espenalb in PR #465 and + // by @mrpi PR #588 + if ( ( c >= 0 && c < '\x09' ) || ( c > '\x0D' && c < '\x20') || c=='\x7F' ) { + // see http://stackoverflow.com/questions/404107/why-are-control-characters-illegal-in-xml-1-0 + os << "\\x" << std::uppercase << std::hex << std::setfill('0') << std::setw(2) + << static_cast<int>( c ); + } + else + os << c; + } + } + } + + friend std::ostream& operator << ( std::ostream& os, XmlEncode const& xmlEncode ) { + xmlEncode.encodeTo( os ); + return os; + } + + private: + std::string m_str; + ForWhat m_forWhat; + }; + + class XmlWriter { + public: + + class ScopedElement { + public: + ScopedElement( XmlWriter* writer ) + : m_writer( writer ) + {} + + ScopedElement( ScopedElement const& other ) + : m_writer( other.m_writer ){ + other.m_writer = CATCH_NULL; + } + + ~ScopedElement() { + if( m_writer ) + m_writer->endElement(); + } + + ScopedElement& writeText( std::string const& text, bool indent = true ) { + m_writer->writeText( text, indent ); + return *this; + } + + template<typename T> + ScopedElement& writeAttribute( std::string const& name, T const& attribute ) { + m_writer->writeAttribute( name, attribute ); + return *this; + } + + private: + mutable XmlWriter* m_writer; + }; + + XmlWriter() + : m_tagIsOpen( false ), + m_needsNewline( false ), + m_os( Catch::cout() ) + { + writeDeclaration(); + } + + XmlWriter( std::ostream& os ) + : m_tagIsOpen( false ), + m_needsNewline( false ), + m_os( os ) + { + writeDeclaration(); + } + + ~XmlWriter() { + while( !m_tags.empty() ) + endElement(); + } + + XmlWriter& startElement( std::string const& name ) { + ensureTagClosed(); + newlineIfNecessary(); + m_os << m_indent << '<' << name; + m_tags.push_back( name ); + m_indent += " "; + m_tagIsOpen = true; + return *this; + } + + ScopedElement scopedElement( std::string const& name ) { + ScopedElement scoped( this ); + startElement( name ); + return scoped; + } + + XmlWriter& endElement() { + newlineIfNecessary(); + m_indent = m_indent.substr( 0, m_indent.size()-2 ); + if( m_tagIsOpen ) { + m_os << "/>"; + m_tagIsOpen = false; + } + else { + m_os << m_indent << "</" << m_tags.back() << ">"; + } + m_os << std::endl; + m_tags.pop_back(); + return *this; + } + + XmlWriter& writeAttribute( std::string const& name, std::string const& attribute ) { + if( !name.empty() && !attribute.empty() ) + m_os << ' ' << name << "=\"" << XmlEncode( attribute, XmlEncode::ForAttributes ) << '"'; + return *this; + } + + XmlWriter& writeAttribute( std::string const& name, bool attribute ) { + m_os << ' ' << name << "=\"" << ( attribute ? "true" : "false" ) << '"'; + return *this; + } + + template<typename T> + XmlWriter& writeAttribute( std::string const& name, T const& attribute ) { + std::ostringstream oss; + oss << attribute; + return writeAttribute( name, oss.str() ); + } + + XmlWriter& writeText( std::string const& text, bool indent = true ) { + if( !text.empty() ){ + bool tagWasOpen = m_tagIsOpen; + ensureTagClosed(); + if( tagWasOpen && indent ) + m_os << m_indent; + m_os << XmlEncode( text ); + m_needsNewline = true; + } + return *this; + } + + XmlWriter& writeComment( std::string const& text ) { + ensureTagClosed(); + m_os << m_indent << "<!--" << text << "-->"; + m_needsNewline = true; + return *this; + } + + void writeStylesheetRef( std::string const& url ) { + m_os << "<?xml-stylesheet type=\"text/xsl\" href=\"" << url << "\"?>\n"; + } + + XmlWriter& writeBlankLine() { + ensureTagClosed(); + m_os << '\n'; + return *this; + } + + void ensureTagClosed() { + if( m_tagIsOpen ) { + m_os << ">" << std::endl; + m_tagIsOpen = false; + } + } + + private: + XmlWriter( XmlWriter const& ); + void operator=( XmlWriter const& ); + + void writeDeclaration() { + m_os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; + } + + void newlineIfNecessary() { + if( m_needsNewline ) { + m_os << std::endl; + m_needsNewline = false; + } + } + + bool m_tagIsOpen; + bool m_needsNewline; + std::vector<std::string> m_tags; + std::string m_indent; + std::ostream& m_os; + }; + +} +// #included from: catch_reenable_warnings.h + +#define TWOBLUECUBES_CATCH_REENABLE_WARNINGS_H_INCLUDED + +#ifdef __clang__ +# ifdef __ICC // icpc defines the __clang__ macro +# pragma warning(pop) +# else +# pragma clang diagnostic pop +# endif +#elif defined __GNUC__ +# pragma GCC diagnostic pop +#endif + + +namespace Catch { + class XmlReporter : public StreamingReporterBase { + public: + XmlReporter( ReporterConfig const& _config ) + : StreamingReporterBase( _config ), + m_xml(_config.stream()), + m_sectionDepth( 0 ) + { + m_reporterPrefs.shouldRedirectStdOut = true; + } + + virtual ~XmlReporter() CATCH_OVERRIDE; + + static std::string getDescription() { + return "Reports test results as an XML document"; + } + + virtual std::string getStylesheetRef() const { + return std::string(); + } + + void writeSourceInfo( SourceLineInfo const& sourceInfo ) { + m_xml + .writeAttribute( "filename", sourceInfo.file ) + .writeAttribute( "line", sourceInfo.line ); + } + + public: // StreamingReporterBase + + virtual void noMatchingTestCases( std::string const& s ) CATCH_OVERRIDE { + StreamingReporterBase::noMatchingTestCases( s ); + } + + virtual void testRunStarting( TestRunInfo const& testInfo ) CATCH_OVERRIDE { + StreamingReporterBase::testRunStarting( testInfo ); + std::string stylesheetRef = getStylesheetRef(); + if( !stylesheetRef.empty() ) + m_xml.writeStylesheetRef( stylesheetRef ); + m_xml.startElement( "Catch" ); + if( !m_config->name().empty() ) + m_xml.writeAttribute( "name", m_config->name() ); + } + + virtual void testGroupStarting( GroupInfo const& groupInfo ) CATCH_OVERRIDE { + StreamingReporterBase::testGroupStarting( groupInfo ); + m_xml.startElement( "Group" ) + .writeAttribute( "name", groupInfo.name ); + } + + virtual void testCaseStarting( TestCaseInfo const& testInfo ) CATCH_OVERRIDE { + StreamingReporterBase::testCaseStarting(testInfo); + m_xml.startElement( "TestCase" ) + .writeAttribute( "name", trim( testInfo.name ) ) + .writeAttribute( "description", testInfo.description ) + .writeAttribute( "tags", testInfo.tagsAsString ); + + writeSourceInfo( testInfo.lineInfo ); + + if ( m_config->showDurations() == ShowDurations::Always ) + m_testCaseTimer.start(); + m_xml.ensureTagClosed(); + } + + virtual void sectionStarting( SectionInfo const& sectionInfo ) CATCH_OVERRIDE { + StreamingReporterBase::sectionStarting( sectionInfo ); + if( m_sectionDepth++ > 0 ) { + m_xml.startElement( "Section" ) + .writeAttribute( "name", trim( sectionInfo.name ) ) + .writeAttribute( "description", sectionInfo.description ); + writeSourceInfo( sectionInfo.lineInfo ); + m_xml.ensureTagClosed(); + } + } + + virtual void assertionStarting( AssertionInfo const& ) CATCH_OVERRIDE { } + + virtual bool assertionEnded( AssertionStats const& assertionStats ) CATCH_OVERRIDE { + const AssertionResult& assertionResult = assertionStats.assertionResult; + + // Print any info messages in <Info> tags. + if( assertionStats.assertionResult.getResultType() != ResultWas::Ok ) { + for( std::vector<MessageInfo>::const_iterator it = assertionStats.infoMessages.begin(), itEnd = assertionStats.infoMessages.end(); + it != itEnd; + ++it ) { + if( it->type == ResultWas::Info ) { + m_xml.scopedElement( "Info" ) + .writeText( it->message ); + } else if ( it->type == ResultWas::Warning ) { + m_xml.scopedElement( "Warning" ) + .writeText( it->message ); + } + } + } + + // Drop out if result was successful but we're not printing them. + if( !m_config->includeSuccessfulResults() && isOk(assertionResult.getResultType()) ) + return true; + + // Print the expression if there is one. + if( assertionResult.hasExpression() ) { + m_xml.startElement( "Expression" ) + .writeAttribute( "success", assertionResult.succeeded() ) + .writeAttribute( "type", assertionResult.getTestMacroName() ); + + writeSourceInfo( assertionResult.getSourceInfo() ); + + m_xml.scopedElement( "Original" ) + .writeText( assertionResult.getExpression() ); + m_xml.scopedElement( "Expanded" ) + .writeText( assertionResult.getExpandedExpression() ); + } + + // And... Print a result applicable to each result type. + switch( assertionResult.getResultType() ) { + case ResultWas::ThrewException: + m_xml.startElement( "Exception" ); + writeSourceInfo( assertionResult.getSourceInfo() ); + m_xml.writeText( assertionResult.getMessage() ); + m_xml.endElement(); + break; + case ResultWas::FatalErrorCondition: + m_xml.startElement( "FatalErrorCondition" ); + writeSourceInfo( assertionResult.getSourceInfo() ); + m_xml.writeText( assertionResult.getMessage() ); + m_xml.endElement(); + break; + case ResultWas::Info: + m_xml.scopedElement( "Info" ) + .writeText( assertionResult.getMessage() ); + break; + case ResultWas::Warning: + // Warning will already have been written + break; + case ResultWas::ExplicitFailure: + m_xml.startElement( "Failure" ); + writeSourceInfo( assertionResult.getSourceInfo() ); + m_xml.writeText( assertionResult.getMessage() ); + m_xml.endElement(); + break; + default: + break; + } + + if( assertionResult.hasExpression() ) + m_xml.endElement(); + + return true; + } + + virtual void sectionEnded( SectionStats const& sectionStats ) CATCH_OVERRIDE { + StreamingReporterBase::sectionEnded( sectionStats ); + if( --m_sectionDepth > 0 ) { + XmlWriter::ScopedElement e = m_xml.scopedElement( "OverallResults" ); + e.writeAttribute( "successes", sectionStats.assertions.passed ); + e.writeAttribute( "failures", sectionStats.assertions.failed ); + e.writeAttribute( "expectedFailures", sectionStats.assertions.failedButOk ); + + if ( m_config->showDurations() == ShowDurations::Always ) + e.writeAttribute( "durationInSeconds", sectionStats.durationInSeconds ); + + m_xml.endElement(); + } + } + + virtual void testCaseEnded( TestCaseStats const& testCaseStats ) CATCH_OVERRIDE { + StreamingReporterBase::testCaseEnded( testCaseStats ); + XmlWriter::ScopedElement e = m_xml.scopedElement( "OverallResult" ); + e.writeAttribute( "success", testCaseStats.totals.assertions.allOk() ); + + if ( m_config->showDurations() == ShowDurations::Always ) + e.writeAttribute( "durationInSeconds", m_testCaseTimer.getElapsedSeconds() ); + + if( !testCaseStats.stdOut.empty() ) + m_xml.scopedElement( "StdOut" ).writeText( trim( testCaseStats.stdOut ), false ); + if( !testCaseStats.stdErr.empty() ) + m_xml.scopedElement( "StdErr" ).writeText( trim( testCaseStats.stdErr ), false ); + + m_xml.endElement(); + } + + virtual void testGroupEnded( TestGroupStats const& testGroupStats ) CATCH_OVERRIDE { + StreamingReporterBase::testGroupEnded( testGroupStats ); + // TODO: Check testGroupStats.aborting and act accordingly. + m_xml.scopedElement( "OverallResults" ) + .writeAttribute( "successes", testGroupStats.totals.assertions.passed ) + .writeAttribute( "failures", testGroupStats.totals.assertions.failed ) + .writeAttribute( "expectedFailures", testGroupStats.totals.assertions.failedButOk ); + m_xml.endElement(); + } + + virtual void testRunEnded( TestRunStats const& testRunStats ) CATCH_OVERRIDE { + StreamingReporterBase::testRunEnded( testRunStats ); + m_xml.scopedElement( "OverallResults" ) + .writeAttribute( "successes", testRunStats.totals.assertions.passed ) + .writeAttribute( "failures", testRunStats.totals.assertions.failed ) + .writeAttribute( "expectedFailures", testRunStats.totals.assertions.failedButOk ); + m_xml.endElement(); + } + + private: + Timer m_testCaseTimer; + XmlWriter m_xml; + int m_sectionDepth; + }; + + INTERNAL_CATCH_REGISTER_REPORTER( "xml", XmlReporter ) + +} // end namespace Catch + +// #included from: ../reporters/catch_reporter_junit.hpp +#define TWOBLUECUBES_CATCH_REPORTER_JUNIT_HPP_INCLUDED + +#include <assert.h> + +namespace Catch { + + namespace { + std::string getCurrentTimestamp() { + // Beware, this is not reentrant because of backward compatibility issues + // Also, UTC only, again because of backward compatibility (%z is C++11) + time_t rawtime; + std::time(&rawtime); + const size_t timeStampSize = sizeof("2017-01-16T17:06:45Z"); + +#ifdef _MSC_VER + std::tm timeInfo = {}; + gmtime_s(&timeInfo, &rawtime); +#else + std::tm* timeInfo; + timeInfo = std::gmtime(&rawtime); +#endif + + char timeStamp[timeStampSize]; + const char * const fmt = "%Y-%m-%dT%H:%M:%SZ"; + +#ifdef _MSC_VER + std::strftime(timeStamp, timeStampSize, fmt, &timeInfo); +#else + std::strftime(timeStamp, timeStampSize, fmt, timeInfo); +#endif + return std::string(timeStamp); + } + + } + + class JunitReporter : public CumulativeReporterBase { + public: + JunitReporter( ReporterConfig const& _config ) + : CumulativeReporterBase( _config ), + xml( _config.stream() ) + { + m_reporterPrefs.shouldRedirectStdOut = true; + } + + virtual ~JunitReporter() CATCH_OVERRIDE; + + static std::string getDescription() { + return "Reports test results in an XML format that looks like Ant's junitreport target"; + } + + virtual void noMatchingTestCases( std::string const& /*spec*/ ) CATCH_OVERRIDE {} + + virtual void testRunStarting( TestRunInfo const& runInfo ) CATCH_OVERRIDE { + CumulativeReporterBase::testRunStarting( runInfo ); + xml.startElement( "testsuites" ); + } + + virtual void testGroupStarting( GroupInfo const& groupInfo ) CATCH_OVERRIDE { + suiteTimer.start(); + stdOutForSuite.str(""); + stdErrForSuite.str(""); + unexpectedExceptions = 0; + CumulativeReporterBase::testGroupStarting( groupInfo ); + } + + virtual bool assertionEnded( AssertionStats const& assertionStats ) CATCH_OVERRIDE { + if( assertionStats.assertionResult.getResultType() == ResultWas::ThrewException ) + unexpectedExceptions++; + return CumulativeReporterBase::assertionEnded( assertionStats ); + } + + virtual void testCaseEnded( TestCaseStats const& testCaseStats ) CATCH_OVERRIDE { + stdOutForSuite << testCaseStats.stdOut; + stdErrForSuite << testCaseStats.stdErr; + CumulativeReporterBase::testCaseEnded( testCaseStats ); + } + + virtual void testGroupEnded( TestGroupStats const& testGroupStats ) CATCH_OVERRIDE { + double suiteTime = suiteTimer.getElapsedSeconds(); + CumulativeReporterBase::testGroupEnded( testGroupStats ); + writeGroup( *m_testGroups.back(), suiteTime ); + } + + virtual void testRunEndedCumulative() CATCH_OVERRIDE { + xml.endElement(); + } + + void writeGroup( TestGroupNode const& groupNode, double suiteTime ) { + XmlWriter::ScopedElement e = xml.scopedElement( "testsuite" ); + TestGroupStats const& stats = groupNode.value; + xml.writeAttribute( "name", stats.groupInfo.name ); + xml.writeAttribute( "errors", unexpectedExceptions ); + xml.writeAttribute( "failures", stats.totals.assertions.failed-unexpectedExceptions ); + xml.writeAttribute( "tests", stats.totals.assertions.total() ); + xml.writeAttribute( "hostname", "tbd" ); // !TBD + if( m_config->showDurations() == ShowDurations::Never ) + xml.writeAttribute( "time", "" ); + else + xml.writeAttribute( "time", suiteTime ); + xml.writeAttribute( "timestamp", getCurrentTimestamp() ); + + // Write test cases + for( TestGroupNode::ChildNodes::const_iterator + it = groupNode.children.begin(), itEnd = groupNode.children.end(); + it != itEnd; + ++it ) + writeTestCase( **it ); + + xml.scopedElement( "system-out" ).writeText( trim( stdOutForSuite.str() ), false ); + xml.scopedElement( "system-err" ).writeText( trim( stdErrForSuite.str() ), false ); + } + + void writeTestCase( TestCaseNode const& testCaseNode ) { + TestCaseStats const& stats = testCaseNode.value; + + // All test cases have exactly one section - which represents the + // test case itself. That section may have 0-n nested sections + assert( testCaseNode.children.size() == 1 ); + SectionNode const& rootSection = *testCaseNode.children.front(); + + std::string className = stats.testInfo.className; + + if( className.empty() ) { + if( rootSection.childSections.empty() ) + className = "global"; + } + writeSection( className, "", rootSection ); + } + + void writeSection( std::string const& className, + std::string const& rootName, + SectionNode const& sectionNode ) { + std::string name = trim( sectionNode.stats.sectionInfo.name ); + if( !rootName.empty() ) + name = rootName + '/' + name; + + if( !sectionNode.assertions.empty() || + !sectionNode.stdOut.empty() || + !sectionNode.stdErr.empty() ) { + XmlWriter::ScopedElement e = xml.scopedElement( "testcase" ); + if( className.empty() ) { + xml.writeAttribute( "classname", name ); + xml.writeAttribute( "name", "root" ); + } + else { + xml.writeAttribute( "classname", className ); + xml.writeAttribute( "name", name ); + } + xml.writeAttribute( "time", Catch::toString( sectionNode.stats.durationInSeconds ) ); + + writeAssertions( sectionNode ); + + if( !sectionNode.stdOut.empty() ) + xml.scopedElement( "system-out" ).writeText( trim( sectionNode.stdOut ), false ); + if( !sectionNode.stdErr.empty() ) + xml.scopedElement( "system-err" ).writeText( trim( sectionNode.stdErr ), false ); + } + for( SectionNode::ChildSections::const_iterator + it = sectionNode.childSections.begin(), + itEnd = sectionNode.childSections.end(); + it != itEnd; + ++it ) + if( className.empty() ) + writeSection( name, "", **it ); + else + writeSection( className, name, **it ); + } + + void writeAssertions( SectionNode const& sectionNode ) { + for( SectionNode::Assertions::const_iterator + it = sectionNode.assertions.begin(), itEnd = sectionNode.assertions.end(); + it != itEnd; + ++it ) + writeAssertion( *it ); + } + void writeAssertion( AssertionStats const& stats ) { + AssertionResult const& result = stats.assertionResult; + if( !result.isOk() ) { + std::string elementName; + switch( result.getResultType() ) { + case ResultWas::ThrewException: + case ResultWas::FatalErrorCondition: + elementName = "error"; + break; + case ResultWas::ExplicitFailure: + elementName = "failure"; + break; + case ResultWas::ExpressionFailed: + elementName = "failure"; + break; + case ResultWas::DidntThrowException: + elementName = "failure"; + break; + + // We should never see these here: + case ResultWas::Info: + case ResultWas::Warning: + case ResultWas::Ok: + case ResultWas::Unknown: + case ResultWas::FailureBit: + case ResultWas::Exception: + elementName = "internalError"; + break; + } + + XmlWriter::ScopedElement e = xml.scopedElement( elementName ); + + xml.writeAttribute( "message", result.getExpandedExpression() ); + xml.writeAttribute( "type", result.getTestMacroName() ); + + std::ostringstream oss; + if( !result.getMessage().empty() ) + oss << result.getMessage() << '\n'; + for( std::vector<MessageInfo>::const_iterator + it = stats.infoMessages.begin(), + itEnd = stats.infoMessages.end(); + it != itEnd; + ++it ) + if( it->type == ResultWas::Info ) + oss << it->message << '\n'; + + oss << "at " << result.getSourceInfo(); + xml.writeText( oss.str(), false ); + } + } + + XmlWriter xml; + Timer suiteTimer; + std::ostringstream stdOutForSuite; + std::ostringstream stdErrForSuite; + unsigned int unexpectedExceptions; + }; + + INTERNAL_CATCH_REGISTER_REPORTER( "junit", JunitReporter ) + +} // end namespace Catch + +// #included from: ../reporters/catch_reporter_console.hpp +#define TWOBLUECUBES_CATCH_REPORTER_CONSOLE_HPP_INCLUDED + +#include <cfloat> +#include <cstdio> + +namespace Catch { + + namespace { + // Because formatting using c++ streams is stateful, drop down to C is required + // Alternatively we could use stringstream, but its performance is... not good. + std::string getFormattedDuration( double duration ) { + // Max exponent + 1 is required to represent the whole part + // + 1 for decimal point + // + 3 for the 3 decimal places + // + 1 for null terminator + const size_t maxDoubleSize = DBL_MAX_10_EXP + 1 + 1 + 3 + 1; + char buffer[maxDoubleSize]; +#ifdef _MSC_VER + sprintf_s(buffer, "%.3f", duration); +#else + sprintf(buffer, "%.3f", duration); +#endif + return std::string(buffer); + } + } + + struct ConsoleReporter : StreamingReporterBase { + ConsoleReporter( ReporterConfig const& _config ) + : StreamingReporterBase( _config ), + m_headerPrinted( false ) + {} + + virtual ~ConsoleReporter() CATCH_OVERRIDE; + static std::string getDescription() { + return "Reports test results as plain lines of text"; + } + + virtual void noMatchingTestCases( std::string const& spec ) CATCH_OVERRIDE { + stream << "No test cases matched '" << spec << '\'' << std::endl; + } + + virtual void assertionStarting( AssertionInfo const& ) CATCH_OVERRIDE { + } + + virtual bool assertionEnded( AssertionStats const& _assertionStats ) CATCH_OVERRIDE { + AssertionResult const& result = _assertionStats.assertionResult; + + bool printInfoMessages = true; + + // Drop out if result was successful and we're not printing those + if( !m_config->includeSuccessfulResults() && result.isOk() ) { + if( result.getResultType() != ResultWas::Warning ) + return false; + printInfoMessages = false; + } + + lazyPrint(); + + AssertionPrinter printer( stream, _assertionStats, printInfoMessages ); + printer.print(); + stream << std::endl; + return true; + } + + virtual void sectionStarting( SectionInfo const& _sectionInfo ) CATCH_OVERRIDE { + m_headerPrinted = false; + StreamingReporterBase::sectionStarting( _sectionInfo ); + } + virtual void sectionEnded( SectionStats const& _sectionStats ) CATCH_OVERRIDE { + if( _sectionStats.missingAssertions ) { + lazyPrint(); + Colour colour( Colour::ResultError ); + if( m_sectionStack.size() > 1 ) + stream << "\nNo assertions in section"; + else + stream << "\nNo assertions in test case"; + stream << " '" << _sectionStats.sectionInfo.name << "'\n" << std::endl; + } + if( m_config->showDurations() == ShowDurations::Always ) { + stream << getFormattedDuration(_sectionStats.durationInSeconds) << " s: " << _sectionStats.sectionInfo.name << std::endl; + } + if( m_headerPrinted ) { + m_headerPrinted = false; + } + StreamingReporterBase::sectionEnded( _sectionStats ); + } + + virtual void testCaseEnded( TestCaseStats const& _testCaseStats ) CATCH_OVERRIDE { + StreamingReporterBase::testCaseEnded( _testCaseStats ); + m_headerPrinted = false; + } + virtual void testGroupEnded( TestGroupStats const& _testGroupStats ) CATCH_OVERRIDE { + if( currentGroupInfo.used ) { + printSummaryDivider(); + stream << "Summary for group '" << _testGroupStats.groupInfo.name << "':\n"; + printTotals( _testGroupStats.totals ); + stream << '\n' << std::endl; + } + StreamingReporterBase::testGroupEnded( _testGroupStats ); + } + virtual void testRunEnded( TestRunStats const& _testRunStats ) CATCH_OVERRIDE { + printTotalsDivider( _testRunStats.totals ); + printTotals( _testRunStats.totals ); + stream << std::endl; + StreamingReporterBase::testRunEnded( _testRunStats ); + } + + private: + + class AssertionPrinter { + void operator= ( AssertionPrinter const& ); + public: + AssertionPrinter( std::ostream& _stream, AssertionStats const& _stats, bool _printInfoMessages ) + : stream( _stream ), + stats( _stats ), + result( _stats.assertionResult ), + colour( Colour::None ), + message( result.getMessage() ), + messages( _stats.infoMessages ), + printInfoMessages( _printInfoMessages ) + { + switch( result.getResultType() ) { + case ResultWas::Ok: + colour = Colour::Success; + passOrFail = "PASSED"; + //if( result.hasMessage() ) + if( _stats.infoMessages.size() == 1 ) + messageLabel = "with message"; + if( _stats.infoMessages.size() > 1 ) + messageLabel = "with messages"; + break; + case ResultWas::ExpressionFailed: + if( result.isOk() ) { + colour = Colour::Success; + passOrFail = "FAILED - but was ok"; + } + else { + colour = Colour::Error; + passOrFail = "FAILED"; + } + if( _stats.infoMessages.size() == 1 ) + messageLabel = "with message"; + if( _stats.infoMessages.size() > 1 ) + messageLabel = "with messages"; + break; + case ResultWas::ThrewException: + colour = Colour::Error; + passOrFail = "FAILED"; + messageLabel = "due to unexpected exception with message"; + break; + case ResultWas::FatalErrorCondition: + colour = Colour::Error; + passOrFail = "FAILED"; + messageLabel = "due to a fatal error condition"; + break; + case ResultWas::DidntThrowException: + colour = Colour::Error; + passOrFail = "FAILED"; + messageLabel = "because no exception was thrown where one was expected"; + break; + case ResultWas::Info: + messageLabel = "info"; + break; + case ResultWas::Warning: + messageLabel = "warning"; + break; + case ResultWas::ExplicitFailure: + passOrFail = "FAILED"; + colour = Colour::Error; + if( _stats.infoMessages.size() == 1 ) + messageLabel = "explicitly with message"; + if( _stats.infoMessages.size() > 1 ) + messageLabel = "explicitly with messages"; + break; + // These cases are here to prevent compiler warnings + case ResultWas::Unknown: + case ResultWas::FailureBit: + case ResultWas::Exception: + passOrFail = "** internal error **"; + colour = Colour::Error; + break; + } + } + + void print() const { + printSourceInfo(); + if( stats.totals.assertions.total() > 0 ) { + if( result.isOk() ) + stream << '\n'; + printResultType(); + printOriginalExpression(); + printReconstructedExpression(); + } + else { + stream << '\n'; + } + printMessage(); + } + + private: + void printResultType() const { + if( !passOrFail.empty() ) { + Colour colourGuard( colour ); + stream << passOrFail << ":\n"; + } + } + void printOriginalExpression() const { + if( result.hasExpression() ) { + Colour colourGuard( Colour::OriginalExpression ); + stream << " "; + stream << result.getExpressionInMacro(); + stream << '\n'; + } + } + void printReconstructedExpression() const { + if( result.hasExpandedExpression() ) { + stream << "with expansion:\n"; + Colour colourGuard( Colour::ReconstructedExpression ); + stream << Text( result.getExpandedExpression(), TextAttributes().setIndent(2) ) << '\n'; + } + } + void printMessage() const { + if( !messageLabel.empty() ) + stream << messageLabel << ':' << '\n'; + for( std::vector<MessageInfo>::const_iterator it = messages.begin(), itEnd = messages.end(); + it != itEnd; + ++it ) { + // If this assertion is a warning ignore any INFO messages + if( printInfoMessages || it->type != ResultWas::Info ) + stream << Text( it->message, TextAttributes().setIndent(2) ) << '\n'; + } + } + void printSourceInfo() const { + Colour colourGuard( Colour::FileName ); + stream << result.getSourceInfo() << ": "; + } + + std::ostream& stream; + AssertionStats const& stats; + AssertionResult const& result; + Colour::Code colour; + std::string passOrFail; + std::string messageLabel; + std::string message; + std::vector<MessageInfo> messages; + bool printInfoMessages; + }; + + void lazyPrint() { + + if( !currentTestRunInfo.used ) + lazyPrintRunInfo(); + if( !currentGroupInfo.used ) + lazyPrintGroupInfo(); + + if( !m_headerPrinted ) { + printTestCaseAndSectionHeader(); + m_headerPrinted = true; + } + } + void lazyPrintRunInfo() { + stream << '\n' << getLineOfChars<'~'>() << '\n'; + Colour colour( Colour::SecondaryText ); + stream << currentTestRunInfo->name + << " is a Catch v" << libraryVersion << " host application.\n" + << "Run with -? for options\n\n"; + + if( m_config->rngSeed() != 0 ) + stream << "Randomness seeded to: " << m_config->rngSeed() << "\n\n"; + + currentTestRunInfo.used = true; + } + void lazyPrintGroupInfo() { + if( !currentGroupInfo->name.empty() && currentGroupInfo->groupsCounts > 1 ) { + printClosedHeader( "Group: " + currentGroupInfo->name ); + currentGroupInfo.used = true; + } + } + void printTestCaseAndSectionHeader() { + assert( !m_sectionStack.empty() ); + printOpenHeader( currentTestCaseInfo->name ); + + if( m_sectionStack.size() > 1 ) { + Colour colourGuard( Colour::Headers ); + + std::vector<SectionInfo>::const_iterator + it = m_sectionStack.begin()+1, // Skip first section (test case) + itEnd = m_sectionStack.end(); + for( ; it != itEnd; ++it ) + printHeaderString( it->name, 2 ); + } + + SourceLineInfo lineInfo = m_sectionStack.back().lineInfo; + + if( !lineInfo.empty() ){ + stream << getLineOfChars<'-'>() << '\n'; + Colour colourGuard( Colour::FileName ); + stream << lineInfo << '\n'; + } + stream << getLineOfChars<'.'>() << '\n' << std::endl; + } + + void printClosedHeader( std::string const& _name ) { + printOpenHeader( _name ); + stream << getLineOfChars<'.'>() << '\n'; + } + void printOpenHeader( std::string const& _name ) { + stream << getLineOfChars<'-'>() << '\n'; + { + Colour colourGuard( Colour::Headers ); + printHeaderString( _name ); + } + } + + // if string has a : in first line will set indent to follow it on + // subsequent lines + void printHeaderString( std::string const& _string, std::size_t indent = 0 ) { + std::size_t i = _string.find( ": " ); + if( i != std::string::npos ) + i+=2; + else + i = 0; + stream << Text( _string, TextAttributes() + .setIndent( indent+i) + .setInitialIndent( indent ) ) << '\n'; + } + + struct SummaryColumn { + + SummaryColumn( std::string const& _label, Colour::Code _colour ) + : label( _label ), + colour( _colour ) + {} + SummaryColumn addRow( std::size_t count ) { + std::ostringstream oss; + oss << count; + std::string row = oss.str(); + for( std::vector<std::string>::iterator it = rows.begin(); it != rows.end(); ++it ) { + while( it->size() < row.size() ) + *it = ' ' + *it; + while( it->size() > row.size() ) + row = ' ' + row; + } + rows.push_back( row ); + return *this; + } + + std::string label; + Colour::Code colour; + std::vector<std::string> rows; + + }; + + void printTotals( Totals const& totals ) { + if( totals.testCases.total() == 0 ) { + stream << Colour( Colour::Warning ) << "No tests ran\n"; + } + else if( totals.assertions.total() > 0 && totals.testCases.allPassed() ) { + stream << Colour( Colour::ResultSuccess ) << "All tests passed"; + stream << " (" + << pluralise( totals.assertions.passed, "assertion" ) << " in " + << pluralise( totals.testCases.passed, "test case" ) << ')' + << '\n'; + } + else { + + std::vector<SummaryColumn> columns; + columns.push_back( SummaryColumn( "", Colour::None ) + .addRow( totals.testCases.total() ) + .addRow( totals.assertions.total() ) ); + columns.push_back( SummaryColumn( "passed", Colour::Success ) + .addRow( totals.testCases.passed ) + .addRow( totals.assertions.passed ) ); + columns.push_back( SummaryColumn( "failed", Colour::ResultError ) + .addRow( totals.testCases.failed ) + .addRow( totals.assertions.failed ) ); + columns.push_back( SummaryColumn( "failed as expected", Colour::ResultExpectedFailure ) + .addRow( totals.testCases.failedButOk ) + .addRow( totals.assertions.failedButOk ) ); + + printSummaryRow( "test cases", columns, 0 ); + printSummaryRow( "assertions", columns, 1 ); + } + } + void printSummaryRow( std::string const& label, std::vector<SummaryColumn> const& cols, std::size_t row ) { + for( std::vector<SummaryColumn>::const_iterator it = cols.begin(); it != cols.end(); ++it ) { + std::string value = it->rows[row]; + if( it->label.empty() ) { + stream << label << ": "; + if( value != "0" ) + stream << value; + else + stream << Colour( Colour::Warning ) << "- none -"; + } + else if( value != "0" ) { + stream << Colour( Colour::LightGrey ) << " | "; + stream << Colour( it->colour ) + << value << ' ' << it->label; + } + } + stream << '\n'; + } + + static std::size_t makeRatio( std::size_t number, std::size_t total ) { + std::size_t ratio = total > 0 ? CATCH_CONFIG_CONSOLE_WIDTH * number/ total : 0; + return ( ratio == 0 && number > 0 ) ? 1 : ratio; + } + static std::size_t& findMax( std::size_t& i, std::size_t& j, std::size_t& k ) { + if( i > j && i > k ) + return i; + else if( j > k ) + return j; + else + return k; + } + + void printTotalsDivider( Totals const& totals ) { + if( totals.testCases.total() > 0 ) { + std::size_t failedRatio = makeRatio( totals.testCases.failed, totals.testCases.total() ); + std::size_t failedButOkRatio = makeRatio( totals.testCases.failedButOk, totals.testCases.total() ); + std::size_t passedRatio = makeRatio( totals.testCases.passed, totals.testCases.total() ); + while( failedRatio + failedButOkRatio + passedRatio < CATCH_CONFIG_CONSOLE_WIDTH-1 ) + findMax( failedRatio, failedButOkRatio, passedRatio )++; + while( failedRatio + failedButOkRatio + passedRatio > CATCH_CONFIG_CONSOLE_WIDTH-1 ) + findMax( failedRatio, failedButOkRatio, passedRatio )--; + + stream << Colour( Colour::Error ) << std::string( failedRatio, '=' ); + stream << Colour( Colour::ResultExpectedFailure ) << std::string( failedButOkRatio, '=' ); + if( totals.testCases.allPassed() ) + stream << Colour( Colour::ResultSuccess ) << std::string( passedRatio, '=' ); + else + stream << Colour( Colour::Success ) << std::string( passedRatio, '=' ); + } + else { + stream << Colour( Colour::Warning ) << std::string( CATCH_CONFIG_CONSOLE_WIDTH-1, '=' ); + } + stream << '\n'; + } + void printSummaryDivider() { + stream << getLineOfChars<'-'>() << '\n'; + } + + private: + bool m_headerPrinted; + }; + + INTERNAL_CATCH_REGISTER_REPORTER( "console", ConsoleReporter ) + +} // end namespace Catch + +// #included from: ../reporters/catch_reporter_compact.hpp +#define TWOBLUECUBES_CATCH_REPORTER_COMPACT_HPP_INCLUDED + +namespace Catch { + + struct CompactReporter : StreamingReporterBase { + + CompactReporter( ReporterConfig const& _config ) + : StreamingReporterBase( _config ) + {} + + virtual ~CompactReporter(); + + static std::string getDescription() { + return "Reports test results on a single line, suitable for IDEs"; + } + + virtual ReporterPreferences getPreferences() const { + ReporterPreferences prefs; + prefs.shouldRedirectStdOut = false; + return prefs; + } + + virtual void noMatchingTestCases( std::string const& spec ) { + stream << "No test cases matched '" << spec << '\'' << std::endl; + } + + virtual void assertionStarting( AssertionInfo const& ) { + } + + virtual bool assertionEnded( AssertionStats const& _assertionStats ) { + AssertionResult const& result = _assertionStats.assertionResult; + + bool printInfoMessages = true; + + // Drop out if result was successful and we're not printing those + if( !m_config->includeSuccessfulResults() && result.isOk() ) { + if( result.getResultType() != ResultWas::Warning ) + return false; + printInfoMessages = false; + } + + AssertionPrinter printer( stream, _assertionStats, printInfoMessages ); + printer.print(); + + stream << std::endl; + return true; + } + + virtual void testRunEnded( TestRunStats const& _testRunStats ) { + printTotals( _testRunStats.totals ); + stream << '\n' << std::endl; + StreamingReporterBase::testRunEnded( _testRunStats ); + } + + private: + class AssertionPrinter { + void operator= ( AssertionPrinter const& ); + public: + AssertionPrinter( std::ostream& _stream, AssertionStats const& _stats, bool _printInfoMessages ) + : stream( _stream ) + , stats( _stats ) + , result( _stats.assertionResult ) + , messages( _stats.infoMessages ) + , itMessage( _stats.infoMessages.begin() ) + , printInfoMessages( _printInfoMessages ) + {} + + void print() { + printSourceInfo(); + + itMessage = messages.begin(); + + switch( result.getResultType() ) { + case ResultWas::Ok: + printResultType( Colour::ResultSuccess, passedString() ); + printOriginalExpression(); + printReconstructedExpression(); + if ( ! result.hasExpression() ) + printRemainingMessages( Colour::None ); + else + printRemainingMessages(); + break; + case ResultWas::ExpressionFailed: + if( result.isOk() ) + printResultType( Colour::ResultSuccess, failedString() + std::string( " - but was ok" ) ); + else + printResultType( Colour::Error, failedString() ); + printOriginalExpression(); + printReconstructedExpression(); + printRemainingMessages(); + break; + case ResultWas::ThrewException: + printResultType( Colour::Error, failedString() ); + printIssue( "unexpected exception with message:" ); + printMessage(); + printExpressionWas(); + printRemainingMessages(); + break; + case ResultWas::FatalErrorCondition: + printResultType( Colour::Error, failedString() ); + printIssue( "fatal error condition with message:" ); + printMessage(); + printExpressionWas(); + printRemainingMessages(); + break; + case ResultWas::DidntThrowException: + printResultType( Colour::Error, failedString() ); + printIssue( "expected exception, got none" ); + printExpressionWas(); + printRemainingMessages(); + break; + case ResultWas::Info: + printResultType( Colour::None, "info" ); + printMessage(); + printRemainingMessages(); + break; + case ResultWas::Warning: + printResultType( Colour::None, "warning" ); + printMessage(); + printRemainingMessages(); + break; + case ResultWas::ExplicitFailure: + printResultType( Colour::Error, failedString() ); + printIssue( "explicitly" ); + printRemainingMessages( Colour::None ); + break; + // These cases are here to prevent compiler warnings + case ResultWas::Unknown: + case ResultWas::FailureBit: + case ResultWas::Exception: + printResultType( Colour::Error, "** internal error **" ); + break; + } + } + + private: + // Colour::LightGrey + + static Colour::Code dimColour() { return Colour::FileName; } + +#ifdef CATCH_PLATFORM_MAC + static const char* failedString() { return "FAILED"; } + static const char* passedString() { return "PASSED"; } +#else + static const char* failedString() { return "failed"; } + static const char* passedString() { return "passed"; } +#endif + + void printSourceInfo() const { + Colour colourGuard( Colour::FileName ); + stream << result.getSourceInfo() << ':'; + } + + void printResultType( Colour::Code colour, std::string passOrFail ) const { + if( !passOrFail.empty() ) { + { + Colour colourGuard( colour ); + stream << ' ' << passOrFail; + } + stream << ':'; + } + } + + void printIssue( std::string issue ) const { + stream << ' ' << issue; + } + + void printExpressionWas() { + if( result.hasExpression() ) { + stream << ';'; + { + Colour colour( dimColour() ); + stream << " expression was:"; + } + printOriginalExpression(); + } + } + + void printOriginalExpression() const { + if( result.hasExpression() ) { + stream << ' ' << result.getExpression(); + } + } + + void printReconstructedExpression() const { + if( result.hasExpandedExpression() ) { + { + Colour colour( dimColour() ); + stream << " for: "; + } + stream << result.getExpandedExpression(); + } + } + + void printMessage() { + if ( itMessage != messages.end() ) { + stream << " '" << itMessage->message << '\''; + ++itMessage; + } + } + + void printRemainingMessages( Colour::Code colour = dimColour() ) { + if ( itMessage == messages.end() ) + return; + + // using messages.end() directly yields compilation error: + std::vector<MessageInfo>::const_iterator itEnd = messages.end(); + const std::size_t N = static_cast<std::size_t>( std::distance( itMessage, itEnd ) ); + + { + Colour colourGuard( colour ); + stream << " with " << pluralise( N, "message" ) << ':'; + } + + for(; itMessage != itEnd; ) { + // If this assertion is a warning ignore any INFO messages + if( printInfoMessages || itMessage->type != ResultWas::Info ) { + stream << " '" << itMessage->message << '\''; + if ( ++itMessage != itEnd ) { + Colour colourGuard( dimColour() ); + stream << " and"; + } + } + } + } + + private: + std::ostream& stream; + AssertionStats const& stats; + AssertionResult const& result; + std::vector<MessageInfo> messages; + std::vector<MessageInfo>::const_iterator itMessage; + bool printInfoMessages; + }; + + // Colour, message variants: + // - white: No tests ran. + // - red: Failed [both/all] N test cases, failed [both/all] M assertions. + // - white: Passed [both/all] N test cases (no assertions). + // - red: Failed N tests cases, failed M assertions. + // - green: Passed [both/all] N tests cases with M assertions. + + std::string bothOrAll( std::size_t count ) const { + return count == 1 ? std::string() : count == 2 ? "both " : "all " ; + } + + void printTotals( const Totals& totals ) const { + if( totals.testCases.total() == 0 ) { + stream << "No tests ran."; + } + else if( totals.testCases.failed == totals.testCases.total() ) { + Colour colour( Colour::ResultError ); + const std::string qualify_assertions_failed = + totals.assertions.failed == totals.assertions.total() ? + bothOrAll( totals.assertions.failed ) : std::string(); + stream << + "Failed " << bothOrAll( totals.testCases.failed ) + << pluralise( totals.testCases.failed, "test case" ) << ", " + "failed " << qualify_assertions_failed << + pluralise( totals.assertions.failed, "assertion" ) << '.'; + } + else if( totals.assertions.total() == 0 ) { + stream << + "Passed " << bothOrAll( totals.testCases.total() ) + << pluralise( totals.testCases.total(), "test case" ) + << " (no assertions)."; + } + else if( totals.assertions.failed ) { + Colour colour( Colour::ResultError ); + stream << + "Failed " << pluralise( totals.testCases.failed, "test case" ) << ", " + "failed " << pluralise( totals.assertions.failed, "assertion" ) << '.'; + } + else { + Colour colour( Colour::ResultSuccess ); + stream << + "Passed " << bothOrAll( totals.testCases.passed ) + << pluralise( totals.testCases.passed, "test case" ) << + " with " << pluralise( totals.assertions.passed, "assertion" ) << '.'; + } + } + }; + + INTERNAL_CATCH_REGISTER_REPORTER( "compact", CompactReporter ) + +} // end namespace Catch + +namespace Catch { + // These are all here to avoid warnings about not having any out of line + // virtual methods + NonCopyable::~NonCopyable() {} + IShared::~IShared() {} + IStream::~IStream() CATCH_NOEXCEPT {} + FileStream::~FileStream() CATCH_NOEXCEPT {} + CoutStream::~CoutStream() CATCH_NOEXCEPT {} + DebugOutStream::~DebugOutStream() CATCH_NOEXCEPT {} + StreamBufBase::~StreamBufBase() CATCH_NOEXCEPT {} + IContext::~IContext() {} + IResultCapture::~IResultCapture() {} + ITestCase::~ITestCase() {} + ITestCaseRegistry::~ITestCaseRegistry() {} + IRegistryHub::~IRegistryHub() {} + IMutableRegistryHub::~IMutableRegistryHub() {} + IExceptionTranslator::~IExceptionTranslator() {} + IExceptionTranslatorRegistry::~IExceptionTranslatorRegistry() {} + IReporter::~IReporter() {} + IReporterFactory::~IReporterFactory() {} + IReporterRegistry::~IReporterRegistry() {} + IStreamingReporter::~IStreamingReporter() {} + AssertionStats::~AssertionStats() {} + SectionStats::~SectionStats() {} + TestCaseStats::~TestCaseStats() {} + TestGroupStats::~TestGroupStats() {} + TestRunStats::~TestRunStats() {} + CumulativeReporterBase::SectionNode::~SectionNode() {} + CumulativeReporterBase::~CumulativeReporterBase() {} + + StreamingReporterBase::~StreamingReporterBase() {} + ConsoleReporter::~ConsoleReporter() {} + CompactReporter::~CompactReporter() {} + IRunner::~IRunner() {} + IMutableContext::~IMutableContext() {} + IConfig::~IConfig() {} + XmlReporter::~XmlReporter() {} + JunitReporter::~JunitReporter() {} + TestRegistry::~TestRegistry() {} + FreeFunctionTestCase::~FreeFunctionTestCase() {} + IGeneratorInfo::~IGeneratorInfo() {} + IGeneratorsForTest::~IGeneratorsForTest() {} + WildcardPattern::~WildcardPattern() {} + TestSpec::Pattern::~Pattern() {} + TestSpec::NamePattern::~NamePattern() {} + TestSpec::TagPattern::~TagPattern() {} + TestSpec::ExcludedPattern::~ExcludedPattern() {} + + void Config::dummy() {} + + namespace TestCaseTracking { + ITracker::~ITracker() {} + TrackerBase::~TrackerBase() {} + SectionTracker::~SectionTracker() {} + IndexTracker::~IndexTracker() {} + } +} + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#endif + +#ifdef CATCH_CONFIG_MAIN +// #included from: internal/catch_default_main.hpp +#define TWOBLUECUBES_CATCH_DEFAULT_MAIN_HPP_INCLUDED + +#ifndef __OBJC__ + +// Standard C/C++ main entry point +int main (int argc, char * argv[]) { + int result = Catch::Session().run( argc, argv ); + return ( result < 0xff ? result : 0xff ); +} + +#else // __OBJC__ + +// Objective-C entry point +int main (int argc, char * const argv[]) { +#if !CATCH_ARC_ENABLED + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; +#endif + + Catch::registerTestMethods(); + int result = Catch::Session().run( argc, (char* const*)argv ); + +#if !CATCH_ARC_ENABLED + [pool drain]; +#endif + + return ( result < 0xff ? result : 0xff ); +} + +#endif // __OBJC__ + +#endif + +#ifdef CLARA_CONFIG_MAIN_NOT_DEFINED +# undef CLARA_CONFIG_MAIN +#endif + +////// + +// If this config identifier is defined then all CATCH macros are prefixed with CATCH_ +#ifdef CATCH_CONFIG_PREFIX_ALL + +#define CATCH_REQUIRE( expr ) INTERNAL_CATCH_TEST( expr, Catch::ResultDisposition::Normal, "CATCH_REQUIRE" ) +#define CATCH_REQUIRE_FALSE( expr ) INTERNAL_CATCH_TEST( expr, Catch::ResultDisposition::Normal | Catch::ResultDisposition::FalseTest, "CATCH_REQUIRE_FALSE" ) + +#define CATCH_REQUIRE_THROWS( expr ) INTERNAL_CATCH_THROWS( expr, Catch::ResultDisposition::Normal, "", "CATCH_REQUIRE_THROWS" ) +#define CATCH_REQUIRE_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( expr, exceptionType, Catch::ResultDisposition::Normal, "CATCH_REQUIRE_THROWS_AS" ) +#define CATCH_REQUIRE_THROWS_WITH( expr, matcher ) INTERNAL_CATCH_THROWS( expr, Catch::ResultDisposition::Normal, matcher, "CATCH_REQUIRE_THROWS_WITH" ) +#define CATCH_REQUIRE_NOTHROW( expr ) INTERNAL_CATCH_NO_THROW( expr, Catch::ResultDisposition::Normal, "CATCH_REQUIRE_NOTHROW" ) + +#define CATCH_CHECK( expr ) INTERNAL_CATCH_TEST( expr, Catch::ResultDisposition::ContinueOnFailure, "CATCH_CHECK" ) +#define CATCH_CHECK_FALSE( expr ) INTERNAL_CATCH_TEST( expr, Catch::ResultDisposition::ContinueOnFailure | Catch::ResultDisposition::FalseTest, "CATCH_CHECK_FALSE" ) +#define CATCH_CHECKED_IF( expr ) INTERNAL_CATCH_IF( expr, Catch::ResultDisposition::ContinueOnFailure, "CATCH_CHECKED_IF" ) +#define CATCH_CHECKED_ELSE( expr ) INTERNAL_CATCH_ELSE( expr, Catch::ResultDisposition::ContinueOnFailure, "CATCH_CHECKED_ELSE" ) +#define CATCH_CHECK_NOFAIL( expr ) INTERNAL_CATCH_TEST( expr, Catch::ResultDisposition::ContinueOnFailure | Catch::ResultDisposition::SuppressFail, "CATCH_CHECK_NOFAIL" ) + +#define CATCH_CHECK_THROWS( expr ) INTERNAL_CATCH_THROWS( expr, Catch::ResultDisposition::ContinueOnFailure, "", "CATCH_CHECK_THROWS" ) +#define CATCH_CHECK_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( expr, exceptionType, Catch::ResultDisposition::ContinueOnFailure, "CATCH_CHECK_THROWS_AS" ) +#define CATCH_CHECK_THROWS_WITH( expr, matcher ) INTERNAL_CATCH_THROWS( expr, Catch::ResultDisposition::ContinueOnFailure, matcher, "CATCH_CHECK_THROWS_WITH" ) +#define CATCH_CHECK_NOTHROW( expr ) INTERNAL_CATCH_NO_THROW( expr, Catch::ResultDisposition::ContinueOnFailure, "CATCH_CHECK_NOTHROW" ) + +#define CATCH_CHECK_THAT( arg, matcher ) INTERNAL_CHECK_THAT( arg, matcher, Catch::ResultDisposition::ContinueOnFailure, "CATCH_CHECK_THAT" ) +#define CATCH_REQUIRE_THAT( arg, matcher ) INTERNAL_CHECK_THAT( arg, matcher, Catch::ResultDisposition::Normal, "CATCH_REQUIRE_THAT" ) + +#define CATCH_INFO( msg ) INTERNAL_CATCH_INFO( msg, "CATCH_INFO" ) +#define CATCH_WARN( msg ) INTERNAL_CATCH_MSG( Catch::ResultWas::Warning, Catch::ResultDisposition::ContinueOnFailure, "CATCH_WARN", msg ) +#define CATCH_SCOPED_INFO( msg ) INTERNAL_CATCH_INFO( msg, "CATCH_INFO" ) +#define CATCH_CAPTURE( msg ) INTERNAL_CATCH_INFO( #msg " := " << msg, "CATCH_CAPTURE" ) +#define CATCH_SCOPED_CAPTURE( msg ) INTERNAL_CATCH_INFO( #msg " := " << msg, "CATCH_CAPTURE" ) + +#ifdef CATCH_CONFIG_VARIADIC_MACROS + #define CATCH_TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE( __VA_ARGS__ ) + #define CATCH_TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, __VA_ARGS__ ) + #define CATCH_METHOD_AS_TEST_CASE( method, ... ) INTERNAL_CATCH_METHOD_AS_TEST_CASE( method, __VA_ARGS__ ) + #define CATCH_REGISTER_TEST_CASE( Function, ... ) INTERNAL_CATCH_REGISTER_TESTCASE( Function, __VA_ARGS__ ) + #define CATCH_SECTION( ... ) INTERNAL_CATCH_SECTION( __VA_ARGS__ ) + #define CATCH_FAIL( ... ) INTERNAL_CATCH_MSG( Catch::ResultWas::ExplicitFailure, Catch::ResultDisposition::Normal, "CATCH_FAIL", __VA_ARGS__ ) + #define CATCH_SUCCEED( ... ) INTERNAL_CATCH_MSG( Catch::ResultWas::Ok, Catch::ResultDisposition::ContinueOnFailure, "CATCH_SUCCEED", __VA_ARGS__ ) +#else + #define CATCH_TEST_CASE( name, description ) INTERNAL_CATCH_TESTCASE( name, description ) + #define CATCH_TEST_CASE_METHOD( className, name, description ) INTERNAL_CATCH_TEST_CASE_METHOD( className, name, description ) + #define CATCH_METHOD_AS_TEST_CASE( method, name, description ) INTERNAL_CATCH_METHOD_AS_TEST_CASE( method, name, description ) + #define CATCH_REGISTER_TEST_CASE( function, name, description ) INTERNAL_CATCH_REGISTER_TESTCASE( function, name, description ) + #define CATCH_SECTION( name, description ) INTERNAL_CATCH_SECTION( name, description ) + #define CATCH_FAIL( msg ) INTERNAL_CATCH_MSG( Catch::ResultWas::ExplicitFailure, Catch::ResultDisposition::Normal, "CATCH_FAIL", msg ) + #define CATCH_SUCCEED( msg ) INTERNAL_CATCH_MSG( Catch::ResultWas::Ok, Catch::ResultDisposition::ContinueOnFailure, "CATCH_SUCCEED", msg ) +#endif +#define CATCH_ANON_TEST_CASE() INTERNAL_CATCH_TESTCASE( "", "" ) + +#define CATCH_REGISTER_REPORTER( name, reporterType ) INTERNAL_CATCH_REGISTER_REPORTER( name, reporterType ) +#define CATCH_REGISTER_LEGACY_REPORTER( name, reporterType ) INTERNAL_CATCH_REGISTER_LEGACY_REPORTER( name, reporterType ) + +#define CATCH_GENERATE( expr) INTERNAL_CATCH_GENERATE( expr ) + +// "BDD-style" convenience wrappers +#ifdef CATCH_CONFIG_VARIADIC_MACROS +#define CATCH_SCENARIO( ... ) CATCH_TEST_CASE( "Scenario: " __VA_ARGS__ ) +#define CATCH_SCENARIO_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, "Scenario: " __VA_ARGS__ ) +#else +#define CATCH_SCENARIO( name, tags ) CATCH_TEST_CASE( "Scenario: " name, tags ) +#define CATCH_SCENARIO_METHOD( className, name, tags ) INTERNAL_CATCH_TEST_CASE_METHOD( className, "Scenario: " name, tags ) +#endif +#define CATCH_GIVEN( desc ) CATCH_SECTION( std::string( "Given: ") + desc, "" ) +#define CATCH_WHEN( desc ) CATCH_SECTION( std::string( " When: ") + desc, "" ) +#define CATCH_AND_WHEN( desc ) CATCH_SECTION( std::string( " And: ") + desc, "" ) +#define CATCH_THEN( desc ) CATCH_SECTION( std::string( " Then: ") + desc, "" ) +#define CATCH_AND_THEN( desc ) CATCH_SECTION( std::string( " And: ") + desc, "" ) + +// If CATCH_CONFIG_PREFIX_ALL is not defined then the CATCH_ prefix is not required +#else + +#define REQUIRE( expr ) INTERNAL_CATCH_TEST( expr, Catch::ResultDisposition::Normal, "REQUIRE" ) +#define REQUIRE_FALSE( expr ) INTERNAL_CATCH_TEST( expr, Catch::ResultDisposition::Normal | Catch::ResultDisposition::FalseTest, "REQUIRE_FALSE" ) + +#define REQUIRE_THROWS( expr ) INTERNAL_CATCH_THROWS( expr, Catch::ResultDisposition::Normal, "", "REQUIRE_THROWS" ) +#define REQUIRE_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( expr, exceptionType, Catch::ResultDisposition::Normal, "REQUIRE_THROWS_AS" ) +#define REQUIRE_THROWS_WITH( expr, matcher ) INTERNAL_CATCH_THROWS( expr, Catch::ResultDisposition::Normal, matcher, "REQUIRE_THROWS_WITH" ) +#define REQUIRE_NOTHROW( expr ) INTERNAL_CATCH_NO_THROW( expr, Catch::ResultDisposition::Normal, "REQUIRE_NOTHROW" ) + +#define CHECK( expr ) INTERNAL_CATCH_TEST( expr, Catch::ResultDisposition::ContinueOnFailure, "CHECK" ) +#define CHECK_FALSE( expr ) INTERNAL_CATCH_TEST( expr, Catch::ResultDisposition::ContinueOnFailure | Catch::ResultDisposition::FalseTest, "CHECK_FALSE" ) +#define CHECKED_IF( expr ) INTERNAL_CATCH_IF( expr, Catch::ResultDisposition::ContinueOnFailure, "CHECKED_IF" ) +#define CHECKED_ELSE( expr ) INTERNAL_CATCH_ELSE( expr, Catch::ResultDisposition::ContinueOnFailure, "CHECKED_ELSE" ) +#define CHECK_NOFAIL( expr ) INTERNAL_CATCH_TEST( expr, Catch::ResultDisposition::ContinueOnFailure | Catch::ResultDisposition::SuppressFail, "CHECK_NOFAIL" ) + +#define CHECK_THROWS( expr ) INTERNAL_CATCH_THROWS( expr, Catch::ResultDisposition::ContinueOnFailure, "", "CHECK_THROWS" ) +#define CHECK_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( expr, exceptionType, Catch::ResultDisposition::ContinueOnFailure, "CHECK_THROWS_AS" ) +#define CHECK_THROWS_WITH( expr, matcher ) INTERNAL_CATCH_THROWS( expr, Catch::ResultDisposition::ContinueOnFailure, matcher, "CHECK_THROWS_WITH" ) +#define CHECK_NOTHROW( expr ) INTERNAL_CATCH_NO_THROW( expr, Catch::ResultDisposition::ContinueOnFailure, "CHECK_NOTHROW" ) + +#define CHECK_THAT( arg, matcher ) INTERNAL_CHECK_THAT( arg, matcher, Catch::ResultDisposition::ContinueOnFailure, "CHECK_THAT" ) +#define REQUIRE_THAT( arg, matcher ) INTERNAL_CHECK_THAT( arg, matcher, Catch::ResultDisposition::Normal, "REQUIRE_THAT" ) + +#define INFO( msg ) INTERNAL_CATCH_INFO( msg, "INFO" ) +#define WARN( msg ) INTERNAL_CATCH_MSG( Catch::ResultWas::Warning, Catch::ResultDisposition::ContinueOnFailure, "WARN", msg ) +#define SCOPED_INFO( msg ) INTERNAL_CATCH_INFO( msg, "INFO" ) +#define CAPTURE( msg ) INTERNAL_CATCH_INFO( #msg " := " << msg, "CAPTURE" ) +#define SCOPED_CAPTURE( msg ) INTERNAL_CATCH_INFO( #msg " := " << msg, "CAPTURE" ) + +#ifdef CATCH_CONFIG_VARIADIC_MACROS + #define TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE( __VA_ARGS__ ) + #define TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, __VA_ARGS__ ) + #define METHOD_AS_TEST_CASE( method, ... ) INTERNAL_CATCH_METHOD_AS_TEST_CASE( method, __VA_ARGS__ ) + #define REGISTER_TEST_CASE( Function, ... ) INTERNAL_CATCH_REGISTER_TESTCASE( Function, __VA_ARGS__ ) + #define SECTION( ... ) INTERNAL_CATCH_SECTION( __VA_ARGS__ ) + #define FAIL( ... ) INTERNAL_CATCH_MSG( Catch::ResultWas::ExplicitFailure, Catch::ResultDisposition::Normal, "FAIL", __VA_ARGS__ ) + #define SUCCEED( ... ) INTERNAL_CATCH_MSG( Catch::ResultWas::Ok, Catch::ResultDisposition::ContinueOnFailure, "SUCCEED", __VA_ARGS__ ) +#else + #define TEST_CASE( name, description ) INTERNAL_CATCH_TESTCASE( name, description ) + #define TEST_CASE_METHOD( className, name, description ) INTERNAL_CATCH_TEST_CASE_METHOD( className, name, description ) + #define METHOD_AS_TEST_CASE( method, name, description ) INTERNAL_CATCH_METHOD_AS_TEST_CASE( method, name, description ) + #define REGISTER_TEST_CASE( method, name, description ) INTERNAL_CATCH_REGISTER_TESTCASE( method, name, description ) + #define SECTION( name, description ) INTERNAL_CATCH_SECTION( name, description ) + #define FAIL( msg ) INTERNAL_CATCH_MSG( Catch::ResultWas::ExplicitFailure, Catch::ResultDisposition::Normal, "FAIL", msg ) + #define SUCCEED( msg ) INTERNAL_CATCH_MSG( Catch::ResultWas::Ok, Catch::ResultDisposition::ContinueOnFailure, "SUCCEED", msg ) +#endif +#define ANON_TEST_CASE() INTERNAL_CATCH_TESTCASE( "", "" ) + +#define REGISTER_REPORTER( name, reporterType ) INTERNAL_CATCH_REGISTER_REPORTER( name, reporterType ) +#define REGISTER_LEGACY_REPORTER( name, reporterType ) INTERNAL_CATCH_REGISTER_LEGACY_REPORTER( name, reporterType ) + +#define GENERATE( expr) INTERNAL_CATCH_GENERATE( expr ) + +#endif + +#define CATCH_TRANSLATE_EXCEPTION( signature ) INTERNAL_CATCH_TRANSLATE_EXCEPTION( signature ) + +// "BDD-style" convenience wrappers +#ifdef CATCH_CONFIG_VARIADIC_MACROS +#define SCENARIO( ... ) TEST_CASE( "Scenario: " __VA_ARGS__ ) +#define SCENARIO_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, "Scenario: " __VA_ARGS__ ) +#else +#define SCENARIO( name, tags ) TEST_CASE( "Scenario: " name, tags ) +#define SCENARIO_METHOD( className, name, tags ) INTERNAL_CATCH_TEST_CASE_METHOD( className, "Scenario: " name, tags ) +#endif +#define GIVEN( desc ) SECTION( std::string(" Given: ") + desc, "" ) +#define WHEN( desc ) SECTION( std::string(" When: ") + desc, "" ) +#define AND_WHEN( desc ) SECTION( std::string("And when: ") + desc, "" ) +#define THEN( desc ) SECTION( std::string(" Then: ") + desc, "" ) +#define AND_THEN( desc ) SECTION( std::string(" And: ") + desc, "" ) + +using Catch::Detail::Approx; + +#endif // TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED + diff --git a/fuzzylite/test/hedge/HedgeFunctionTest.cpp b/fuzzylite/test/hedge/HedgeFunctionTest.cpp new file mode 100644 index 0000000..dab70f5 --- /dev/null +++ b/fuzzylite/test/hedge/HedgeFunctionTest.cpp @@ -0,0 +1,141 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "test/catch.hpp" +#include "fl/Headers.h" + +namespace fl { + + /** + * Tests: hedge/HedgeFunction + * + * @author Juan Rada-Vilela, Ph.D. + * + */ + + static std::string hedgeEngine() { +#ifdef FL_CPP98 + return ""; +#else + return R""( +Engine: Sugeno-Tip-Calculator +InputVariable: FoodQuality + enabled: true + range: 1.000 10.000 + lock-range: false + term: Bad Trapezoid 0.000 1.000 3.000 7.000 + term: Good Trapezoid 3.000 7.000 10.000 11.000 +InputVariable: Service + enabled: true + range: 1.000 10.000 + lock-range: false + term: Bad Trapezoid 0.000 1.000 3.000 7.000 + term: Good Trapezoid 3.000 7.000 10.000 11.000 +OutputVariable: CheapTip + enabled: true + range: 5.000 25.000 + lock-range: false + aggregation: none + defuzzifier: WeightedAverage TakagiSugeno + default: nan + lock-previous: false + term: Low Constant 10.000 + term: Medium Constant 15.000 + term: High Constant 20.000 +OutputVariable: AverageTip + enabled: true + range: 5.000 25.000 + lock-range: false + aggregation: none + defuzzifier: WeightedAverage TakagiSugeno + default: nan + lock-previous: false + term: Low Constant 10.000 + term: Medium Constant 15.000 + term: High Constant 20.000 +OutputVariable: GenerousTip + enabled: true + range: 5.000 25.000 + lock-range: false + aggregation: none + defuzzifier: WeightedAverage TakagiSugeno + default: nan + lock-previous: false + term: Low Constant 10.000 + term: Medium Constant 15.000 + term: High Constant 20.000 +RuleBlock: + enabled: true + conjunction: EinsteinProduct + disjunction: none + implication: none + activation: General + rule: if FoodQuality is extremely Bad and Service is extremely Bad then CheapTip is extremely Low and AverageTip is very Low and GenerousTip is Low + rule: if FoodQuality is Good and Service is extremely Bad then CheapTip is Low and AverageTip is Low and GenerousTip is Medium + rule: if FoodQuality is very Good and Service is very Bad then CheapTip is Low and AverageTip is Medium and GenerousTip is High + rule: if FoodQuality is Bad and Service is Bad then CheapTip is Low and AverageTip is Low and GenerousTip is Medium + rule: if FoodQuality is Good and Service is Bad then CheapTip is Low and AverageTip is Medium and GenerousTip is High + rule: if FoodQuality is extremely Good and Service is Bad then CheapTip is Low and AverageTip is Medium and GenerousTip is very High + rule: if FoodQuality is Bad and Service is Good then CheapTip is Low and AverageTip is Medium and GenerousTip is High + rule: if FoodQuality is Good and Service is Good then CheapTip is Medium and AverageTip is Medium and GenerousTip is very High + rule: if FoodQuality is very Bad and Service is very Good then CheapTip is Low and AverageTip is Medium and GenerousTip is High + rule: if FoodQuality is very very Good and Service is very very Good then CheapTip is High and AverageTip is very High and GenerousTip is extremely High +)""; +#endif + } + + static Hedge* myVeryConstructor() { + return new HedgeFunction("x*x"); + } + + static Hedge* myExtraVeryConstructor() { + return new HedgeFunction("x*x*x"); + } + + TEST_CASE("HedgeFunction x*x is equivalent to hedge Very", "[hedge][function]") { +#ifdef FL_CPP98 + FL_IUNUSED(&(hedgeEngine)); + FL_IUNUSED(&(myVeryConstructor)); + FL_IUNUSED(&(myExtraVeryConstructor)); + WARN("Test only runs with -DFL_CPP98=OFF"); + return; +#else + std::string fllEngine = hedgeEngine(); + //Import using regular hedge very + FL_unique_ptr<Engine> engine(FllImporter().fromString(fllEngine)); + std::string fldVery = FldExporter().toString(engine.get(), 1024); + + //Replace hedge very with a HedgeFunction(x*x) + HedgeFactory* factory = FactoryManager::instance()->hedge(); + factory->registerConstructor("very", &(myVeryConstructor)); + //Import again with new HedgeFunction + engine.reset(FllImporter().fromString(fllEngine)); + std::string anotherFld = FldExporter().toString(engine.get(), 1024); + //Both must be equal + CHECK(fldVery == anotherFld); + + //Replace very with a HedgeFunction(x*x*x) + factory->registerConstructor("very", &(myExtraVeryConstructor)); + + engine.reset(FllImporter().fromString(fllEngine)); + anotherFld = FldExporter().toString(engine.get(), 1024); + + //Must be different + CHECK(fldVery != anotherFld); +#endif + } + +} diff --git a/fuzzylite/test/imex/FldExporterTest.cpp b/fuzzylite/test/imex/FldExporterTest.cpp new file mode 100644 index 0000000..e312f1d --- /dev/null +++ b/fuzzylite/test/imex/FldExporterTest.cpp @@ -0,0 +1,45 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "test/catch.hpp" +#include "fl/Headers.h" + +namespace fl { + + TEST_CASE("Exports same number of values in scopes", "[imex]") { + FL_unique_ptr<Engine> engine(Console::mamdani()); + engine->addInputVariable(new InputVariable("Dummy2", 0, 1)); + engine->addInputVariable(new InputVariable("Dummy3", 0, 1)); + engine->addInputVariable(new InputVariable("Dummy4", 0, 1)); + + FldExporter exporter("\t"); + exporter.setExportHeader(false); + + int valuesEachVariable = 3; + int expectedValues = (int) std::pow(valuesEachVariable, 1.0 * engine->numberOfInputVariables()); + + std::string eachVariable = exporter.toString(engine.get(), valuesEachVariable, FldExporter::EachVariable); + // FL_LOG("eachVariable:\n" << eachVariable); + std::vector<std::string> linesByVariable = Op::split(eachVariable, "\n"); + CHECK(int(linesByVariable.size()) == expectedValues); + + std::string allVariables = exporter.toString(engine.get(), expectedValues, FldExporter::AllVariables); + std::vector<std::string> linesAllVariables = Op::split(allVariables, "\n"); + // FL_LOG("allVariables:\n" << allVariables); + CHECK(int(linesAllVariables.size()) == expectedValues); + } + +} diff --git a/fuzzylite/test/imex/FllImporterTest.cpp b/fuzzylite/test/imex/FllImporterTest.cpp new file mode 100644 index 0000000..d31701a --- /dev/null +++ b/fuzzylite/test/imex/FllImporterTest.cpp @@ -0,0 +1,56 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "test/catch.hpp" +#include "fl/Headers.h" + +namespace fl { + + TEST_CASE("Template of FuzzyLite Language works", "[imex]") { + std::string fllTemplate; +#ifdef FL_CPP98 + //ignore +#else + fllTemplate = R""( +#Template: FuzzyLite Language (FLL) +#Engine: string +#InputVariable: identifier +# enabled: boolean +# range: scalar scalar +# term: identifier Term [parameters] +#OutputVariable: identifier +# enabled: boolean +# range: scalar scalar +# aggregation: SNorm|none +# defuzzifier: [Defuzzifier [parameter]]|none +# default: scalar +# lock-previous: boolean +# lock-range: boolean +# term: identifier Term [parameters] +#RuleBlock: string +# enabled: boolean +# conjunction: TNorm|none +# disjunction: SNorm|none +# implication: TNorm|none +# rule: if antecedent then consequent with weight +)""; +#endif + FL_unique_ptr<Engine> engine(FllImporter().fromString(fllTemplate)); + Engine empty; + CHECK(FllExporter().toString(engine.get()) == FllExporter().toString(&empty)); + } + +} diff --git a/fuzzylite/test/imex/RScriptExporterTest.cpp b/fuzzylite/test/imex/RScriptExporterTest.cpp new file mode 100644 index 0000000..b6d87d8 --- /dev/null +++ b/fuzzylite/test/imex/RScriptExporterTest.cpp @@ -0,0 +1,103 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "test/catch.hpp" +#include "fl/Headers.h" +#include <fstream> + +namespace fl { + + TEST_CASE("RExporter creates plots with a single variable", "[imex]") { + FL_unique_ptr<Engine> engine(Console::mamdani()); + engine->addInputVariable(new InputVariable("Dummy", 0, 1)); + + std::string filename("/tmp/simple-dimmer.fld"); + { + std::ofstream os(filename.c_str()); + FldExporter().write(engine.get(), os, 1024); + os.close(); + } + + std::ifstream is(filename.c_str()); + std::string dummy; + std::getline(is, dummy); //remove header + + std::ostringstream os; + + RScriptExporter().writeScriptImportingDataFrame(engine.get(), os, + engine->getInputVariable(0), engine->getInputVariable(1), + "/dummy/path/to/x.fld", + engine->outputVariables()); + FL_LOG(os.str()); + } + + TEST_CASE("RExporter ") { + std::string fll = + "Engine: mam21\n" + "InputVariable: angle\n" + " enabled: true\n" + " range: -5.000 5.000\n" + " lock-range: false\n" + " term: small Bell -5.000 5.000 8.000\n" + " term: big Bell 5.000 5.000 8.000\n" + "InputVariable: velocity\n" + " enabled: true\n" + " range: -5.000 5.000\n" + " lock-range: false\n" + " term: small Bell -5.000 5.000 2.000\n" + " term: big Bell 5.000 5.000 2.000\n" + "OutputVariable: force\n" + " enabled: true\n" + " range: -5.000 5.000\n" + " lock-range: false\n" + " aggregation: Maximum\n" + " defuzzifier: Centroid 200\n" + " default: nan\n" + " lock-previous: false\n" + " term: negBig Bell -5.000 1.670 8.000\n" + " term: negSmall Bell -1.670 1.670 8.000\n" + " term: posSmall Bell 1.670 1.670 8.000\n" + " term: posBig Bell 5.000 1.670 8.000\n" + "OutputVariable: force2\n" + " enabled: true\n" + " range: -5.000 5.000\n" + " lock-range: false\n" + " aggregation: Maximum\n" + " defuzzifier: Centroid 200\n" + " default: nan\n" + " lock-previous: false\n" + " term: negBig2 Bell -3.000 1.670 8.000\n" + " term: negSmall2 Bell -1.000 1.670 8.000\n" + " term: posSmall2 Bell 1.000 1.670 8.000\n" + " term: posBig2 Bell 3.000 1.670 8.000\n" + "RuleBlock: \n" + " enabled: true\n" + " conjunction: Minimum\n" + " disjunction: Maximum\n" + " implication: Minimum\n" + " activation: General\n" + " rule: if angle is small and velocity is small then force is negBig and force2 is posBig2\n" + " rule: if angle is small and velocity is big then force is negSmall and force2 is posSmall2\n" + " rule: if angle is big and velocity is small then force is posSmall and force2 is negSmall2\n" + " rule: if angle is big and velocity is big then force is posBig and force2 is negBig2\n"; + + Engine* engine = FllImporter().fromString(fll); + RScriptExporter().toFile("/tmp/mam22.R", engine, + engine->getInputVariable(0), engine->getInputVariable(1), + 1024, FldExporter::AllVariables, engine->outputVariables()); + } + +} diff --git a/fuzzylite/test/norm/NormFunctionTest.cpp b/fuzzylite/test/norm/NormFunctionTest.cpp new file mode 100644 index 0000000..afce4c8 --- /dev/null +++ b/fuzzylite/test/norm/NormFunctionTest.cpp @@ -0,0 +1,202 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "test/catch.hpp" +#include "fl/Headers.h" + +namespace fl { + + /** + * Tests: norm/NormFunctions + * + * @author Juan Rada-Vilela, Ph.D. + * + */ + + static std::string snormEngine() { +#ifdef FL_CPP98 + return ""; +#else + return R""( +Engine: tipper +InputVariable: service + enabled: true + range: 0.000 10.000 + lock-range: false + term: poor Gaussian 0.000 1.500 + term: good Gaussian 5.000 1.500 + term: excellent Gaussian 10.000 1.500 +InputVariable: food + enabled: true + range: 0.000 10.000 + lock-range: false + term: rancid Trapezoid 0.000 0.000 1.000 3.000 + term: delicious Trapezoid 7.000 9.000 10.000 10.000 +OutputVariable: tip + enabled: true + range: 0.000 30.000 + lock-range: false + aggregation: Maximum + defuzzifier: Centroid 200 + default: nan + lock-previous: false + term: cheap Triangle 0.000 5.000 10.000 + term: average Triangle 10.000 15.000 20.000 + term: generous Triangle 20.000 25.000 30.000 +RuleBlock: + enabled: true + conjunction: Minimum + disjunction: Maximum + implication: Minimum + activation: General + rule: if service is poor or food is rancid then tip is cheap + rule: if service is good then tip is average + rule: if service is excellent or food is delicious then tip is generous +)""; +#endif + } + + static SNorm* myMaximumNorm() { + return new SNormFunction("max(a,b)"); + } + + static SNorm* myNotSoMaximumNorm() { + return new SNormFunction("max(a,b) * 0.5"); + } + + TEST_CASE("SNormFunction (max(a,b)) is equivalent to Maximum", "[snorm][maximum]") { +#ifdef FL_CPP98 + FL_IUNUSED(&(myMaximumNorm)); + FL_IUNUSED(&(myNotSoMaximumNorm)); + FL_IUNUSED(&(snormEngine)); + WARN("Test only runs with -DFL_CPP98=OFF"); + return; +#else + std::string fllEngine = snormEngine(); + FL_unique_ptr<Engine> engine(FllImporter().fromString(fllEngine)); + std::string fld = FldExporter().toString(engine.get(), 1024); + + SNormFactory* factory = FactoryManager::instance()->snorm(); + factory->registerConstructor("Maximum", &(myMaximumNorm)); + + //Check our custom SNorm is registered + FL_unique_ptr<SNorm> x(factory->constructObject("Maximum")); + CHECK(Op::isEq(x->compute(0, 0.5), 0.5)); + + //Test creating an engine with the new SNorm + engine.reset(FllImporter().fromString(fllEngine)); + std::string anotherFld = FldExporter().toString(engine.get(), 1024); + + CHECK(fld == anotherFld); + + //Make sure a different SNorm fails in results + + factory->registerConstructor("Maximum", &(myNotSoMaximumNorm)); + engine.reset(FllImporter().fromString(fllEngine)); + anotherFld = FldExporter().toString(engine.get(), 1024); + + CHECK(fld != anotherFld); +#endif + } + + static std::string tnormEngine() { +#ifdef FL_CPP98 + return ""; +#else + return R""( +Engine: mam21 +InputVariable: angle + enabled: true + range: -5.000 5.000 + lock-range: false + term: small Bell -5.000 5.000 8.000 + term: big Bell 5.000 5.000 8.000 +InputVariable: velocity + enabled: true + range: -5.000 5.000 + lock-range: false + term: small Bell -5.000 5.000 2.000 + term: big Bell 5.000 5.000 2.000 +OutputVariable: force + enabled: true + range: -5.000 5.000 + lock-range: false + aggregation: Maximum + defuzzifier: Centroid 200 + default: nan + lock-previous: false + term: negBig Bell -5.000 1.670 8.000 + term: negSmall Bell -1.670 1.670 8.000 + term: posSmall Bell 1.670 1.670 8.000 + term: posBig Bell 5.000 1.670 8.000 +RuleBlock: + enabled: true + conjunction: Minimum + disjunction: Maximum + implication: Minimum + activation: General + rule: if angle is small and velocity is small then force is negBig + rule: if angle is small and velocity is big then force is negSmall + rule: if angle is big and velocity is small then force is posSmall + rule: if angle is big and velocity is big then force is posBig +)""; +#endif + } + + static TNorm* myMinimumNorm() { + return new TNormFunction("min(a,b)"); + } + + static TNorm* myNotSoMinimumNorm() { + return new TNormFunction("min(a,b) * 0.5"); + } + + TEST_CASE("TNormFunction (min(a,b)) is equivalent to Minimum", "[tnorm][minimum]") { +#ifdef FL_CPP98 + FL_IUNUSED(&(myMinimumNorm)); + FL_IUNUSED(&(myNotSoMinimumNorm)); + FL_IUNUSED(&(tnormEngine)); + WARN("Test only runs with -DFL_CPP98=OFF"); + return; +#else + std::string fllEngine = tnormEngine(); + FL_unique_ptr<Engine> engine(FllImporter().fromString(fllEngine)); + std::string fld = FldExporter().toString(engine.get(), 1024); + + TNormFactory* factory = FactoryManager::instance()->tnorm(); + factory->registerConstructor("Minimum", &(myMinimumNorm)); + + //Check our custom SNorm is registered + FL_unique_ptr<TNorm> x(factory->constructObject("Minimum")); + CHECK(Op::isEq(x->compute(0.5, 1), 0.5)); + + //Test creating an engine with the new SNorm + engine.reset(FllImporter().fromString(fllEngine)); + std::string anotherFld = FldExporter().toString(engine.get(), 1024); + + CHECK(fld == anotherFld); + + //Make sure a different SNorm fails in results + + factory->registerConstructor("Minimum", &(myNotSoMinimumNorm)); + engine.reset(FllImporter().fromString(fllEngine)); + anotherFld = FldExporter().toString(engine.get(), 1024); + + CHECK(fld != anotherFld); +#endif + } + +} diff --git a/fuzzylite/test/term/AggregatedTest.cpp b/fuzzylite/test/term/AggregatedTest.cpp new file mode 100644 index 0000000..bb3f0e4 --- /dev/null +++ b/fuzzylite/test/term/AggregatedTest.cpp @@ -0,0 +1,52 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "test/catch.hpp" +#include "fl/Headers.h" + +namespace fl { + + /** + * Tests: term/Aggregated + * + * @author Juan Rada-Vilela, Ph.D. + * + */ + + TEST_CASE("highest term in aggregated", "[term][aggregated]") { + + FL_unique_ptr<Term> dark(new Triangle("DARK", 0.000, 0.250, 0.500)); + FL_unique_ptr<Term> medium(new Triangle("MEDIUM", 0.250, 0.500, 0.750)); + FL_unique_ptr<Term> bright(new Triangle("BRIGHT", 0.500, 0.750, 1.000)); + + Aggregated aggregated; + aggregated.addTerm(dark.get(), 0.5, fl::null); + aggregated.addTerm(medium.get(), 0.1, fl::null); + aggregated.addTerm(bright.get(), 0.6, fl::null); + + REQUIRE(aggregated.highestActivatedTerm()->getTerm() == bright.get()); + + aggregated.terms().at(1).setDegree(0.7); + REQUIRE(aggregated.highestActivatedTerm()->getTerm() == medium.get()); + + aggregated.terms().front().setDegree(0.9); + REQUIRE(aggregated.highestActivatedTerm()->getTerm() == dark.get()); + + aggregated.clear(); + REQUIRE(aggregated.highestActivatedTerm() == fl::null); + } + +} diff --git a/fuzzylite/test/term/DiscreteTest.cpp b/fuzzylite/test/term/DiscreteTest.cpp new file mode 100644 index 0000000..2fb32aa --- /dev/null +++ b/fuzzylite/test/term/DiscreteTest.cpp @@ -0,0 +1,125 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "test/catch.hpp" +#include "fl/Headers.h" + +namespace fl { + + /** + * Tests: term/Discrete + * + * @author Juan Rada-Vilela, Ph.D. + * + */ + + TEST_CASE("discrete finds elements using binary search", "[term][discrete]") { + fuzzylite::setLogging(true); + fuzzylite::setDebugging(false); + Rectangle rectangle("rectangle", 0, 1); + FL_unique_ptr<Discrete> discrete(Discrete::discretize(&rectangle, rectangle.getStart(), rectangle.getEnd(), 10)); + FL_LOG(discrete->toString()); + + CHECK(discrete->membership(.25) == 1.0); + CHECK(discrete->membership(0.0) == 1.0); + CHECK(discrete->membership(-1.0) == 1.0); + CHECK(discrete->membership(1.0) == 1.0); + CHECK(discrete->membership(2.0) == 1.0); + CHECK(Op::isNaN(discrete->membership(fl::nan))); + CHECK(discrete->membership(fl::inf) == 1.0); + CHECK(discrete->membership(-fl::inf) == 1.0); + } + + TEST_CASE("discrete still finds elements using binary search", "[term][discrete]") { + fuzzylite::setLogging(true); + fuzzylite::setDebugging(false); + Triangle triangle("triangle", 0, 1); + FL_unique_ptr<Discrete> discrete(Discrete::discretize(&triangle, triangle.getVertexA(), triangle.getVertexC(), 100)); + FL_LOG(discrete->toString()); + for (int i = 0; i < 200; ++i) { + scalar x = Op::scale(i, 0, 200, -1, 1); + if (not Op::isEq(triangle.membership(x), discrete->membership(x))) { + fuzzylite::setDebugging(true); + CHECK(Op::isEq(triangle.membership(x), discrete->membership(x))); + fuzzylite::setDebugging(false); + } + } + } + + TEST_CASE("discrete finds all elements using binary search", "[term][discrete]") { + fuzzylite::setLogging(true); + fuzzylite::setDebugging(false); + scalar min = -1.0; + scalar max = 1.0; + scalar range = max - min; + scalar mean = 0.5 * (max + min); + + + std::vector<Term*> terms; + terms.push_back(new Triangle("triangle", min, mean, max)); + terms.push_back(new Trapezoid("trapezoid", min, min + .25 * range, min + .75 * range, max)); + terms.push_back(new Rectangle("rectangle", min, max)); + terms.push_back(new Bell("bell", mean, range / 4, 3.0)); + terms.push_back(new Cosine("cosine", mean, range)); + terms.push_back(new Gaussian("gaussian", mean, range / 4)); + terms.push_back(new GaussianProduct("gaussianProduct", mean, range / 4, mean, range / 4)); + terms.push_back(new PiShape("piShape", min, mean, mean, max)); + terms.push_back(new SigmoidDifference("sigmoidDifference", min + .25 * range, 20 / range, 20 / range, max - .25 * range)); + terms.push_back(new SigmoidProduct("sigmoidProduct", min + .25 * range, 20 / range, 20 / range, max - .25 * range)); + terms.push_back(new Spike("spike", mean, range)); + + terms.push_back(new Binary("binary", min, max)); + terms.push_back(new Concave("concave", mean, max)); + terms.push_back(new Ramp("ramp", min, max)); + terms.push_back(new Sigmoid("sigmoid", mean, 20 / range)); + terms.push_back(new SShape("sshape", min, max)); + terms.push_back(new ZShape("zshape", min, max)); + + for (std::size_t t = 0; t < terms.size(); ++t) { + Term* term = terms.at(t); + std::vector<Discrete::Pair> pairs; + srand(0); + for (int i = 0; i < 1000; ++i) { + int randomX = std::rand(); + scalar x = Op::scale(randomX % 100, 0, 100, -1, 1); + pairs.push_back(Discrete::Pair(x, term->membership(x))); + } + Discrete::sort(pairs); + + Discrete discrete("discrete", pairs); + for (std::size_t i = 0; i < pairs.size(); ++i) { + Discrete::Pair pair = pairs.at(i); + scalar x = pair.first; + if (not Op::isEq(discrete.membership(x), term->membership(x))) { + fuzzylite::setDebugging(true); + CHECK(discrete.membership(x) == term->membership(x)); + fuzzylite::setDebugging(false); + } + } + for (int i = 0 ; i < 100 ; i++) { + scalar x = Op::scale(i, 0, 100, -1, 1); + if (not Op::isEq(discrete.membership(x), term->membership(x))) { + fuzzylite::setDebugging(true); + CHECK(discrete.membership(x) == term->membership(x)); + fuzzylite::setDebugging(false); + } + } + } + for (std::size_t i = 0 ; i < terms.size(); ++i){ + delete terms.at(i); + } + } +} diff --git a/fuzzylite/test/term/FunctionTest.cpp b/fuzzylite/test/term/FunctionTest.cpp new file mode 100644 index 0000000..a42e37b --- /dev/null +++ b/fuzzylite/test/term/FunctionTest.cpp @@ -0,0 +1,115 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "test/catch.hpp" +#include "fl/Headers.h" + +namespace fl { + + /** + * Tests: term/Function + * + * @author Juan Rada-Vilela, Ph.D. + * + */ + + TEST_CASE("function parses basic arithmetic", "[term][function]") { + Function f; + std::string text = "3+4*2/(1-5)^2^3"; + CHECK(f.toPostfix(text) == "3 4 2 * 1 5 - 2 3 ^ ^ / +"); + CHECK(f.parse(text)->toInfix() == "3.000 ^ 2.000 ^ 5.000 - 1.000 / 2.000 * 4.000 + 3.000"); + CHECK(f.parse(text)->toPrefix() == "+ / ^ ^ 3.000 2.000 - 5.000 1.000 * 2.000 4.000 3.000"); + } + + TEST_CASE("function parses basic trigonometry", "[term][function]") { + Function f; + std::string text = "sin(y*x)^2/x"; + + CHECK_THROWS(f.load(text)); + + f.variables["y"] = 1.0; + f.load(text); + + CHECK(f.toPostfix(text) == "y x * sin 2 ^ x /"); + CHECK(f.parse(text)->toInfix() == "x / 2.000 ^ x * y sin"); + CHECK(f.parse(text)->toPrefix() == "/ x ^ 2.000 sin * x y"); + } + + TEST_CASE("function parses propositions", "[term][function]") { + Function f; + + std::string text = "(Temperature is High and Oxygen is Low) or " + "(Temperature is Low and (Oxygen is Low or Oxygen is High))"; + + CHECK(f.toPostfix(text) == "Temperature is High Oxygen is Low " + "and Temperature is Low Oxygen is Low Oxygen is High or and or"); + } + + TEST_CASE("function cannot deal with negative numbers", "[term][function]") { + Function f; + std::string text = "-5 *4/sin(-pi/2)"; + + SECTION("function throws exception") { + CHECK_THROWS(f.parse(text)->evaluate()); + } + + f.variables["pi"] = 3.14; + CHECK_THROWS(f.parse(text)->evaluate(&f.variables)); + + text = "~5 *4/sin(~pi/2)"; + CHECK(f.parse(text)->evaluate(&f.variables) == Approx(20)); + + f.load(text); + + f.variables["pi"] = 3.14; + + CHECK(f.toPostfix(text) == "5 ~ 4 * pi ~ 2 / sin /"); + CHECK(f.parse(text)->toInfix() == "2.000 / pi ~ sin / 4.000 * 5.000 ~"); + CHECK(f.parse(text)->toPrefix() == "/ sin / 2.000 ~ pi * 4.000 ~ 5.000"); + } + + TEST_CASE("Function is clonable", "[term][function]") { + Function* f = new Function; + std::string text = "2+2"; + f->load(text); + CHECK(Op::isEq(f->membership(fl::nan), 4)); + Function* clone = f->clone(); + delete f; + CHECK(Op::isEq(clone->membership(fl::nan), 4)); + delete clone; + } + + TEST_CASE("Function is constructor copyable", "[term][function]") { + Function* f = new Function; + std::string text = "2+2"; + f->load(text); + CHECK(Op::isEq(f->membership(fl::nan), 4)); + Function* clone = new Function(*f); + delete f; + CHECK(Op::isEq(clone->membership(fl::nan), 4)); + delete clone; + } + + TEST_CASE("Function computes tree size correctly", "[term][function]"){ + Function f("f", "x*x+(x-x)/x+log(x)"); + f.load(); + CHECK(f.root()->treeSize() == 6); + CHECK(f.root()->treeSize(Function::Element::Function) == 1); + CHECK(f.root()->treeSize(Function::Element::Operator) == 5); + FL_LOG(f.complexity().toString()); + } + +} diff --git a/fuzzylite/test/term/TrapezoidTest.cpp b/fuzzylite/test/term/TrapezoidTest.cpp new file mode 100644 index 0000000..8581ac1 --- /dev/null +++ b/fuzzylite/test/term/TrapezoidTest.cpp @@ -0,0 +1,53 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "test/catch.hpp" +#include "fl/Headers.h" + +namespace fl { + + /** + * Tests: term/Trapezoid + * + * @author Juan Rada-Vilela, Ph.D. + * + */ + + TEST_CASE("trapezoid can be open ended with -infinity", "[term][trapezoid]") { + Trapezoid trapezoid("A", -fl::inf, 0, 1, 2); + Ramp ramp("a", 2, 1); + //(-inf, inf) + for (scalar i = -10.0; Op::isLE(i, 10.0); i += .2) { + FL_DBG("A(" << i << ")=" << trapezoid.membership(i)); + FL_DBG("a(" << i << ")=" << ramp.membership(i)); + REQUIRE(Op::isEq(trapezoid.membership(i), ramp.membership(i))); + } + REQUIRE(Op::isEq(trapezoid.membership(-fl::inf), 1.0)); + REQUIRE(Op::isEq(trapezoid.membership(fl::inf), 0.0)); + } + + TEST_CASE("trapezoid can be open ended with +infinity", "[term][trapezoid]") { + Trapezoid trapezoid("A", 0, 1, 2, fl::inf); + Ramp ramp("a", 0, 1); + //(-inf, inf) + for (scalar i = -10.0; Op::isLE(i, 10.0); i += .2) { + REQUIRE(Op::isEq(trapezoid.membership(i), ramp.membership(i))); + } + REQUIRE(Op::isEq(trapezoid.membership(fl::inf), 1.0)); + REQUIRE(Op::isEq(trapezoid.membership(-fl::inf), 0.0)); + } + +} diff --git a/fuzzylite/test/term/TriangleTest.cpp b/fuzzylite/test/term/TriangleTest.cpp new file mode 100644 index 0000000..bd1459d --- /dev/null +++ b/fuzzylite/test/term/TriangleTest.cpp @@ -0,0 +1,66 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "test/catch.hpp" +#include "fl/Headers.h" + +namespace fl { + + /** + * Tests: term/Triangle + * + * @author Juan Rada-Vilela, Ph.D. + * + */ + + TEST_CASE("triangle can be open ended with -infinity", "[term][triangle]") { + Triangle triangle("A", -fl::inf, 0, 1); + Ramp ramp("a", 1, 0); + for (int i = -10; i < 2; ++i) { + FL_DBG("A(" << i << ")=" << triangle.membership(i)); + FL_DBG("a(" << i << ")=" << ramp.membership(i)); + REQUIRE(Op::isEq(triangle.membership(i), ramp.membership(i))); + } + REQUIRE(Op::isEq(triangle.membership(-fl::inf), 1.0)); + REQUIRE(Op::isEq(triangle.membership(fl::inf), 0.0)); + } + + TEST_CASE("triangle can be open ended with +infinity", "[term][triangle]") { + Triangle triangle("A", 0, 1, fl::inf); + Ramp ramp("a", 0, 1); + for (int i = 10; i >= -2; --i) { + FL_DBG("A(" << i << ")=" << triangle.membership(i)); + FL_DBG("a(" << i << ")=" << ramp.membership(i)); + REQUIRE(Op::isEq(triangle.membership(i), ramp.membership(i))); + } + REQUIRE(Op::isEq(triangle.membership(fl::inf), 1.0)); + REQUIRE(Op::isEq(triangle.membership(-fl::inf), 0.0)); + } + + TEST_CASE("triangle defuzzification is correct", "[term][triangle]") { + Triangle a("A", 0, 1, 2); + Triangle b("B", 1, 2, 3); + AlgebraicProduct times; + Aggregated x("X", 0, 3, new AlgebraicSum); + x.addTerm(&a, 1, ×); + x.addTerm(&b, 1, ×); + Centroid c(101); + FL_LOG(c.defuzzify(&x, 0, 3)); + + Triangle t("T", 0, 0, 1); + FL_LOG(c.defuzzify(&t, 0, 1)); + } +} diff --git a/fuzzylite/test/variable/VariableTest.cpp b/fuzzylite/test/variable/VariableTest.cpp new file mode 100644 index 0000000..6f77e94 --- /dev/null +++ b/fuzzylite/test/variable/VariableTest.cpp @@ -0,0 +1,74 @@ +/* + fuzzylite (R), a fuzzy logic control library in C++. + Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. + Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com> + + This file is part of fuzzylite. + + fuzzylite is free software: you can redistribute it and/or modify it under + the terms of the FuzzyLite License included with the software. + + You should have received a copy of the FuzzyLite License along with + fuzzylite. If not, see <http://www.fuzzylite.com/license/>. + + fuzzylite is a registered trademark of FuzzyLite Limited. + */ + +#include "test/catch.hpp" +#include "fl/Headers.h" + +#include <algorithm> // std::random_shuffle + +namespace fl { + + /** + * Tests: variable/Variable + * + * @author Juan Rada-Vilela, Ph.D. + * + */ + + TEST_CASE("variable of Constant terms is sorted", "[variable][variable]") { + Variable variable("Variable", -10, 10); + for (int i = 0; i <= 20; ++i) { + variable.addTerm(new Constant(Op::str(i), i - 10)); + } + std::random_shuffle(variable.terms().begin(), variable.terms().end()); + FL_DBG(variable.toString()); + REQUIRE(variable.numberOfTerms() == 21); + variable.sort(); + REQUIRE(variable.numberOfTerms() == 21); + int value = -10; + for (std::size_t i = 0; i < variable.terms().size(); ++i) { + Constant* term = dynamic_cast<Constant*> (variable.terms().at(i)); + REQUIRE(term); + REQUIRE(term->getValue() == float(value)); + ++value; + } + FL_DBG(variable.toString()); + } + + TEST_CASE("variable of Triangle terms is sorted", "[variable][variable]") { + Variable variable("Variable", -30, 30); + for (int i = 0; i <= 20; ++i) { + variable.addTerm(new Triangle(Op::str(i), i - 1, i, i + 1)); + } + std::random_shuffle(variable.terms().begin(), variable.terms().end()); + FL_DBG(variable.toString()); + REQUIRE(variable.numberOfTerms() == 21); + variable.sort(); + FL_DBG("Shuffled:"); + FL_DBG(variable.toString()); + REQUIRE(variable.numberOfTerms() == 21); + int value = 0; + for (std::size_t i = 0; i < variable.terms().size(); ++i) { + Triangle* term = dynamic_cast<Triangle*> (variable.terms().at(i)); + REQUIRE(term); + REQUIRE(term->getName() == Op::str(value < 0 ? -1 * value : value)); + ++value; + } + FL_DBG(variable.toString()); + } + +} + |