Skip to content

Commit

Permalink
updated DefaultDataSetLock in response to #33
Browse files Browse the repository at this point in the history
  • Loading branch information
RalphSteinhagen committed Sep 14, 2019
1 parent d5a4de1 commit 6bb9276
Show file tree
Hide file tree
Showing 3 changed files with 264 additions and 207 deletions.
71 changes: 51 additions & 20 deletions chartfx-chart/src/main/java/de/gsi/chart/XYChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import de.gsi.dataset.AxisDescription;
import de.gsi.dataset.DataSet;
import de.gsi.dataset.DataSet3D;
import de.gsi.dataset.locks.DefaultDataSetLock;
import de.gsi.dataset.utils.AssertUtils;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
Expand Down Expand Up @@ -242,11 +243,46 @@ public void updateAxisRange() {
return;
}

getAxes().forEach(chartAxis -> {
final List<DataSet> dataSets = getDataSetForAxis(chartAxis);
updateNumericAxis(chartAxis, dataSets);
// chartAxis.requestAxisLayout();
});
// lock datasets to prevent writes while updating the axes
ObservableList<DataSet> dataSets = this.getAllDatasets();
// check that all registered data sets have proper ranges defined
dataSets.parallelStream().forEach(dataset -> dataset.getAxisDescriptions().parallelStream()
.filter(axisD -> !axisD.isDefined()).forEach(axisDescription -> {
final boolean oldAutoState = dataset.isAutoNotification();
dataset.setAutoNotifaction(false);
dataset.recomputeLimits(dataset.getAxisDescriptions().indexOf(axisDescription));
dataset.setAutoNotifaction(oldAutoState);
}));

// N.B. possible race condition on this line -> for the future to solve
// recomputeLimits holds a writeLock the following sections need a read lock (for allowing parallel axis)
// there isn't an easy way to down-grade the established write locks into read locks (yet)
// Experimental version:
// dataSets.forEach(dataset -> {
// dataset.lock().writeLock();
// dataset.getAxisDescriptions().parallelStream().filter(axisD -> !axisD.isDefined())
// .forEach(axisDescription -> {
// final boolean oldAutoState = dataset.isAutoNotification();
// dataset.setAutoNotifaction(false);
// dataset.recomputeLimits(dataset.getAxisDescriptions().indexOf(axisDescription));
// dataset.setAutoNotifaction(oldAutoState);
// });
// DefaultDataSetLock<DataSet> myLock = (DefaultDataSetLock<DataSet>) dataset.lock();
// myLock.downGradeWriteLock();
// });

dataSets.forEach(ds -> ds.lock().readLock());
try {
getAxes().forEach(chartAxis -> {
final List<DataSet> dataSetForAxis = getDataSetForAxis(chartAxis);
updateNumericAxis(chartAxis, dataSetForAxis);
// chartAxis.requestAxisLayout();
});
} finally {
dataSets.forEach(ds -> ds.lock().readUnLock());
}

// unlock datasets again
}

