1 /*
2  fuzzylite (R), a fuzzy logic control library in C++.
3  Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved.
4  Author: Juan Rada-Vilela, Ph.D. <jcrada@fuzzylite.com>
5 
6  This file is part of fuzzylite.
7 
8  fuzzylite is free software: you can redistribute it and/or modify it under
9  the terms of the FuzzyLite License included with the software.
10 
11  You should have received a copy of the FuzzyLite License along with
12  fuzzylite. If not, see <http://www.fuzzylite.com/license/>.
13 
14  fuzzylite is a registered trademark of FuzzyLite Limited.
15  */
16 
17 #include "fl/term/Binary.h"
18 
19 namespace fl {
20 
Binary(const std::string & name,scalar start,scalar direction,scalar height)21     Binary::Binary(const std::string& name, scalar start, scalar direction, scalar height)
22     : Term(name, height), _start(start), _direction(direction) { }
23 
~Binary()24     Binary::~Binary() { }
25 
className() const26     std::string Binary::className() const {
27         return "Binary";
28     }
29 
complexity() const30     Complexity Binary::complexity() const {
31         return Complexity().comparison(5).arithmetic(1);
32     }
33 
membership(scalar x) const34     scalar Binary::membership(scalar x) const {
35         if (Op::isNaN(x)) return fl::nan;
36         if (_direction > _start and Op::isGE(x, _start)) {
37             return Term::_height * 1.0;
38         }
39         if (_direction < _start and Op::isLE(x, _start)) {
40             return Term::_height * 1.0;
41         }
42         return Term::_height * 0.0;
43     }
44 
parameters() const45     std::string Binary::parameters() const {
46         return Op::join(2, " ", _start, _direction) +
47                 (not Op::isEq(getHeight(), 1.0) ? " " + Op::str(getHeight()) : "");
48     }
49 
configure(const std::string & parameters)50     void Binary::configure(const std::string& parameters) {
51         if (parameters.empty()) return;
52         std::vector<std::string> values = Op::split(parameters, " ");
53         std::size_t required = 2;
54         if (values.size() < required) {
55             std::ostringstream ex;
56             ex << "[configuration error] term <" << className() << ">"
57                     << " requires <" << required << "> parameters";
58             throw Exception(ex.str(), FL_AT);
59         }
60         setStart(Op::toScalar(values.at(0)));
61         setDirection(Op::toScalar(values.at(1)));
62         if (values.size() > required)
63             setHeight(Op::toScalar(values.at(required)));
64     }
65 
setStart(scalar minimum)66     void Binary::setStart(scalar minimum) {
67         this->_start = minimum;
68     }
69 
getStart() const70     scalar Binary::getStart() const {
71         return this->_start;
72     }
73 
setDirection(scalar direction)74     void Binary::setDirection(scalar direction) {
75         this->_direction = direction;
76     }
77 
getDirection() const78     scalar Binary::getDirection() const {
79         return this->_direction;
80     }
81 
direction() const82     Binary::Direction Binary::direction() const {
83         if (this->_direction > _start) return Positive;
84         if (this->_direction < _start) return Negative;
85         return Undefined;
86     }
87 
clone() const88     Binary* Binary::clone() const {
89         return new Binary(*this);
90     }
91 
constructor()92     Term* Binary::constructor() {
93         return new Binary;
94     }
95 
96 }
97