use of org.apache.commons.lang3.tuple.MutablePair in project azure-iot-sdk-java by Azure.
the class MqttDeviceTwinTest method receiveParsesResponseTopicInvalidStatusThrowsException.
/*
**Tests_SRS_MQTTDEVICETWIN_25_039: [If the topic is of type response topic and if status is either a non 3 digit number or not found then receive shall throw TransportException ]
*/
@Test(expected = TransportException.class)
public void receiveParsesResponseTopicInvalidStatusThrowsException() throws TransportException {
final byte[] actualPayload = "GetTwinResponseDataContainingDesiredAndReportedPropertiesDocument".getBytes(StandardCharsets.UTF_8);
final String expectedTopic = "$iothub/twin/res/" + "abc/" + "?$rid=" + mockReqId;
IotHubTransportMessage receivedMessage = null;
try {
// arrange
MqttDeviceTwin testTwin = new MqttDeviceTwin("", mockedConnectOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
Queue<Pair<String, byte[]>> testreceivedMessages = new ConcurrentLinkedQueue<>();
testreceivedMessages.add(new MutablePair<>(expectedTopic, actualPayload));
Deencapsulation.setField(testTwin, "receivedMessages", testreceivedMessages);
Deencapsulation.setField(testTwin, "stateLock", new Object());
// act
receivedMessage = testTwin.receive();
} finally {
// assert
assertNull(receivedMessage);
}
}
use of org.apache.commons.lang3.tuple.MutablePair in project azure-iot-sdk-java by Azure.
the class MqttTest method receiveSuccessWithModuleIdAndInputName.
// Tests_SRS_Mqtt_34_050: [This function shall extract the inputName from the topic if the topic string fits the following convention: 'devices/<deviceId>/modules/<moduleId>/inputs/<inputName>']
// Tests_SRS_Mqtt_34_051: [This function shall extract the moduleId from the topic if the topic string fits the following convention: 'devices/<deviceId>/modules/<moduleId>']
@Test
public void receiveSuccessWithModuleIdAndInputName() throws MqttException, TransportException {
// arrange
final byte[] payload = { 0x61, 0x62, 0x63 };
baseConnectExpectation();
final Mqtt mockMqtt = new MqttMessaging(CLIENT_ID, null, "", false, mockMqttConnectionOptions, new HashMap<Integer, Message>(), new ConcurrentLinkedQueue<Pair<String, byte[]>>());
Deencapsulation.invoke(mockMqtt, "setMqttAsyncClient", mockMqttAsyncClient);
new NonStrictExpectations() {
{
mockMqttAsyncClient.isConnected();
result = true;
}
};
Queue<Pair<String, byte[]>> testreceivedMessages = new ConcurrentLinkedQueue<>();
Deencapsulation.setField(mockMqtt, "receivedMessages", testreceivedMessages);
testreceivedMessages.add(new MutablePair<>(MOCK_PARSE_TOPIC_WITH_INPUT_NAME, payload));
Deencapsulation.invoke(mockMqtt, "connect");
// act
Message receivedMessage = mockMqtt.receive();
// assert
assertEquals(expectedInputName, receivedMessage.getInputName());
assertEquals(expectedModuleId, receivedMessage.getConnectionModuleId());
}
use of org.apache.commons.lang3.tuple.MutablePair in project shifu by ShifuML.
the class TreeModel method getFeatureImportances.
/**
* Get feature importance of current model.
*
* @return map of feature importance, key is column index.
*/
public Map<Integer, MutablePair<String, Double>> getFeatureImportances() {
Map<Integer, MutablePair<String, Double>> importancesSum = new HashMap<Integer, MutablePair<String, Double>>();
Map<Integer, String> nameMapping = this.getIndependentTreeModel().getNumNameMapping();
int treeSize = this.getIndependentTreeModel().getTrees().size();
// such case we only support treeModel is one element list
if (this.getIndependentTreeModel().getTrees().size() != 1) {
throw new RuntimeException("Bagging model cannot be supported in Tree Model one element feature importance computing.");
}
for (TreeNode tree : this.getIndependentTreeModel().getTrees().get(0)) {
// get current tree importance at first
Map<Integer, Double> subImportances = tree.computeFeatureImportance();
// merge feature importance from different trees
for (Entry<Integer, Double> entry : subImportances.entrySet()) {
String featureName = nameMapping.get(entry.getKey());
MutablePair<String, Double> importance = MutablePair.of(featureName, entry.getValue());
if (!importancesSum.containsKey(entry.getKey())) {
importance.setValue(importance.getValue() / treeSize);
importancesSum.put(entry.getKey(), importance);
} else {
MutablePair<String, Double> current = importancesSum.get(entry.getKey());
current.setValue(current.getValue() + importance.getValue() / treeSize);
importancesSum.put(entry.getKey(), current);
}
}
}
return importancesSum;
}
use of org.apache.commons.lang3.tuple.MutablePair in project BWAPI4J by OpenBW.
the class MapPrinterExample method printMap.
public void printMap(Map theMap) {
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 (// groundHeight is usefull only for walkable miniTiles
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();
// same size for other races
WalkPosition size = 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();
// same size for other races
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);
}
}
try {
mapPrinter.writeImageToFile(Paths.get("map.png"), "png");
} catch (IOException ex) {
ex.printStackTrace();
}
}
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<Void>>, RpcResult<T>> compositeTransform) {
final MutablePair<RpcResult<T>, RpcResult<Void>> resultPair = new MutablePair<>();
// store input result and append barrier
final ListenableFuture<RpcResult<Void>> barrierResult = Futures.transformAsync(input, interInput -> {
resultPair.setLeft(interInput);
final SendBarrierInput barrierInput = createSendBarrierInput(nodeRef);
return JdkFutureAdapters.listenInPoolThread(transactionService.sendBarrier(barrierInput));
}, MoreExecutors.directExecutor());
// store barrier result and return initiated pair
final ListenableFuture<Pair<RpcResult<T>, RpcResult<Void>>> compositeResult = Futures.transform(barrierResult, new Function<RpcResult<Void>, Pair<RpcResult<T>, RpcResult<Void>>>() {
@Nullable
@Override
public Pair<RpcResult<T>, RpcResult<Void>> apply(@Nullable final RpcResult<Void> input) {
resultPair.setRight(input);
return resultPair;
}
}, MoreExecutors.directExecutor());
// append assembling transform to barrier result
return Futures.transform(compositeResult, compositeTransform, MoreExecutors.directExecutor());
}
Aggregations