Search in sources :

Example 96 with MutablePair

use of org.apache.commons.lang3.tuple.MutablePair in project openflowplugin by opendaylight.

the class BarrierUtil method chainBarrier.

/**
 * Chain a barrier message - regardless of previous result and use given {@link Function} to combine
 * original result and barrier result.
 *
 * @param <T>                type of input future
 * @param input              future to chain barrier to
 * @param nodeRef            target device
 * @param transactionService barrier service
 * @param compositeTransform composite transform
 * @return future holding both results (input and of the barrier)
 */
public static <T> ListenableFuture<RpcResult<T>> chainBarrier(final ListenableFuture<RpcResult<T>> input, final NodeRef nodeRef, final FlowCapableTransactionService transactionService, final Function<Pair<RpcResult<T>, RpcResult<SendBarrierOutput>>, RpcResult<T>> compositeTransform) {
    final MutablePair<RpcResult<T>, RpcResult<SendBarrierOutput>> resultPair = new MutablePair<>();
    // store input result and append barrier
    final ListenableFuture<RpcResult<SendBarrierOutput>> barrierResult = Futures.transformAsync(input, interInput -> {
        resultPair.setLeft(interInput);
        final SendBarrierInput barrierInput = createSendBarrierInput(nodeRef);
        return transactionService.sendBarrier(barrierInput);
    }, MoreExecutors.directExecutor());
    // store barrier result and return initiated pair
    final ListenableFuture<Pair<RpcResult<T>, RpcResult<SendBarrierOutput>>> compositeResult = Futures.transform(barrierResult, input1 -> {
        resultPair.setRight(input1);
        return resultPair;
    }, MoreExecutors.directExecutor());
    // append assembling transform to barrier result
    return Futures.transform(compositeResult, compositeTransform, MoreExecutors.directExecutor());
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) SendBarrierInput(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.SendBarrierInput) SendBarrierOutput(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.SendBarrierOutput) RpcResult(org.opendaylight.yangtools.yang.common.RpcResult) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair)

Example 97 with MutablePair

use of org.apache.commons.lang3.tuple.MutablePair in project BWAPI4J by OpenBW.

the class Graph method createRawFrontierByAreaPairMap.

// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
// 2) Dispatch the global raw frontier between all the relevant pairs of areas:
// ----------------------------------------------------------------------
private java.util.Map<MutablePair<AreaId, AreaId>, List<WalkPosition>> createRawFrontierByAreaPairMap(final List<MutablePair<MutablePair<AreaId, AreaId>, WalkPosition>> rawFrontier) {
    final java.util.Map<MutablePair<AreaId, AreaId>, List<WalkPosition>> rawFrontierByAreaPair = new HashMap<>();
    for (final MutablePair<MutablePair<AreaId, AreaId>, WalkPosition> raw : rawFrontier) {
        int a = raw.getLeft().getLeft().intValue();
        int b = raw.getLeft().getRight().intValue();
        if (a > b) {
            final int a_tmp = a;
            a = b;
            b = a_tmp;
        }
        // bwem_assert(a <= b);
        if (!(a <= b)) {
            throw new IllegalStateException();
        }
        // bwem_assert((a >= 1) && (b <= areasCount()));
        if (!((a >= 1) && (b <= getAreaCount()))) {
            throw new IllegalStateException();
        }
        final MutablePair<AreaId, AreaId> key = new MutablePair<>(new AreaId(a), new AreaId(b));
        rawFrontierByAreaPair.computeIfAbsent(key, mp -> new ArrayList<>()).add(raw.getRight());
    }
    return rawFrontierByAreaPair;
}
Also used : Utils(bwem.util.Utils) Altitude(bwem.typedef.Altitude) MutableInt(org.apache.commons.lang3.mutable.MutableInt) PriorityQueue(java.util.PriorityQueue) Map(bwem.map.Map) HashMap(java.util.HashMap) TilePosition(org.openbw.bwapi4j.TilePosition) Pair(org.openbw.bwapi4j.util.Pair) ArrayList(java.util.ArrayList) TerrainData(bwem.map.TerrainData) Area(bwem.area.Area) MutablePair(org.apache.commons.lang3.tuple.MutablePair) GroupId(bwem.area.typedef.GroupId) AreaId(bwem.area.typedef.AreaId) CPPath(bwem.typedef.CPPath) Index(bwem.typedef.Index) StaticBuilding(bwem.unit.StaticBuilding) Position(org.openbw.bwapi4j.Position) Tile(bwem.tile.Tile) Neutral(bwem.unit.Neutral) AreaInitializer(bwem.area.AreaInitializer) WalkPosition(org.openbw.bwapi4j.WalkPosition) Mineral(bwem.unit.Mineral) TileImpl(bwem.tile.TileImpl) List(java.util.List) Geyser(bwem.unit.Geyser) AreaInitializerImpl(bwem.area.AreaInitializerImpl) BwemExt(bwem.util.BwemExt) Queue(java.util.Queue) Comparator(java.util.Comparator) MiniTile(bwem.tile.MiniTile) HashMap(java.util.HashMap) AreaId(bwem.area.typedef.AreaId) ArrayList(java.util.ArrayList) MutablePair(org.apache.commons.lang3.tuple.MutablePair) WalkPosition(org.openbw.bwapi4j.WalkPosition) ArrayList(java.util.ArrayList) List(java.util.List)

