Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • evoal/source/evoal-core
1 result
Show changes
Commits on Source (2)
......@@ -100,6 +100,26 @@ public final class BeanFactory {
return createComponent(type, name, configuration);
}
public static <S extends EvoalComponent<S>, T extends EvoalComponentProvider<S>> S createComponentUsingProvider(final Class<T> type, final Instance configuration) {
Requirements.requireNotNull(type);
Requirements.requireNotNull(configuration);
final String name = new FQNProvider().get(configuration);
Requirements.requireNotNull(name);
log.info("Creating bean for instance of type {} using component provider.", name);
try {
return BeanProvider.getContextualReference(name, false, type)
.create(configuration)
.init(configuration);
} catch (final IllegalStateException | IllegalArgumentException | InitializationException e) {
log.error("Failed to create contextual reference of type '{}' with name '{}'.", type, name);
logInstantiationError(type, e);
throw new RuntimeException("Failed to instanciate component due to an error.", e);
}
}
public static <T extends EvoalComponent<T>> T createComponentForAttribute(final Class<T> type, final Instance configuration, final String attributeName) {
final LanguageHelper helper = BeanFactory.create(LanguageHelper.class);
final Instance child = helper.lookup(configuration, attributeName);
......
package de.evoal.core.api.cdi;
import de.evoal.core.api.utils.InitializationException;
import de.evoal.languages.model.base.Instance;
/**
* Provider of EvoAl components if the components cannot be instantiated directly.
*/
@FunctionalInterface
public interface EvoalComponentProvider<T extends EvoalComponent<T>> {
/**
* Creates a component based on the given configuration.
*
* @param configuration The configuration to use.
*
* @return The component itself.
* @throws InitializationException on errors during initialisation.
*/
public T create(final Instance configuration) throws InitializationException;
}
package de.evoal.core.ea.api.operators;
import de.evoal.core.api.cdi.EvoalComponentProvider;
public interface AltererComponentProvider extends EvoalComponentProvider<AltererComponent> {
}
package de.evoal.core.ea.api.operators;
import de.evoal.core.api.cdi.EvoalComponentProvider;
public interface SelectorComponentProvider extends EvoalComponentProvider<SelectorComponent> {
}
......@@ -8,6 +8,8 @@ import de.evoal.core.api.optimisation.OptimisationValue;
import de.evoal.core.ea.api.codec.CustomCodec;
import de.evoal.core.ea.api.codec.program.Operation;
import de.evoal.core.ea.api.operators.AltererComponent;
import de.evoal.core.ea.api.operators.AltererComponentProvider;
import de.evoal.core.ea.api.operators.SelectorComponent;
import de.evoal.core.ea.main.alterer.internal.MeanCorrelationAlterer;
import de.evoal.core.ea.main.alterer.mutator.SingleBitFlipMutator;
import de.evoal.core.ea.main.alterer.crossover.*;
......@@ -88,8 +90,11 @@ public class AltererFactory {
case "single-node-crossover": return (Alterer<G, OptimisationValue>) createSingleNodeCrossover(config);
}
return BeanFactory.createComponent(AltererComponent.class, config);
try {
return BeanFactory.createComponent(AltererComponent.class, config);
} catch(final RuntimeException e) {
return BeanFactory.createComponentUsingProvider(AltererComponentProvider.class, config);
}
}
private <G extends TreeGene<?, G>> Alterer<G, OptimisationValue> createSingleNodeCrossover(Instance config) {
......
......@@ -4,9 +4,13 @@ import de.evoal.core.api.board.CoreBlackboardEntries;
import de.evoal.core.api.cdi.BeanFactory;
import de.evoal.core.api.cdi.ConfigurationValue;
import de.evoal.core.api.utils.LanguageHelper;
import de.evoal.core.ea.api.operators.AltererComponent;
import de.evoal.core.ea.api.operators.AltererComponentProvider;
import de.evoal.core.ea.api.operators.SelectorComponent;
import de.evoal.core.ea.api.operators.SelectorComponentProvider;
import de.evoal.core.ea.main.comparator.ParetoOptimisationValue;
import de.evoal.languages.model.base.Instance;
import de.evoal.languages.model.dl.util.FQNProvider;
import io.jenetics.*;
import io.jenetics.ext.moea.NSGA2Selector;
import lombok.extern.slf4j.Slf4j;
......@@ -58,7 +62,11 @@ public class SelectorFactory {
case "roulette-wheel-selector": return createRouletteWheelSelector(config);
}
return BeanFactory.createComponent(SelectorComponent.class, config);
try {
return BeanFactory.createComponent(SelectorComponent.class, config);
} catch(final RuntimeException e) {
return BeanFactory.createComponentUsingProvider(SelectorComponentProvider.class, config);
}
}
private <G extends Gene<?, G>, C extends Comparable<? super C>> Selector<G, C> createNSGA2Selector(final Instance config) {
......