WIP: страницы для правил #62

Closed
Ghost wants to merge 132 commits from master into 61-rules-pages
5 changed files with 50 additions and 33 deletions
Showing only changes of commit 1a0d57a78e - Show all commits

View File

@ -1,17 +1,5 @@
package ru.ulstu.extractor; 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.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling; 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 STATISTIC = "statistic";
public static final String LIST_RULE = "listRules"; public static final String LIST_RULE = "listRules";
public static final String ADD_RULE = "addRule"; public static final String ADD_RULE = "addRule";
public static final String RECOMMENDATIONS = "recommendations";
public static String getLIST_INDEXED_REPOSITORIES() { public static String getLIST_INDEXED_REPOSITORIES() {
return LIST_INDEXED_REPOSITORIES; return LIST_INDEXED_REPOSITORIES;
@ -38,4 +39,8 @@ public class Route {
public static String getSTATISTIC() { public static String getSTATISTIC() {
return STATISTIC; 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 @Service
public class FuzzyInferenceService { 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 RuleService ruleService;
private final AntecedentValueService antecedentValueService; private final AntecedentValueService antecedentValueService;
@ -49,7 +52,7 @@ public class FuzzyInferenceService {
input.setName(key); input.setName(key);
input.setDescription(""); input.setDescription("");
input.setEnabled(true); input.setEnabled(true);
input.setRange(0.000, 1.000); input.setRange(0.000, antecedentValues.size() + 1);
input.setLockValueInRange(false); input.setLockValueInRange(false);
for (int i = 0; i < antecedentValues.size(); i++) { for (int i = 0; i < antecedentValues.size(); i++) {
input.addTerm(new Triangle(antecedentValues.get(i).getAntecedentValue(), i, i + 2)); input.addTerm(new Triangle(antecedentValues.get(i).getAntecedentValue(), i, i + 2));
@ -57,6 +60,16 @@ public class FuzzyInferenceService {
engine.addInputVariable(input); 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(); RuleBlock mamdani = new RuleBlock();
mamdani.setName("mamdani"); mamdani.setName("mamdani");
@ -81,17 +94,19 @@ public class FuzzyInferenceService {
Engine engine = getFuzzyEngine(); Engine engine = getFuzzyEngine();
List<AntecedentValue> antecedentValues = antecedentValueService.getList(); List<AntecedentValue> antecedentValues = antecedentValueService.getList();
Map<String, Double> variableValues = new HashMap<>(); Map<String, Double> variableValues = new HashMap<>();
variableValues.put("COMMITS", 10.0);
variableValues.put("AUTHORS", 10.0);
engine.addRuleBlock(getRuleBlock(engine, variableValues, antecedentValues)); engine.addRuleBlock(getRuleBlock(engine, variableValues, antecedentValues));
return getConsequent(engine, variableValues); return getConsequent(engine, variableValues);
} }
private String getConsequent(Engine engine, Map<String, Double> 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()) { for (Map.Entry<String, Double> variableValue : variableValues.entrySet()) {
InputVariable inputVariable = engine.getInputVariable(variableValue.getKey()); InputVariable inputVariable = engine.getInputVariable(variableValue.getKey());
inputVariable.setValue(variableValue.getValue()); inputVariable.setValue(variableValue.getValue());
} }
engine.process(); 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"/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head> </head>
<div class="container" layout:fragment="content"> <div class="container" layout:fragment="content">
<table class="table table-striped"> <div th:text="${recommendations}"></div>
<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> </div>
</html> </html>