Commit 6e2b548a authored by Falko Schumann's avatar Falko Schumann

Abhängigkeit zu Funclib BitCtrl beseitigt

parent 5890ddbb
......@@ -27,8 +27,5 @@ nerzswe {
String kernsoftware_version = '3.9.7'
dependencies {
// api group: 'de.bsvrz.sys', name: 'de.bsvrz.sys.funclib.bitctrl', version: '1.5.3'
api project(':de.bsvrz.sys.funclib.bitctrl:subprojects:de.bsvrz.sys.funclib.bitctrl')
testImplementation group: 'junit', name: 'junit', version: '4.12'
}
/*
* Segment 5 Intelligente Analyseverfahren, SWE 5.4 Funktionen Fuzzy
* Copyright (C) 2007-2015 BitCtrl Systems GmbH
*
* This library 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 2.1 of the License, or (at your option)
* any later version.
*
* This library 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 this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Contact Information:
* BitCtrl Systems GmbH
* Weißenfelser Straße 67
* 04229 Leipzig
* Phone: +49 341-490670
* mailto: info@bitctrl.de
*/
package de.bsvrz.iav.fuzzylib.fuzzylib;
/**
* Bestimmt das algebraische Produkt zweier Gleitkommazahlen.
*
* @author BitCtrl Systems GmbH, Falko Schumann
*/
public final class AlgebraischesProdukt extends BiOperation {
public AlgebraischesProdukt(Ausdruck<Double> a, Ausdruck<Double> b) {
super(Funktionen::algebraischesProdukt, a, b);
}
@Override
public String toString() {
return "produkt(" + getA() + ", " + getB() + ")";
}
}
/*
* Segment 5 Intelligente Analyseverfahren, SWE 5.4 Funktionen Fuzzy
* Copyright (C) 2007-2015 BitCtrl Systems GmbH
*
* This library 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 2.1 of the License, or (at your option)
* any later version.
*
* This library 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 this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Contact Information:
* BitCtrl Systems GmbH
* Weißenfelser Straße 67
* 04229 Leipzig
* Phone: +49 341-490670
* mailto: info@bitctrl.de
*/
package de.bsvrz.iav.fuzzylib.fuzzylib;
/**
* Bestimmt die algebraische Summe zweier Gleitkommazahlen.
*
* @author BitCtrl Systems GmbH, Falko Schumann
*/
public final class AlgebraischesSumme extends BiOperation {
public AlgebraischesSumme(Ausdruck<Double> a, Ausdruck<Double> b) {
super(Funktionen::algebraischesSumme, a, b);
}
@Override
public String toString() {
return "summe(" + getA() + ", " + getB() + ")";
}
}
/*
* Segment 5 Intelligente Analyseverfahren, SWE 5.4 Funktionen Fuzzy
* Copyright (C) 2007-2015 BitCtrl Systems GmbH
*
* This library 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 2.1 of the License, or (at your option)
* any later version.
*
* This library 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 this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Contact Information:
* BitCtrl Systems GmbH
* Weißenfelser Straße 67
* 04229 Leipzig
* Phone: +49 341-490670
* mailto: info@bitctrl.de
*/
package de.bsvrz.iav.fuzzylib.fuzzylib;
import java.util.*;
public interface Ausdruck<T> {
T interpretiere(Map<String, ?> kontext);
}
/*
* Segment 5 Intelligente Analyseverfahren, SWE 5.4 Funktionen Fuzzy
* Copyright (C) 2007-2015 BitCtrl Systems GmbH
*
* This library 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 2.1 of the License, or (at your option)
* any later version.
*
* This library 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 this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Contact Information:
* BitCtrl Systems GmbH
* Weißenfelser Straße 67
* 04229 Leipzig
* Phone: +49 341-490670
* mailto: info@bitctrl.de
*/
package de.bsvrz.iav.fuzzylib.fuzzylib;
import java.util.*;
import java.util.function.*;
/**
* Definiert eine Operation auf zwei Gleitkommazahlen.
*
* @author BitCtrl Systems GmbH, Falko Schumann
*/
public class BiOperation implements Ausdruck<Double> {
private BiFunction<Double, Double, Double> operation;
private final Ausdruck<Double> a;
private final Ausdruck<Double> b;
public BiOperation(BiFunction<Double, Double, Double> operation, Ausdruck<Double> a, Ausdruck<Double> b) {
this.operation = Objects.requireNonNull(operation, "operation");
this.a = Objects.requireNonNull(a, "a");
this.b = Objects.requireNonNull(b, "b");
}
public Ausdruck<Double> getA() {
return a;
}
public Ausdruck<Double> getB() {
return b;
}
@Override
public Double interpretiere(Map<String, ?> kontext) {
return operation.apply(a.interpretiere(kontext), b.interpretiere(kontext));
}
}
......@@ -26,14 +26,14 @@
package de.bsvrz.iav.fuzzylib.fuzzylib;
import de.bsvrz.sys.funclib.bitctrl.interpreter.*;
import java.util.*;
/**
* Stellt eine definierte Gleitkommazahl in einem Ausdruck dar.
*
* @author BitCtrl Systems GmbH, Falko Schumann
*/
public final class DoubleLiteral implements Literal<Double> {
public final class DoubleLiteral implements Ausdruck<Double> {
private final double wert;
......@@ -42,7 +42,7 @@ public final class DoubleLiteral implements Literal<Double> {
}
@Override
public Double interpret(Kontext kontext) {
public Double interpretiere(Map<String, ?> kontext) {
return wert;
}
......
......@@ -26,8 +26,6 @@
package de.bsvrz.iav.fuzzylib.fuzzylib;
import de.bsvrz.sys.funclib.bitctrl.interpreter.*;
import java.util.*;
/**
......@@ -35,23 +33,25 @@ import java.util.*;
*
* @author BitCtrl Systems GmbH, Falko Schumann
*/
public final class DoubleVariable implements Variable<Double> {
public final class DoubleVariable implements Ausdruck<Double> {
private final String name;
public DoubleVariable(String name) {
Kontext.pruefeName(name);
this.name = Objects.requireNonNull(name, "name");
}
@Override
public String getName() {
return name;
}
@Override
public Double interpret(Kontext kontext) {
return kontext.get(name, Number.class).doubleValue();
public Double interpretiere(Map<String, ?> kontext) {
Object variable = kontext.get(name);
if (variable == null) {
throw new VariableNichtDefiniertException("Die Variable \"" + name + "\" ist nicht definiert.");
} else if (!(variable instanceof Number)) {
throw new InterpreterException("Die Variable \"" + name + "\" muss eine Zahl sein.");
}
Number zahl = (Number) variable;
return zahl.doubleValue();
}
@Override
......
/*
* Segment 5 Intelligente Analyseverfahren, SWE 5.4 Funktionen Fuzzy
* Copyright (C) 2007-2015 BitCtrl Systems GmbH
*
* This library 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 2.1 of the License, or (at your option)
* any later version.
*
* This library 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 this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Contact Information:
* BitCtrl Systems GmbH
* Weißenfelser Straße 67
* 04229 Leipzig
* Phone: +49 341-490670
* mailto: info@bitctrl.de
*/
package de.bsvrz.iav.fuzzylib.fuzzylib;
import java.util.function.*;
/**
* Definiert mathematische Funktionen.
*
* @author BitCtrl Systems GmbH, Falko Schumann
*/
public final class Funktionen {
private Funktionen() {
// utility class
}
public static Double minimum(Double a, Double b) {
if (a == null || b == null) {
return null;
}
return Math.min(a, b);
}
public static Double maximum(Double a, Double b) {
if (a == null || b == null) {
return null;
}
return Math.max(a, b);
}
public static Double komplement(Double a) {
if (a == null) {
return null;
}
return 1.0 - a;
}
public static Double implikation(Double a, Double b) {
return maximum(komplement(a), b);
}
public static Double algebraischesProdukt(Double a, Double b) {
if (a == null || b == null) {
return null;
}
return a * b;
}
public static Double algebraischesSumme(Double a, Double b) {
if (a == null || b == null) {
return null;
}
return (a + b) - (a * b);
}
public static Double gammaOperator(Double a, Double b, Double gamma) {
if (a == null || b == null || gamma == null) {
return null;
}
return Math.pow(a * b, 1 - gamma) * Math.pow((a + b) - (a * b), gamma);
}
public static BiFunction<Double, Double, Double> gammaOperator(Double gamma) {
if (gamma == null) {
return (a, b) -> null;
}
return (a, b) -> Math.pow(a * b, 1 - gamma) * Math.pow((a + b) - (a * b), gamma);
}
}
/*
* Segment 5 Intelligente Analyseverfahren, SWE 5.4 Funktionen Fuzzy
* Copyright (C) 2007-2015 BitCtrl Systems GmbH
*
* This library 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 2.1 of the License, or (at your option)
* any later version.
*
* This library 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 this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Contact Information:
* BitCtrl Systems GmbH
* Weißenfelser Straße 67
* 04229 Leipzig
* Phone: +49 341-490670
* mailto: info@bitctrl.de
*/
package de.bsvrz.iav.fuzzylib.fuzzylib;
import de.bsvrz.sys.funclib.bitctrl.interpreter.*;
import java.util.*;
import java.util.stream.*;
/**
* Dieser Handler stellt Operationen mit Termen und Fuzzy-Variablen bereit.
* <p>
* Diese Operationen werden für die Regeln in der Regelbasis verwendet.
*
* @author BitCtrl Systems GmbH, Falko Schumann
* @see Term
* @see FuzzyVariable
* @see Regel
* @see Regelbasis
*/
public class FuzzyLogikHandler extends Handler {
/**
* Konjunktion bzw. logisches Und.
*/
public static final Operator UND = Operator.getOperator("und");
/**
* Disjunktion bzw. logisches Oder.
*/
public static final Operator ODER = Operator.getOperator("oder");
/**
* Negation bzw. logisches Nicht.
*/
public static final Operator NICHT = Operator.getOperator("nicht");
/**
* Logische Implikation.
*/
public static final Operator IMPLIKATION = Operator.getOperator("->");
/**
* Vereinigung bzw. Maximum.
*/
public static final Operator MAXIMUM = Operator.getOperator("Maximum");
/**
* Durchschnitt bzw. Minimum.
*/
public static final Operator MINIMUM = Operator.getOperator("Minimum");
/**
* Komplementärmenge.
*/
public static final Operator KOMPLEMENT = Operator.getOperator("Komplement");
/**
* Algebraische Summe.
*/
public static final Operator ALGEBRAISCHE_SUMME = Operator.getOperator("Algebraische Summe");
/**
* Algebraisches Produkt.
*/
public static final Operator ALGEBRAISCHES_PRODUKT = Operator.getOperator("Algebraisches Produkt");
/**
* Gamma-Operator.
*/
public static final Operator GAMMA_OPERATOR = Operator.getOperator("Gamma-Operator");
private static Operator[] operatoren = {
UND, ODER, NICHT, MINIMUM, MAXIMUM, KOMPLEMENT, IMPLIKATION,
ALGEBRAISCHES_PRODUKT, ALGEBRAISCHE_SUMME, GAMMA_OPERATOR
};
@Override
public Operator[] getHandledOperators() {
return operatoren;
}
@Override
public Object perform(Operator operator, List<?> operanden) {
if (!validiereHandler(operator, operanden).isValid()) {
throw new InterpreterException(InterpreterMessages.HandlerNotFound.toString());
}
if (operanden.stream().anyMatch(Objects::isNull))
return null;
double[] zahlen = operanden.stream().mapToDouble(o -> ((Number) o).doubleValue()).toArray();
if (operator == UND || operator == MINIMUM) {
return minimum(zahlen);
} else if (operator == ODER || operator == MAXIMUM) {
return maximum(zahlen);
} else if (operator == NICHT || operator == KOMPLEMENT) {
return komplement(zahlen[0]);
} else if (operator == IMPLIKATION) {
return implikation(zahlen[0], zahlen[1]);
} else if (operator == ALGEBRAISCHES_PRODUKT) {
return algebraischesProdukt(zahlen[0], zahlen[1]);
} else if (operator == ALGEBRAISCHE_SUMME) {
return algebraischeSumme(zahlen[0], zahlen[1]);
} else if (operator == GAMMA_OPERATOR) {
return gammaOperator(zahlen[0], zahlen[1], zahlen[2]);
}
throw new IllegalStateException("[unreachable code] Nicht behandelter Operator: " + operator);
}
@Override
public HandlerValidation validiereHandler(Operator operator, List<?> operanden) {
Objects.requireNonNull(operator, "operator");
Objects.requireNonNull(operanden, "operanden");
boolean anzahlOk = hatOperatorRichtigeAnzahlOperanden(operator, operanden.size());
boolean typOk = operanden.stream().allMatch(o -> o == null || o instanceof Number);
return new HandlerValidation(anzahlOk, typOk);
}
private boolean hatOperatorRichtigeAnzahlOperanden(Operator operator, int operandenanzahl) {
if (operandenanzahl <= 0) {
return false;
}
if (NICHT.equals(operator) || KOMPLEMENT.equals(operator)) {
return operandenanzahl == 1;
}
if (IMPLIKATION.equals(operator) || ALGEBRAISCHE_SUMME.equals(operator) || ALGEBRAISCHES_PRODUKT.equals(operator)) {
return operandenanzahl == 2;
}
if (GAMMA_OPERATOR.equals(operator)) {
return operandenanzahl == 3;
}
return UND.equals(operator) || ODER.equals(operator) || MAXIMUM.equals(operator) || MINIMUM.equals(operator);
}
private static double minimum(double... operanden) {
return DoubleStream.of(operanden).min()
.orElseThrow(() -> new IllegalArgumentException("Für das Minimum wird mindestens ein Operand benötigt."));
}
private static double maximum(double... operanden) {
return DoubleStream.of(operanden).max()
.orElseThrow(() -> new IllegalArgumentException("Für das Maximum wird mindestens ein Operand benötigt."));
}
private static double komplement(double operand) {
if (operand < 0.0 || operand > 1.0) {
throw new IllegalArgumentException("Für das Komplement muss der Operand zwischen 0 und 1 liegen: " + operand);
}
return 1.0 - operand;
}
private static double implikation(double a, double b) {
return maximum(komplement(a), b);
}
private static double algebraischesProdukt(double a, double b) {
return a * b;
}
private static double algebraischeSumme(double a, double b) {
return (a + b) - (a * b);
}
private static double gammaOperator(double gamma, double a, double b) {
return Math.pow(a * b, 1 - gamma) * Math.pow((a + b) - (a * b), gamma);
}
}
/*
* Segment 5 Intelligente Analyseverfahren, SWE 5.4 Funktionen Fuzzy
* Copyright (C) 2007-2015 BitCtrl Systems GmbH
*
* This library 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 2.1 of the License, or (at your option)
* any later version.
*
* This library 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 this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Contact Information:
* BitCtrl Systems GmbH
* Weißenfelser Straße 67
* 04229 Leipzig
* Phone: +49 341-490670
* mailto: info@bitctrl.de
*/
package de.bsvrz.iav.fuzzylib.fuzzylib;
import java.util.*;
/**
* Bestimmt das Ergebnis des Gammaoperators zweier Gleitkommazahlen.
*
* @author BitCtrl Systems GmbH, Falko Schumann
*/
public final class GammaOperator implements Ausdruck<Double> {
private final Ausdruck<Double> a;
private final Ausdruck<Double> b;
private final Ausdruck<Double> gamma;
public GammaOperator(Ausdruck<Double> a, Ausdruck<Double> b, Ausdruck<Double> gamma) {
this.a = Objects.requireNonNull(a, "a");
this.b = Objects.requireNonNull(b, "b");
this.gamma = Objects.requireNonNull(gamma, "gamma");
}
public Ausdruck<Double> getA() {
return a;
}
public Ausdruck<Double> getB() {
return b;
}
@Override
public Double interpretiere(Map<String, ?> kontext) {
return Funktionen.gammaOperator(
a.interpretiere(kontext),
b.interpretiere(kontext),
gamma.interpretiere(kontext)
);
}
@Override
public String toString() {
return "gamma(" + a + ", " + b + ", " + gamma + ")";
}
}
/*
* Segment 5 Intelligente Analyseverfahren, SWE 5.4 Funktionen Fuzzy
* Copyright (C) 2007-2015 BitCtrl Systems GmbH
*
* This library 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 2.1 of the License, or (at your option)
* any later version.
*
* This library 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 this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Contact Information:
* BitCtrl Systems GmbH
* Weißenfelser Straße 67
* 04229 Leipzig
* Phone: +49 341-490670
* mailto: info@bitctrl.de
*/
package de.bsvrz.iav.fuzzylib.fuzzylib;
/**
* Bestimmt die Implikation zweier Gleitkommazahlen.
*
* @author BitCtrl Systems GmbH, Falko Schumann
*/
public final class Implikation extends BiOperation {
public Implikation(Ausdruck<Double> a, Ausdruck<Double> b) {
super(Funktionen::implikation, a, b);
}
@Override
public String toString() {
return getA() + " -> " + getB();
}
}
/*
* Segment 5 Intelligente Analyseverfahren, SWE 5.4 Funktionen Fuzzy
* Copyright (C) 2007-2015 BitCtrl Systems GmbH
*
* This library is free software; you can redistribute it and/or modify it under