Commit bfd6290e authored by Alexander Diewald's avatar Alexander Diewald

Add a migrator for the commonized ExplorationTargets

Issue-Ref: 3365
Issue-Url: https://af3-developer.fortiss.org/issues/3365Signed-off-by: Alexander Diewald's avatarAlexander Diewald <diewald@fortiss.org>
parent 7305126c
......@@ -11,4 +11,13 @@
class="org.fortiss.af3.exploration.smt.model.AF3ExplorationSMTPackage"
genModel="model/model.genmodel"/>
</extension>
<extension
point="org.fortiss.tooling.kernel.migrationProvider">
<migrationProvider
migrationProvider="org.fortiss.af3.exploration.smt.migrate.SMTExplorationTargetMigrator">
<objectClass
objectClass="org.fortiss.af3.exploration.model.project.DSE">
</objectClass>
</migrationProvider>
</extension>
</plugin>
SMTExplorationTargetMigrator.java 843c5e4b8ddb7ddb1ca6f670da6b3d3bb839fe88 YELLOW
/*-------------------------------------------------------------------------+
| Copyright 2020 fortiss GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.af3.exploration.smt.migrate;
import static org.eclipse.emf.ecore.util.EcoreUtil.replace;
import static org.fortiss.af3.exploration.util.ExplorationModelElementFactory.createExplorationConstraint;
import static org.fortiss.af3.exploration.util.ExplorationModelElementFactory.createExplorationObjective;
import static org.fortiss.af3.exploration.util.ExplorationModelElementFactory.createUserDefinedDimension;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.getChildrenWithType;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.getFirstChildWith;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.xml.type.AnyType;
import org.fortiss.af3.exploration.model.ExplorationConstraint;
import org.fortiss.af3.exploration.model.ExplorationObjective;
import org.fortiss.af3.exploration.model.IExplorationTarget;
import org.fortiss.af3.exploration.model.project.DSE;
import org.fortiss.af3.exploration.model.project.RuleSet;
import org.fortiss.af3.exploration.model.solutions.ExplorationResult;
import org.fortiss.af3.exploration.model.solutions.SingleExplorationSolution;
import org.fortiss.af3.exploration.smt.model.CustomDimension;
import org.fortiss.af3.exploration.smt.model.SMTConstraint;
import org.fortiss.af3.exploration.smt.model.SMTObjective;
import org.fortiss.af3.project.model.FileProject;
import org.fortiss.tooling.kernel.extension.IMigrationProvider;
import org.fortiss.tooling.kernel.extension.data.ITopLevelElement;
/**
* Migrator for {@link SMTConstraint}s, {@link SMTObjective}s, and {@link CustomDimension}s to the
* commonized {@link IExplorationTarget}s.
*
* @author diewald
*/
@SuppressWarnings({"deprecation", "javadoc"})
public class SMTExplorationTargetMigrator implements IMigrationProvider {
/** {@inheritDoc} */
@Override
public boolean needMigration(ITopLevelElement modelElement,
Map<EObject, AnyType> unknownFeatures) {
EObject rootObj = modelElement.getRootModelElement();
if(!(rootObj instanceof FileProject)) {
return false;
}
FileProject fp = (FileProject)rootObj;
return getFirstChildWith(fp, c -> c instanceof SMTObjective || c instanceof SMTConstraint ||
c instanceof CustomDimension) != null;
}
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public Map<EObject, AnyType> migrate(ITopLevelElement modelElement,
Map<EObject, AnyType> unknownFeatures) {
EObject rootObj = modelElement.getRootModelElement();
if(!(rootObj instanceof FileProject)) {
return unknownFeatures;
}
FileProject fp = (FileProject)rootObj;
for(DSE dse : getChildrenWithType(fp, DSE.class)) {
Map<IExplorationTarget<?>, IExplorationTarget<?>> oldNewMap = new HashMap<>();
for(SMTObjective objective : getChildrenWithType(dse, SMTObjective.class)) {
ExplorationObjective<Double> newObjective = createExplorationObjective(Double.class,
createUserDefinedDimension(objective.getDimension()),
objective.getExpression(), objective.getName());
replace(objective, newObjective);
oldNewMap.put(objective, newObjective);
}
for(SMTConstraint constraint : getChildrenWithType(dse, SMTConstraint.class)) {
ExplorationConstraint<Boolean> newConstraint = createExplorationConstraint(
Boolean.class, createUserDefinedDimension(constraint.getDimension()),
constraint.getExpression(), constraint.getName(), constraint.isImplicit());
replace(constraint, newConstraint);
oldNewMap.put(constraint, newConstraint);
}
for(RuleSet ruleSet : getChildrenWithType(dse, RuleSet.class)) {
for(IExplorationTarget<?> target : new ArrayList<>(
ruleSet.getExplorationTargets())) {
ruleSet.getExplorationTargets().remove(target);
ruleSet.getExplorationTargets().add(oldNewMap.get(target));
}
}
for(SingleExplorationSolution sES : getChildrenWithType(dse,
SingleExplorationSolution.class)) {
for(IExplorationTarget<?> target : new ArrayList<>(sES.keySet())) {
IExplorationTarget<Object> castedTarget = (IExplorationTarget<Object>)target;
ExplorationResult<Object> result = (ExplorationResult<Object>)sES.get(target);
sES.removeKey(target);
sES.put((IExplorationTarget<Object>)oldNewMap.get(castedTarget), result);
}
}
}
return unknownFeatures;
}
}
Markdown is supported
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