use of org.palladiosimulator.pcm.usagemodel.Branch in project iobserve-analysis by research-iobserve.
the class LoopWithinBranchReference method getModel.
/**
* It creates a reference usage model that contains loops within branches. Accordingly, user
* sessions whose call sequences differ from each other at the positions of the branches and
* that contain iterated call sequences are created.(RQ-1.6)
*
* @param referenceUsageModelFileName
* file name of the reference model to store its result
* @param repositoryLookupModel
* repository lookup model
* @param correspondenceModel
* correspondence model
*
* @return a reference model and corresponding user sessions
* @throws IOException
* on error
*/
public static ReferenceElements getModel(final String referenceUsageModelFileName, final RepositoryLookupModelProvider repositoryLookupModel, final ICorrespondence correspondenceModel) throws IOException {
// Create a random number of user sessions and random model element parameters. The user
// sessions' behavior will be created according to the reference usage model and
// subsequently the user sessions are used to create a usage model. The created usage model
// is matched against the reference usage model. The minimum number of user sessions is set
// dependently from the random number of branch transitions, because it must be ensured that
// each branch transition is represented within the user sessions.
final int numberOfBranchTransitions = TestHelper.getRandomInteger(3, 2);
final int numberOfConcurrentUsers = TestHelper.getRandomInteger(30, 10 * numberOfBranchTransitions);
final int lengthOfBranchSequence = TestHelper.getRandomInteger(2, 1);
final int countOfLoop = TestHelper.getRandomInteger(3, 2);
final EntryCallSequenceModel entryCallSequenceModel = new EntryCallSequenceModel(TestHelper.getUserSessions(numberOfConcurrentUsers));
final ReferenceElements referenceElements = new ReferenceElements();
// In the following the reference usage model is created
final UsageModel usageModel = UsageModelFactory.createUsageModel();
final UsageScenario usageScenario = UsageModelFactory.createUsageScenario("", usageModel);
final ScenarioBehaviour scenarioBehaviour = usageScenario.getScenarioBehaviour_UsageScenario();
// lastAction = start;
final Branch branch = LoopWithinBranchReference.createBranch(repositoryLookupModel, scenarioBehaviour, correspondenceModel, numberOfBranchTransitions, lengthOfBranchSequence, countOfLoop);
// According to the reference usage model user sessions are created that exactly represent
// the user behavior of the reference usage model. The entry and exit times enable that the
// calls within the user sessions are ordered according to the reference usage model. The
// branch transition counter ensures that each branch transition is represnted within the
// user sessions
final List<Integer> branchTransitionCounter = new ArrayList<>();
boolean areAllBranchesVisited = true;
do {
for (int i = 0; i < branch.getBranchTransitions_Branch().size(); i++) {
branchTransitionCounter.add(i, 0);
}
int entryTime = 1;
for (int i = 0; i < entryCallSequenceModel.getUserSessions().size(); i++) {
entryTime = 1;
// Each user session represents one of the branch transitions
final int branchDecisioner = TestHelper.getRandomInteger(numberOfBranchTransitions - 1, 0);
if (branchDecisioner == 0) {
entryTime = LoopWithinBranchReference.createLoop(branchTransitionCounter, entryTime, 0, 1, entryCallSequenceModel, lengthOfBranchSequence, countOfLoop, i);
} else if (branchDecisioner == 1) {
entryTime = LoopWithinBranchReference.createLoop(branchTransitionCounter, entryTime, 1, 2, entryCallSequenceModel, lengthOfBranchSequence, countOfLoop, i);
} else if (branchDecisioner == 2) {
entryTime = LoopWithinBranchReference.createLoop(branchTransitionCounter, entryTime, 2, 0, entryCallSequenceModel, lengthOfBranchSequence, countOfLoop, i);
}
}
// It is checked whether all branch transitions are represented within the user sessions
for (int i = 0; i < branchTransitionCounter.size(); i++) {
if (branchTransitionCounter.get(i) == 0) {
areAllBranchesVisited = false;
break;
}
}
} while (!areAllBranchesVisited);
// Sets the likelihoods of the branch transitions according to the created user sessions
for (int i = 0; i < branch.getBranchTransitions_Branch().size(); i++) {
branch.getBranchTransitions_Branch().get(i).setBranchProbability((double) branchTransitionCounter.get(i) / (double) numberOfConcurrentUsers);
}
// Saves the reference usage model and sets the usage model and the EntryCallSequenceModel
// as the reference elements. Our approach is now executed with the EntryCallSequenceModel
// and the resulting usage model can be matched against the reference usage model
TestHelper.saveModel(usageModel, referenceUsageModelFileName);
referenceElements.setEntryCallSequenceModel(entryCallSequenceModel);
referenceElements.setUsageModel(usageModel);
return referenceElements;
}
use of org.palladiosimulator.pcm.usagemodel.Branch in project iobserve-analysis by research-iobserve.
the class LoopWithinBranchReference method createBranch.
/**
* Creates a branch and branch transitions according to the random countOfBranchTransitions.
*
* @param scenarioBehaviour
* @param repositoryLookupModel
* @param correspondenceModel
* @param numberOfBranchTransitions
* @param lengthOfBranchSequence
* @param countOfLoop
* @return
*/
private static Branch createBranch(final RepositoryLookupModelProvider repositoryLookupModel, final ScenarioBehaviour scenarioBehaviour, final ICorrespondence correspondenceModel, final int numberOfBranchTransitions, final int lengthOfBranchSequence, final int countOfLoop) {
final Start start = UsageModelFactory.createAddStartAction("", scenarioBehaviour);
final Branch branch = UsageModelFactory.createBranch("", scenarioBehaviour);
final Stop stop = UsageModelFactory.createAddStopAction("", scenarioBehaviour);
UsageModelFactory.connect(start, branch);
UsageModelFactory.connect(branch, stop);
AbstractUserAction lastAction = start;
// For each branch transition its calls are added to the branch transition
for (int i = 0; i < numberOfBranchTransitions; i++) {
final BranchTransition branchTransition = UsageModelFactory.createBranchTransition(branch);
final ScenarioBehaviour branchTransitionBehaviour = branchTransition.getBranchedBehaviour_BranchTransition();
final Start startBranchTransition = UsageModelFactory.createStart("");
UsageModelFactory.addUserAction(branchTransitionBehaviour, startBranchTransition);
final Stop stopBranchTransition = UsageModelFactory.createStop("");
UsageModelFactory.addUserAction(branchTransitionBehaviour, stopBranchTransition);
lastAction = startBranchTransition;
if (i >= 0 && i < 3) {
final Optional<Correspondent> optionCorrespondent = correspondenceModel.getCorrespondent(ReferenceUsageModelBuilder.CLASS_SIGNATURE[i], ReferenceUsageModelBuilder.OPERATION_SIGNATURE[i]);
if (optionCorrespondent.isPresent()) {
final Correspondent correspondent = optionCorrespondent.get();
final EntryLevelSystemCall entryLevelSystemCall = UsageModelFactory.createEntryLevelSystemCall(repositoryLookupModel, correspondent);
UsageModelFactory.addUserAction(branchTransitionBehaviour, entryLevelSystemCall);
UsageModelFactory.connect(lastAction, entryLevelSystemCall);
lastAction = entryLevelSystemCall;
}
} else {
throw new IllegalArgumentException("Illegal value of model element parameter");
}
if (lengthOfBranchSequence == 2) {
final Optional<Correspondent> optionCorrespondent = correspondenceModel.getCorrespondent(ReferenceUsageModelBuilder.CLASS_SIGNATURE[4], ReferenceUsageModelBuilder.OPERATION_SIGNATURE[4]);
if (optionCorrespondent.isPresent()) {
final Correspondent correspondent = optionCorrespondent.get();
final EntryLevelSystemCall entryLevelSystemCall = UsageModelFactory.createEntryLevelSystemCall(repositoryLookupModel, correspondent);
UsageModelFactory.addUserAction(branchTransitionBehaviour, entryLevelSystemCall);
UsageModelFactory.connect(lastAction, entryLevelSystemCall);
lastAction = entryLevelSystemCall;
}
}
// Within the branch transition a loop element is created
final Loop loop = UsageModelFactory.createLoop("", branchTransitionBehaviour);
UsageModelFactory.connect(lastAction, loop);
final PCMRandomVariable pcmLoop2Iteration = CoreFactory.eINSTANCE.createPCMRandomVariable();
pcmLoop2Iteration.setSpecification(String.valueOf(countOfLoop));
loop.setLoopIteration_Loop(pcmLoop2Iteration);
final Start loopStart = UsageModelFactory.createStart("");
UsageModelFactory.addUserAction(loop.getBodyBehaviour_Loop(), loopStart);
final Stop loopStop = UsageModelFactory.createStop("");
UsageModelFactory.addUserAction(loop.getBodyBehaviour_Loop(), loopStop);
lastAction = loopStart;
// The calls that are iterated are added to the loop
final Optional<Correspondent> optionCorrespondent;
switch(i) {
case 0:
optionCorrespondent = correspondenceModel.getCorrespondent(ReferenceUsageModelBuilder.CLASS_SIGNATURE[1], ReferenceUsageModelBuilder.OPERATION_SIGNATURE[1]);
break;
case 1:
optionCorrespondent = correspondenceModel.getCorrespondent(ReferenceUsageModelBuilder.CLASS_SIGNATURE[2], ReferenceUsageModelBuilder.OPERATION_SIGNATURE[2]);
break;
case 2:
optionCorrespondent = correspondenceModel.getCorrespondent(ReferenceUsageModelBuilder.CLASS_SIGNATURE[0], ReferenceUsageModelBuilder.OPERATION_SIGNATURE[0]);
break;
default:
throw new IllegalArgumentException("Illegal value of model element parameter");
}
if (optionCorrespondent.isPresent()) {
final Correspondent correspondent = optionCorrespondent.get();
final EntryLevelSystemCall entryLevelSystemCall = UsageModelFactory.createEntryLevelSystemCall(repositoryLookupModel, correspondent);
UsageModelFactory.addUserAction(loop.getBodyBehaviour_Loop(), entryLevelSystemCall);
UsageModelFactory.connect(lastAction, entryLevelSystemCall);
lastAction = entryLevelSystemCall;
}
UsageModelFactory.connect(lastAction, loopStop);
UsageModelFactory.connect(loop, stopBranchTransition);
}
return branch;
}
use of org.palladiosimulator.pcm.usagemodel.Branch in project iobserve-analysis by research-iobserve.
the class UserBehaviorEvaluation method getModelElements.
/**
* Extracts for a scenario behavior the model elements.
*
* @param scenarioBehaviour
* whose model elements are extracted
* @param modelElements
* the list to that the model elements are added
*/
private static void getModelElements(final ScenarioBehaviour scenarioBehaviour, final List<ModelElement> modelElements) {
final List<AbstractUserAction> actionsOfScenarioBehaviour = scenarioBehaviour.getActions_ScenarioBehaviour();
AbstractUserAction nextActionOfScenarioBehaviour = actionsOfScenarioBehaviour.get(0);
// element
while (nextActionOfScenarioBehaviour != null) {
if (nextActionOfScenarioBehaviour.getClass().equals(StartImpl.class)) {
final ModelElement modelElement = new ModelElement(true, false, false, false, false, "", "", 0);
modelElements.add(modelElement);
} else if (nextActionOfScenarioBehaviour.getClass().equals(StopImpl.class)) {
final ModelElement modelElement = new ModelElement(false, true, false, false, false, "", "", 0);
modelElements.add(modelElement);
} else if (nextActionOfScenarioBehaviour.getClass().equals(EntryLevelSystemCallImpl.class)) {
final EntryLevelSystemCall call = (EntryLevelSystemCall) nextActionOfScenarioBehaviour;
final ModelElement modelElement = new ModelElement(false, false, true, false, false, call.getEntityName(), "", 0);
modelElements.add(modelElement);
} else if (nextActionOfScenarioBehaviour.getClass().equals(LoopImpl.class)) {
final Loop loop = (Loop) nextActionOfScenarioBehaviour;
final ModelElement modelElement = new ModelElement(false, false, false, false, true, "", loop.getLoopIteration_Loop().getSpecification(), 0);
modelElements.add(modelElement);
UserBehaviorEvaluation.getModelElements(loop.getBodyBehaviour_Loop(), modelElements);
} else if (nextActionOfScenarioBehaviour.getClass().equals(BranchImpl.class)) {
final Branch branch = (Branch) nextActionOfScenarioBehaviour;
// Because the branch transitions of the usage models are not always in the same
// order, we order the branch transitions by the entity name of their first
// EntryLevelSystemCall. In doing so, the model elements of each usage model are
// added in the same order independently of the ordering of the usage model. The
// ordering of the branch transitions does not affect the accuracy of an usage model
// and is created randomly within the usage model and can not be influenced.
final List<BranchTransition> branchTransitionsSorted = UserBehaviorEvaluation.sortBranchTransitions(branch.getBranchTransitions_Branch());
for (int i = 0; i < branchTransitionsSorted.size(); i++) {
final ModelElement modelElement = new ModelElement(false, false, false, true, false, "", "", branchTransitionsSorted.get(i).getBranchProbability());
modelElements.add(modelElement);
UserBehaviorEvaluation.getModelElements(branchTransitionsSorted.get(i).getBranchedBehaviour_BranchTransition(), modelElements);
}
}
// Gets the successor model element
nextActionOfScenarioBehaviour = nextActionOfScenarioBehaviour.getSuccessor();
}
}
use of org.palladiosimulator.pcm.usagemodel.Branch in project iobserve-analysis by research-iobserve.
the class TUsageModelToBehaviorModel method findLoopStart.
/**
* Find first {@link EntryLevelSystemCall} element of a {@link Loop} body.
*
* @param loop
* loop
* @return first found {@link EntryLevelSystemCall}, if found
*/
private Optional<EntryCallNode> findLoopStart(final AbstractUserAction action) {
if (action instanceof Stop) {
// LoopStart not found
return Optional.empty();
} else if (action instanceof EntryLevelSystemCall) {
// found loop start
final EntryLevelSystemCall entryLevelSystemCall = (EntryLevelSystemCall) action;
final EntryCallNode entryCallNode = this.createEntryCallNode(entryLevelSystemCall);
return Optional.of(entryCallNode);
} else if (action instanceof Loop) {
// search nested scenario
final Loop loop = (Loop) action;
final List<AbstractUserAction> userActions = loop.getBodyBehaviour_Loop().getActions_ScenarioBehaviour();
if (userActions.size() > 0) {
return this.findLoopStart(userActions.get(0));
} else {
return Optional.empty();
}
} else if (action instanceof Branch) {
// will always found before
return Optional.empty();
} else {
// next action
return this.findLoopStart(action.getSuccessor());
}
}
use of org.palladiosimulator.pcm.usagemodel.Branch in project iobserve-analysis by research-iobserve.
the class TUsageModelToBehaviorModel method traverseBranch.
private Map<EntryCallNode, Double> traverseBranch(final BehaviorModel behaviorModel, final Optional<Map<EntryCallNode, Double>> optPreviousNodes, final Branch branch) {
// assign new probabilities to the nodes
final Map<BranchTransition, Map<EntryCallNode, Double>> transitionMap = new HashMap<>();
for (final BranchTransition transition : branch.getBranchTransitions_Branch()) {
final Map<EntryCallNode, Double> branchMap = new HashMap<>();
optPreviousNodes.ifPresent(previousNodes -> previousNodes.keySet().stream().forEach(node -> branchMap.put(node, transition.getBranchProbability())));
transitionMap.put(transition, branchMap);
}
// traverse all branches and collect the end nodes
final Map<EntryCallNode, Double> endNodes = branch.getBranchTransitions_Branch().stream().map(transition -> this.traverseScenarioBehavior(transition.getBranchedBehaviour_BranchTransition(), behaviorModel, // all
Optional.of(transitionMap.get(transition)))).collect(HashMap::new, Map::putAll, // collect endNodes
Map::putAll);
return endNodes;
}
Aggregations