Skip to content

Commit

Permalink
added support to pass context parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
rsfzi committed May 13, 2024
1 parent 025da12 commit 4d2b2d7
Show file tree
Hide file tree
Showing 8 changed files with 770 additions and 688 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

Expand All @@ -14,6 +16,7 @@
import org.palladiosimulator.pcm.repository.Repository;
import org.palladiosimulator.pcm.repository.RepositoryPackage;
import org.palladiosimulator.simulizar.action.context.ExecutionContext;
import org.palladiosimulator.simulizar.action.core.AdaptationStep;
import org.palladiosimulator.simulizar.action.core.EnactAdaptationStep;
import org.palladiosimulator.simulizar.action.core.GuardedTransition;
import org.palladiosimulator.simulizar.action.core.ResourceDemandingStep;
Expand All @@ -38,14 +41,15 @@
*/
class TransientEffectQVTOExecutor extends AbstractQVTOExecutor {

private static final Logger LOGGER = Logger.getLogger(TransientEffectQVTOExecutor.class);
private static final Logger LOGGER = Logger.getLogger(TransientEffectQVTOExecutor.class);

private static final EPackage MAPPING_EPACKAGE = MappingPackage.Literals.MAPPING.getEPackage();
private static final EPackage REPOSITORY_EPACKAGE = RepositoryPackage.Literals.REPOSITORY.getEPackage();

private final Collection<ModelExtent> currentPureOutParams;

protected TransientEffectQVTOExecutor(ModelTransformationCache transformationCache, QVToModelCache availableModels) {

protected TransientEffectQVTOExecutor(ModelTransformationCache transformationCache,
QVToModelCache availableModels) {
super(transformationCache, Objects.requireNonNull(availableModels));
this.currentPureOutParams = new ArrayList<>();

Expand All @@ -57,21 +61,27 @@ Optional<Mapping> executeControllerCompletion(Repository controllerCompletionRep
Collection<EObject> cachedRepo = this.getModelsByType(REPOSITORY_EPACKAGE);
this.storeModel(controllerCompletionRepository);
URI controllerCompletionUri = URI.createURI(controllerCompletionPath);
QvtoModelTransformation controllerCompletion = this.getTransformationByUri(controllerCompletionUri).get();
boolean result = this.executeTransformation(controllerCompletion, resourceTableManager);
QvtoModelTransformation controllerCompletion = this.getTransformationByUri(controllerCompletionUri)
.get();
Map<String, Object> configParams = Collections.emptyMap();
boolean result = this.executeTransformation(controllerCompletion, resourceTableManager, configParams);
// restore if necessary
cachedRepo.forEach(repoInCache -> this.storeModel(repoInCache));
if (result) {
return this.getModelByType(MAPPING_EPACKAGE).map(obj -> (Mapping) obj);
return this.getModelByType(MAPPING_EPACKAGE)
.map(obj -> (Mapping) obj);
}
return Optional.empty();
}

boolean executeGuardedTransition(GuardedTransition guardedTransition, IResourceTableManager resourceTableManager) {
URI conditionUri = URI.createURI(guardedTransition.getConditionURI());
QvtoModelTransformation condition = this.getTransformationByUri(conditionUri).get();
ExecutionDiagnostic result = this.executeTransformationInternal(Objects.requireNonNull(condition), resourceTableManager);
return handleExecutionResultForGuardedTransition(guardedTransition, result);
URI conditionUri = URI.createURI(guardedTransition.getConditionURI());
QvtoModelTransformation condition = this.getTransformationByUri(conditionUri)
.get();
Map<String, Object> configParams = Collections.emptyMap();
ExecutionDiagnostic result = this.executeTransformationInternal(Objects.requireNonNull(condition),
resourceTableManager, configParams);
return handleExecutionResultForGuardedTransition(guardedTransition, result);
}

private void storeModel(EObject model) {
Expand All @@ -88,10 +98,10 @@ private void prepareTransformation(String transformationUri) {
}

void addTransformationParameters(RoleSet roleSet, ExecutionContext context) {
storeModel(roleSet);
storeModel(context);
storeModel(roleSet);
storeModel(context);
}

void enableForTransformationExecution(EnactAdaptationStep enactAdaptationStep) {
storeModel(Objects.requireNonNull(enactAdaptationStep));
prepareTransformation(enactAdaptationStep.getAdaptationStepURI());
Expand All @@ -107,7 +117,8 @@ void enableForTransformationExecution(StateTransformingStep stateTransformingSte
}

void enableForTransformationExecution(GuardedTransition guardedTransition) {
prepareTransformation(Objects.requireNonNull(guardedTransition).getConditionURI());
prepareTransformation(Objects.requireNonNull(guardedTransition)
.getConditionURI());
}

Optional<QvtoModelTransformation> getTransformationByUri(URI transformationId) {
Expand All @@ -120,35 +131,45 @@ Collection<EObject> getModelsByType(EPackage modelType) {

Optional<EObject> getModelByType(EPackage modelType) {
Collection<EObject> modelsOfType = this.getModelsByType(Objects.requireNonNull(modelType));
return modelsOfType.isEmpty() ? Optional.empty() : Optional.of(modelsOfType.iterator().next());
return modelsOfType.isEmpty() ? Optional.empty()
: Optional.of(modelsOfType.iterator()
.next());
}

@Override
protected boolean handleExecutionResult(ExecutionDiagnostic executionResult) {
boolean result = super.handleExecutionResult(executionResult);
if (result) {
this.currentPureOutParams.stream().map(ModelExtent::getContents).filter(contents -> !contents.isEmpty())
.map(contents -> contents.get(0)).forEach(getAvailableModels()::storeModel);
this.currentPureOutParams.stream()
.map(ModelExtent::getContents)
.filter(contents -> !contents.isEmpty())
.map(contents -> contents.get(0))
.forEach(getAvailableModels()::storeModel);
}
return result;
}

protected boolean handleExecutionResultForGuardedTransition(GuardedTransition guardedTransition, ExecutionDiagnostic executionResult) {
if (executionResult.getCode() == ExecutionDiagnostic.FATAL_ASSERTION) {
LOGGER.info("Guard Condition of \"" + guardedTransition.getEntityName() + "\" evaluated to false. The transition is not taken.");
return false;
} else {
return this.handleExecutionResult(executionResult);
}


protected boolean handleExecutionResultForGuardedTransition(GuardedTransition guardedTransition,
ExecutionDiagnostic executionResult) {
if (executionResult.getCode() == ExecutionDiagnostic.FATAL_ASSERTION) {
LOGGER.info("Guard Condition of \"" + guardedTransition.getEntityName()
+ "\" evaluated to false. The transition is not taken.");
return false;
} else {
return this.handleExecutionResult(executionResult);
}

}

@Override
protected ModelExtent[] setupModelExtents(QvtoModelTransformation transformation) {
this.currentPureOutParams.clear();
ModelExtent[] result = super.setupModelExtents(transformation);
transformation.getPureOutParameters().stream().mapToInt(TransformationParameterInformation::getParameterIndex)
.mapToObj(index -> result[index]).forEach(this.currentPureOutParams::add);
transformation.getPureOutParameters()
.stream()
.mapToInt(TransformationParameterInformation::getParameterIndex)
.mapToObj(index -> result[index])
.forEach(this.currentPureOutParams::add);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.palladiosimulator.simulizar.reconfiguration.henshin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.henshin.interpreter.EGraph;
import org.eclipse.emf.henshin.interpreter.Engine;
Expand All @@ -23,109 +24,119 @@

public class HenshinReconfigurator extends AbstractReconfigurator {

/**
* Henshin reconfigurator default constructor.
*/
public HenshinReconfigurator() {
super();
}

/**
* This class' internal LOGGER.
*/
private static final Logger LOGGER = Logger.getLogger(HenshinReconfigurator.class);

@Override
public void setConfiguration(final SimuLizarWorkflowConfiguration configuration) {
this.configuration = configuration;
}

/**
* @param app
* @param resourceSet
* @param module
* @param saveResult
*/
private boolean executeReconfiguration(UnitApplication app, Module module) {
// Load the measurement model into an EGraph
LOGGER.info("Called Henshin reconfiguration engine.");
EGraph graph = new EGraphImpl(this.pcmPartitionManager.getGlobalPCMModel().getAllocation());

app.setEGraph(graph);

// Set parameters for rule and execute...
app.setUnit(module.getUnit("execute"));

if (app.execute(null)) {
LOGGER.debug("Successfully executed Henshin rule.");
return true;
} else {
LOGGER.debug("Executing Henshin rule failed.");
return false;

}

}

/**
* @param app
* @param resourceSet
* @param module
*/
private boolean analyzeReconfiguration(UnitApplication app, Module module) {

// Load the example model into an EGraph:
RuntimeMeasurementModel rmModel = this.pcmPartitionManager.findModel(RuntimeMeasurementPackage.eINSTANCE.getRuntimeMeasurementModel());
EGraph graph = new EGraphImpl(rmModel);
app.setEGraph(graph);

// Execute analyze step of rule
app.setUnit(module.getUnit("analyze"));

if (app.execute(null)) {
LOGGER.debug("Found matching Henshin rule.");
return true;
} else {
LOGGER.debug("No matching Henshin rule found.");
return false;
}
}

@Override
public boolean runCheck(EList<? extends ModelTransformation<? extends Object>> checks, EObject monitoredElement, IResourceTableManager resourceTableManager) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean runExecute(EList<? extends ModelTransformation<? extends Object>> actions,
EObject monitoredElement, IResourceTableManager resourceTableManager) {
List<HenshinModelTransformation> transformations = new ArrayList<HenshinModelTransformation>();
LOGGER.info("Executing Story Diagram Model Transformation.");
for (ModelTransformation<? extends Object> action : actions) {
try {
if (action instanceof HenshinModelTransformation) {
HenshinModelTransformation henshinModelTransformation = (HenshinModelTransformation) action;
transformations.add(henshinModelTransformation);
}
} catch (ClassCastException e) {
LOGGER.info("Not a Storydiagram model transformation.");
}
}

return executeTransformations(transformations);
}

private boolean executeTransformations(List<HenshinModelTransformation> transformations) {
Engine engine = new EngineImpl();
UnitApplication app = new UnitApplicationImpl(engine);
boolean result = false;
for (final HenshinModelTransformation transformation : transformations) {
if (analyzeReconfiguration(app, transformation.getModelTransformation())) {
result |= executeReconfiguration(app, transformation.getModelTransformation());
}
}
return result;
}
/**
* Henshin reconfigurator default constructor.
*/
public HenshinReconfigurator() {
super();
}

/**
* This class' internal LOGGER.
*/
private static final Logger LOGGER = Logger.getLogger(HenshinReconfigurator.class);

@Override
public void setConfiguration(final SimuLizarWorkflowConfiguration configuration) {
this.configuration = configuration;
}

/**
* @param app
* @param resourceSet
* @param module
* @param saveResult
*/
private boolean executeReconfiguration(UnitApplication app, Module module) {
// Load the measurement model into an EGraph
LOGGER.info("Called Henshin reconfiguration engine.");
EGraph graph = new EGraphImpl(this.pcmPartitionManager.getGlobalPCMModel()
.getAllocation());

app.setEGraph(graph);

// Set parameters for rule and execute...
app.setUnit(module.getUnit("execute"));

if (app.execute(null)) {
LOGGER.debug("Successfully executed Henshin rule.");
return true;
} else {
LOGGER.debug("Executing Henshin rule failed.");
return false;

}

}

/**
* @param app
* @param resourceSet
* @param module
*/
private boolean analyzeReconfiguration(UnitApplication app, Module module) {

// Load the example model into an EGraph:
RuntimeMeasurementModel rmModel = this.pcmPartitionManager
.findModel(RuntimeMeasurementPackage.eINSTANCE.getRuntimeMeasurementModel());
EGraph graph = new EGraphImpl(rmModel);
app.setEGraph(graph);

// Execute analyze step of rule
app.setUnit(module.getUnit("analyze"));

if (app.execute(null)) {
LOGGER.debug("Found matching Henshin rule.");
return true;
} else {
LOGGER.debug("No matching Henshin rule found.");
return false;
}
}

@Override
public boolean runCheck(List<? extends ModelTransformation<? extends Object>> checks, EObject monitoredElement,
IResourceTableManager resourceTableManager) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean runExecute(List<? extends ModelTransformation<? extends Object>> actions, EObject monitoredElement,
IResourceTableManager resourceTableManager) {
Map<String, Object> configParams = Collections.emptyMap();
return runExecute(actions, monitoredElement, resourceTableManager, configParams);
}

@Override
public boolean runExecute(List<? extends ModelTransformation<? extends Object>> actions, EObject monitoredElement,
IResourceTableManager resourceTableManager, Map<String, Object> configParams) {
List<HenshinModelTransformation> transformations = new ArrayList<>();
LOGGER.info("Executing Story Diagram Model Transformation.");
for (ModelTransformation<? extends Object> action : actions) {
try {
if (action instanceof HenshinModelTransformation) {
HenshinModelTransformation henshinModelTransformation = (HenshinModelTransformation) action;
transformations.add(henshinModelTransformation);
}
} catch (ClassCastException e) {
LOGGER.info("Not a Storydiagram model transformation.");
}
}

return executeTransformations(transformations);
}

private boolean executeTransformations(List<HenshinModelTransformation> transformations) {
Engine engine = new EngineImpl();
UnitApplication app = new UnitApplicationImpl(engine);
boolean result = false;
for (final HenshinModelTransformation transformation : transformations) {
if (analyzeReconfiguration(app, transformation.getModelTransformation())) {
result |= executeReconfiguration(app, transformation.getModelTransformation());
}
}
return result;
}

}
Loading

0 comments on commit 4d2b2d7

Please sign in to comment.