/**
Expand Down Expand Up @@ -314,7 +350,7 @@ protected void axisSideChanged(final ObservableValue<? extends Side> change, fin
}

// check if axis is in correct pane
if (getAxesPane(axis.getSide()).getChildren().contains((Node)axis)) {
if (getAxesPane(axis.getSide()).getChildren().contains((Node) axis)) {
// yes, it is continue with next axis
continue;
}
Expand Down Expand Up @@ -403,18 +439,13 @@ protected static void updateNumericAxis(final Axis axis, final List<DataSet> dat
final Side side = axis.getSide();
axis.getAutoRange().empty();
dataSets.forEach(dataset -> {
for (AxisDescription axisDescription : dataset.getAxisDescriptions()) {
if (!axisDescription.isDefined()) {
dataset.recomputeLimits(dataset.getAxisDescriptions().indexOf(axisDescription));
}
}
if (dataset instanceof DataSet3D && (side == Side.RIGHT || side == Side.TOP)) {
final DataSet3D mDataSet = (DataSet3D) dataset;
axis.getAutoRange().add(mDataSet.getAxisDescription(2).getMin());
axis.getAutoRange().add(mDataSet.getAxisDescription(2).getMax());
} else {
axis.getAutoRange().add(dataset.getAxisDescription(isHorizontal ? 0:1).getMin());
axis.getAutoRange().add(dataset.getAxisDescription(isHorizontal ? 0:1).getMax());
axis.getAutoRange().add(dataset.getAxisDescription(isHorizontal ? 0 : 1).getMin());
axis.getAutoRange().add(dataset.getAxisDescription(isHorizontal ? 0 : 1).getMax());
}
});
axis.getAutoRange().setAxisLength(axis.getLength() == 0 ? 1 : axis.getLength(), side);
Expand All @@ -427,18 +458,18 @@ protected static void updateNumericAxis(final Axis axis, final List<DataSet> dat

final List<Number> dataMinMax = new ArrayList<>();
dataSets.forEach(dataset -> {
// for (AxisDescription axisDescription : dataset.getAxisDescriptions()) {
// if (!axisDescription.isDefined()) {
// dataset.recomputeLimits(dataset.getAxisDescriptions().indexOf(axisDescription));
// }
// }
// for (AxisDescription axisDescription : dataset.getAxisDescriptions()) {
// if (!axisDescription.isDefined()) {
// dataset.recomputeLimits(dataset.getAxisDescriptions().indexOf(axisDescription));
// }
// }
if (dataset instanceof DataSet3D && (side == Side.RIGHT || side == Side.TOP)) {
final DataSet3D mDataSet = (DataSet3D) dataset;
dataMinMax.add(mDataSet.getAxisDescription(2).getMin());
dataMinMax.add(mDataSet.getAxisDescription(2).getMax());
} else {
dataMinMax.add(dataset.getAxisDescription(isHorizontal ? 0:1).getMin());
dataMinMax.add(dataset.getAxisDescription(isHorizontal ? 0:1).getMax());
dataMinMax.add(dataset.getAxisDescription(isHorizontal ? 0 : 1).getMin());
dataMinMax.add(dataset.getAxisDescription(isHorizontal ? 0 : 1).getMax());
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ protected void computeBoundaryVariables(final Axis xAxis, final Axis yAxis) {
}

protected void setStyleVariable(final DataSet dataSet, final int dsIndex) {
dataSet.lock().readLockGuard(() -> defaultStyle = dataSet.getStyle());
dataSet.lock().readLockGuardOptimistic(() -> defaultStyle = dataSet.getStyle());
final Integer layoutOffset = StyleParser.getIntegerPropertyValue(defaultStyle,
XYChartCss.DATASET_LAYOUT_OFFSET);
final Integer dsIndexLocal = StyleParser.getIntegerPropertyValue(defaultStyle, XYChartCss.DATASET_INDEX);
Expand All @@ -188,7 +188,7 @@ protected void setErrorType(final DataSet dataSet, final ErrorStyle errorStyle)
// compute screen coordinates of other points
if (dataSet instanceof DataSetError) {
final DataSetError ds = (DataSetError) dataSet;
errorType = dataSet.lock().readLockGuard(ds::getErrorType);
errorType = dataSet.lock().readLockGuardOptimistic(ds::getErrorType);
} else {
// fall-back for standard DataSet

Expand Down Expand Up @@ -290,8 +290,8 @@ private void computeNoErrorPolar(final Axis yAxis, final DataSet dataSet, final

private void computeYonly(final Axis xAxis, final Axis yAxis, final DataSet dataSet, final int min, final int max) {
if (dataSet instanceof DataSetError) {
final DataSetError ds = (DataSetError) dataSet;
dataSet.lock().readLockGuard(() -> {
final DataSetError ds = (DataSetError) dataSet;
for (int index = min; index < max; index++) {
final double x = dataSet.getX(index);
final double y = dataSet.getY(index);
Expand Down Expand Up @@ -341,7 +341,6 @@ private void computeYonly(final Axis xAxis, final Axis yAxis, final DataSet data
}

private void computeYonlyPolar(final Axis yAxis, final DataSet dataSet, final int min, final int max) {

dataSet.lock().readLockGuard(() -> {
for (int index = min; index < max; index++) {
final double x = dataSet.getX(index);
Expand Down
Loading

0 comments on commit 6bb9276

Please sign in to comment.