blob: 861b4effe7d0b44d0f7178591a8cdb737a700d07 [file] [log] [blame]
/*******************************************************************************
* <copyright>
*
* Copyright (c) 2013, 2013 SAP AG.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* SAP AG - initial API, implementation and documentation
*
* </copyright>
*
*******************************************************************************/
package org.eclipse.fmc;
import static org.eclipse.fmc.blockdiagram.editor.diagram.BlockDiagramFeatureProvider.CREATE_FEATURE_AGENT;
import static org.eclipse.fmc.blockdiagram.editor.diagram.BlockDiagramFeatureProvider.CREATE_FEATURE_HUMAN_AGENT;
import static org.eclipse.fmc.blockdiagram.editor.diagram.BlockDiagramFeatureProvider.CREATE_FEATURE_STORAGE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.fmc.blockdiagram.editor.algorithm.node.AgentAlgorithm;
import org.eclipse.fmc.blockdiagram.editor.algorithm.node.HumanAgentAlgorithm;
import org.eclipse.fmc.blockdiagram.editor.algorithm.node.LAgentAlgorithm;
import org.eclipse.fmc.blockdiagram.editor.algorithm.node.LStorageAlgorithm;
import org.eclipse.fmc.blockdiagram.editor.algorithm.node.StorageAlgorithm;
import org.eclipse.fmc.blockdiagram.editor.algorithm.node.FMCNodeAlgorithm;
import org.eclipse.fmc.blockdiagram.editor.algorithm.node.FMCNodeAlgorithmFactory;
import org.eclipse.fmc.blockdiagram.editor.algorithm.node.UAgentAlgorithm;
import org.eclipse.fmc.blockdiagram.editor.algorithm.node.UStorageAlgorithm;
import org.eclipse.fmc.blockdiagram.editor.property.PropertyActions;
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
import org.eclipse.graphiti.mm.pictograms.Shape;
import org.junit.Test;
public class ShapeTransformationsTest extends FMCTestCase {
private static final int STYLE_RECT = 0;
private static final int STYLE_L = 1;
private static final int STYLE_U = 2;
private static final int WIDTH = 200;
private static final int HEIGHT = 120;
// private FMCTypeChecker helper = FMCTypeHelperFactory.getInstance();
private FMCNodeAlgorithmFactory factory = FMCNodeAlgorithmFactory
.getInstance();
private PropertyActions actions;
private PropertyActions getPropertyActions() {
if (actions == null)
actions = new PropertyActions(getDiagram(), getFeatureProvider(),
getEditingDomain());
return actions;
}
@Test
public void testTransformRectStorageToLandUPlusMultiInstances() {
ContainerShape storage = (ContainerShape) createShape(
CREATE_FEATURE_STORAGE, WIDTH, HEIGHT, 20, 30);
FMCNodeAlgorithm nodeAlgo = factory.getShape(storage);
assertTrue(nodeAlgo instanceof StorageAlgorithm);
StorageAlgorithm storageAlgo = (StorageAlgorithm) nodeAlgo;
assertFalse(storageAlgo.isMultipleInstances(storage));
assertSize(storage, WIDTH, HEIGHT);
assertPosition(storage, 20, 30);
checkDiagramState();
assertFalse(storageAlgo.isMultipleInstances(storage));
turnStorageIntoL(storage);
assertFalse(storageAlgo.isMultipleInstances(storage));
turnStorageIntoU(storage);
assertFalse(storageAlgo.isMultipleInstances(storage));
turnStorageIntoL(storage);
assertFalse(storageAlgo.isMultipleInstances(storage));
turnStorageIntoRect(storage);
assertFalse(storageAlgo.isMultipleInstances(storage));
turnStorageIntoU(storage);
assertFalse(storageAlgo.isMultipleInstances(storage));
turnStorageIntoRect(storage);
assertFalse(storageAlgo.isMultipleInstances(storage));
turnStorageIntoU(storage);
assertFalse(storageAlgo.isMultipleInstances(storage));
turnStorageIntoL(storage);
assertFalse(storageAlgo.isMultipleInstances(storage));
turnStorageIntoU(storage);
assertFalse(storageAlgo.isMultipleInstances(storage));
turnStorageIntoRect(storage);
assertFalse(storageAlgo.isMultipleInstances(storage));
// Now again but with multiInstance
setMultipleInstances(factory.getShape(storage), storage, true);
assertTrue(storageAlgo.isMultipleInstances(storage));
turnStorageIntoL(storage);
assertTrue(storageAlgo.isMultipleInstances(storage));
turnStorageIntoU(storage);
assertTrue(storageAlgo.isMultipleInstances(storage));
turnStorageIntoL(storage);
assertTrue(storageAlgo.isMultipleInstances(storage));
turnStorageIntoRect(storage);
assertTrue(storageAlgo.isMultipleInstances(storage));
turnStorageIntoU(storage);
assertTrue(storageAlgo.isMultipleInstances(storage));
turnStorageIntoRect(storage);
assertTrue(storageAlgo.isMultipleInstances(storage));
turnStorageIntoU(storage);
assertTrue(storageAlgo.isMultipleInstances(storage));
turnStorageIntoL(storage);
assertTrue(storageAlgo.isMultipleInstances(storage));
turnStorageIntoU(storage);
assertTrue(storageAlgo.isMultipleInstances(storage));
turnStorageIntoRect(storage);
}
@Test
public void testTransformRectAgentToLandUPlusMultiInstances() {
ContainerShape agent = (ContainerShape) createShape(
CREATE_FEATURE_AGENT, WIDTH, HEIGHT, 20, 30);
FMCNodeAlgorithm nodeAlgo = factory.getShape(agent);
assertTrue(nodeAlgo instanceof AgentAlgorithm);
AgentAlgorithm agentAlgo = (AgentAlgorithm) nodeAlgo;
assertFalse(agentAlgo.isMultipleInstances(agent));
expectHumanAgent(agent, false);
assertSize(agent, WIDTH, HEIGHT);
assertPosition(agent, 20, 30);
checkDiagramState();
assertFalse(agentAlgo.isMultipleInstances(agent));
turnAgentIntoL(agent);
assertFalse(agentAlgo.isMultipleInstances(agent));
turnAgentIntoU(agent);
assertFalse(agentAlgo.isMultipleInstances(agent));
turnAgentIntoL(agent);
assertFalse(agentAlgo.isMultipleInstances(agent));
turnAgentIntoRect(agent);
assertFalse(agentAlgo.isMultipleInstances(agent));
turnAgentIntoU(agent);
assertFalse(agentAlgo.isMultipleInstances(agent));
turnAgentIntoRect(agent);
assertFalse(agentAlgo.isMultipleInstances(agent));
turnAgentIntoU(agent);
assertFalse(agentAlgo.isMultipleInstances(agent));
turnAgentIntoL(agent);
assertFalse(agentAlgo.isMultipleInstances(agent));
turnAgentIntoU(agent);
assertFalse(agentAlgo.isMultipleInstances(agent));
turnAgentIntoRect(agent);
assertFalse(agentAlgo.isMultipleInstances(agent));
// Now again but with multiInstance
setMultipleInstances(factory.getShape(agent), agent, true);
assertTrue(agentAlgo.isMultipleInstances(agent));
turnAgentIntoL(agent);
assertTrue(agentAlgo.isMultipleInstances(agent));
turnAgentIntoU(agent);
assertTrue(agentAlgo.isMultipleInstances(agent));
turnAgentIntoL(agent);
assertTrue(agentAlgo.isMultipleInstances(agent));
turnAgentIntoRect(agent);
assertTrue(agentAlgo.isMultipleInstances(agent));
turnAgentIntoU(agent);
assertTrue(agentAlgo.isMultipleInstances(agent));
turnAgentIntoRect(agent);
assertTrue(agentAlgo.isMultipleInstances(agent));
turnAgentIntoU(agent);
assertTrue(agentAlgo.isMultipleInstances(agent));
turnAgentIntoL(agent);
assertTrue(agentAlgo.isMultipleInstances(agent));
turnAgentIntoU(agent);
assertTrue(agentAlgo.isMultipleInstances(agent));
turnAgentIntoRect(agent);
}
private void turnAgentIntoRect(ContainerShape agent) {
getPropertyActions().changeShapeStyle(agent, STYLE_RECT);
FMCNodeAlgorithm nodeAlgo = factory.getShape(agent);
assertTrue(nodeAlgo instanceof AgentAlgorithm);
expectHumanAgent(agent, false);
assertSize(agent, WIDTH, HEIGHT);
assertPosition(agent, 20, 30);
checkDiagramState();
}
private void turnAgentIntoL(ContainerShape agent) {
FMCNodeAlgorithm nodeAlgo;
getPropertyActions().changeShapeStyle(agent, STYLE_L);
nodeAlgo = factory.getShape(agent);
assertTrue(nodeAlgo instanceof AgentAlgorithm);
assertTrue(nodeAlgo instanceof LAgentAlgorithm);
expectHumanAgent(agent, false);
assertSize(agent, WIDTH, HEIGHT);
assertPosition(agent, 20, 30);
checkDiagramState();
}
private void turnAgentIntoU(ContainerShape agent) {
FMCNodeAlgorithm nodeAlgo;
getPropertyActions().changeShapeStyle(agent, STYLE_U);
nodeAlgo = factory.getShape(agent);
assertTrue(nodeAlgo instanceof AgentAlgorithm);
assertTrue(nodeAlgo instanceof UAgentAlgorithm);
expectHumanAgent(agent, false);
assertSize(agent, WIDTH, HEIGHT);
assertPosition(agent, 20, 30);
checkDiagramState();
}
private void turnStorageIntoRect(ContainerShape agent) {
getPropertyActions().changeShapeStyle(agent, STYLE_RECT);
FMCNodeAlgorithm nodeAlgo = factory.getShape(agent);
assertTrue(nodeAlgo instanceof StorageAlgorithm);
expectHumanAgent(agent, false);
assertSize(agent, WIDTH, HEIGHT);
assertPosition(agent, 20, 30);
checkDiagramState();
}
private void turnStorageIntoL(ContainerShape agent) {
FMCNodeAlgorithm nodeAlgo;
getPropertyActions().changeShapeStyle(agent, STYLE_L);
nodeAlgo = factory.getShape(agent);
assertTrue(nodeAlgo instanceof StorageAlgorithm);
assertTrue(nodeAlgo instanceof LStorageAlgorithm);
expectHumanAgent(agent, false);
assertSize(agent, WIDTH, HEIGHT);
assertPosition(agent, 20, 30);
checkDiagramState();
}
private void turnStorageIntoU(ContainerShape agent) {
FMCNodeAlgorithm nodeAlgo;
getPropertyActions().changeShapeStyle(agent, STYLE_U);
nodeAlgo = factory.getShape(agent);
assertTrue(nodeAlgo instanceof StorageAlgorithm);
assertTrue(nodeAlgo instanceof UStorageAlgorithm);
expectHumanAgent(agent, false);
assertSize(agent, WIDTH, HEIGHT);
assertPosition(agent, 20, 30);
checkDiagramState();
}
@Test
public void testTransformSingleAgentToHumanToMultipleInstances() {
ContainerShape agent = (ContainerShape) createShape(
CREATE_FEATURE_AGENT, WIDTH, HEIGHT, 20, 30);
FMCNodeAlgorithm nodeAlgo = factory.getShape(agent);
assertTrue(nodeAlgo instanceof AgentAlgorithm);
AgentAlgorithm agentAlgo = (AgentAlgorithm) nodeAlgo;
expectHumanAgent(agent, false);
setMultipleInstances(agentAlgo, agent, true);
assertSize(agent, WIDTH, HEIGHT);
assertPosition(agent, 20, 30);
assertTrue(agentAlgo.isMultipleInstances(agent));
assertNotNull(agentAlgo.getMultiInstanceChild(agent, true));
assertNotNull(agentAlgo.getMultiInstanceChild(agent, false));
// Now turn into Human Agent
turnIntoHumanAgent(agent);
expectHumanAgent(agent, true);
assertTrue(agentAlgo.isMultipleInstances(agent));
checkDiagramState();
// Turn into normal Agent
turnIntoNormalAgent(agent);
expectHumanAgent(agent, false);
assertTrue(agentAlgo.isMultipleInstances(agent));
checkDiagramState();
// Turn into Human Agent
turnIntoHumanAgent(agent);
expectHumanAgent(agent, true);
assertTrue(agentAlgo.isMultipleInstances(agent));
checkDiagramState();
// Turn into single Human Agent
setMultipleInstances(agentAlgo, agent, false);
expectHumanAgent(agent, true);
assertFalse(agentAlgo.isMultipleInstances(agent));
checkDiagramState();
// Turn into single normal Agent
turnIntoNormalAgent(agent);
expectHumanAgent(agent, false);
assertFalse(agentAlgo.isMultipleInstances(agent));
checkDiagramState();
// Turn into single Human Agent
turnIntoHumanAgent(agent);
expectHumanAgent(agent, true);
assertFalse(agentAlgo.isMultipleInstances(agent));
checkDiagramState();
// Turn into multiple Human Agent
setMultipleInstances(agentAlgo, agent, true);
expectHumanAgent(agent, true);
assertTrue(agentAlgo.isMultipleInstances(agent));
checkDiagramState();
// Turn into single Human Agent
setMultipleInstances(agentAlgo, agent, false);
expectHumanAgent(agent, true);
assertFalse(agentAlgo.isMultipleInstances(agent));
}
private void expectHumanAgent(ContainerShape shape, boolean human) {
if (human) {
assertTrue(factory.getHumanAgent().hasHumanFigure(shape));
assertEquals(1, countHumanFigures(shape));
} else {
assertFalse(factory.getHumanAgent().hasHumanFigure(shape));
assertEquals(0, countHumanFigures(shape));
}
}
@Test
public void testTransformNormalAgentToHumanAgent() {
ContainerShape agent = (ContainerShape) createShape(
CREATE_FEATURE_HUMAN_AGENT, WIDTH, HEIGHT, 20, 30);
FMCNodeAlgorithm nodeAlgo = factory.getShape(agent);
assertTrue(nodeAlgo instanceof HumanAgentAlgorithm);
HumanAgentAlgorithm hagentAlgo = (HumanAgentAlgorithm) nodeAlgo;
turnIntoNormalAgent(agent);
expectHumanAgent(agent, false);
assertFalse(hagentAlgo.isMultipleInstances(agent));
checkDiagramState();
nodeAlgo = factory.getShape(agent);
assertTrue(nodeAlgo instanceof AgentAlgorithm);
AgentAlgorithm agentAlgo = (HumanAgentAlgorithm) nodeAlgo;
checkDiagramState();
turnIntoHumanAgent(agent);
expectHumanAgent(agent, true);
assertFalse(hagentAlgo.isMultipleInstances(agent));
checkDiagramState();
setMultipleInstances(agentAlgo, agent, true);
expectHumanAgent(agent, true);
assertTrue(hagentAlgo.isMultipleInstances(agent));
checkDiagramState();
setMultipleInstances(hagentAlgo, agent, false);
expectHumanAgent(agent, true);
assertFalse(hagentAlgo.isMultipleInstances(agent));
}
public int countHumanFigures(ContainerShape element) {
int counter = 0;
if (element instanceof ContainerShape) {
ContainerShape container = (ContainerShape) element;
for (Shape shape : new ArrayList<Shape>(container.getChildren())) {
if (shape instanceof ContainerShape
&& shape.getGraphicsAlgorithm()
.getGraphicsAlgorithmChildren().size() == 5) {
counter++;
} else if (shape instanceof ContainerShape) {
counter += countHumanFigures((ContainerShape) shape);
}
}
;
}
// ContainerShape multiInstanceChild = (ContainerShape)
// factory.getHumanAgent().getMultiInstanceChild(element, false);
// if(element != null){
// for (Shape shape : new ArrayList<Shape>(element.getChildren())) {
// if(shape instanceof ContainerShape &&
// ((ContainerShape) shape).getChildren().size() == 5){
// counter++;
// }
// };
//
// }
return counter;
}
private void turnIntoHumanAgent(final ContainerShape shape) {
getEditingDomain().getCommandStack().execute(
new RecordingCommand(getEditingDomain()) {
@Override
protected void doExecute() {
if (isWriteTransaction(getEditingDomain())) {
factory.getHumanAgent().addHumanFigure(
getDiagram(), shape,
shape.getGraphicsAlgorithm().getWidth());
}
}
});
}
private void turnIntoNormalAgent(final ContainerShape shape) {
getEditingDomain().getCommandStack().execute(
new RecordingCommand(getEditingDomain()) {
@Override
protected void doExecute() {
if (isWriteTransaction(getEditingDomain())) {
factory.getHumanAgent().removeHumanFigure(shape);
}
}
});
}
private void setMultipleInstances(final FMCNodeAlgorithm algo,
final ContainerShape shape, final boolean multi) {
getEditingDomain().getCommandStack().execute(
new RecordingCommand(getEditingDomain()) {
@Override
protected void doExecute() {
if (isWriteTransaction(getEditingDomain())) {
algo.setMultipleInstances(shape,
getFeatureProvider(), multi);
}
}
});
}
/*
* private void refreshOldAnchors(Shape shape, GraphicsAlgorithm algorithm,
* Set<BoxRelativeAnchor> stillUsedAnchors){ for (BoxRelativeAnchor anchor :
* stillUsedAnchors) { anchor.setParent(shape);
* shape.getAnchors().add(anchor);
* anchor.setReferencedGraphicsAlgorithm(algorithm); } }
*/
}