diff options
Diffstat (limited to 'fuzzylite')
245 files changed, 29239 insertions, 8674 deletions
diff --git a/fuzzylite/CMakeLists.txt b/fuzzylite/CMakeLists.txt index f00a71a..28435e2 100644 --- a/fuzzylite/CMakeLists.txt +++ b/fuzzylite/CMakeLists.txt @@ -1,17 +1,28 @@ -cmake_minimum_required(VERSION 2.8) +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() -set(FL_VERSION 5.1) -add_definitions(-DFL_VERSION="${FL_VERSION}") - -set(FL_DATE "1507") -add_definitions(-DFL_DATE="${FL_DATE}") +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__ @@ -21,35 +32,50 @@ 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" OFF) -option(FL_CPP11 "Builds utilizing C++11, i.e., passing -std=c++11" OFF) +option(FL_BACKTRACE "Provide backtrace information in case of errors" ON) -if(FL_CPP11 AND MSVC) - message("Warning: MSVC not compatible with FL_CPP11 flag") -endif() +option(FL_BUILD_TESTS "Builds the unit tests" ON) if(FL_USE_FLOAT) add_definitions(-DFL_USE_FLOAT) endif(FL_USE_FLOAT) -if(NOT FL_BACKTRACE) - add_definitions(-DFL_BACKTRACE_OFF) +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_CPP11) - add_definitions(-DFL_CPP11) +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(NOT MSVC) -endif(FL_CPP11) + 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) - set(CMAKE_CXX_FLAGS "-pedantic -Wall -Wextra ${CMAKE_CXX_FLAGS}") +#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}") @@ -59,38 +85,64 @@ if(NOT MSVC) 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(.) -if (APPLE) - cmake_policy(SET CMP0042 NEW) -endif() 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 d) +set(CMAKE_DEBUG_POSTFIX debug) if (MSVC OR CMAKE_GENERATOR STREQUAL Xcode) if(FL_BUILD_SHARED) @@ -119,16 +171,16 @@ endif() if(FL_BUILD_SHARED) set_target_properties(fl-shared PROPERTIES OUTPUT_NAME fuzzylite) - set_target_properties(fl-shared PROPERTIES DEBUG_POSTFIX d) - set_target_properties(fl-shared PROPERTIES COMPILE_DEFINITIONS "FL_EXPORT_LIBRARY") - set_target_properties(fl-shared PROPERTIES VERSION 5.1) + 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 d) - set_target_properties(fl-static PROPERTIES VERSION 5.1) + 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() @@ -136,12 +188,40 @@ 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 d) - - set_target_properties(fl-bin PROPERTIES COMPILE_DEFINITIONS "FL_IMPORT_LIBRARY") + 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) @@ -156,7 +236,7 @@ if(NOT FL_INSTALL_LIBDIR) endif() if(FL_BUILD_BINARY) - install(TARGETS fl-bin + install(TARGETS fl-bin RUNTIME DESTINATION ${FL_INSTALL_BINDIR} LIBRARY DESTINATION ${FL_INSTALL_LIBDIR} ARCHIVE DESTINATION ${FL_INSTALL_LIBDIR} @@ -164,7 +244,7 @@ if(FL_BUILD_BINARY) endif() if(FL_BUILD_SHARED) - install(TARGETS fl-shared + install(TARGETS fl-shared RUNTIME DESTINATION ${FL_INSTALL_BINDIR} LIBRARY DESTINATION ${FL_INSTALL_LIBDIR} ARCHIVE DESTINATION ${FL_INSTALL_LIBDIR} @@ -186,13 +266,26 @@ configure_file(${CMAKE_SOURCE_DIR}/fuzzylite.pc.in ${CMAKE_BINARY_DIR}/fuzzylite install(FILES ${CMAKE_BINARY_DIR}/fuzzylite.pc DESTINATION ${FL_INSTALL_LIBDIR}/pkgconfig) message("=====================================") -message("fuzzylite v.${FL_VERSION}b${FL_DATE}\n") +message("fuzzylite v6.0\n") +message("FL_CPP98=${FL_CPP98}") message("FL_USE_FLOAT=${FL_USE_FLOAT}") -message("FL_CPP11=${FL_CPP11}") 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("Build=${CMAKE_BUILD_TYPE}") +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 diff --git a/fuzzylite/FL_HEADERS b/fuzzylite/FL_HEADERS index f8b578c..b852ad8 100644 --- a/fuzzylite/FL_HEADERS +++ b/fuzzylite/FL_HEADERS @@ -1,4 +1,14 @@ +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 @@ -7,10 +17,13 @@ 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 @@ -25,6 +38,7 @@ 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 @@ -40,6 +54,7 @@ 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 @@ -49,6 +64,8 @@ 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 @@ -57,6 +74,7 @@ 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 @@ -64,9 +82,10 @@ fl/rule/Consequent.h fl/rule/Expression.h fl/rule/RuleBlock.h fl/rule/Rule.h -fl/term/Accumulated.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 diff --git a/fuzzylite/FL_SOURCES b/fuzzylite/FL_SOURCES index 64d07f9..16d8ab4 100644 --- a/fuzzylite/FL_SOURCES +++ b/fuzzylite/FL_SOURCES @@ -1,4 +1,13 @@ +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 @@ -6,12 +15,13 @@ 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/CloningFactory.cpp -src/factory/ConstructionFactory.cpp +src/factory/ActivationFactory.cpp src/factory/DefuzzifierFactory.cpp src/factory/FactoryManager.cpp src/factory/FunctionFactory.cpp @@ -22,6 +32,7 @@ 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 @@ -37,6 +48,7 @@ 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 @@ -46,6 +58,8 @@ 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 @@ -53,15 +67,16 @@ src/norm/t/EinsteinProduct.cpp src/norm/t/HamacherProduct.cpp src/norm/t/Minimum.cpp src/norm/t/NilpotentMinimum.cpp -src/Operation.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/Accumulated.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 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 index 17f5b6d..11f62f8 100644 --- a/fuzzylite/build.bat +++ b/fuzzylite/build.bat @@ -13,7 +13,8 @@ for %%a in (%*) do ( 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 @@ -38,13 +39,13 @@ goto:eof 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_CPP11=OFF + 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 **************************************** @@ -55,13 +56,13 @@ goto:eof 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_CPP11=OFF + 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 **************************************** @@ -72,21 +73,37 @@ goto:eof echo. echo **************************************** echo STARTING: all - call:debug 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 **************************************** @@ -102,4 +119,4 @@ goto:eof echo ^ help shows this information echo. -ENDLOCAL
\ No newline at end of file +ENDLOCAL diff --git a/fuzzylite/build.sh b/fuzzylite/build.sh index 78343c4..da39905 100755 --- a/fuzzylite/build.sh +++ b/fuzzylite/build.sh @@ -1,19 +1,46 @@ #!/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" -DCMAKE_BUILD_TYPE=Debug -DFL_BACKTRACE=ON -DFL_USE_FLOAT=OFF -DFL_CPP11=OFF - make + 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" -DCMAKE_BUILD_TYPE=Release -DFL_BACKTRACE=ON -DFL_USE_FLOAT=OFF -DFL_CPP11=OFF - make + 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(){ @@ -37,20 +64,21 @@ usage(){ } ############################# +echo "Parameters: $@" -OPTIONS=( "all" "debug" "release" "clean" "help") +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" ]]; + 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+=( "all" ); fi +if [ ${#BUILD[@]} -eq 0 ]; then BUILD+=( "release" "debug" ); fi echo "Building schedule: ${BUILD[@]}" echo "Starting in 3 seconds..." 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 index 59e818d..b96d677 100644 --- a/fuzzylite/fl/Console.h +++ b/fuzzylite/fl/Console.h @@ -1,32 +1,23 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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> @@ -34,57 +25,131 @@ 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 = "") : - key(key), value(value), description(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; - static const std::string KW_DATA_INPUT; - static const std::string KW_DATA_MAXIMUM; + /**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: - static std::map<std::string, std::string> parse(int argc, char** argv); - static void process(const std::map<std::string, std::string>& options); + virtual std::map<std::string, std::string> parse(int argc, const char* argv[]); + virtual void process(const std::map<std::string, std::string>& options); - static void process(const std::string& input, std::ostream& writer, + 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); - static int readCharacter(); - static void interactive(std::ostream& writer, Engine* engine); - static std::string interactiveHelp(); - - static void exportAllExamples(const std::string& from, const std::string& to); - static void exportAllExamples(const std::string& from, const std::string& to, const std::string& path); -#ifdef FL_CPP11 - static void benchmarkExamples(const std::string& path, int runs); -#endif + 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: - static std::string usage(); - static std::vector<Option> availableOptions(); - - static int main(int argc, char** argv); + /** + 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 index 532c5e9..3a88478 100644 --- a/fuzzylite/fl/Engine.h +++ b/fuzzylite/fl/Engine.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -27,7 +19,7 @@ #include "fl/fuzzylite.h" -#include "fl/defuzzifier/IntegralDefuzzifier.h" +#include "fl/Complexity.h" #include <string> #include <vector> @@ -42,16 +34,29 @@ namespace fl { 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: - void copyFrom(const Engine& source); - protected: std::string _name; + std::string _description; std::vector<InputVariable*> _inputVariables; std::vector<OutputVariable*> _outputVariables; - std::vector<RuleBlock*> _ruleblocks; + std::vector<RuleBlock*> _ruleBlocks; + + void copyFrom(const Engine& source); + protected: void updateReferences() const; public: @@ -61,92 +66,413 @@ namespace fl { virtual ~Engine(); FL_DEFAULT_MOVE(Engine) - //TODO: remove int resolution in v6.0 - virtual void configure(const std::string& conjunctionT, - const std::string& disjunctionS, - const std::string& activationT, - const std::string& accumulationS, + /** + 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, - int resolution = IntegralDefuzzifier::defaultResolution()); + 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* activation, SNorm* accumulation, - Defuzzifier* defuzzifier); + 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, Larsen, TakagiSugeno, - Tsukamoto, InverseTsukamoto, Hybrid, Unknown + /**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; /** - * Operations for iterable datatype _inputVariables + Adds the input variable + @param inputVariable is the input variable */ virtual void addInputVariable(InputVariable* inputVariable); - virtual InputVariable* setInputVariable(InputVariable* inputVariable, int index); - virtual void insertInputVariable(InputVariable* inputVariable, int index); - virtual InputVariable* getInputVariable(int index) const; + /** + 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; - virtual InputVariable* removeInputVariable(int index); + /** + 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; - virtual int numberOfInputVariables() 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(); /** - * Operations for iterable datatype _outputVariables + Adds the output variable + @param outputVariable is the output variable */ virtual void addOutputVariable(OutputVariable* outputVariable); - virtual OutputVariable* setOutputVariable(OutputVariable* outputVariable, int index); - virtual void insertOutputVariable(OutputVariable* outputVariable, int index); - virtual OutputVariable* getOutputVariable(int index) const; + /** + 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; - virtual OutputVariable* removeOutputVariable(int index); + /** + 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); - virtual int numberOfOutputVariables() const; + /** + 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(); /** - * Operations for iterable datatype _ruleblocks + 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 void addRuleBlock(RuleBlock* ruleblock); - virtual RuleBlock* setRuleBlock(RuleBlock* ruleBlock, int index); - virtual void insertRuleBlock(RuleBlock* ruleblock, int index); - virtual RuleBlock* getRuleBlock(int index) const; 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; - virtual RuleBlock* removeRuleBlock(int index); + /** + 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); - virtual int numberOfRuleBlocks() const; + /** + 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 index f33fb13..60996f1 100644 --- a/fuzzylite/fl/Exception.h +++ b/fuzzylite/fl/Exception.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -33,34 +25,116 @@ 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 { - protected: +#ifdef FL_WINDOWS +#pragma warning (pop) +#endif + private: std::string _what; public: explicit Exception(const std::string& what); - Exception(const std::string& what, const std::string& file, int line, + /** + 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 index 9c2299a..1c41002 100644 --- a/fuzzylite/fl/Headers.h +++ b/fuzzylite/fl/Headers.h @@ -1,36 +1,46 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -42,6 +52,7 @@ #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" @@ -61,10 +72,12 @@ #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" @@ -84,6 +97,8 @@ #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" @@ -92,6 +107,7 @@ #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" @@ -99,8 +115,9 @@ #include "fl/rule/RuleBlock.h" #include "fl/rule/Expression.h" -#include "fl/term/Accumulated.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" diff --git a/fuzzylite/fl/Operation.h b/fuzzylite/fl/Operation.h index d92e66c..8070116 100644 --- a/fuzzylite/fl/Operation.h +++ b/fuzzylite/fl/Operation.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -34,111 +26,1044 @@ namespace fl { - class FL_API Operation { + /** + 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); - //Is less than + /** + 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); - static bool isLt(scalar a, scalar b, scalar macheps = fl::fuzzylite::macheps()); - static bool isLE(scalar a, scalar b, scalar macheps = fl::fuzzylite::macheps()); - static bool isEq(scalar a, scalar b, scalar macheps = fl::fuzzylite::macheps()); - static bool isGt(scalar a, scalar b, scalar macheps = fl::fuzzylite::macheps()); - static bool isGE(scalar a, scalar b, scalar macheps = fl::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 = false); + 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); - //greater than + /** + 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); - //greater than or equal to + /** + 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); - //equal to + /** + 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); - //not equal to + /** + 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); - //less than or equal to + /** + 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); - //less than + /** + 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); - static double mean(const std::vector<scalar>& x); - static double variance(const std::vector<scalar>& x); - static double variance(const std::vector<scalar>& x, scalar mean); - static double standardDeviation(const std::vector<scalar>& x); - static double standardDeviation(const std::vector<scalar>& x, scalar mean); + /** + 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); - static int isValidForName(int character); - + /** + 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 = ""); - //Intentionally results in a compiler error in C++11, or linker error in C++98 - //in order to avoid the deprecated usage of this method from version 4.0 - static scalar toScalar(const std::string& x, bool quiet, - scalar alternative = fl::nan) FL_IDELETE; + /** + 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) - 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; - static scalar toScalar(const std::string& x, scalar alternative) 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()); + 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 index 7ba8db2..046b185 100644 --- a/fuzzylite/fl/defuzzifier/Bisector.h +++ b/fuzzylite/fl/defuzzifier/Bisector.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,6 +21,16 @@ 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()); @@ -36,13 +38,26 @@ namespace fl { 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 index 7510673..b056eac 100644 --- a/fuzzylite/fl/defuzzifier/Centroid.h +++ b/fuzzylite/fl/defuzzifier/Centroid.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,6 +21,16 @@ 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()); @@ -36,12 +38,26 @@ namespace fl { 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 index 3598113..25749eb 100644 --- a/fuzzylite/fl/defuzzifier/Defuzzifier.h +++ b/fuzzylite/fl/defuzzifier/Defuzzifier.h @@ -1,38 +1,39 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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. */ -//TODO Check http://en.wikipedia.org/wiki/Defuzzification for other defuzzifiers. - #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: @@ -43,11 +44,33 @@ namespace fl { } 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 index 61aaf8a..9cfd132 100644 --- a/fuzzylite/fl/defuzzifier/IntegralDefuzzifier.h +++ b/fuzzylite/fl/defuzzifier/IntegralDefuzzifier.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -28,24 +20,52 @@ #include "fl/defuzzifier/Defuzzifier.h" namespace fl { - //TODO: check http://en.wikipedia.org/wiki/Adaptive_quadrature + /** + 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 { - protected: + private: static int _defaultResolution; int _resolution; public: - - static void setDefaultResolution(int defaultResolution); - static int defaultResolution(); - 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(); + }; } diff --git a/fuzzylite/fl/defuzzifier/LargestOfMaximum.h b/fuzzylite/fl/defuzzifier/LargestOfMaximum.h index 8f1d3c8..e325db5 100644 --- a/fuzzylite/fl/defuzzifier/LargestOfMaximum.h +++ b/fuzzylite/fl/defuzzifier/LargestOfMaximum.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,6 +21,18 @@ 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()); @@ -36,6 +40,21 @@ namespace fl { 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; @@ -43,5 +62,6 @@ namespace fl { static Defuzzifier* constructor(); }; } + #endif /* FL_LARGESTOFMAXIMUM_H */ diff --git a/fuzzylite/fl/defuzzifier/MeanOfMaximum.h b/fuzzylite/fl/defuzzifier/MeanOfMaximum.h index 2c09759..027f43b 100644 --- a/fuzzylite/fl/defuzzifier/MeanOfMaximum.h +++ b/fuzzylite/fl/defuzzifier/MeanOfMaximum.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,6 +21,18 @@ 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()); @@ -36,6 +40,21 @@ namespace fl { 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; diff --git a/fuzzylite/fl/defuzzifier/SmallestOfMaximum.h b/fuzzylite/fl/defuzzifier/SmallestOfMaximum.h index 289e2a9..94f8a7a 100644 --- a/fuzzylite/fl/defuzzifier/SmallestOfMaximum.h +++ b/fuzzylite/fl/defuzzifier/SmallestOfMaximum.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,6 +21,18 @@ 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()); @@ -36,6 +40,21 @@ namespace fl { 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; diff --git a/fuzzylite/fl/defuzzifier/WeightedAverage.h b/fuzzylite/fl/defuzzifier/WeightedAverage.h index 3fbbd38..7f8e9f6 100644 --- a/fuzzylite/fl/defuzzifier/WeightedAverage.h +++ b/fuzzylite/fl/defuzzifier/WeightedAverage.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -30,6 +22,18 @@ 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); @@ -38,6 +42,23 @@ namespace fl { 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; 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 index 2bf0495..2045772 100644 --- a/fuzzylite/fl/defuzzifier/WeightedDefuzzifier.h +++ b/fuzzylite/fl/defuzzifier/WeightedDefuzzifier.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -30,33 +22,68 @@ 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, TakagiSugeno, Tsukamoto + /**Automatic: Automatically inferred from the terms */ + Automatic, + /**TakagiSugeno: Manually set to TakagiSugeno (or Inverse Tsukamoto)*/ + TakagiSugeno, + /**Tsukamoto: Manually set to Tsukamoto*/ + Tsukamoto }; - static std::string typeName(Type); + /** + 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) - virtual void setType(Type type); - virtual Type getType() const; + /** + 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; - virtual bool isMonotonic(const Term* term) const; - - virtual scalar tsukamoto(const Term* monotonic, scalar activationDegree, - scalar minimum, scalar maximum) const; - - protected: - Type _type; }; - } #endif /* FL_WEIGHTEDDEFUZZIFIER_H */ diff --git a/fuzzylite/fl/defuzzifier/WeightedSum.h b/fuzzylite/fl/defuzzifier/WeightedSum.h index a754023..fb61ef3 100644 --- a/fuzzylite/fl/defuzzifier/WeightedSum.h +++ b/fuzzylite/fl/defuzzifier/WeightedSum.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -30,6 +22,18 @@ 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); @@ -38,6 +42,23 @@ namespace fl { 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; 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 index 3262721..a21e1d2 100644 --- a/fuzzylite/fl/factory/CloningFactory.h +++ b/fuzzylite/fl/factory/CloningFactory.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -33,9 +25,20 @@ 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 FL_API CloningFactory { - protected: + class CloningFactory { + private: std::string _name; std::map<std::string, T> _objects; @@ -46,17 +49,178 @@ namespace fl { 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 index d01ca7d..2558d7f 100644 --- a/fuzzylite/fl/factory/ConstructionFactory.h +++ b/fuzzylite/fl/factory/ConstructionFactory.h @@ -1,29 +1,21 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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_FACTORY_H -#define FL_FACTORY_H +#ifndef FL_CONSTRUCTIONFACTORY_H +#define FL_CONSTRUCTIONFACTORY_H #include "fl/fuzzylite.h" @@ -33,12 +25,25 @@ 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 FL_API ConstructionFactory { + class ConstructionFactory { public: + /** + The Constructor type definition refers to a zero-parameter method + which returns an instance of T + */ typedef T(*Constructor)(); - protected: + private: std::string _name; std::map<std::string, Constructor> _constructors; @@ -47,18 +52,151 @@ namespace fl { 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; }; } -#endif /* FL_FACTORY_H */ +/** + * 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 index 32d3ee7..8fa4c1b 100644 --- a/fuzzylite/fl/factory/DefuzzifierFactory.h +++ b/fuzzylite/fl/factory/DefuzzifierFactory.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -33,19 +25,51 @@ 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) const; + 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 index d853d93..79f7996 100644 --- a/fuzzylite/fl/factory/FactoryManager.h +++ b/fuzzylite/fl/factory/FactoryManager.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -27,55 +19,136 @@ #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 { - class TNormFactory; - class SNormFactory; - class DefuzzifierFactory; - class TermFactory; - class HedgeFactory; - class FunctionFactory; + /** + 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 { - protected: - static FactoryManager _instance; - + 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(); - FactoryManager(TNormFactory* tnorm, SNormFactory* snorm, - DefuzzifierFactory* defuzzifier, TermFactory* term, - HedgeFactory* hedge, FunctionFactory* function); - FactoryManager(const FactoryManager& other); + 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(); - public: + /** + 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 index c1ea9f1..84e0104 100644 --- a/fuzzylite/fl/factory/FunctionFactory.h +++ b/fuzzylite/fl/factory/FunctionFactory.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -31,7 +23,18 @@ namespace fl { - class FunctionFactory : public CloningFactory<Function::Element*> { + /** + 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(); @@ -40,11 +43,18 @@ namespace fl { 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 index aca5d3a..e4733b2 100644 --- a/fuzzylite/fl/factory/HedgeFactory.h +++ b/fuzzylite/fl/factory/HedgeFactory.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -31,6 +23,15 @@ 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(); @@ -38,5 +39,6 @@ namespace fl { FL_DEFAULT_COPY_AND_MOVE(HedgeFactory) }; } + #endif /* FL_HEDGEFACTORY_H */ diff --git a/fuzzylite/fl/factory/SNormFactory.h b/fuzzylite/fl/factory/SNormFactory.h index ca8bf1a..111664a 100644 --- a/fuzzylite/fl/factory/SNormFactory.h +++ b/fuzzylite/fl/factory/SNormFactory.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -31,6 +23,15 @@ 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(); @@ -38,5 +39,6 @@ namespace fl { FL_DEFAULT_COPY_AND_MOVE(SNormFactory) }; } + #endif /* FL_SNORMFACTORY_H */ diff --git a/fuzzylite/fl/factory/TNormFactory.h b/fuzzylite/fl/factory/TNormFactory.h index 6af2249..5093fa7 100644 --- a/fuzzylite/fl/factory/TNormFactory.h +++ b/fuzzylite/fl/factory/TNormFactory.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -31,6 +23,15 @@ 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(); @@ -38,5 +39,6 @@ namespace fl { FL_DEFAULT_COPY_AND_MOVE(TNormFactory) }; } + #endif /* FL_TNORMFACTORY_H */ diff --git a/fuzzylite/fl/factory/TermFactory.h b/fuzzylite/fl/factory/TermFactory.h index 6fd0656..f6a5d60 100644 --- a/fuzzylite/fl/factory/TermFactory.h +++ b/fuzzylite/fl/factory/TermFactory.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -32,6 +24,15 @@ 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(); @@ -39,5 +40,6 @@ namespace fl { FL_DEFAULT_COPY_AND_MOVE(TermFactory) }; } + #endif /* FL_TERMFACTORY_H */ diff --git a/fuzzylite/fl/fuzzylite.h b/fuzzylite/fl/fuzzylite.h index 18c8dfa..3cca94b 100644 --- a/fuzzylite/fl/fuzzylite.h +++ b/fuzzylite/fl/fuzzylite.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -31,14 +23,7 @@ #include <sstream> #include <limits> #include <memory> - -#ifndef FL_VERSION -#define FL_VERSION "?" -#endif - -#ifndef FL_DATE -#define FL_DATE "?" -#endif +#include <cstddef> #ifndef FL_BUILD_PATH #define FL_BUILD_PATH "" @@ -54,19 +39,21 @@ #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_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_LOG(message) {if (fl::fuzzylite::logging()){std::cout << FL_LOG_PREFIX << message << std::endl;}} -#define FL_LOGP(message) {if (fl::fuzzylite::logging()){std::cout << message << std::endl;}} - -#define FL_DEBUG_BEGIN if (fl::fuzzylite::debug()){ +#define FL_DEBUG_BEGIN if (fl::fuzzylite::isDebugging()){ #define FL_DEBUG_END } #define FL_DBG(message) FL_DEBUG_BEGIN\ @@ -83,7 +70,7 @@ //#define bitand & //#define bitor | -//TODO: Address warning 4251 by exporting members? +//@todo: Address warning 4251 by exporting members? //http://www.unknownroad.com/rtfm/VisualStudio/warningC4251.html #ifdef _MSC_VER #pragma warning (disable:4251) @@ -107,21 +94,80 @@ #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 - const scalar nan = std::numeric_limits<scalar>::quiet_NaN(); - const scalar inf = std::numeric_limits<scalar>::infinity(); +#define FL_IUNUSED(x) (void) (x) + +#ifdef __GNUC__ +#define FL_IUNUSED_DECL __attribute__((unused)) +#else +#define FL_IUNUSED_DECL +#endif -#ifdef FL_CPP11 + /** + 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) @@ -132,6 +178,7 @@ namespace fl { #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) \ @@ -150,73 +197,231 @@ namespace fl { Class(const Class &) = delete;\ Class &operator=(const Class &) = delete; -#else - //C++98 defines +#endif - //Pointers - 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() - //Constructors -#define FL_DEFAULT_COPY(Class) -#define FL_DEFAULT_MOVE(Class) -#define FL_DEFAULT_COPY_AND_MOVE(Class) +namespace fl { -#define FL_DISABLE_COPY(Class) \ - Class(const Class &);\ - Class &operator=(const Class &); + /** -#endif -} + The fuzzylite class contains global settings and information about the + library. + @author Juan Rada-Vilela, Ph.D. + @since 4.0 -namespace fl { + */ class FL_API fuzzylite { - protected: + friend class Operation; + private: static int _decimals; static scalar _macheps; - static bool _debug; + 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(); - static std::string fullname(); + /** + Returns the version of the `fuzzylite` library + @return the version of the `fuzzylite` library + */ static std::string version(); - static std::string longVersion(); + /** + 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(); - static std::string author(); - static std::string company(); - static std::string website(); - static std::string date(); - static std::string platform(); + /** + Returns the name of the author of the `fuzzylite` library + @return "Juan Rada-Vilela, Ph.D." + */ + static std::string author(); - static std::string floatingPoint(); + /** + Returns the name of the company that owns the `fuzzylite` library + @return "FuzzyLite Limited" + */ + static std::string company(); - static bool debug(); - static void setDebug(bool debug); + /** + 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); - static bool logging(); + /** + 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 index c30b4c7..bf71842 100644 --- a/fuzzylite/fl/hedge/Any.h +++ b/fuzzylite/fl/hedge/Any.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -28,8 +20,20 @@ #include "fl/hedge/Hedge.h" namespace fl { - //Only this hedge has virtual methods due to its special case use. + /** + 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(); @@ -37,11 +41,20 @@ namespace fl { 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 index 7ecda66..fbd617a 100644 --- a/fuzzylite/fl/hedge/Extremely.h +++ b/fuzzylite/fl/hedge/Extremely.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,14 +21,35 @@ namespace fl { - class FL_API Extremely : public Hedge { + /** + 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; - scalar hedge(scalar x) const; - Extremely* clone() const; + 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 index 011c745..9dac17c 100644 --- a/fuzzylite/fl/hedge/Hedge.h +++ b/fuzzylite/fl/hedge/Hedge.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -27,10 +19,24 @@ #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: @@ -41,8 +47,28 @@ namespace fl { } 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; }; 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 index 161fefd..e981ac9 100644 --- a/fuzzylite/fl/hedge/Not.h +++ b/fuzzylite/fl/hedge/Not.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,14 +21,31 @@ namespace fl { - class FL_API Not : public Hedge { + /** + 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 index a2a96c2..0bbedbf 100644 --- a/fuzzylite/fl/hedge/Seldom.h +++ b/fuzzylite/fl/hedge/Seldom.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,14 +21,36 @@ namespace fl { - class FL_API Seldom : public Hedge { + /** + 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 index 89a14a6..ccd21ec 100644 --- a/fuzzylite/fl/hedge/Somewhat.h +++ b/fuzzylite/fl/hedge/Somewhat.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,14 +21,32 @@ namespace fl { - class FL_API Somewhat : public Hedge { + /** + 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 index bcfe5e4..b80c994 100644 --- a/fuzzylite/fl/hedge/Very.h +++ b/fuzzylite/fl/hedge/Very.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,14 +21,30 @@ namespace fl { - class FL_API Very : public Hedge { + /** + 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 index e53cf15..1c48225 100644 --- a/fuzzylite/fl/imex/CppExporter.h +++ b/fuzzylite/fl/imex/CppExporter.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -36,30 +28,129 @@ namespace fl { 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 { - protected: - bool _prefixNamespace; - virtual std::string fl(const std::string& clazz) const; + private: + bool _usingNamespace; + bool _usingVariableNames; public: - explicit CppExporter(bool prefixNamespace = false); + 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; - virtual void setPrefixNamespace(bool prefixNamespace); - virtual bool isPrefixNamespace() const; - + /** + 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; - virtual std::string toString(const Norm* op) 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; diff --git a/fuzzylite/fl/imex/Exporter.h b/fuzzylite/fl/imex/Exporter.h index 8b55c21..b883edf 100644 --- a/fuzzylite/fl/imex/Exporter.h +++ b/fuzzylite/fl/imex/Exporter.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -32,6 +24,16 @@ 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: @@ -39,10 +41,30 @@ namespace fl { 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; }; diff --git a/fuzzylite/fl/imex/FclExporter.h b/fuzzylite/fl/imex/FclExporter.h index 9bfa3ed..efec3c4 100644 --- a/fuzzylite/fl/imex/FclExporter.h +++ b/fuzzylite/fl/imex/FclExporter.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -37,8 +29,17 @@ namespace fl { 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 { - protected: + private: std::string _indent; public: @@ -46,25 +47,59 @@ namespace fl { 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; - - virtual std::string toString(const TNorm* tnorm) const; //TODO: Delete in v6.0 - virtual std::string toString(const SNorm* snorm) const; //TODO: Delete in v6.0 + /** + 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 index b219717..2e75432 100644 --- a/fuzzylite/fl/imex/FclImporter.h +++ b/fuzzylite/fl/imex/FclImporter.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -39,6 +31,15 @@ namespace fl { 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(); @@ -69,6 +70,5 @@ namespace fl { 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 index 05d6a22..a7f5d8c 100644 --- a/fuzzylite/fl/imex/FisExporter.h +++ b/fuzzylite/fl/imex/FisExporter.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -39,6 +31,15 @@ namespace fl { 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: @@ -52,22 +53,68 @@ namespace fl { virtual std::string name() const FL_IOVERRIDE; virtual std::string toString(const Engine* engine) const FL_IOVERRIDE; - - virtual std::string toString(const Norm* norm) const; - virtual std::string toString(const TNorm* tnorm) const; //TODO: delete in v6.0 - virtual std::string toString(const SNorm* snorm) const; //TODO: delete in v6.0 + + /** + 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 index b631a48..2c9ce36 100644 --- a/fuzzylite/fl/imex/FisImporter.h +++ b/fuzzylite/fl/imex/FisImporter.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -39,6 +31,16 @@ namespace fl { 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(); @@ -60,20 +62,19 @@ namespace fl { 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; - - //TODO: rename extract to translate in v6.0 - virtual std::string extractTNorm(const std::string& tnorm) const; - virtual std::string extractSNorm(const std::string& tnorm) const; - virtual std::string extractDefuzzifier(const std::string& defuzzifier) 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; - //TODO: rename to parseRange in v6.0 - virtual std::pair<scalar, scalar> range(const std::string& range) 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 index 71679a6..98a14c9 100644 --- a/fuzzylite/fl/imex/FldExporter.h +++ b/fuzzylite/fl/imex/FldExporter.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -34,47 +26,219 @@ namespace fl { 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 { - protected: + 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; - //WARNING: The engine will be const_casted in order to be processed! + /** + 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; - virtual std::string toString(Engine* engine, int maximumNumberOfResults) const; - virtual std::string toString(Engine* engine, const std::string& inputData) 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 + @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; - virtual void toFile(const std::string& path, Engine* engine, int maximumNumberOfResults) const; - virtual void toFile(const std::string& path, Engine* engine, const std::string& inputData) const; - - virtual std::vector<scalar> parse(const std::string& x) const; - void write(Engine* engine, std::ostream& writer, int maximumNumberOfResults) const; - void write(Engine* engine, std::ostream& writer, std::istream& reader) const; - void write(Engine* engine, std::ostream& writer, const std::vector<scalar>& inputValues) 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; }; diff --git a/fuzzylite/fl/imex/FllExporter.h b/fuzzylite/fl/imex/FllExporter.h index e6a89f5..5a222e4 100644 --- a/fuzzylite/fl/imex/FllExporter.h +++ b/fuzzylite/fl/imex/FllExporter.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -36,11 +28,23 @@ namespace fl { 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 { - protected: + private: std::string _indent; std::string _separator; public: @@ -50,33 +54,114 @@ namespace fl { 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 index 5be41c7..055bb88 100644 --- a/fuzzylite/fl/imex/FllImporter.h +++ b/fuzzylite/fl/imex/FllImporter.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -32,18 +24,39 @@ 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 { - protected: + 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; @@ -52,6 +65,7 @@ namespace fl { 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; @@ -59,6 +73,7 @@ namespace fl { 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; @@ -68,7 +83,6 @@ namespace fl { virtual std::pair<std::string, std::string> parseKeyValue(const std::string& text, char separator = ':') const; - virtual std::string clean(const std::string& line) const; }; } diff --git a/fuzzylite/fl/imex/Importer.h b/fuzzylite/fl/imex/Importer.h index 28d2b36..1250e66 100644 --- a/fuzzylite/fl/imex/Importer.h +++ b/fuzzylite/fl/imex/Importer.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -32,6 +24,16 @@ 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: @@ -39,13 +41,30 @@ namespace fl { virtual ~Importer(); FL_DEFAULT_COPY_AND_MOVE(Importer) - virtual Engine* fromString(const std::string& s) const = 0; + /** + 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 index e882d4a..a1817f6 100644 --- a/fuzzylite/fl/imex/JavaExporter.h +++ b/fuzzylite/fl/imex/JavaExporter.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -38,30 +30,122 @@ namespace fl { 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: - JavaExporter(); + 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; - virtual std::string toString(const SNorm* norm) const;//TODO: delete in v6.0 - virtual std::string toString(const TNorm* norm) const;//TODO: delete in v6.0 + + /** + 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 index 5f1b578..682d2ad 100644 --- a/fuzzylite/fl/norm/Norm.h +++ b/fuzzylite/fl/norm/Norm.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -27,12 +19,22 @@ #include "fl/fuzzylite.h" -#include "fl/Operation.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: @@ -43,13 +45,31 @@ namespace fl { } 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 index a281f52..a68081c 100644 --- a/fuzzylite/fl/norm/SNorm.h +++ b/fuzzylite/fl/norm/SNorm.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,6 +21,19 @@ 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: @@ -44,4 +49,3 @@ namespace fl { }; } #endif /* FL_SNORM_H */ - diff --git a/fuzzylite/fl/norm/TNorm.h b/fuzzylite/fl/norm/TNorm.h index 8ba8538..b1f7cb7 100644 --- a/fuzzylite/fl/norm/TNorm.h +++ b/fuzzylite/fl/norm/TNorm.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,6 +21,18 @@ 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: @@ -44,4 +48,3 @@ namespace fl { }; } #endif /* TNORM_H */ - diff --git a/fuzzylite/fl/norm/s/AlgebraicSum.h b/fuzzylite/fl/norm/s/AlgebraicSum.h index 786bcbd..2fbf4c1 100644 --- a/fuzzylite/fl/norm/s/AlgebraicSum.h +++ b/fuzzylite/fl/norm/s/AlgebraicSum.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -27,20 +19,35 @@ #include "fl/norm/SNorm.h" - namespace fl { - class FL_API AlgebraicSum : public SNorm { + /** + 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 index 5231856..cd9438e 100644 --- a/fuzzylite/fl/norm/s/BoundedSum.h +++ b/fuzzylite/fl/norm/s/BoundedSum.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,15 +21,34 @@ namespace fl { - class FL_API BoundedSum : public SNorm { + /** + 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 index 65d4d4c..a1e33fe 100644 --- a/fuzzylite/fl/norm/s/DrasticSum.h +++ b/fuzzylite/fl/norm/s/DrasticSum.h @@ -1,43 +1,55 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 { - class FL_API DrasticSum : public SNorm { + /** + 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 index 8114c68..7450330 100644 --- a/fuzzylite/fl/norm/s/EinsteinSum.h +++ b/fuzzylite/fl/norm/s/EinsteinSum.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,16 +21,32 @@ namespace fl { - class FL_API EinsteinSum : public SNorm { + /** + 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 index 3daf852..0242512 100644 --- a/fuzzylite/fl/norm/s/HamacherSum.h +++ b/fuzzylite/fl/norm/s/HamacherSum.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,16 +21,32 @@ namespace fl { - class FL_API HamacherSum : public SNorm { + /** + 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 index 28a3b6d..c8ce488 100644 --- a/fuzzylite/fl/norm/s/Maximum.h +++ b/fuzzylite/fl/norm/s/Maximum.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,17 +21,31 @@ namespace fl { - class FL_API Maximum : public SNorm { + /** + 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 index 151d92d..13e9793 100644 --- a/fuzzylite/fl/norm/s/NilpotentMaximum.h +++ b/fuzzylite/fl/norm/s/NilpotentMaximum.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,9 +21,31 @@ namespace fl { - class FL_API NilpotentMaximum : public SNorm { + /** + 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; @@ -40,4 +54,3 @@ namespace fl { } #endif /* FL_NILPOTENTMAXIMUM_H */ - diff --git a/fuzzylite/fl/norm/s/NormalizedSum.h b/fuzzylite/fl/norm/s/NormalizedSum.h index 59063a6..e2e757b 100644 --- a/fuzzylite/fl/norm/s/NormalizedSum.h +++ b/fuzzylite/fl/norm/s/NormalizedSum.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,16 +21,32 @@ namespace fl { - class FL_API NormalizedSum : public SNorm { + /** + 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 index e4b0865..d19cb64 100644 --- a/fuzzylite/fl/norm/t/AlgebraicProduct.h +++ b/fuzzylite/fl/norm/t/AlgebraicProduct.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,17 +21,32 @@ namespace fl { - class FL_API AlgebraicProduct : public TNorm { + /** + 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 index 3dc9d3a..250512b 100644 --- a/fuzzylite/fl/norm/t/BoundedDifference.h +++ b/fuzzylite/fl/norm/t/BoundedDifference.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,16 +21,32 @@ namespace fl { - class FL_API BoundedDifference : public TNorm { + /** + 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 index efd589d..d0d490e 100644 --- a/fuzzylite/fl/norm/t/DrasticProduct.h +++ b/fuzzylite/fl/norm/t/DrasticProduct.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,17 +21,35 @@ namespace fl { - class FL_API DrasticProduct : public TNorm { + /** + 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 index a4c1141..9d3a71b 100644 --- a/fuzzylite/fl/norm/t/EinsteinProduct.h +++ b/fuzzylite/fl/norm/t/EinsteinProduct.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,17 +21,32 @@ namespace fl { - class FL_API EinsteinProduct : public TNorm { + /** + 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 index 9bc6b40..79c56ee 100644 --- a/fuzzylite/fl/norm/t/HamacherProduct.h +++ b/fuzzylite/fl/norm/t/HamacherProduct.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,16 +21,32 @@ namespace fl { - class FL_API HamacherProduct : public TNorm { + /** + 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 index 3590e42..5df1ae2 100644 --- a/fuzzylite/fl/norm/t/Minimum.h +++ b/fuzzylite/fl/norm/t/Minimum.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,16 +21,31 @@ namespace fl { - class FL_API Minimum : public TNorm { + /** + 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 index b3c11e0..2ea7f3c 100644 --- a/fuzzylite/fl/norm/t/NilpotentMinimum.h +++ b/fuzzylite/fl/norm/t/NilpotentMinimum.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,15 +21,35 @@ namespace fl { - class FL_API NilpotentMinimum : public TNorm { + /** + 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 index 02724e5..a424d95 100644 --- a/fuzzylite/fl/rule/Antecedent.h +++ b/fuzzylite/fl/rule/Antecedent.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -27,6 +19,8 @@ #include "fl/fuzzylite.h" +#include "fl/Complexity.h" + #include <string> namespace fl { @@ -36,42 +30,156 @@ namespace fl { 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 { - protected: + private: std::string _text; - Expression* _expression; + 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(); - virtual void load(Rule* rule, const Engine* engine); - virtual void load(const std::string& antecedent, Rule* rule, const Engine* engine); + /** + 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 index 5394dc0..275d1e7 100644 --- a/fuzzylite/fl/rule/Consequent.h +++ b/fuzzylite/fl/rule/Consequent.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -27,6 +19,8 @@ #include "fl/fuzzylite.h" +#include "fl/Complexity.h" + #include <string> #include <vector> @@ -36,8 +30,26 @@ namespace fl { 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 { - protected: + private: std::string _text; std::vector<Proposition*> _conclusions; @@ -45,24 +57,82 @@ namespace fl { 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(); - virtual void load(Rule* rule, const Engine* engine); - virtual void load(const std::string& consequent, Rule* rule, const Engine* engine); - - virtual void modify(scalar activationDegree, const TNorm* activation); - + /** + 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 index cb48355..dbacc60 100644 --- a/fuzzylite/fl/rule/Expression.h +++ b/fuzzylite/fl/rule/Expression.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -37,48 +29,105 @@ namespace fl { 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) }; - class FL_API Proposition : public 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(); - virtual ~Proposition() FL_IOVERRIDE; + ~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; - virtual std::string toString() const FL_IOVERRIDE; private: FL_DISABLE_COPY(Proposition) }; - class FL_API Operator : public Expression { + /** + 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(); - virtual ~Operator() FL_IOVERRIDE; + ~Operator() FL_IOVERRIDE; + + Expression::Type type() const FL_IOVERRIDE; - virtual std::string toString() 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 index 20d39fb..b123d15 100644 --- a/fuzzylite/fl/rule/Rule.h +++ b/fuzzylite/fl/rule/Rule.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -27,25 +19,59 @@ #include "fl/fuzzylite.h" +#include "fl/rule/Consequent.h" +#include "fl/rule/Antecedent.h" + #include <map> #include <string> namespace fl { class Engine; - class Antecedent; - class Consequent; 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 { - protected: + private: + bool _enabled; std::string _text; scalar _weight; + scalar _activationDegree; + bool _triggered; FL_unique_ptr<Antecedent> _antecedent; FL_unique_ptr<Consequent> _consequent; - std::map<std::string, Hedge*> _hedges; public: explicit Rule(const std::string& text = "", scalar weight = 1.0); @@ -54,68 +80,226 @@ namespace fl { 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; - virtual void addHedge(Hedge* hedge); - virtual Hedge* getHedge(const std::string& name) const; - virtual Hedge* removeHedge(const std::string& hedge); - virtual bool hasHedge(const std::string& name) const; - virtual int numberOfHedges() const; - virtual void setHedges(const std::map<std::string, Hedge*>& hedges); - virtual const std::map<std::string, Hedge*>& hedges() const; - virtual std::map<std::string, Hedge*>& hedges(); - - virtual scalar activationDegree(const TNorm* conjunction, const SNorm* disjunction) const; - virtual void activate(scalar degree, const TNorm* activation) 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); - static std::string ifKeyword() { + /** + 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"; } - static std::string isKeyword() { + /** + 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"; } - static std::string thenKeyword() { + /** + 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"; } - static std::string andKeyword() { + /** + 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"; } - static std::string orKeyword() { + /** + 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"; } - static std::string withKeyword() { + /** + 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 index 35fe62e..d4ccace 100644 --- a/fuzzylite/fl/rule/RuleBlock.h +++ b/fuzzylite/fl/rule/RuleBlock.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -27,6 +19,9 @@ #include "fl/fuzzylite.h" +#include "fl/activation/Activation.h" +#include "fl/Complexity.h" + #include <string> #include <vector> @@ -38,16 +33,29 @@ namespace fl { 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: - void copyFrom(const RuleBlock& source); - protected: + 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> _activation; - bool _enabled; + FL_unique_ptr<TNorm> _implication; + FL_unique_ptr<Activation> _activation; + + void copyFrom(const RuleBlock& source); public: explicit RuleBlock(const std::string& name = ""); @@ -56,42 +64,170 @@ namespace fl { 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; - virtual void setActivation(TNorm* activation); - virtual TNorm* getActivation() 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; - virtual void setEnabled(bool enabled); - virtual bool isEnabled() 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; /** - * Operations for iterable datatype _rules + 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); - virtual void insertRule(Rule* rule, int index); - virtual Rule* getRule(int index) const; - virtual Rule* removeRule(int index); - virtual int numberOfRules() const; + /** + 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/Accumulated.h b/fuzzylite/fl/term/Accumulated.h deleted file mode 100644 index 51625d0..0000000 --- a/fuzzylite/fl/term/Accumulated.h +++ /dev/null @@ -1,94 +0,0 @@ -/* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved - - This file is part of fuzzylite. - - fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. - - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. - - */ - -#ifndef FL_ACCUMULATED_H -#define FL_ACCUMULATED_H - -#include "fl/term/Term.h" - -#include <vector> - -namespace fl { - - class Activated; - class SNorm; - class TNorm; - - class FL_API Accumulated : public Term { - private: - void copyFrom(const Accumulated& source); - protected: - std::vector<Activated*> _terms; - scalar _minimum, _maximum; - FL_unique_ptr<SNorm> _accumulation; - public: - explicit Accumulated(const std::string& name = "", - scalar minimum = fl::nan, - scalar maximum = fl::nan, - SNorm* accumulation = fl::null); - Accumulated(const Accumulated& other); - Accumulated& operator=(const Accumulated& other); - virtual ~Accumulated() FL_IOVERRIDE; - FL_DEFAULT_MOVE(Accumulated) - - virtual std::string className() const FL_IOVERRIDE; - virtual std::string parameters() const FL_IOVERRIDE; - virtual void configure(const std::string& parameters) FL_IOVERRIDE; - - virtual Accumulated* clone() const FL_IOVERRIDE; - - virtual scalar membership(scalar x) const FL_IOVERRIDE; - virtual scalar activationDegree(const Term* forTerm) const; - - virtual std::string toString() const FL_IOVERRIDE; - - - virtual void setMinimum(scalar minimum); - virtual scalar getMinimum() const; - - virtual void setMaximum(scalar maximum); - virtual scalar getMaximum() const; - - virtual void setRange(scalar minimum, scalar maximum); - virtual scalar range() const; - - virtual void setAccumulation(SNorm* accumulation); - virtual SNorm* getAccumulation() const; - - /** - * Operations for std::vector _terms - */ - virtual void addTerm(const Term* term, scalar degree, const TNorm* activation); - virtual void addTerm(Activated* term); - virtual Activated* getTerm(int index) const; - virtual Activated* removeTerm(int index); - virtual int numberOfTerms() const; - virtual const std::vector<Activated*>& terms() const; - virtual std::vector<Activated*>& terms(); - virtual bool isEmpty() const; - virtual void clear(); - }; - -} -#endif /* FL_ACCUMULATED_H */ diff --git a/fuzzylite/fl/term/Activated.h b/fuzzylite/fl/term/Activated.h index 10c1b40..f122a25 100644 --- a/fuzzylite/fl/term/Activated.h +++ b/fuzzylite/fl/term/Activated.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -30,36 +22,86 @@ 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 { - protected: + private: const Term* _term; scalar _degree; - const TNorm* _activation; + const TNorm* _implication; public: explicit Activated(const Term* term = fl::null, scalar degree = 1.0, - const TNorm* activationOperator = fl::null); + 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; - virtual void setActivation(const TNorm* activation); - virtual const TNorm* getActivation() 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 index 115479c..e27d9c9 100644 --- a/fuzzylite/fl/term/Bell.h +++ b/fuzzylite/fl/term/Bell.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,8 +21,19 @@ 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 { - protected: + private: scalar _center; scalar _width; scalar _slope; @@ -44,18 +47,62 @@ namespace fl { 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; @@ -63,6 +110,5 @@ namespace fl { 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 index b3aabe6..12d7160 100644 --- a/fuzzylite/fl/term/Concave.h +++ b/fuzzylite/fl/term/Concave.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -27,11 +19,21 @@ #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 { - protected: + private: scalar _inflection, _end; public: explicit Concave(const std::string& name = "", @@ -42,15 +44,61 @@ namespace fl { 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; @@ -58,7 +106,5 @@ namespace fl { static Term* constructor(); }; } - - #endif /* FL_CONCAVE_H */ diff --git a/fuzzylite/fl/term/Constant.h b/fuzzylite/fl/term/Constant.h index c4ed17d..40ba77b 100644 --- a/fuzzylite/fl/term/Constant.h +++ b/fuzzylite/fl/term/Constant.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,8 +21,17 @@ 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 { - protected: + private: scalar _value; public: @@ -40,12 +41,35 @@ namespace fl { 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; @@ -53,6 +77,5 @@ namespace fl { static Term* constructor(); }; } - #endif /* FL_CONSTANT_H */ diff --git a/fuzzylite/fl/term/Cosine.h b/fuzzylite/fl/term/Cosine.h index 510669f..b45ab85 100644 --- a/fuzzylite/fl/term/Cosine.h +++ b/fuzzylite/fl/term/Cosine.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,8 +21,20 @@ 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 { - protected: + private: scalar _center, _width; public: explicit Cosine(const std::string& name = "", @@ -41,15 +45,51 @@ namespace fl { 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; @@ -57,6 +97,5 @@ namespace fl { static Term* constructor(); }; } - #endif /* FL_COSINE_H */ diff --git a/fuzzylite/fl/term/Discrete.h b/fuzzylite/fl/term/Discrete.h index fc554b3..ba5ae70 100644 --- a/fuzzylite/fl/term/Discrete.h +++ b/fuzzylite/fl/term/Discrete.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -27,15 +19,30 @@ #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; - protected: + private: std::vector<Pair> _xy; public: explicit Discrete(const std::string& name = "", @@ -45,37 +52,229 @@ namespace fl { 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; - //Warning: this method is unsafe. Make sure you use it correctly. - template <typename T> - static Discrete* create(const std::string& name, int argc, - T x1, T y1, ...); // throw (fl::Exception); + /** + 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(); - virtual const Pair& xy(int index) const; - virtual Pair& xy(int index); + /** + 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& postfix = ")", const std::string& outerSeparator = " "); + 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 index 99580b7..14e3427 100644 --- a/fuzzylite/fl/term/Function.h +++ b/fuzzylite/fl/term/Function.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -34,27 +26,77 @@ 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 { - /**************************** - * Parsing Elements - ****************************/ - 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 + 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; - int precedence; //Operator + /**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, @@ -64,24 +106,46 @@ namespace fl { 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; }; - /************************** - * Tree elements, wrap Elements into Nodes. - **************************/ - + /** + 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); @@ -92,14 +156,79 @@ namespace fl { 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); @@ -112,11 +241,12 @@ namespace fl { * Term ******************************/ - protected: + 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); @@ -125,47 +255,145 @@ namespace fl { 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); // throw (fl::Exception); - + 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; - virtual scalar evaluate(const std::map<std::string, scalar>* variables) const; + /** + 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(); - virtual void load(); // throw (fl::Exception); - virtual void load(const std::string& formula); // throw (fl::Exception); - virtual void load(const std::string& formula, const Engine* engine); // throw (fl::Exception); - - virtual Node* parse(const std::string& formula); // throw (fl::Exception); - - virtual std::string toPostfix(const std::string& formula) const; //throw (fl::Exception); - + /** + 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(); - static void main(); - }; - } - #endif /* FL_FUNCTION_H */ diff --git a/fuzzylite/fl/term/Gaussian.h b/fuzzylite/fl/term/Gaussian.h index a5b8055..9a7475d 100644 --- a/fuzzylite/fl/term/Gaussian.h +++ b/fuzzylite/fl/term/Gaussian.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,8 +21,19 @@ 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 { - protected: + private: scalar _mean; scalar _standardDeviation; @@ -43,21 +46,54 @@ namespace fl { 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; - virtual void setMean(scalar c); + /** + 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; - virtual void setStandardDeviation(scalar sigma); + /** + 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 index bd1eaaa..e4a500c 100644 --- a/fuzzylite/fl/term/GaussianProduct.h +++ b/fuzzylite/fl/term/GaussianProduct.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,8 +21,19 @@ 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 { - protected: + private: scalar _meanA; scalar _standardDeviationA; scalar _meanB; @@ -47,21 +50,83 @@ namespace fl { 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; - virtual void setStandardDeviationA(scalar sigmaA); + /** + 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; - virtual void setStandardDeviationB(scalar sigmaB); + /** + 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; diff --git a/fuzzylite/fl/term/Linear.h b/fuzzylite/fl/term/Linear.h index 67ede04..1105a1d 100644 --- a/fuzzylite/fl/term/Linear.h +++ b/fuzzylite/fl/term/Linear.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -30,8 +22,23 @@ 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 { - protected: + private: + /**Contains the coefficients @f$c_i@f$ and the constant @f$k@f$*/ std::vector<scalar> _coefficients; const Engine* _engine; public: @@ -42,31 +49,131 @@ namespace fl { 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 scalar membership(scalar x) const FL_IOVERRIDE; + virtual Complexity complexity() const FL_IOVERRIDE; - virtual void set(const std::vector<scalar>& coeffs, const Engine* engine); + /** + 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; - virtual void setCoefficients(const std::vector<scalar>& coeffs); + /** + 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(); - //Warning: this method is unsafe, make sure you use it correctly. + /** + 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, ...); // throw (fl::Exception); + 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 index a0c2f78..24e98ed 100644 --- a/fuzzylite/fl/term/PiShape.h +++ b/fuzzylite/fl/term/PiShape.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,8 +21,19 @@ 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 { - protected: + private: scalar _bottomLeft; scalar _topLeft; scalar _topRight; @@ -47,21 +50,82 @@ namespace fl { 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; - virtual void setBottomLeft(scalar a); + /** + 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; - virtual void setTopLeft(scalar b); + /** + 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; - virtual void setTopRight(scalar d); + /** + 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; - virtual void setBottomRight(scalar c); + /** + 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; @@ -69,6 +133,5 @@ namespace fl { static Term* constructor(); }; } - #endif /* FL_PISHAPE_H */ diff --git a/fuzzylite/fl/term/Ramp.h b/fuzzylite/fl/term/Ramp.h index 63a683c..dc49301 100644 --- a/fuzzylite/fl/term/Ramp.h +++ b/fuzzylite/fl/term/Ramp.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,14 +21,30 @@ 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 { - protected: + private: scalar _start, _end; public: + /** + Direction is an enumerator that indicates the direction of the ramp. + */ enum Direction { - POSITIVE, ZERO, NEGATIVE + /** `(_/)` increases to the right */ Positive, + /** `(--)` slope is zero */ Zero, + /** `(\\_)` increases to the left */ Negative }; explicit Ramp(const std::string& name = "", scalar start = fl::nan, @@ -46,17 +54,76 @@ namespace fl { 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; diff --git a/fuzzylite/fl/term/Rectangle.h b/fuzzylite/fl/term/Rectangle.h index 23b5eda..0e29cd2 100644 --- a/fuzzylite/fl/term/Rectangle.h +++ b/fuzzylite/fl/term/Rectangle.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,8 +21,19 @@ 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 { - protected: + private: scalar _start, _end; public: @@ -42,21 +45,58 @@ namespace fl { 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 index 0891357..1d107a5 100644 --- a/fuzzylite/fl/term/SShape.h +++ b/fuzzylite/fl/term/SShape.h @@ -1,28 +1,19 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -30,8 +21,19 @@ 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 { - protected: + private: scalar _start, _end; public: @@ -43,15 +45,59 @@ namespace fl { 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; @@ -59,6 +105,5 @@ namespace fl { static Term* constructor(); }; } - #endif /* FL_SSHAPE_H */ diff --git a/fuzzylite/fl/term/Sigmoid.h b/fuzzylite/fl/term/Sigmoid.h index b37b1be..bde9c2f 100644 --- a/fuzzylite/fl/term/Sigmoid.h +++ b/fuzzylite/fl/term/Sigmoid.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,14 +21,31 @@ 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 { - protected: + private: scalar _inflection; scalar _slope; public: + /** + Direction is an enumerator that indicates the direction of the + sigmoid. + */ enum Direction { - POSITIVE, ZERO, NEGATIVE + /** `(_/)` increases to the right */ Positive, + /** `(--)` slope is zero */ Zero, + /** `(\\_)` increases to the left */ Negative }; explicit Sigmoid(const std::string& name = "", scalar inflection = fl::nan, @@ -46,23 +55,66 @@ namespace fl { 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 index 020f164..4861584 100644 --- a/fuzzylite/fl/term/SigmoidDifference.h +++ b/fuzzylite/fl/term/SigmoidDifference.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,8 +21,19 @@ 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 { - protected: + private: scalar _left; scalar _rising; scalar _falling; @@ -47,21 +50,76 @@ namespace fl { 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; diff --git a/fuzzylite/fl/term/SigmoidProduct.h b/fuzzylite/fl/term/SigmoidProduct.h index cb83f30..19cd4d7 100644 --- a/fuzzylite/fl/term/SigmoidProduct.h +++ b/fuzzylite/fl/term/SigmoidProduct.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,8 +21,19 @@ 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 { - protected: + private: scalar _left; scalar _rising; scalar _falling; @@ -47,23 +50,79 @@ namespace fl { 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(); diff --git a/fuzzylite/fl/term/Spike.h b/fuzzylite/fl/term/Spike.h index 7e520c9..7349307 100644 --- a/fuzzylite/fl/term/Spike.h +++ b/fuzzylite/fl/term/Spike.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,8 +21,19 @@ 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 { - protected: + private: scalar _center, _width; public: explicit Spike(const std::string& name = "", @@ -41,15 +44,49 @@ namespace fl { 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; @@ -57,6 +94,5 @@ namespace fl { static Term* constructor(); }; } - #endif /* FL_SPIKE_H */ diff --git a/fuzzylite/fl/term/Term.h b/fuzzylite/fl/term/Term.h index 6c2b2b1..31dcabd 100644 --- a/fuzzylite/fl/term/Term.h +++ b/fuzzylite/fl/term/Term.h @@ -1,34 +1,26 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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> @@ -37,9 +29,33 @@ 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 { - protected: + private: std::string _name; + protected: scalar _height; public: @@ -47,25 +63,103 @@ namespace fl { 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; - - //FIXME: This should not be static, and may not be here either. - static void updateReference(Term* term, const Engine* engine); - }; + /** + 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 index f896997..ebf5ed8 100644 --- a/fuzzylite/fl/term/Trapezoid.h +++ b/fuzzylite/fl/term/Trapezoid.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,8 +21,19 @@ 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 { - protected: + private: scalar _vertexA, _vertexB, _vertexC, _vertexD; public: explicit Trapezoid(const std::string& name = "", @@ -43,27 +46,84 @@ namespace fl { 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 index 9311687..8b16fa9 100644 --- a/fuzzylite/fl/term/Triangle.h +++ b/fuzzylite/fl/term/Triangle.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,8 +21,19 @@ 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 { - protected: + private: scalar _vertexA; scalar _vertexB; scalar _vertexC; @@ -44,18 +47,66 @@ namespace fl { 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; @@ -63,6 +114,5 @@ namespace fl { static Term* constructor(); }; - } #endif /* FL_TRIANGLE_H */ diff --git a/fuzzylite/fl/term/ZShape.h b/fuzzylite/fl/term/ZShape.h index b53906e..1737891 100644 --- a/fuzzylite/fl/term/ZShape.h +++ b/fuzzylite/fl/term/ZShape.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,8 +21,20 @@ 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 { - protected: + private: scalar _start, _end; public: @@ -42,15 +46,59 @@ namespace fl { 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; diff --git a/fuzzylite/fl/variable/InputVariable.h b/fuzzylite/fl/variable/InputVariable.h index bba3c7d..3201e3d 100644 --- a/fuzzylite/fl/variable/InputVariable.h +++ b/fuzzylite/fl/variable/InputVariable.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -29,9 +21,17 @@ 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 { - protected: - scalar _inputValue; public: explicit InputVariable(const std::string& name = "", scalar minimum = -fl::inf, @@ -39,14 +39,22 @@ namespace fl { virtual ~InputVariable() FL_IOVERRIDE; FL_DEFAULT_COPY_AND_MOVE(InputVariable) - virtual void setInputValue(scalar inputValue); - virtual scalar getInputValue() const; + /** + 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 index 9d00aaf..b79b13c 100644 --- a/fuzzylite/fl/variable/OutputVariable.h +++ b/fuzzylite/fl/variable/OutputVariable.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -27,64 +19,208 @@ #include "fl/variable/Variable.h" +#include "fl/term/Activated.h" +#include "fl/term/Aggregated.h" + +#include "fl/defuzzifier/Defuzzifier.h" + namespace fl { - class Accumulated; - class Defuzzifier; + /** + 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: - void copyFrom(const OutputVariable& other); - protected: - FL_unique_ptr<Accumulated> _fuzzyOutput; + FL_unique_ptr<Aggregated> _fuzzyOutput; FL_unique_ptr<Defuzzifier> _defuzzifier; - scalar _outputValue; - scalar _previousOutputValue; + scalar _previousValue; scalar _defaultValue; - bool _lockOutputValueInRange; - bool _lockPreviousOutputValue; + bool _lockPreviousValue; + + void copyFrom(const OutputVariable& other); public: explicit OutputVariable(const std::string& name = "", scalar minimum = -fl::inf, scalar maximum = fl::inf); - OutputVariable(const OutputVariable& other); + explicit OutputVariable(const OutputVariable& other); OutputVariable& operator=(const OutputVariable& other); virtual ~OutputVariable() FL_IOVERRIDE; FL_DEFAULT_MOVE(OutputVariable) - virtual Accumulated* fuzzyOutput() const; + /** + 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; - virtual void setOutputValue(scalar outputValue); - virtual scalar getOutputValue() const; - - virtual void setPreviousOutputValue(scalar defuzzifiedValue); - virtual scalar getPreviousOutputValue() 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; - virtual void setLockOutputValueInRange(bool lockOutputValueInRange); - virtual bool isLockedOutputValueInRange() const; - - virtual void setLockPreviousOutputValue(bool lockPreviousOutputValue); - virtual bool isLockedPreviousOutputValue() 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 index 61870af..47530c0 100644 --- a/fuzzylite/fl/variable/Variable.h +++ b/fuzzylite/fl/variable/Variable.h @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 @@ -35,62 +27,272 @@ 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 { - private: - void copyFrom(const Variable& source); + 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); - Variable(const Variable& other); + 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; /** - * Operations for iterable datatype _terms + 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); - virtual void insertTerm(Term* term, int index); - virtual Term* getTerm(int index) const; + /** + 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; - virtual Term* removeTerm(int index); - virtual int numberOfTerms() 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.doxygen b/fuzzylite/fuzzylite.doxygen deleted file mode 100644 index 4ee925f..0000000 --- a/fuzzylite/fuzzylite.doxygen +++ /dev/null @@ -1,1781 +0,0 @@ -# Doxyfile 1.7.6.1 - -# This file describes the settings to be used by the documentation system -# doxygen (www.doxygen.org) for a project. -# -# All text after a hash (#) is considered a comment and will be ignored. -# The format is: -# TAG = value [value, ...] -# For lists items can also be appended using: -# TAG += value [value, ...] -# Values that contain spaces should be placed between quotes (" "). - -#--------------------------------------------------------------------------- -# Project related configuration options -#--------------------------------------------------------------------------- - -# This tag specifies the encoding used for all characters in the config file -# that follow. The default is UTF-8 which is also the encoding used for all -# text before the first occurrence of this tag. Doxygen uses libiconv (or the -# iconv built into libc) for the transcoding. See -# http://www.gnu.org/software/libiconv for the list of possible encodings. - -DOXYFILE_ENCODING = UTF-8 - -# The PROJECT_NAME tag is a single word (or sequence of words) that should -# identify the project. Note that if you do not use Doxywizard you need -# to put quotes around the project name if it contains spaces. - -PROJECT_NAME = "fuzzylite" - -# The PROJECT_NUMBER tag can be used to enter a project or revision number. -# This could be handy for archiving the generated documentation or -# if some version control system is used. - -PROJECT_NUMBER = 4.0 - -# Using the PROJECT_BRIEF tag one can provide an optional one line description -# for a project that appears at the top of each page and should give viewer -# a quick idea about the purpose of the project. Keep the description short. - -PROJECT_BRIEF = "A Fuzzy Logic Control Library in C++" - -# With the PROJECT_LOGO tag one can specify an logo or icon that is -# included in the documentation. The maximum height of the logo should not -# exceed 55 pixels and the maximum width should not exceed 200 pixels. -# Doxygen will copy the logo to the output directory. - -PROJECT_LOGO = ../fuzzylite.png - -# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) -# base path where the generated documentation will be put. -# If a relative path is entered, it will be relative to the location -# where doxygen was started. If left blank the current directory will be used. - -OUTPUT_DIRECTORY = docs - -# If the CREATE_SUBDIRS tag is set to YES, then doxygen will create -# 4096 sub-directories (in 2 levels) under the output directory of each output -# format and will distribute the generated files over these directories. -# Enabling this option can be useful when feeding doxygen a huge amount of -# source files, where putting all generated files in the same directory would -# otherwise cause performance problems for the file system. - -CREATE_SUBDIRS = NO - -# The OUTPUT_LANGUAGE tag is used to specify the language in which all -# documentation generated by doxygen is written. Doxygen will use this -# information to generate all constant output in the proper language. -# The default language is English, other supported languages are: -# Afrikaans, Arabic, Brazilian, Catalan, Chinese, Chinese-Traditional, -# Croatian, Czech, Danish, Dutch, Esperanto, Farsi, Finnish, French, German, -# Greek, Hungarian, Italian, Japanese, Japanese-en (Japanese with English -# messages), Korean, Korean-en, Lithuanian, Norwegian, Macedonian, Persian, -# Polish, Portuguese, Romanian, Russian, Serbian, Serbian-Cyrillic, Slovak, -# Slovene, Spanish, Swedish, Ukrainian, and Vietnamese. - -OUTPUT_LANGUAGE = English - -# If the BRIEF_MEMBER_DESC tag is set to YES (the default) Doxygen will -# include brief member descriptions after the members that are listed in -# the file and class documentation (similar to JavaDoc). -# Set to NO to disable this. - -BRIEF_MEMBER_DESC = YES - -# If the REPEAT_BRIEF tag is set to YES (the default) Doxygen will prepend -# the brief description of a member or function before the detailed description. -# Note: if both HIDE_UNDOC_MEMBERS and BRIEF_MEMBER_DESC are set to NO, the -# brief descriptions will be completely suppressed. - -REPEAT_BRIEF = YES - -# This tag implements a quasi-intelligent brief description abbreviator -# that is used to form the text in various listings. Each string -# in this list, if found as the leading text of the brief description, will be -# stripped from the text and the result after processing the whole list, is -# used as the annotated text. Otherwise, the brief description is used as-is. -# If left blank, the following values are used ("$name" is automatically -# replaced with the name of the entity): "The $name class" "The $name widget" -# "The $name file" "is" "provides" "specifies" "contains" -# "represents" "a" "an" "the" - -ABBREVIATE_BRIEF = - -# If the ALWAYS_DETAILED_SEC and REPEAT_BRIEF tags are both set to YES then -# Doxygen will generate a detailed section even if there is only a brief -# description. - -ALWAYS_DETAILED_SEC = NO - -# If the INLINE_INHERITED_MEMB tag is set to YES, doxygen will show all -# inherited members of a class in the documentation of that class as if those -# members were ordinary class members. Constructors, destructors and assignment -# operators of the base classes will not be shown. - -INLINE_INHERITED_MEMB = NO - -# If the FULL_PATH_NAMES tag is set to YES then Doxygen will prepend the full -# path before files name in the file list and in the header files. If set -# to NO the shortest path that makes the file name unique will be used. - -FULL_PATH_NAMES = YES - -# If the FULL_PATH_NAMES tag is set to YES then the STRIP_FROM_PATH tag -# can be used to strip a user-defined part of the path. Stripping is -# only done if one of the specified strings matches the left-hand part of -# the path. The tag can be used to show relative paths in the file list. -# If left blank the directory from which doxygen is run is used as the -# path to strip. - -STRIP_FROM_PATH = - -# The STRIP_FROM_INC_PATH tag can be used to strip a user-defined part of -# the path mentioned in the documentation of a class, which tells -# the reader which header file to include in order to use a class. -# If left blank only the name of the header file containing the class -# definition is used. Otherwise one should specify the include paths that -# are normally passed to the compiler using the -I flag. - -STRIP_FROM_INC_PATH = - -# If the SHORT_NAMES tag is set to YES, doxygen will generate much shorter -# (but less readable) file names. This can be useful if your file system -# doesn't support long names like on DOS, Mac, or CD-ROM. - -SHORT_NAMES = NO - -# If the JAVADOC_AUTOBRIEF tag is set to YES then Doxygen -# will interpret the first line (until the first dot) of a JavaDoc-style -# comment as the brief description. If set to NO, the JavaDoc -# comments will behave just like regular Qt-style comments -# (thus requiring an explicit @brief command for a brief description.) - -JAVADOC_AUTOBRIEF = NO - -# If the QT_AUTOBRIEF tag is set to YES then Doxygen will -# interpret the first line (until the first dot) of a Qt-style -# comment as the brief description. If set to NO, the comments -# will behave just like regular Qt-style comments (thus requiring -# an explicit \brief command for a brief description.) - -QT_AUTOBRIEF = NO - -# The MULTILINE_CPP_IS_BRIEF tag can be set to YES to make Doxygen -# treat a multi-line C++ special comment block (i.e. a block of //! or /// -# comments) as a brief description. This used to be the default behaviour. -# The new default is to treat a multi-line C++ comment block as a detailed -# description. Set this tag to YES if you prefer the old behaviour instead. - -MULTILINE_CPP_IS_BRIEF = NO - -# If the INHERIT_DOCS tag is set to YES (the default) then an undocumented -# member inherits the documentation from any documented member that it -# re-implements. - -INHERIT_DOCS = YES - -# If the SEPARATE_MEMBER_PAGES tag is set to YES, then doxygen will produce -# a new page for each member. If set to NO, the documentation of a member will -# be part of the file/class/namespace that contains it. - -SEPARATE_MEMBER_PAGES = NO - -# The TAB_SIZE tag can be used to set the number of spaces in a tab. -# Doxygen uses this value to replace tabs by spaces in code fragments. - -TAB_SIZE = 8 - -# This tag can be used to specify a number of aliases that acts -# as commands in the documentation. An alias has the form "name=value". -# For example adding "sideeffect=\par Side Effects:\n" will allow you to -# put the command \sideeffect (or @sideeffect) in the documentation, which -# will result in a user-defined paragraph with heading "Side Effects:". -# You can put \n's in the value part of an alias to insert newlines. - -ALIASES = - -# This tag can be used to specify a number of word-keyword mappings (TCL only). -# A mapping has the form "name=value". For example adding -# "class=itcl::class" will allow you to use the command class in the -# itcl::class meaning. - -TCL_SUBST = - -# Set the OPTIMIZE_OUTPUT_FOR_C tag to YES if your project consists of C -# sources only. Doxygen will then generate output that is more tailored for C. -# For instance, some of the names that are used will be different. The list -# of all members will be omitted, etc. - -OPTIMIZE_OUTPUT_FOR_C = NO - -# Set the OPTIMIZE_OUTPUT_JAVA tag to YES if your project consists of Java -# sources only. Doxygen will then generate output that is more tailored for -# Java. For instance, namespaces will be presented as packages, qualified -# scopes will look different, etc. - -OPTIMIZE_OUTPUT_JAVA = NO - -# Set the OPTIMIZE_FOR_FORTRAN tag to YES if your project consists of Fortran -# sources only. Doxygen will then generate output that is more tailored for -# Fortran. - -OPTIMIZE_FOR_FORTRAN = NO - -# Set the OPTIMIZE_OUTPUT_VHDL tag to YES if your project consists of VHDL -# sources. Doxygen will then generate output that is tailored for -# VHDL. - -OPTIMIZE_OUTPUT_VHDL = NO - -# Doxygen selects the parser to use depending on the extension of the files it -# parses. With this tag you can assign which parser to use for a given extension. -# Doxygen has a built-in mapping, but you can override or extend it using this -# tag. The format is ext=language, where ext is a file extension, and language -# is one of the parsers supported by doxygen: IDL, Java, Javascript, CSharp, C, -# C++, D, PHP, Objective-C, Python, Fortran, VHDL, C, C++. For instance to make -# doxygen treat .inc files as Fortran files (default is PHP), and .f files as C -# (default is Fortran), use: inc=Fortran f=C. Note that for custom extensions -# you also need to set FILE_PATTERNS otherwise the files are not read by doxygen. - -EXTENSION_MAPPING = - -# If you use STL classes (i.e. std::string, std::vector, etc.) but do not want -# to include (a tag file for) the STL sources as input, then you should -# set this tag to YES in order to let doxygen match functions declarations and -# definitions whose arguments contain STL classes (e.g. func(std::string); v.s. -# func(std::string) {}). This also makes the inheritance and collaboration -# diagrams that involve STL classes more complete and accurate. - -BUILTIN_STL_SUPPORT = YES - -# If you use Microsoft's C++/CLI language, you should set this option to YES to -# enable parsing support. - -CPP_CLI_SUPPORT = NO - -# Set the SIP_SUPPORT tag to YES if your project consists of sip sources only. -# Doxygen will parse them like normal C++ but will assume all classes use public -# instead of private inheritance when no explicit protection keyword is present. - -SIP_SUPPORT = NO - -# For Microsoft's IDL there are propget and propput attributes to indicate getter -# and setter methods for a property. Setting this option to YES (the default) -# will make doxygen replace the get and set methods by a property in the -# documentation. This will only work if the methods are indeed getting or -# setting a simple type. If this is not the case, or you want to show the -# methods anyway, you should set this option to NO. - -IDL_PROPERTY_SUPPORT = YES - -# If member grouping is used in the documentation and the DISTRIBUTE_GROUP_DOC -# tag is set to YES, then doxygen will reuse the documentation of the first -# member in the group (if any) for the other members of the group. By default -# all members of a group must be documented explicitly. - -DISTRIBUTE_GROUP_DOC = NO - -# Set the SUBGROUPING tag to YES (the default) to allow class member groups of -# the same type (for instance a group of public functions) to be put as a -# subgroup of that type (e.g. under the Public Functions section). Set it to -# NO to prevent subgrouping. Alternatively, this can be done per class using -# the \nosubgrouping command. - -SUBGROUPING = YES - -# When the INLINE_GROUPED_CLASSES tag is set to YES, classes, structs and -# unions are shown inside the group in which they are included (e.g. using -# @ingroup) instead of on a separate page (for HTML and Man pages) or -# section (for LaTeX and RTF). - -INLINE_GROUPED_CLASSES = NO - -# When the INLINE_SIMPLE_STRUCTS tag is set to YES, structs, classes, and -# unions with only public data fields will be shown inline in the documentation -# of the scope in which they are defined (i.e. file, namespace, or group -# documentation), provided this scope is documented. If set to NO (the default), -# structs, classes, and unions are shown on a separate page (for HTML and Man -# pages) or section (for LaTeX and RTF). - -INLINE_SIMPLE_STRUCTS = NO - -# When TYPEDEF_HIDES_STRUCT is enabled, a typedef of a struct, union, or enum -# is documented as struct, union, or enum with the name of the typedef. So -# typedef struct TypeS {} TypeT, will appear in the documentation as a struct -# with name TypeT. When disabled the typedef will appear as a member of a file, -# namespace, or class. And the struct will be named TypeS. This can typically -# be useful for C code in case the coding convention dictates that all compound -# types are typedef'ed and only the typedef is referenced, never the tag name. - -TYPEDEF_HIDES_STRUCT = NO - -# The SYMBOL_CACHE_SIZE determines the size of the internal cache use to -# determine which symbols to keep in memory and which to flush to disk. -# When the cache is full, less often used symbols will be written to disk. -# For small to medium size projects (<1000 input files) the default value is -# probably good enough. For larger projects a too small cache size can cause -# doxygen to be busy swapping symbols to and from disk most of the time -# causing a significant performance penalty. -# If the system has enough physical memory increasing the cache will improve the -# performance by keeping more symbols in memory. Note that the value works on -# a logarithmic scale so increasing the size by one will roughly double the -# memory usage. The cache size is given by this formula: -# 2^(16+SYMBOL_CACHE_SIZE). The valid range is 0..9, the default is 0, -# corresponding to a cache size of 2^16 = 65536 symbols. - -SYMBOL_CACHE_SIZE = 0 - -# Similar to the SYMBOL_CACHE_SIZE the size of the symbol lookup cache can be -# set using LOOKUP_CACHE_SIZE. This cache is used to resolve symbols given -# their name and scope. Since this can be an expensive process and often the -# same symbol appear multiple times in the code, doxygen keeps a cache of -# pre-resolved symbols. If the cache is too small doxygen will become slower. -# If the cache is too large, memory is wasted. The cache size is given by this -# formula: 2^(16+LOOKUP_CACHE_SIZE). The valid range is 0..9, the default is 0, -# corresponding to a cache size of 2^16 = 65536 symbols. - -LOOKUP_CACHE_SIZE = 0 - -#--------------------------------------------------------------------------- -# Build related configuration options -#--------------------------------------------------------------------------- - -# If the EXTRACT_ALL tag is set to YES doxygen will assume all entities in -# documentation are documented, even if no documentation was available. -# Private class members and static file members will be hidden unless -# the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set to YES - -EXTRACT_ALL = YES - -# If the EXTRACT_PRIVATE tag is set to YES all private members of a class -# will be included in the documentation. - -EXTRACT_PRIVATE = NO - -# If the EXTRACT_STATIC tag is set to YES all static members of a file -# will be included in the documentation. - -EXTRACT_STATIC = YES - -# If the EXTRACT_LOCAL_CLASSES tag is set to YES classes (and structs) -# defined locally in source files will be included in the documentation. -# If set to NO only classes defined in header files are included. - -EXTRACT_LOCAL_CLASSES = YES - -# This flag is only useful for Objective-C code. When set to YES local -# methods, which are defined in the implementation section but not in -# the interface are included in the documentation. -# If set to NO (the default) only methods in the interface are included. - -EXTRACT_LOCAL_METHODS = NO - -# If this flag is set to YES, the members of anonymous namespaces will be -# extracted and appear in the documentation as a namespace called -# 'anonymous_namespace{file}', where file will be replaced with the base -# name of the file that contains the anonymous namespace. By default -# anonymous namespaces are hidden. - -EXTRACT_ANON_NSPACES = NO - -# If the HIDE_UNDOC_MEMBERS tag is set to YES, Doxygen will hide all -# undocumented members of documented classes, files or namespaces. -# If set to NO (the default) these members will be included in the -# various overviews, but no documentation section is generated. -# This option has no effect if EXTRACT_ALL is enabled. - -HIDE_UNDOC_MEMBERS = NO - -# If the HIDE_UNDOC_CLASSES tag is set to YES, Doxygen will hide all -# undocumented classes that are normally visible in the class hierarchy. -# If set to NO (the default) these classes will be included in the various -# overviews. This option has no effect if EXTRACT_ALL is enabled. - -HIDE_UNDOC_CLASSES = NO - -# If the HIDE_FRIEND_COMPOUNDS tag is set to YES, Doxygen will hide all -# friend (class|struct|union) declarations. -# If set to NO (the default) these declarations will be included in the -# documentation. - -HIDE_FRIEND_COMPOUNDS = NO - -# If the HIDE_IN_BODY_DOCS tag is set to YES, Doxygen will hide any -# documentation blocks found inside the body of a function. -# If set to NO (the default) these blocks will be appended to the -# function's detailed documentation block. - -HIDE_IN_BODY_DOCS = NO - -# The INTERNAL_DOCS tag determines if documentation -# that is typed after a \internal command is included. If the tag is set -# to NO (the default) then the documentation will be excluded. -# Set it to YES to include the internal documentation. - -INTERNAL_DOCS = NO - -# If the CASE_SENSE_NAMES tag is set to NO then Doxygen will only generate -# file names in lower-case letters. If set to YES upper-case letters are also -# allowed. This is useful if you have classes or files whose names only differ -# in case and if your file system supports case sensitive file names. Windows -# and Mac users are advised to set this option to NO. - -CASE_SENSE_NAMES = YES - -# If the HIDE_SCOPE_NAMES tag is set to NO (the default) then Doxygen -# will show members with their full class and namespace scopes in the -# documentation. If set to YES the scope will be hidden. - -HIDE_SCOPE_NAMES = NO - -# If the SHOW_INCLUDE_FILES tag is set to YES (the default) then Doxygen -# will put a list of the files that are included by a file in the documentation -# of that file. - -SHOW_INCLUDE_FILES = YES - -# If the FORCE_LOCAL_INCLUDES tag is set to YES then Doxygen -# will list include files with double quotes in the documentation -# rather than with sharp brackets. - -FORCE_LOCAL_INCLUDES = NO - -# If the INLINE_INFO tag is set to YES (the default) then a tag [inline] -# is inserted in the documentation for inline members. - -INLINE_INFO = YES - -# If the SORT_MEMBER_DOCS tag is set to YES (the default) then doxygen -# will sort the (detailed) documentation of file and class members -# alphabetically by member name. If set to NO the members will appear in -# declaration order. - -SORT_MEMBER_DOCS = YES - -# If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the -# brief documentation of file, namespace and class members alphabetically -# by member name. If set to NO (the default) the members will appear in -# declaration order. - -SORT_BRIEF_DOCS = NO - -# If the SORT_MEMBERS_CTORS_1ST tag is set to YES then doxygen -# will sort the (brief and detailed) documentation of class members so that -# constructors and destructors are listed first. If set to NO (the default) -# the constructors will appear in the respective orders defined by -# SORT_MEMBER_DOCS and SORT_BRIEF_DOCS. -# This tag will be ignored for brief docs if SORT_BRIEF_DOCS is set to NO -# and ignored for detailed docs if SORT_MEMBER_DOCS is set to NO. - -SORT_MEMBERS_CTORS_1ST = NO - -# If the SORT_GROUP_NAMES tag is set to YES then doxygen will sort the -# hierarchy of group names into alphabetical order. If set to NO (the default) -# the group names will appear in their defined order. - -SORT_GROUP_NAMES = NO - -# If the SORT_BY_SCOPE_NAME tag is set to YES, the class list will be -# sorted by fully-qualified names, including namespaces. If set to -# NO (the default), the class list will be sorted only by class name, -# not including the namespace part. -# Note: This option is not very useful if HIDE_SCOPE_NAMES is set to YES. -# Note: This option applies only to the class list, not to the -# alphabetical list. - -SORT_BY_SCOPE_NAME = NO - -# If the STRICT_PROTO_MATCHING option is enabled and doxygen fails to -# do proper type resolution of all parameters of a function it will reject a -# match between the prototype and the implementation of a member function even -# if there is only one candidate or it is obvious which candidate to choose -# by doing a simple string match. By disabling STRICT_PROTO_MATCHING doxygen -# will still accept a match between prototype and implementation in such cases. - -STRICT_PROTO_MATCHING = NO - -# The GENERATE_TODOLIST tag can be used to enable (YES) or -# disable (NO) the todo list. This list is created by putting \todo -# commands in the documentation. - -GENERATE_TODOLIST = YES - -# The GENERATE_TESTLIST tag can be used to enable (YES) or -# disable (NO) the test list. This list is created by putting \test -# commands in the documentation. - -GENERATE_TESTLIST = YES - -# The GENERATE_BUGLIST tag can be used to enable (YES) or -# disable (NO) the bug list. This list is created by putting \bug -# commands in the documentation. - -GENERATE_BUGLIST = YES - -# The GENERATE_DEPRECATEDLIST tag can be used to enable (YES) or -# disable (NO) the deprecated list. This list is created by putting -# \deprecated commands in the documentation. - -GENERATE_DEPRECATEDLIST= YES - -# The ENABLED_SECTIONS tag can be used to enable conditional -# documentation sections, marked by \if sectionname ... \endif. - -ENABLED_SECTIONS = - -# The MAX_INITIALIZER_LINES tag determines the maximum number of lines -# the initial value of a variable or macro consists of for it to appear in -# the documentation. If the initializer consists of more lines than specified -# here it will be hidden. Use a value of 0 to hide initializers completely. -# The appearance of the initializer of individual variables and macros in the -# documentation can be controlled using \showinitializer or \hideinitializer -# command in the documentation regardless of this setting. - -MAX_INITIALIZER_LINES = 30 - -# Set the SHOW_USED_FILES tag to NO to disable the list of files generated -# at the bottom of the documentation of classes and structs. If set to YES the -# list will mention the files that were used to generate the documentation. - -SHOW_USED_FILES = YES - -# If the sources in your project are distributed over multiple directories -# then setting the SHOW_DIRECTORIES tag to YES will show the directory hierarchy -# in the documentation. The default is NO. - -SHOW_DIRECTORIES = YES - -# Set the SHOW_FILES tag to NO to disable the generation of the Files page. -# This will remove the Files entry from the Quick Index and from the -# Folder Tree View (if specified). The default is YES. - -SHOW_FILES = YES - -# Set the SHOW_NAMESPACES tag to NO to disable the generation of the -# Namespaces page. -# This will remove the Namespaces entry from the Quick Index -# and from the Folder Tree View (if specified). The default is YES. - -SHOW_NAMESPACES = YES - -# The FILE_VERSION_FILTER tag can be used to specify a program or script that -# doxygen should invoke to get the current version for each file (typically from -# the version control system). Doxygen will invoke the program by executing (via -# popen()) the command <command> <input-file>, where <command> is the value of -# the FILE_VERSION_FILTER tag, and <input-file> is the name of an input file -# provided by doxygen. Whatever the program writes to standard output -# is used as the file version. See the manual for examples. - -FILE_VERSION_FILTER = - -# The LAYOUT_FILE tag can be used to specify a layout file which will be parsed -# by doxygen. The layout file controls the global structure of the generated -# output files in an output format independent way. The create the layout file -# that represents doxygen's defaults, run doxygen with the -l option. -# You can optionally specify a file name after the option, if omitted -# DoxygenLayout.xml will be used as the name of the layout file. - -LAYOUT_FILE = - -# The CITE_BIB_FILES tag can be used to specify one or more bib files -# containing the references data. This must be a list of .bib files. The -# .bib extension is automatically appended if omitted. Using this command -# requires the bibtex tool to be installed. See also -# http://en.wikipedia.org/wiki/BibTeX for more info. For LaTeX the style -# of the bibliography can be controlled using LATEX_BIB_STYLE. To use this -# feature you need bibtex and perl available in the search path. - -CITE_BIB_FILES = - -#--------------------------------------------------------------------------- -# configuration options related to warning and progress messages -#--------------------------------------------------------------------------- - -# The QUIET tag can be used to turn on/off the messages that are generated -# by doxygen. Possible values are YES and NO. If left blank NO is used. - -QUIET = NO - -# The WARNINGS tag can be used to turn on/off the warning messages that are -# generated by doxygen. Possible values are YES and NO. If left blank -# NO is used. - -WARNINGS = YES - -# If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate warnings -# for undocumented members. If EXTRACT_ALL is set to YES then this flag will -# automatically be disabled. - -WARN_IF_UNDOCUMENTED = YES - -# If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings for -# potential errors in the documentation, such as not documenting some -# parameters in a documented function, or documenting parameters that -# don't exist or using markup commands wrongly. - -WARN_IF_DOC_ERROR = YES - -# The WARN_NO_PARAMDOC option can be enabled to get warnings for -# functions that are documented, but have no documentation for their parameters -# or return value. If set to NO (the default) doxygen will only warn about -# wrong or incomplete parameter documentation, but not about the absence of -# documentation. - -WARN_NO_PARAMDOC = YES - -# The WARN_FORMAT tag determines the format of the warning messages that -# doxygen can produce. The string should contain the $file, $line, and $text -# tags, which will be replaced by the file and line number from which the -# warning originated and the warning text. Optionally the format may contain -# $version, which will be replaced by the version of the file (if it could -# be obtained via FILE_VERSION_FILTER) - -WARN_FORMAT = "$file:$line: $text" - -# The WARN_LOGFILE tag can be used to specify a file to which warning -# and error messages should be written. If left blank the output is written -# to stderr. - -WARN_LOGFILE = - -#--------------------------------------------------------------------------- -# configuration options related to the input files -#--------------------------------------------------------------------------- - -# The INPUT tag can be used to specify the files and/or directories that contain -# documented source files. You may enter file names like "myfile.cpp" or -# directories like "/usr/src/myproject". Separate the files or directories -# with spaces. - -INPUT = - -# This tag can be used to specify the character encoding of the source files -# that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is -# also the default input encoding. Doxygen uses libiconv (or the iconv built -# into libc) for the transcoding. See http://www.gnu.org/software/libiconv for -# the list of possible encodings. - -INPUT_ENCODING = UTF-8 - -# If the value of the INPUT tag contains directories, you can use the -# FILE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp -# and *.h) to filter out the source-files in the directories. If left -# blank the following patterns are tested: -# *.c *.cc *.cxx *.cpp *.c++ *.d *.java *.ii *.ixx *.ipp *.i++ *.inl *.h *.hh -# *.hxx *.hpp *.h++ *.idl *.odl *.cs *.php *.php3 *.inc *.m *.mm *.dox *.py -# *.f90 *.f *.for *.vhd *.vhdl - -FILE_PATTERNS = *.h *.cpp - -# The RECURSIVE tag can be used to turn specify whether or not subdirectories -# should be searched for input files as well. Possible values are YES and NO. -# If left blank NO is used. - -RECURSIVE = YES - -# The EXCLUDE tag can be used to specify files and/or directories that should be -# excluded from the INPUT source files. This way you can easily exclude a -# subdirectory from a directory tree whose root is specified with the INPUT tag. -# Note that relative paths are relative to the directory from which doxygen is -# run. - -EXCLUDE = bin/ CMakeFiles/ docs/ lib/ nbproject/ - -# The EXCLUDE_SYMLINKS tag can be used to select whether or not files or -# directories that are symbolic links (a Unix file system feature) are excluded -# from the input. - -EXCLUDE_SYMLINKS = NO - -# If the value of the INPUT tag contains directories, you can use the -# EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude -# certain files from those directories. Note that the wildcards are matched -# against the file with absolute path, so to exclude all test directories -# for example use the pattern */test/* - -EXCLUDE_PATTERNS = - -# The EXCLUDE_SYMBOLS tag can be used to specify one or more symbol names -# (namespaces, classes, functions, etc.) that should be excluded from the -# output. The symbol name can be a fully qualified name, a word, or if the -# wildcard * is used, a substring. Examples: ANamespace, AClass, -# AClass::ANamespace, ANamespace::*Test - -EXCLUDE_SYMBOLS = - -# The EXAMPLE_PATH tag can be used to specify one or more files or -# directories that contain example code fragments that are included (see -# the \include command). - -EXAMPLE_PATH = - -# If the value of the EXAMPLE_PATH tag contains directories, you can use the -# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp -# and *.h) to filter out the source-files in the directories. If left -# blank all files are included. - -EXAMPLE_PATTERNS = - -# If the EXAMPLE_RECURSIVE tag is set to YES then subdirectories will be -# searched for input files to be used with the \include or \dontinclude -# commands irrespective of the value of the RECURSIVE tag. -# Possible values are YES and NO. If left blank NO is used. - -EXAMPLE_RECURSIVE = NO - -# The IMAGE_PATH tag can be used to specify one or more files or -# directories that contain image that are included in the documentation (see -# the \image command). - -IMAGE_PATH = - -# The INPUT_FILTER tag can be used to specify a program that doxygen should -# invoke to filter for each input file. Doxygen will invoke the filter program -# by executing (via popen()) the command <filter> <input-file>, where <filter> -# is the value of the INPUT_FILTER tag, and <input-file> is the name of an -# input file. Doxygen will then use the output that the filter program writes -# to standard output. -# If FILTER_PATTERNS is specified, this tag will be -# ignored. - -INPUT_FILTER = - -# The FILTER_PATTERNS tag can be used to specify filters on a per file pattern -# basis. -# Doxygen will compare the file name with each pattern and apply the -# filter if there is a match. -# The filters are a list of the form: -# pattern=filter (like *.cpp=my_cpp_filter). See INPUT_FILTER for further -# info on how filters are used. If FILTER_PATTERNS is empty or if -# non of the patterns match the file name, INPUT_FILTER is applied. - -FILTER_PATTERNS = - -# If the FILTER_SOURCE_FILES tag is set to YES, the input filter (if set using -# INPUT_FILTER) will be used to filter the input files when producing source -# files to browse (i.e. when SOURCE_BROWSER is set to YES). - -FILTER_SOURCE_FILES = NO - -# The FILTER_SOURCE_PATTERNS tag can be used to specify source filters per file -# pattern. A pattern will override the setting for FILTER_PATTERN (if any) -# and it is also possible to disable source filtering for a specific pattern -# using *.ext= (so without naming a filter). This option only has effect when -# FILTER_SOURCE_FILES is enabled. - -FILTER_SOURCE_PATTERNS = - -#--------------------------------------------------------------------------- -# configuration options related to source browsing -#--------------------------------------------------------------------------- - -# If the SOURCE_BROWSER tag is set to YES then a list of source files will -# be generated. Documented entities will be cross-referenced with these sources. -# Note: To get rid of all source code in the generated output, make sure also -# VERBATIM_HEADERS is set to NO. - -SOURCE_BROWSER = NO - -# Setting the INLINE_SOURCES tag to YES will include the body -# of functions and classes directly in the documentation. - -INLINE_SOURCES = NO - -# Setting the STRIP_CODE_COMMENTS tag to YES (the default) will instruct -# doxygen to hide any special comment blocks from generated source code -# fragments. Normal C and C++ comments will always remain visible. - -STRIP_CODE_COMMENTS = YES - -# If the REFERENCED_BY_RELATION tag is set to YES -# then for each documented function all documented -# functions referencing it will be listed. - -REFERENCED_BY_RELATION = NO - -# If the REFERENCES_RELATION tag is set to YES -# then for each documented function all documented entities -# called/used by that function will be listed. - -REFERENCES_RELATION = NO - -# If the REFERENCES_LINK_SOURCE tag is set to YES (the default) -# and SOURCE_BROWSER tag is set to YES, then the hyperlinks from -# functions in REFERENCES_RELATION and REFERENCED_BY_RELATION lists will -# link to the source code. -# Otherwise they will link to the documentation. - -REFERENCES_LINK_SOURCE = YES - -# If the USE_HTAGS tag is set to YES then the references to source code -# will point to the HTML generated by the htags(1) tool instead of doxygen -# built-in source browser. The htags tool is part of GNU's global source -# tagging system (see http://www.gnu.org/software/global/global.html). You -# will need version 4.8.6 or higher. - -USE_HTAGS = NO - -# If the VERBATIM_HEADERS tag is set to YES (the default) then Doxygen -# will generate a verbatim copy of the header file for each class for -# which an include is specified. Set to NO to disable this. - -VERBATIM_HEADERS = YES - -#--------------------------------------------------------------------------- -# configuration options related to the alphabetical class index -#--------------------------------------------------------------------------- - -# If the ALPHABETICAL_INDEX tag is set to YES, an alphabetical index -# of all compounds will be generated. Enable this if the project -# contains a lot of classes, structs, unions or interfaces. - -ALPHABETICAL_INDEX = YES - -# If the alphabetical index is enabled (see ALPHABETICAL_INDEX) then -# the COLS_IN_ALPHA_INDEX tag can be used to specify the number of columns -# in which this list will be split (can be a number in the range [1..20]) - -COLS_IN_ALPHA_INDEX = 5 - -# In case all classes in a project start with a common prefix, all -# classes will be put under the same header in the alphabetical index. -# The IGNORE_PREFIX tag can be used to specify one or more prefixes that -# should be ignored while generating the index headers. - -IGNORE_PREFIX = - -#--------------------------------------------------------------------------- -# configuration options related to the HTML output -#--------------------------------------------------------------------------- - -# If the GENERATE_HTML tag is set to YES (the default) Doxygen will -# generate HTML output. - -GENERATE_HTML = YES - -# The HTML_OUTPUT tag is used to specify where the HTML docs will be put. -# If a relative path is entered the value of OUTPUT_DIRECTORY will be -# put in front of it. If left blank `html' will be used as the default path. - -HTML_OUTPUT = html - -# The HTML_FILE_EXTENSION tag can be used to specify the file extension for -# each generated HTML page (for example: .htm,.php,.asp). If it is left blank -# doxygen will generate files with .html extension. - -HTML_FILE_EXTENSION = .html - -# The HTML_HEADER tag can be used to specify a personal HTML header for -# each generated HTML page. If it is left blank doxygen will generate a -# standard header. Note that when using a custom header you are responsible -# for the proper inclusion of any scripts and style sheets that doxygen -# needs, which is dependent on the configuration options used. -# It is advised to generate a default header using "doxygen -w html -# header.html footer.html stylesheet.css YourConfigFile" and then modify -# that header. Note that the header is subject to change so you typically -# have to redo this when upgrading to a newer version of doxygen or when -# changing the value of configuration settings such as GENERATE_TREEVIEW! - -HTML_HEADER = - -# The HTML_FOOTER tag can be used to specify a personal HTML footer for -# each generated HTML page. If it is left blank doxygen will generate a -# standard footer. - -HTML_FOOTER = - -# The HTML_STYLESHEET tag can be used to specify a user-defined cascading -# style sheet that is used by each HTML page. It can be used to -# fine-tune the look of the HTML output. If the tag is left blank doxygen -# will generate a default style sheet. Note that doxygen will try to copy -# the style sheet file to the HTML output directory, so don't put your own -# style sheet in the HTML output directory as well, or it will be erased! - -HTML_STYLESHEET = - -# The HTML_EXTRA_FILES tag can be used to specify one or more extra images or -# other source files which should be copied to the HTML output directory. Note -# that these files will be copied to the base HTML output directory. Use the -# $relpath$ marker in the HTML_HEADER and/or HTML_FOOTER files to load these -# files. In the HTML_STYLESHEET file, use the file name only. Also note that -# the files will be copied as-is; there are no commands or markers available. - -HTML_EXTRA_FILES = - -# The HTML_COLORSTYLE_HUE tag controls the color of the HTML output. -# Doxygen will adjust the colors in the style sheet and background images -# according to this color. Hue is specified as an angle on a colorwheel, -# see http://en.wikipedia.org/wiki/Hue for more information. -# For instance the value 0 represents red, 60 is yellow, 120 is green, -# 180 is cyan, 240 is blue, 300 purple, and 360 is red again. -# The allowed range is 0 to 359. - -HTML_COLORSTYLE_HUE = 220 - -# The HTML_COLORSTYLE_SAT tag controls the purity (or saturation) of -# the colors in the HTML output. For a value of 0 the output will use -# grayscales only. A value of 255 will produce the most vivid colors. - -HTML_COLORSTYLE_SAT = 100 - -# The HTML_COLORSTYLE_GAMMA tag controls the gamma correction applied to -# the luminance component of the colors in the HTML output. Values below -# 100 gradually make the output lighter, whereas values above 100 make -# the output darker. The value divided by 100 is the actual gamma applied, -# so 80 represents a gamma of 0.8, The value 220 represents a gamma of 2.2, -# and 100 does not change the gamma. - -HTML_COLORSTYLE_GAMMA = 80 - -# If the HTML_TIMESTAMP tag is set to YES then the footer of each generated HTML -# page will contain the date and time when the page was generated. Setting -# this to NO can help when comparing the output of multiple runs. - -HTML_TIMESTAMP = YES - -# If the HTML_ALIGN_MEMBERS tag is set to YES, the members of classes, -# files or namespaces will be aligned in HTML using tables. If set to -# NO a bullet list will be used. - -HTML_ALIGN_MEMBERS = YES - -# If the HTML_DYNAMIC_SECTIONS tag is set to YES then the generated HTML -# documentation will contain sections that can be hidden and shown after the -# page has loaded. For this to work a browser that supports -# JavaScript and DHTML is required (for instance Mozilla 1.0+, Firefox -# Netscape 6.0+, Internet explorer 5.0+, Konqueror, or Safari). - -HTML_DYNAMIC_SECTIONS = NO - -# If the GENERATE_DOCSET tag is set to YES, additional index files -# will be generated that can be used as input for Apple's Xcode 3 -# integrated development environment, introduced with OSX 10.5 (Leopard). -# To create a documentation set, doxygen will generate a Makefile in the -# HTML output directory. Running make will produce the docset in that -# directory and running "make install" will install the docset in -# ~/Library/Developer/Shared/Documentation/DocSets so that Xcode will find -# it at startup. -# See http://developer.apple.com/tools/creatingdocsetswithdoxygen.html -# for more information. - -GENERATE_DOCSET = NO - -# When GENERATE_DOCSET tag is set to YES, this tag determines the name of the -# feed. A documentation feed provides an umbrella under which multiple -# documentation sets from a single provider (such as a company or product suite) -# can be grouped. - -DOCSET_FEEDNAME = "Doxygen generated docs" - -# When GENERATE_DOCSET tag is set to YES, this tag specifies a string that -# should uniquely identify the documentation set bundle. This should be a -# reverse domain-name style string, e.g. com.mycompany.MyDocSet. Doxygen -# will append .docset to the name. - -DOCSET_BUNDLE_ID = com.fuzzylite - -# When GENERATE_PUBLISHER_ID tag specifies a string that should uniquely identify -# the documentation publisher. This should be a reverse domain-name style -# string, e.g. com.mycompany.MyDocSet.documentation. - -DOCSET_PUBLISHER_ID = com.fuzzylite.documentation - -# The GENERATE_PUBLISHER_NAME tag identifies the documentation publisher. - -DOCSET_PUBLISHER_NAME = Juan Rada-Vilela - -# If the GENERATE_HTMLHELP tag is set to YES, additional index files -# will be generated that can be used as input for tools like the -# Microsoft HTML help workshop to generate a compiled HTML help file (.chm) -# of the generated HTML documentation. - -GENERATE_HTMLHELP = NO - -# If the GENERATE_HTMLHELP tag is set to YES, the CHM_FILE tag can -# be used to specify the file name of the resulting .chm file. You -# can add a path in front of the file if the result should not be -# written to the html output directory. - -CHM_FILE = - -# If the GENERATE_HTMLHELP tag is set to YES, the HHC_LOCATION tag can -# be used to specify the location (absolute path including file name) of -# the HTML help compiler (hhc.exe). If non-empty doxygen will try to run -# the HTML help compiler on the generated index.hhp. - -HHC_LOCATION = - -# If the GENERATE_HTMLHELP tag is set to YES, the GENERATE_CHI flag -# controls if a separate .chi index file is generated (YES) or that -# it should be included in the master .chm file (NO). - -GENERATE_CHI = NO - -# If the GENERATE_HTMLHELP tag is set to YES, the CHM_INDEX_ENCODING -# is used to encode HtmlHelp index (hhk), content (hhc) and project file -# content. - -CHM_INDEX_ENCODING = - -# If the GENERATE_HTMLHELP tag is set to YES, the BINARY_TOC flag -# controls whether a binary table of contents is generated (YES) or a -# normal table of contents (NO) in the .chm file. - -BINARY_TOC = NO - -# The TOC_EXPAND flag can be set to YES to add extra items for group members -# to the contents of the HTML help documentation and to the tree view. - -TOC_EXPAND = NO - -# If the GENERATE_QHP tag is set to YES and both QHP_NAMESPACE and -# QHP_VIRTUAL_FOLDER are set, an additional index file will be generated -# that can be used as input for Qt's qhelpgenerator to generate a -# Qt Compressed Help (.qch) of the generated HTML documentation. - -GENERATE_QHP = NO - -# If the QHG_LOCATION tag is specified, the QCH_FILE tag can -# be used to specify the file name of the resulting .qch file. -# The path specified is relative to the HTML output folder. - -QCH_FILE = - -# The QHP_NAMESPACE tag specifies the namespace to use when generating -# Qt Help Project output. For more information please see -# http://doc.trolltech.com/qthelpproject.html#namespace - -QHP_NAMESPACE = org.doxygen.Project - -# The QHP_VIRTUAL_FOLDER tag specifies the namespace to use when generating -# Qt Help Project output. For more information please see -# http://doc.trolltech.com/qthelpproject.html#virtual-folders - -QHP_VIRTUAL_FOLDER = doc - -# If QHP_CUST_FILTER_NAME is set, it specifies the name of a custom filter to -# add. For more information please see -# http://doc.trolltech.com/qthelpproject.html#custom-filters - -QHP_CUST_FILTER_NAME = - -# The QHP_CUST_FILT_ATTRS tag specifies the list of the attributes of the -# custom filter to add. For more information please see -# <a href="http://doc.trolltech.com/qthelpproject.html#custom-filters"> -# Qt Help Project / Custom Filters</a>. - -QHP_CUST_FILTER_ATTRS = - -# The QHP_SECT_FILTER_ATTRS tag specifies the list of the attributes this -# project's -# filter section matches. -# <a href="http://doc.trolltech.com/qthelpproject.html#filter-attributes"> -# Qt Help Project / Filter Attributes</a>. - -QHP_SECT_FILTER_ATTRS = - -# If the GENERATE_QHP tag is set to YES, the QHG_LOCATION tag can -# be used to specify the location of Qt's qhelpgenerator. -# If non-empty doxygen will try to run qhelpgenerator on the generated -# .qhp file. - -QHG_LOCATION = - -# If the GENERATE_ECLIPSEHELP tag is set to YES, additional index files -# will be generated, which together with the HTML files, form an Eclipse help -# plugin. To install this plugin and make it available under the help contents -# menu in Eclipse, the contents of the directory containing the HTML and XML -# files needs to be copied into the plugins directory of eclipse. The name of -# the directory within the plugins directory should be the same as -# the ECLIPSE_DOC_ID value. After copying Eclipse needs to be restarted before -# the help appears. - -GENERATE_ECLIPSEHELP = NO - -# A unique identifier for the eclipse help plugin. When installing the plugin -# the directory name containing the HTML and XML files should also have -# this name. - -ECLIPSE_DOC_ID = org.doxygen.Project - -# The DISABLE_INDEX tag can be used to turn on/off the condensed index (tabs) -# at top of each HTML page. The value NO (the default) enables the index and -# the value YES disables it. Since the tabs have the same information as the -# navigation tree you can set this option to NO if you already set -# GENERATE_TREEVIEW to YES. - -DISABLE_INDEX = NO - -# The GENERATE_TREEVIEW tag is used to specify whether a tree-like index -# structure should be generated to display hierarchical information. -# If the tag value is set to YES, a side panel will be generated -# containing a tree-like index structure (just like the one that -# is generated for HTML Help). For this to work a browser that supports -# JavaScript, DHTML, CSS and frames is required (i.e. any modern browser). -# Windows users are probably better off using the HTML help feature. -# Since the tree basically has the same information as the tab index you -# could consider to set DISABLE_INDEX to NO when enabling this option. - -GENERATE_TREEVIEW = YES - -# The ENUM_VALUES_PER_LINE tag can be used to set the number of enum values -# (range [0,1..20]) that doxygen will group on one line in the generated HTML -# documentation. Note that a value of 0 will completely suppress the enum -# values from appearing in the overview section. - -ENUM_VALUES_PER_LINE = 4 - -# By enabling USE_INLINE_TREES, doxygen will generate the Groups, Directories, -# and Class Hierarchy pages using a tree view instead of an ordered list. - -USE_INLINE_TREES = NO - -# If the treeview is enabled (see GENERATE_TREEVIEW) then this tag can be -# used to set the initial width (in pixels) of the frame in which the tree -# is shown. - -TREEVIEW_WIDTH = 250 - -# When the EXT_LINKS_IN_WINDOW option is set to YES doxygen will open -# links to external symbols imported via tag files in a separate window. - -EXT_LINKS_IN_WINDOW = NO - -# Use this tag to change the font size of Latex formulas included -# as images in the HTML documentation. The default is 10. Note that -# when you change the font size after a successful doxygen run you need -# to manually remove any form_*.png images from the HTML output directory -# to force them to be regenerated. - -FORMULA_FONTSIZE = 10 - -# Use the FORMULA_TRANPARENT tag to determine whether or not the images -# generated for formulas are transparent PNGs. Transparent PNGs are -# not supported properly for IE 6.0, but are supported on all modern browsers. -# Note that when changing this option you need to delete any form_*.png files -# in the HTML output before the changes have effect. - -FORMULA_TRANSPARENT = YES - -# Enable the USE_MATHJAX option to render LaTeX formulas using MathJax -# (see http://www.mathjax.org) which uses client side Javascript for the -# rendering instead of using prerendered bitmaps. Use this if you do not -# have LaTeX installed or if you want to formulas look prettier in the HTML -# output. When enabled you also need to install MathJax separately and -# configure the path to it using the MATHJAX_RELPATH option. - -USE_MATHJAX = NO - -# When MathJax is enabled you need to specify the location relative to the -# HTML output directory using the MATHJAX_RELPATH option. The destination -# directory should contain the MathJax.js script. For instance, if the mathjax -# directory is located at the same level as the HTML output directory, then -# MATHJAX_RELPATH should be ../mathjax. The default value points to the -# mathjax.org site, so you can quickly see the result without installing -# MathJax, but it is strongly recommended to install a local copy of MathJax -# before deployment. - -MATHJAX_RELPATH = http://www.mathjax.org/mathjax - -# The MATHJAX_EXTENSIONS tag can be used to specify one or MathJax extension -# names that should be enabled during MathJax rendering. - -MATHJAX_EXTENSIONS = - -# When the SEARCHENGINE tag is enabled doxygen will generate a search box -# for the HTML output. The underlying search engine uses javascript -# and DHTML and should work on any modern browser. Note that when using -# HTML help (GENERATE_HTMLHELP), Qt help (GENERATE_QHP), or docsets -# (GENERATE_DOCSET) there is already a search function so this one should -# typically be disabled. For large projects the javascript based search engine -# can be slow, then enabling SERVER_BASED_SEARCH may provide a better solution. - -SEARCHENGINE = YES - -# When the SERVER_BASED_SEARCH tag is enabled the search engine will be -# implemented using a PHP enabled web server instead of at the web client -# using Javascript. Doxygen will generate the search PHP script and index -# file to put on the web server. The advantage of the server -# based approach is that it scales better to large projects and allows -# full text search. The disadvantages are that it is more difficult to setup -# and does not have live searching capabilities. - -SERVER_BASED_SEARCH = NO - -#--------------------------------------------------------------------------- -# configuration options related to the LaTeX output -#--------------------------------------------------------------------------- - -# If the GENERATE_LATEX tag is set to YES (the default) Doxygen will -# generate Latex output. - -GENERATE_LATEX = NO - -# The LATEX_OUTPUT tag is used to specify where the LaTeX docs will be put. -# If a relative path is entered the value of OUTPUT_DIRECTORY will be -# put in front of it. If left blank `latex' will be used as the default path. - -LATEX_OUTPUT = latex - -# The LATEX_CMD_NAME tag can be used to specify the LaTeX command name to be -# invoked. If left blank `latex' will be used as the default command name. -# Note that when enabling USE_PDFLATEX this option is only used for -# generating bitmaps for formulas in the HTML output, but not in the -# Makefile that is written to the output directory. - -LATEX_CMD_NAME = latex - -# The MAKEINDEX_CMD_NAME tag can be used to specify the command name to -# generate index for LaTeX. If left blank `makeindex' will be used as the -# default command name. - -MAKEINDEX_CMD_NAME = makeindex - -# If the COMPACT_LATEX tag is set to YES Doxygen generates more compact -# LaTeX documents. This may be useful for small projects and may help to -# save some trees in general. - -COMPACT_LATEX = NO - -# The PAPER_TYPE tag can be used to set the paper type that is used -# by the printer. Possible values are: a4, letter, legal and -# executive. If left blank a4wide will be used. - -PAPER_TYPE = a4 - -# The EXTRA_PACKAGES tag can be to specify one or more names of LaTeX -# packages that should be included in the LaTeX output. - -EXTRA_PACKAGES = - -# The LATEX_HEADER tag can be used to specify a personal LaTeX header for -# the generated latex document. The header should contain everything until -# the first chapter. If it is left blank doxygen will generate a -# standard header. Notice: only use this tag if you know what you are doing! - -LATEX_HEADER = - -# The LATEX_FOOTER tag can be used to specify a personal LaTeX footer for -# the generated latex document. The footer should contain everything after -# the last chapter. If it is left blank doxygen will generate a -# standard footer. Notice: only use this tag if you know what you are doing! - -LATEX_FOOTER = - -# If the PDF_HYPERLINKS tag is set to YES, the LaTeX that is generated -# is prepared for conversion to pdf (using ps2pdf). The pdf file will -# contain links (just like the HTML output) instead of page references -# This makes the output suitable for online browsing using a pdf viewer. - -PDF_HYPERLINKS = YES - -# If the USE_PDFLATEX tag is set to YES, pdflatex will be used instead of -# plain latex in the generated Makefile. Set this option to YES to get a -# higher quality PDF documentation. - -USE_PDFLATEX = YES - -# If the LATEX_BATCHMODE tag is set to YES, doxygen will add the \\batchmode. -# command to the generated LaTeX files. This will instruct LaTeX to keep -# running if errors occur, instead of asking the user for help. -# This option is also used when generating formulas in HTML. - -LATEX_BATCHMODE = NO - -# If LATEX_HIDE_INDICES is set to YES then doxygen will not -# include the index chapters (such as File Index, Compound Index, etc.) -# in the output. - -LATEX_HIDE_INDICES = NO - -# If LATEX_SOURCE_CODE is set to YES then doxygen will include -# source code with syntax highlighting in the LaTeX output. -# Note that which sources are shown also depends on other settings -# such as SOURCE_BROWSER. - -LATEX_SOURCE_CODE = NO - -# The LATEX_BIB_STYLE tag can be used to specify the style to use for the -# bibliography, e.g. plainnat, or ieeetr. The default style is "plain". See -# http://en.wikipedia.org/wiki/BibTeX for more info. - -LATEX_BIB_STYLE = plain - -#--------------------------------------------------------------------------- -# configuration options related to the RTF output -#--------------------------------------------------------------------------- - -# If the GENERATE_RTF tag is set to YES Doxygen will generate RTF output -# The RTF output is optimized for Word 97 and may not look very pretty with -# other RTF readers or editors. - -GENERATE_RTF = NO - -# The RTF_OUTPUT tag is used to specify where the RTF docs will be put. -# If a relative path is entered the value of OUTPUT_DIRECTORY will be -# put in front of it. If left blank `rtf' will be used as the default path. - -RTF_OUTPUT = rtf - -# If the COMPACT_RTF tag is set to YES Doxygen generates more compact -# RTF documents. This may be useful for small projects and may help to -# save some trees in general. - -COMPACT_RTF = NO - -# If the RTF_HYPERLINKS tag is set to YES, the RTF that is generated -# will contain hyperlink fields. The RTF file will -# contain links (just like the HTML output) instead of page references. -# This makes the output suitable for online browsing using WORD or other -# programs which support those fields. -# Note: wordpad (write) and others do not support links. - -RTF_HYPERLINKS = NO - -# Load style sheet definitions from file. Syntax is similar to doxygen's -# config file, i.e. a series of assignments. You only have to provide -# replacements, missing definitions are set to their default value. - -RTF_STYLESHEET_FILE = - -# Set optional variables used in the generation of an rtf document. -# Syntax is similar to doxygen's config file. - -RTF_EXTENSIONS_FILE = - -#--------------------------------------------------------------------------- -# configuration options related to the man page output -#--------------------------------------------------------------------------- - -# If the GENERATE_MAN tag is set to YES (the default) Doxygen will -# generate man pages - -GENERATE_MAN = NO - -# The MAN_OUTPUT tag is used to specify where the man pages will be put. -# If a relative path is entered the value of OUTPUT_DIRECTORY will be -# put in front of it. If left blank `man' will be used as the default path. - -MAN_OUTPUT = man - -# The MAN_EXTENSION tag determines the extension that is added to -# the generated man pages (default is the subroutine's section .3) - -MAN_EXTENSION = .3 - -# If the MAN_LINKS tag is set to YES and Doxygen generates man output, -# then it will generate one additional man file for each entity -# documented in the real man page(s). These additional files -# only source the real man page, but without them the man command -# would be unable to find the correct page. The default is NO. - -MAN_LINKS = NO - -#--------------------------------------------------------------------------- -# configuration options related to the XML output -#--------------------------------------------------------------------------- - -# If the GENERATE_XML tag is set to YES Doxygen will -# generate an XML file that captures the structure of -# the code including all documentation. - -GENERATE_XML = NO - -# The XML_OUTPUT tag is used to specify where the XML pages will be put. -# If a relative path is entered the value of OUTPUT_DIRECTORY will be -# put in front of it. If left blank `xml' will be used as the default path. - -XML_OUTPUT = xml - -# The XML_SCHEMA tag can be used to specify an XML schema, -# which can be used by a validating XML parser to check the -# syntax of the XML files. - -XML_SCHEMA = - -# The XML_DTD tag can be used to specify an XML DTD, -# which can be used by a validating XML parser to check the -# syntax of the XML files. - -XML_DTD = - -# If the XML_PROGRAMLISTING tag is set to YES Doxygen will -# dump the program listings (including syntax highlighting -# and cross-referencing information) to the XML output. Note that -# enabling this will significantly increase the size of the XML output. - -XML_PROGRAMLISTING = YES - -#--------------------------------------------------------------------------- -# configuration options for the AutoGen Definitions output -#--------------------------------------------------------------------------- - -# If the GENERATE_AUTOGEN_DEF tag is set to YES Doxygen will -# generate an AutoGen Definitions (see autogen.sf.net) file -# that captures the structure of the code including all -# documentation. Note that this feature is still experimental -# and incomplete at the moment. - -GENERATE_AUTOGEN_DEF = NO - -#--------------------------------------------------------------------------- -# configuration options related to the Perl module output -#--------------------------------------------------------------------------- - -# If the GENERATE_PERLMOD tag is set to YES Doxygen will -# generate a Perl module file that captures the structure of -# the code including all documentation. Note that this -# feature is still experimental and incomplete at the -# moment. - -GENERATE_PERLMOD = NO - -# If the PERLMOD_LATEX tag is set to YES Doxygen will generate -# the necessary Makefile rules, Perl scripts and LaTeX code to be able -# to generate PDF and DVI output from the Perl module output. - -PERLMOD_LATEX = NO - -# If the PERLMOD_PRETTY tag is set to YES the Perl module output will be -# nicely formatted so it can be parsed by a human reader. -# This is useful -# if you want to understand what is going on. -# On the other hand, if this -# tag is set to NO the size of the Perl module output will be much smaller -# and Perl will parse it just the same. - -PERLMOD_PRETTY = YES - -# The names of the make variables in the generated doxyrules.make file -# are prefixed with the string contained in PERLMOD_MAKEVAR_PREFIX. -# This is useful so different doxyrules.make files included by the same -# Makefile don't overwrite each other's variables. - -PERLMOD_MAKEVAR_PREFIX = - -#--------------------------------------------------------------------------- -# Configuration options related to the preprocessor -#--------------------------------------------------------------------------- - -# If the ENABLE_PREPROCESSING tag is set to YES (the default) Doxygen will -# evaluate all C-preprocessor directives found in the sources and include -# files. - -ENABLE_PREPROCESSING = YES - -# If the MACRO_EXPANSION tag is set to YES Doxygen will expand all macro -# names in the source code. If set to NO (the default) only conditional -# compilation will be performed. Macro expansion can be done in a controlled -# way by setting EXPAND_ONLY_PREDEF to YES. - -MACRO_EXPANSION = NO - -# If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES -# then the macro expansion is limited to the macros specified with the -# PREDEFINED and EXPAND_AS_DEFINED tags. - -EXPAND_ONLY_PREDEF = NO - -# If the SEARCH_INCLUDES tag is set to YES (the default) the includes files -# pointed to by INCLUDE_PATH will be searched when a #include is found. - -SEARCH_INCLUDES = YES - -# The INCLUDE_PATH tag can be used to specify one or more directories that -# contain include files that are not input files but should be processed by -# the preprocessor. - -INCLUDE_PATH = - -# You can use the INCLUDE_FILE_PATTERNS tag to specify one or more wildcard -# patterns (like *.h and *.hpp) to filter out the header-files in the -# directories. If left blank, the patterns specified with FILE_PATTERNS will -# be used. - -INCLUDE_FILE_PATTERNS = - -# The PREDEFINED tag can be used to specify one or more macro names that -# are defined before the preprocessor is started (similar to the -D option of -# gcc). The argument of the tag is a list of macros of the form: name -# or name=definition (no spaces). If the definition and the = are -# omitted =1 is assumed. To prevent a macro definition from being -# undefined via #undef or recursively expanded use the := operator -# instead of the = operator. - -PREDEFINED = - -# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then -# this tag can be used to specify a list of macro names that should be expanded. -# The macro definition that is found in the sources will be used. -# Use the PREDEFINED tag if you want to use a different macro definition that -# overrules the definition found in the source code. - -EXPAND_AS_DEFINED = - -# If the SKIP_FUNCTION_MACROS tag is set to YES (the default) then -# doxygen's preprocessor will remove all references to function-like macros -# that are alone on a line, have an all uppercase name, and do not end with a -# semicolon, because these will confuse the parser if not removed. - -SKIP_FUNCTION_MACROS = YES - -#--------------------------------------------------------------------------- -# Configuration::additions related to external references -#--------------------------------------------------------------------------- - -# The TAGFILES option can be used to specify one or more tagfiles. -# Optionally an initial location of the external documentation -# can be added for each tagfile. The format of a tag file without -# this location is as follows: -# -# TAGFILES = file1 file2 ... -# Adding location for the tag files is done as follows: -# -# TAGFILES = file1=loc1 "file2 = loc2" ... -# where "loc1" and "loc2" can be relative or absolute paths or -# URLs. If a location is present for each tag, the installdox tool -# does not have to be run to correct the links. -# Note that each tag file must have a unique name -# (where the name does NOT include the path) -# If a tag file is not located in the directory in which doxygen -# is run, you must also specify the path to the tagfile here. - -TAGFILES = - -# When a file name is specified after GENERATE_TAGFILE, doxygen will create -# a tag file that is based on the input files it reads. - -GENERATE_TAGFILE = - -# If the ALLEXTERNALS tag is set to YES all external classes will be listed -# in the class index. If set to NO only the inherited external classes -# will be listed. - -ALLEXTERNALS = NO - -# If the EXTERNAL_GROUPS tag is set to YES all external groups will be listed -# in the modules index. If set to NO, only the current project's groups will -# be listed. - -EXTERNAL_GROUPS = YES - -# The PERL_PATH should be the absolute path and name of the perl script -# interpreter (i.e. the result of `which perl'). - -PERL_PATH = /usr/bin/perl - -#--------------------------------------------------------------------------- -# Configuration options related to the dot tool -#--------------------------------------------------------------------------- - -# If the CLASS_DIAGRAMS tag is set to YES (the default) Doxygen will -# generate a inheritance diagram (in HTML, RTF and LaTeX) for classes with base -# or super classes. Setting the tag to NO turns the diagrams off. Note that -# this option also works with HAVE_DOT disabled, but it is recommended to -# install and use dot, since it yields more powerful graphs. - -CLASS_DIAGRAMS = YES - -# You can define message sequence charts within doxygen comments using the \msc -# command. Doxygen will then run the mscgen tool (see -# http://www.mcternan.me.uk/mscgen/) to produce the chart and insert it in the -# documentation. The MSCGEN_PATH tag allows you to specify the directory where -# the mscgen tool resides. If left empty the tool is assumed to be found in the -# default search path. - -MSCGEN_PATH = - -# If set to YES, the inheritance and collaboration graphs will hide -# inheritance and usage relations if the target is undocumented -# or is not a class. - -HIDE_UNDOC_RELATIONS = YES - -# If you set the HAVE_DOT tag to YES then doxygen will assume the dot tool is -# available from the path. This tool is part of Graphviz, a graph visualization -# toolkit from AT&T and Lucent Bell Labs. The other options in this section -# have no effect if this option is set to NO (the default) - -HAVE_DOT = YES - -# The DOT_NUM_THREADS specifies the number of dot invocations doxygen is -# allowed to run in parallel. When set to 0 (the default) doxygen will -# base this on the number of processors available in the system. You can set it -# explicitly to a value larger than 0 to get control over the balance -# between CPU load and processing speed. - -DOT_NUM_THREADS = 0 - -# By default doxygen will use the Helvetica font for all dot files that -# doxygen generates. When you want a differently looking font you can specify -# the font name using DOT_FONTNAME. You need to make sure dot is able to find -# the font, which can be done by putting it in a standard location or by setting -# the DOTFONTPATH environment variable or by setting DOT_FONTPATH to the -# directory containing the font. - -DOT_FONTNAME = Helvetica - -# The DOT_FONTSIZE tag can be used to set the size of the font of dot graphs. -# The default size is 10pt. - -DOT_FONTSIZE = 10 - -# By default doxygen will tell dot to use the Helvetica font. -# If you specify a different font using DOT_FONTNAME you can use DOT_FONTPATH to -# set the path where dot can find it. - -DOT_FONTPATH = - -# If the CLASS_GRAPH and HAVE_DOT tags are set to YES then doxygen -# will generate a graph for each documented class showing the direct and -# indirect inheritance relations. Setting this tag to YES will force the -# CLASS_DIAGRAMS tag to NO. - -CLASS_GRAPH = YES - -# If the COLLABORATION_GRAPH and HAVE_DOT tags are set to YES then doxygen -# will generate a graph for each documented class showing the direct and -# indirect implementation dependencies (inheritance, containment, and -# class references variables) of the class with other documented classes. - -COLLABORATION_GRAPH = YES - -# If the GROUP_GRAPHS and HAVE_DOT tags are set to YES then doxygen -# will generate a graph for groups, showing the direct groups dependencies - -GROUP_GRAPHS = YES - -# If the UML_LOOK tag is set to YES doxygen will generate inheritance and -# collaboration diagrams in a style similar to the OMG's Unified Modeling -# Language. - -UML_LOOK = YES - -# If set to YES, the inheritance and collaboration graphs will show the -# relations between templates and their instances. - -TEMPLATE_RELATIONS = NO - -# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDE_GRAPH, and HAVE_DOT -# tags are set to YES then doxygen will generate a graph for each documented -# file showing the direct and indirect include dependencies of the file with -# other documented files. - -INCLUDE_GRAPH = YES - -# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDED_BY_GRAPH, and -# HAVE_DOT tags are set to YES then doxygen will generate a graph for each -# documented header file showing the documented files that directly or -# indirectly include this file. - -INCLUDED_BY_GRAPH = YES - -# If the CALL_GRAPH and HAVE_DOT options are set to YES then -# doxygen will generate a call dependency graph for every global function -# or class method. Note that enabling this option will significantly increase -# the time of a run. So in most cases it will be better to enable call graphs -# for selected functions only using the \callgraph command. - -CALL_GRAPH = NO - -# If the CALLER_GRAPH and HAVE_DOT tags are set to YES then -# doxygen will generate a caller dependency graph for every global function -# or class method. Note that enabling this option will significantly increase -# the time of a run. So in most cases it will be better to enable caller -# graphs for selected functions only using the \callergraph command. - -CALLER_GRAPH = NO - -# If the GRAPHICAL_HIERARCHY and HAVE_DOT tags are set to YES then doxygen -# will generate a graphical hierarchy of all classes instead of a textual one. - -GRAPHICAL_HIERARCHY = YES - -# If the DIRECTORY_GRAPH, SHOW_DIRECTORIES and HAVE_DOT tags are set to YES -# then doxygen will show the dependencies a directory has on other directories -# in a graphical way. The dependency relations are determined by the #include -# relations between the files in the directories. - -DIRECTORY_GRAPH = YES - -# The DOT_IMAGE_FORMAT tag can be used to set the image format of the images -# generated by dot. Possible values are svg, png, jpg, or gif. -# If left blank png will be used. If you choose svg you need to set -# HTML_FILE_EXTENSION to xhtml in order to make the SVG files -# visible in IE 9+ (other browsers do not have this requirement). - -DOT_IMAGE_FORMAT = png - -# If DOT_IMAGE_FORMAT is set to svg, then this option can be set to YES to -# enable generation of interactive SVG images that allow zooming and panning. -# Note that this requires a modern browser other than Internet Explorer. -# Tested and working are Firefox, Chrome, Safari, and Opera. For IE 9+ you -# need to set HTML_FILE_EXTENSION to xhtml in order to make the SVG files -# visible. Older versions of IE do not have SVG support. - -INTERACTIVE_SVG = YES - -# The tag DOT_PATH can be used to specify the path where the dot tool can be -# found. If left blank, it is assumed the dot tool can be found in the path. - -DOT_PATH = - -# The DOTFILE_DIRS tag can be used to specify one or more directories that -# contain dot files that are included in the documentation (see the -# \dotfile command). - -DOTFILE_DIRS = - -# The MSCFILE_DIRS tag can be used to specify one or more directories that -# contain msc files that are included in the documentation (see the -# \mscfile command). - -MSCFILE_DIRS = - -# The DOT_GRAPH_MAX_NODES tag can be used to set the maximum number of -# nodes that will be shown in the graph. If the number of nodes in a graph -# becomes larger than this value, doxygen will truncate the graph, which is -# visualized by representing a node as a red box. Note that doxygen if the -# number of direct children of the root node in a graph is already larger than -# DOT_GRAPH_MAX_NODES then the graph will not be shown at all. Also note -# that the size of a graph can be further restricted by MAX_DOT_GRAPH_DEPTH. - -DOT_GRAPH_MAX_NODES = 50 - -# The MAX_DOT_GRAPH_DEPTH tag can be used to set the maximum depth of the -# graphs generated by dot. A depth value of 3 means that only nodes reachable -# from the root by following a path via at most 3 edges will be shown. Nodes -# that lay further from the root node will be omitted. Note that setting this -# option to 1 or 2 may greatly reduce the computation time needed for large -# code bases. Also note that the size of a graph can be further restricted by -# DOT_GRAPH_MAX_NODES. Using a depth of 0 means no depth restriction. - -MAX_DOT_GRAPH_DEPTH = 0 - -# Set the DOT_TRANSPARENT tag to YES to generate images with a transparent -# background. This is disabled by default, because dot on Windows does not -# seem to support this out of the box. Warning: Depending on the platform used, -# enabling this option may lead to badly anti-aliased labels on the edges of -# a graph (i.e. they become hard to read). - -DOT_TRANSPARENT = NO - -# Set the DOT_MULTI_TARGETS tag to YES allow dot to generate multiple output -# files in one run (i.e. multiple -o and -T options on the command line). This -# makes dot run faster, but since only newer versions of dot (>1.8.10) -# support this, this feature is disabled by default. - -DOT_MULTI_TARGETS = YES - -# If the GENERATE_LEGEND tag is set to YES (the default) Doxygen will -# generate a legend page explaining the meaning of the various boxes and -# arrows in the dot generated graphs. - -GENERATE_LEGEND = YES - -# If the DOT_CLEANUP tag is set to YES (the default) Doxygen will -# remove the intermediate dot files that are used to generate -# the various graphs. - -DOT_CLEANUP = YES diff --git a/fuzzylite/fuzzylite.pc.in b/fuzzylite/fuzzylite.pc.in index 421c61a..f6f7bee 100644 --- a/fuzzylite/fuzzylite.pc.in +++ b/fuzzylite/fuzzylite.pc.in @@ -1,10 +1,10 @@ prefix=@CMAKE_INSTALL_PREFIX@ exec_prefix=${prefix} includedir=${exec_prefix}/include -libdir=@CMAKE_INSTALL_LIBDIR@ +libdir=${exec_prefix}/lib Name: fuzzylite Description: A fuzzy logic control library in C++ Version: @FL_VERSION@ Libs: -L${libdir} -lfuzzylite -Cflags: -I${includedir}/fl +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 index 3a55fc5..5a00c50 100644 --- a/fuzzylite/src/Console.cpp +++ b/fuzzylite/src/Console.cpp @@ -1,37 +1,24 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 <algorithm> -#include <cctype> #include <fstream> -#include <stdlib.h> -#include <utility> -#include <vector> #ifdef FL_UNIX #include <termios.h> @@ -40,9 +27,6 @@ #include <conio.h> #endif -#ifdef FL_CPP11 -#include <chrono> -#endif namespace fl { const std::string Console::KW_INPUT_FILE = "-i"; @@ -51,11 +35,16 @@ namespace fl { 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 = "-d"; - const std::string Console::KW_DATA_MAXIMUM = "-dmaximum"; + 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")); @@ -64,8 +53,9 @@ namespace fl { 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, "datafile", "if exporting to fld, file of input values to evaluate your engine on")); - options.push_back(Option(KW_DATA_MAXIMUM, "number", "if exporting to fld without datafile, maximum number of results to export")); + 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; @@ -74,14 +64,16 @@ namespace fl { 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::longVersion() << "\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 << "] "; @@ -103,15 +95,15 @@ namespace fl { ss << "\n"; ss << "Visit " << fuzzylite::website() << " for more information.\n\n"; - ss << "Copyright (C) 2010-2015 FuzzyLite Limited.\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, char** argv) { + std::map<std::string, std::string> Console::parse(int argc, const char* argv[]) { if ((argc - 1) % 2 != 0) { - throw fl::Exception("[option error] incomplete number of parameters [key value]", FL_AT); + 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) { @@ -139,7 +131,7 @@ namespace fl { } } if (not isValid) { - throw fl::Exception("[option error] option <" + it->first + "> not recognized", FL_AT); + throw Exception("[option error] option <" + it->first + "> not recognized", FL_AT); } } } @@ -151,7 +143,7 @@ namespace fl { it = options.find(KW_DECIMALS); if (it != options.end()) { - fl::fuzzylite::setDecimals((int) fl::Op::toScalar(it->second)); + fuzzylite::setDecimals((int) Op::toScalar(it->second)); } std::string example; @@ -170,7 +162,7 @@ namespace fl { } else if (example == "t" or example == "ts" or example == "takagi-sugeno") { engine = takagiSugeno(); } else { - throw fl::Exception("[option error] example <" + example + "> not available", FL_AT); + throw Exception("[option error] example <" + example + "> not available", FL_AT); } inputFormat = "fll"; textEngine << FllExporter().toString(engine); @@ -179,12 +171,12 @@ namespace fl { } else { it = options.find(KW_INPUT_FILE); if (it == options.end()) { - throw fl::Exception("[option error] no input file specified", FL_AT); + 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 fl::Exception("[file error] file <" + inputFilename + "> could not be opened", FL_AT); + throw Exception("[file error] file <" + inputFilename + "> could not be opened", FL_AT); } std::string line; while (std::getline(inputFile, line)) { @@ -200,7 +192,7 @@ namespace fl { if (extensionIndex != std::string::npos) { inputFormat = inputFilename.substr(extensionIndex + 1); } else { - throw fl::Exception("[format error] unspecified format of input file", FL_AT); + throw Exception("[format error] unspecified format of input file", FL_AT); } } } @@ -220,7 +212,7 @@ namespace fl { if (extensionIndex != std::string::npos) { outputFormat = outputFilename.substr(extensionIndex + 1); } else { - throw fl::Exception("[format error] unspecified format of output file", FL_AT); + throw Exception("[format error] unspecified format of output file", FL_AT); } } @@ -230,7 +222,7 @@ namespace fl { } else { std::ofstream writer(outputFilename.c_str()); if (not writer.is_open()) { - throw fl::Exception("[file error] file <" + outputFilename + "> could not be created", FL_AT); + throw Exception("[file error] file <" + outputFilename + "> could not be created", FL_AT); } process(textEngine.str(), writer, inputFormat, outputFormat, options); writer.flush(); @@ -252,7 +244,7 @@ namespace fl { } else if ("fis" == inputFormat) { importer.reset(new FisImporter); } else { - throw fl::Exception("[import error] format <" + inputFormat + "> " + throw Exception("[import error] format <" + inputFormat + "> " "not supported", FL_AT); } @@ -262,7 +254,7 @@ namespace fl { std::map<std::string, std::string>::const_iterator it; FldExporter fldExporter; - fldExporter.setSeparator("\t"); + fldExporter.setSeparator(" "); bool exportHeaders = true; if ((it = options.find(KW_DATA_EXPORT_HEADER)) != options.end()) { exportHeaders = ("true" == it->second); @@ -273,22 +265,32 @@ namespace fl { exportInputValues = ("true" == it->second); } fldExporter.setExportInputValues(exportInputValues); - if ((it = options.find(KW_DATA_INPUT)) != options.end()) { + if ((it = options.find(KW_DATA_INPUT_FILE)) != options.end()) { std::ifstream dataFile(it->second.c_str()); if (not dataFile.is_open()) { - throw fl::Exception("[export error] file <" + it->second + "> could not be opened", FL_AT); + throw Exception("[export error] file <" + it->second + "> could not be opened", FL_AT); } try { fldExporter.write(engine.get(), writer, dataFile); } catch (std::exception& ex) { - (void) ex; + FL_IUNUSED(ex); dataFile.close(); throw; } } else { - if ((it = options.find(KW_DATA_MAXIMUM)) != options.end()) { - fldExporter.write(engine.get(), writer, (int) fl::Op::toScalar(it->second)); + 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"; @@ -311,7 +313,7 @@ namespace fl { exporter.reset(new CppExporter); } else if ("java" == outputFormat) { exporter.reset(new JavaExporter); - } else throw fl::Exception("[export error] format <" + outputFormat + "> " + } else throw Exception("[export error] format <" + outputFormat + "> " "not supported", FL_AT); writer << exporter->toString(engine.get()); } @@ -321,14 +323,14 @@ namespace fl { int ch = 0; #ifdef FL_UNIX struct termios oldt, newt; - tcgetattr(STDIN_FILENO, &oldt); + ::tcgetattr(STDIN_FILENO, &oldt); newt = oldt; newt.c_lflag &= ~(ICANON | ECHO); - tcsetattr(STDIN_FILENO, TCSANOW, &newt); - ch = getchar(); - tcsetattr(STDIN_FILENO, TCSANOW, &oldt); + ::tcsetattr(STDIN_FILENO, TCSANOW, &newt); + ch = ::getchar(); + ::tcsetattr(STDIN_FILENO, TCSANOW, &oldt); #elif defined(FL_WINDOWS) - ch = _getch(); + ch = ::_getch(); #endif return ch; } @@ -349,13 +351,17 @@ namespace fl { ch = readCharacter(); + if (ch == EOF) { + break; + } + if (std::isspace(ch)) { - scalar value = engine->getInputVariable(inputValues.size())->getInputValue(); + scalar value = engine->getInputVariable(inputValues.size())->getValue(); try { - value = fl::Op::toScalar(inputValue.str()); + value = Op::toScalar(inputValue.str()); } catch (std::exception& ex) { - (void) ex; - buffer << "[" << fl::Op::str(value) << "]"; + FL_IUNUSED(ex); + buffer << "[" << Op::str(value) << "]"; } buffer << space; inputValue.str(""); @@ -375,7 +381,7 @@ namespace fl { case 'r': case 'R': engine->restart(); buffer << "#[Restart]"; - //fall through + continue; //fall through case 'd': case 'D': inputValues.clear(); buffer << "#[Discard]\n>"; @@ -388,15 +394,15 @@ namespace fl { for (std::size_t i = 0; i < inputValues.size(); ++i) { InputVariable* inputVariable = engine->inputVariables().at(i); - inputVariable->setInputValue(inputValues.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->getInputValue()); + missingInputs.push_back(inputVariable->getValue()); } inputValues.clear(); - buffer << fl::Op::join(missingInputs, space); + buffer << Op::join(missingInputs, space); if (not missingInputs.empty()) buffer << space; buffer << "=" << space; try { @@ -405,9 +411,9 @@ namespace fl { for (std::size_t i = 0; i < engine->outputVariables().size(); ++i) { OutputVariable* outputVariable = engine->outputVariables().at(i); outputVariable->defuzzify(); - outputValues.push_back(outputVariable->getOutputValue()); + outputValues.push_back(outputVariable->getValue()); } - buffer << fl::Op::join(outputValues, space) << "\n>"; + buffer << Op::join(outputValues, space) << "\n>"; } catch (std::exception& ex) { buffer << "#[Error: " << ex.what() << "]"; @@ -422,7 +428,7 @@ namespace fl { inputValue.str(""); break; } - } while (not (ch == 'Q' or ch == 'q')); + } while (not (ch == 'Q' or ch == 'q' or ch == 4)); writer << std::endl; } @@ -439,145 +445,283 @@ namespace fl { } Engine* Console::mamdani() { - Engine* engine = new Engine("simple-dimmer"); - - InputVariable* ambient = new InputVariable("Ambient", 0, 1); - ambient->addTerm(new Triangle("DARK", .0, .25, .5)); - ambient->addTerm(new Triangle("MEDIUM", .25, .5, .75)); - ambient->addTerm(new Triangle("BRIGHT", .5, .75, 1)); + 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", 0, 2); + 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->addTerm(new Triangle("LOW", 0.0, 0.5, 1)); - power->addTerm(new Triangle("MEDIUM", 0.5, 1, 1.5)); - power->addTerm(new Triangle("HIGH", 1, 1.5, 2)); + 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->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); - - engine->configure("", "", "Minimum", "Maximum", "Centroid"); + 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("approximation of sin(x)/x"); - - fl::InputVariable* inputX = new fl::InputVariable("inputX"); - inputX->setRange(0, 10); - inputX->addTerm(new fl::Triangle("NEAR_1", 0, 1, 2)); - inputX->addTerm(new fl::Triangle("NEAR_2", 1, 2, 3)); - inputX->addTerm(new fl::Triangle("NEAR_3", 2, 3, 4)); - inputX->addTerm(new fl::Triangle("NEAR_4", 3, 4, 5)); - inputX->addTerm(new fl::Triangle("NEAR_5", 4, 5, 6)); - inputX->addTerm(new fl::Triangle("NEAR_6", 5, 6, 7)); - inputX->addTerm(new fl::Triangle("NEAR_7", 6, 7, 8)); - inputX->addTerm(new fl::Triangle("NEAR_8", 7, 8, 9)); - inputX->addTerm(new fl::Triangle("NEAR_9", 8, 9, 10)); + 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); - - fl::OutputVariable* outputFx = new fl::OutputVariable("outputFx"); - outputFx->setRange(-1, 1); + 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->setLockPreviousOutputValue(true); //To use its value with diffFx - outputFx->addTerm(new Constant("f1", 0.84)); - outputFx->addTerm(new Constant("f2", 0.45)); - outputFx->addTerm(new Constant("f3", 0.04)); - outputFx->addTerm(new Constant("f4", -0.18)); - outputFx->addTerm(new Constant("f5", -0.19)); - outputFx->addTerm(new Constant("f6", -0.04)); - outputFx->addTerm(new Constant("f7", 0.09)); - outputFx->addTerm(new Constant("f8", 0.12)); - outputFx->addTerm(new Constant("f9", 0.04)); + 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); - fl::OutputVariable* trueFx = new fl::OutputVariable("trueFx"); - trueFx->setRange(fl::nan, fl::nan); - trueFx->setLockPreviousOutputValue(true); //To use its value with diffFx - trueFx->addTerm(fl::Function::create("fx", "sin(inputX)/inputX", engine)); - engine->addOutputVariable(trueFx); - - fl::OutputVariable* diffFx = new fl::OutputVariable("diffFx"); - diffFx->addTerm(fl::Function::create("diff", "fabs(outputFx-trueFx)", engine)); - diffFx->setRange(fl::nan, fl::nan); - // diffFx->setLockValidOutput(true); //To use in input diffPreviousFx - engine->addOutputVariable(diffFx); - - fl::RuleBlock* block = new fl::RuleBlock(); - block->addRule(fl::Rule::parse("if inputX is NEAR_1 then outputFx is f1", engine)); - block->addRule(fl::Rule::parse("if inputX is NEAR_2 then outputFx is f2", engine)); - block->addRule(fl::Rule::parse("if inputX is NEAR_3 then outputFx is f3", engine)); - block->addRule(fl::Rule::parse("if inputX is NEAR_4 then outputFx is f4", engine)); - block->addRule(fl::Rule::parse("if inputX is NEAR_5 then outputFx is f5", engine)); - block->addRule(fl::Rule::parse("if inputX is NEAR_6 then outputFx is f6", engine)); - block->addRule(fl::Rule::parse("if inputX is NEAR_7 then outputFx is f7", engine)); - block->addRule(fl::Rule::parse("if inputX is NEAR_8 then outputFx is f8", engine)); - block->addRule(fl::Rule::parse("if inputX is NEAR_9 then outputFx is f9", engine)); - block->addRule(fl::Rule::parse("if inputX is any then trueFx is fx and diffFx is diff", engine)); - engine->addRuleBlock(block); - - engine->configure("", "", "AlgebraicProduct", "AlgebraicSum", "WeightedAverage"); + 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, "."); + Console::exportAllExamples(from, to, "./", "/tmp/"); } - void Console::exportAllExamples(const std::string& from, const std::string& to, const std::string& path) { + 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/Laundry"); - examples.push_back("/mamdani/SimpleDimmer"); - // 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/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"); - - std::string sourceBase = path + "/original"; - std::string targetBase = path + "/tmp/"; + 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 fl::Exception("[examples error] unrecognized format <" + from + "> to import", FL_AT); + else throw Exception("[examples error] unrecognized format <" + from + "> to import", FL_AT); FL_unique_ptr<Exporter> exporter; if (to == "fll") exporter.reset(new FllExporter); @@ -586,16 +730,19 @@ namespace fl { 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 throw fl::Exception("[examples error] unrecognized format <" + to + "> to export", FL_AT); + 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) { - FL_LOG("Processing " << (i + 1) << "/" << examples.size() << ": " << examples.at(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 = sourceBase + examples.at(i) + "." + from; + std::string input = sourcePath + "/" + example + "." + from; std::ifstream source(input.c_str()); if (source.is_open()) { std::string line; @@ -604,29 +751,40 @@ namespace fl { ss << line << "\n"; } source.close(); - } else throw fl::Exception("[examples error] file not found: " + input, FL_AT); + } 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) { - std::string out = tests.at(t).first->toString(engine.get()); - FL_unique_ptr<Engine> copy(tests.at(t).second->fromString(out)); - std::string out_copy = tests.at(t).first->toString(copy.get()); - - if (out != out_copy) { - std::ostringstream ss; - ss << "[imex error] different results <" - << importer->name() << "," << exporter->name() << "> " - "at " + examples.at(t) + "." + from + ":\n"; - ss << "<Engine A>\n" << out << "\n\n" << + 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" << out_copy; - throw fl::Exception(ss.str(), FL_AT); + "<Engine B>\n" << imported; + throw Exception(msg.str(), FL_AT); } } - std::string output = targetBase + examples.at(i) + "." + to; + 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" @@ -634,8 +792,9 @@ namespace fl { << exporter->toString(engine.get()) << "\n}\n"; } else if (to == "java") { - std::string className = examples.at(i).substr(examples.at(i).find_last_of('/') + 1); + 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" @@ -650,171 +809,215 @@ namespace fl { << "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()); - (void) copyConstructor; + FL_IUNUSED(copyConstructor); Engine assignmentOperator = *engine.get(); - (void) assignmentOperator; + 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; } } -#ifdef FL_CPP11 - - void Console::benchmarkExamples(const std::string& path, int runs) { - std::string sourceBase = path + "/original"; - typedef std::pair<std::string, int > Example; - std::vector<Example> examples; - examples.push_back(Example("/mamdani/AllTerms", 1e4)); - examples.push_back(Example("/mamdani/SimpleDimmer", 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", 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", 1e6)); - examples.push_back(Example("/takagi-sugeno/SimpleDimmer", 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", 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", 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", 1e6)); - - for (std::size_t i = 0; i < examples.size(); ++i) { - FL_LOG(examples.at(i).first << "\t" << examples.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 << " ..."); } - - std::vector<std::string> runNumbers(runs); for (int i = 0; i < runs; ++i) { - runNumbers.at(i) = std::to_string(i + 1); + benchmark.runOnce(); } - std::string spacedPath(40, ' '); - std::copy(path.begin(), path.end(), spacedPath.begin()); - FL_LOG(spacedPath << "\t" << "mean\tstdev\n" << Op::join(runNumbers, "\t")); - - FllImporter importer; - FldExporter exporter; - exporter.setExportHeader(false); - exporter.setExportInputValues(false); - exporter.setExportOutputValues(false); - std::ostream dummy(0); + 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)); + } + } - for (std::size_t e = 0; e < examples.size(); ++e) { - FL_unique_ptr<Engine> engine(importer.fromFile(sourceBase + examples.at(e).first + ".fll")); + void Console::benchmarks(const std::string& fllFileList, + const std::string& fldFileList, int runs, std::ofstream* writer) const { + std::vector<std::string> fllFiles, fldFiles; - std::vector<scalar> seconds; - int results = std::pow(1.0 * examples.at(e).second, engine->numberOfInputVariables()); + { + 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"); + } - for (int r = 0; r < runs; ++r) { - auto start = std::chrono::system_clock::now(); - exporter.write(engine.get(), dummy, results); - auto end = std::chrono::system_clock::now(); + 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); + } + } - auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds> (end - start); + if (writer) { + *writer << Op::join(Benchmark().header(runs, true), "\t") << "\n"; + } else { + FL_LOGP(Op::join(Benchmark().header(runs, true), "\t")); + } - seconds.push_back(elapsed.count() / 1e3); + for (std::size_t i = 0; i < fllFiles.size(); ++i) { + if (writer) { + FL_LOG("Benchmark " << (i + 1) << "/" << fllFiles.size() << ": " + << fllFiles.at(i)); } - scalar mean = Op::mean(seconds); - scalar stdev = Op::standardDeviation(seconds, mean); - - std::string spacedExample(40, ' '); - std::string exampleName = examples.at(e).first; - std::copy(exampleName.begin(), exampleName.end(), spacedExample.begin()); - FL_LOG(spacedExample << "\t" << fl::Op::str(mean) << "\t" << fl::Op::str(stdev) << "\n" << - Op::join(seconds, "\t")); + benchmark(fllFiles.at(i), fldFiles.at(i), runs, writer); } } -#endif - int Console::main(int argc, char** argv) { - (void) argc; - (void) argv; + int Console::main(int argc, const char* argv[]) { + fuzzylite::setLogging(true); + + Console console; if (argc <= 2) { - std::cout << usage() << std::endl; + FL_LOGP(console.usage() << "\n"); return EXIT_SUCCESS; } - const std::string firstArgument = std::string(argv[1]); - + const std::string firstArgument(argv[1]); if (firstArgument == "export-examples") { std::string path = "."; if (argc > 2) { path = std::string(argv[2]); } - FL_LOG("Path=" << path); + std::string outputPath = "/tmp/"; + if (argc > 3) { + outputPath = std::string(argv[3]); + } + FL_LOG("Origin=" << path); + FL_LOG("Target=" << outputPath); + fuzzylite::setDecimals(3); try { - fuzzylite::setDecimals(3); FL_LOG("Processing fll->fll"); - exportAllExamples("fll", "fll", path); + console.exportAllExamples("fll", "fll", path, outputPath); FL_LOG("Processing fll->fcl"); - exportAllExamples("fll", "fcl", path); + console.exportAllExamples("fll", "fcl", path, outputPath); FL_LOG("Processing fll->fis"); - exportAllExamples("fll", "fis", path); + console.exportAllExamples("fll", "fis", path, outputPath); FL_LOG("Processing fll->cpp"); - exportAllExamples("fll", "cpp", path); + console.exportAllExamples("fll", "cpp", path, outputPath); FL_LOG("Processing fll->java"); - exportAllExamples("fll", "java", path); - fuzzylite::setDecimals(8); - fuzzylite::setMachEps(1e-6); + 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"); - exportAllExamples("fll", "fld", path); + 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) { - std::cout << ex.what() << "\nBACKTRACE:\n" << - fl::Exception::btCallStack() << std::endl; + FL_LOGP(ex.what() << "\n"); return EXIT_FAILURE; } return EXIT_SUCCESS; + } else if (firstArgument == "benchmarks") { -#ifdef FL_CPP11 - std::string path = "."; - if (argc > 2) { - path = std::string(argv[2]); + if (argc < 5) { + FL_LOG("[error] not enough parameters"); + return EXIT_FAILURE; } - int runs = 10; - if (argc > 3) { - runs = (int) Op::toScalar(argv[3]); + 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; } - fuzzylite::setDecimals(3); - Console::benchmarkExamples(path, runs); return EXIT_SUCCESS; -#else - throw fl::Exception("[benchmarks error] implementation available only when built with C++11 (-DFL_CPP11)", FL_AT); -#endif } + //MAIN: try { - std::map<std::string, std::string> options = parse(argc, argv); - process(options); + std::map<std::string, std::string> options = console.parse(argc, argv); + console.process(options); + } catch (std::exception& ex) { - std::cout << ex.what() << "\n" << std::endl; - // std::cout << fl::Exception::btCallStack() << std::endl; + FL_LOGP(ex.what() << "\n"); return EXIT_FAILURE; } return EXIT_SUCCESS; } + } diff --git a/fuzzylite/src/Engine.cpp b/fuzzylite/src/Engine.cpp index f1e23c0..dbe4b17 100644 --- a/fuzzylite/src/Engine.cpp +++ b/fuzzylite/src/Engine.cpp @@ -1,46 +1,35 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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/factory/SNormFactory.h" -#include "fl/factory/TNormFactory.h" -#include "fl/hedge/Hedge.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/Accumulated.h" +#include "fl/term/Aggregated.h" #include "fl/term/Constant.h" #include "fl/term/Linear.h" -#include "fl/term/Function.h" #include "fl/term/Ramp.h" #include "fl/term/Sigmoid.h" #include "fl/term/SShape.h" @@ -48,21 +37,19 @@ #include "fl/variable/InputVariable.h" #include "fl/variable/OutputVariable.h" - namespace fl { - Engine::Engine(const std::string& name) : _name(name) { - } + Engine::Engine(const std::string& name) : _name(name) { } - Engine::Engine(const Engine& other) : _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 < _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(); @@ -77,6 +64,7 @@ namespace fl { 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) @@ -84,13 +72,14 @@ namespace fl { updateReferences(); - for (std::size_t i = 0; i < other._ruleblocks.size(); ++i) { - RuleBlock* ruleBlock = new RuleBlock(*other._ruleblocks.at(i)); + 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); + _ruleBlocks.push_back(ruleBlock); } } @@ -98,78 +87,89 @@ namespace fl { std::vector<Variable*> myVariables = variables(); for (std::size_t i = 0; i < myVariables.size(); ++i) { Variable* variable = myVariables.at(i); - for (int t = 0; t < variable->numberOfTerms(); ++t) { - Term::updateReference(variable->getTerm(t), this); + 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); + 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& conjunctionT, const std::string& disjunctionS, - const std::string& activationT, const std::string& accumulationS, - const std::string& defuzzifierName, int resolution) { + 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(); - TNorm* conjunction = tnormFactory->constructObject(conjunctionT); - SNorm* disjunction = snormFactory->constructObject(disjunctionS); - TNorm* activation = tnormFactory->constructObject(activationT); - SNorm* accumulation = snormFactory->constructObject(accumulationS); - Defuzzifier* defuzzifier = defuzzFactory->constructObject(defuzzifierName); - IntegralDefuzzifier* integralDefuzzifier = dynamic_cast<IntegralDefuzzifier*> (defuzzifier); - if (integralDefuzzifier) integralDefuzzifier->setResolution(resolution); + 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(conjunction, disjunction, activation, accumulation, defuzzifier); + configure(conjunctionObject, disjunctionObject, + implicationObject, aggregationObject, defuzzifierObject, + activationObject); } void Engine::configure(TNorm* conjunction, SNorm* disjunction, - TNorm* activation, SNorm* accumulation, Defuzzifier* defuzzifier) { - for (std::size_t i = 0; i < _ruleblocks.size(); ++i) { - _ruleblocks.at(i)->setConjunction(conjunction ? conjunction->clone() : fl::null); - _ruleblocks.at(i)->setDisjunction(disjunction ? disjunction->clone() : fl::null); - _ruleblocks.at(i)->setActivation(activation ? activation->clone() : fl::null); + 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 < _outputVariables.size(); ++i) { - _outputVariables.at(i)->setDefuzzifier(defuzzifier ? defuzzifier->clone() : fl::null); - _outputVariables.at(i)->fuzzyOutput()->setAccumulation( - accumulation ? accumulation->clone() : fl::null); + 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 (accumulation) delete accumulation; - if (activation) delete activation; + 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 <" << _name << "> has no input variables\n"; + 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); + for (std::size_t i = 0; i < inputVariables().size(); ++i) { + InputVariable* inputVariable = inputVariables().at(i); if (not inputVariable) { - ss << "- Engine <" << _name << "> 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"; + 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()) { + 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); + for (std::size_t i = 0; i < outputVariables().size(); ++i) { + OutputVariable* outputVariable = outputVariables().at(i); if (not outputVariable) { - ss << "- Engine <" << _name << "> has a fl::null output variable at index <" << i << ">\n"; + ss << "- Engine <" << getName() << "> has a fl::null output variable at index <" << i << ">\n"; } else { if (outputVariable->terms().empty()) { ss << "- Output variable <" << outputVariable->getName() << ">" @@ -180,29 +180,29 @@ namespace fl { ss << "- Output variable <" << outputVariable->getName() << ">" << " has no defuzzifier\n"; } - SNorm* accumulation = outputVariable->fuzzyOutput()->getAccumulation(); - if (not accumulation and dynamic_cast<IntegralDefuzzifier*> (defuzzifier)) { + SNorm* aggregation = outputVariable->fuzzyOutput()->getAggregation(); + if (not aggregation and dynamic_cast<IntegralDefuzzifier*> (defuzzifier)) { ss << "- Output variable <" << outputVariable->getName() << ">" - << " has no accumulation operator\n"; + << " has no aggregation operator\n"; } } } - if (_ruleblocks.empty()) { - ss << "- Engine <" << _name << "> has no rule blocks\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); + for (std::size_t i = 0; i < ruleBlocks().size(); ++i) { + RuleBlock* ruleblock = ruleBlocks().at(i); if (not ruleblock) { - ss << "- Engine <" << _name << "> has a fl::null rule block at index <" << i << ">\n"; + 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 requiresActivation = 0; - for (int r = 0; r < ruleblock->numberOfRules(); ++r) { + 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() @@ -224,7 +224,7 @@ namespace fl { const OutputVariable* outputVariable = dynamic_cast<const OutputVariable*> (proposition->variable); if (outputVariable and dynamic_cast<IntegralDefuzzifier*> (outputVariable->getDefuzzifier())) { - ++requiresActivation; + ++requiresImplication; break; } } @@ -243,11 +243,11 @@ namespace fl { ss << "- Rule block " << (i + 1) << " <" << ruleblock->getName() << "> has " << requiresDisjunction << " rules that require disjunction operator\n"; } - const TNorm* activation = ruleblock->getActivation(); - if (requiresActivation > 0 and not activation) { - ss << "- Rule block " << (i + 1) << " <" << ruleblock->getName() << "> has no activation 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 " - << requiresActivation << " rules that require activation operator\n"; + << requiresImplication << " rules that require implication operator\n"; } } } @@ -256,14 +256,25 @@ namespace fl { } void Engine::restart() { - for (std::size_t i = 0; i < _inputVariables.size(); ++i) { - _inputVariables.at(i)->setInputValue(fl::nan); + 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(); + 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(); @@ -274,7 +285,7 @@ namespace fl { FL_DBG("CURRENT INPUTS:"); for (std::size_t i = 0; i < _inputVariables.size(); ++i) { InputVariable* inputVariable = _inputVariables.at(i); - scalar inputValue = inputVariable->getInputValue(); + scalar inputValue = inputVariable->getValue(); if (inputVariable->isEnabled()) { FL_DBG(inputVariable->getName() << ".input = " << Op::str(inputValue)); FL_DBG(inputVariable->getName() << ".fuzzy = " << inputVariable->fuzzify(inputValue)); @@ -285,9 +296,11 @@ namespace fl { FL_DEBUG_END; - for (std::size_t i = 0; i < _ruleblocks.size(); ++i) { - RuleBlock* ruleBlock = _ruleblocks.at(i); + 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(); } } @@ -306,12 +319,12 @@ namespace fl { << outputVariable->getDefaultValue()); FL_DBG(outputVariable->getName() << ".lockValueInRange = " - << outputVariable->isLockedOutputValueInRange()); + << outputVariable->isLockValueInRange()); FL_DBG(outputVariable->getName() << ".lockPreviousValue= " - << outputVariable->isLockedPreviousOutputValue()); + << outputVariable->isLockPreviousValue()); - scalar output = outputVariable->getOutputValue(); + scalar output = outputVariable->getValue(); FL_DBG(outputVariable->getName() << ".output = " << output); FL_DBG(outputVariable->getName() << ".fuzzy = " << outputVariable->fuzzify(output)); @@ -332,12 +345,20 @@ namespace fl { 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 (outputVariables().empty()) { if (name) *name = "Unknown"; if (reason) *reason = "- Engine has no output variables"; return Engine::Unknown; @@ -345,24 +366,24 @@ namespace fl { //Mamdani bool mamdani = true; - for (std::size_t i = 0; mamdani and i < _outputVariables.size(); ++i) { - OutputVariable* outputVariable = _outputVariables.at(i); + 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 Activation + 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->getActivation()); + 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" - "- Rule blocks activate using the algebraic product T-Norm"; + "- Implication in rule blocks is the algebraic product T-Norm"; return Engine::Larsen; } if (mamdani) { @@ -374,8 +395,8 @@ namespace fl { //TakagiSugeno bool takagiSugeno = true; - for (std::size_t i = 0; takagiSugeno and i < _outputVariables.size(); ++i) { - OutputVariable* outputVariable = _outputVariables.at(i); + 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()); @@ -386,7 +407,7 @@ namespace fl { if (takagiSugeno) { //Takagi-Sugeno has only Constant, Linear or Function terms - for (int t = 0; takagiSugeno and t < outputVariable->numberOfTerms(); ++t) { + 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; @@ -402,8 +423,8 @@ namespace fl { //Tsukamoto bool tsukamoto = true; - for (std::size_t i = 0; tsukamoto and i < _outputVariables.size(); ++i) { - OutputVariable* outputVariable = _outputVariables.at(i); + 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()); @@ -413,9 +434,9 @@ namespace fl { weightedDefuzzifier->getType() == WeightedDefuzzifier::Tsukamoto); if (tsukamoto) { //Tsukamoto has only monotonic terms: Concave, Ramp, Sigmoid, SShape, or ZShape - for (int t = 0; tsukamoto and t < outputVariable->numberOfTerms(); ++t) { + for (std::size_t t = 0; tsukamoto and t < outputVariable->numberOfTerms(); ++t) { Term* term = outputVariable->getTerm(t); - tsukamoto = tsukamoto and weightedDefuzzifier->isMonotonic(term); + tsukamoto = tsukamoto and term->isMonotonic(); } } } @@ -428,8 +449,8 @@ namespace fl { //Inverse Tsukamoto bool inverseTsukamoto = true; - for (std::size_t i = 0; inverseTsukamoto and i < _outputVariables.size(); ++i) { - OutputVariable* outputVariable = _outputVariables.at(i); + 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()); @@ -444,19 +465,19 @@ namespace fl { } bool hybrid = true; - for (std::size_t i = 0; i < _outputVariables.size(); ++i) { - OutputVariable* outputVariable = _outputVariables.at(i); + 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 defuzzifiers"; + if (reason) *reason = "- Output variables have different types of defuzzifiers"; return Engine::Hybrid; } if (name) *name = "Unknown"; - if (reason) *reason = "- There are output variables without a defuzzifier"; + if (reason) *reason = "- One or more output variables do not have a defuzzifier"; return Engine::Unknown; } @@ -466,9 +487,9 @@ namespace fl { 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()); + result.reserve(inputVariables().size() + outputVariables().size()); + result.insert(result.end(), inputVariables().begin(), inputVariables().end()); + result.insert(result.end(), outputVariables().begin(), outputVariables().end()); return result; } @@ -477,63 +498,62 @@ namespace fl { */ void Engine::setInputValue(const std::string& name, scalar value) { InputVariable* inputVariable = getInputVariable(name); - inputVariable->setInputValue(value); + inputVariable->setValue(value); } void Engine::addInputVariable(InputVariable* inputVariable) { - this->_inputVariables.push_back(inputVariable); + inputVariables().push_back(inputVariable); } - InputVariable* Engine::setInputVariable(InputVariable* inputVariable, int index) { - InputVariable* result = this->_inputVariables.at(index); - this->_inputVariables.at(index) = 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, int index) { - this->_inputVariables.insert(this->_inputVariables.begin() + index, - inputVariable); + void Engine::insertInputVariable(InputVariable* inputVariable, std::size_t index) { + inputVariables().insert(inputVariables().begin() + index, inputVariable); } - InputVariable* Engine::getInputVariable(int index) const { - return this->_inputVariables.at(index); + 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); + for (std::size_t i = 0; i < inputVariables().size(); ++i) { + if (inputVariables().at(i)->getName() == name) + return inputVariables().at(i); } - throw fl::Exception("[engine error] input variable <" + name + "> not found", FL_AT); + 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) + for (std::size_t i = 0; i < inputVariables().size(); ++i) { + if (inputVariables().at(i)->getName() == name) return true; } return false; } - InputVariable* Engine::removeInputVariable(int index) { - InputVariable* result = this->_inputVariables.at(index); - this->_inputVariables.erase(this->_inputVariables.begin() + index); + 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 = this->_inputVariables.at(i); - this->_inputVariables.erase(this->_inputVariables.begin() + i); + 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 fl::Exception("[engine error] input variable <" + name + "> not found", FL_AT); + throw Exception("[engine error] input variable <" + name + "> not found", FL_AT); } - int Engine::numberOfInputVariables() const { - return this->_inputVariables.size(); + std::size_t Engine::numberOfInputVariables() const { + return inputVariables().size(); } const std::vector<InputVariable*>& Engine::inputVariables() const { @@ -553,63 +573,62 @@ namespace fl { */ scalar Engine::getOutputValue(const std::string& name) { OutputVariable* outputVariable = getOutputVariable(name); - return outputVariable->getOutputValue(); + return outputVariable->getValue(); } void Engine::addOutputVariable(OutputVariable* outputVariable) { - this->_outputVariables.push_back(outputVariable); + outputVariables().push_back(outputVariable); } - OutputVariable* Engine::setOutputVariable(OutputVariable* outputVariable, int index) { - OutputVariable* result = this->_outputVariables.at(index); - this->_outputVariables.at(index) = 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, int index) { - this->_outputVariables.insert(this->_outputVariables.begin() + index, - outputVariable); + void Engine::insertOutputVariable(OutputVariable* outputVariable, std::size_t index) { + outputVariables().insert(outputVariables().begin() + index, outputVariable); } - OutputVariable* Engine::getOutputVariable(int index) const { - return this->_outputVariables.at(index); + 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); + for (std::size_t i = 0; i < outputVariables().size(); ++i) { + if (outputVariables().at(i)->getName() == name) + return outputVariables().at(i); } - throw fl::Exception("[engine error] output variable <" + name + "> not found", FL_AT); + 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) + for (std::size_t i = 0; i < outputVariables().size(); ++i) { + if (outputVariables().at(i)->getName() == name) return true; } return false; } - OutputVariable* Engine::removeOutputVariable(int index) { - OutputVariable* result = this->_outputVariables.at(index); - this->_outputVariables.erase(this->_outputVariables.begin() + index); + 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 = this->_outputVariables.at(i); - this->_outputVariables.erase(this->_outputVariables.begin() + i); + 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 fl::Exception("[engine error] output variable <" + name + "> not found", FL_AT); + throw Exception("[engine error] output variable <" + name + "> not found", FL_AT); } - int Engine::numberOfOutputVariables() const { - return this->_outputVariables.size(); + std::size_t Engine::numberOfOutputVariables() const { + return outputVariables().size(); } const std::vector<OutputVariable*>& Engine::outputVariables() const { @@ -628,71 +647,70 @@ namespace fl { * Operations for iterable datatype _ruleblocks */ void Engine::addRuleBlock(RuleBlock* ruleblock) { - this->_ruleblocks.push_back(ruleblock); + ruleBlocks().push_back(ruleblock); } - RuleBlock* Engine::setRuleBlock(RuleBlock* ruleBlock, int index) { - RuleBlock* result = this->_ruleblocks.at(index); - this->_ruleblocks.at(index) = 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, int index) { - this->_ruleblocks.insert(this->_ruleblocks.begin() + index, ruleblock); + void Engine::insertRuleBlock(RuleBlock* ruleblock, std::size_t index) { + ruleBlocks().insert(ruleBlocks().begin() + index, ruleblock); } - RuleBlock* Engine::getRuleBlock(int index) const { - return this->_ruleblocks.at(index); + 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); + for (std::size_t i = 0; i < ruleBlocks().size(); ++i) { + if (ruleBlocks().at(i)->getName() == name) + return ruleBlocks().at(i); } - throw fl::Exception("[engine error] rule block <" + name + "> not found", FL_AT); + 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) + for (std::size_t i = 0; i < ruleBlocks().size(); ++i) { + if (ruleBlocks().at(i)->getName() == name) return true; } return false; } - RuleBlock* Engine::removeRuleBlock(int index) { - RuleBlock* result = this->_ruleblocks.at(index); - this->_ruleblocks.erase(this->_ruleblocks.begin() + index); + 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 = this->_ruleblocks.at(i); - this->_ruleblocks.erase(this->_ruleblocks.begin() + i); + 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 fl::Exception("[engine error] rule block <" + name + "> not found", FL_AT); + throw Exception("[engine error] rule block <" + name + "> not found", FL_AT); } - int Engine::numberOfRuleBlocks() const { - return this->_ruleblocks.size(); + std::size_t Engine::numberOfRuleBlocks() const { + return ruleBlocks().size(); } const std::vector<RuleBlock*>& Engine::ruleBlocks() const { - return this->_ruleblocks; + return this->_ruleBlocks; } void Engine::setRuleBlocks(const std::vector<RuleBlock*>& ruleBlocks) { - this->_ruleblocks = ruleBlocks; + this->_ruleBlocks = ruleBlocks; } std::vector<RuleBlock*>& Engine::ruleBlocks() { - return this->_ruleblocks; + return this->_ruleBlocks; } - } diff --git a/fuzzylite/src/Exception.cpp b/fuzzylite/src/Exception.cpp index d24adac..1c27e70 100644 --- a/fuzzylite/src/Exception.cpp +++ b/fuzzylite/src/Exception.cpp @@ -1,48 +1,47 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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_OFF -//do nothing +#ifdef FL_BACKTRACE -#elif defined FL_UNIX +#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 <signal.h> -#include <stdlib.h> -#include <string.h> +#include <csignal> +#include <cstring> namespace fl { @@ -58,8 +57,7 @@ namespace fl { FL_DBG(this->what()); } - Exception::~Exception() FL_INOEXCEPT { - } + Exception::~Exception() FL_INOEXCEPT { } void Exception::setWhat(const std::string& what) { this->_what = what; @@ -90,23 +88,25 @@ namespace fl { } std::string Exception::btCallStack() { -#ifdef FL_BACKTRACE_OFF - return "[backtrace disabled] fuzzylite was built with option -DFL_BACKTRACE_OFF"; +#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); + 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]"; + if (backtraceSize == 0) { + btStream << "[backtrace is empty]"; + } for (int i = 0; i < backtraceSize; ++i) { btStream << btSymbols[i] << "\n"; } } - free(btSymbols); + ::free(btSymbols); return btStream.str(); @@ -123,51 +123,52 @@ namespace fl { } else { btSymbol->MaxNameLen = 255; btSymbol->SizeOfStruct = sizeof ( SYMBOL_INFO); - if (backtraceSize == 0) btStream << "[backtrace is empty]"; + 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); + ::free(btSymbol); return btStream.str(); #else return "[backtrace missing] supported only in Unix and Windows platforms"; #endif } - //execinfo - void Exception::signalHandler(int signal) { + void Exception::signalHandler(int unixSignal) { std::ostringstream ex; - ex << "[unexpected signal " << signal << "] "; + ex << "[unexpected signal " << unixSignal << "] "; #ifdef FL_UNIX - ex << strsignal(signal); + ex << ::strsignal(unixSignal); #endif ex << "\nBACKTRACE:\n" << btCallStack(); - fl::Exception::catchException(fl::Exception(ex.str(), FL_AT)); - exit(EXIT_FAILURE); + Exception::catchException(Exception(ex.str(), FL_AT)); + ::exit(EXIT_FAILURE); } - void Exception::convertToException(int signal) { + void Exception::convertToException(int unixSignal) { std::string signalDescription; #ifdef FL_UNIX //Unblock the signal sigset_t empty; sigemptyset(&empty); - sigaddset(&empty, signal); + sigaddset(&empty, unixSignal); sigprocmask(SIG_UNBLOCK, &empty, fl::null); - signalDescription = strsignal(signal); + signalDescription = ::strsignal(unixSignal); #endif std::ostringstream ex; - ex << "[signal " << signal << "] " << signalDescription << "\n"; + ex << "[signal " << unixSignal << "] " << signalDescription << "\n"; ex << "BACKTRACE:\n" << btCallStack(); - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } void Exception::terminate() { - fl::Exception::catchException(fl::Exception("[unexpected exception] BACKTRACE:\n" + btCallStack(), FL_AT)); - exit(EXIT_FAILURE); + Exception::catchException(Exception("[unexpected exception] BACKTRACE:\n" + btCallStack(), FL_AT)); + ::exit(EXIT_FAILURE); } void Exception::catchException(const std::exception& exception) { diff --git a/fuzzylite/src/Operation.cpp b/fuzzylite/src/Operation.cpp deleted file mode 100644 index 1596ea4..0000000 --- a/fuzzylite/src/Operation.cpp +++ /dev/null @@ -1,477 +0,0 @@ -/* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved - - This file is part of fuzzylite. - - fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. - - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. - - */ - -#include "fl/Operation.h" - -#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> - T Operation::min(T a, T b) { - if (isNaN(a)) return b; - if (isNaN(b)) return a; - return a < b ? a : b; - } - template FL_API scalar Operation::min(scalar a, scalar b); - template FL_API int Operation::min(int a, int b); - - template <typename T> - T Operation::max(T a, T b) { - if (isNaN(a)) return b; - if (isNaN(b)) return a; - return a > b ? a : b; - } - template FL_API scalar Operation::max(scalar a, scalar b); - template FL_API int Operation::max(int a, int b); - - template <typename T> - T Operation::bound(T x, T min, T max) { - if (isGt(x, max)) return max; - if (isLt(x, min)) return min; - return x; - } - template FL_API scalar Operation::bound(scalar x, scalar min, scalar max); - template FL_API int Operation::bound(int x, int min, int max); - - template <typename T> - bool Operation::in(T x, T min, T max, bool geq, bool leq) { - bool left = geq ? isGE(x, min) : isGt(x, min); - bool right = leq ? isLE(x, max) : isLt(x, max); - return (left and right); - } - template FL_API bool Operation::in(scalar x, scalar min, scalar max, bool geq, bool leq); - template FL_API bool Operation::in(int x, int min, int max, bool geq, bool leq); - - template <typename T> - bool Operation::isInf(T x) { - return std::abs(x) == fl::inf; - } - template FL_API bool Operation::isInf(int x); - template FL_API bool Operation::isInf(scalar x); - - template <typename T> - bool Operation::isNaN(T x) { - return not (x == x); - } - template FL_API bool Operation::isNaN(int x); - template FL_API bool Operation::isNaN(scalar x); - - template<typename T> - bool Operation::isFinite(T x) { - return not (isNaN(x) or isInf(x)); - } - template FL_API bool Operation::isFinite(int x); - template FL_API bool Operation::isFinite(scalar x); - - bool Operation::isLt(scalar a, scalar b, scalar macheps) { - return not isEq(a, b, macheps) and a < b; - } - - bool Operation::isLE(scalar a, scalar b, scalar macheps) { - return isEq(a, b, macheps) or a < b; - } - - bool Operation::isEq(scalar a, scalar b, scalar macheps) { - return a == b or std::fabs(a - b) < macheps or (isNaN(a) and isNaN(b)); - } - - bool Operation::isGt(scalar a, scalar b, scalar macheps) { - return not isEq(a, b, macheps) and a > b; - } - - bool Operation::isGE(scalar a, scalar b, scalar macheps) { - return isEq(a, b, macheps) or a > b; - } - - 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 ? fl::Op::bound(result, toMin, toMax) : result; - } - - scalar Operation::add(scalar a, scalar b) { - return a + b; - } - - scalar Operation::subtract(scalar a, scalar b) { - return a - b; - } - - scalar Operation::multiply(scalar a, scalar b) { - return a * b; - } - - scalar Operation::divide(scalar a, scalar b) { - return a / b; - } - - scalar Operation::modulo(scalar a, scalar b) { - return fmod(a, b); - } - - scalar Operation::logicalAnd(scalar a, scalar b) { - return (isEq(a, 1.0) and isEq(b, 1.0)) ? 1.0 : 0.0; - } - - scalar Operation::logicalOr(scalar a, scalar b) { - return (isEq(a, 1.0) or isEq(b, 1.0)) ? 1.0 : 0.0; - } - - scalar Operation::logicalNot(scalar a) { - return isEq(a, 1.0) ? 0.0 : 1.0; - } - - scalar Operation::negate(scalar a) { - return -a; - } - - scalar Operation::round(scalar x) { - return (x > 0.0) ? std::floor(x + 0.5) : std::ceil(x - 0.5); - } - - scalar Operation::gt(scalar a, scalar b) { - return isGt(a, b); - } - - scalar Operation::ge(scalar a, scalar b) { - return isGE(a, b); - } - - scalar Operation::eq(scalar a, scalar b) { - return isEq(a, b); - } - - scalar Operation::neq(scalar a, scalar b) { - return not isEq(a, b); - } - - scalar Operation::le(scalar a, scalar b) { - return isLE(a, b); - } - - scalar Operation::lt(scalar a, scalar b) { - return isLt(a, b); - } - - bool Operation::increment(std::vector<int>& x, std::vector<int>& min, std::vector<int>& max) { - return increment(x, (int) x.size() - 1, min, max); - } - - bool Operation::increment(std::vector<int>& x, int position, std::vector<int>& min, std::vector<int>& max) { - if (x.empty() or position < 0) return true; - - bool overflow = false; - if (x.at(position) < max.at(position)) { - ++x.at(position); - } else { - overflow = (position == 0); - x.at(position) = min.at(position); - --position; - if (position >= 0) { - overflow = increment(x, position, min, max); - } - } - return overflow; - } - - double Operation::mean(const std::vector<scalar>& x) { - if (x.size() == 0) return fl::nan; - scalar sum = 0.0; - for (std::size_t i = 0; i < x.size(); ++i) sum += x.at(i); - return sum / x.size(); - } - - double Operation::standardDeviation(const std::vector<scalar>& x) { - if (x.size() <= 1) return 0.0; - return standardDeviation(x, mean(x)); - } - - double Operation::standardDeviation(const std::vector<scalar>& x, scalar mean) { - if (x.size() <= 1) return 0.0; - return std::sqrt(variance(x, mean)); - } - - double Operation::variance(const std::vector<scalar>& x) { - if (x.size() <= 1) return 0.0; - return variance(x, mean(x)); - } - - double Operation::variance(const std::vector<scalar>& x, scalar mean) { - if (x.size() <= 1) return 0.0; - scalar result = 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: - - 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(); - } - - int Operation::isValidForName(int character) { - return character == '_' or character == '.' or isalnum(character); - } - - 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(); - } - - 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; - } - - 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; - int 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; - } - int length = end - start + 1; - if (length <= 0) return ""; - return text.substr(start, length); - } - - 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(); - } - - 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, pInf, nInf; - nan << fl::nan; - pInf << fl::inf; - nInf << (-fl::inf); - - if (x == nan.str() or x == "nan") - return fl::nan; - if (x == pInf.str() or x == "inf") - return fl::inf; - if (x == nInf.str() or x == "-inf") - return -fl::inf; - - std::ostringstream ex; - ex << "[conversion error] from <" << x << "> to scalar"; - throw fl::Exception(ex.str(), FL_AT); - } - - scalar Operation::toScalar(const std::string& x, scalar alternative) FL_INOEXCEPT { - std::istringstream iss(x); - scalar result; - iss >> result; - char strict; - if (not (iss.fail() or iss.get(strict))) return result; - - std::ostringstream nan, pInf, nInf; - nan << fl::nan; - pInf << fl::inf; - nInf << (-fl::inf); - - if (x == nan.str() or x == "nan") - return fl::nan; - if (x == pInf.str() or x == "inf") - return fl::inf; - if (x == nInf.str() or x == "-inf") - return -fl::inf; - - return alternative; - } - - bool Operation::isNumeric(const std::string& x) { - try { - fl::Op::toScalar(x); - return true; - } catch (std::exception& ex) { - (void) ex; - return false; - } - } - - template <typename T> - std::string Operation::str(T x, int decimals) { - std::ostringstream ss; - ss << std::setprecision(decimals) << std::fixed; - if (fl::Op::isNaN(x)) { - ss << "nan"; - } else if (fl::Op::isInf(x)) { - ss << (fl::Op::isLt(x, 0.0) ? "-inf" : "inf"); - } else if (fl::Op::isEq(x, 0.0)) { - ss << 0.0; - } else ss << x; - return ss.str(); - } - template FL_API std::string Operation::str(int x, int precision); - template FL_API std::string Operation::str(scalar x, int precision); - - template <> FL_API std::string Operation::str(const std::string& x, int precision) { - (void) precision; - return x; - } - - template <typename T> - 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 std::string Operation::join(const std::vector<int>& x, - const std::string& separator); - template FL_API std::string Operation::join(const std::vector<scalar>& x, - const std::string& separator); - - template <> FL_API - 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> - 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 std::string Operation::join(int items, const std::string& separator, - int first, ...); - template FL_API std::string Operation::join(int items, const std::string& separator, - double first, ...); - - template <> FL_API 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 - 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(); - } - -} 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 index ee4d2fc..ed61b6f 100644 --- a/fuzzylite/src/defuzzifier/Bisector.cpp +++ b/fuzzylite/src/defuzzifier/Bisector.cpp @@ -1,76 +1,60 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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/Accumulated.h" #include "fl/term/Term.h" namespace fl { Bisector::Bisector(int resolution) - : IntegralDefuzzifier(resolution) { - } + : IntegralDefuzzifier(resolution) { } - Bisector::~Bisector() { - - } + 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 fl::Op::isFinite(minimum + maximum)) { - return fl::nan; - } - if (maximum - minimum > _resolution) { - FL_DBG("[accuracy warning] the resolution <" << _resolution << "> " - "is smaller than the range <" << minimum << ", " << maximum << ">. In order to " - "improve the accuracy, the resolution should be at least equal to the range."); - } - scalar dx = (maximum - minimum) / _resolution; + if (not Op::isFinite(minimum + maximum)) return fl::nan; - int counter = _resolution; + 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 (fl::Op::isLE(leftArea, rightArea)) { + if (Op::isLE(leftArea, rightArea)) { xLeft = minimum + (left + 0.5) * dx; leftArea += term->membership(xLeft); - left++; + ++left; } else { xRight = maximum - (right + 0.5) * dx; rightArea += term->membership(xRight); - right++; + ++right; } } - //Inverse weighted average to compensate - scalar bisector = (leftArea * xRight + rightArea * xLeft) / (leftArea + rightArea); - return bisector; + return (leftArea * xRight + rightArea * xLeft) / (leftArea + rightArea); } Bisector* Bisector::clone() const { @@ -81,5 +65,4 @@ namespace fl { return new Bisector; } - } diff --git a/fuzzylite/src/defuzzifier/Centroid.cpp b/fuzzylite/src/defuzzifier/Centroid.cpp index 01490d3..177da26 100644 --- a/fuzzylite/src/defuzzifier/Centroid.cpp +++ b/fuzzylite/src/defuzzifier/Centroid.cpp @@ -1,71 +1,60 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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/Accumulated.h" #include "fl/term/Term.h" - namespace fl { Centroid::Centroid(int resolution) - : IntegralDefuzzifier(resolution) { - } - - Centroid::~Centroid() { + : 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 fl::Op::isFinite(minimum + maximum)) { - return fl::nan; - } - if (maximum - minimum > _resolution) { - FL_DBG("[accuracy warning] the resolution <" << _resolution << "> " - "is smaller than the range <" << minimum << ", " << maximum << ">. In order to " - "improve the accuracy, the resolution should be at least equal to the range."); - } - scalar dx = (maximum - minimum) / _resolution; + 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, ycentroid = 0; - for (int i = 0; i < _resolution; ++i) { + 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; + //ycentroid += y * y; area += y; } - xcentroid /= area; - ycentroid /= 2 * area; - area *= dx; //total area... unused, but for future reference. - return xcentroid; + //Final results not computed for efficiency + //xcentroid /= area; + //ycentroid /= 2 * area; + //area *= dx; + return xcentroid / area; } Centroid* Centroid::clone() const { @@ -76,5 +65,4 @@ namespace fl { return new Centroid; } - } diff --git a/fuzzylite/src/defuzzifier/IntegralDefuzzifier.cpp b/fuzzylite/src/defuzzifier/IntegralDefuzzifier.cpp index 2badf14..3b06b42 100644 --- a/fuzzylite/src/defuzzifier/IntegralDefuzzifier.cpp +++ b/fuzzylite/src/defuzzifier/IntegralDefuzzifier.cpp @@ -1,32 +1,24 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 = 200; + int IntegralDefuzzifier::_defaultResolution = 100; void IntegralDefuzzifier::setDefaultResolution(int defaultResolution) { _defaultResolution = defaultResolution; @@ -37,11 +29,9 @@ namespace fl { } IntegralDefuzzifier::IntegralDefuzzifier(int resolution) - : Defuzzifier(), _resolution(resolution) { - } + : Defuzzifier(), _resolution(resolution) { } - IntegralDefuzzifier::~IntegralDefuzzifier() { - } + IntegralDefuzzifier::~IntegralDefuzzifier() { } void IntegralDefuzzifier::setResolution(int resolution) { this->_resolution = resolution; @@ -50,4 +40,5 @@ namespace fl { int IntegralDefuzzifier::getResolution() const { return this->_resolution; } + } diff --git a/fuzzylite/src/defuzzifier/LargestOfMaximum.cpp b/fuzzylite/src/defuzzifier/LargestOfMaximum.cpp index 470af52..ff05707 100644 --- a/fuzzylite/src/defuzzifier/LargestOfMaximum.cpp +++ b/fuzzylite/src/defuzzifier/LargestOfMaximum.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -30,29 +22,27 @@ namespace fl { LargestOfMaximum::LargestOfMaximum(int resolution) - : IntegralDefuzzifier(resolution) { - } + : IntegralDefuzzifier(resolution) { } - LargestOfMaximum::~LargestOfMaximum() { - } + 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 fl::Op::isFinite(minimum + maximum)) { - return fl::nan; - } - if (maximum - minimum > _resolution) { - FL_DBG("[accuracy warning] the resolution <" << _resolution << "> " - "is smaller than the range <" << minimum << ", " << maximum << ">. In order to " - "improve the accuracy, the resolution should be at least equal to the range."); - } - scalar dx = (maximum - minimum) / _resolution; + 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) { + for (int i = 0; i < resolution; ++i) { x = minimum + (i + 0.5) * dx; y = term->membership(x); diff --git a/fuzzylite/src/defuzzifier/MeanOfMaximum.cpp b/fuzzylite/src/defuzzifier/MeanOfMaximum.cpp index 7c40527..961e505 100644 --- a/fuzzylite/src/defuzzifier/MeanOfMaximum.cpp +++ b/fuzzylite/src/defuzzifier/MeanOfMaximum.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -27,36 +19,33 @@ #include "fl/Exception.h" #include "fl/term/Term.h" - namespace fl { MeanOfMaximum::MeanOfMaximum(int resolution) - : IntegralDefuzzifier(resolution) { - } + : IntegralDefuzzifier(resolution) { } - MeanOfMaximum::~MeanOfMaximum() { - } + 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 fl::Op::isFinite(minimum + maximum)) { - return fl::nan; - } - if (maximum - minimum > _resolution) { - FL_DBG("[accuracy warning] the resolution <" << _resolution << "> " - "is smaller than the range <" << minimum << ", " << maximum << ">. In order to " - "improve the accuracy, the resolution should be at least equal to the range."); - } - scalar dx = (maximum - minimum) / _resolution; + 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) { + for (int i = 0; i < resolution; ++i) { x = minimum + (i + 0.5) * dx; y = term->membership(x); @@ -67,14 +56,14 @@ namespace fl { xlargest = x; samePlateau = true; - } else if (Op::isEq(y, ymax) and samePlateau) { + } else if (samePlateau and Op::isEq(y, ymax)) { xlargest = x; } else if (Op::isLt(y, ymax)) { samePlateau = false; } } - return (xlargest + xsmallest) / 2.0; + return 0.5 * (xlargest + xsmallest); } MeanOfMaximum* MeanOfMaximum::clone() const { diff --git a/fuzzylite/src/defuzzifier/SmallestOfMaximum.cpp b/fuzzylite/src/defuzzifier/SmallestOfMaximum.cpp index 1e67395..7333702 100644 --- a/fuzzylite/src/defuzzifier/SmallestOfMaximum.cpp +++ b/fuzzylite/src/defuzzifier/SmallestOfMaximum.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -27,33 +19,30 @@ #include "fl/Exception.h" #include "fl/term/Term.h" - namespace fl { SmallestOfMaximum::SmallestOfMaximum(int resolution) - : IntegralDefuzzifier(resolution) { - } + : IntegralDefuzzifier(resolution) { } - SmallestOfMaximum::~SmallestOfMaximum() { - } + 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 fl::Op::isFinite(minimum + maximum)) { - return fl::nan; - } - if (maximum - minimum > _resolution) { - FL_DBG("[accuracy warning] the resolution <" << _resolution << "> " - "is smaller than the range <" << minimum << ", " << maximum << ">. In order to " - "improve the accuracy, the resolution should be at least equal to the range."); - } - scalar dx = (maximum - minimum) / _resolution; + 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) { + for (int i = 0; i < resolution; ++i) { x = minimum + (i + 0.5) * dx; y = term->membership(x); @@ -74,4 +63,3 @@ namespace fl { } } - diff --git a/fuzzylite/src/defuzzifier/WeightedAverage.cpp b/fuzzylite/src/defuzzifier/WeightedAverage.cpp index 105c9d4..34af8c9 100644 --- a/fuzzylite/src/defuzzifier/WeightedAverage.cpp +++ b/fuzzylite/src/defuzzifier/WeightedAverage.cpp @@ -1,113 +1,89 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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/Accumulated.h" -#include "fl/term/Activated.h" -#include "fl/norm/Norm.h" -#include "fl/norm/SNorm.h" -#include "fl/norm/TNorm.h" +#include "fl/term/Aggregated.h" #include <map> namespace fl { - WeightedAverage::WeightedAverage(Type type) : WeightedDefuzzifier(type) { - } + WeightedAverage::WeightedAverage(Type type) : WeightedDefuzzifier(type) { } - WeightedAverage::WeightedAverage(const std::string& type) : WeightedDefuzzifier(type) { - } + WeightedAverage::WeightedAverage(const std::string& type) : WeightedDefuzzifier(type) { } - WeightedAverage::~WeightedAverage() { - } + 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 Accumulated* fuzzyOutput = dynamic_cast<const Accumulated*> (term); + const Aggregated* fuzzyOutput = dynamic_cast<const Aggregated*> (term); if (not fuzzyOutput) { std::ostringstream ss; ss << "[defuzzification error]" - << "expected an Accumulated term instead of" - << "<" << term->toString() << ">"; - throw fl::Exception(ss.str(), FL_AT); + << "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; - - if (not fuzzyOutput->getAccumulation()) { - Type type = _type; - for (int i = 0; i < fuzzyOutput->numberOfTerms(); ++i) { - Activated* activated = fuzzyOutput->getTerm(i); - scalar w = activated->getDegree(); - - if (type == Automatic) type = inferType(activated->getTerm()); - - scalar z = (type == TakagiSugeno) - //? activated.getTerm()->membership(fl::nan) Would ensure no Tsukamoto applies, but Inverse Tsukamoto with Functions would not work. - ? activated->getTerm()->membership(w) //Provides Takagi-Sugeno and Inverse Tsukamoto of Functions - : tsukamoto(activated->getTerm(), w, minimum, maximum); - + 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 { - typedef std::map<const Term*, std::vector<Activated*> > TermGroup; - TermGroup groups; - for (int i = 0; i < fuzzyOutput->numberOfTerms(); ++i) { - Activated* value = fuzzyOutput->getTerm(i); - const Term* key = value->getTerm(); - groups[key].push_back(value); - } - TermGroup::const_iterator it = groups.begin(); - Type type = _type; - while (it != groups.end()) { - const Term* activatedTerm = it->first; - scalar accumulatedDegree = 0.0; - for (std::size_t i = 0; i < it->second.size(); ++i) - accumulatedDegree = fuzzyOutput->getAccumulation()->compute( - accumulatedDegree, it->second.at(i)->getDegree()); - - if (type == Automatic) type = inferType(activatedTerm); - - scalar z = (type == TakagiSugeno) - //? activated.getTerm()->membership(fl::nan) Would ensure no Tsukamoto applies, but Inverse Tsukamoto with Functions would not work. - ? activatedTerm->membership(accumulatedDegree) //Provides Takagi-Sugeno and Inverse Tsukamoto of Functions - : tsukamoto(activatedTerm, accumulatedDegree, minimum, maximum); - - sum += accumulatedDegree * z; - weights += accumulatedDegree; - - ++it; + 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; @@ -120,4 +96,5 @@ namespace fl { 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 index 760a5dc..743c59f 100644 --- a/fuzzylite/src/defuzzifier/WeightedDefuzzifier.cpp +++ b/fuzzylite/src/defuzzifier/WeightedDefuzzifier.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -36,24 +28,20 @@ namespace fl { - WeightedDefuzzifier::WeightedDefuzzifier(Type type) : _type(type) { - - } + WeightedDefuzzifier::WeightedDefuzzifier(Type type) : _type(type) { } WeightedDefuzzifier::WeightedDefuzzifier(const std::string& type) { - if (type == "Automatic") _type = Automatic; - else if (type == "TakagiSugeno") _type = TakagiSugeno; - else if (type == "Tsukamoto") _type = Tsukamoto; + if (type == "Automatic") setType(Automatic); + else if (type == "TakagiSugeno") setType(TakagiSugeno); + else if (type == "Tsukamoto") setType(Tsukamoto); else { - _type = Automatic; + setType(Automatic); FL_LOG("[warning] incorrect type <" + type + "> of WeightedDefuzzifier" + " has been defaulted to <Automatic>"); } } - WeightedDefuzzifier::~WeightedDefuzzifier() { - - } + WeightedDefuzzifier::~WeightedDefuzzifier() { } std::string WeightedDefuzzifier::typeName(Type type) { switch (type) { @@ -73,7 +61,7 @@ namespace fl { } std::string WeightedDefuzzifier::getTypeName() const { - return typeName(this->_type); + return typeName(getType()); } WeightedDefuzzifier::Type WeightedDefuzzifier::inferType(const Term* term) const { @@ -85,94 +73,4 @@ namespace fl { return Tsukamoto; } - bool WeightedDefuzzifier::isMonotonic(const Term* term) const { - return (dynamic_cast<const Concave*> (term)) or - (dynamic_cast<const Ramp*> (term)) or - (dynamic_cast<const Sigmoid*> (term)) or - (dynamic_cast<const SShape*> (term)) or - (dynamic_cast<const ZShape*> (term)); - } - - /** - * Instead of computing y=f(x), the goal of Tsukamoto is to find x=f(w), - * where f is monotonic. - */ - scalar WeightedDefuzzifier::tsukamoto(const Term* monotonic, scalar activationDegree, - scalar minimum, scalar maximum) const { - scalar w = activationDegree; - scalar z = fl::nan; //result; - bool isTsukamoto = true; - if (const Ramp* ramp = dynamic_cast<const Ramp*> (monotonic)) { - z = Op::scale(w, 0, 1, ramp->getStart(), ramp->getEnd()); - - } else if (const Sigmoid* sigmoid = dynamic_cast<const Sigmoid*> (monotonic)) { - if (Op::isEq(w, 1.0)) { - if (Op::isGE(sigmoid->getSlope(), 0.0)) { - z = maximum; - } else { - z = minimum; - } - - } else if (Op::isEq(w, 0.0)) { - if (Op::isGE(sigmoid->getSlope(), 0.0)) { - z = minimum; - } else { - z = maximum; - } - } else { - scalar a = sigmoid->getSlope(); - scalar b = sigmoid->getInflection(); - z = b + (std::log(1.0 / w - 1.0) / -a); - } - - } else if (const SShape* sshape = dynamic_cast<const SShape*> (monotonic)) { - scalar difference = sshape->getEnd() - sshape->getStart(); - scalar a = sshape->getStart() + std::sqrt(w * difference * difference / 2.0); - scalar b = sshape->getEnd() + std::sqrt(difference * difference * (w - 1.0) / -2.0); - if (std::fabs(w - monotonic->membership(a)) < - std::fabs(w - monotonic->membership(b))) { - z = a; - } else { - z = b; - } - - } else if (const ZShape* zshape = dynamic_cast<const ZShape*> (monotonic)) { - scalar difference = zshape->getEnd() - zshape->getStart(); - scalar a = zshape->getStart() + std::sqrt(difference * difference * (w - 1.0) / -2.0); - scalar b = zshape->getEnd() + std::sqrt(w * difference * difference / 2.0); - if (std::fabs(w - monotonic->membership(a)) < - std::fabs(w - monotonic->membership(b))) { - z = a; - } else { - z = b; - } - - } else if (const Concave* concave = dynamic_cast<const Concave*> (monotonic)) { - scalar i = concave->getInflection(); - scalar e = concave->getEnd(); - z = (i - e) / concave->membership(w) + 2 * e - i; - } else { - isTsukamoto = false; - } - - if (isTsukamoto) { - //Compare difference between estimated and true value - scalar fz = monotonic->membership(z); - if (not Op::isEq(w, fz, 1e-2)) { - FL_DBG("[tsukamoto warning] difference <" << Op::str(std::abs(w - fz)) << "> " - "might suggest an inaccurate computation of z because it is " - "expected w=f(z) in " << monotonic->className() << - " term <" << monotonic->getName() << ">, but " - "w=" << w << " " - "f(z)=" << fz << " and " - "z=" << Op::str(z)); - } - } else { - // else fallback to the regular Takagi-Sugeno or inverse Tsukamoto (according to term) - z = monotonic->membership(w); - } - return z; - } - - } diff --git a/fuzzylite/src/defuzzifier/WeightedSum.cpp b/fuzzylite/src/defuzzifier/WeightedSum.cpp index fb3e2e3..6c2343d 100644 --- a/fuzzylite/src/defuzzifier/WeightedSum.cpp +++ b/fuzzylite/src/defuzzifier/WeightedSum.cpp @@ -1,110 +1,86 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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/Accumulated.h" -#include "fl/term/Activated.h" -#include "fl/norm/SNorm.h" -#include "fl/norm/TNorm.h" +#include "fl/term/Aggregated.h" #include <map> + namespace fl { - WeightedSum::WeightedSum(Type type) : WeightedDefuzzifier(type) { - } + WeightedSum::WeightedSum(Type type) : WeightedDefuzzifier(type) { } - WeightedSum::WeightedSum(const std::string& type) : WeightedDefuzzifier(type) { + WeightedSum::WeightedSum(const std::string& type) : WeightedDefuzzifier(type) { } - } - - WeightedSum::~WeightedSum() { - } + 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 Accumulated* fuzzyOutput = dynamic_cast<const Accumulated*> (term); + const Aggregated* fuzzyOutput = dynamic_cast<const Aggregated*> (term); if (not fuzzyOutput) { std::ostringstream ss; ss << "[defuzzification error]" - << "expected an Accumulated term instead of" - << "<" << term->toString() << ">"; - throw fl::Exception(ss.str(), FL_AT); + << "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; - - if (not fuzzyOutput->getAccumulation()) { - Type type = _type; - for (int i = 0; i < fuzzyOutput->numberOfTerms(); ++i) { - Activated* activated = fuzzyOutput->getTerm(i); - scalar w = activated->getDegree(); - - if (type == Automatic) type = inferType(activated->getTerm()); - - scalar z = (type == TakagiSugeno) - //? activated.getTerm()->membership(fl::nan) Would ensure no Tsukamoto applies, but Inverse Tsukamoto with Functions would not work. - ? activated->getTerm()->membership(w) //Provides Takagi-Sugeno and Inverse Tsukamoto of Functions - : tsukamoto(activated->getTerm(), w, minimum, maximum); - + 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 { - typedef std::map<const Term*, std::vector<Activated*> > TermGroup; - TermGroup groups; - for (int i = 0; i < fuzzyOutput->numberOfTerms(); ++i) { - Activated* value = fuzzyOutput->getTerm(i); - const Term* key = value->getTerm(); - groups[key].push_back(value); - } - TermGroup::const_iterator it = groups.begin(); - Type type = _type; - while (it != groups.end()) { - const Term* activatedTerm = it->first; - scalar accumulatedDegree = 0.0; - for (std::size_t i = 0; i < it->second.size(); ++i) - accumulatedDegree = fuzzyOutput->getAccumulation()->compute( - accumulatedDegree, it->second.at(i)->getDegree()); - - if (type == Automatic) type = inferType(activatedTerm); - - scalar z = (type == TakagiSugeno) - //? activated.getTerm()->membership(fl::nan) Would ensure no Tsukamoto applies, but Inverse Tsukamoto with Functions would not work. - ? activatedTerm->membership(accumulatedDegree) //Provides Takagi-Sugeno and Inverse Tsukamoto of Functions - : tsukamoto(activatedTerm, accumulatedDegree, minimum, maximum); - - sum += accumulatedDegree * z; - - ++it; + 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; 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/CloningFactory.cpp b/fuzzylite/src/factory/CloningFactory.cpp deleted file mode 100644 index b187171..0000000 --- a/fuzzylite/src/factory/CloningFactory.cpp +++ /dev/null @@ -1,135 +0,0 @@ -/* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved - - This file is part of fuzzylite. - - fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. - - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. - - */ - -#include "fl/factory/CloningFactory.h" - -#include "fl/Exception.h" -#include "fl/term/Function.h" - -namespace fl { - - template<typename T> - CloningFactory<T>::CloningFactory(const std::string& name) : _name(name) { - - } - - template<typename T> - 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> - 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> - 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> - std::string CloningFactory<T>::name() const { - return this->_name; - } - - template<typename T> - void CloningFactory<T>::registerObject(const std::string& key, T object) { - this->_objects[key] = object; - } - - template<typename T> - 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> - 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> - 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> - 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 fl::Exception("[cloning error] " + _name + " object by name <" + key + "> not registered", FL_AT); - } - - template<typename T> - 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 class fl::CloningFactory<fl::Function::Element*>; -} - - diff --git a/fuzzylite/src/factory/ConstructionFactory.cpp b/fuzzylite/src/factory/ConstructionFactory.cpp deleted file mode 100644 index b316e52..0000000 --- a/fuzzylite/src/factory/ConstructionFactory.cpp +++ /dev/null @@ -1,114 +0,0 @@ -/* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved - - This file is part of fuzzylite. - - fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. - - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. - - */ - -#include "fl/factory/ConstructionFactory.h" - -#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/Function.h" -#include "fl/term/Term.h" - - - -namespace fl { - - template <typename T> - ConstructionFactory<T>::ConstructionFactory(const std::string& name) : _name(name) { - - } - - template <typename T> - ConstructionFactory<T>::~ConstructionFactory() { - } - - template<typename T> - std::string ConstructionFactory<T>::name() const { - return this->_name; - } - - template <typename T> - void ConstructionFactory<T>::registerConstructor(const std::string& key, Constructor constructor) { - this->_constructors[key] = constructor; - } - - template <typename T> - 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> - 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> - 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> - 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 fl::Exception(ss.str(), FL_AT); - } - - template <typename T> - 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 class ConstructionFactory<Defuzzifier*>; - template class ConstructionFactory<Hedge*>; - template class ConstructionFactory<SNorm*>; - template class ConstructionFactory<TNorm*>; - template class ConstructionFactory<Term*>; -} - - - diff --git a/fuzzylite/src/factory/DefuzzifierFactory.cpp b/fuzzylite/src/factory/DefuzzifierFactory.cpp index 8d89c2d..3b0bfb1 100644 --- a/fuzzylite/src/factory/DefuzzifierFactory.cpp +++ b/fuzzylite/src/factory/DefuzzifierFactory.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -30,7 +22,9 @@ #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 { @@ -42,12 +36,12 @@ namespace fl { 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() { - - } + DefuzzifierFactory::~DefuzzifierFactory() { } Defuzzifier* DefuzzifierFactory::constructDefuzzifier(const std::string& key, int resolution, WeightedDefuzzifier::Type type) const { @@ -75,4 +69,5 @@ namespace fl { } return result; } + } diff --git a/fuzzylite/src/factory/FactoryManager.cpp b/fuzzylite/src/factory/FactoryManager.cpp index c2374dc..367d982 100644 --- a/fuzzylite/src/factory/FactoryManager.cpp +++ b/fuzzylite/src/factory/FactoryManager.cpp @@ -1,60 +1,46 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" -#include "fl/factory/DefuzzifierFactory.h" -#include "fl/factory/FunctionFactory.h" -#include "fl/factory/HedgeFactory.h" -#include "fl/factory/SNormFactory.h" -#include "fl/factory/TermFactory.h" -#include "fl/factory/TNormFactory.h" - namespace fl { - FactoryManager FactoryManager::_instance; - FactoryManager* FactoryManager::instance() { + static FL_ITHREAD_LOCAL FactoryManager _instance; return &_instance; } FactoryManager::FactoryManager() : - _tnorm(new TNormFactory), _snorm(new SNormFactory), _defuzzifier(new DefuzzifierFactory), - _term(new TermFactory), _hedge(new HedgeFactory), _function(new FunctionFactory) { - } + _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, - DefuzzifierFactory* defuzzifier, TermFactory* term, - HedgeFactory* hedge, FunctionFactory* function) : - _tnorm(tnorm), _snorm(snorm), _defuzzifier(defuzzifier), _term(term), _hedge(hedge), - _function(function) { - } + 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), _defuzzifier(fl::null), _term(fl::null), _hedge(fl::null), _function(fl::null) { + : _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())); @@ -63,8 +49,17 @@ namespace fl { 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())); @@ -73,8 +68,7 @@ namespace fl { return *this; } - FactoryManager::~FactoryManager() { - } + FactoryManager::~FactoryManager() { } void FactoryManager::setTnorm(TNormFactory* tnorm) { this->_tnorm.reset(tnorm); @@ -92,6 +86,14 @@ namespace fl { 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); } diff --git a/fuzzylite/src/factory/FunctionFactory.cpp b/fuzzylite/src/factory/FunctionFactory.cpp index 3c0718d..8e52ddf 100644 --- a/fuzzylite/src/factory/FunctionFactory.cpp +++ b/fuzzylite/src/factory/FunctionFactory.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -33,125 +25,130 @@ namespace fl { registerFunctions(); } - FunctionFactory::~FunctionFactory() { - - } + FunctionFactory::~FunctionFactory() { } void FunctionFactory::registerOperators() { //OPERATORS: int p = 100; //First order: not, negate: registerObject("!", new Function::Element("!", "Logical NOT", - Function::Element::OPERATOR, &(fl::Op::logicalNot), p, 1)); //logical not + Function::Element::Operator, &(Op::logicalNot), p, 1)); //logical not registerObject("~", new Function::Element("~", "Negation", - Function::Element::OPERATOR, &(fl::Op::negate), p, 1)); // ~ negates a number + 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)); + Function::Element::Operator, &(std::pow), p, 1)); p -= 10; //Third order: multiplication, division, modulo registerObject("*", new Function::Element("*", "Multiplication", - Function::Element::OPERATOR, &(fl::Op::multiply), p)); + Function::Element::Operator, &(Op::multiply), p)); registerObject("/", new Function::Element("/", "Division", - Function::Element::OPERATOR, &(fl::Op::divide), p)); + Function::Element::Operator, &(Op::divide), p)); registerObject("%", new Function::Element("%", "Modulo", - Function::Element::OPERATOR, &(fl::Op::modulo), p)); + Function::Element::Operator, &(Op::modulo), p)); p -= 10; //Fourth order: addition, subtraction registerObject("+", new Function::Element("+", "Addition", - Function::Element::OPERATOR, &(fl::Op::add), p)); + Function::Element::Operator, &(Op::add), p)); registerObject("-", new Function::Element("-", "Subtraction", - Function::Element::OPERATOR, &(fl::Op::subtract), p)); + Function::Element::Operator, &(Op::subtract), p)); //Fifth order: logical and, logical or p -= 10; //Logical AND - registerObject(fl::Rule::andKeyword(), new Function::Element(fl::Rule::andKeyword(), "Logical AND", - Function::Element::OPERATOR, &(fl::Op::logicalAnd), p)); + registerObject(Rule::andKeyword(), new Function::Element(Rule::andKeyword(), "Logical AND", + Function::Element::Operator, &(Op::logicalAnd), p)); p -= 10; //Logical OR - registerObject(fl::Rule::orKeyword(), new Function::Element(fl::Rule::orKeyword(), "Logical OR", - Function::Element::OPERATOR, &(fl::Op::logicalOr), p)); + 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, &(fl::Op::gt))); + Function::Element::Function, &(Op::gt))); registerObject("ge", new Function::Element("ge", "Greater than or equal to (>=)", - Function::Element::FUNCTION, &(fl::Op::ge))); + Function::Element::Function, &(Op::ge))); registerObject("eq", new Function::Element("eq", "Equal to (==)", - Function::Element::FUNCTION, &(fl::Op::eq))); + Function::Element::Function, &(Op::eq))); registerObject("neq", new Function::Element("neq", "Not equal to (!=)", - Function::Element::FUNCTION, &(fl::Op::neq))); + Function::Element::Function, &(Op::neq))); registerObject("le", new Function::Element("le", "Less than or equal to (<=)", - Function::Element::FUNCTION, &(fl::Op::le))); + Function::Element::Function, &(Op::le))); registerObject("lt", new Function::Element("lt", "Less than (<)", - Function::Element::FUNCTION, &(fl::Op::lt))); + 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))); + Function::Element::Function, &(std::acos))); registerObject("asin", new Function::Element("asin", "Inverse sine", - Function::Element::FUNCTION, &(std::asin))); + Function::Element::Function, &(std::asin))); registerObject("atan", new Function::Element("atan", "Inverse tangent", - Function::Element::FUNCTION, &(std::atan))); + Function::Element::Function, &(std::atan))); registerObject("ceil", new Function::Element("ceil", "Ceiling", - Function::Element::FUNCTION, &(std::ceil))); + Function::Element::Function, &(std::ceil))); registerObject("cos", new Function::Element("cos", "Cosine", - Function::Element::FUNCTION, &(std::cos))); + Function::Element::Function, &(std::cos))); registerObject("cosh", new Function::Element("cosh", "Hyperbolic cosine", - Function::Element::FUNCTION, &(std::cosh))); + Function::Element::Function, &(std::cosh))); registerObject("exp", new Function::Element("exp", "Exponential", - Function::Element::FUNCTION, &(std::exp))); + 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))); + Function::Element::Function, &(std::fabs))); registerObject("floor", new Function::Element("floor", "Floor", - Function::Element::FUNCTION, &(std::floor))); + Function::Element::Function, &(std::floor))); registerObject("log", new Function::Element("log", "Natural logarithm", - Function::Element::FUNCTION, &(std::log))); + Function::Element::Function, &(std::log))); registerObject("log10", new Function::Element("log10", "Common logarithm", - Function::Element::FUNCTION, &(std::log10))); + Function::Element::Function, &(std::log10))); registerObject("round", new Function::Element("round", "Round", - Function::Element::FUNCTION, &(fl::Op::round))); + Function::Element::Function, &(Op::round))); registerObject("sin", new Function::Element("sin", "Sine", - Function::Element::FUNCTION, &(std::sin))); + Function::Element::Function, &(std::sin))); registerObject("sinh", new Function::Element("sinh", "Hyperbolic sine", - Function::Element::FUNCTION, &(std::sinh))); + Function::Element::Function, &(std::sinh))); registerObject("sqrt", new Function::Element("sqrt", "Square root", - Function::Element::FUNCTION, &(std::sqrt))); + Function::Element::Function, &(std::sqrt))); registerObject("tan", new Function::Element("tan", "Tangent", - Function::Element::FUNCTION, &(std::tan))); + Function::Element::Function, &(std::tan))); registerObject("tanh", new Function::Element("tanh", "Hyperbolic tangent", - Function::Element::FUNCTION, &(std::tanh))); + 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))); + Function::Element::Function, &(log1p))); registerObject("acosh", new Function::Element("acosh", "Inverse hyperbolic cosine", - Function::Element::FUNCTION, &(acosh))); + Function::Element::Function, &(acosh))); registerObject("asinh", new Function::Element("asinh", "Inverse hyperbolic sine", - Function::Element::FUNCTION, &(asinh))); + Function::Element::Function, &(asinh))); registerObject("atanh", new Function::Element("atanh", "Inverse hyperbolic tangent", - Function::Element::FUNCTION, &(atanh))); + Function::Element::Function, &(atanh))); #endif registerObject("pow", new Function::Element("pow", "Power", - Function::Element::FUNCTION, &(std::pow))); + Function::Element::Function, &(std::pow))); registerObject("atan2", new Function::Element("atan2", "Inverse tangent (y,x)", - Function::Element::FUNCTION, &(std::atan2))); + Function::Element::Function, &(std::atan2))); registerObject("fmod", new Function::Element("fmod", "Floating-point remainder", - Function::Element::FUNCTION, &(std::fmod))); + 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) + 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; } @@ -160,9 +157,9 @@ namespace fl { 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) + 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; } diff --git a/fuzzylite/src/factory/HedgeFactory.cpp b/fuzzylite/src/factory/HedgeFactory.cpp index ea5f77a..1a6452c 100644 --- a/fuzzylite/src/factory/HedgeFactory.cpp +++ b/fuzzylite/src/factory/HedgeFactory.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -31,7 +23,6 @@ #include "fl/hedge/Somewhat.h" #include "fl/hedge/Very.h" - namespace fl { HedgeFactory::HedgeFactory() : ConstructionFactory<Hedge*>("Hedge") { @@ -44,8 +35,6 @@ namespace fl { registerConstructor(Very().name(), &(Very::constructor)); } - HedgeFactory::~HedgeFactory() { - - } + HedgeFactory::~HedgeFactory() { } } diff --git a/fuzzylite/src/factory/SNormFactory.cpp b/fuzzylite/src/factory/SNormFactory.cpp index 6e42551..fdaef87 100644 --- a/fuzzylite/src/factory/SNormFactory.cpp +++ b/fuzzylite/src/factory/SNormFactory.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -32,6 +24,7 @@ #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 { @@ -45,11 +38,9 @@ namespace fl { registerConstructor(Maximum().className(), &(Maximum::constructor)); registerConstructor(NilpotentMaximum().className(), &(NilpotentMaximum::constructor)); registerConstructor(NormalizedSum().className(), &(NormalizedSum::constructor)); + registerConstructor(UnboundedSum().className(), &(UnboundedSum::constructor)); } - SNormFactory::~SNormFactory() { - - } - + SNormFactory::~SNormFactory() { } } diff --git a/fuzzylite/src/factory/TNormFactory.cpp b/fuzzylite/src/factory/TNormFactory.cpp index 95628cc..0dda02e 100644 --- a/fuzzylite/src/factory/TNormFactory.cpp +++ b/fuzzylite/src/factory/TNormFactory.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -45,9 +37,6 @@ namespace fl { registerConstructor(NilpotentMinimum().className(), &(NilpotentMinimum::constructor)); } - TNormFactory::~TNormFactory() { - - } - + TNormFactory::~TNormFactory() { } } diff --git a/fuzzylite/src/factory/TermFactory.cpp b/fuzzylite/src/factory/TermFactory.cpp index 10f3c15..e135704 100644 --- a/fuzzylite/src/factory/TermFactory.cpp +++ b/fuzzylite/src/factory/TermFactory.cpp @@ -1,32 +1,23 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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/Exception.h" -#include "fl/term/Term.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" @@ -52,6 +43,7 @@ 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)); @@ -73,9 +65,6 @@ namespace fl { registerConstructor(ZShape().className(), &(ZShape::constructor)); } - TermFactory::~TermFactory() { - - } - + TermFactory::~TermFactory() { } } diff --git a/fuzzylite/src/fuzzylite.cpp b/fuzzylite/src/fuzzylite.cpp index 06b635a..db17fec 100644 --- a/fuzzylite/src/fuzzylite.cpp +++ b/fuzzylite/src/fuzzylite.cpp @@ -1,73 +1,31 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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::_debug = false; + bool fuzzylite::_debugging = false; bool fuzzylite::_logging = true; - std::string fuzzylite::name() { - return "fuzzylite"; - } - - std::string fuzzylite::fullname() { - return name() + "-" + longVersion(); - } - - std::string fuzzylite::version() { - return FL_VERSION; - } - - std::string fuzzylite::longVersion() { - return FL_VERSION "b" FL_DATE; - } - - std::string fuzzylite::license() { - return "GNU Lesser General Public License v3.0"; - } - - std::string fuzzylite::author() { - return "Juan Rada-Vilela, Ph.D."; - } - - std::string fuzzylite::company() { - return "FuzzyLite Limited"; - } - - std::string fuzzylite::website() { - return "http://www.fuzzylite.com/"; - } - - std::string fuzzylite::date() { - return FL_DATE; - } - - std::string fuzzylite::platform() { + std::string platform() { #ifdef FL_UNIX return "Unix"; #elif defined FL_WINDOWS @@ -77,9 +35,9 @@ namespace fl { #endif } - std::string fuzzylite::floatingPoint() { + std::string floatingPoint() { scalar someScalar = 0; - (void) someScalar; + FL_IUNUSED(someScalar); std::string type; std::ostringstream ss; @@ -92,37 +50,4 @@ namespace fl { sizeof (someScalar) << " bytes"; return ss.str(); } - - void fuzzylite::setDebug(bool debug) { - _debug = debug; - } - - bool fuzzylite::debug() { - return _debug; - } - - void fuzzylite::setDecimals(int decimals) { - _decimals = decimals; - } - - int fuzzylite::decimals() { - return _decimals; - } - - void fuzzylite::setMachEps(scalar macheps) { - _macheps = macheps; - } - - scalar fuzzylite::macheps() { - return _macheps; - } - - void fuzzylite::setLogging(bool logging) { - _logging = logging; - } - - bool fuzzylite::logging() { - return _logging; - } - } diff --git a/fuzzylite/src/hedge/Any.cpp b/fuzzylite/src/hedge/Any.cpp index 72724f1..9d04262 100644 --- a/fuzzylite/src/hedge/Any.cpp +++ b/fuzzylite/src/hedge/Any.cpp @@ -1,43 +1,37 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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() { } - Any::~Any() { - } + Any::~Any() { } std::string Any::name() const { return "any"; } + Complexity Any::complexity() const { + return Complexity(); + } + scalar Any::hedge(scalar x) const { - (void) x; + FL_IUNUSED(x); return 1.0; } @@ -50,3 +44,4 @@ namespace fl { } } + diff --git a/fuzzylite/src/hedge/Extremely.cpp b/fuzzylite/src/hedge/Extremely.cpp index eabbb60..60d3abd 100644 --- a/fuzzylite/src/hedge/Extremely.cpp +++ b/fuzzylite/src/hedge/Extremely.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -32,10 +24,14 @@ namespace fl { 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); + : (1.0 - 2.0 * (1.0 - x) * (1.0 - x)); } Extremely* Extremely::clone() const { @@ -46,5 +42,5 @@ namespace fl { 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 index 531213f..defbabd 100644 --- a/fuzzylite/src/hedge/Not.cpp +++ b/fuzzylite/src/hedge/Not.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -30,6 +22,10 @@ namespace fl { return "not"; } + Complexity Not::complexity() const { + return Complexity().arithmetic(1); + } + scalar Not::hedge(scalar x) const { return 1.0 - x; } @@ -42,5 +38,4 @@ namespace fl { return new Not; } - } diff --git a/fuzzylite/src/hedge/Seldom.cpp b/fuzzylite/src/hedge/Seldom.cpp index d87b48a..092e873 100644 --- a/fuzzylite/src/hedge/Seldom.cpp +++ b/fuzzylite/src/hedge/Seldom.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -32,10 +24,14 @@ namespace fl { 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(x / 2.0) - : 1.0 - std::sqrt((1.0 - x) / 2.0); + ? std::sqrt(0.5 * x) + : (1.0 - std::sqrt(0.5 * (1.0 - x))); } Seldom* Seldom::clone() const { @@ -46,5 +42,4 @@ namespace fl { return new Seldom; } - } diff --git a/fuzzylite/src/hedge/Somewhat.cpp b/fuzzylite/src/hedge/Somewhat.cpp index 16c371b..4d68c20 100644 --- a/fuzzylite/src/hedge/Somewhat.cpp +++ b/fuzzylite/src/hedge/Somewhat.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -30,6 +22,10 @@ namespace fl { return "somewhat"; } + Complexity Somewhat::complexity() const { + return Complexity().function(1); + } + scalar Somewhat::hedge(scalar x) const { return std::sqrt(x); } diff --git a/fuzzylite/src/hedge/Very.cpp b/fuzzylite/src/hedge/Very.cpp index dc1fbec..5ac7a86 100644 --- a/fuzzylite/src/hedge/Very.cpp +++ b/fuzzylite/src/hedge/Very.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -30,6 +22,10 @@ namespace fl { return "very"; } + Complexity Very::complexity() const { + return Complexity().arithmetic(1); + } + scalar Very::hedge(scalar x) const { return x * x; } diff --git a/fuzzylite/src/imex/CppExporter.cpp b/fuzzylite/src/imex/CppExporter.cpp index 7b21087..8a77c0c 100644 --- a/fuzzylite/src/imex/CppExporter.cpp +++ b/fuzzylite/src/imex/CppExporter.cpp @@ -1,75 +1,73 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" -#include <algorithm> - namespace fl { - CppExporter::CppExporter(bool prefixNamespace) : Exporter(), - _prefixNamespace(prefixNamespace) { - } + CppExporter::CppExporter(bool prefixNamespace, bool usingVariableNames) : Exporter(), + _usingNamespace(prefixNamespace), _usingVariableNames(usingVariableNames) { } - CppExporter::~CppExporter() { - } + CppExporter::~CppExporter() { } std::string CppExporter::name() const { return "CppExporter"; } std::string CppExporter::fl(const std::string& clazz) const { - return _prefixNamespace ? "fl::" + clazz : clazz; + return _usingNamespace ? "fl::" + clazz : clazz; + } + + void CppExporter::setUsingNamespace(bool usingNamespace) { + this->_usingNamespace = usingNamespace; } - void CppExporter::setPrefixNamespace(bool prefixNamespace){ - this->_prefixNamespace = prefixNamespace; + bool CppExporter::isUsingNamespace() const { + return this->_usingNamespace; } - bool CppExporter::isPrefixNamespace() const{ - return this->_prefixNamespace; + 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; - if (not _prefixNamespace) cpp << "using namespace fl;\n\n"; + 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 (int i = 0; i < engine->numberOfInputVariables(); ++i) { + for (std::size_t i = 0; i < engine->numberOfInputVariables(); ++i) { cpp << toString(engine->getInputVariable(i), engine) << "\n"; } - for (int i = 0; i < engine->numberOfOutputVariables(); ++i) { + for (std::size_t i = 0; i < engine->numberOfOutputVariables(); ++i) { cpp << toString(engine->getOutputVariable(i), engine) << "\n"; } - for (int i = 0; i < engine->numberOfRuleBlocks(); ++i) { + for (std::size_t i = 0; i < engine->numberOfRuleBlocks(); ++i) { cpp << toString(engine->getRuleBlock(i), engine) << "\n"; } @@ -77,21 +75,28 @@ namespace fl { } std::string CppExporter::toString(const InputVariable* inputVariable, const Engine* engine) const { - std::ostringstream ss; - std::string name = "inputVariable"; - if (engine->numberOfInputVariables() > 1) { - int index = std::distance(engine->inputVariables().begin(), - std::find(engine->inputVariables().begin(), - engine->inputVariables().end(), inputVariable)); - name += Op::str<int>(index + 1); + 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 << "->setEnabled(" << (inputVariable->isEnabled() ? "true" : "false") << ");\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"; - for (int t = 0; t < inputVariable->numberOfTerms(); ++t) { + 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"; @@ -99,58 +104,73 @@ namespace fl { } std::string CppExporter::toString(const OutputVariable* outputVariable, const Engine* engine) const { - std::ostringstream ss; - std::string name = "outputVariable"; - if (engine->numberOfOutputVariables() > 1) { - int index = std::distance(engine->outputVariables().begin(), - std::find(engine->outputVariables().begin(), - engine->outputVariables().end(), outputVariable)); - name += Op::str<int>(index + 1); + 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 << "->setEnabled(" << (outputVariable->isEnabled() ? "true" : "false") << ");\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 << "->fuzzyOutput()->setAccumulation(" << - toString(outputVariable->fuzzyOutput()->getAccumulation()) << ");\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 << "->setLockPreviousOutputValue(" << - (outputVariable->isLockedPreviousOutputValue() ? "true" : "false") << ");\n"; - ss << name << "->setLockOutputValueInRange(" << - (outputVariable->isLockedOutputValueInRange() ? "true" : "false") << ");\n"; - for (int t = 0; t < outputVariable->numberOfTerms(); ++t) { + 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(); } - //TODO: addRules using `new Rule` instead of `Rule::parse` in version 6.0 std::string CppExporter::toString(const RuleBlock* ruleBlock, const Engine* engine) const { - std::ostringstream ss; - std::string name = "ruleBlock"; - if (engine->numberOfRuleBlocks() > 1) { - int index = std::distance(engine->ruleBlocks().begin(), - std::find(engine->ruleBlocks().begin(), - engine->ruleBlocks().end(), ruleBlock)); - name += Op::str<int>(index + 1); + 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 << "->setEnabled(" << (ruleBlock->isEnabled() ? "true" : "false") << ");\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 (int r = 0; r < ruleBlock->numberOfRules(); ++r) { - ss << name << "->addRule(" << "fl::Rule::parse(\"" << + 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"; @@ -158,12 +178,12 @@ namespace fl { } std::string CppExporter::toString(scalar value) const { - if (fl::Op::isNaN(value)) + if (Op::isNaN(value)) return "fl::nan"; - if (fl::Op::isInf(value)){ - return (value > 0 ? "fl::inf" : "-fl::inf"); + if (Op::isInf(value)) { + return (value > 0 ? "fl::inf" : "-fl::inf"); } - return fl::Op::str(value); + return Op::str(value); } std::string CppExporter::toString(const Term* term) const { @@ -172,8 +192,8 @@ namespace fl { if (const Discrete * discrete = dynamic_cast<const Discrete*> (term)) { std::ostringstream ss; ss << fl(term->className()) << "::create(\"" << term->getName() << "\", " - << discrete->xy().size() * 2 << ", " - << fl::Op::join(Discrete::toVector(discrete->xy()), ", ") << ")"; + << (discrete->xy().size() * 2) << ", " + << Op::join(Discrete::toVector(discrete->xy()), ", ") << ")"; return ss.str(); } @@ -187,7 +207,7 @@ namespace fl { if (const Linear * linear = dynamic_cast<const Linear*> (term)) { std::ostringstream ss; ss << fl(term->className()) << "::create(\"" << term->getName() << "\", " - << "engine, " << fl::Op::join(linear->coefficients(), ", ") << ")"; + << "engine, " << Op::join(linear->coefficients(), ", ") << ")"; return ss.str(); } @@ -217,7 +237,7 @@ namespace fl { if (const IntegralDefuzzifier * integralDefuzzifier = dynamic_cast<const IntegralDefuzzifier*> (defuzzifier)) { return "new " + fl(integralDefuzzifier->className()) + "(" - + fl::Op::str(integralDefuzzifier->getResolution()) + ")"; + + Op::str(integralDefuzzifier->getResolution()) + ")"; } if (const WeightedDefuzzifier * weightedDefuzzifier = dynamic_cast<const WeightedDefuzzifier*> (defuzzifier)) { @@ -227,6 +247,19 @@ namespace fl { 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 index 5b0fe1b..1aaf24e 100644 --- a/fuzzylite/src/imex/Exporter.cpp +++ b/fuzzylite/src/imex/Exporter.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -29,18 +21,14 @@ namespace fl { - Exporter::Exporter() { - - } - - Exporter::~Exporter() { + 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 fl::Exception("[file error] file <" + path + "> could not be created", FL_AT); + 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 index cf8ffb0..98f1177 100644 --- a/fuzzylite/src/imex/FclExporter.cpp +++ b/fuzzylite/src/imex/FclExporter.cpp @@ -1,39 +1,28 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" -#include <sstream> - namespace fl { - FclExporter::FclExporter(const std::string& indent) : Exporter(), _indent(indent) { - } + FclExporter::FclExporter(const std::string& indent) : Exporter(), _indent(indent) { } - FclExporter::~FclExporter() { - } + FclExporter::~FclExporter() { } void FclExporter::setIndent(const std::string& indent) { this->_indent = indent; @@ -49,31 +38,32 @@ namespace fl { 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 (int i = 0; i < engine->numberOfInputVariables(); ++i) { + 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 (int i = 0; i < engine->numberOfOutputVariables(); ++i) { + 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 (int i = 0; i < engine->numberOfInputVariables(); ++i) { + for (std::size_t i = 0; i < engine->numberOfInputVariables(); ++i) { InputVariable* inputVariable = engine->getInputVariable(i); fcl << toString(inputVariable) << "\n"; } - for (int i = 0; i < engine->numberOfOutputVariables(); ++i) { + for (std::size_t i = 0; i < engine->numberOfOutputVariables(); ++i) { OutputVariable* outputVariable = engine->getOutputVariable(i); fcl << toString(outputVariable) << "\n"; } - for (int i = 0; i < engine->numberOfRuleBlocks(); ++i) { + for (std::size_t i = 0; i < engine->numberOfRuleBlocks(); ++i) { RuleBlock* ruleblock = engine->getRuleBlock(i); fcl << toString(ruleblock) << "\n"; } @@ -85,15 +75,11 @@ namespace fl { std::string FclExporter::toString(const InputVariable* inputVariable) const { std::ostringstream fcl; fcl << "FUZZIFY " << Op::validName(inputVariable->getName()) << "\n"; - if (not inputVariable->isEnabled()) { - fcl << _indent << "ENABLED : " << - (inputVariable->isEnabled() ? "TRUE" : "FALSE") << ";\n"; - } - fcl << _indent << "RANGE := (" << fl::Op::join(2, " .. ", + fcl << _indent << "RANGE := (" << Op::join(2, " .. ", inputVariable->getMinimum(), inputVariable->getMaximum()) << ");\n"; - for (int t = 0; t < inputVariable->numberOfTerms(); ++t) { + 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"; @@ -105,15 +91,11 @@ namespace fl { std::string FclExporter::toString(const OutputVariable* outputVariable) const { std::ostringstream fcl; fcl << "DEFUZZIFY " << Op::validName(outputVariable->getName()) << "\n"; - if (not outputVariable->isEnabled()) { - fcl << _indent << "ENABLED : " << - (outputVariable->isEnabled() ? "TRUE" : "FALSE") << ";\n"; - } - fcl << _indent << "RANGE := (" << fl::Op::join(2, " .. ", + fcl << _indent << "RANGE := (" << Op::join(2, " .. ", outputVariable->getMinimum(), outputVariable->getMaximum()) << ");\n"; - for (int t = 0; t < outputVariable->numberOfTerms(); ++t) { + 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"; @@ -121,19 +103,15 @@ namespace fl { if (outputVariable->getDefuzzifier()) { fcl << _indent << "METHOD : " << toString(outputVariable->getDefuzzifier()) << ";\n"; } - if (outputVariable->fuzzyOutput()->getAccumulation()) - fcl << _indent << "ACCU : " << toString(outputVariable->fuzzyOutput()->getAccumulation()) << ";\n"; + if (outputVariable->fuzzyOutput()->getAggregation()) + fcl << _indent << "ACCU : " << toString(outputVariable->fuzzyOutput()->getAggregation()) << ";\n"; - fcl << _indent << "DEFAULT := " << fl::Op::str(outputVariable->getDefaultValue()); - if (outputVariable->isLockedPreviousOutputValue()) { + fcl << _indent << "DEFAULT := " << Op::str(outputVariable->getDefaultValue()); + if (outputVariable->isLockPreviousValue()) { fcl << " | NC"; } fcl << ";\n"; - if (outputVariable->isLockedOutputValueInRange()) { - fcl << _indent << "LOCK : RANGE;\n"; - } - fcl << "END_DEFUZZIFY\n"; return fcl.str(); } @@ -141,28 +119,24 @@ namespace fl { std::string FclExporter::toString(const RuleBlock* ruleBlock) const { std::ostringstream fcl; fcl << "RULEBLOCK " << ruleBlock->getName() << "\n"; - if (not ruleBlock->isEnabled()) { - fcl << _indent << "ENABLED : " << - (ruleBlock->isEnabled() ? "TRUE" : "FALSE") << ";\n"; - } if (ruleBlock->getConjunction()) fcl << _indent << "AND : " << toString(ruleBlock->getConjunction()) << ";\n"; if (ruleBlock->getDisjunction()) fcl << _indent << "OR : " << toString(ruleBlock->getDisjunction()) << ";\n"; - if (ruleBlock->getActivation()) - fcl << _indent << "ACT : " << toString(ruleBlock->getActivation()) << ";\n"; + if (ruleBlock->getImplication()) + fcl << _indent << "ACT : " << toString(ruleBlock->getImplication()) << ";\n"; - for (int r = 0; r < ruleBlock->numberOfRules(); ++r) { + 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{ + + 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"; @@ -172,7 +146,7 @@ namespace fl { 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"; @@ -182,37 +156,8 @@ namespace fl { if (name == EinsteinSum().className()) return "ESUM"; if (name == HamacherSum().className()) return "HSUM"; if (name == NilpotentMaximum().className()) return "NMAX"; - - return norm->className(); - } - - //TODO: Delete in v6.0 - std::string FclExporter::toString(const TNorm* tnorm) const { - if (not tnorm) return "NONE"; - std::string name = tnorm->className(); - 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"; - return tnorm->className(); - } - //TODO: Delete in v6.0 - std::string FclExporter::toString(const SNorm* snorm) const { - if (not snorm) return "NONE"; - std::string name = snorm->className(); - 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 snorm->className(); + return norm->className(); } std::string FclExporter::toString(const Defuzzifier* defuzzifier) const { @@ -232,15 +177,15 @@ namespace fl { if (const Discrete * discrete = dynamic_cast<const Discrete*> (term)) { std::ostringstream ss; for (std::size_t i = 0; i < discrete->xy().size(); ++i) { - ss << "(" << fl::Op::str(discrete->xy(i).first) << ", " - << fl::Op::str(discrete->xy(i).second) << ")"; + 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 fl::Op::str(constant->getValue()); + return Op::str(constant->getValue()); } std::ostringstream ss; @@ -252,5 +197,4 @@ namespace fl { return new FclExporter(*this); } - } diff --git a/fuzzylite/src/imex/FclImporter.cpp b/fuzzylite/src/imex/FclImporter.cpp index a56a9e1..06879cd 100644 --- a/fuzzylite/src/imex/FclImporter.cpp +++ b/fuzzylite/src/imex/FclImporter.cpp @@ -1,41 +1,28 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" -#include <iostream> -#include <sstream> - namespace fl { - FclImporter::FclImporter() : Importer() { - } + FclImporter::FclImporter() : Importer() { } - FclImporter::~FclImporter() { - } + FclImporter::~FclImporter() { } std::string FclImporter::name() const { return "FclImporter"; @@ -57,24 +44,16 @@ namespace fl { std::istringstream fclReader(fcl); std::string line; - int lineNumber = 0; + std::size_t lineNumber = 0; while (std::getline(fclReader, line)) { ++lineNumber; - std::vector<std::string> comments; - comments = Op::split(line, "//"); - if (comments.size() > 1) { - line = comments.front(); - } - comments = Op::split(line, "#"); - if (comments.size() > 1) { - line = comments.front(); - } - line = Op::trim(line); - if (line.empty() or line.at(0) == '%' or line.at(0) == '#' - or (line.substr(0, 2) == "//")) { + 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; } - line = fl::Op::findReplace(line, ";", ""); + std::istringstream tokenizer(line); std::string firstToken; tokenizer >> firstToken; @@ -102,7 +81,7 @@ namespace fl { std::ostringstream ex; ex << "[syntax error] unknown block definition <" << firstToken << "> " << " in line " << lineNumber << ": " << line; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } currentTag = tagFinder->first; closingTag = tagFinder->second; @@ -122,7 +101,7 @@ namespace fl { std::ostringstream ex; ex << "[syntax error] expected <" << closingTag << "> before <" << firstToken << "> in line: " << line; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } else { block << line << "\n"; } @@ -138,7 +117,7 @@ namespace fl { } else { ex << "expected <" << closingTag << ">, but not found"; } - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } return engine.release(); } @@ -155,7 +134,7 @@ namespace fl { } else { std::ostringstream ex; ex << "[syntax error] unexpected tag <" << tag << "> for block:\n" << block; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } } @@ -169,9 +148,9 @@ namespace fl { if (token.size() != 2) { std::ostringstream ex; ex << "[syntax error] expected property of type (key : value) in line: " << line; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } - std::string name = fl::Op::validName(token.at(0)); + std::string name = Op::validName(token.at(0)); if (tag == "VAR_INPUT") engine->addInputVariable(new InputVariable(name)); else if (tag == "VAR_OUTPUT") @@ -179,7 +158,7 @@ namespace fl { else { std::ostringstream ex; ex << "[syntax error] unexpected tag <" << tag << "> in line: " << line; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } } } @@ -192,17 +171,17 @@ namespace fl { std::string name; std::size_t index = line.find_first_of(' '); if (index != std::string::npos) { - name = fl::Op::validName(line.substr(index + 1)); + name = Op::validName(line.substr(index + 1)); } else { std::ostringstream ex; ex << "[syntax error] expected name of input variable in line: " << line; - throw fl::Exception(ex.str(), FL_AT); + 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 fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } InputVariable* inputVariable = engine->getInputVariable(name); @@ -219,9 +198,9 @@ namespace fl { inputVariable->setEnabled(parseEnabled(line)); } else if (firstToken == "TERM") { inputVariable->addTerm(parseTerm(line, engine)); - } else throw fl::Exception("[syntax error] unexpected token " + } else throw Exception("[syntax error] unexpected token " "<" + firstToken + ">" + line, FL_AT); - } catch (fl::Exception& ex) { + } catch (Exception& ex) { ex.append("At line: <" + line + ">"); throw; } @@ -237,23 +216,23 @@ namespace fl { std::string name; std::size_t index = line.find_first_of(' '); if (index != std::string::npos) { - name = fl::Op::validName(line.substr(index + 1)); + name = Op::validName(line.substr(index + 1)); } else { std::ostringstream ex; ex << "[syntax error] expected an output variable name in line: " << line; - throw fl::Exception(ex.str(), FL_AT); + 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 fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } OutputVariable* outputVariable = engine->getOutputVariable(name); while (std::getline(blockReader, line)) { - line = fl::Op::trim(line); + line = Op::trim(line); std::istringstream tokenizer(line); std::string firstToken; tokenizer >> firstToken; @@ -262,27 +241,27 @@ namespace fl { } else if (firstToken == "METHOD") { outputVariable->setDefuzzifier(parseDefuzzifier(line)); } else if (firstToken == "ACCU") { - outputVariable->fuzzyOutput()->setAccumulation(parseSNorm(line)); + outputVariable->fuzzyOutput()->setAggregation(parseSNorm(line)); } else if (firstToken == "DEFAULT") { std::pair<scalar, bool> defaultAndLock = parseDefaultValue(line); outputVariable->setDefaultValue(defaultAndLock.first); - outputVariable->setLockPreviousOutputValue(defaultAndLock.second or - outputVariable->isLockedPreviousOutputValue()); + 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->setLockPreviousOutputValue(output_range.first); - outputVariable->setLockOutputValueInRange(output_range.second); + 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 fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } } @@ -295,8 +274,9 @@ namespace fl { std::string name; std::getline(blockReader, line); std::size_t index = line.find_last_of(' '); - if (index != std::string::npos) name = line.substr(index); + 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)) { @@ -306,14 +286,14 @@ namespace fl { } else if (firstToken == "OR") { ruleblock->setDisjunction(parseSNorm(line)); } else if (firstToken == "ACT") { - ruleblock->setActivation(parseTNorm(line)); + 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 = fl::Op::trim(ruleText); + ruleText = Op::trim(ruleText); Rule* rule = new Rule(ruleText); try { rule->load(engine); @@ -325,7 +305,7 @@ namespace fl { std::ostringstream ex; ex << "[syntax error] keyword <" << firstToken << "> not recognized in line: " << line; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } } } @@ -336,7 +316,7 @@ namespace fl { std::ostringstream ex; ex << "[syntax error] expected property of type (key : value) in line: " << line; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } std::string name = Op::trim(token.at(1)); std::string className = name; @@ -358,7 +338,7 @@ namespace fl { std::ostringstream ex; ex << "[syntax error] expected property of type (key : value) in line: " << line; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } std::string name = Op::trim(token.at(1)); std::string className = name; @@ -412,7 +392,7 @@ namespace fl { continue; } if (state == S_TERMCLASS) { - if (fl::Op::isNumeric(token)) { + if (Op::isNumeric(token)) { termClass = Constant().className(); parameters.push_back(token); } else if (token == "(") { @@ -429,16 +409,16 @@ namespace fl { continue; } if (token == ";") break; - parameters.push_back(fl::Op::trim(token)); + parameters.push_back(Op::trim(token)); } } if (state <= S_TERMCLASS) - throw fl::Exception("[syntax error] malformed term in line: " + line, FL_AT); + 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(term.get(), engine); - term->setName(fl::Op::validName(name)); + term->updateReference(engine); + term->setName(Op::validName(name)); std::string separator; if (not dynamic_cast<Function*> (term.get())) { separator = " "; @@ -453,10 +433,10 @@ namespace fl { std::ostringstream ex; ex << "[syntax error] expected property of type (key : value) in " << "line: " << line; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } - std::string name = fl::Op::trim(token.at(1)); + std::string name = Op::trim(token.at(1)); std::string className = name; if (name == "NONE") className = ""; else if (name == "COG") className = Centroid().className(); @@ -476,7 +456,7 @@ namespace fl { std::ostringstream ex; ex << "[syntax error] expected property of type (key := value) in line: " << line; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } std::vector<std::string> values = Op::split(token.at(1), "|"); @@ -485,24 +465,24 @@ namespace fl { std::string nc; if (values.size() == 2) nc = values.back(); - defaultValue = fl::Op::trim(defaultValue); - nc = fl::Op::trim(nc); + defaultValue = Op::trim(defaultValue); + nc = Op::trim(nc); scalar value; try { - value = fl::Op::toScalar(defaultValue); + value = Op::toScalar(defaultValue); } catch (...) { std::ostringstream ex; ex << "[syntax error] expected numeric value, " << "but found <" << defaultValue << "> in line: " << line; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } bool lockPreviousOutput = (nc == "NC"); if (not (lockPreviousOutput or nc.empty())) { - throw fl::Exception("[syntax error] expected keyword <NC>, " + throw Exception("[syntax error] expected keyword <NC>, " "but found <" + nc + "> in line: " + line, FL_AT); } @@ -515,7 +495,7 @@ namespace fl { std::ostringstream ex; ex << "[syntax error] expected property of type (key := value) in line: " << line; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } std::string rangeToken = token.at(1); @@ -532,7 +512,7 @@ namespace fl { std::ostringstream ex; ex << "[syntax error] expected property of type 'start .. end', " << "but found <" << range.str() << "> in line: " << line; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } scalar minimum, maximum; int index; @@ -540,11 +520,11 @@ namespace fl { minimum = Op::toScalar(token.at(index = 0)); maximum = Op::toScalar(token.at(index = 1)); } catch (std::exception& ex) { - (void) ex; + FL_IUNUSED(ex); std::ostringstream ss; ss << "[syntax error] expected numeric value, but found <" << token.at(index) << "> in " << "line: " << line; - throw fl::Exception(ss.str(), FL_AT); + throw Exception(ss.str(), FL_AT); } return std::pair<scalar, scalar>(minimum, maximum); } @@ -552,32 +532,32 @@ namespace fl { 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 fl::Exception("[syntax error] expected property of type " + 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 = fl::Op::split(value, "|"); + std::vector<std::string> flags = Op::split(value, "|"); if (flags.size() == 1) { - std::string flag = fl::Op::trim(flags.front()); + std::string flag = Op::trim(flags.front()); output = (flag == "PREVIOUS"); range = (flag == "RANGE"); if (not (output or range)) { - throw fl::Exception("[syntax error] expected locking flags " + 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 = fl::Op::trim(flags.front()); - std::string flagB = fl::Op::trim(flags.back()); + 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 fl::Exception("[syntax error] expected locking flags " + throw Exception("[syntax error] expected locking flags " "<PREVIOUS|RANGE>, but found " "<" + flags.front() + "|" + flags.back() + "> in line: " + line, FL_AT); } } else { - throw fl::Exception("[syntax error] expected locking flags " + throw Exception("[syntax error] expected locking flags " "<PREVIOUS|RANGE>, but found " "<" + value + "> in line: " + line, FL_AT); } @@ -590,13 +570,13 @@ namespace fl { std::ostringstream ex; ex << "[syntax error] expected property of type (key : value) in " << "line: " << line; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } - std::string boolean = fl::Op::trim(tokens.at(1)); + std::string boolean = Op::trim(tokens.at(1)); if (boolean == "TRUE") return true; if (boolean == "FALSE") return false; - throw fl::Exception("[syntax error] expected boolean <TRUE|FALSE>, but found <" + line + ">", FL_AT); + throw Exception("[syntax error] expected boolean <TRUE|FALSE>, but found <" + line + ">", FL_AT); } FclImporter* FclImporter::clone() const { diff --git a/fuzzylite/src/imex/FisExporter.cpp b/fuzzylite/src/imex/FisExporter.cpp index 0934b33..2daac9a 100644 --- a/fuzzylite/src/imex/FisExporter.cpp +++ b/fuzzylite/src/imex/FisExporter.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -30,11 +22,9 @@ namespace fl { - FisExporter::FisExporter() : Exporter() { - } + FisExporter::FisExporter() : Exporter() { } - FisExporter::~FisExporter() { - } + FisExporter::~FisExporter() { } std::string FisExporter::name() const { return "FisExporter"; @@ -52,9 +42,10 @@ namespace fl { return fis.str(); } - //TODO: deal with multiple ruleblocks, merge them into one. + 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; @@ -66,57 +57,54 @@ namespace fl { type = "tsukamoto"; } else if (engine->type() == Engine::InverseTsukamoto) { type = "inverse tsukamoto"; - }else if (engine->type() == Engine::Hybrid){ + } else if (engine->type() == Engine::Hybrid) { type = "hybrid"; } else { type = "unknown"; } fis << "Type='" << type << "'\n"; - // fis << "Version=" << FL_VERSION << "\n"; + fis << "Version=" << fuzzylite::version() << "\n"; fis << "NumInputs=" << engine->numberOfInputVariables() << "\n"; fis << "NumOutputs=" << engine->numberOfOutputVariables() << "\n"; - - int numberOfRules = 0; + + std::size_t numberOfRules = 0; const TNorm* conjunction = fl::null; const SNorm* disjunction = fl::null; - const TNorm* activation = fl::null; - for (int i = 0; i < engine->numberOfRuleBlocks(); ++i) { + 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 activation) activation = rb->getActivation(); + if (not implication) implication = rb->getImplication(); } fis << "NumRules=" << numberOfRules << "\n"; - fis << "AndMethod='" << toString(conjunction) << "'\n"; - fis << "OrMethod='" << toString(disjunction) << "'\n"; - fis << "ImpMethod='" << toString(activation) << "'\n"; + fis << "AndMethod='" << (conjunction ? toString(conjunction) : "min") << "'\n"; + fis << "OrMethod='" << (disjunction ? toString(disjunction) : "max") << "'\n"; + fis << "ImpMethod='" << (implication ? toString(implication) : "min") << "'\n"; - const SNorm* accumulation = fl::null; + const SNorm* aggregation = fl::null; Defuzzifier* defuzzifier = fl::null; - for (int i = 0; i < engine->numberOfOutputVariables(); ++i) { + for (std::size_t i = 0; i < engine->numberOfOutputVariables(); ++i) { OutputVariable* outputVariable = engine->getOutputVariable(i); - if (not accumulation) accumulation = outputVariable->fuzzyOutput()->getAccumulation(); + if (not aggregation) aggregation = outputVariable->fuzzyOutput()->getAggregation(); if (not defuzzifier) defuzzifier = outputVariable->getDefuzzifier(); } - fis << "AggMethod='" << toString(accumulation) << "'\n"; + 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 (int ixVar = 0; ixVar < engine->numberOfInputVariables(); ++ixVar) { + for (std::size_t ixVar = 0; ixVar < engine->numberOfInputVariables(); ++ixVar) { InputVariable* var = engine->getInputVariable(ixVar); fis << "[Input" << (ixVar + 1) << "]\n"; - if (not var->isEnabled()) { - fis << "Enabled=" << var->isEnabled() << "\n"; - } fis << "Name='" << Op::validName(var->getName()) << "'\n"; - fis << "Range=[" << fl::Op::join(2, " ", var->getMinimum(), var->getMaximum()) << "]\n"; + fis << "Range=[" << Op::join(2, " ", var->getMinimum(), var->getMaximum()) << "]\n"; fis << "NumMFs=" << var->numberOfTerms() << "\n"; - for (int ixTerm = 0; ixTerm < var->numberOfTerms(); ++ixTerm) { + 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"; } @@ -127,25 +115,13 @@ namespace fl { std::string FisExporter::exportOutputs(const Engine* engine) const { std::ostringstream fis; - for (int ixVar = 0; ixVar < engine->numberOfOutputVariables(); ++ixVar) { + for (std::size_t ixVar = 0; ixVar < engine->numberOfOutputVariables(); ++ixVar) { OutputVariable* var = engine->getOutputVariable(ixVar); fis << "[Output" << (ixVar + 1) << "]\n"; - if (not var->isEnabled()) { - fis << "Enabled=" << var->isEnabled() << "\n"; - } fis << "Name='" << Op::validName(var->getName()) << "'\n"; - fis << "Range=[" << fl::Op::join(2, " ", var->getMinimum(), var->getMaximum()) << "]\n"; - if (not fl::Op::isNaN(var->getDefaultValue())) { - fis << "Default=" << fl::Op::str(var->getDefaultValue()) << "\n"; - } - if (var->isLockedPreviousOutputValue()) { - fis << "LockPrevious=" << var->isLockedPreviousOutputValue() << "\n"; - } - if (var->isLockedOutputValueInRange()) { - fis << "LockRange=" << var->isLockedOutputValueInRange() << "\n"; - } + fis << "Range=[" << Op::join(2, " ", var->getMinimum(), var->getMaximum()) << "]\n"; fis << "NumMFs=" << var->numberOfTerms() << "\n"; - for (int ixTerm = 0; ixTerm < var->numberOfTerms(); ++ixTerm) { + 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"; } @@ -157,10 +133,10 @@ namespace fl { std::string FisExporter::exportRules(const Engine* engine) const { std::ostringstream fis; fis << "[Rules]\n"; - for (int ixRuleBlock = 0; ixRuleBlock < engine->numberOfRuleBlocks(); ++ixRuleBlock) { + 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 (int ixRule = 0; ixRule < rb->numberOfRules(); ++ixRule) { + for (std::size_t ixRule = 0; ixRule < rb->numberOfRules(); ++ixRule) { Rule* rule = rb->getRule(ixRule); if (rule->isLoaded()) { fis << exportRule(rule, engine) << "\n"; @@ -198,15 +174,15 @@ namespace fl { } } if (not equalOperators) { - throw fl::Exception("[exporter error] " + 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 (int i = 0; i < engine->numberOfInputVariables(); ++i) + for (std::size_t i = 0; i < engine->numberOfInputVariables(); ++i) inputVariables.push_back(engine->getInputVariable(i)); - for (int i = 0; i < engine->numberOfOutputVariables(); ++i) + for (std::size_t i = 0; i < engine->numberOfOutputVariables(); ++i) outputVariables.push_back(engine->getOutputVariable(i)); fis << translate(propositions, inputVariables) << ", "; @@ -226,14 +202,14 @@ namespace fl { std::ostringstream ss; for (std::size_t ixVariable = 0; ixVariable < variables.size(); ++ixVariable) { Variable* variable = variables.at(ixVariable); - int termIndexPlusOne = 0; + 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 (int termIndex = 0; termIndex < variable->numberOfTerms(); ++termIndex) { + for (std::size_t termIndex = 0; termIndex < variable->numberOfTerms(); ++termIndex) { if (variable->getTerm(termIndex) == proposition->term) { termIndexPlusOne = termIndex + 1; break; @@ -259,8 +235,8 @@ namespace fl { break; } if (negated < 0) ss << "-"; - if (not fl::Op::isNaN(plusHedge)) { - ss << fl::Op::str(termIndexPlusOne + plusHedge); + if (not Op::isNaN(plusHedge)) { + ss << Op::str(termIndexPlusOne + plusHedge); } else { ss << termIndexPlusOne << ".?"; // Unreconized hedge combination } @@ -268,29 +244,6 @@ namespace fl { } return ss.str(); } - - std::string FisExporter::toString(const Norm * norm) const { - if (not norm) return ""; - //TNorm - if (norm->className() == Minimum().className()) return "min"; - if (norm->className() == AlgebraicProduct().className()) return "prod"; - if (norm->className() == BoundedDifference().className()) return "bounded_difference"; - if (norm->className() == DrasticProduct().className()) return "drastic_product"; - if (norm->className() == EinsteinProduct().className()) return "einstein_product"; - if (norm->className() == HamacherProduct().className()) return "hamacher_product"; - if (norm->className() == NilpotentMinimum().className()) return "nilpotent_minimum"; - //SNorm - if (norm->className() == Maximum().className()) return "max"; - if (norm->className() == AlgebraicSum().className()) return "sum"; - if (norm->className() == BoundedSum().className()) return "bounded_sum"; - if (norm->className() == NormalizedSum().className()) return "normalized_sum"; - if (norm->className() == DrasticSum().className()) return "drastic_sum"; - if (norm->className() == EinsteinSum().className()) return "einstein_sum"; - if (norm->className() == HamacherSum().className()) return "hamacher_sum"; - if (norm->className() == NilpotentMaximum().className()) return "nilpotent_maximum"; - - return norm->className(); - } std::string FisExporter::toString(const TNorm * tnorm) const { if (not tnorm) return ""; @@ -304,16 +257,17 @@ namespace fl { return tnorm->className(); } - std::string FisExporter::toString(const SNorm * snorm) const { + 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 "sum"; + 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(); } @@ -332,30 +286,36 @@ namespace fl { std::string FisExporter::toString(const Term * term) const { std::ostringstream ss; if (const Bell * x = dynamic_cast<const Bell*> (term)) { - ss << "'gbellmf',[" << fl::Op::join(3, " ", + 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',[" << fl::Op::join(2, " ", + ss << "'concavemf',[" << Op::join(2, " ", x->getInflection(), x->getEnd()) << "]"; return ss.str(); } if (const Constant * x = dynamic_cast<const Constant*> (term)) { - ss << "'constant',[" << fl::Op::str(x->getValue()) << "]"; + ss << "'constant',[" << Op::str(x->getValue()) << "]"; return ss.str(); } if (const Cosine * x = dynamic_cast<const Cosine*> (term)) { - ss << "'cosinemf',[" << fl::Op::join(2, " ", + ss << "'cosinemf',[" << Op::join(2, " ", x->getCenter(), x->getWidth()) << "]"; return ss.str(); } if (const Discrete * x = dynamic_cast<const Discrete*> (term)) { - ss << "'discretemf',[" << fl::Op::join(Discrete::toVector(x->xy()), " ") << "]"; + ss << "'discretemf',[" << Op::join(Discrete::toVector(x->xy()), " ") << "]"; return ss.str(); } @@ -365,95 +325,95 @@ namespace fl { } if (const Gaussian * x = dynamic_cast<const Gaussian*> (term)) { - ss << "'gaussmf',[" << fl::Op::join(2, " ", + ss << "'gaussmf',[" << Op::join(2, " ", x->getStandardDeviation(), x->getMean()) << "]"; return ss.str(); } if (const GaussianProduct * x = dynamic_cast<const GaussianProduct*> (term)) { - ss << "'gauss2mf',[" << fl::Op::join(4, " ", + 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',[" << fl::Op::join<scalar>(x->coefficients(), " ") << "]"; + ss << "'linear',[" << Op::join<scalar>(x->coefficients(), " ") << "]"; return ss.str(); } if (const PiShape * x = dynamic_cast<const PiShape*> (term)) { - ss << "'pimf',[" << fl::Op::join(4, " ", + 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',[" << fl::Op::join(2, " ", + ss << "'rampmf',[" << Op::join(2, " ", x->getStart(), x->getEnd()) << "]"; return ss.str(); } if (const Rectangle * x = dynamic_cast<const Rectangle*> (term)) { - ss << "'rectmf',[" << fl::Op::join(2, " ", + ss << "'rectmf',[" << Op::join(2, " ", x->getStart(), x->getEnd()) << "]"; return ss.str(); } if (const SigmoidDifference * x = dynamic_cast<const SigmoidDifference*> (term)) { - ss << "'dsigmf',[" << fl::Op::join(4, " ", + 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',[" << fl::Op::join(2, " ", + ss << "'sigmf',[" << Op::join(2, " ", x->getSlope(), x->getInflection()) << "]"; return ss.str(); } if (const SigmoidProduct * x = dynamic_cast<const SigmoidProduct*> (term)) { - ss << "'psigmf',[" << fl::Op::join(4, " ", + 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',[" << fl::Op::join(2, " ", + ss << "'smf',[" << Op::join(2, " ", x->getStart(), x->getEnd()) << "]"; return ss.str(); } if (const Spike * x = dynamic_cast<const Spike*> (term)) { - ss << "'spikemf',[" << fl::Op::join(2, " ", + ss << "'spikemf',[" << Op::join(2, " ", x->getCenter(), x->getWidth()) << "]"; return ss.str(); } if (const Trapezoid * x = dynamic_cast<const Trapezoid*> (term)) { - ss << "'trapmf',[" << fl::Op::join(4, " ", + 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',[" << fl::Op::join(3, " ", + 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',[" << fl::Op::join(2, " ", + ss << "'zmf',[" << Op::join(2, " ", x->getStart(), x->getEnd()) << "]"; return ss.str(); } - ss << "[exporter error] term of class <" << term->className() << "> not supported"; - throw fl::Exception(ss.str(), FL_AT); + ss << "[exporter error] term of class <" << (term ? term->className() : "null") << "> not supported"; + throw Exception(ss.str(), FL_AT); } FisExporter* FisExporter::clone() const { diff --git a/fuzzylite/src/imex/FisImporter.cpp b/fuzzylite/src/imex/FisImporter.cpp index 741719c..f846469 100644 --- a/fuzzylite/src/imex/FisImporter.cpp +++ b/fuzzylite/src/imex/FisImporter.cpp @@ -1,42 +1,28 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" -#include <sstream> -#include <iostream> -#include <cctype> - namespace fl { - FisImporter::FisImporter() : Importer() { - } + FisImporter::FisImporter() : Importer() { } - FisImporter::~FisImporter() { - } + FisImporter::~FisImporter() { } std::string FisImporter::name() const { return "FisImporter"; @@ -47,27 +33,20 @@ namespace fl { std::istringstream fisReader(fis); std::string line; - int lineNumber = 0; + std::size_t lineNumber = 0; std::vector<std::string> sections; while (std::getline(fisReader, line)) { ++lineNumber; - std::vector<std::string> comments; - comments = Op::split(line, "//"); - if (comments.size() > 1) { - line = comments.front(); - } - comments = Op::split(line, "#"); - if (comments.size() > 1) { - line = comments.front(); - } + //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) == '%' or line.at(0) == '#' - or (line.substr(0, 2) == "//")) { + if (line.empty() or line.at(0) == '%') { continue; } - line = fl::Op::findReplace(line, "'", ""); + line = Op::findReplace(line, "'", ""); if ("[System]" == line.substr(0, std::string("[System]").size()) or "[Input" == line.substr(0, std::string("[Input").size()) @@ -79,9 +58,9 @@ namespace fl { sections.at(sections.size() - 1) += "\n" + line; } else { std::ostringstream ss; - ss << "[import error] line " << lineNumber << " <" << line + "> " + ss << "[import error] line " << lineNumber << " <" << line << "> " "does not belong to any section"; - throw fl::Exception(ss.str(), FL_AT); + throw Exception(ss.str(), FL_AT); } } } @@ -96,12 +75,12 @@ namespace fl { 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 fl::Exception("[import error] section <" + else throw Exception("[import error] section <" + sections.at(i) + "> not recognized", FL_AT); } - engine->configure(extractTNorm(andMethod), extractSNorm(orMethod), - extractTNorm(impMethod), extractSNorm(aggMethod), - extractDefuzzifier(defuzzMethod)); + engine->configure(translateTNorm(andMethod), translateSNorm(orMethod), + translateTNorm(impMethod), translateSNorm(aggMethod), + translateDefuzzifier(defuzzMethod), General().className()); return engine.release(); } @@ -113,14 +92,14 @@ namespace fl { std::string line; std::getline(reader, line); //ignore first line [System] while (std::getline(reader, line)) { - std::vector<std::string> keyValue = fl::Op::split(line, "="); + std::vector<std::string> keyValue = Op::split(line, "="); - std::string key = fl::Op::trim(keyValue.at(0)); + 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 = fl::Op::trim(value); + value = Op::trim(value); if (key == "Name") engine->setName(value); else if (key == "AndMethod") andMethod = value; else if (key == "OrMethod") orMethod = value; @@ -131,7 +110,7 @@ namespace fl { or key == "NumInputs" or key == "NumOutputs" or key == "NumRules" or key == "NumMFs") { //ignore because are redundant. - } else throw fl::Exception("[import error] token <" + key + "> not recognized", FL_AT); + } else throw Exception("[import error] token <" + key + "> not recognized", FL_AT); } } @@ -144,18 +123,19 @@ namespace fl { engine->addInputVariable(input); while (std::getline(reader, line)) { - std::vector<std::string> keyValue = fl::Op::split(line, "="); + std::vector<std::string> keyValue = Op::split(line, "="); if (keyValue.size() != 2) - throw fl::Exception("[syntax error] expected a property of type " + throw Exception("[syntax error] expected a property of type " "'key=value', but found <" + line + ">", FL_AT); - std::string key = fl::Op::trim(keyValue.at(0)); - std::string value = fl::Op::trim(keyValue.at(1)); + std::string key = Op::trim(keyValue.at(0)); + std::string value = Op::trim(keyValue.at(1)); - if (key == "Name") input->setName(fl::Op::validName(value)); - else if (key == "Enabled") { + 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 = range(value); + std::pair<scalar, scalar> minmax = parseRange(value); input->setMinimum(minmax.first); input->setMaximum(minmax.second); } else if (key.substr(0, 2) == "MF") { @@ -163,7 +143,7 @@ namespace fl { } else if (key == "NumMFs") { //ignore } else { - throw fl::Exception("[import error] token <" + key + "> not recognized", FL_AT); + throw Exception("[import error] token <" + key + "> not recognized", FL_AT); } } } @@ -178,32 +158,33 @@ namespace fl { while (std::getline(reader, line)) { - std::vector<std::string> keyValue = fl::Op::split(line, "="); + std::vector<std::string> keyValue = Op::split(line, "="); if (keyValue.size() != 2) - throw fl::Exception("[syntax error] expected a property of type " + throw Exception("[syntax error] expected a property of type " "'key=value', but found < " + line + ">", FL_AT); - std::string key = fl::Op::trim(keyValue.at(0)); - std::string value = fl::Op::trim(keyValue.at(1)); + std::string key = Op::trim(keyValue.at(0)); + std::string value = Op::trim(keyValue.at(1)); - if (key == "Name") output->setName(fl::Op::validName(value)); - else if (key == "Enabled") { + 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 = range(value); + 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(fl::Op::toScalar(value)); + output->setDefaultValue(Op::toScalar(value)); } else if (key == "LockPrevious") { - output->setLockPreviousOutputValue(fl::Op::isEq(fl::Op::toScalar(value), 1.0)); + output->setLockPreviousValue(Op::isEq(Op::toScalar(value), 1.0)); } else if (key == "LockRange") { - output->setLockOutputValueInRange(fl::Op::isEq(fl::Op::toScalar(value), 1.0)); + output->setLockValueInRange(Op::isEq(Op::toScalar(value), 1.0)); } else if (key == "NumMFs") { //ignore } else { - throw fl::Exception("[import error] token <" + key + "> not recognized", FL_AT); + throw Exception("[import error] token <" + key + "> not recognized", FL_AT); } } } @@ -217,78 +198,78 @@ namespace fl { engine->addRuleBlock(ruleblock); while (std::getline(reader, line)) { - std::vector<std::string> inputsAndRest = fl::Op::split(line, ","); + std::vector<std::string> inputsAndRest = Op::split(line, ","); if (inputsAndRest.size() != 2) - throw fl::Exception("[syntax error] expected rule to match pattern " + 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 = fl::Op::split(inputsAndRest.at(1), ":"); + std::vector <std::string> outputsAndRest = Op::split(inputsAndRest.at(1), ":"); if (outputsAndRest.size() != 2) - throw fl::Exception("[syntax error] expected rule to match pattern " + 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 = fl::Op::split(inputsAndRest.at(0), " "); - std::vector<std::string> outputs = fl::Op::split(outputsAndRest.at(0), " "); + 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 = fl::Op::trim(outputsAndRest.at(1)); + std::string connector = Op::trim(outputsAndRest.at(1)); - if ((int) inputs.size() != engine->numberOfInputVariables()) { + 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 fl::Exception(ss.str(), FL_AT); + throw Exception(ss.str(), FL_AT); } - if ((int) outputs.size() != engine->numberOfOutputVariables()) { + 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 fl::Exception(ss.str(), FL_AT); + throw Exception(ss.str(), FL_AT); } std::vector<std::string> antecedent, consequent; for (std::size_t i = 0; i < inputs.size(); ++i) { - scalar inputCode = fl::Op::toScalar(inputs.at(i)); - if (fl::Op::isEq(inputCode, 0.0)) continue; + scalar inputCode = Op::toScalar(inputs.at(i)); + if (Op::isEq(inputCode, 0.0)) continue; std::ostringstream ss; ss << engine->getInputVariable(i)->getName() << " " - << fl::Rule::isKeyword() << " " + << Rule::isKeyword() << " " << translateProposition(inputCode, engine->getInputVariable(i)); antecedent.push_back(ss.str()); } for (std::size_t i = 0; i < outputs.size(); ++i) { - scalar outputCode = fl::Op::toScalar(outputs.at(i)); - if (fl::Op::isEq(outputCode, 0.0)) continue; + scalar outputCode = Op::toScalar(outputs.at(i)); + if (Op::isEq(outputCode, 0.0)) continue; std::ostringstream ss; ss << engine->getOutputVariable(i)->getName() << " " - << fl::Rule::isKeyword() << " " + << Rule::isKeyword() << " " << translateProposition(outputCode, engine->getOutputVariable(i)); consequent.push_back(ss.str()); } std::ostringstream ruleText; - ruleText << fl::Rule::ifKeyword() << " "; + 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 << fl::Rule::andKeyword() << " "; - else if (connector == "2") ruleText << fl::Rule::orKeyword() << " "; - else throw fl::Exception("[syntax error] connector <" + 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 << " " << fl::Rule::thenKeyword() << " "; + ruleText << " " << Rule::thenKeyword() << " "; for (std::size_t i = 0; i < consequent.size(); ++i) { ruleText << consequent.at(i); if (i + 1 < consequent.size()) { - ruleText << " " << fl::Rule::andKeyword() << " "; + ruleText << " " << Rule::andKeyword() << " "; } } @@ -300,9 +281,9 @@ namespace fl { ss << weightInParenthesis.at(i); } - scalar weight = fl::Op::toScalar(ss.str()); - if (not fl::Op::isEq(weight, 1.0)) - ruleText << " " << fl::Rule::withKeyword() << " " << Op::str(weight); + 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); @@ -314,34 +295,34 @@ namespace fl { } std::string FisImporter::translateProposition(scalar code, Variable* variable) const { - int intPart = (int) std::floor(std::fabs(code)) - 1; - scalar fracPart = std::fmod(std::fabs(code), 1.0); - if (intPart >= variable->numberOfTerms()) { + 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 fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } bool isAny = intPart < 0; std::ostringstream ss; if (code < 0) ss << Not().name() << " "; - if (fl::Op::isEq(fracPart, 0.01)) ss << Seldom().name() << " "; - else if (fl::Op::isEq(fracPart, 0.05)) ss << Somewhat().name() << " "; - else if (fl::Op::isEq(fracPart, 0.2)) ss << Very().name() << " "; - else if (fl::Op::isEq(fracPart, 0.3)) ss << Extremely().name() << " "; - else if (fl::Op::isEq(fracPart, 0.4)) ss << Very().name() << " " << Very().name() << " "; - else if (fl::Op::isEq(fracPart, 0.99)) ss << Any().name() << " "; - else if (not fl::Op::isEq(fracPart, 0)) - throw fl::Exception("[syntax error] no hedge defined in FIS format for <" - + fl::Op::str(fracPart) + ">", FL_AT); + 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::extractTNorm(const std::string & name) const { + 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(); @@ -353,20 +334,21 @@ namespace fl { return name; } - std::string FisImporter::extractSNorm(const std::string & name) const { + std::string FisImporter::translateSNorm(const std::string& name) const { if (name.empty()) return ""; if (name == "max") return Maximum().className(); - if (name == "sum" or name == "probor") return AlgebraicSum().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::extractDefuzzifier(const std::string & name) const { + 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(); @@ -378,22 +360,22 @@ namespace fl { return name; } - std::pair<scalar, scalar> FisImporter::range(const std::string& range) const { - std::vector<std::string> parts = fl::Op::split(range, " "); + std::pair<scalar, scalar> FisImporter::parseRange(const std::string& range) const { + std::vector<std::string> parts = Op::split(range, " "); if (parts.size() != 2) - throw fl::Exception("[syntax error] expected range in format '[begin end]'," + 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 fl::Exception("[syntax error] expected range in format '[begin end]'," + throw Exception("[syntax error] expected range in format '[begin end]'," " but found <" + range + ">", FL_AT); std::pair<scalar, scalar> result; - result.first = fl::Op::toScalar(begin.substr(1)); - result.second = fl::Op::toScalar(end.substr(0, end.size() - 1)); + 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 { + 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) == ']')) { @@ -402,35 +384,36 @@ namespace fl { } std::string line = ss.str(); - std::vector<std::string> nameTerm = fl::Op::split(line, ":"); + std::vector<std::string> nameTerm = Op::split(line, ":"); if (nameTerm.size() != 2) { - throw fl::Exception("[syntax error] expected term in format 'name':'class',[params], " + throw Exception("[syntax error] expected term in format 'name':'class',[params], " "but found <" + line + ">", FL_AT); } - std::vector<std::string> termParams = fl::Op::split(nameTerm.at(1), ","); + std::vector<std::string> termParams = Op::split(nameTerm.at(1), ","); if (termParams.size() != 2) { - throw fl::Exception("[syntax error] expected term in format 'name':'class',[params], " + throw Exception("[syntax error] expected term in format 'name':'class',[params], " "but found " + line, FL_AT); } - std::vector<std::string> parameters = fl::Op::split(termParams.at(1), " "); + std::vector<std::string> parameters = Op::split(termParams.at(1), " "); for (std::size_t i = 0; i < parameters.size(); ++i) { - parameters.at(i) = fl::Op::trim(parameters.at(i)); + parameters.at(i) = Op::trim(parameters.at(i)); } return createInstance( - fl::Op::trim(termParams.at(0)), - fl::Op::trim(nameTerm.at(0)), + Op::trim(termParams.at(0)), + Op::trim(nameTerm.at(0)), parameters, engine); } - Term * FisImporter::createInstance(const std::string& mClass, + 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["discretemf"] = Discrete().className(); + 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(); @@ -484,7 +467,7 @@ namespace fl { FL_unique_ptr<Term> term; term.reset(FactoryManager::instance()->term()->constructObject(flClass)); - Term::updateReference(term.get(), engine); + term->updateReference(engine); term->setName(Op::validName(name)); std::string separator; if (not dynamic_cast<Function*> (term.get())) { diff --git a/fuzzylite/src/imex/FldExporter.cpp b/fuzzylite/src/imex/FldExporter.cpp index 9064250..e25af1b 100644 --- a/fuzzylite/src/imex/FldExporter.cpp +++ b/fuzzylite/src/imex/FldExporter.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -30,20 +22,15 @@ #include "fl/variable/InputVariable.h" #include "fl/variable/OutputVariable.h" -#include <cmath> #include <fstream> -#include <vector> namespace fl { FldExporter::FldExporter(const std::string& separator) : Exporter(), _separator(separator), _exportHeaders(true), - _exportInputValues(true), _exportOutputValues(true) { - - } + _exportInputValues(true), _exportOutputValues(true) { } - FldExporter::~FldExporter() { - } + FldExporter::~FldExporter() { } std::string FldExporter::name() const { return "FldExporter"; @@ -84,156 +71,235 @@ namespace fl { std::string FldExporter::header(const Engine* engine) const { std::vector<std::string> result; if (_exportInputValues) { - for (int i = 0; i < engine->numberOfInputVariables(); ++i) { + for (std::size_t i = 0; i < engine->numberOfInputVariables(); ++i) { InputVariable* inputVariable = engine->getInputVariable(i); - result.push_back("@InputVariable: " + inputVariable->getName() + ";"); + result.push_back(inputVariable->getName()); } } if (_exportOutputValues) { - for (int i = 0; i < engine->numberOfOutputVariables(); ++i) { + for (std::size_t i = 0; i < engine->numberOfOutputVariables(); ++i) { OutputVariable* outputVariable = engine->getOutputVariable(i); - result.push_back("@OutputVariable: " + outputVariable->getName() + ";"); + result.push_back(outputVariable->getName()); } } - return "#@Engine: " + engine->getName() + ";\n#" + Op::join(result, _separator); + return Op::join(result, _separator); } std::string FldExporter::toString(const Engine* engine) const { - return toString(const_cast<Engine*> (engine), 1024); + return toString(const_cast<Engine*> (engine), 1024, AllVariables); } - std::string FldExporter::toString(Engine* engine, int maximumNumberOfResults) const { - std::ostringstream result; - write(engine, result, maximumNumberOfResults); - return result.str(); + std::string FldExporter::toString(Engine* engine, int values, ScopeOfValues scope) const { + return toString(engine, values, scope, engine->inputVariables()); } - void FldExporter::toFile(const std::string& path, Engine* engine, int maximumNumberOfResults) const { - std::ofstream writer(path.c_str()); - if (not writer.is_open()) { - throw fl::Exception("[file error] file <" + path + "> could not be created", FL_AT); - } - write(engine, writer, maximumNumberOfResults); - writer.close(); + 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, const std::string& inputData) const { + std::string FldExporter::toString(Engine* engine, std::istream& reader) const { std::ostringstream writer; if (_exportHeaders) writer << header(engine) << "\n"; - std::istringstream reader(inputData); 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 = parse(line); - write(engine, writer, inputValues); - writer.flush(); + 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, const std::string& inputData) const { + 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 fl::Exception("[file error] file <" + path + "> could not be created", FL_AT); + 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::istringstream reader(inputData); + 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 = parse(line); - write(engine, writer, inputValues); - writer.flush(); + 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& x) const { + std::vector<scalar> FldExporter::parse(const std::string& values) const { std::vector<scalar> inputValues; - if (not (x.empty() or x.at(0) == '#')) { - std::istringstream tokenizer(x); - std::string token; - while (tokenizer >> token) - inputValues.push_back(fl::Op::toScalar(token)); + if (not (values.empty() or values.at(0) == '#')) { + inputValues = Op::toScalars(values); } return inputValues; } - void FldExporter::write(Engine* engine, std::ostream& writer, int maximum) const { + 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"; - int resolution = -1 + (int) std::max(1.0, std::pow( - maximum, 1.0 / engine->numberOfInputVariables())); + 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 (int i = 0; i < engine->numberOfInputVariables(); ++i) { + for (std::size_t i = 0; i < engine->numberOfInputVariables(); ++i) { sampleValues.push_back(0); minSampleValues.push_back(0); - maxSampleValues.push_back(resolution); + if (engine->inputVariables().at(i) == activeVariables.at(i)) + maxSampleValues.push_back(resolution); + else maxSampleValues.push_back(0); } - engine->restart(); - - bool overflow = false; std::vector<scalar> inputValues(engine->numberOfInputVariables()); - while (not overflow) { - for (int i = 0; i < engine->numberOfInputVariables(); ++i) { + do { + for (std::size_t i = 0; i < engine->numberOfInputVariables(); ++i) { InputVariable* inputVariable = engine->getInputVariable(i); - inputValues.at(i) = inputVariable->getMinimum() - + sampleValues.at(i) * inputVariable->range() / std::max(1, resolution); + 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); - overflow = Op::increment(sampleValues, minSampleValues, maxSampleValues); - } + 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"; - engine->restart(); - std::string line; - int lineNumber = 0; + std::size_t lineNumber = 0; while (std::getline(reader, line)) { ++lineNumber; - std::vector<scalar> inputValues = parse(Op::trim(line)); + 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); - } catch (fl::Exception& ex) { + 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 { + 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 (int(inputValues.size()) < engine->numberOfInputVariables()) { + 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 fl::Exception(ex.str(), FL_AT); + 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<std::string> values; - for (int i = 0; i < engine->numberOfInputVariables(); ++i) { + std::vector<scalar> values; + for (std::size_t i = 0; i < engine->numberOfInputVariables(); ++i) { InputVariable* inputVariable = engine->getInputVariable(i); - scalar inputValue = inputVariable->isEnabled() ? inputValues.at(i) : fl::nan; - inputVariable->setInputValue(inputValue); - if (_exportInputValues) values.push_back(Op::str(inputValue)); + 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 (int i = 0; i < engine->numberOfOutputVariables(); ++i) { + for (std::size_t i = 0; i < engine->numberOfOutputVariables(); ++i) { OutputVariable* outputVariable = engine->getOutputVariable(i); - outputVariable->defuzzify(); if (_exportOutputValues) - values.push_back(Op::str(outputVariable->getOutputValue())); + values.push_back(outputVariable->getValue()); } writer << Op::join(values, _separator) << "\n"; diff --git a/fuzzylite/src/imex/FllExporter.cpp b/fuzzylite/src/imex/FllExporter.cpp index 0ffeb2f..6a86152 100644 --- a/fuzzylite/src/imex/FllExporter.cpp +++ b/fuzzylite/src/imex/FllExporter.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -29,11 +21,9 @@ namespace fl { FllExporter::FllExporter(const std::string& indent, const std::string& separator) - : Exporter(), _indent(indent), _separator(separator) { - } + : Exporter(), _indent(indent), _separator(separator) { } - FllExporter::~FllExporter() { - } + FllExporter::~FllExporter() { } std::string FllExporter::name() const { return "FllExporter"; @@ -58,9 +48,17 @@ namespace fl { std::string FllExporter::toString(const Engine* engine) const { std::vector<std::string> result; result.push_back("Engine: " + engine->getName()); - result.push_back(toString(engine->inputVariables())); - result.push_back(toString(engine->outputVariables())); - result.push_back(toString(engine->ruleBlocks())); + 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); } @@ -99,10 +97,15 @@ namespace fl { 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())); - for (int i = 0; i < variable->numberOfTerms(); ++i) { + 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); @@ -111,10 +114,15 @@ namespace fl { 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())); - for (int i = 0; i < inputVariable->numberOfTerms(); ++i) { + 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); @@ -123,19 +131,22 @@ namespace fl { 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 + "accumulation: " + - toString(outputVariable->fuzzyOutput()->getAccumulation())); + 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->isLockedPreviousOutputValue() ? "true" : "false")); - result.push_back(_indent + "lock-range: " + - (outputVariable->isLockedOutputValueInRange() ? "true" : "false")); - for (int i = 0; i < outputVariable->numberOfTerms(); ++i) { + (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); @@ -144,12 +155,16 @@ namespace fl { 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 (int i = 0; i < ruleBlock->numberOfRules(); ++i) { + for (std::size_t i = 0; i < ruleBlock->numberOfRules(); ++i) { result.push_back(_indent + toString(ruleBlock->getRule(i))); } return Op::join(result, _separator); @@ -169,11 +184,17 @@ namespace fl { 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<int>(integralDefuzzifier->getResolution()); + return defuzzifier->className() + " " + Op::str(integralDefuzzifier->getResolution()); } else if (const WeightedDefuzzifier * weightedDefuzzifier = dynamic_cast<const WeightedDefuzzifier*> (defuzzifier)) { diff --git a/fuzzylite/src/imex/FllImporter.cpp b/fuzzylite/src/imex/FllImporter.cpp index 2443d10..ac9d1fc 100644 --- a/fuzzylite/src/imex/FllImporter.cpp +++ b/fuzzylite/src/imex/FllImporter.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -31,12 +23,9 @@ namespace fl { FllImporter::FllImporter(const std::string& separator) : Importer(), - _separator(separator) { - } - - FllImporter::~FllImporter() { + _separator(separator) { } - } + FllImporter::~FllImporter() { } std::string FllImporter::name() const { return "FllImporter"; @@ -50,62 +39,48 @@ namespace fl { return this->_separator; } - Engine* FllImporter::fromString(const std::string& fll) const { + 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::ostringstream block; - std::istringstream fclReader(fll); + std::vector<std::string> block; + std::istringstream fllReader(fll); std::string line; - std::queue<std::string> lineQueue; - - bool processPending = false; - int lineNumber = 0; - while (not lineQueue.empty() or std::getline(fclReader, line)) { - if (not lineQueue.empty()) { - line = lineQueue.front(); - lineQueue.pop(); - } else { - line = clean(line); - if (line.empty()) continue; - std::vector<std::string> split = Op::split(line, _separator); - line = clean(split.front()); - for (std::size_t i = 1; i < split.size(); ++i) { - lineQueue.push(clean(split.at(i))); - } - ++lineNumber; - } + + 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 fl::Exception("[import error] expected a colon at line " + - Op::str(lineNumber) + ": " + line, FL_AT); + 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 { - processPending = (key == "InputVariable" - or key == "OutputVariable" - or key == "RuleBlock"); - } - if (processPending) { - process(tag, block.str(), engine.get()); - block.str(""); //clear buffer + } 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 - processPending = false; tag = key; + } else if (tag.empty()) { + throw Exception("[import error] unexpected block: " + line, FL_AT); } - block << key << ":" << value << "\n"; + block.push_back(key + ":" + value); } - process(tag, block.str(), engine.get()); + 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) { @@ -113,42 +88,47 @@ namespace fl { } else if ("RuleBlock" == tag) { processRuleBlock(block, engine); } else { - throw fl::Exception("[import error] block tag <" + tag + "> not recognized", FL_AT); + 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; - InputVariable* inputVariable = new InputVariable; - engine->addInputVariable(inputVariable); + 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 fl::Exception("[import error] key <" + keyValue.first + "> not " + 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; - OutputVariable* outputVariable = new OutputVariable; - engine->addOutputVariable(outputVariable); + 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) { @@ -157,39 +137,64 @@ namespace fl { } else if ("default" == keyValue.first) { outputVariable->setDefaultValue(Op::toScalar(keyValue.second)); } else if ("lock-previous" == keyValue.first or "lock-valid" == keyValue.first) { - outputVariable->setLockPreviousOutputValue(parseBoolean(keyValue.second)); + outputVariable->setLockPreviousValue(parseBoolean(keyValue.second)); } else if ("lock-range" == keyValue.first) { - outputVariable->setLockOutputValueInRange(parseBoolean(keyValue.second)); + 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()->setAccumulation(parseSNorm(keyValue.second)); + 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 fl::Exception("[import error] key <" + keyValue.first + "> not " + 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; - RuleBlock* ruleBlock = new RuleBlock; - engine->addRuleBlock(ruleBlock); + 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) { - ruleBlock->setActivation(parseTNorm(keyValue.second)); + 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); @@ -200,10 +205,14 @@ namespace fl { } ruleBlock->addRule(rule); } else { - throw fl::Exception("[import error] key <" + keyValue.first + "> not " + 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 { @@ -212,12 +221,12 @@ namespace fl { //MEDIUM Triangle 0.500 1.000 1.500 if (tokens.size() < 2) { - throw fl::Exception("[syntax error] expected a term in format <name class parameters>, " + 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(term.get(), engine); + term->updateReference(engine); term->setName(Op::validName(tokens.at(0))); std::ostringstream parameters; for (std::size_t i = 2; i < tokens.size(); ++i) { @@ -238,6 +247,20 @@ namespace fl { 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); @@ -252,7 +275,7 @@ namespace fl { if (parameter == "Automatic") type = WeightedDefuzzifier::Automatic; else if (parameter == "TakagiSugeno") type = WeightedDefuzzifier::TakagiSugeno; else if (parameter == "Tsukamoto") type = WeightedDefuzzifier::Tsukamoto; - else throw fl::Exception("[syntax error] unknown parameter of WeightedDefuzzifier <" + parameter + ">", FL_AT); + else throw Exception("[syntax error] unknown parameter of WeightedDefuzzifier <" + parameter + ">", FL_AT); weightedDefuzzifier->setType(type); } } @@ -267,7 +290,7 @@ namespace fl { bool FllImporter::parseBoolean(const std::string& boolean) const { if ("true" == boolean) return true; if ("false" == boolean) return false; - throw fl::Exception("[syntax error] expected boolean <true|false>, " + throw Exception("[syntax error] expected boolean <true|false>, " "but found <" + boolean + ">", FL_AT); } @@ -278,7 +301,7 @@ namespace fl { std::ostringstream ex; ex << "[syntax error] expected pair in the form " "<key" << separator << "value>, but found <" << text << ">"; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } std::pair<std::string, std::string> result; result.first = text.substr(0, half); @@ -286,32 +309,8 @@ namespace fl { return result; } - std::string FllImporter::clean(const std::string& line) const { - if (line.empty()) return line; - if (line.size() == 1) return isspace(line.at(0)) ? "" : line; - int start = 0, end = line.size() - 1; - while (start <= end and isspace(line.at(start))) { - ++start; - } - int sharp = start; - while (sharp <= end) { - if (line.at(sharp) == '#') { - end = sharp - 1; - break; - } - ++sharp; - } - while (end >= start and (line.at(end) == '#' or isspace(line.at(end)))) { - --end; - } - - int length = end - start + 1; - return line.substr(start, length); - } - FllImporter* FllImporter::clone() const { return new FllImporter(*this); } - } diff --git a/fuzzylite/src/imex/Importer.cpp b/fuzzylite/src/imex/Importer.cpp index faef71d..c0be7f5 100644 --- a/fuzzylite/src/imex/Importer.cpp +++ b/fuzzylite/src/imex/Importer.cpp @@ -1,46 +1,34 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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> -#include <ostream> namespace fl { - Importer::Importer() { - } + Importer::Importer() { } - Importer::~Importer() { - - } + Importer::~Importer() { } Engine* Importer::fromFile(const std::string& path) const { std::ifstream reader(path.c_str()); if (not reader.is_open()) { - throw fl::Exception("[file error] file <" + path + "> could not be opened", FL_AT); + throw Exception("[file error] file <" + path + "> could not be opened", FL_AT); } std::ostringstream textEngine; std::string line; diff --git a/fuzzylite/src/imex/JavaExporter.cpp b/fuzzylite/src/imex/JavaExporter.cpp index a4948f8..6981dae 100644 --- a/fuzzylite/src/imex/JavaExporter.cpp +++ b/fuzzylite/src/imex/JavaExporter.cpp @@ -1,60 +1,59 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" -#include <algorithm> namespace fl { - JavaExporter::JavaExporter() : Exporter() { - } + JavaExporter::JavaExporter(bool usingVariableNames) : Exporter(), + _usingVariableNames(usingVariableNames) { } - JavaExporter::~JavaExporter() { - - } + 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 (int i = 0; i < engine->numberOfInputVariables(); ++i) { + for (std::size_t i = 0; i < engine->numberOfInputVariables(); ++i) { ss << toString(engine->getInputVariable(i), engine) << "\n"; } - for (int i = 0; i < engine->numberOfOutputVariables(); ++i) { + for (std::size_t i = 0; i < engine->numberOfOutputVariables(); ++i) { ss << toString(engine->getOutputVariable(i), engine) << "\n"; } - for (int i = 0; i < engine->numberOfRuleBlocks(); ++i) { + for (std::size_t i = 0; i < engine->numberOfRuleBlocks(); ++i) { ss << toString(engine->getRuleBlock(i), engine) << "\n"; } @@ -63,21 +62,28 @@ namespace fl { std::string JavaExporter::toString(const InputVariable* inputVariable, const Engine* engine) const { std::ostringstream ss; - std::string name = "inputVariable"; - if (engine->numberOfInputVariables() > 1) { - int index = std::distance(engine->inputVariables().begin(), - std::find(engine->inputVariables().begin(), - engine->inputVariables().end(), inputVariable)); - name += Op::str<int>(index + 1); + 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 << ".setEnabled(" << (inputVariable->isEnabled() ? "true" : "false") << ");\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"; - - for (int i = 0; i < inputVariable->numberOfTerms(); ++i) { + 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"; } @@ -87,30 +93,36 @@ namespace fl { std::string JavaExporter::toString(const OutputVariable* outputVariable, const Engine* engine) const { std::ostringstream ss; - std::string name = "outputVariable"; - if (engine->numberOfOutputVariables() > 1) { - int index = std::distance(engine->outputVariables().begin(), - std::find(engine->outputVariables().begin(), - engine->outputVariables().end(), outputVariable)); - name += Op::str<int>(index + 1); + 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 << ".setEnabled(" << (outputVariable->isEnabled() ? "true" : "false") << ");\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 << ".fuzzyOutput().setAccumulation(" << - toString(outputVariable->fuzzyOutput()->getAccumulation()) << ");\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 << ".setLockPreviousOutputValue(" << - (outputVariable->isLockedPreviousOutputValue() ? "true" : "false") << ");\n"; - ss << name << ".setLockOutputValueInRange(" << - (outputVariable->isLockedOutputValueInRange() ? "true" : "false") << ");\n"; - for (int i = 0; i < outputVariable->numberOfTerms(); ++i) { + 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"; } @@ -120,23 +132,33 @@ namespace fl { std::string JavaExporter::toString(const RuleBlock* ruleBlock, const Engine* engine) const { std::ostringstream ss; - std::string name = "ruleBlock"; - if (engine->numberOfRuleBlocks() > 1) { - int index = std::distance(engine->ruleBlocks().begin(), - std::find(engine->ruleBlocks().begin(), - engine->ruleBlocks().end(), ruleBlock)); - name += Op::str<int>(index + 1); + 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 << ".setEnabled(" << (ruleBlock->isEnabled() ? "true" : "false") << ");\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 (int i = 0; i < ruleBlock->numberOfRules(); ++i) { + for (std::size_t i = 0; i < ruleBlock->numberOfRules(); ++i) { Rule* rule = ruleBlock->getRule(i); ss << name << ".addRule(Rule.parse(\"" << rule->getText() << "\", engine));\n"; } @@ -183,7 +205,7 @@ namespace fl { if (const IntegralDefuzzifier * integralDefuzzifier = dynamic_cast<const IntegralDefuzzifier*> (defuzzifier)) { return "new " + integralDefuzzifier->className() + "(" - + fl::Op::str(integralDefuzzifier->getResolution()) + ")"; + + Op::str(integralDefuzzifier->getResolution()) + ")"; } if (const WeightedDefuzzifier * weightedDefuzzifier = dynamic_cast<const WeightedDefuzzifier*> (defuzzifier)) { @@ -193,19 +215,22 @@ namespace fl { 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 TNorm* norm) const { + std::string JavaExporter::toString(const Norm* norm) const { if (not norm) return "null"; return "new " + norm->className() + "()"; } - std::string JavaExporter::toString(const SNorm* 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 { @@ -223,6 +248,4 @@ namespace fl { 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/m/compare.m b/fuzzylite/src/m/compare.m deleted file mode 100755 index 56da68d..0000000 --- a/fuzzylite/src/m/compare.m +++ /dev/null @@ -1,60 +0,0 @@ -%{/* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved - - This file is part of fuzzylite. - - fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. - - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzylite (R) is a registered trademark of FuzzyLite Limited. - */ -}% - -function [engine] = compare(fisFile, fldFile, delimiter, hasMetadata) -if (nargin < 3) - delimiter = ' '; -end -if (nargin < 4) - hasMetadata = true; -end - -engine = readfis(fisFile); -flMatrix = dlmread(fldFile, delimiter, hasMetadata ~ = 0, 0); - -if (length(engine.input) + length(engine.output) ~ = size(flMatrix, 2)) - error('fuzzylite:compare.m', 'Number of inputs and outputs in engine differ from FLD matrix'); -end - -if (isempty(engine.andMethod)) - engine.andMethod = 'min'; -end -if (isempty(engine.orMethod)) - engine.orMethod = 'max'; -end - -engine.inputValues = flMatrix(1 : end, 1 : length(engine.input)); -engine.outputValues = evalfis(engine.inputValues, engine); -engine.flOutputValues = flMatrix(1 : end, (length(engine.input) + 1) : (length(engine.input) + length(engine.output))); -engine.outputDiff = engine.outputValues - engine.flOutputValues; -engine.fld = [engine.inputValues engine.outputValues engine.flOutputValues engine.outputDiff]; -engine.nanfreeDiff = engine.outputDiff; -engine.nanfreeDiff(find(isnan(engine.nanfreeDiff))) = 0; -engine.mse = nansum(engine.outputDiff.^2) / size(engine.outputDiff, 1); -engine.quantiles = prctile(engine.nanfreeDiff, 0 : 25 : 100); - -end - - - diff --git a/fuzzylite/src/m/compare_examples.m b/fuzzylite/src/m/compare_examples.m deleted file mode 100755 index 1bf6818..0000000 --- a/fuzzylite/src/m/compare_examples.m +++ /dev/null @@ -1,45 +0,0 @@ -%{/*
- Author: Juan Rada-Vilela, Ph.D.
- Copyright (C) 2010-2014 FuzzyLite Limited
- All rights reserved
-
- This file is part of fuzzylite.
-
- fuzzylite is free software: you can redistribute it and/or modify it under
- the terms of the GNU Lesser General Public License as published by the Free
- Software Foundation, either version 3 of the License, or (at your option)
- any later version.
-
- fuzzylite is distributed in the hope that it will be useful, but WITHOUT
- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
- for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with fuzzylite. If not, see <http://www.gnu.org/licenses/>.
-
- fuzzylite (R) is a registered trademark of FuzzyLite Limited.
- */
-}%
-
-
-function [ engines ] = compare_examples(path, delimiter, hasMetadata)
-if (nargin < 2)
- delimiter = ' ';
-end
-if (nargin < 3)
- hasMetadata = true;
-end
-
-examples = {'\mamdani\SimpleDimmer', '\mamdani\matlab\mam21', '\mamdani\matlab\mam22', '\mamdani\matlab\shower', '\mamdani\matlab\tank', '\mamdani\matlab\tank2', '\mamdani\matlab\tipper', '\mamdani\matlab\tipper1', '\mamdani\octave\mamdani_tip_calculator', '\takagi-sugeno\SimpleDimmer', '\takagi-sugeno\matlab\fpeaks', '\takagi-sugeno\matlab\invkine1', '\takagi-sugeno\matlab\invkine2', '\takagi-sugeno\matlab\juggler', '\takagi-sugeno\matlab\membrn1', '\takagi-sugeno\matlab\membrn2', '\takagi-sugeno\matlab\slbb', '\takagi-sugeno\matlab\slcp', '\takagi-sugeno\matlab\slcp1', '\takagi-sugeno\matlab\slcpp1', '\takagi-sugeno\matlab\sltbu_fl', '\takagi-sugeno\matlab\sugeno1', '\takagi-sugeno\matlab\tanksg', '\takagi-sugeno\matlab\tippersg', '\takagi-sugeno\octave\cubic_approximator', '\takagi-sugeno\octave\heart_disease_risk', '\takagi-sugeno\octave\linear_tip_calculator'};
-pending = {'\mamdani\octave\investment_portfolio', '\takagi-sugeno\approximation', '\takagi-sugeno\octave\sugeno_tip_calculator', '\tsukamoto\tsukamoto'};
-engines = [];
-for i = 1 : length(examples)
- fisFile = strcat(path, examples{i}, '.fis')
- fldFile = strcat(path, examples{i}, '.fld');
- engines = [engines compare(fisFile, fldFile, delimiter, hasMetadata)];
- disp(strcat('Five number summary (', num2str(i), '): ', fisFile));
- engines(i).quantiles
-end
-end
-
diff --git a/fuzzylite/src/main.cpp b/fuzzylite/src/main.cpp index 6d8d701..06ca139 100644 --- a/fuzzylite/src/main.cpp +++ b/fuzzylite/src/main.cpp @@ -1,70 +1,49 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 <cstdlib> #include <fstream> -#include <iomanip> -#include <signal.h> -#include <typeinfo> - -using namespace fl; - -/* -#ifdef FL_WINDOWS -#include <windows.h> -BOOL WINAPI flSignalHandler(DWORD dwCtrlType) -{ - FL_LOG("Signal: " << dwCtrlType); - if (CTRL_C_EVENT == dwCtrlType){ - } - //return FALSE; - return TRUE; -} -#endif - */ +#include <csignal> -int main(int argc, char** argv) { - (void) argc; - (void) argv; +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); + ::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); + ::signal(SIGBUS, fl::Exception::signalHandler); + ::signal(SIGPIPE, fl::Exception::signalHandler); #endif #ifdef FL_WINDOWS //SetConsoleCtrlHandler(flSignalHandler, TRUE); #endif - fuzzylite::setDebug(false); - return Console::main(argc, argv); + 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 index e96c1a7..0497f23 100644 --- a/fuzzylite/src/norm/s/AlgebraicSum.cpp +++ b/fuzzylite/src/norm/s/AlgebraicSum.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -30,6 +22,10 @@ namespace fl { return "AlgebraicSum"; } + Complexity AlgebraicSum::complexity() const { + return Complexity().arithmetic(3); + } + scalar AlgebraicSum::compute(scalar a, scalar b) const { return a + b - (a * b); } diff --git a/fuzzylite/src/norm/s/BoundedSum.cpp b/fuzzylite/src/norm/s/BoundedSum.cpp index 9c050c8..b1cb42b 100644 --- a/fuzzylite/src/norm/s/BoundedSum.cpp +++ b/fuzzylite/src/norm/s/BoundedSum.cpp @@ -1,35 +1,33 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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); } diff --git a/fuzzylite/src/norm/s/DrasticSum.cpp b/fuzzylite/src/norm/s/DrasticSum.cpp index c0629db..b0d9f1e 100644 --- a/fuzzylite/src/norm/s/DrasticSum.cpp +++ b/fuzzylite/src/norm/s/DrasticSum.cpp @@ -1,35 +1,33 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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); diff --git a/fuzzylite/src/norm/s/EinsteinSum.cpp b/fuzzylite/src/norm/s/EinsteinSum.cpp index ec0fbd8..a638c7b 100644 --- a/fuzzylite/src/norm/s/EinsteinSum.cpp +++ b/fuzzylite/src/norm/s/EinsteinSum.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -30,6 +22,10 @@ namespace fl { 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); } diff --git a/fuzzylite/src/norm/s/HamacherSum.cpp b/fuzzylite/src/norm/s/HamacherSum.cpp index f9c0994..41242ee 100644 --- a/fuzzylite/src/norm/s/HamacherSum.cpp +++ b/fuzzylite/src/norm/s/HamacherSum.cpp @@ -1,36 +1,35 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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); } diff --git a/fuzzylite/src/norm/s/Maximum.cpp b/fuzzylite/src/norm/s/Maximum.cpp index ed8a839..5277d49 100644 --- a/fuzzylite/src/norm/s/Maximum.cpp +++ b/fuzzylite/src/norm/s/Maximum.cpp @@ -1,35 +1,33 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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); } diff --git a/fuzzylite/src/norm/s/NilpotentMaximum.cpp b/fuzzylite/src/norm/s/NilpotentMaximum.cpp index 0a09136..7c63999 100644 --- a/fuzzylite/src/norm/s/NilpotentMaximum.cpp +++ b/fuzzylite/src/norm/s/NilpotentMaximum.cpp @@ -1,38 +1,36 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 std::max(a, b); + return Op::max(a, b); } return 1.0; } @@ -45,6 +43,4 @@ namespace fl { return new NilpotentMaximum; } - } - diff --git a/fuzzylite/src/norm/s/NormalizedSum.cpp b/fuzzylite/src/norm/s/NormalizedSum.cpp index 94ad5ea..c420ed1 100644 --- a/fuzzylite/src/norm/s/NormalizedSum.cpp +++ b/fuzzylite/src/norm/s/NormalizedSum.cpp @@ -1,37 +1,35 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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), Op::max(a, b)); + return (a + b) / Op::max(scalar(1.0), a + b); } NormalizedSum* NormalizedSum::clone() const { @@ -42,5 +40,4 @@ namespace fl { 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 index eee1061..eefe309 100644 --- a/fuzzylite/src/norm/t/AlgebraicProduct.cpp +++ b/fuzzylite/src/norm/t/AlgebraicProduct.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -30,6 +22,10 @@ namespace fl { return "AlgebraicProduct"; } + Complexity AlgebraicProduct::complexity() const { + return Complexity().arithmetic(1); + } + scalar AlgebraicProduct::compute(scalar a, scalar b) const { return a * b; } diff --git a/fuzzylite/src/norm/t/BoundedDifference.cpp b/fuzzylite/src/norm/t/BoundedDifference.cpp index aa4fead..3775aa4 100644 --- a/fuzzylite/src/norm/t/BoundedDifference.cpp +++ b/fuzzylite/src/norm/t/BoundedDifference.cpp @@ -1,29 +1,22 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 { @@ -31,6 +24,10 @@ namespace fl { 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)); } diff --git a/fuzzylite/src/norm/t/DrasticProduct.cpp b/fuzzylite/src/norm/t/DrasticProduct.cpp index 124b79d..5bfdc87 100644 --- a/fuzzylite/src/norm/t/DrasticProduct.cpp +++ b/fuzzylite/src/norm/t/DrasticProduct.cpp @@ -1,35 +1,33 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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); diff --git a/fuzzylite/src/norm/t/EinsteinProduct.cpp b/fuzzylite/src/norm/t/EinsteinProduct.cpp index ea79188..bbb3966 100644 --- a/fuzzylite/src/norm/t/EinsteinProduct.cpp +++ b/fuzzylite/src/norm/t/EinsteinProduct.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -30,6 +22,10 @@ namespace fl { 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)); } diff --git a/fuzzylite/src/norm/t/HamacherProduct.cpp b/fuzzylite/src/norm/t/HamacherProduct.cpp index e3f093e..9416084 100644 --- a/fuzzylite/src/norm/t/HamacherProduct.cpp +++ b/fuzzylite/src/norm/t/HamacherProduct.cpp @@ -1,29 +1,22 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 { @@ -31,7 +24,12 @@ namespace fl { 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); } diff --git a/fuzzylite/src/norm/t/Minimum.cpp b/fuzzylite/src/norm/t/Minimum.cpp index 34f348f..1a63658 100644 --- a/fuzzylite/src/norm/t/Minimum.cpp +++ b/fuzzylite/src/norm/t/Minimum.cpp @@ -1,35 +1,33 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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); } diff --git a/fuzzylite/src/norm/t/NilpotentMinimum.cpp b/fuzzylite/src/norm/t/NilpotentMinimum.cpp index b52a2fc..7ab906b 100644 --- a/fuzzylite/src/norm/t/NilpotentMinimum.cpp +++ b/fuzzylite/src/norm/t/NilpotentMinimum.cpp @@ -1,35 +1,33 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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); @@ -47,4 +45,3 @@ namespace fl { } - 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 index aa18a68..5f0b0be 100644 --- a/fuzzylite/src/rule/Antecedent.cpp +++ b/fuzzylite/src/rule/Antecedent.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -28,29 +20,21 @@ #include "fl/factory/HedgeFactory.h" #include "fl/factory/FactoryManager.h" #include "fl/hedge/Any.h" -#include "fl/hedge/Hedge.h" -#include "fl/norm/SNorm.h" -#include "fl/norm/TNorm.h" #include "fl/rule/Expression.h" #include "fl/rule/Rule.h" -#include "fl/term/Accumulated.h" -#include "fl/term/Function.h" -#include "fl/term/Term.h" +#include "fl/term/Aggregated.h" #include "fl/variable/InputVariable.h" #include "fl/variable/OutputVariable.h" -#include <algorithm> #include <stack> - namespace fl { Antecedent::Antecedent() - : _text(""), _expression(fl::null) { - } + : _text(""), _expression(fl::null) { } Antecedent::~Antecedent() { - unload(); + _expression.reset(fl::null); } void Antecedent::setText(const std::string& text) { @@ -62,24 +46,29 @@ namespace fl { } Expression* Antecedent::getExpression() const { - return this->_expression; + return this->_expression.get(); + } + + void Antecedent::setExpression(Expression* expression) { + this->_expression.reset(expression); } bool Antecedent::isLoaded() const { - return this->_expression != fl::null; + return _expression.get() != fl::null; } scalar Antecedent::activationDegree(const TNorm* conjunction, const SNorm* disjunction) const { - return this->activationDegree(conjunction, disjunction, this->_expression); + return this->activationDegree(conjunction, disjunction, _expression.get()); } scalar Antecedent::activationDegree(const TNorm* conjunction, const SNorm* disjunction, const Expression* node) const { if (not isLoaded()) { - throw fl::Exception("[antecedent error] antecedent <" + _text + "> is not loaded", FL_AT); + throw Exception("[antecedent error] antecedent <" + getText() + "> is not loaded", FL_AT); } - const Proposition* proposition = dynamic_cast<const Proposition*> (node); - if (proposition) { + 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; } @@ -96,62 +85,147 @@ namespace fl { } } scalar result = fl::nan; - if (InputVariable * inputVariable = dynamic_cast<InputVariable*> (proposition->variable)) { - result = proposition->term->membership(inputVariable->getInputValue()); - } else if (OutputVariable * outputVariable = dynamic_cast<OutputVariable*> (proposition->variable)) { - result = outputVariable->fuzzyOutput()->activationDegree(proposition->term); + 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); } - for (std::vector<Hedge*>::const_reverse_iterator rit = proposition->hedges.rbegin(); - rit != proposition->hedges.rend(); ++rit) { - result = (*rit)->hedge(result); + + 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 - const Operator* fuzzyOperator = dynamic_cast<const Operator*> (node); - if (not (fuzzyOperator->left and fuzzyOperator->right)) { + 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] left and right operands must exist"; - throw fl::Exception(ex.str(), FL_AT); + 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); } - if (fuzzyOperator->name == Rule::andKeyword()) { - if (not conjunction) throw fl::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)); + } + + + 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(); } - if (fuzzyOperator->name == Rule::orKeyword()) { - if (not disjunction) throw fl::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)); + 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; } - std::ostringstream ex; - ex << "[syntax error] operator <" << fuzzyOperator->name << "> not recognized"; - throw fl::Exception(ex.str(), FL_AT); + //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() { - if (_expression) { - delete _expression; - _expression = fl::null; - } + _expression.reset(fl::null); } - void Antecedent::load(fl::Rule* rule, const Engine* engine) { - load(_text, rule, engine); + void Antecedent::load(const Engine* engine) { + load(getText(), engine); } - void Antecedent::load(const std::string& antecedent, fl::Rule* rule, const Engine* engine) { + void Antecedent::load(const std::string& antecedent, const Engine* engine) { FL_DBG("Antecedent: " << antecedent); unload(); - this->_text = antecedent; - if (fl::Op::trim(antecedent).empty()) { - throw fl::Exception("[syntax error] antecedent is empty", FL_AT); + 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. @@ -179,8 +253,10 @@ namespace fl { 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 (engine->hasInputVariable(token)) + variable = engine->getInputVariable(token); + else if (engine->hasOutputVariable(token)) + variable = engine->getOutputVariable(token); if (variable) { proposition = new Proposition; proposition->variable = variable; @@ -201,15 +277,9 @@ namespace fl { } if (state bitand S_HEDGE) { - Hedge* hedge = rule->getHedge(token); - if (not hedge) { - HedgeFactory* factory = FactoryManager::instance()->hedge(); - if (factory->hasConstructor(token)) { - hedge = factory->constructObject(token); - rule->addHedge(hedge); - } - } - if (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; @@ -236,7 +306,7 @@ namespace fl { std::ostringstream ex; ex << "[syntax error] logical operator <" << token << "> expects two operands," << "but found <" << expressionStack.size() << "> in antecedent"; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } Operator* fuzzyOperator = new Operator; fuzzyOperator->name = token; @@ -258,33 +328,33 @@ namespace fl { 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 fl::Exception(ex.str(), FL_AT); + 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 fl::Exception(ex.str(), FL_AT); + 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 fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } std::ostringstream ex; ex << "[syntax error] unexpected token <" << token << "> in antecedent"; - throw fl::Exception(ex.str(), FL_AT); + 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 fl::Exception(ex.str(), FL_AT); + 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 fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } } @@ -299,7 +369,7 @@ namespace fl { std::ostringstream ex; ex << "[syntax error] unable to parse the following expressions in antecedent <" << Op::join(errors, " ") << ">"; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } } catch (...) { for (std::size_t i = 0; i < expressionStack.size(); ++i) { @@ -308,59 +378,68 @@ namespace fl { } throw; } - this->_expression = expressionStack.top(); + setExpression(expressionStack.top()); } std::string Antecedent::toString() const { - return toInfix(this->_expression); + return toInfix(getExpression()); } std::string Antecedent::toPrefix(const Expression* node) const { if (not isLoaded()) { - throw fl::Exception("[antecedent error] antecedent <" + _text + "> is not loaded", FL_AT); + throw Exception("[antecedent error] antecedent <" + _text + "> is not loaded", FL_AT); } - if (not node) node = this->_expression; + if (not node) node = getExpression(); if (dynamic_cast<const Proposition*> (node)) { return node->toString(); } - const Operator* fuzzyOperator = dynamic_cast<const Operator*> (node); std::stringstream ss; - ss << fuzzyOperator->toString() << " " - << toPrefix(fuzzyOperator->left) << " " - << toPrefix(fuzzyOperator->right) << " "; + 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 fl::Exception("[antecedent error] antecedent <" + _text + "> is not loaded", FL_AT); + throw Exception("[antecedent error] antecedent <" + _text + "> is not loaded", FL_AT); } - if (not node) node = this->_expression; + if (not node) node = getExpression(); if (dynamic_cast<const Proposition*> (node)) { return node->toString(); } - const Operator* fuzzyOperator = dynamic_cast<const Operator*> (node); std::stringstream ss; - ss << toInfix(fuzzyOperator->left) << " " - << fuzzyOperator->toString() << " " - << toInfix(fuzzyOperator->right) << " "; + 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 fl::Exception("[antecedent error] antecedent <" + _text + "> is not loaded", FL_AT); + throw Exception("[antecedent error] antecedent <" + _text + "> is not loaded", FL_AT); } - if (not node) node = this->_expression; + if (not node) node = getExpression(); if (dynamic_cast<const Proposition*> (node)) { return node->toString(); } - const Operator* fuzzyOperator = dynamic_cast<const Operator*> (node); std::stringstream ss; - ss << toPostfix(fuzzyOperator->left) << " " - << toPostfix(fuzzyOperator->right) << " " - << fuzzyOperator->toString() << " "; + 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 index 28d3390..a90b1a9 100644 --- a/fuzzylite/src/rule/Consequent.cpp +++ b/fuzzylite/src/rule/Consequent.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -27,24 +19,21 @@ #include "fl/Engine.h" #include "fl/factory/HedgeFactory.h" #include "fl/factory/FactoryManager.h" -#include "fl/hedge/Hedge.h" #include "fl/hedge/Any.h" -#include "fl/norm/TNorm.h" #include "fl/rule/Expression.h" #include "fl/rule/Rule.h" -#include "fl/term/Accumulated.h" -#include "fl/term/Activated.h" +#include "fl/term/Aggregated.h" #include "fl/variable/OutputVariable.h" -#include <algorithm> - namespace fl { - Consequent::Consequent() { - } + Consequent::Consequent() { } Consequent::~Consequent() { - unload(); + for (std::size_t i = 0; i < _conclusions.size(); ++i) { + delete _conclusions.at(i); + } + _conclusions.clear(); } std::string Consequent::getText() const { @@ -59,9 +48,29 @@ namespace fl { return this->_conclusions; } - void Consequent::modify(scalar activationDegree, const TNorm* activation) { + 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 fl::Exception("[consequent error] consequent <" + _text + "> is not loaded", FL_AT); + 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); @@ -72,10 +81,9 @@ namespace fl { activationDegree = (*rit)->hedge(activationDegree); } } - Activated* term = new Activated(_conclusions.at(i)->term, activationDegree, activation); - OutputVariable* outputVariable = dynamic_cast<OutputVariable*> (proposition->variable); - outputVariable->fuzzyOutput()->addTerm(term); - FL_DBG("Accumulating " << term->toString()); + + static_cast<OutputVariable*> (proposition->variable)->fuzzyOutput() + ->addTerm(proposition->term, activationDegree, implication); } } } @@ -91,16 +99,16 @@ namespace fl { _conclusions.clear(); } - void Consequent::load(Rule* rule, const Engine* engine) { - load(_text, rule, engine); + void Consequent::load(const Engine* engine) { + load(getText(), engine); } - void Consequent::load(const std::string& consequent, Rule* rule, const Engine* engine) { + void Consequent::load(const std::string& consequent, const Engine* engine) { unload(); - this->_text = consequent; + setText(consequent); - if (fl::Op::trim(consequent).empty()) { - throw fl::Exception("[syntax error] consequent is empty", FL_AT); + if (Op::trim(consequent).empty()) { + throw Exception("[syntax error] consequent is empty", FL_AT); } /** @@ -129,8 +137,7 @@ namespace fl { if (engine->hasOutputVariable(token)) { proposition = new Proposition; proposition->variable = engine->getOutputVariable(token); - _conclusions.push_back(proposition); - + conclusions().push_back(proposition); state = S_IS; continue; } @@ -144,15 +151,9 @@ namespace fl { } if (state bitand S_HEDGE) { - Hedge* hedge = rule->getHedge(token); - if (not hedge) { - HedgeFactory* factory = FactoryManager::instance()->hedge(); - if (factory->hasConstructor(token)){ - hedge = factory->constructObject(token); - rule->addHedge(hedge); - } - } - if (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; @@ -178,19 +179,19 @@ namespace fl { if (state bitand S_VARIABLE) { std::ostringstream ex; ex << "[syntax error] consequent expected output variable, but found <" << token << ">"; - throw fl::Exception(ex.str(), FL_AT); + 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 fl::Exception(ex.str(), FL_AT); + 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 fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } if ((state bitand S_AND) or (state bitand S_WITH)) { @@ -198,30 +199,30 @@ namespace fl { ex << "[syntax error] consequent expected operator <" << Rule::andKeyword() << "> " << "or keyword <" << Rule::withKeyword() << ">, " << "but found <" << token << ">"; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } std::ostringstream ex; ex << "[syntax error] unexpected token <" << token << "> in consequent"; - throw fl::Exception(ex.str(), FL_AT); + 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 fl::Exception(ex.str(), FL_AT); + 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 fl::Exception(ex.str(), FL_AT); + 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 fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } } } catch (...) { @@ -232,9 +233,9 @@ namespace fl { 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()) + 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 index ea7873e..9b0ab81 100644 --- a/fuzzylite/src/rule/Expression.cpp +++ b/fuzzylite/src/rule/Expression.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -31,18 +23,22 @@ namespace fl { - Expression::Expression() { - } + Expression::Expression() { } - Expression::~Expression() { - } + Expression::~Expression() { } - Proposition::Proposition() - : Expression(), variable(fl::null), term(fl::null) { - } + 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 { @@ -68,14 +64,18 @@ namespace fl { return ss.str(); } - Operator::Operator() : Expression(), name(""), left(fl::null), right(fl::null) { - } + 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 index 446290b..0338cd9 100644 --- a/fuzzylite/src/rule/Rule.cpp +++ b/fuzzylite/src/rule/Rule.cpp @@ -1,55 +1,43 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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/hedge/Hedge.h" #include "fl/imex/FllExporter.h" #include "fl/norm/Norm.h" -#include "fl/rule/Antecedent.h" -#include "fl/rule/Consequent.h" - -#include <sstream> -#include <vector> +#include "fl/Operation.h" namespace fl { Rule::Rule(const std::string& text, scalar weight) - : _text(text), _weight(weight), _antecedent(new Antecedent), _consequent(new Consequent) { - } + : _enabled(true), _text(text), _weight(weight), _activationDegree(0.0), _triggered(false), + _antecedent(new Antecedent), _consequent(new Consequent) { } - Rule::Rule(const Rule& other) : _text(other._text), _weight(other._weight), - _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) { - unload(); - + _enabled = other._enabled; _text = other._text; _weight = other._weight; + _activationDegree = other._activationDegree; + _triggered = other._triggered; _antecedent.reset(new Antecedent); _consequent.reset(new Consequent); } @@ -57,7 +45,8 @@ namespace fl { } Rule::~Rule() { - unload(); + if (_antecedent.get()) _antecedent->unload(); + if (_consequent.get()) _consequent->unload(); } void Rule::setText(const std::string& text) { @@ -92,87 +81,93 @@ namespace fl { return this->_consequent.get(); } - /** - * Operations for std::vector _hedges - */ - void Rule::addHedge(Hedge* hedge) { - this->_hedges[hedge->name()] = hedge; + void Rule::setEnabled(bool active) { + this->_enabled = active; } - Hedge* Rule::getHedge(const std::string& name) const { - std::map<std::string, Hedge*>::const_iterator it = this->_hedges.find(name); - if (it != this->_hedges.end()) { - if (it->second) return it->second; - } - return fl::null; + bool Rule::isEnabled() const { + return this->_enabled; } - Hedge* Rule::removeHedge(const std::string& name) { - Hedge* result = fl::null; - std::map<std::string, Hedge*>::iterator it = this->_hedges.find(name); - if (it != this->_hedges.end()) { - result = it->second; - this->_hedges.erase(it); - } - return result; + void Rule::setActivationDegree(scalar activationDegree) { + this->_activationDegree = activationDegree; } - bool Rule::hasHedge(const std::string& name) const { - std::map<std::string, Hedge*>::const_iterator it = this->_hedges.find(name); - return (it != this->_hedges.end()); + scalar Rule::getActivationDegree() const { + return this->_activationDegree; } - int Rule::numberOfHedges() const { - return this->_hedges.size(); + void Rule::deactivate() { + _activationDegree = 0.0; + _triggered = false; } - void Rule::setHedges(const std::map<std::string, Hedge*>& hedges) { - this->_hedges = hedges; + 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; } - const std::map<std::string, Hedge*>& Rule::hedges() const { - return this->_hedges; + 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; + } } - std::map<std::string, Hedge*>& Rule::hedges() { - return this->_hedges; + bool Rule::isTriggered() const { + return this->_triggered; } - scalar Rule::activationDegree(const TNorm* conjunction, const SNorm* disjunction) const { - if (not isLoaded()) { - throw fl::Exception("[rule error] the following rule is not loaded: " + _text, FL_AT); + 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 _weight * getAntecedent()->activationDegree(conjunction, disjunction); + return result; } - void Rule::activate(scalar degree, const TNorm* activation) const { - if (not isLoaded()) { - throw fl::Exception("[rule error] the following rule is not loaded: " + _text, FL_AT); + Complexity Rule::complexityOfFiring(const TNorm* implication) const { + Complexity result; + result.comparison(3); + if (isLoaded()) { + result += _consequent->complexity(implication); } - _consequent->modify(degree, activation); + 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->isLoaded() and _consequent->isLoaded(); + return _antecedent.get() and _consequent.get() + and _antecedent->isLoaded() and _consequent->isLoaded(); } void Rule::unload() { - _antecedent->unload(); - _consequent->unload(); - - for (std::map<std::string, Hedge*>::const_iterator it = _hedges.begin(); - it != _hedges.end(); ++it) { - delete it->second; - } - _hedges.clear(); + deactivate(); + if (getAntecedent()) getAntecedent()->unload(); + if (getConsequent()) getConsequent()->unload(); } void Rule::load(const Engine* engine) { - load(_text, engine); + load(getText(), engine); } void Rule::load(const std::string& rule, const Engine* engine) { - this->_text = rule; + deactivate(); + setEnabled(true); + setText(rule); std::istringstream tokenizer(rule.substr(0, rule.find_first_of('#'))); std::string token; std::ostringstream ossAntecedent, ossConsequent; @@ -192,7 +187,7 @@ namespace fl { std::ostringstream ex; ex << "[syntax error] expected keyword <" << Rule::ifKeyword() << ">, but found <" << token << "> in rule: " << rule; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } break; case S_IF: @@ -205,39 +200,46 @@ namespace fl { break; case S_WITH: try { - weight = fl::Op::toScalar(token); + weight = Op::toScalar(token); state = S_END; - } catch (fl::Exception& e) { + } 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 e; + throw; } break; case S_END: + { std::ostringstream ex; ex << "[syntax error] unexpected token <" << token << "> at the end of rule"; - throw fl::Exception(ex.str(), FL_AT); + 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 fl::Exception(ex.str(), FL_AT); + 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 fl::Exception(ex.str(), FL_AT); + 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 fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } - _antecedent->load(ossAntecedent.str(), this, engine); - _consequent->load(ossConsequent.str(), this, engine); - _weight = weight; + getAntecedent()->load(ossAntecedent.str(), engine); + getConsequent()->load(ossConsequent.str(), engine); + setWeight(weight); } catch (...) { unload(); diff --git a/fuzzylite/src/rule/RuleBlock.cpp b/fuzzylite/src/rule/RuleBlock.cpp index 9ab813e..d4e2a82 100644 --- a/fuzzylite/src/rule/RuleBlock.cpp +++ b/fuzzylite/src/rule/RuleBlock.cpp @@ -1,44 +1,35 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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 <sstream> +#include "fl/Operation.h" namespace fl { RuleBlock::RuleBlock(const std::string& name) - : _name(name), _enabled(true) { - } + : _enabled(true), _name(name), _description("") { } - RuleBlock::RuleBlock(const RuleBlock& other) : _name(other._name), - _enabled(true) { + RuleBlock::RuleBlock(const RuleBlock& other) : _enabled(true), _name(other._name), + _description(other._description) { copyFrom(other); } @@ -50,6 +41,7 @@ namespace fl { _rules.clear(); _conjunction.reset(fl::null); _disjunction.reset(fl::null); + _implication.reset(fl::null); _activation.reset(fl::null); copyFrom(other); @@ -58,11 +50,13 @@ namespace fl { } void RuleBlock::copyFrom(const RuleBlock& source) { - _name = source._name; _enabled = source._enabled; - if (source._activation.get()) _activation.reset(source._activation->clone()); + _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()); } @@ -75,21 +69,25 @@ namespace fl { _rules.clear(); } - void RuleBlock::activate() { - FL_DBG("==================="); - FL_DBG("ACTIVATING RULEBLOCK " << _name); - for (std::size_t i = 0; i < _rules.size(); ++i) { - Rule* rule = _rules.at(i); - if (rule->isLoaded()) { - scalar activationDegree = rule->activationDegree(_conjunction.get(), _disjunction.get()); - FL_DBG("[degree=" << Op::str(activationDegree) << "] " << rule->toString()); - if (Op::isGt(activationDegree, 0.0)) { - rule->activate(activationDegree, _activation.get()); - } - } else { - FL_DBG("Rule not loaded: " << rule->toString()); + 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 { @@ -114,7 +112,7 @@ namespace fl { } } if (throwException) { - fl::Exception exception("[ruleblock error] the following " + Exception exception("[ruleblock error] the following " "rules could not be loaded:\n" + exceptions.str(), FL_AT); throw exception; } @@ -133,6 +131,14 @@ namespace fl { 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); } @@ -149,11 +155,19 @@ namespace fl { return this->_disjunction.get(); } - void RuleBlock::setActivation(TNorm* activation) { + 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); } - TNorm* RuleBlock::getActivation() const { + Activation* RuleBlock::getActivation() const { return this->_activation.get(); } @@ -173,25 +187,25 @@ namespace fl { * Operations for std::vector _rules */ void RuleBlock::addRule(Rule* rule) { - this->_rules.push_back(rule); + _rules.push_back(rule); } - void RuleBlock::insertRule(Rule* rule, int index) { - this->_rules.insert(this->_rules.begin() + index, rule); + void RuleBlock::insertRule(Rule* rule, std::size_t index) { + _rules.insert(_rules.begin() + index, rule); } - Rule* RuleBlock::getRule(int index) const { - return this->_rules.at(index); + Rule* RuleBlock::getRule(std::size_t index) const { + return _rules.at(index); } - Rule* RuleBlock::removeRule(int index) { - Rule* result = this->_rules.at(index); - this->_rules.erase(this->_rules.begin() + index); + Rule* RuleBlock::removeRule(std::size_t index) { + Rule* result = _rules.at(index); + _rules.erase(_rules.begin() + index); return result; } - int RuleBlock::numberOfRules() const { - return this->_rules.size(); + std::size_t RuleBlock::numberOfRules() const { + return _rules.size(); } const std::vector<Rule*>& RuleBlock::rules() const { @@ -206,5 +220,8 @@ namespace fl { return this->_rules; } + RuleBlock* RuleBlock::clone() const { + return new RuleBlock(*this); + } } diff --git a/fuzzylite/src/term/Accumulated.cpp b/fuzzylite/src/term/Accumulated.cpp deleted file mode 100644 index 979af9f..0000000 --- a/fuzzylite/src/term/Accumulated.cpp +++ /dev/null @@ -1,211 +0,0 @@ -/* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved - - This file is part of fuzzylite. - - fuzzylite is free software: you can redistribute it and/or modify it under - the terms of the GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. - - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. - - */ - -#include "fl/term/Accumulated.h" - -#include "fl/imex/FllExporter.h" -#include "fl/norm/SNorm.h" -#include "fl/norm/s/Maximum.h" -#include "fl/term/Activated.h" - - -namespace fl { - - Accumulated::Accumulated(const std::string& name, scalar minimum, scalar maximum, - SNorm* accumulation) - : Term(name), _minimum(minimum), _maximum(maximum), _accumulation(accumulation) { - } - - Accumulated::Accumulated(const Accumulated& other) : Term(other) { - copyFrom(other); - } - - Accumulated& Accumulated::operator=(const Accumulated& other) { - if (this != &other) { - clear(); - _accumulation.reset(fl::null); - - Term::operator=(other); - copyFrom(other); - } - return *this; - } - - Accumulated::~Accumulated() { - clear(); - } - - void Accumulated::copyFrom(const Accumulated& source) { - _minimum = source._minimum; - _maximum = source._maximum; - - if (source._accumulation.get()) - _accumulation.reset(source._accumulation->clone()); - - for (std::size_t i = 0; i < source._terms.size(); ++i) { - _terms.push_back(source._terms.at(i)->clone()); - } - } - - std::string Accumulated::className() const { - return "Accumulated"; - } - - scalar Accumulated::membership(scalar x) const { - if (fl::Op::isNaN(x)) return fl::nan; - if (not (_terms.empty() or _accumulation.get())) { //Exception for IntegralDefuzzifiers - throw fl::Exception("[accumulation error] " - "accumulation operator needed to accumulate " + toString(), FL_AT); - } - scalar mu = 0.0; - for (std::size_t i = 0; i < _terms.size(); ++i) { - mu = _accumulation->compute(mu, _terms.at(i)->membership(x)); - } - return mu; - } - - scalar Accumulated::activationDegree(const Term* forTerm) const { - scalar result = 0.0; - for (std::size_t i = 0; i < _terms.size(); ++i) { - Activated* activatedTerm = _terms.at(i); - if (activatedTerm->getTerm() == forTerm) { - if (_accumulation.get()) result = _accumulation->compute(result, activatedTerm->getDegree()); - else result += activatedTerm->getDegree(); //Default for WeightDefuzzifier - } - } - return result; - } - - std::string Accumulated::parameters() const { - FllExporter exporter; - std::ostringstream ss; - ss << exporter.toString(_accumulation.get()); - ss << " " << Op::str(_minimum) << " " << Op::str(_maximum) << " "; - for (std::size_t i = 0; i < _terms.size(); ++i) { - ss << " " << exporter.toString(_terms.at(i)); - } - return ss.str(); - } - - void Accumulated::configure(const std::string& parameters) { - (void) parameters; - } - - Accumulated* Accumulated::clone() const { - return new Accumulated(*this); - } - - std::string Accumulated::toString() const { - std::vector<std::string> accumulate; - for (std::size_t i = 0; i < _terms.size(); ++i) { - accumulate.push_back(_terms.at(i)->toString()); - } - FllExporter exporter; - std::ostringstream ss; - ss << _name << ": " << className() << " " - << exporter.toString(_accumulation.get()) << "[" - << fl::Op::join(accumulate, ",") << "]"; - return ss.str(); - } - - void Accumulated::setMinimum(scalar minimum) { - this->_minimum = minimum; - } - - scalar Accumulated::getMinimum() const { - return this->_minimum; - } - - void Accumulated::setMaximum(scalar maximum) { - this->_maximum = maximum; - } - - scalar Accumulated::getMaximum() const { - return this->_maximum; - } - - void Accumulated::setRange(scalar minimum, scalar maximum) { - setMinimum(minimum); - setMaximum(maximum); - } - - scalar Accumulated::range() const { - return this->_maximum - this->_minimum; - } - - void Accumulated::setAccumulation(SNorm* accumulation) { - this->_accumulation.reset(accumulation); - } - - SNorm* Accumulated::getAccumulation() const { - return this->_accumulation.get(); - } - - /** - * Operations for std::vector _terms - */ - - - void Accumulated::addTerm(const Term* term, scalar degree, const TNorm* activation) { - this->_terms.push_back(new Activated(term, degree, activation)); - } - - void Accumulated::addTerm(Activated* term) { - this->_terms.push_back(term); - } - - Activated* Accumulated::removeTerm(int index) { - Activated* term = this->_terms.at(index); - this->_terms.erase(this->_terms.begin() + index); - return term; - } - - void Accumulated::clear() { - for (std::size_t i = 0; i < _terms.size(); ++i) { - delete _terms.at(i); - } - _terms.clear(); - } - - Activated* Accumulated::getTerm(int index) const { - return this->_terms.at(index); - } - - const std::vector<Activated*>& Accumulated::terms() const { - return this->_terms; - } - - std::vector<Activated*>& Accumulated::terms() { - return this->_terms; - } - - int Accumulated::numberOfTerms() const { - return _terms.size(); - } - - bool Accumulated::isEmpty() const { - return _terms.empty(); - } - -} diff --git a/fuzzylite/src/term/Activated.cpp b/fuzzylite/src/term/Activated.cpp index 9a27b4b..80eda6c 100644 --- a/fuzzylite/src/term/Activated.cpp +++ b/fuzzylite/src/term/Activated.cpp @@ -1,71 +1,81 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" -#include "fl/norm/TNorm.h" namespace fl { - Activated::Activated(const Term* term, scalar degree, const TNorm* activation) - : Term(""), _term(term), _degree(degree), _activation(activation) { - if (term) this->_name = term->getName(); + Activated::Activated(const Term* term, scalar degree, const TNorm* implication) + : Term(""), _term(term), _degree(degree), _implication(implication) { + if (term) setName(term->getName()); } - Activated::~Activated() { - } + 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 (fl::Op::isNaN(x)) return fl::nan; - if (not _activation) throw fl::Exception("[activation error] " - "activation operator needed to activate " + _term->toString(), FL_AT); - return _activation->compute(this->_term->membership(x), _degree); + 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(_degree) << " " << exporter.toString(_activation) << " " - << exporter.toString(_term); + ss << Op::str(getDegree()) << " " << exporter.toString(getImplication()) << " " + << exporter.toString(getTerm()); return ss.str(); } void Activated::configure(const std::string& parameters) { - (void) parameters; + FL_IUNUSED(parameters); } std::string Activated::toString() const { FllExporter exporter; std::ostringstream ss; - ss << exporter.toString(_activation) << "(" - << Op::str(_degree) << "," - << _term->getName() << ")"; + if (getImplication()) { + ss << exporter.toString(getImplication()) << "(" + << Op::str(getDegree()) << "," + << getTerm()->getName() << ")"; + } else { + ss << "(" << Op::str(getDegree()) << "*" //"\u2297: (*)" + << getTerm()->getName() << ")"; + } return ss.str(); } @@ -85,12 +95,12 @@ namespace fl { return this->_degree; } - void Activated::setActivation(const TNorm* activation) { - this->_activation = activation; + void Activated::setImplication(const TNorm* implication) { + this->_implication = implication; } - const TNorm* Activated::getActivation() const { - return this->_activation; + const TNorm* Activated::getImplication() const { + return this->_implication; } Activated* Activated::clone() const { 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 index fc0b215..bd63753 100644 --- a/fuzzylite/src/term/Bell.cpp +++ b/fuzzylite/src/term/Bell.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -27,24 +19,26 @@ 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) { - } + : Term(name, height), _center(center), _width(width), _slope(slope) { } - Bell::~Bell() { - } + 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 (fl::Op::isNaN(x)) return fl::nan; - return _height * (1.0 / (1.0 + std::pow(std::abs((x - _center) / _width), 2 * _slope))); + 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(_height, 1.0) ? " " + Op::str(_height) : ""); + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); } void Bell::configure(const std::string& parameters) { @@ -55,7 +49,7 @@ namespace fl { std::ostringstream ex; ex << "[configuration error] term <" << className() << ">" << " requires <" << required << "> parameters"; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } setCenter(Op::toScalar(values.at(0))); setWidth(Op::toScalar(values.at(1))); 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 index 37679ae..c585edc 100644 --- a/fuzzylite/src/term/Concave.cpp +++ b/fuzzylite/src/term/Concave.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -27,35 +19,47 @@ namespace fl { Concave::Concave(const std::string& name, scalar inflection, scalar end, scalar height) - : Term(name, height), _inflection(inflection), _end(end) { - - } + : Term(name, height), _inflection(inflection), _end(end) { } - Concave::~Concave() { - - } + 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 (fl::Op::isNaN(x)) return fl::nan; - if (fl::Op::isLE(_inflection, _end)) { //Concave increasing - if (fl::Op::isLt(x, _end)) { - return _height * (_end - _inflection) / (2 * _end - _inflection - x); + 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 (fl::Op::isGt(x, _end)) { - return _height * (_inflection - _end) / (_inflection - 2 * _end + x); + if (Op::isGt(x, _end)) { + return Term::_height * (_inflection - _end) / (_inflection - 2.0 * _end + x); } } - return _height * 1.0; + 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(_height, 1.0) ? " " + Op::str(_height) : ""); + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); } @@ -67,7 +71,7 @@ namespace fl { std::ostringstream ex; ex << "[configuration error] term <" << className() << ">" << " requires <" << required << "> parameters"; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } setInflection(Op::toScalar(values.at(0))); setEnd(Op::toScalar(values.at(1))); @@ -100,8 +104,4 @@ namespace fl { return new Concave; } - - - - } diff --git a/fuzzylite/src/term/Constant.cpp b/fuzzylite/src/term/Constant.cpp index d52f8ec..7c0422e 100644 --- a/fuzzylite/src/term/Constant.cpp +++ b/fuzzylite/src/term/Constant.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -27,18 +19,20 @@ namespace fl { Constant::Constant(const std::string& name, scalar value) - : Term(name), _value(value) { - } + : Term(name), _value(value) { } - Constant::~Constant() { - } + Constant::~Constant() { } std::string Constant::className() const { return "Constant"; } + Complexity Constant::complexity() const { + return Complexity(); + } + scalar Constant::membership(scalar x) const { - (void) x; + FL_IUNUSED(x); return this->_value; } diff --git a/fuzzylite/src/term/Cosine.cpp b/fuzzylite/src/term/Cosine.cpp index a1402ad..78162ea 100644 --- a/fuzzylite/src/term/Cosine.cpp +++ b/fuzzylite/src/term/Cosine.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -27,13 +19,9 @@ namespace fl { Cosine::Cosine(const std::string& name, scalar center, scalar width, scalar height) - : Term(name, height), _center(center), _width(width) { + : Term(name, height), _center(center), _width(width) { } - } - - Cosine::~Cosine() { - - } + Cosine::~Cosine() { } std::string Cosine::className() const { return "Cosine"; @@ -41,7 +29,7 @@ namespace fl { std::string Cosine::parameters() const { return Op::join(2, " ", _center, _width) + - (not Op::isEq(_height, 1.0) ? " " + Op::str(_height) : ""); + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); } void Cosine::configure(const std::string& parameters) { @@ -52,7 +40,7 @@ namespace fl { std::ostringstream ex; ex << "[configuration error] term <" << className() << ">" << " requires <" << required << "> parameters"; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } setCenter(Op::toScalar(values.at(0))); setWidth(Op::toScalar(values.at(1))); @@ -61,13 +49,17 @@ namespace fl { } + Complexity Cosine::complexity() const { + return Complexity().comparison(3).arithmetic(4 + 1 + 7).function(2); + } + scalar Cosine::membership(scalar x) const { - if (fl::Op::isNaN(x)) return fl::nan; - if (fl::Op::isLt(x, _center - _width / 2.0) - or fl::Op::isGt(x, _center + _width / 2.0)) - return _height * 0.0; + 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 _height * (0.5 * (1.0 + std::cos(2.0 / _width * pi * (x - _center)))); + return Term::_height * (0.5 * (1.0 + std::cos(2.0 / _width * pi * (x - _center)))); } void Cosine::setCenter(scalar center) { @@ -93,4 +85,5 @@ namespace fl { Term* Cosine::constructor() { return new Cosine; } + } diff --git a/fuzzylite/src/term/Discrete.cpp b/fuzzylite/src/term/Discrete.cpp index 212ada2..9951867 100644 --- a/fuzzylite/src/term/Discrete.cpp +++ b/fuzzylite/src/term/Discrete.cpp @@ -1,88 +1,89 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" -#include <cstdarg> - namespace fl { Discrete::Discrete(const std::string& name, const std::vector<Pair>& xy, scalar height) - : Term(name, height), _xy(xy) { - } + : Term(name, height), _xy(xy) { } - Discrete::~Discrete() { - } + Discrete::~Discrete() { } std::string Discrete::className() const { return "Discrete"; } - scalar Discrete::membership(scalar _x_) const { - if (fl::Op::isNaN(_x_)) return fl::nan; + 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 fl::Exception("[discrete error] term is empty", FL_AT); + throw Exception("[discrete error] term is empty", FL_AT); /* ______________________ - * / \ - * / \ - * ____________/ \____________ - * x[0] x[n-1] + / \ + / \ + ____________/ \____________ + 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; - if (fl::Op::isLE(_x_, _xy.front().first)) return _height * _xy.front().second; - if (fl::Op::isGE(_x_, _xy.back().first)) return _height * _xy.back().second; - - int lower = -1, upper = -1; + 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)); - for (std::size_t i = 0; i < _xy.size(); ++i) { - if (Op::isEq(_xy.at(i).first, _x_)) return _height * _xy.at(i).second; - //approximate on the left - if (Op::isLt(_xy.at(i).first, _x_)) { - lower = i; - } - //get the immediate next one on the right - if (Op::isGt(_xy.at(i).first, _x_)) { - upper = i; - break; - } + //if the lower bound is equal to x + if (Op::isEq(x, lowerBound->first)) { + return Term::_height * lowerBound->second; } - if (upper < 0) upper = _xy.size() - 1; - if (lower < 0) lower = 0; - - return _height * Op::scale(_x_, _xy.at(lower).first, _xy.at(upper).first, - _xy.at(lower).second, _xy.at(upper).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 << fl::Op::str(_xy.at(i).first) << " " << fl::Op::str(_xy.at(i).second); + ss << Op::str(_xy.at(i).first) << " " << Op::str(_xy.at(i).second); if (i + 1 < _xy.size()) ss << " "; } - if (not Op::isEq(_height, 1.0)) ss << " " << Op::str(_height); + if (not Op::isEq(getHeight(), 1.0)) ss << " " << Op::str(getHeight()); return ss.str(); } @@ -102,36 +103,8 @@ namespace fl { this->_xy = toPairs(values); } - template <typename T> - Discrete* Discrete::create(const std::string& name, int argc, - T x1, T y1, ...) { // throw (fl::Exception) { - 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(); - } - - template FL_API Discrete* Discrete::create(const std::string& name, int argc, - double x1, double y1, ...); // throw (fl::Exception); - //double, not scalar because variadic promotes floats to double - template FL_API Discrete* Discrete::create(const std::string& name, int argc, - int x1, int y1, ...); // throw (fl::Exception); - - void Discrete::setXY(const std::vector<Pair>& pairs) { - this->_xy = pairs; + void Discrete::setXY(const std::vector<Pair>& xy) { + this->_xy = xy; } const std::vector<Discrete::Pair>& Discrete::xy() const { @@ -142,19 +115,51 @@ namespace fl { return this->_xy; } - const Discrete::Pair& Discrete::xy(int index) const { + const Discrete::Pair& Discrete::xy(std::size_t index) const { return this->_xy.at(index); } - Discrete::Pair& Discrete::xy(int 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 fl::Exception(os.str(), FL_AT); + throw Exception(os.str(), FL_AT); } std::vector<Pair> result((xy.size() + 1) / 2); @@ -188,16 +193,32 @@ namespace fl { return result; } - std::string Discrete::formatXY(const std::vector<Pair>& xy, const std::string& prefix, const std::string& innerSeparator, const std::string& postfix, const std::string& outerSeparator) { + 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 << fl::Op::str(xy.at(i).first) << innerSeparator - << fl::Op::str(xy.at(i).second) << postfix; + 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); } @@ -206,5 +227,4 @@ namespace fl { return new Discrete; } - } diff --git a/fuzzylite/src/term/Function.cpp b/fuzzylite/src/term/Function.cpp index c0f54b8..42f4aaa 100644 --- a/fuzzylite/src/term/Function.cpp +++ b/fuzzylite/src/term/Function.cpp @@ -1,43 +1,30 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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/factory/FunctionFactory.h" #include "fl/rule/Rule.h" #include "fl/variable/InputVariable.h" #include "fl/variable/OutputVariable.h" -#include <cctype> -#include <functional> #include <queue> -#include <signal.h> #include <stack> - namespace fl { /** @@ -47,32 +34,26 @@ namespace fl { 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) { - - } + 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) { - } + 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) { - } + precedence(precedence), associativity(associativity) { } - Function::Element::~Element() { - - } + Function::Element::~Element() { } bool Function::Element::isOperator() const { - return type == OPERATOR; + return type == Operator; } bool Function::Element::isFunction() const { - return type == FUNCTION; + return type == Function; } Function::Element* Function::Element::clone() const { @@ -82,7 +63,7 @@ namespace fl { std::string Function::Element::toString() const { std::ostringstream ss; - if (type == OPERATOR) { + if (type == Operator) { ss << "Operator (name=" << name << ", " << "description=" << description << ", " << "precedence=" << precedence << ", " @@ -92,7 +73,7 @@ namespace fl { else if (arity == 2) ss << "pointer=" << binary; else ss << "pointer=error"; ss << ")"; - } else if (type == FUNCTION) { + } else if (type == Function) { ss << "Function (name=" << name << ", " << "description=" << description << ", " << "arity=" << arity << ", " @@ -110,16 +91,13 @@ namespace fl { ******************************/ Function::Node::Node(Element* element, Node* left, Node* right) - : element(element), left(left), right(right), variable(""), value(fl::nan) { - } + : 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) { - } + : 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) { - } + : 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) { @@ -145,8 +123,7 @@ namespace fl { value = other.value; } - Function::Node::~Node() { - } + Function::Node::~Node() { } scalar Function::Node::evaluate(const std::map<std::string, scalar>* variables) const { scalar result = fl::nan; @@ -160,17 +137,17 @@ namespace fl { ex << "[function error] arity <" << element->arity << "> of " << (element->isOperator() ? "operator" : "function") << " <" << element->name << "> is fl::null"; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } } else if (not variable.empty()) { if (not variables) { - throw fl::Exception("[function error] " + 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 fl::Exception("[function error] " + else throw Exception("[function error] " "unknown variable <" + variable + ">", FL_AT); } else { result = value; @@ -178,6 +155,36 @@ namespace fl { 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); } @@ -186,14 +193,14 @@ namespace fl { std::ostringstream ss; if (element.get()) ss << element->name; else if (not variable.empty()) ss << variable; - else ss << fl::Op::str(value); + else ss << Op::str(value); return ss.str(); } std::string Function::Node::toPrefix(const Node* node) const { if (not node) node = this; - if (not fl::Op::isNaN(node->value)) { //is terminal - return fl::Op::str(node->value); + if (not Op::isNaN(node->value)) { //is terminal + return Op::str(node->value); } if (not node->variable.empty()) { return node->variable; @@ -210,8 +217,8 @@ namespace fl { std::string Function::Node::toInfix(const Node* node) const { if (not node) node = this; - if (not fl::Op::isNaN(node->value)) { //is proposition - return fl::Op::str(node->value); + if (not Op::isNaN(node->value)) { //is proposition + return Op::str(node->value); } if (not node->variable.empty()) { return node->variable; @@ -228,8 +235,8 @@ namespace fl { std::string Function::Node::toPostfix(const Node* node) const { if (not node) node = this; - if (not fl::Op::isNaN(node->value)) { //is proposition - return fl::Op::str(node->value); + if (not Op::isNaN(node->value)) { //is proposition + return Op::str(node->value); } if (not node->variable.empty()) { return node->variable; @@ -249,8 +256,7 @@ namespace fl { **********************************/ Function::Function(const std::string& name, const std::string& formula, const Engine* engine) - : Term(name), _root(fl::null), _formula(formula), _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) { @@ -271,25 +277,41 @@ namespace fl { return *this; } - Function::~Function() { - } + 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 this->_root.get()) { - throw fl::Exception("[function error] function <" + _formula + "> not loaded.", FL_AT); + if (not _root.get()) { + throw Exception("[function error] function <" + _formula + "> not loaded.", FL_AT); } - if (this->_engine) { - for (int i = 0; i < this->_engine->numberOfInputVariables(); ++i) { - InputVariable* input = this->_engine->getInputVariable(i); - this->variables[input->getName()] = input->getInputValue(); + if (_engine) { + for (std::size_t i = 0; i < _engine->numberOfInputVariables(); ++i) { + InputVariable* input = _engine->getInputVariable(i); + this->variables[input->getName()] = input->getValue(); } - for (int i = 0; i < this->_engine->numberOfOutputVariables(); ++i) { - OutputVariable* output = this->_engine->getOutputVariable(i); - this->variables[output->getName()] = output->getOutputValue(); + 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; @@ -297,15 +319,15 @@ namespace fl { } scalar Function::evaluate(const std::map<std::string, scalar>* localVariables) const { - if (not this->_root.get()) - throw fl::Exception("[function error] evaluation failed because the function is not loaded", FL_AT); + if (not _root.get()) + throw Exception("[function error] evaluation failed because the function is not loaded", FL_AT); if (localVariables) - return this->_root->evaluate(localVariables); - return this->_root->evaluate(&this->variables); + return _root->evaluate(localVariables); + return _root->evaluate(&this->variables); } std::string Function::parameters() const { - return _formula; + return getFormula(); } void Function::configure(const std::string& parameters) { @@ -329,18 +351,17 @@ namespace fl { } void Function::load() { - load(this->_formula); + load(getFormula()); } void Function::load(const std::string& formula) { - load(formula, this->_engine); + load(formula, getEngine()); } void Function::load(const std::string& formula, const Engine* engine) { - unload(); - this->_formula = formula; - this->_engine = engine; + setFormula(formula); + setEngine(engine); this->_root.reset(parse(formula)); membership(0.0); //make sure function evaluates without throwing exception. } @@ -373,23 +394,32 @@ namespace fl { 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 = fl::FactoryManager::instance()->function()->availableOperators(); + std::vector<std::string> operators = FactoryManager::instance()->function()->availableOperators(); for (std::size_t i = 0; i < operators.size(); ++i) { - if (not (operators.at(i) == fl::Rule::andKeyword() or - operators.at(i) == fl::Rule::orKeyword())) { + 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 = fl::Op::findReplace(result, chars.at(i), " " + chars.at(i) + " "); + result = Op::findReplace(result, chars.at(i), " " + chars.at(i) + " "); } return result; } @@ -397,7 +427,7 @@ namespace fl { /**************************************** * The Glorious Parser * Shunting-yard algorithm - * TODO: Maybe change it for http://en.wikipedia.org/wiki/Operator-precedence_parser + * @todo: maybe change it for http://en.wikipedia.org/wiki/Operator-precedence_parser ***************************************/ std::string Function::toPostfix(const std::string& formula) const { @@ -408,7 +438,7 @@ namespace fl { std::stringstream tokenizer(spacedFormula); std::string token; - FunctionFactory* factory = fl::FactoryManager::instance()->function(); + FunctionFactory* factory = FactoryManager::instance()->function(); while (tokenizer >> token) { Element* element = factory->getObject(token); bool isOperand = not element and token != "(" and token != ")" and token != ","; @@ -427,7 +457,7 @@ namespace fl { if (stack.empty() or stack.top() != "(") { std::ostringstream ex; ex << "[parsing error] mismatching parentheses in: " << formula; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } } else if (element and element->isOperator()) { @@ -457,7 +487,7 @@ namespace fl { if (stack.empty() or stack.top() != "(") { std::ostringstream ex; ex << "[parsing error] mismatching parentheses in: " << formula; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } stack.pop(); //get rid of "(" @@ -470,7 +500,7 @@ namespace fl { } else { std::ostringstream ex; ex << "[parsing error] unexpected error with token <" << token << ">"; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } } @@ -478,7 +508,7 @@ namespace fl { if (stack.top() == "(" or stack.top() == ")") { std::ostringstream ex; ex << "[parsing error] mismatching parentheses in: " << formula; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } queue.push(stack.top()); stack.pop(); @@ -490,43 +520,31 @@ namespace fl { queue.pop(); if (not queue.empty()) ssPostfix << " "; } - // FL_DBG("postfix=" << ssPostfix.str()); return ssPostfix.str(); } - // bool FunctionFactory::isOperand(const std::string& name) const { - // //An operand is not a parenthesis... - // if (name == "(" or name == ")" or name == ",") return false; - // //nor an operator... - // if (isOperator(name)) return false; - // //nor a function... - // if (isFunction(name)) return false; - // //...it is everything else :) - // return true; - // } - Function::Node* Function::parse(const std::string& formula) { if (formula.empty()) - throw fl::Exception("[function error] formula is empty", FL_AT); + 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 = fl::FactoryManager::instance()->function(); + 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 > (int) stack.size()) { + 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 fl::Exception(ss.str(), FL_AT); + throw Exception(ss.str(), FL_AT); } Node* node = new Node(element->clone()); @@ -541,10 +559,10 @@ namespace fl { } else if (isOperand) { Node* node; try { - scalar value = fl::Op::toScalar(token); + scalar value = Op::toScalar(token); node = new Node(value); } catch (std::exception& ex) { - (void) ex; + FL_IUNUSED(ex); node = new Node(token); } stack.push(node); @@ -552,49 +570,9 @@ namespace fl { } if (stack.size() != 1) - throw fl::Exception("[function error] ill-formed formula <" + formula + ">", FL_AT); + throw Exception("[function error] ill-formed formula <" + formula + ">", FL_AT); return stack.top(); } - void Function::main() { - Function f; - std::string text = "3+4*2/(1-5)^2^3"; - FL_LOG(f.toPostfix(text)); - FL_LOG("P: " << f.parse(text)->toInfix()); - FL_LOG(">" << f.parse(text)->evaluate()); - //3 4 2 * 1 5 - 2 3 ^ ^ / + - - f.variables["y"] = 1.0; - text = "sin(y*x)^2/x"; - FL_LOG("pre: " << f.parse(text)->toPrefix()); - FL_LOG("in: " << f.parse(text)->toInfix()); - FL_LOG("pos: " << f.parse(text)->toPostfix()); - f.load(text); - FL_LOG("Result: " << f.membership(1)); - //y x * sin 2 ^ x / - - - text = "(Temperature is High and Oxygen is Low) or " - "(Temperature is Low and (Oxygen is Low or Oxygen is High))"; - FL_LOG(f.toPostfix(text)); - - f.variables["pi"] = 3.14; - text = "-5 *4/sin(-pi/2)"; - FL_LOG(f.toPostfix(text)); - try { - FL_LOG(f.parse(text)->evaluate()); - } catch (std::exception& e) { - FL_LOG(e.what()); - } - f.variables["pi"] = 3.14; - text = "~5 *4/sin(~pi/2)"; - FL_LOG(f.toPostfix(text)); - try { - FL_LOG(f.parse(text)->evaluate(&f.variables)); - } catch (std::exception& e) { - FL_LOG(e.what()); - } - } - } diff --git a/fuzzylite/src/term/Gaussian.cpp b/fuzzylite/src/term/Gaussian.cpp index 5b709f9..8413b59 100644 --- a/fuzzylite/src/term/Gaussian.cpp +++ b/fuzzylite/src/term/Gaussian.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -28,24 +20,26 @@ namespace fl { Gaussian::Gaussian(const std::string& name, scalar mean, scalar standardDeviation, scalar height) - : Term(name, height), _mean(mean), _standardDeviation(standardDeviation) { - } + : Term(name, height), _mean(mean), _standardDeviation(standardDeviation) { } - Gaussian::~Gaussian() { - } + 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 (fl::Op::isNaN(x)) return fl::nan; - return _height * std::exp((-(x - _mean) * (x - _mean)) / (2 * _standardDeviation * _standardDeviation)); + 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(_height, 1.0) ? " " + Op::str(_height) : ""); + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); } void Gaussian::configure(const std::string& parameters) { @@ -56,7 +50,7 @@ namespace fl { std::ostringstream ex; ex << "[configuration error] term <" << className() << ">" << " requires <" << required << "> parameters"; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } setMean(Op::toScalar(values.at(0))); setStandardDeviation(Op::toScalar(values.at(1))); @@ -64,16 +58,16 @@ namespace fl { setHeight(Op::toScalar(values.at(required))); } - void Gaussian::setMean(scalar c) { - this->_mean = c; + void Gaussian::setMean(scalar mean) { + this->_mean = mean; } scalar Gaussian::getMean() const { return this->_mean; } - void Gaussian::setStandardDeviation(scalar sigma) { - this->_standardDeviation = sigma; + void Gaussian::setStandardDeviation(scalar standardDeviation) { + this->_standardDeviation = standardDeviation; } scalar Gaussian::getStandardDeviation() const { diff --git a/fuzzylite/src/term/GaussianProduct.cpp b/fuzzylite/src/term/GaussianProduct.cpp index b9652e1..791790c 100644 --- a/fuzzylite/src/term/GaussianProduct.cpp +++ b/fuzzylite/src/term/GaussianProduct.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -30,32 +22,37 @@ namespace fl { scalar meanA, scalar standardDeviationA, scalar meanB, scalar standardDeviationB, scalar height) : Term(name, height), _meanA(meanA), _standardDeviationA(standardDeviationA), - _meanB(meanB), _standardDeviationB(standardDeviationB) { - } + _meanB(meanB), _standardDeviationB(standardDeviationB) { } - GaussianProduct::~GaussianProduct() { - } + 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 (fl::Op::isNaN(x)) return fl::nan; - bool xLEa = fl::Op::isLE(x, _meanA); - scalar a = (1 - xLEa) + xLEa * std::exp( - (-(x - _meanA) * (x - _meanA)) / (2 * _standardDeviationA * _standardDeviationA) - ); - bool xGEb = fl::Op::isGE(x, _meanB); - scalar b = (1 - xGEb) + xGEb * std::exp( - (-(x - _meanB) * (x - _meanB)) / (2 * _standardDeviationB * _standardDeviationB) - ); - return _height * a * b; + 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(_height, 1.0) ? " " + Op::str(_height) : ""); + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); } void GaussianProduct::configure(const std::string& parameters) { @@ -66,7 +63,7 @@ namespace fl { std::ostringstream ex; ex << "[configuration error] term <" << className() << ">" << " requires <" << required << "> parameters"; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } setMeanA(Op::toScalar(values.at(0))); setStandardDeviationA(Op::toScalar(values.at(1))); diff --git a/fuzzylite/src/term/Linear.cpp b/fuzzylite/src/term/Linear.cpp index 4111a00..e6efb43 100644 --- a/fuzzylite/src/term/Linear.cpp +++ b/fuzzylite/src/term/Linear.cpp @@ -1,72 +1,72 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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/Engine.h" #include "fl/variable/InputVariable.h" -#include <cstdarg> - namespace fl { Linear::Linear(const std::string& name, const std::vector<scalar>& coefficients, const Engine* engine) - : Term(name), _coefficients(coefficients), _engine(engine) { - } + : Term(name), _coefficients(coefficients), _engine(engine) { } - Linear::~Linear() { - } + 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 { - (void) x; - if (not _engine) throw fl::Exception("[linear error] term <" + getName() + "> " + 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; - for (std::size_t i = 0; i < _engine->inputVariables().size(); ++i) { - if (i < _coefficients.size()) - result += _coefficients.at(i) * _engine->inputVariables().at(i)->getInputValue(); + 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 (_coefficients.size() > _engine->inputVariables().size()) { + if (numberOfCoefficients > numberOfInputVariables) { result += _coefficients.back(); } return result; } - void Linear::set(const std::vector<scalar>& coeffs, const Engine* engine) { - setCoefficients(coeffs); + void Linear::set(const std::vector<scalar>& coefficients, const Engine* engine) { + setCoefficients(coefficients); setEngine(engine); } - void Linear::setCoefficients(const std::vector<scalar>& coeffs) { - this->_coefficients = coeffs; + void Linear::setCoefficients(const std::vector<scalar>& coefficients) { + this->_coefficients = coefficients; } const std::vector<scalar>& Linear::coefficients() const { @@ -90,6 +90,7 @@ namespace fl { } 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; @@ -103,33 +104,12 @@ namespace fl { return new Linear(*this); } - Term* Linear::constructor() { - return new Linear; + void Linear::updateReference(const Engine* engine) { + setEngine(engine); } - template <typename T> - Linear* Linear::create(const std::string& name, - const Engine* engine, T firstCoefficient, ...) {// throw (fl::Exception) { - if (not engine) throw fl::Exception("[linear error] cannot create term <" + name + "> " - "without a reference to the engine", FL_AT); - std::vector<scalar> coefficients; - coefficients.push_back(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); + Term* Linear::constructor() { + return new Linear; } - template FL_API Linear* Linear::create(const std::string& name, - const Engine* engine, - double firstCoefficient, ...); - - template FL_API Linear* Linear::create(const std::string& name, - const Engine* engine, - int firstCoefficient, ...); } diff --git a/fuzzylite/src/term/PiShape.cpp b/fuzzylite/src/term/PiShape.cpp index ba3e99f..6c91f54 100644 --- a/fuzzylite/src/term/PiShape.cpp +++ b/fuzzylite/src/term/PiShape.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -29,47 +21,45 @@ 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) { - } + _topRight(topRight), _bottomRight(bottomRight) { } - PiShape::~PiShape() { - } + PiShape::~PiShape() { } std::string PiShape::className() const { return "PiShape"; } - scalar PiShape::membership(scalar x) const { - if (fl::Op::isNaN(x)) return fl::nan; - //from Octave smf.m - scalar a_b_ave = (_bottomLeft + _topLeft) / 2.0; - scalar b_minus_a = _topLeft - _bottomLeft; - scalar c_d_ave = (_topRight + _bottomRight) / 2.0; - scalar d_minus_c = _bottomRight - _topRight; - - if (Op::isLE(x, _bottomLeft)) return _height * 0.0; - - if (Op::isLE(x, a_b_ave)) - return _height * (2.0 * std::pow((x - _bottomLeft) / b_minus_a, 2)); - - if (Op::isLt(x, _topLeft)) - return _height * (1.0 - 2.0 * std::pow((x - _topLeft) / b_minus_a, 2)); + 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)) - return _height * 1.0; - - if (Op::isLE(x, c_d_ave)) - return _height * (1.0 - 2.0 * std::pow((x - _topRight) / d_minus_c, 2)); - - if (Op::isLt(x, _bottomRight)) - return _height * (2.0 * std::pow((x - _bottomRight) / d_minus_c, 2)); + 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 _height * 0.0; + return Term::_height * sshape * zshape; } std::string PiShape::parameters() const { return Op::join(4, " ", _bottomLeft, _topLeft, _topRight, _bottomRight) + - (not Op::isEq(_height, 1.0) ? " " + Op::str(_height) : ""); + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); } void PiShape::configure(const std::string& parameters) { @@ -80,7 +70,7 @@ namespace fl { std::ostringstream ex; ex << "[configuration error] term <" << className() << ">" << " requires <" << required << "> parameters"; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } setBottomLeft(Op::toScalar(values.at(0))); setTopLeft(Op::toScalar(values.at(1))); diff --git a/fuzzylite/src/term/Ramp.cpp b/fuzzylite/src/term/Ramp.cpp index f224045..d29233d 100644 --- a/fuzzylite/src/term/Ramp.cpp +++ b/fuzzylite/src/term/Ramp.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -27,35 +19,52 @@ namespace fl { Ramp::Ramp(const std::string& name, scalar start, scalar end, scalar height) - : Term(name, height), _start(start), _end(end) { - } + : Term(name, height), _start(start), _end(end) { } - Ramp::~Ramp() { - } + 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 (fl::Op::isNaN(x)) return fl::nan; + if (Op::isNaN(x)) return fl::nan; - if (Op::isEq(_start, _end)) return _height * 0.0; + if (Op::isEq(_start, _end)) + return Term::_height * 0.0; if (Op::isLt(_start, _end)) { - if (Op::isLE(x, _start)) return _height * 0.0; - if (Op::isGE(x, _end)) return _height * 1.0; - return _height * (x - _start) / (_end - _start); + 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 _height * 0.0; - if (Op::isLE(x, _end)) return _height * 1.0; - return _height * (_start - x) / (_start - _end); + 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(_height, 1.0) ? " " + Op::str(_height) : ""); + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); } void Ramp::configure(const std::string& parameters) { @@ -66,7 +75,7 @@ namespace fl { std::ostringstream ex; ex << "[configuration error] term <" << className() << ">" << " requires <" << required << "> parameters"; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } setStart(Op::toScalar(values.at(0))); setEnd(Op::toScalar(values.at(1))); @@ -92,11 +101,11 @@ namespace fl { Ramp::Direction Ramp::direction() const { scalar range = this->_end - this->_start; - if (not fl::Op::isFinite(range) or fl::Op::isEq(range, 0.0)) return ZERO; + if (not Op::isFinite(range) or Op::isEq(range, 0.0)) return Zero; - if (fl::Op::isGt(range, 0.0)) return POSITIVE; + if (Op::isGt(range, 0.0)) return Positive; - return NEGATIVE; + return Negative; } Ramp* Ramp::clone() const { diff --git a/fuzzylite/src/term/Rectangle.cpp b/fuzzylite/src/term/Rectangle.cpp index 363b15d..4f975d0 100644 --- a/fuzzylite/src/term/Rectangle.cpp +++ b/fuzzylite/src/term/Rectangle.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -27,26 +19,28 @@ namespace fl { Rectangle::Rectangle(const std::string& name, scalar start, scalar end, scalar height) - : Term(name, height), _start(start), _end(end) { - } + : Term(name, height), _start(start), _end(end) { } - Rectangle::~Rectangle() { - } + 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 (fl::Op::isNaN(x)) return fl::nan; - if (fl::Op::isLt(x, _start) or fl::Op::isGt(x, _end)) - return _height * 0.0; - return _height * 1.0; + 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(_height, 1.0) ? " " + Op::str(_height) : ""); + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); } void Rectangle::configure(const std::string& parameters) { @@ -57,7 +51,7 @@ namespace fl { std::ostringstream ex; ex << "[configuration error] term <" << className() << ">" << " requires <" << required << "> parameters"; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } setStart(Op::toScalar(values.at(0))); setEnd(Op::toScalar(values.at(1))); diff --git a/fuzzylite/src/term/SShape.cpp b/fuzzylite/src/term/SShape.cpp index cd02be1..65dfd2b 100644 --- a/fuzzylite/src/term/SShape.cpp +++ b/fuzzylite/src/term/SShape.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -27,36 +19,58 @@ namespace fl { SShape::SShape(const std::string& name, scalar start, scalar end, scalar height) - : Term(name, height), _start(start), _end(end) { - } + : Term(name, height), _start(start), _end(end) { } - SShape::~SShape() { - } + 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 (fl::Op::isNaN(x)) return fl::nan; - //from Octave smf.m - scalar average = (_start + _end) / 2.0; - scalar difference = _end - _start; + if (Op::isNaN(x)) return fl::nan; - if (Op::isLE(x, _start)) return _height * 0.0; + if (Op::isLE(x, _start)) + return Term::_height * 0.0; - if (Op::isLE(x, average)) - return _height * (2.0 * std::pow((x - _start) / difference, 2)); + 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 _height * (1.0 - 2.0 * std::pow((x - _end) / difference, 2)); + 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; + } - return _height * 1.0; + bool SShape::isMonotonic() const { + return true; } std::string SShape::parameters() const { return Op::join(2, " ", _start, _end) + - (not Op::isEq(_height, 1.0) ? " " + Op::str(_height) : ""); + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); } void SShape::configure(const std::string& parameters) { @@ -67,7 +81,7 @@ namespace fl { std::ostringstream ex; ex << "[configuration error] term <" << className() << ">" << " requires <" << required << "> parameters"; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } setStart(Op::toScalar(values.at(0))); setEnd(Op::toScalar(values.at(1))); diff --git a/fuzzylite/src/term/Sigmoid.cpp b/fuzzylite/src/term/Sigmoid.cpp index 77d7f82..fcf165e 100644 --- a/fuzzylite/src/term/Sigmoid.cpp +++ b/fuzzylite/src/term/Sigmoid.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -27,24 +19,58 @@ namespace fl { Sigmoid::Sigmoid(const std::string& name, scalar inflection, scalar slope, scalar height) - : Term(name, height), _inflection(inflection), _slope(slope) { - } + : Term(name, height), _inflection(inflection), _slope(slope) { } - Sigmoid::~Sigmoid() { - } + 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 (fl::Op::isNaN(x)) return fl::nan; - return _height * 1.0 / (1.0 + std::exp(-_slope * (x - _inflection))); + 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(_height, 1.0) ? " " + Op::str(_height) : ""); + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); } void Sigmoid::configure(const std::string& parameters) { @@ -55,7 +81,7 @@ namespace fl { std::ostringstream ex; ex << "[configuration error] term <" << className() << ">" << " requires <" << required << "> parameters"; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } setInflection(Op::toScalar(values.at(0))); setSlope(Op::toScalar(values.at(1))); @@ -80,11 +106,11 @@ namespace fl { } Sigmoid::Direction Sigmoid::direction() const { - if (not fl::Op::isFinite(_slope) or fl::Op::isEq(_slope, 0.0)) return ZERO; + if (not Op::isFinite(_slope) or Op::isEq(_slope, 0.0)) return Zero; - if (fl::Op::isGt(_slope, 0.0)) return POSITIVE; + if (Op::isGt(_slope, 0.0)) return Positive; - return NEGATIVE; + return Negative; } Sigmoid* Sigmoid::clone() const { diff --git a/fuzzylite/src/term/SigmoidDifference.cpp b/fuzzylite/src/term/SigmoidDifference.cpp index 55a586c..8f7ee80 100644 --- a/fuzzylite/src/term/SigmoidDifference.cpp +++ b/fuzzylite/src/term/SigmoidDifference.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -29,27 +21,29 @@ 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) { - } + : Term(name, height), _left(left), _rising(rising), _falling(falling), _right(right) { } - SigmoidDifference::~SigmoidDifference() { - } + 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 (fl::Op::isNaN(x)) return fl::nan; + if (Op::isNaN(x)) return fl::nan; - scalar a = 1.0 / (1 + std::exp(-_rising * (x - _left))); - scalar b = 1.0 / (1 + std::exp(-_falling * (x - _right))); - return _height * std::abs(a - b); + 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(_height, 1.0) ? " " + Op::str(_height) : ""); + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); } void SigmoidDifference::configure(const std::string& parameters) { @@ -60,7 +54,7 @@ namespace fl { std::ostringstream ex; ex << "[configuration error] term <" << className() << ">" << " requires <" << required << "> parameters"; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } setLeft(Op::toScalar(values.at(0))); setRising(Op::toScalar(values.at(1))); diff --git a/fuzzylite/src/term/SigmoidProduct.cpp b/fuzzylite/src/term/SigmoidProduct.cpp index 9e43ac9..5f35750 100644 --- a/fuzzylite/src/term/SigmoidProduct.cpp +++ b/fuzzylite/src/term/SigmoidProduct.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -29,25 +21,28 @@ 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) { - } + : Term(name, height), _left(left), _rising(rising), _falling(falling), _right(right) { } - SigmoidProduct::~SigmoidProduct() { - } + 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 { - scalar a = 1.0 / (1 + std::exp(-_rising * (x - _left))); - scalar b = 1.0 / (1 + std::exp(-_falling * (x - _right))); - return _height * a * b; + 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(_height, 1.0) ? " " + Op::str(_height) : ""); + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); } void SigmoidProduct::configure(const std::string& parameters) { @@ -58,7 +53,7 @@ namespace fl { std::ostringstream ex; ex << "[configuration error] term <" << className() << ">" << " requires <" << required << "> parameters"; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } setLeft(Op::toScalar(values.at(0))); setRising(Op::toScalar(values.at(1))); diff --git a/fuzzylite/src/term/Spike.cpp b/fuzzylite/src/term/Spike.cpp index f4b73bf..a19810d 100644 --- a/fuzzylite/src/term/Spike.cpp +++ b/fuzzylite/src/term/Spike.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -27,25 +19,26 @@ namespace fl { Spike::Spike(const std::string& name, scalar center, scalar width, scalar height) - : Term(name, height), _center(center), _width(width) { - } - - Spike::~Spike() { + : 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 (fl::Op::isNaN(x)) return fl::nan; - return _height * std::exp(-std::fabs(10.0 / _width * (x - _center))); + 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(_height, 1.0) ? " " + Op::str(_height) : ""); + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); } void Spike::configure(const std::string& parameters) { @@ -56,7 +49,7 @@ namespace fl { std::ostringstream ex; ex << "[configuration error] term <" << className() << ">" << " requires <" << required << "> parameters"; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } setCenter(Op::toScalar(values.at(0))); setWidth(Op::toScalar(values.at(1))); @@ -87,4 +80,5 @@ namespace fl { Term* Spike::constructor() { return new Spike; } + } diff --git a/fuzzylite/src/term/Term.cpp b/fuzzylite/src/term/Term.cpp index 143a98f..bf7520d 100644 --- a/fuzzylite/src/term/Term.cpp +++ b/fuzzylite/src/term/Term.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -30,13 +22,9 @@ namespace fl { - Term::Term(const std::string& name, scalar height) : _name(name), _height(height) { - - } - - Term::~Term() { + Term::Term(const std::string& name, scalar height) : _name(name), _height(height) { } - } + Term::~Term() { } void Term::setName(const std::string& name) { this->_name = name; @@ -58,17 +46,20 @@ namespace fl { return FllExporter().toString(this); } - void Term::updateReference(Term* term, const Engine* engine) { - if (Linear * linear = dynamic_cast<Linear*> (term)) { - linear->setEngine(engine); - } else if (Function * function = dynamic_cast<Function*> (term)) { - function->setEngine(engine); - try { - function->load(); - } catch (...) { - //ignore - } - } + 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 index 60abcc4..4773dbb 100644 --- a/fuzzylite/src/term/Trapezoid.cpp +++ b/fuzzylite/src/term/Trapezoid.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -30,7 +22,6 @@ namespace fl { 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)) { - //TODO: Modify FLL to allow passing two parameters only. this->_vertexD = _vertexB; scalar range = _vertexD - _vertexA; this->_vertexB = _vertexA + range * 1.0 / 5.0; @@ -38,34 +29,41 @@ namespace fl { } } - Trapezoid::~Trapezoid() { - } + 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 (fl::Op::isNaN(x)) return fl::nan; + if (Op::isNaN(x)) return fl::nan; if (Op::isLt(x, _vertexA) or Op::isGt(x, _vertexD)) - return _height * 0.0; - - if (Op::isLt(x, _vertexB)) - return _height * Op::min(scalar(1.0), (x - _vertexA) / (_vertexB - _vertexA)); + 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 _height * 1.0; + return Term::_height * 1.0; - if (Op::isLt(x, _vertexD)) - return _height * (_vertexD - x) / (_vertexD - _vertexC); + if (Op::isLt(x, _vertexD)) { + if (_vertexD == fl::inf) return Term::_height * 1.0; + return Term::_height * (_vertexD - x) / (_vertexD - _vertexC); + } - return _height * 0.0; + 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(_height, 1.0) ? " " + Op::str(_height) : ""); + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); } void Trapezoid::configure(const std::string& parameters) { @@ -76,7 +74,7 @@ namespace fl { std::ostringstream ex; ex << "[configuration error] term <" << className() << ">" << " requires <" << required << "> parameters"; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } setVertexA(Op::toScalar(values.at(0))); setVertexB(Op::toScalar(values.at(1))); diff --git a/fuzzylite/src/term/Triangle.cpp b/fuzzylite/src/term/Triangle.cpp index 2fce1a6..9d1835c 100644 --- a/fuzzylite/src/term/Triangle.cpp +++ b/fuzzylite/src/term/Triangle.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -28,38 +20,44 @@ 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 (fl::Op::isNaN(vertexC)) { - //TODO: Modify FLL to allow passing two parameters only. - this->_vertexC = vertexB; - this->_vertexB = (vertexA + vertexB) / 2.0; + if (Op::isNaN(vertexC)) { + this->_vertexC = _vertexB; + this->_vertexB = 0.5 * (_vertexA + _vertexB); } } - Triangle::~Triangle() { - } + 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 (fl::Op::isNaN(x)) return fl::nan; + if (Op::isNaN(x)) return fl::nan; if (Op::isLt(x, _vertexA) or Op::isGt(x, _vertexC)) - return _height * 0.0; + return Term::_height * 0.0; if (Op::isEq(x, _vertexB)) - return _height * 1.0; - - if (Op::isLt(x, _vertexB)) - return _height * (x - _vertexA) / (_vertexB - _vertexA); + return Term::_height * 1.0; - return _height * (_vertexC - x) / (_vertexC - _vertexB); + 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(_height, 1.0) ? " " + Op::str(_height) : ""); + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); } void Triangle::configure(const std::string& parameters) { @@ -70,7 +68,7 @@ namespace fl { std::ostringstream ex; ex << "[configuration error] term <" << className() << ">" << " requires <" << required << "> parameters"; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } setVertexA(Op::toScalar(values.at(0))); setVertexB(Op::toScalar(values.at(1))); diff --git a/fuzzylite/src/term/ZShape.cpp b/fuzzylite/src/term/ZShape.cpp index 86668b6..9054b20 100644 --- a/fuzzylite/src/term/ZShape.cpp +++ b/fuzzylite/src/term/ZShape.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -27,36 +19,58 @@ namespace fl { ZShape::ZShape(const std::string& name, scalar start, scalar end, scalar height) - : Term(name, height), _start(start), _end(end) { - } + : Term(name, height), _start(start), _end(end) { } - ZShape::~ZShape() { - } + 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 (fl::Op::isNaN(x)) return fl::nan; - //from Octave zmf.m - scalar average = (_start + _end) / 2; - scalar difference = _end - _start; + if (Op::isNaN(x)) return fl::nan; - if (Op::isLE(x, _start)) return _height * 1.0; + if (Op::isLE(x, _start)) + return Term::_height * 1.0; - if (Op::isLE(x, average)) - return _height * (1.0 - 2.0 * std::pow((x - _start) / difference, 2)); + 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 _height * (2.0 * std::pow((x - _end) / difference, 2)); + 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; + } - return _height * 0.0; + bool ZShape::isMonotonic() const { + return true; } std::string ZShape::parameters() const { return Op::join(2, " ", _start, _end) + - (not Op::isEq(_height, 1.0) ? " " + Op::str(_height) : ""); + (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : ""); } void ZShape::configure(const std::string& parameters) { @@ -67,7 +81,7 @@ namespace fl { std::ostringstream ex; ex << "[configuration error] term <" << className() << ">" << " requires <" << required << "> parameters"; - throw fl::Exception(ex.str(), FL_AT); + throw Exception(ex.str(), FL_AT); } setStart(Op::toScalar(values.at(0))); setEnd(Op::toScalar(values.at(1))); diff --git a/fuzzylite/src/variable/InputVariable.cpp b/fuzzylite/src/variable/InputVariable.cpp index 9fa2932..1364d8f 100644 --- a/fuzzylite/src/variable/InputVariable.cpp +++ b/fuzzylite/src/variable/InputVariable.cpp @@ -1,25 +1,17 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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" @@ -29,26 +21,24 @@ namespace fl { InputVariable::InputVariable(const std::string& name, scalar minimum, scalar maximum) - : Variable(name, minimum, maximum), _inputValue(fl::nan) { - } - - InputVariable::~InputVariable() { - } + : Variable(name, minimum, maximum) { } - void InputVariable::setInputValue(scalar inputValue) { - this->_inputValue = inputValue; - } + InputVariable::~InputVariable() { } - scalar InputVariable::getInputValue() const { - return this->_inputValue; + std::string InputVariable::fuzzyInputValue() const { + return fuzzify(getValue()); } - std::string InputVariable::fuzzyInputValue() const { - return fuzzify(_inputValue); + 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 index 6e1d906..0f85dc4 100644 --- a/fuzzylite/src/variable/OutputVariable.cpp +++ b/fuzzylite/src/variable/OutputVariable.cpp @@ -1,44 +1,31 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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/defuzzifier/Defuzzifier.h" #include "fl/imex/FllExporter.h" -#include "fl/norm/SNorm.h" -#include "fl/term/Accumulated.h" -#include "fl/term/Activated.h" namespace fl { OutputVariable::OutputVariable(const std::string& name, scalar minimum, scalar maximum) : Variable(name, minimum, maximum), - _fuzzyOutput(new Accumulated(name, minimum, maximum)), _outputValue(fl::nan), - _previousOutputValue(fl::nan), _defaultValue(fl::nan), - _lockOutputValueInRange(false), _lockPreviousOutputValue(false) { - } + _fuzzyOutput(new Aggregated(name, minimum, maximum)), + _previousValue(fl::nan), _defaultValue(fl::nan), + _lockPreviousValue(false) { } OutputVariable::OutputVariable(const OutputVariable& other) : Variable(other) { copyFrom(other); @@ -55,36 +42,33 @@ namespace fl { return *this; } - OutputVariable::~OutputVariable() { - } + OutputVariable::~OutputVariable() { } void OutputVariable::copyFrom(const OutputVariable& other) { _fuzzyOutput.reset(other._fuzzyOutput->clone()); if (other._defuzzifier.get()) _defuzzifier.reset(other._defuzzifier->clone()); - _outputValue = other._outputValue; - _previousOutputValue = other._previousOutputValue; + _previousValue = other._previousValue; _defaultValue = other._defaultValue; - _lockOutputValueInRange = other._lockOutputValueInRange; - _lockPreviousOutputValue = other._lockPreviousOutputValue; + _lockPreviousValue = other._lockPreviousValue; } void OutputVariable::setName(const std::string& name) { Variable::setName(name); - this->_fuzzyOutput->setName(name); + _fuzzyOutput->setName(name); } - Accumulated* OutputVariable::fuzzyOutput() const { + Aggregated* OutputVariable::fuzzyOutput() const { return this->_fuzzyOutput.get(); } void OutputVariable::setMinimum(scalar minimum) { Variable::setMinimum(minimum); - this->_fuzzyOutput->setMinimum(minimum); + _fuzzyOutput->setMinimum(minimum); } void OutputVariable::setMaximum(scalar maximum) { Variable::setMaximum(maximum); - this->_fuzzyOutput->setMaximum(maximum); + _fuzzyOutput->setMaximum(maximum); } void OutputVariable::setDefuzzifier(Defuzzifier* defuzzifier) { @@ -95,20 +79,20 @@ namespace fl { return this->_defuzzifier.get(); } - void OutputVariable::setOutputValue(scalar outputValue) { - this->_outputValue = outputValue; + void OutputVariable::setAggregation(SNorm* aggregation) { + this->_fuzzyOutput->setAggregation(aggregation); } - scalar OutputVariable::getOutputValue() const { - return this->_outputValue; + SNorm* OutputVariable::getAggregation() const { + return this->_fuzzyOutput->getAggregation(); } - void OutputVariable::setPreviousOutputValue(scalar previousOutputValue) { - this->_previousOutputValue = previousOutputValue; + void OutputVariable::setPreviousValue(scalar previousOutputValue) { + this->_previousValue = previousOutputValue; } - scalar OutputVariable::getPreviousOutputValue() const { - return this->_previousOutputValue; + scalar OutputVariable::getPreviousValue() const { + return this->_previousValue; } void OutputVariable::setDefaultValue(scalar defaultValue) { @@ -119,77 +103,124 @@ namespace fl { return this->_defaultValue; } - void OutputVariable::setLockOutputValueInRange(bool lockOutputValueInRange) { - this->_lockOutputValueInRange = lockOutputValueInRange; + void OutputVariable::setLockPreviousValue(bool lockPreviousValue) { + this->_lockPreviousValue = lockPreviousValue; } - bool OutputVariable::isLockedOutputValueInRange() const { - return this->_lockOutputValueInRange; + bool OutputVariable::isLockPreviousValue() const { + return this->_lockPreviousValue; } - void OutputVariable::setLockPreviousOutputValue(bool lockPreviousOutputValue) { - this->_lockPreviousOutputValue = lockPreviousOutputValue; + 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(); } - bool OutputVariable::isLockedPreviousOutputValue() const { - return this->_lockPreviousOutputValue; + Complexity OutputVariable::currentComplexity() const { + if (_defuzzifier.get()) + return _defuzzifier->complexity(_fuzzyOutput.get()); + return _fuzzyOutput->complexity(); } void OutputVariable::defuzzify() { - if (fl::Op::isFinite(this->_outputValue)) { - this->_previousOutputValue = this->_outputValue; + if (not _enabled) return; + + if (Op::isFinite(_value)) { + _previousValue = _value; } + std::string exception; scalar result = fl::nan; - bool isValid = this->_enabled and not this->_fuzzyOutput->isEmpty(); + bool isValid = not _fuzzyOutput->isEmpty(); if (isValid) { - if (not _defuzzifier.get()) { - throw fl::Exception("[defuzzifier error] " - "defuzzifier needed to defuzzify output variable <" + _name + ">", FL_AT); + /* 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() + ">"; } - result = this->_defuzzifier->defuzzify(this->_fuzzyOutput.get(), _minimum, _maximum); - } else { + } + + 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 (_lockPreviousOutputValue and not Op::isNaN(_previousOutputValue)) { - result = _previousOutputValue; + if (_lockPreviousValue and not Op::isNaN(_previousValue)) { + result = _previousValue; } else { result = _defaultValue; } } - if (_lockOutputValueInRange) { - result = fl::Op::bound(result, _minimum, _maximum); - } + setValue(result); - this->_outputValue = result; + if (not exception.empty()) { + throw Exception(exception, FL_AT); + } } std::string OutputVariable::fuzzyOutputValue() const { std::ostringstream ss; - for (std::size_t i = 0; i < _terms.size(); ++i) { - scalar degree = _fuzzyOutput->activationDegree(_terms.at(i)); - if (i == 0) { - ss << fl::Op::str(degree); - } else { - if (fl::Op::isNaN(degree) or fl::Op::isGE(degree, 0.0)) - ss << " + " << fl::Op::str(degree); - else - ss << " - " << fl::Op::str(std::fabs(degree)); - } - ss << "/" << _terms.at(i)->getName(); + 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(); - setPreviousOutputValue(fl::nan); - setOutputValue(fl::nan); + 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 index 37cf25c..516b466 100644 --- a/fuzzylite/src/variable/Variable.cpp +++ b/fuzzylite/src/variable/Variable.cpp @@ -1,45 +1,34 @@ /* - Author: Juan Rada-Vilela, Ph.D. - Copyright (C) 2010-2014 FuzzyLite Limited - All rights reserved + 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 GNU Lesser General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your option) - any later version. + the terms of the FuzzyLite License included with the software. - fuzzylite is distributed in the hope that it will be useful, but WITHOUT - ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - for more details. - - You should have received a copy of the GNU Lesser General Public License - along with fuzzylite. If not, see <http://www.gnu.org/licenses/>. - - fuzzyliteâ„¢ is a trademark of FuzzyLite Limited. + 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/defuzzifier/Centroid.h" #include "fl/imex/FllExporter.h" #include "fl/norm/Norm.h" #include "fl/term/Constant.h" #include "fl/term/Linear.h" -#include "fl/term/Term.h" -#include <algorithm> -#include <map> -#include <sstream> +#include <queue> namespace fl { Variable::Variable(const std::string& name, scalar minimum, scalar maximum) - : _name(name), _minimum(minimum), _maximum(maximum), _enabled(true) { - } + : _name(name), _description(""), + _value(fl::nan), _minimum(minimum), _maximum(maximum), + _enabled(true), _lockValueInRange(false) { } Variable::Variable(const Variable& other) { copyFrom(other); @@ -58,9 +47,12 @@ namespace fl { void Variable::copyFrom(const Variable& other) { _name = other._name; - _enabled = other._enabled; + _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()); } @@ -80,13 +72,31 @@ namespace fl { 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 this->_maximum - this->_minimum; + return getMaximum() - getMinimum(); } void Variable::setMinimum(scalar minimum) { @@ -113,24 +123,47 @@ namespace fl { 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) { + for (std::size_t i = 0; i < terms().size(); ++i) { + Term* term = _terms.at(i); scalar fx = fl::nan; try { - fx = _terms.at(i)->membership(x); + fx = term->membership(x); } catch (...) { //ignore } if (i == 0) { - ss << fl::Op::str(fx); + ss << Op::str(fx); } else { - if (fl::Op::isNaN(fx) or fl::Op::isGE(fx, 0.0)) - ss << " + " << fl::Op::str(fx); + if (Op::isNaN(fx) or Op::isGE(fx, 0.0)) + ss << " + " << Op::str(fx); else - ss << " - " << fl::Op::str(std::fabs(fx)); + ss << " - " << Op::str(std::abs(fx)); } - ss << "/" << _terms.at(i)->getName(); + ss << "/" << term->getName(); } return ss.str(); } @@ -140,14 +173,15 @@ namespace fl { 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 = _terms.at(i)->membership(x); + y = term->membership(x); } catch (...) { //ignore } - if (fl::Op::isGt(y, ymax)) { + if (Op::isGt(y, ymax)) { ymax = y; - result = _terms.at(i); + result = term; } } if (yhighest) *yhighest = ymax; @@ -162,70 +196,83 @@ namespace fl { * Operations for datatype _terms */ - struct SortByCoG { - std::map<const Term*, scalar> centroids; + typedef std::pair<Term*, scalar> TermCentroid; + + struct Ascending { - bool operator()(const Term* a, const Term * b) { - return fl::Op::isLt( - centroids.find(a)->second, - centroids.find(b)->second); + 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; - std::map<const Term*, scalar> centroids; + 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)) { - centroids[term] = term->membership(0); + centroid = term->membership(0); } else { - centroids[term] = defuzzifier.defuzzify(term, _minimum, _maximum); + centroid = defuzzifier.defuzzify(term, getMinimum(), getMaximum()); } } catch (...) { //ignore error possibly due to Function not loaded - centroids[term] = fl::inf; + centroid = fl::inf; } + termCentroids.push(TermCentroid(term, centroid)); + FL_DBG(term->toString() << " -> " << centroid) } - SortByCoG criterion; - criterion.centroids = centroids; - std::sort(_terms.begin(), _terms.end(), criterion); + + 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) { - this->_terms.push_back(term); + _terms.push_back(term); } - void Variable::insertTerm(Term* term, int index) { - this->_terms.insert(this->_terms.begin() + index, term); + void Variable::insertTerm(Term* term, std::size_t index) { + _terms.insert(_terms.begin() + index, term); } - Term* Variable::getTerm(int index) const { - return this->_terms.at(index); + 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) { + for (std::size_t i = 0; i < terms().size(); ++i) { if (_terms.at(i)->getName() == name) { - return _terms.at(i); + return terms().at(i); } } - throw fl::Exception("[variable error] term <" + name + "> " - "not found in variable <" + this->_name + ">", FL_AT); + throw Exception("[variable error] term <" + name + "> " + "not found in variable <" + getName() + ">", FL_AT); } bool Variable::hasTerm(const std::string& name) const { - return getTerm(name) != fl::null; + for (std::size_t i = 0; i < _terms.size(); ++i) { + if (_terms.at(i)->getName() == name) { + return true; + } + } + return false; } - Term* Variable::removeTerm(int index) { - Term* result = this->_terms.at(index); - this->_terms.erase(this->_terms.begin() + index); + Term* Variable::removeTerm(std::size_t index) { + Term* result = _terms.at(index); + _terms.erase(_terms.begin() + index); return result; } - int Variable::numberOfTerms() const { - return this->_terms.size(); + std::size_t Variable::numberOfTerms() const { + return _terms.size(); } const std::vector<Term*>& Variable::terms() const { @@ -240,5 +287,9 @@ namespace fl { 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()); + } + +} + |