use of au.gov.asd.tac.constellation.utilities.datastructure.Tuple in project constellation by constellation-app.
the class CopyCustomMarkersToGraphPlugin method edit.
@Override
protected void edit(final GraphWriteMethods graph, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
final int vertexIdentifierAttributeId = VisualConcept.VertexAttribute.IDENTIFIER.ensure(graph);
final int vertexTypeAttributeId = AnalyticConcept.VertexAttribute.TYPE.ensure(graph);
final int vertexLatitudeAttributeId = SpatialConcept.VertexAttribute.LATITUDE.ensure(graph);
final int vertexLongitudeAttributeId = SpatialConcept.VertexAttribute.LONGITUDE.ensure(graph);
final int vertexPrecisionAttributeId = SpatialConcept.VertexAttribute.PRECISION.ensure(graph);
final int vertexShapeAttributeId = SpatialConcept.VertexAttribute.SHAPE.ensure(graph);
final MarkerCache markerCache = MarkerCache.getDefault();
for (final ConstellationAbstractMarker marker : markerCache.getCustomMarkers()) {
final String markerId = marker.getId() == null ? marker.toString() : marker.getId();
final int vertexId = graph.addVertex();
graph.setStringValue(vertexIdentifierAttributeId, vertexId, markerId);
graph.setObjectValue(vertexTypeAttributeId, vertexId, AnalyticConcept.VertexType.LOCATION);
graph.setFloatValue(vertexLatitudeAttributeId, vertexId, marker.getLocation().getLat());
graph.setFloatValue(vertexLongitudeAttributeId, vertexId, marker.getLocation().getLon());
graph.setFloatValue(vertexPrecisionAttributeId, vertexId, (float) marker.getRadius());
try {
final Shape.GeometryType geometryType;
if (marker instanceof ConstellationMultiMarker) {
geometryType = Shape.GeometryType.MULTI_POLYGON;
} else if (marker instanceof ConstellationPolygonMarker) {
geometryType = Shape.GeometryType.POLYGON;
} else if (marker instanceof ConstellationLineMarker) {
geometryType = Shape.GeometryType.LINE;
} else {
geometryType = Shape.GeometryType.POINT;
}
final List<Tuple<Double, Double>> coordinates = marker.getLocations().stream().map(location -> Tuple.create((double) location.getLon(), (double) location.getLat())).collect(Collectors.toList());
final String shape = Shape.generateShape(markerId, geometryType, coordinates);
graph.setStringValue(vertexShapeAttributeId, vertexId, shape);
} catch (final IOException ex) {
throw new PluginException(PluginNotificationLevel.ERROR, ex);
}
}
PluginExecution.withPlugin(VisualSchemaPluginRegistry.COMPLETE_SCHEMA).executeNow(graph);
PluginExecution.withPlugin(InteractiveGraphPluginRegistry.RESET_VIEW).executeNow(graph);
}
use of au.gov.asd.tac.constellation.utilities.datastructure.Tuple in project constellation by constellation-app.
the class ActiveTableReferenceNGTest method updateVisibleColumnsRemove.
@Test
public void updateVisibleColumnsRemove() {
try (MockedStatic<PluginExecution> pluginExecutionMockedStatic = Mockito.mockStatic(PluginExecution.class)) {
final Graph graph = mock(Graph.class);
final Attribute attribute = mock(Attribute.class);
final PluginExecution pluginExecution = mock(PluginExecution.class);
final List<Tuple<String, Attribute>> paramColumnAttributes = List.of(Tuple.create("paramAttr", attribute));
final List<Tuple<String, Attribute>> stateColumnAttributes = List.of(Tuple.create("stateAttr", attribute), Tuple.create("paramAttr", attribute));
final TableViewState tableViewState = new TableViewState();
tableViewState.setColumnAttributes(stateColumnAttributes);
pluginExecutionMockedStatic.when(() -> PluginExecution.withPlugin(any(Plugin.class))).thenAnswer(executeUpdateStatePlugin(pluginExecution, tableViewState, List.of(Tuple.create("stateAttr", attribute))));
activeTableReference.updateVisibleColumns(graph, tableViewState, paramColumnAttributes, UpdateMethod.REMOVE);
verify(pluginExecution).executeLater(graph);
}
}
use of au.gov.asd.tac.constellation.utilities.datastructure.Tuple in project constellation by constellation-app.
the class Shape method generateShape.
/**
* Construct geojson to represent a single shape given a list of
* coordinates, a desired shape type and a unique identifier.
*
* @param uuid a unique identifier for the shape
* @param type a type of shape
* @param coordinates a list of coordinate tuples of the form (longitude,
* latitude) from which to build the shape
* @return a geojson string representing a single shape
* @throws IOException there was a problem writing the generated shape
*/
public static String generateShape(final String uuid, final GeometryType type, final List<Tuple<Double, Double>> coordinates) throws IOException {
// build precision formatter
final StringBuilder precisionPattern = new StringBuilder("#.");
for (int decimalPlace = 0; decimalPlace < GEOMETRY_PRECISION; decimalPlace++) {
precisionPattern.append("#");
}
final DecimalFormat precisionFormat = new DecimalFormat(precisionPattern.toString());
precisionFormat.setRoundingMode(RoundingMode.CEILING);
// calculate geometry
final double centroidLongitude = coordinates.stream().mapToDouble(coordinate -> coordinate.getFirst()).reduce((lon1, lon2) -> lon1 + lon2).getAsDouble() / coordinates.size();
final double centroidLatitude = coordinates.stream().mapToDouble(coordinate -> coordinate.getSecond()).reduce((lat1, lat2) -> lat1 + lat2).getAsDouble() / coordinates.size();
final double errorLongitude = coordinates.stream().mapToDouble(coordinate -> Math.abs(centroidLongitude - coordinate.getFirst())).max().getAsDouble();
final double errorLatitude = coordinates.stream().mapToDouble(coordinate -> Math.abs(centroidLatitude - coordinate.getSecond())).max().getAsDouble();
final double minLongitude = centroidLongitude - errorLongitude;
final double minLatitude = centroidLatitude - errorLatitude;
final double maxLongitude = centroidLongitude + errorLongitude;
final double maxLatitude = centroidLatitude + errorLatitude;
final double radius = Math.max(errorLatitude, errorLongitude);
// build geometry
final GeometryBuilder geometryBuilder = new GeometryBuilder();
final Geometry geometry;
switch(type) {
case POINT:
geometry = geometryBuilder.point(centroidLongitude, centroidLatitude);
break;
case LINE:
geometry = geometryBuilder.lineString(coordinates.stream().flatMap(Tuple::stream).mapToDouble(coordinate -> (Double) coordinate).toArray());
break;
case POLYGON:
geometry = geometryBuilder.polygon(coordinates.stream().flatMap(Tuple::stream).mapToDouble(coordinate -> (Double) coordinate).toArray());
break;
case BOX:
final List<Tuple<Double, Double>> boxCoordinates = new ArrayList<>();
boxCoordinates.add(Tuple.create(minLongitude, minLatitude));
boxCoordinates.add(Tuple.create(minLongitude, maxLatitude));
boxCoordinates.add(Tuple.create(maxLongitude, maxLatitude));
boxCoordinates.add(Tuple.create(maxLongitude, minLatitude));
geometry = geometryBuilder.polygon(boxCoordinates.stream().flatMap(Tuple::stream).mapToDouble(coordinate -> (Double) coordinate).toArray());
break;
default:
throw new IllegalArgumentException(String.format("The specified shape type, %s, is not currently supported.", type));
}
// initialise json
final String wgs84;
try {
wgs84 = SpatialReference.WGS84.getSrs();
} catch (final FactoryException ex) {
throw new IOException(ex);
}
final SimpleFeatureType featureType = generateFeatureType(uuid, wgs84, DEFAULT_GEOMETRY_ATTRIBUTE, type.getGeomertyClass(), null);
final FeatureJSON featureJson = generateFeatureJson(featureType, false);
// build feature
final SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
featureBuilder.add(geometry);
featureBuilder.set(NAME_ATTRIBUTE, uuid);
featureBuilder.set(CENTROID_LATITUDE_ATTRIBUTE, precisionFormat.format(centroidLatitude));
featureBuilder.set(CENTROID_LONGITUDE_ATTRIBUTE, precisionFormat.format(centroidLongitude));
featureBuilder.set(RADIUS_ATTRIBUTE, precisionFormat.format(radius));
final SimpleFeature feature = featureBuilder.buildFeature(uuid);
final DefaultFeatureCollection featureCollection = new DefaultFeatureCollection(uuid, featureType);
featureCollection.add(feature);
// build json
final ByteArrayOutputStream output = new ByteArrayOutputStream();
featureJson.writeFeatureCollection(featureCollection, output);
return output.toString(StandardCharsets.UTF_8.name());
}
use of au.gov.asd.tac.constellation.utilities.datastructure.Tuple in project constellation by constellation-app.
the class ListeningTopComponent method newActiveGraph.
@Override
public final void newActiveGraph(final Graph graph) {
LOGGER.finer("NewActiveGraph");
if (currentGraph != graph) {
if (currentGraph != null) {
currentGraph.removeGraphChangeListener(this);
currentGraph = null;
}
if (graph != null) {
currentGraph = graph;
currentGraph.addGraphChangeListener(this);
ReadableGraph readableGraph = currentGraph.getReadableGraph();
try {
final Map<GlobalMonitor, Consumer<Graph>> globalMonitorsCopy;
synchronized (globalMonitors) {
globalMonitorsCopy = new HashMap<>(globalMonitors);
}
globalMonitorsCopy.forEach((monitor, handler) -> monitor.update(readableGraph));
final Map<StructureMonitor, Consumer<Graph>> structureMonitorsCopy;
synchronized (globalMonitors) {
structureMonitorsCopy = new HashMap<>(structureMonitors);
}
structureMonitorsCopy.forEach((monitor, handler) -> monitor.update(readableGraph));
final Map<AttributeCountMonitor, Consumer<Graph>> attributeCountMonitorsCopy;
synchronized (globalMonitors) {
attributeCountMonitorsCopy = new HashMap<>(attributeCountMonitors);
}
attributeCountMonitorsCopy.forEach((monitor, handler) -> monitor.update(readableGraph));
final Map<AttributeValueMonitor, Tuple<Consumer<Graph>, MonitorTransitionFilter>> attributeMonitorsCopy;
synchronized (globalMonitors) {
attributeMonitorsCopy = new HashMap<>(attributeValueMonitors);
}
attributeMonitorsCopy.forEach((monitor, handler) -> monitor.update(readableGraph));
} finally {
readableGraph.release();
}
graphChanged(null);
}
}
handleNewGraph(graph);
}
use of au.gov.asd.tac.constellation.utilities.datastructure.Tuple in project constellation by constellation-app.
the class ListeningTopComponent method graphChanged.
@Override
public final void graphChanged(final GraphChangeEvent event) {
LOGGER.finer("GraphChange");
if (event != null && ignoredEvents.contains(event.getDescription())) {
LOGGER.log(Level.FINER, "IgnoringEvent::{0}", event.getDescription());
return;
}
ReadableGraph readableGraph = currentGraph.getReadableGraph();
try {
final Map<GlobalMonitor, Consumer<Graph>> globalMonitorsCopy;
synchronized (globalMonitors) {
globalMonitorsCopy = new HashMap<>(globalMonitors);
}
globalMonitorsCopy.forEach((monitor, handler) -> {
LOGGER.finer("GraphChanged::CheckGlobal");
if (monitor.update(readableGraph) == MonitorTransition.CHANGED) {
LOGGER.finer("GraphChanged::UpdateGlobal");
if (handler != null) {
handler.accept(currentGraph);
}
}
});
final Map<StructureMonitor, Consumer<Graph>> structureMonitorsCopy;
synchronized (globalMonitors) {
structureMonitorsCopy = new HashMap<>(structureMonitors);
}
structureMonitorsCopy.forEach((monitor, handler) -> {
LOGGER.finer("GraphChanged::CheckStructure");
if (monitor.update(readableGraph) == MonitorTransition.CHANGED) {
LOGGER.finer("GraphChanged::UpdateStructure");
if (handler != null) {
handler.accept(currentGraph);
}
}
});
final Map<AttributeCountMonitor, Consumer<Graph>> attributeCountMonitorsCopy;
synchronized (globalMonitors) {
attributeCountMonitorsCopy = new HashMap<>(attributeCountMonitors);
}
attributeCountMonitorsCopy.forEach((monitor, handler) -> {
LOGGER.finer("GraphChanged::CheckAttributeCount");
if (monitor.update(readableGraph) == MonitorTransition.CHANGED) {
LOGGER.finer("GraphChanged::UpdateAttributeCount");
if (handler != null) {
handler.accept(currentGraph);
}
}
});
final Map<AttributeValueMonitor, Tuple<Consumer<Graph>, MonitorTransitionFilter>> attributeMonitorsCopy;
synchronized (globalMonitors) {
attributeMonitorsCopy = new HashMap<>(attributeValueMonitors);
}
attributeMonitorsCopy.forEach((monitor, handlerPair) -> {
LOGGER.finer("GraphChanged::CheckAttribute");
final Consumer<Graph> handler = handlerPair.getFirst();
final MonitorTransitionFilter transitionFilter = handlerPair.getSecond();
monitor.update(readableGraph);
if (transitionFilter.matchesTransitions(monitor)) {
LOGGER.log(Level.FINER, "GraphChanged::UpdateAttribute::{0}", monitor.getName());
if (handler != null) {
handler.accept(currentGraph);
}
}
});
} finally {
readableGraph.release();
}
handleGraphChange(event);
}
Aggregations