Example 98 with MutablePair

use of org.apache.commons.lang3.tuple.MutablePair in project BWAPI4J by OpenBW.

the class MapPrinterExample method printMap.

/**
 * Draws the specified map data to the internal map printer and generates an image file.
 *
 * @param theMap the specified BWEM map data to print
 * @param filename the specified file without a file extension
 */
public void printMap(Map theMap, String filename) {
    java.util.Map<Integer, Color> mapZoneColor = new HashMap<>();
    for (int y = 0; y < theMap.getData().getMapData().getWalkSize().getY(); ++y) for (int x = 0; x < theMap.getData().getMapData().getWalkSize().getX(); ++x) {
        WalkPosition p = new WalkPosition(x, y);
        MiniTile miniTile = theMap.getData().getMiniTile(p, CheckMode.NO_CHECK);
        Color col;
        if (miniTile.isSea()) {
            if (mapPrinter.showSeaSide && theMap.getData().isSeaWithNonSeaNeighbors(p))
                col = MapPrinter.CustomColor.SEA_SIDE.color();
            else
                col = MapPrinter.CustomColor.SEA.color();
        } else {
            if (mapPrinter.showLakes && miniTile.isLake()) {
                col = MapPrinter.CustomColor.LAKE.color();
            } else {
                if (mapPrinter.showAltitude) {
                    int c = 255 - ((miniTile.getAltitude().intValue() * 255) / theMap.getHighestAltitude().intValue());
                    col = new Color(c, c, c);
                } else {
                    col = MapPrinter.CustomColor.TERRAIN.color();
                }
                if (mapPrinter.showAreas || mapPrinter.showContinents)
                    if (miniTile.getAreaId().intValue() > 0) {
                        Area area = theMap.getArea(miniTile.getAreaId());
                        Color zoneColor = getZoneColor(area, mapZoneColor);
                        int red = zoneColor.getRed() * col.getRed() / 255;
                        int green = zoneColor.getGreen() * col.getGreen() / 255;
                        col = new Color(red, green, 0);
                    } else {
                        col = MapPrinter.CustomColor.TINY_AREA.color();
                    }
            }
        }
        mapPrinter.point(p, col);
    }
    if (mapPrinter.showData)
        for (int y = 0; y < theMap.getData().getMapData().getTileSize().getY(); ++y) for (int x = 0; x < theMap.getData().getMapData().getTileSize().getX(); ++x) {
            int data = ((TileImpl) theMap.getData().getTile(new TilePosition(x, y))).getInternalData();
            int c = (((data / 1) * 1) % 256);
            Color col = new Color(c, c, c);
            WalkPosition origin = (new TilePosition(x, y)).toWalkPosition();
            mapPrinter.rectangle(origin, origin.add(new WalkPosition(3, 3)), col, MapPrinter.fill_t.fill);
        }
    if (mapPrinter.showUnbuildable)
        for (int y = 0; y < theMap.getData().getMapData().getTileSize().getY(); ++y) for (int x = 0; x < theMap.getData().getMapData().getTileSize().getX(); ++x) if (!theMap.getData().getTile(new TilePosition(x, y)).isBuildable()) {
            WalkPosition origin = (new TilePosition(x, y)).toWalkPosition();
            mapPrinter.rectangle(origin.add(new WalkPosition(1, 1)), origin.add(new WalkPosition(2, 2)), MapPrinter.CustomColor.UNBUILDABLE.color());
        }
    if (mapPrinter.showGroundHeight)
        for (int y = 0; y < theMap.getData().getMapData().getTileSize().getY(); ++y) for (int x = 0; x < theMap.getData().getMapData().getTileSize().getX(); ++x) {
            Tile.GroundHeight groundHeight = theMap.getData().getTile(new TilePosition(x, y)).getGroundHeight();
            if (groundHeight.intValue() >= Tile.GroundHeight.HIGH_GROUND.intValue())
                for (int dy = 0; dy < 4; ++dy) for (int dx = 0; dx < 4; ++dx) {
                    WalkPosition p = (new TilePosition(x, y).toWalkPosition()).add(new WalkPosition(dx, dy));
                    if (theMap.getData().getMiniTile(p, CheckMode.NO_CHECK).isWalkable())
                        if (((dx + dy) & (groundHeight == Tile.GroundHeight.HIGH_GROUND ? 1 : 3)) != 0)
                            mapPrinter.point(p, MapPrinter.CustomColor.HIGHER_GROUND.color());
                }
        }
    if (mapPrinter.showAssignedResources)
        for (Area area : theMap.getAreas()) for (Base base : area.getBases()) {
            for (Mineral m : base.getMinerals()) mapPrinter.line(base.getCenter().toWalkPosition(), m.getCenter().toWalkPosition(), MapPrinter.CustomColor.BASES.color());
            for (Geyser g : base.getGeysers()) mapPrinter.line(base.getCenter().toWalkPosition(), g.getCenter().toWalkPosition(), MapPrinter.CustomColor.BASES.color());
        }
    if (mapPrinter.showGeysers)
        for (Geyser g : theMap.getNeutralData().getGeysers()) printNeutral(theMap, g, MapPrinter.CustomColor.GEYSERS.color());
    if (mapPrinter.showMinerals)
        for (Mineral m : theMap.getNeutralData().getMinerals()) printNeutral(theMap, m, MapPrinter.CustomColor.MINERALS.color());
    if (mapPrinter.showStaticBuildings)
        for (StaticBuilding s : theMap.getNeutralData().getStaticBuildings()) printNeutral(theMap, s, MapPrinter.CustomColor.STATIC_BUILDINGS.color());
    if (mapPrinter.showStartingLocations)
        for (TilePosition t : theMap.getData().getMapData().getStartingLocations()) {
            WalkPosition origin = t.toWalkPosition();
            WalkPosition size = // same size for other races
            UnitType.Terran_Command_Center.tileSize().toWalkPosition();
            mapPrinter.rectangle(origin, origin.add(size).subtract(new WalkPosition(1, 1)), MapPrinter.CustomColor.STARTING_LOCATIONS.color(), MapPrinter.fill_t.fill);
        }
    if (mapPrinter.showBases)
        for (Area area : theMap.getAreas()) {
            for (Base base : area.getBases()) {
                WalkPosition origin = base.getLocation().toWalkPosition();
                WalkPosition size = UnitType.Terran_Command_Center.tileSize().toWalkPosition();
                MapPrinter.dashed_t dashMode = base.getBlockingMinerals().isEmpty() ? MapPrinter.dashed_t.not_dashed : MapPrinter.dashed_t.dashed;
                mapPrinter.rectangle(origin, origin.add(size).subtract(new WalkPosition(1, 1)), MapPrinter.CustomColor.BASES.color(), MapPrinter.fill_t.do_not_fill, dashMode);
            }
        }
    if (mapPrinter.showChokePoints) {
        for (MutablePair<MutablePair<AreaId, AreaId>, WalkPosition> f : theMap.getRawFrontier()) mapPrinter.point(f.getRight(), mapPrinter.showAreas ? MapPrinter.CustomColor.CHOKE_POINTS_SHOW_AREAS.color() : MapPrinter.CustomColor.CHOKE_POINTS_SHOW_CONTINENTS.color());
        for (Area area : theMap.getAreas()) for (ChokePoint cp : area.getChokePoints()) {
            ChokePoint.Node[] nodes = { ChokePoint.Node.END1, ChokePoint.Node.END2 };
            for (ChokePoint.Node n : nodes) mapPrinter.square(cp.getNodePosition(n), 1, new Color(255, 0, 255), MapPrinter.fill_t.fill);
            mapPrinter.square(cp.getCenter(), 1, new Color(0, 0, 255), MapPrinter.fill_t.fill);
        }
    }
    // TODO: Handle exception.
    try {
        mapPrinter.writeImageToFile(Paths.get(filename + ".png"), "png");
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
Also used : TileImpl(bwem.tile.TileImpl) Mineral(bwem.unit.Mineral) StaticBuilding(bwem.unit.StaticBuilding) HashMap(java.util.HashMap) MiniTile(bwem.tile.MiniTile) Tile(bwem.tile.Tile) MiniTile(bwem.tile.MiniTile) IOException(java.io.IOException) ChokePoint(bwem.ChokePoint) Base(bwem.Base) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Area(bwem.area.Area) Geyser(bwem.unit.Geyser) TilePosition(org.openbw.bwapi4j.TilePosition) WalkPosition(org.openbw.bwapi4j.WalkPosition) ChokePoint(bwem.ChokePoint)

Example 99 with MutablePair

use of org.apache.commons.lang3.tuple.MutablePair in project BWAPI4J by OpenBW.

the class MapInitializerImpl method replaceAreaIds.

@Override
public void replaceAreaIds(final WalkPosition p, final AreaId newAreaId) {
    final MiniTile origin = ((TerrainDataInitializer) getData()).getMiniTile_(p, CheckMode.NO_CHECK);
    final AreaId oldAreaId = origin.getAreaId();
    ((MiniTileImpl) origin).replaceAreaId(newAreaId);
    List<WalkPosition> toSearch = new ArrayList<>();
    toSearch.add(p);
    while (!toSearch.isEmpty()) {
        final WalkPosition current = toSearch.remove(toSearch.size() - 1);
        final WalkPosition[] deltas = { new WalkPosition(0, -1), new WalkPosition(-1, 0), new WalkPosition(+1, 0), new WalkPosition(0, +1) };
        for (final WalkPosition delta : deltas) {
            final WalkPosition next = current.add(delta);
            if (getData().getMapData().isValid(next)) {
                final MiniTile miniTile = ((TerrainDataInitializer) getData()).getMiniTile_(next, CheckMode.NO_CHECK);
                if (miniTile.getAreaId().equals(oldAreaId)) {
                    toSearch.add(next);
                    ((MiniTileImpl) miniTile).replaceAreaId(newAreaId);
                }
            }
        }
    }
    // also replaces references of oldAreaId by newAreaId in getRawFrontier:
    if (newAreaId.intValue() > 0) {
        for (final MutablePair<MutablePair<AreaId, AreaId>, WalkPosition> f : super.rawFrontier) {
            if (f.getLeft().getLeft().equals(oldAreaId)) {
                f.getLeft().setLeft(newAreaId);
            }
            if (f.getLeft().getRight().equals(oldAreaId)) {
                f.getLeft().setRight(newAreaId);
            }
        }
    }
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) MiniTile(bwem.tile.MiniTile) AreaId(bwem.area.typedef.AreaId) WalkPosition(org.openbw.bwapi4j.WalkPosition) ArrayList(java.util.ArrayList) MiniTileImpl(bwem.tile.MiniTileImpl)

Example 100 with MutablePair

use of org.apache.commons.lang3.tuple.MutablePair in project openlmis-stockmanagement by OpenLMIS.

the class Resource2Db method resourceCsvToBatchedPair.

/*
   converts a Resource which is a CSV, into a Pair where Pair.left is the SQL column names,
   and Pair.right is the rows of data which go into those columns (each row is an array, the array
   matches the order of the columns
   */
Pair<List<String>, List<Object[]>> resourceCsvToBatchedPair(final Resource resource) throws IOException {
    XLOGGER.entry(resource.getDescription());
    // parse CSV
    try (InputStreamReader isReader = new InputStreamReader(new BOMInputStream(resource.getInputStream(), ByteOrderMark.UTF_8))) {
        CSVParser parser = CSVFormat.DEFAULT.withHeader().withNullString("").parse(isReader);
        // read header row
        MutablePair<List<String>, List<Object[]>> readData = new MutablePair<>();
        readData.setLeft(new ArrayList<>(parser.getHeaderMap().keySet()));
        XLOGGER.info("Read header: " + readData.getLeft());
        // read data rows
        List<Object[]> rows = new ArrayList<>();
        for (CSVRecord record : parser.getRecords()) {
            if (!record.isConsistent()) {
                throw new IllegalArgumentException("CSV record inconsistent: " + record);
            }
            List theRow = IteratorUtils.toList(record.iterator());
            rows.add(theRow.toArray());
        }
        readData.setRight(rows);
        XLOGGER.exit("Records read: " + readData.getRight().size());
        return readData;
    }
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) BOMInputStream(org.apache.commons.io.input.BOMInputStream) InputStreamReader(java.io.InputStreamReader) CSVParser(org.apache.commons.csv.CSVParser) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CSVRecord(org.apache.commons.csv.CSVRecord)

Aggregations

MutablePair (org.apache.commons.lang3.tuple.MutablePair)116 Pair (org.apache.commons.lang3.tuple.Pair)49 ArrayList (java.util.ArrayList)32 Test (org.junit.Test)32 HashMap (java.util.HashMap)29 Message (com.microsoft.azure.sdk.iot.device.Message)27 IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)27 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)23 List (java.util.List)20 MqttDeviceTwin (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin)17 Map (java.util.Map)14 IOException (java.io.IOException)13 WalkPosition (org.openbw.bwapi4j.WalkPosition)9 MiniTile (bwem.tile.MiniTile)8 DeviceOperations (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations)8 AreaId (bwem.area.typedef.AreaId)7 Collectors (java.util.stream.Collectors)7 PositionImpl (org.apache.bookkeeper.mledger.impl.PositionImpl)7 HashSet (java.util.HashSet)6 TileImpl (bwem.tile.TileImpl)5