Commit 73c10129 authored by mamunozgil's avatar mamunozgil
Browse files

Revert "Refactor for backtracking manifesto"

This reverts commit 375b37bf.
parent c1884045
This commit is part of merge request !1. Comments created here will be created in the context of that merge request.
Showing with 308 additions and 321 deletions
+308 -321
...@@ -3,188 +3,134 @@ package de.hftstuttgart.dtabackend.utils; ...@@ -3,188 +3,134 @@ package de.hftstuttgart.dtabackend.utils;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.net.MalformedURLException;
import java.nio.file.Path; import java.nio.file.Path;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import com.fasterxml.jackson.core.exc.StreamReadException;
import com.fasterxml.jackson.databind.DatabindException;
import de.hftstuttgart.dtabackend.models.ExerciseCompetencyProfile; import de.hftstuttgart.dtabackend.models.ExerciseCompetencyProfile;
import de.hftstuttgart.dtabackend.models.ICompetencyProfile; import de.hftstuttgart.dtabackend.models.ICompetencyProfile;
import de.hftstuttgart.dtabackend.models.Result; import de.hftstuttgart.dtabackend.models.Result;
import de.hftstuttgart.dtabackend.models.ResultSummary; import de.hftstuttgart.dtabackend.models.ResultSummary;
import de.hftstuttgart.dtabackend.models.TestCompetencyProfile; import de.hftstuttgart.dtabackend.models.TestCompetencyProfile;
public class CompetencyAssessmentUtil { import java.io.FileNotFoundException;
private static final Logger LOG = LogManager.getLogger(CompetencyAssessmentUtil.class);
public static final String TEST_COMPETENCY_MANIFEST_FILE_NAME = "competency-tests.mft";
public static final String EXERCISE_COMPETENCY_MANIFEST_FILE_NAME = "exercise-tests.mft";
/**
* Retrieves the base directory where the test competency manifest is located
* by traversing upwards until it finds the file "exercise-tests.mft".
*
* @param startDir Starting directory path to begin the search
* @return Path of the base directory if found; otherwise, null
*/
public static Path getBaseDirectory(Path startDir) {
Path currentDir = startDir;
while (currentDir != null) {
Path manifestPath = currentDir.resolve(EXERCISE_COMPETENCY_MANIFEST_FILE_NAME);
if (Files.exists(manifestPath)) {
return currentDir;
}
currentDir = currentDir.getParent();
}
LOG.warn("Base directory with " + EXERCISE_COMPETENCY_MANIFEST_FILE_NAME + " not found starting from " + startDir);
return null;
}
/**
* Reads and loads test competency profiles from the base directory.
*
* @param exercisePath Path to the starting exercise directory
* @return List of TestCompetencyProfile instances loaded from the manifest
*/
public static List<TestCompetencyProfile> readTestCompetencyProfiles(Path exercisePath) {
Path baseDir = getBaseDirectory(exercisePath);
if (baseDir == null) {
LOG.error("Unable to locate the base directory for reading test competency profiles.");
return null;
}
return readTestCompetencyProfiles(baseDir, TEST_COMPETENCY_MANIFEST_FILE_NAME);
}
/**
* Reads and loads exercise competency profiles from the specified exercise directory.
*
* @param exercisePath Path to the exercise directory
* @return List of ExerciseCompetencyProfile instances loaded from the manifest
*/
public static List<ExerciseCompetencyProfile> readExerciseCompetencyProfiles(Path exercisePath) {
return readExerciseCompetencyProfiles(exercisePath, EXERCISE_COMPETENCY_MANIFEST_FILE_NAME);
}
private static List<TestCompetencyProfile> readTestCompetencyProfiles(Path baseDir, String fileName) {
List<TestCompetencyProfile> testCompetencyProfiles = new ArrayList<>();
Path manifestPath = baseDir.resolve(fileName);
try (BufferedReader testCompetencyManifest = new BufferedReader(new FileReader(manifestPath.toFile()))) { public class CompetencyAssessmentUtil {
String testEntry = testCompetencyManifest.readLine(); private static final Logger LOG = LogManager.getLogger(CompetencyAssessmentUtil.class);
while (testEntry != null) {
String[] testEntryComponents = testEntry.split(ICompetencyProfile.COMPETENCY_SEPARATOR); public static String TEST_COMPETENCY_MANIFEST_FILE_NAME="competency-tests.mft";
TestCompetencyProfile currentProfile = new TestCompetencyProfile(); public static String EXERCISE_COMPETENCY_MANIFEST_FILE_NAME="exercise-tests.mft";
currentProfile.testPackageName = testEntryComponents[0];
currentProfile.testClassName = testEntryComponents[1]; /*public static void main(String[] args) throws StreamReadException, DatabindException, MalformedURLException, IOException {
currentProfile.testName = testEntryComponents[2]; ResultSummary summary=ExecuteTestUtil.generateResult("1", Path.of(args[0]), Path.of(args[1]));
for (int competencyIndex = 0; competencyIndex < ICompetencyProfile.MAX_COMPETENCY_DIMENSIONS; competencyIndex++) { System.out.println(summary.successfulTestCompetencyProfile);
currentProfile.competencyAssessments[competencyIndex] = Float.valueOf(testEntryComponents[competencyIndex + 3]); }
} */
testCompetencyProfiles.add(currentProfile); public static float[] sumTestCompetencyProfiles(List<TestCompetencyProfile> testCompetencyProfiles) {
testEntry = testCompetencyManifest.readLine(); float[] tcpTotalProfile=new float[ICompetencyProfile.MAX_COMPETENCY_DIMENSIONS];
} for(TestCompetencyProfile currentProfile: testCompetencyProfiles) {
LOG.info("Loaded " + testCompetencyProfiles.size() + " test competency profiles from " + manifestPath); tcpTotalProfile=ICompetencyProfile.competencySum(tcpTotalProfile, currentProfile.competencyAssessments);
} catch (IOException e) { }
LOG.error("Error reading test competency manifest file at " + manifestPath, e); return tcpTotalProfile;
} }
return testCompetencyProfiles; public static float[] sumSuccessfulCompetencyProfiles(List<TestCompetencyProfile> testCompetencyProfiles, ResultSummary resultSummary, boolean success) {
} float[] sumSuccessful=new float[ICompetencyProfile.MAX_COMPETENCY_DIMENSIONS];
for(Result currentResult: resultSummary.results) {
private static List<ExerciseCompetencyProfile> readExerciseCompetencyProfiles(Path exercisePath, String fileName) { boolean isSuccess = Integer.valueOf(currentResult.state).equals(Result.State.SUCCESS.ordinal());
List<ExerciseCompetencyProfile> exerciseCompetencyProfiles = new ArrayList<>(); if (isSuccess == success) {
Path manifestPath = exercisePath.resolve(fileName); TestCompetencyProfile currentProfile=new TestCompetencyProfile();
currentProfile.testPackageName=(currentResult.packageName!=null)?currentResult.packageName:"";
try (BufferedReader exerciseCompetencyManifest = new BufferedReader(new FileReader(manifestPath.toFile()))) { currentProfile.testClassName=(currentResult.className!=null)?currentResult.className:"";
String exerciseEntry = exerciseCompetencyManifest.readLine(); currentProfile.testName=(currentResult.name!=null)?currentResult.name:"";
while (exerciseEntry != null) { int testIndex=testCompetencyProfiles.indexOf(currentProfile);
String[] exerciseEntryComponents = exerciseEntry.split(ExerciseCompetencyProfile.COMPETENCY_SEPARATOR); if(testIndex!=-1) {
ExerciseCompetencyProfile currentProfile = new ExerciseCompetencyProfile(); sumSuccessful=ICompetencyProfile.competencySum(sumSuccessful, testCompetencyProfiles.get(testIndex).competencyAssessments);
currentProfile.exerciseTopicName = exerciseEntryComponents[0]; }
currentProfile.exerciseName = exerciseEntryComponents[1]; }
currentProfile.exerciseURL = exerciseEntryComponents[2]; }
for (int competencyIndex = 0; competencyIndex < ExerciseCompetencyProfile.MAX_COMPETENCY_DIMENSIONS; competencyIndex++) { return sumSuccessful;
currentProfile.competencyAssessments[competencyIndex] = Float.valueOf(exerciseEntryComponents[competencyIndex + 3]); }
}
currentProfile.difficulty = Float.parseFloat(exerciseEntryComponents[19]); public static List<TestCompetencyProfile> readTestCompetencyProfiles(Path testPath, String fileName) {
exerciseCompetencyProfiles.add(currentProfile); List<TestCompetencyProfile> testCompetencyProfiles=new ArrayList<TestCompetencyProfile>();
exerciseEntry = exerciseCompetencyManifest.readLine(); try {
} BufferedReader testCompetencyManifest=new BufferedReader(new FileReader(new File(testPath.toFile(), fileName)));
LOG.info("Loaded " + exerciseCompetencyProfiles.size() + " exercise competency profiles from " + manifestPath); String testEntry=testCompetencyManifest.readLine();
} catch (IOException e) { while(testEntry!=null)
LOG.error("Error reading exercise competency manifest file at " + manifestPath, e); {
} String[] testEntyComponents=testEntry.split(ICompetencyProfile.COMPETENCY_SEPARATOR);
TestCompetencyProfile currentProfile=new TestCompetencyProfile();
return exerciseCompetencyProfiles; currentProfile.testPackageName=testEntyComponents[0];
} currentProfile.testClassName=testEntyComponents[1];
currentProfile.testName=testEntyComponents[2];
/** for(int competencyIndex=0; competencyIndex<ICompetencyProfile.MAX_COMPETENCY_DIMENSIONS; competencyIndex++) {
* Converts an array of floats into a semicolon-separated string. currentProfile.competencyAssessments[competencyIndex]=Float.valueOf(testEntyComponents[competencyIndex+3]);
* }
* @param array Array of float values to pack testCompetencyProfiles.add(currentProfile);
* @return Semicolon-separated string of float values testEntry=testCompetencyManifest.readLine();
*/ }
public static String packFloats(float[] array) { testCompetencyManifest.close();
return IntStream.range(0, array.length) LOG.info("Added "+testCompetencyProfiles.size()+" test competency profiles from test competency manifest. Optional agent functionality enabled.");
.mapToObj(i -> String.valueOf(array[i])) } catch (FileNotFoundException e) {
.collect(Collectors.joining(";")); LOG.info("Test competency manifest file for agent feedback not found. Skipping optional functionality.");
} testCompetencyProfiles=null;
} catch (IOException e) {
/** LOG.info("Test competency manifest file for agent feedback unreadable. Skipping optional functionality.");
* Sums the competency profiles across all test competency profiles. testCompetencyProfiles=null;
* }
* @param testCompetencyProfiles List of test competency profiles return testCompetencyProfiles;
* @return An array representing the sum of competencies }
*/
public static float[] sumTestCompetencyProfiles(List<TestCompetencyProfile> testCompetencyProfiles) { public static List<ExerciseCompetencyProfile> readExerciseCompetencyProfiles(Path exercisePath, String fileName) {
float[] tcpTotalProfile = new float[ICompetencyProfile.MAX_COMPETENCY_DIMENSIONS]; List<ExerciseCompetencyProfile> exerciseCompetencyProfiles = new ArrayList<>();
for (TestCompetencyProfile currentProfile : testCompetencyProfiles) {
tcpTotalProfile = ICompetencyProfile.competencySum(tcpTotalProfile, currentProfile.competencyAssessments); try (BufferedReader exerciseCompetencyManifest = new BufferedReader(new FileReader(new File(exercisePath.toFile(), fileName)))) {
} String exerciseEntry = exerciseCompetencyManifest.readLine();
return tcpTotalProfile;
} while (exerciseEntry != null) {
String[] exerciseEntyComponents = exerciseEntry.split(ExerciseCompetencyProfile.COMPETENCY_SEPARATOR);
/** ExerciseCompetencyProfile currentProfile = new ExerciseCompetencyProfile();
* Sums only the successful test competency profiles, based on the results summary.
* currentProfile.exerciseTopicName = exerciseEntyComponents[0];
* @param testCompetencyProfiles List of test competency profiles currentProfile.exerciseName = exerciseEntyComponents[1];
* @param resultSummary The result summary containing test results currentProfile.exerciseURL = exerciseEntyComponents[2];
* @param success Indicates whether to sum successful (true) or unsuccessful (false) profiles
* @return An array representing the sum of competencies for successful or unsuccessful profiles for (int competencyIndex = 0; competencyIndex < ExerciseCompetencyProfile.MAX_COMPETENCY_DIMENSIONS; competencyIndex++) {
*/ currentProfile.competencyAssessments[competencyIndex] = Float.valueOf(exerciseEntyComponents[competencyIndex+3]);
public static float[] sumSuccessfulCompetencyProfiles(List<TestCompetencyProfile> testCompetencyProfiles, ResultSummary resultSummary, boolean success) { }
float[] sumSuccessful = new float[ICompetencyProfile.MAX_COMPETENCY_DIMENSIONS];
for (Result currentResult : resultSummary.results) { currentProfile.difficulty = Float.parseFloat(exerciseEntyComponents[19]);
boolean isSuccess = Integer.valueOf(currentResult.state).equals(Result.State.SUCCESS.ordinal());
if (isSuccess == success) { exerciseCompetencyProfiles.add(currentProfile);
TestCompetencyProfile currentProfile = new TestCompetencyProfile();
currentProfile.testPackageName = (currentResult.packageName != null) ? currentResult.packageName : ""; exerciseEntry = exerciseCompetencyManifest.readLine();
currentProfile.testClassName = (currentResult.className != null) ? currentResult.className : ""; }
currentProfile.testName = (currentResult.name != null) ? currentResult.name : ""; exerciseCompetencyManifest.close();
int testIndex = testCompetencyProfiles.indexOf(currentProfile); LOG.info("Added " + exerciseCompetencyProfiles.size() + " test competency profiles from exercise competency manifest.");
if (testIndex != -1) { } catch (FileNotFoundException e) {
sumSuccessful = ICompetencyProfile.competencySum(sumSuccessful, testCompetencyProfiles.get(testIndex).competencyAssessments); LOG.info("Exercise competency manifest file not found.");
} } catch (IOException e) {
} LOG.info("Exercise competency manifest file unreadable.");
} }
return sumSuccessful;
} return exerciseCompetencyProfiles;
}
public static String packFloats(float[] array) {
return IntStream.range(0, array.length)
.mapToObj(i -> String.valueOf(array[i]))
.collect(Collectors.joining(";"));
}
/**
* Checks if all elements in the competency profile array are zero (indicating full success).
*
* @param competencyProfile Array of competency values
* @return True if all values are zero; false otherwise
*/
public static boolean isFullSuccess(float[] competencyProfile) {
for (float value : competencyProfile) {
if (value != 0.0f) {
return false;
}
}
return true;
}
} }
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment