Skip to content

Commit

Permalink
Feature/qo 26 feedback extrahieren (qped-eu#7)
Browse files Browse the repository at this point in the history
* create Feedback package for Syntax Errors

* added changes from master

* test Feedback

* test Feedback

* test Feedback

* demo for Syntax Feedback

* demo for Syntax Feedback

* demo for Syntax Feedback

* demo for Syntax Feedback

* demo for Syntax Feedback

* demo for Syntax Feedback

* demo for Syntax Feedback

* demo for Syntax Feedback

* demo for Syntax Feedback

* extract syntax feedback generator

* extract syntax feedback generator

Co-authored-by: Ajio <[email protected]>
  • Loading branch information
OmarAji and Ajio authored May 8, 2022
1 parent 25940ea commit 14d3f4d
Show file tree
Hide file tree
Showing 11 changed files with 739 additions and 110 deletions.
121 changes: 59 additions & 62 deletions src/main/java/eu/qped/java/checkers/mass/Mass.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package eu.qped.java.checkers.mass;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import eu.qped.framework.Checker;
import eu.qped.framework.QfProperty;
import eu.qped.framework.qf.QfObject;
Expand All @@ -14,85 +10,85 @@
import eu.qped.java.checkers.style.StyleConfigurator;
import eu.qped.java.checkers.style.StyleFeedback;
import eu.qped.java.checkers.syntax.SyntaxChecker;
import eu.qped.java.checkers.syntax.SyntaxFeedback;
import eu.qped.java.feedback.syntaxLagacy.SyntaxFeedback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Mass implements Checker {

@QfProperty
private QFMainSettings mainSettings;

@QfProperty
private QFStyleSettings styleSettings;

@QfProperty
private QFSemSettings semSettings;
@QfProperty
private QFMainSettings mainSettings;

@QfProperty
private QFStyleSettings styleSettings;

@QfProperty
private QFSemSettings semSettings;

private final static String NEW_LINE = "\n" + "\n";

@Override
public void check(QfObject qfObject) throws Exception {

Map<String, String> styleSettings = new HashMap<>();
Map<String , String> mainSettings = new HashMap<>();
Map<String, String> mainSettings = new HashMap<>();


/*
main settings
*/
if (this.mainSettings != null) {
mainSettings.put("syntaxLevel" , this.mainSettings.getSyntaxLevel());
mainSettings.put("preferredLanguage" , this.mainSettings.getPreferredLanguage());
mainSettings.put("styleNeeded" , this.mainSettings.getStyleNeeded());
mainSettings.put("semanticNeeded" , this.mainSettings.getSemanticNeeded());
mainSettings.put("syntaxLevel", this.mainSettings.getSyntaxLevel());
mainSettings.put("preferredLanguage", this.mainSettings.getPreferredLanguage());
mainSettings.put("styleNeeded", this.mainSettings.getStyleNeeded());
mainSettings.put("semanticNeeded", this.mainSettings.getSemanticNeeded());
}

/*
Style Configs
*/
if (this.styleSettings != null) {
styleSettings.put("mainLevel" , this.styleSettings.getMainLevel());
styleSettings.put("maxClassLength" , this.styleSettings.getClassLength());
styleSettings.put("maxMethodLength", this.styleSettings.getMethodLength());
styleSettings.put("maxFieldsCount", this.styleSettings.getFieldsCount());
styleSettings.put("maxCycloComplexity", this.styleSettings.getCycloComplexity());
styleSettings.put("varNamesRegEx", this.styleSettings.getVarName());
styleSettings.put("methodNamesRegEx", this.styleSettings.getMethodName());
styleSettings.put("classNameRegEx", this.styleSettings.getClassName());
styleSettings.put("basisLevel", this.styleSettings.getBasisLevel());
styleSettings.put("namesLevel", this.styleSettings.getNamesLevel());
styleSettings.put("compLevel", this.styleSettings.getCompLevel());
styleSettings.put("mainLevel", this.styleSettings.getMainLevel());
styleSettings.put("maxClassLength", this.styleSettings.getClassLength());
styleSettings.put("maxMethodLength", this.styleSettings.getMethodLength());
styleSettings.put("maxFieldsCount", this.styleSettings.getFieldsCount());
styleSettings.put("maxCycloComplexity", this.styleSettings.getCycloComplexity());
styleSettings.put("varNamesRegEx", this.styleSettings.getVarName());
styleSettings.put("methodNamesRegEx", this.styleSettings.getMethodName());
styleSettings.put("classNameRegEx", this.styleSettings.getClassName());
styleSettings.put("basisLevel", this.styleSettings.getBasisLevel());
styleSettings.put("namesLevel", this.styleSettings.getNamesLevel());
styleSettings.put("compLevel", this.styleSettings.getCompLevel());
}

StyleConfigurator styleConfigurator = StyleConfigurator.createStyleConfigurator(this.styleSettings);

StyleChecker styleChecker = new StyleChecker(styleConfigurator);




Map<String , String> semanticSettings = new HashMap<>();
Map<String, String> semanticSettings = new HashMap<>();

/*
Semantic Configs
*/
if (this.semSettings != null) {
semanticSettings.put("methodName" , this.semSettings.getMethodName());
semanticSettings.put("recursionAllowed" , this.semSettings.getRecursionAllowed());
semanticSettings.put("whileLoop" , this.semSettings.getWhileLoop());
semanticSettings.put("forLoop" , this.semSettings.getForLoop());
semanticSettings.put("forEachLoop" , this.semSettings.getForEachLoop());
semanticSettings.put("ifElseStmt" , this.semSettings.getIfElseStmt());
semanticSettings.put("doWhileLoop" , this.semSettings.getDoWhileLoop());
semanticSettings.put("returnType" , this.semSettings.getReturnType());
semanticSettings.put("methodName", this.semSettings.getMethodName());
semanticSettings.put("recursionAllowed", this.semSettings.getRecursionAllowed());
semanticSettings.put("whileLoop", this.semSettings.getWhileLoop());
semanticSettings.put("forLoop", this.semSettings.getForLoop());
semanticSettings.put("forEachLoop", this.semSettings.getForEachLoop());
semanticSettings.put("ifElseStmt", this.semSettings.getIfElseStmt());
semanticSettings.put("doWhileLoop", this.semSettings.getDoWhileLoop());
semanticSettings.put("returnType", this.semSettings.getReturnType());
}

MainSettings mainSettingsConfiguratorConf = new MainSettings(mainSettings);
SemanticConfigurator semanticConfigurator = SemanticConfigurator.createSemanticConfigurator(new QFSemSettings());




SemanticChecker semanticChecker = SemanticChecker.createSemanticMassChecker(semanticConfigurator);
SyntaxChecker syntaxChecker = SyntaxChecker.builder().answer(qfObject.getAnswer()).build();

Expand All @@ -117,33 +113,34 @@ public void check(QfObject qfObject) throws Exception {

int i = 0;

for (StyleFeedback styleFeedback : styleFeedbacks){
for (StyleFeedback styleFeedback : styleFeedbacks) {
result[i] = "style Feedback";
result[i+1] = styleFeedback.getDesc()
result[i + 1] = styleFeedback.getDesc()
+ NEW_LINE
+ styleFeedback.getBody()
+ NEW_LINE
+styleFeedback.getBody()
+NEW_LINE
+styleFeedback.getLine()
+NEW_LINE
+styleFeedback.getExample()
+ styleFeedback.getLine()
+ NEW_LINE
+"------------------------------------------------------------------------------";
i = i+2;
+ styleFeedback.getExample()
+ NEW_LINE
+ "------------------------------------------------------------------------------";
i = i + 2;
}

for (SemanticFeedback semanticFeedback : semanticFeedbacks){
for (SemanticFeedback semanticFeedback : semanticFeedbacks) {
result[i] = "semantic Feedback";
result[i+1] = semanticFeedback.getBody() + NEW_LINE
+"--------------------------------------------------";
i = i + 2 ;
result[i + 1] = semanticFeedback.getBody() + NEW_LINE
+ "--------------------------------------------------";
i = i + 2;
}

for (SyntaxFeedback syntax: syntaxFeedbacks){
result[i+1] = syntax.getFeedbackContent() + NEW_LINE
+ syntax.getBody() + NEW_LINE +
syntax.getSolutionExample() + NEW_LINE
+"--------------------------------------------------";
i = i+2;
for (SyntaxFeedback syntax : syntaxFeedbacks) {
result[i + 1] = ""
+ syntax.getFeedbackContent()
+ NEW_LINE
+ "--------------------------------------------------"
;
i = i + 2;
}

qfObject.setFeedback(result);
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/eu/qped/java/checkers/mass/MassExecutor.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package eu.qped.java.checkers.mass;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import eu.qped.framework.Feedback;
import eu.qped.framework.Translator;
Expand All @@ -14,7 +12,8 @@
import eu.qped.java.checkers.style.StyleViolation;
import eu.qped.java.checkers.syntax.SyntaxError;
import eu.qped.java.checkers.syntax.SyntaxChecker;
import eu.qped.java.checkers.syntax.SyntaxFeedback;
import eu.qped.java.feedback.syntaxLagacy.SyntaxFeedback;
import eu.qped.java.feedback.syntaxLagacy.SyntaxFeedbackGenerator;

/**
* Executor class, execute all components of the System to analyze the code
Expand Down Expand Up @@ -89,10 +88,11 @@ public void execute() {
}
} else {
syntaxChecker.setLevel(mainSettingsConfigurator.getSyntaxLevel());
syntaxFeedbacks = syntaxChecker.getFeedbacks();
SyntaxFeedbackGenerator feedbackGenerator = SyntaxFeedbackGenerator.builder().build();
syntaxErrors = syntaxChecker.getSyntaxErrors();
syntaxFeedbacks = feedbackGenerator.generateFeedbacks(syntaxErrors);

//auto checker
syntaxErrors = syntaxChecker.getSyntaxErrors();
}

// translate Feedback body if needed
Expand Down Expand Up @@ -130,6 +130,7 @@ private void translate(boolean styleNeeded, boolean semanticNeeded) {
}
}


public List<StyleFeedback> getStyleFeedbacks() {
return styleFeedbacks;
}
Expand Down Expand Up @@ -256,4 +257,4 @@ public List<SyntaxError> getSyntaxErrors() {
// }


}
}
13 changes: 7 additions & 6 deletions src/main/java/eu/qped/java/checkers/syntax/SyntaxChecker.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package eu.qped.java.checkers.syntax;

import eu.qped.framework.CheckLevel;
import eu.qped.java.feedback.syntaxLagacy.SyntaxFeedback;
import eu.qped.java.feedback.syntaxLagacy.SyntaxFeedbackGenerator;
import eu.qped.java.utils.compiler.Compiler;
import lombok.*;
import lombok.Data;
Expand Down Expand Up @@ -28,7 +30,6 @@ public class SyntaxChecker {
private String sourceCode;
private CheckLevel level;
private List<SyntaxError> syntaxErrors;
private List<SyntaxFeedback> feedbacks;
@NonNull
private boolean errorOccurred;

Expand All @@ -39,11 +40,6 @@ public void check() {
this.setSourceCode(compiler.getFullSourceCode());
if (!diagnostics.isEmpty()) {
analyseDiagnostics(diagnostics);
SyntaxFeedbackGenerator feedbackGenerator = new SyntaxFeedbackGenerator(this.getSourceCode(), this.getLevel());
feedbacks = feedbackGenerator.generateFeedbacks(syntaxErrors);
} else {
syntaxErrors = Collections.emptyList();
feedbacks = Collections.emptyList();
}
}

Expand All @@ -65,6 +61,7 @@ private void analyseDiagnostics(List<Diagnostic<? extends JavaFileObject>> diagn
addProp.put("forSemExpected", forExpected);
}
String errorTrigger = splitSource[0];

syntaxErrors.add(
SyntaxError.builder()
.errorCode(diagnostic.getCode())
Expand All @@ -74,8 +71,12 @@ private void analyseDiagnostics(List<Diagnostic<? extends JavaFileObject>> diagn
.line(diagnostic.getLineNumber())
.additionalProperties(addProp)
.errorTrigger(errorTrigger)
.kind(diagnostic.getKind())
.columnNumber(diagnostic.getColumnNumber())
.source(diagnostic.getSource())
.build()
);

}
}
}
6 changes: 6 additions & 0 deletions src/main/java/eu/qped/java/checkers/syntax/SyntaxError.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package eu.qped.java.checkers.syntax;


import javax.tools.Diagnostic.Kind;
import javax.tools.JavaFileObject;

import lombok.*;

import java.util.Map;
Expand All @@ -16,10 +19,13 @@ public class SyntaxError {
private String errorCode;
private String errorMsg;
private String errorTrigger;
private Kind kind;
private JavaFileObject source;

private long line;
private long startPos;
private long endPos;
private long columnNumber;

@Override
public boolean equals(Object o){
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/eu/qped/java/feedback/FeedbackGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package eu.qped.java.feedback;

import eu.qped.framework.Feedback;

import java.util.List;

public interface FeedbackGenerator<F,E> {
List<F> generateFeedbacks(List<E> errors);
}
Loading

0 comments on commit 14d3f4d

Please sign in to comment.