Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.oracle.labs.mlrg.olcut.config.Configurable;
import com.oracle.labs.mlrg.olcut.config.ConfigurationData;
import com.oracle.labs.mlrg.olcut.config.ConfigurationManager;
import com.oracle.labs.mlrg.olcut.config.PropertyException;
import com.oracle.labs.mlrg.olcut.provenance.ConfiguredObjectProvenance;
import com.oracle.labs.mlrg.olcut.provenance.ListProvenance;
import com.oracle.labs.mlrg.olcut.provenance.MapProvenance;
Expand Down Expand Up @@ -176,11 +177,60 @@ public Trainer<T> recoverTrainer() {
// construct the correct component name using ProvenanceUtil.computeName, and
// link a provObject to its corresponding configuration in the cm.
ProvenanceUtil.ProvenanceOrdering ordering = ProvenanceUtil.orderProvenances(this.modelProvenance);
Set<String> usedComponentNames = new java.util.HashSet<>();

for (int i = 0; i < ordering.traversalOrder.size(); i++){
if(ordering.traversalOrder.get(i) instanceof TrainerProvenance trainerProvenance){
String componentName = ProvenanceUtil.computeName(trainerProvenance, i);
Configurable configurableObject = cm.lookup(componentName);
Configurable configurableObject;
try {
configurableObject = cm.lookup(componentName);
usedComponentNames.add(componentName);
} catch (PropertyException e) {
try {
Class<?> trainerClass = Class.forName(trainerProvenance.getClassName());
if (Configurable.class.isAssignableFrom(trainerClass)) {
@SuppressWarnings("unchecked")
Class<? extends Configurable> configurableClass = (Class<? extends Configurable>) trainerClass;
List<String> names = cm.listAll(configurableClass);
// Sort names to ensure deterministic mapping, preferring numeric suffix sort
Collections.sort(names, (a, b) -> {
try {
int lastDashA = a.lastIndexOf('-');
int lastDashB = b.lastIndexOf('-');
if (lastDashA != -1 && lastDashB != -1) {
int valA = Integer.parseInt(a.substring(lastDashA + 1));
int valB = Integer.parseInt(b.substring(lastDashB + 1));
return Integer.compare(valA, valB);
}
} catch (NumberFormatException nfe) {
// ignore
}
return a.compareTo(b);
});

String foundName = null;
for (String name : names) {
if (!usedComponentNames.contains(name)) {
foundName = name;
break;
}
}
if (foundName != null) {
logger.log(Level.WARNING, "Fallback to class-based lookup for component " + componentName + ", found " + foundName);
configurableObject = cm.lookup(foundName);
usedComponentNames.add(foundName);
} else {
throw e;
}
} else {
throw e;
}
} catch (ClassNotFoundException ex) {
throw new IllegalStateException("Failed to load trainer class " + trainerProvenance.getClassName(), ex);
}
}

// Once a Trainer is identified we need to set the invocationCount as identified
// in the provenance. Invocation count is not configurable since it is a provenance value,
// it is an immutable value mapping one-to-one to a single execution.
Expand Down