From 1c8eabfc08da8131c8f2b63d230abf1117c2d85c Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 11 Sep 2023 18:22:00 +0400 Subject: [PATCH] #3 -- Config of fuzzy engine --- .../java/ru/ulstu/fc/config/FuzzyEngine.java | 16 ++++++++++++++++ .../fc/rule/service/FuzzyInferenceService.java | 18 ++++++++---------- 2 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 src/main/java/ru/ulstu/fc/config/FuzzyEngine.java diff --git a/src/main/java/ru/ulstu/fc/config/FuzzyEngine.java b/src/main/java/ru/ulstu/fc/config/FuzzyEngine.java new file mode 100644 index 0000000..15b1e76 --- /dev/null +++ b/src/main/java/ru/ulstu/fc/config/FuzzyEngine.java @@ -0,0 +1,16 @@ +package ru.ulstu.fc.config; + +import com.fuzzylite.Engine; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class FuzzyEngine { + @Bean + public Engine getFuzzyEngine() { + Engine engine = new Engine(); + engine.setName("Fuzzy rules"); + engine.setDescription(""); + return engine; + } +} diff --git a/src/main/java/ru/ulstu/fc/rule/service/FuzzyInferenceService.java b/src/main/java/ru/ulstu/fc/rule/service/FuzzyInferenceService.java index 518a9e5..231b644 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/FuzzyInferenceService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/FuzzyInferenceService.java @@ -30,6 +30,7 @@ public class FuzzyInferenceService { + OUTPUT_VARIABLE_NAME + " is %s"; private final static String NO_RESULT = "Нет результата"; + private final Engine fuzzyEngine; private Map>> inputFuzzyTerms = Map.of( "возраст", @@ -48,6 +49,10 @@ public class FuzzyInferenceService { new AbstractMap.SimpleEntry("средний", 100000), new AbstractMap.SimpleEntry("большой", 1000000))); + public FuzzyInferenceService(Engine fuzzyEngine) { + this.fuzzyEngine = fuzzyEngine; + } + private List getDemoRules() { return List.of( String.format(RULE_TEMPLATE, "возраст", "молодой", "доход", "высокий", "большой"), @@ -112,13 +117,6 @@ public class FuzzyInferenceService { return mamdani; } - private Engine getFuzzyEngine() { - Engine engine = new Engine(); - engine.setName("Fuzzy rules"); - engine.setDescription(""); - return engine; - } - private Map getConsequent(Engine engine, Map variableValues) { OutputVariable outputVariable = engine.getOutputVariable(OUTPUT_VARIABLE_NAME); for (Map.Entry variableValue : variableValues.entrySet()) { @@ -135,9 +133,9 @@ public class FuzzyInferenceService { } public Map getFuzzyInference() { - Engine engine = getFuzzyEngine(); - engine.addRuleBlock(getRuleBlock(engine, getDemoRules())); - return getConsequent(engine, Map.of("возраст", 20.0, "доход", 250000.0)); + fuzzyEngine.getRuleBlocks().clear(); + fuzzyEngine.addRuleBlock(getRuleBlock(fuzzyEngine, getDemoRules())); + return getConsequent(fuzzyEngine, Map.of("возраст", 20.0, "доход", 250000.0)); } }