ModularMultiTypeMovementModel.java 3.08 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
package de.tud.kom.p2psim.impl.topology.movement.modularosm;

import de.tud.kom.p2psim.api.topology.Topology;
import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.movement.local.RealWorldStreetsMovement;
import de.tud.kom.p2psim.impl.util.Either;
import de.tudarmstadt.maki.simonstrator.api.Binder;

import java.util.HashMap;

/**
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * This class is meant to be used with the RealWorldStreetsMovement
 * if different movement types should be used simultaneously. It acts
 * like the {@link ModularMovementModel}, but each of the {@link SimLocationActuator}s
 * can have a different movement type and the movement route will be calculated according
 * to this type. <BR><BR>
 *
 * USAGE:
 * So basically if you want to use this movement model, all you have to do is
 * (besides specifying it in your config) call the {@link #setMovementType(SimLocationActuator, String)}
 * for each of your components.<BR><BR>
 *
 * NOTE: All the movement types you are using need to be specified in you config for the
 * {@link RealWorldStreetsMovement}. E.g if you are using 'car' and 'foot' movement types,
 * in you config the MovementType for the {@link RealWorldStreetsMovement} should be specified as 'car,foot'.
 * If not done properly there won't be an error, but the movement behaviour will be strange.
 *
 * @author Clemens Krug
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
 */
public class ModularMultiTypeMovementModel extends ModularMovementModel
{
    private HashMap<SimLocationActuator, String> movementTypes;

    public ModularMultiTypeMovementModel()
    {
        super();
        movementTypes = new HashMap<>();
    }


    @Override
    protected void doLocalMovement(SimLocationActuator ms, PositionVector destination)
    {
        assert localMovementStrategy instanceof RealWorldStreetsMovement: "ModularMultiTypeMovementModel can only be used with RealWorldStreetsMovement!";

        Either<PositionVector, Boolean> either;
        if(movementTypes.containsKey(ms)) either =  ((RealWorldStreetsMovement) localMovementStrategy).nextPosition(ms, destination, movementTypes.get(ms));
        else either = localMovementStrategy.nextPosition(ms, destination);

        if (either.hasLeft()) {
            ms.updateCurrentLocation(either.getLeft());
			/*
			 * Check for negative or out of bound coordinates!
			 */
            assert ms.getRealPosition().getX() >= 0.0
                    && ms.getRealPosition().getX() <= Binder
                    .getComponentOrNull(Topology.class)
                    .getWorldDimensions().getX();
            assert ms.getRealPosition().getY() >= 0.0
                    && ms.getRealPosition().getY() <= Binder
                    .getComponentOrNull(Topology.class)
                    .getWorldDimensions().getY();
        } else {
            transition.reachedAttractionPoint(ms);
        }
    }

    public void setMovementType(SimLocationActuator ms, String movementType)
    {
        movementTypes.put(ms, movementType);
    }
73
74
75
76
77

    public String getMovementType(SimLocationActuator ms)
    {
        return movementTypes.get(ms);
    }
78
}