Commit c1884045 authored by mamunozgil's avatar mamunozgil
Browse files

Refactor for backtracking manifesto

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