#74 -- fix rules

This commit is contained in:
Anton Romanov 2022-10-25 15:37:54 +04:00
parent 5b080f41c2
commit 1a0d57a78e
5 changed files with 50 additions and 33 deletions

View File

@ -1,17 +1,5 @@
package ru.ulstu.extractor;
import com.fuzzylite.Engine;
import com.fuzzylite.FuzzyLite;
import com.fuzzylite.Op;
import com.fuzzylite.activation.Highest;
import com.fuzzylite.defuzzifier.Centroid;
import com.fuzzylite.norm.s.Maximum;
import com.fuzzylite.norm.t.AlgebraicProduct;
import com.fuzzylite.rule.Rule;
import com.fuzzylite.rule.RuleBlock;
import com.fuzzylite.term.Trapezoid;
import com.fuzzylite.variable.InputVariable;
import com.fuzzylite.variable.OutputVariable;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

View File

@ -18,6 +18,7 @@ public class Route {
public static final String STATISTIC = "statistic";
public static final String LIST_RULE = "listRules";
public static final String ADD_RULE = "addRule";
public static final String RECOMMENDATIONS = "recommendations";
public static String getLIST_INDEXED_REPOSITORIES() {
return LIST_INDEXED_REPOSITORIES;
@ -38,4 +39,8 @@ public class Route {
public static String getSTATISTIC() {
return STATISTIC;
}
public static String getRECOMMENDATIONS() {
return RECOMMENDATIONS;
}
}

View File

@ -0,0 +1,25 @@
package ru.ulstu.extractor.rule.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import ru.ulstu.extractor.rule.service.FuzzyInferenceService;
import springfox.documentation.annotations.ApiIgnore;
import static ru.ulstu.extractor.core.Route.RECOMMENDATIONS;
@Controller
@ApiIgnore
public class RecommendationController {
private final FuzzyInferenceService fuzzyInferenceService;
public RecommendationController(FuzzyInferenceService fuzzyInferenceService) {
this.fuzzyInferenceService = fuzzyInferenceService;
}
@GetMapping(RECOMMENDATIONS)
public String getRecommendations(Model model) {
model.addAttribute("recommendations", fuzzyInferenceService.run());
return RECOMMENDATIONS;
}
}

View File

@ -19,7 +19,10 @@ import java.util.stream.Collectors;
@Service
public class FuzzyInferenceService {
private final static String RULE_TEMPLATE = "if %s is %s AND %s is %s then state is %s";
private final static String OUTPUT_VARIABLE_NAME = "state";
private final static String RULE_TEMPLATE = "if %s is %s and %s is %s then "
+ OUTPUT_VARIABLE_NAME
+ " is %s";
private final RuleService ruleService;
private final AntecedentValueService antecedentValueService;
@ -49,7 +52,7 @@ public class FuzzyInferenceService {
input.setName(key);
input.setDescription("");
input.setEnabled(true);
input.setRange(0.000, 1.000);
input.setRange(0.000, antecedentValues.size() + 1);
input.setLockValueInRange(false);
for (int i = 0; i < antecedentValues.size(); i++) {
input.addTerm(new Triangle(antecedentValues.get(i).getAntecedentValue(), i, i + 2));
@ -57,6 +60,16 @@ public class FuzzyInferenceService {
engine.addInputVariable(input);
});
OutputVariable output = new OutputVariable();
output.setName(OUTPUT_VARIABLE_NAME);
output.setDescription("");
output.setEnabled(true);
output.setRange(0.000, antecedentValues.size() + 1);
output.setLockValueInRange(false);
for (int i = 0; i < antecedentValues.size(); i++) {
output.addTerm(new Triangle(antecedentValues.get(i).getAntecedentValue(), i, i + 2));
}
engine.addOutputVariable(output);
RuleBlock mamdani = new RuleBlock();
mamdani.setName("mamdani");
@ -81,17 +94,19 @@ public class FuzzyInferenceService {
Engine engine = getFuzzyEngine();
List<AntecedentValue> antecedentValues = antecedentValueService.getList();
Map<String, Double> variableValues = new HashMap<>();
variableValues.put("COMMITS", 10.0);
variableValues.put("AUTHORS", 10.0);
engine.addRuleBlock(getRuleBlock(engine, variableValues, antecedentValues));
return getConsequent(engine, variableValues);
}
private String getConsequent(Engine engine, Map<String, Double> variableValues) {
OutputVariable outputVariable = engine.getOutputVariable("state");
OutputVariable outputVariable = engine.getOutputVariable(OUTPUT_VARIABLE_NAME);
for (Map.Entry<String, Double> variableValue : variableValues.entrySet()) {
InputVariable inputVariable = engine.getInputVariable(variableValue.getKey());
inputVariable.setValue(variableValue.getValue());
}
engine.process();
return outputVariable.highestMembership(outputVariable.getValue()).getSecond().getName();
return outputVariable == null ? "" : outputVariable.fuzzyOutputValue();
}
}

View File

@ -7,22 +7,6 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<div class="container" layout:fragment="content">
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<th scope="col">Рекомендации</th>
<th width="10%"></th>
</tr>
</thead>
<tbody>
<tr>
<td>Увеличить колличество авторов</td>
</tr>
<tr>
<td>Оценнить эффективность авторов</td>
</tr>
</tbody>
</table>
<input type="submit" class="btn btn-outline-success" value="Применить правила"/>
<div th:text="${recommendations}"></div>
</div>
</html>