// Get the range.
ArrayToken rangeValue = (ArrayToken) range.getToken();
int dimensions = rangeValue.length();
double[] randomLocation = new double[dimensions];
CompositeActor container = (CompositeActor) getContainer();
StringBuffer changeMoML = new StringBuffer("<group>\n");
Iterator actors = container.deepEntityList().iterator();
while (actors.hasNext()) {
Entity node = (Entity) actors.next();
// Skip actors that are not properly marked.
Attribute mark = node.getAttribute("randomize");
if (!(mark instanceof Variable)) {
continue;
}
Token markValue = ((Variable) mark).getToken();
if (!(markValue instanceof BooleanToken)) {
continue;
}
if (!((BooleanToken) markValue).booleanValue()) {
continue;
}
for (int i = 0; i < dimensions; i++) {
ArrayToken lowHigh = (ArrayToken) rangeValue.getElement(i);
if (lowHigh.length() < 2) {
throw new IllegalActionException(this, "Invalid range: "
+ range.getExpression());
}
double low = ((DoubleToken) lowHigh.getElement(0))
.doubleValue();
double high = ((DoubleToken) lowHigh.getElement(1))
.doubleValue();
if (high < low) {
throw new IllegalActionException(this, "Invalid range: "
+ range.getExpression());
}
// If the precision is 0, then use the maximum precision allowed by double.
// Otherwise, round according to the maxPrecision parameter.
if (_mathContext == null) {
randomLocation[i] = low
+ ((_random.nextDouble()) * (high - low));
} else {
double candidateRandomLocation = low
+ ((_random.nextDouble()) * (high - low));
// Create a BigDecimal with the specified precision.
BigDecimal decimal = new BigDecimal(
candidateRandomLocation, _mathContext);
// Obtain the rounded double value.
randomLocation[i] = decimal.doubleValue();
}
}
changeMoML.append(_getLocationSetMoML(container, node,
randomLocation));
}
changeMoML.append("</group>");
MoMLChangeRequest request = new MoMLChangeRequest(this, container,
changeMoML.toString());
container.requestChange(request);
// Increment the workspace version number, since the wireless
// graph connectivity probably changed as a result of the node
// location randomization. This is used in conjunction with
// LimitedRangeChannel.valueChanged(), which invalidates the