-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1825 from jplag/report-viewer/display-base
Display Basecode in report viewer
- Loading branch information
Showing
12 changed files
with
283 additions
and
15 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
core/src/main/java/de/jplag/reporting/jsonfactory/BaseCodeReportWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package de.jplag.reporting.jsonfactory; | ||
|
||
import java.nio.file.Path; | ||
import java.util.Comparator; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
|
||
import de.jplag.JPlagComparison; | ||
import de.jplag.JPlagResult; | ||
import de.jplag.Match; | ||
import de.jplag.Submission; | ||
import de.jplag.Token; | ||
import de.jplag.reporting.FilePathUtil; | ||
import de.jplag.reporting.reportobject.model.BaseCodeMatch; | ||
import de.jplag.reporting.reportobject.model.CodePosition; | ||
import de.jplag.reporting.reportobject.writer.JPlagResultWriter; | ||
|
||
/** | ||
* Writes the comparisons of each Submission to the basecode in its own file | ||
*/ | ||
public class BaseCodeReportWriter { | ||
|
||
private final JPlagResultWriter resultWriter; | ||
private final Function<Submission, String> submissionToIdFunction; | ||
public static final String BASEPATH = "basecode"; | ||
|
||
/** | ||
* Creates a new BaseCodeReportWriter | ||
* @param submissionToIdFunction Function for translating a submission to a unique id | ||
* @param resultWriter Writer used for writing the result | ||
*/ | ||
public BaseCodeReportWriter(Function<Submission, String> submissionToIdFunction, JPlagResultWriter resultWriter) { | ||
this.submissionToIdFunction = submissionToIdFunction; | ||
this.resultWriter = resultWriter; | ||
} | ||
|
||
/** | ||
* Writes the basecode of each submission in the result into its own file in the result writer | ||
* @param jPlagResult The result containing the submissions | ||
*/ | ||
public void writeBaseCodeReport(JPlagResult jPlagResult) { | ||
Set<Submission> submissions = new HashSet<>(); | ||
|
||
int numberOfComparisons = jPlagResult.getOptions().maximumNumberOfComparisons(); | ||
List<JPlagComparison> comparisons = jPlagResult.getComparisons(numberOfComparisons); | ||
for (JPlagComparison comparison : comparisons) { | ||
submissions.add(comparison.firstSubmission()); | ||
submissions.add(comparison.secondSubmission()); | ||
} | ||
submissions.forEach(this::writeBaseCodeFile); | ||
} | ||
|
||
private void writeBaseCodeFile(Submission submission) { | ||
List<BaseCodeMatch> matches = List.of(); | ||
if (submission.getBaseCodeComparison() != null) { | ||
JPlagComparison baseCodeComparison = submission.getBaseCodeComparison(); | ||
boolean takeLeft = baseCodeComparison.firstSubmission().equals(submission); | ||
matches = baseCodeComparison.matches().stream().map(match -> convertToBaseCodeMatch(submission, match, takeLeft)).toList(); | ||
} | ||
resultWriter.addJsonEntry(matches, Path.of(BASEPATH, submissionToIdFunction.apply(submission).concat(".json"))); | ||
} | ||
|
||
private BaseCodeMatch convertToBaseCodeMatch(Submission submission, Match match, boolean takeLeft) { | ||
List<Token> tokens = submission.getTokenList().subList(takeLeft ? match.startOfFirst() : match.startOfSecond(), | ||
(takeLeft ? match.endOfFirst() : match.endOfSecond()) + 1); | ||
|
||
Comparator<? super Token> lineComparator = Comparator.comparingInt(Token::getLine); | ||
Token start = tokens.stream().min(lineComparator).orElseThrow(); | ||
Token end = tokens.stream().max(lineComparator).orElseThrow(); | ||
|
||
CodePosition startPosition = new CodePosition(start.getLine(), start.getColumn() - 1, | ||
takeLeft ? match.startOfFirst() : match.startOfSecond()); | ||
CodePosition endPosition = new CodePosition(end.getLine(), end.getColumn() + end.getLength() - 1, | ||
takeLeft ? match.endOfFirst() : match.endOfSecond()); | ||
|
||
return new BaseCodeMatch(FilePathUtil.getRelativeSubmissionPath(start.getFile(), submission, submissionToIdFunction).toString(), | ||
startPosition, endPosition, match.length()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
core/src/main/java/de/jplag/reporting/reportobject/model/BaseCodeMatch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package de.jplag.reporting.reportobject.model; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public record BaseCodeMatch(@JsonProperty("file_name") String fileName, @JsonProperty("start") CodePosition start, | ||
@JsonProperty("end") CodePosition end, @JsonProperty("tokens") int tokens) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { CodePosition } from './Match' | ||
import { MatchInSingleFile } from './MatchInSingleFile' | ||
|
||
export class BaseCodeMatch extends MatchInSingleFile { | ||
constructor(fileName: string, start: CodePosition, end: CodePosition, tokens: number) { | ||
super( | ||
{ | ||
firstFile: fileName, | ||
secondFile: fileName, | ||
startInFirst: start, | ||
endInFirst: end, | ||
startInSecond: start, | ||
endInSecond: end, | ||
colorIndex: 'base', | ||
tokens | ||
}, | ||
1 | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
report-viewer/src/model/factories/BaseCodeReportFactory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import slash from 'slash' | ||
import { BaseCodeMatch } from '../BaseCodeReport' | ||
import { BaseFactory } from './BaseFactory' | ||
import type { CodePosition } from '../Match' | ||
|
||
export class BaseCodeReportFactory extends BaseFactory { | ||
private static readonly basePath = 'basecode' | ||
|
||
public static async getReport(submissionId: string): Promise<BaseCodeMatch[]> { | ||
const a = await this.extractReport( | ||
JSON.parse(await this.getFile(slash(`${this.basePath}/${submissionId}.json`))) | ||
) | ||
return a | ||
} | ||
|
||
private static async extractReport(json: Record<string, unknown>[]): Promise<BaseCodeMatch[]> { | ||
return json.map((match: Record<string, unknown>) => { | ||
return new BaseCodeMatch( | ||
match['file_name'] as string, | ||
match['start'] as CodePosition, | ||
match['end'] as CodePosition, | ||
match['tokens'] as number | ||
) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.