Search in sources :

Example 1 with LayerName

use of au.gov.asd.tac.constellation.graph.schema.visual.attribute.objects.LayerName in project constellation by constellation-app.

the class LayerByTimePlugin method read.

@Override
public void read(final GraphReadMethods rg, final PluginInteraction interaction, final PluginParameters parameters) throws PluginException, InterruptedException {
    // We have the dtAttr from the original wg: we should have been passed the label, but never mind.
    // We need to get the label from the original, so we can get the dtAttr for the copy.
    final String dtAttrOrig = parameters.getParameters().get(DATETIME_ATTRIBUTE_PARAMETER_ID).getStringValue();
    if (dtAttrOrig == null) {
        interaction.notify(PluginNotificationLevel.ERROR, "A date-time attribute must be specified.");
        return;
    }
    final int dtAttrOrigId = rg.getAttribute(GraphElementType.TRANSACTION, dtAttrOrig);
    if (dtAttrOrigId == Graph.NOT_FOUND) {
        interaction.notify(PluginNotificationLevel.ERROR, "A valid date-time attribute must be specified.");
        return;
    }
    Graph copy;
    try {
        final Plugin copyGraphPlugin = PluginRegistry.get(InteractiveGraphPluginRegistry.COPY_TO_NEW_GRAPH);
        final PluginParameters copyParams = copyGraphPlugin.createParameters();
        copyParams.getParameters().get(CopyToNewGraphPlugin.NEW_SCHEMA_NAME_PARAMETER_ID).setStringValue(rg.getSchema().getFactory().getName());
        copyParams.getParameters().get(CopyToNewGraphPlugin.COPY_ALL_PARAMETER_ID).setBooleanValue(true);
        copyParams.getParameters().get(CopyToNewGraphPlugin.COPY_KEYS_PARAMETER_ID).setBooleanValue(false);
        PluginExecution.withPlugin(copyGraphPlugin).withParameters(copyParams).executeNow(rg);
        copy = (Graph) copyParams.getParameters().get(CopyToNewGraphPlugin.NEW_GRAPH_OUTPUT_PARAMETER_ID).getObjectValue();
    } catch (final PluginException ex) {
        copy = null;
        LOGGER.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    }
    if (copy == null) {
        // The copy failed, drop out now.
        return;
    }
    final Attribute dt = new GraphAttribute(rg, dtAttrOrigId);
    final WritableGraph wgcopy = copy.getWritableGraph("Layer by time", true);
    try {
        final int dtAttr = wgcopy.getAttribute(GraphElementType.TRANSACTION, dt.getName());
        final boolean useIntervals = parameters.getParameters().get(LAYER_BY_PARAMETER_ID).getStringValue().equals(INTERVAL_METHOD);
        final ZonedDateTime[] startEnd = parameters.getParameters().get(DATE_RANGE_PARAMETER_ID).getDateTimeRangeValue().getZonedStartEnd();
        final ZonedDateTime start = startEnd[0];
        final ZonedDateTime end = startEnd[1];
        final boolean isTransactionLayers = parameters.getParameters().get(TRANSACTION_AS_LAYER_PARAMETER_ID).getBooleanValue();
        // Establish new attributes.
        // Create and store graph attributes.
        final LayerName defaultName = new LayerName(Graph.NOT_FOUND, "Default");
        final int timeLayerAttr = wgcopy.addAttribute(GraphElementType.TRANSACTION, LayerNameAttributeDescription.ATTRIBUTE_NAME, LAYER_NAME, "time layer", defaultName, null);
        wgcopy.addAttribute(GraphElementType.GRAPH, IntegerAttributeDescription.ATTRIBUTE_NAME, NLAYERS, "The number of layers to layer by time", 1, null);
        final int txColorAttr = wgcopy.getAttribute(GraphElementType.TRANSACTION, "color");
        final int txGuideline = wgcopy.addAttribute(GraphElementType.TRANSACTION, BooleanAttributeDescription.ATTRIBUTE_NAME, "layer_guideline", "This transaction is a layer guideline", false, null);
        final ConstellationColor guidelineColor = ConstellationColor.getColorValue(0.25F, 0.25F, 0.25F, 1F);
        wgcopy.addAttribute(GraphElementType.VERTEX, IntegerAttributeDescription.ATTRIBUTE_NAME, ORIGINAL_ID_LABEL, "Original Node Id", -1, null);
        final List<Float> values = new ArrayList<>();
        final Map<Integer, List<Float>> remappedLayers = new HashMap<>();
        final Map<Integer, String> displayNames = new HashMap<>();
        if (useIntervals) {
            final int intervalUnit = LAYER_INTERVALS.get(parameters.getParameters().get(UNIT_PARAMETER_ID).getStringValue());
            final int intervalAmount = parameters.getParameters().get(AMOUNT_PARAMETER_ID).getIntegerValue();
            buildIntervals(wgcopy, values, remappedLayers, displayNames, dtAttr, start.toInstant(), end.toInstant(), intervalUnit, intervalAmount);
        } else {
            final int calendarUnit = BIN_CALENDAR_UNITS.get(parameters.getParameters().get(UNIT_PARAMETER_ID).getStringValue());
            final int binAmount = parameters.getParameters().get(AMOUNT_PARAMETER_ID).getIntegerValue();
            buildBins(wgcopy, values, remappedLayers, displayNames, dtAttr, start.toInstant(), end.toInstant(), calendarUnit, binAmount);
        }
        final boolean keepTxColors = parameters.getParameters().get(KEEP_TX_COLORS_PARAMETER_ID).getBooleanValue();
        final boolean drawTxGuides = parameters.getParameters().get(DRAW_TX_GUIDES_PARAMETER_ID).getBooleanValue();
        // Modify the copied graph to show our layers.
        int z = 0;
        float step = getWidth(wgcopy) / values.size();
        for (final Entry<Integer, List<Float>> entry : remappedLayers.entrySet()) {
            for (final Entry<Float, List<Integer>> currentLayer : transactionLayers.entrySet()) {
                if (entry.getValue().contains(currentLayer.getKey())) {
                    for (final int txId : currentLayer.getValue()) {
                        final float origLayer = currentLayer.getKey();
                        int newLayer = 0;
                        if (entry.getValue().contains(origLayer)) {
                            // Overwrite value
                            newLayer = entry.getKey();
                        }
                        final LayerName dn = new LayerName(newLayer, displayNames.get(newLayer));
                        wgcopy.setObjectValue(timeLayerAttr, txId, dn);
                        final float normLayer = newLayer / (remappedLayers.keySet().size() * 1F);
                        if (!keepTxColors) {
                            final Color heatmap = new Color(Color.HSBtoRGB((1 - normLayer) * 2F / 3F, 0.5F, 1));
                            final ConstellationColor color = ConstellationColor.getColorValue(heatmap.getRed() / 255F, heatmap.getGreen() / 255F, heatmap.getBlue() / 255F, 1F);
                            wgcopy.setObjectValue(txColorAttr, txId, color);
                        }
                        if (isTransactionLayers) {
                            transactionsAsLayers(wgcopy, txId, z, step);
                        } else {
                            nodesAsLayers(wgcopy, txId, newLayer);
                        }
                    }
                }
            }
            if (isTransactionLayers) {
                srcVxMap = dstVxMap;
                dstVxMap = new HashMap<>();
                z += step;
            }
        }
        // Remove any outstanding transactions flagged for deletion
        for (int txId = txToDelete.nextSetBit(0); txId >= 0; txId = txToDelete.nextSetBit(txId + 1)) {
            wgcopy.removeTransaction(txId);
        }
        // Get rid of all of the nodes that don't have any transactions.
        // By definition, the duplicates will have transactions between them, including the original layer
        // (because we just deleted transactions that belong in different layers, leaving only the transactions
        // that belong in the original layer).
        final List<Integer> vertices = new ArrayList<>();
        for (int position = 0; position < wgcopy.getVertexCount(); position++) {
            final int vertexId = wgcopy.getVertex(position);
            final int nTx = wgcopy.getVertexTransactionCount(vertexId);
            if (nTx == 0) {
                vertices.add(vertexId);
            }
        }
        vertices.stream().forEach(wgcopy::removeVertex);
        if (drawTxGuides) {
            interaction.setProgress(5, 6, "Draw guide lines", false);
            // We have to do this after the "remove node without transactions" step because we're adding more transactions.
            if (!isTransactionLayers && remappedLayers.keySet().size() > 1) {
                nodeIdToLayers.keySet().stream().forEach(origNodeId -> {
                    int prevNodeId = -1;
                    final BitSet layers = nodeIdToLayers.get(origNodeId);
                    for (int layer = layers.nextSetBit(0); layer >= 0; layer = layers.nextSetBit(layer + 1)) {
                        final int nodeId = layer == 0 ? origNodeId : nodeDups.get(String.format("%s/%s", origNodeId, layer));
                        if (prevNodeId != -1) {
                            final int sTxId = wgcopy.addTransaction(prevNodeId, nodeId, false);
                            wgcopy.setBooleanValue(txGuideline, sTxId, true);
                            wgcopy.setObjectValue(txColorAttr, sTxId, guidelineColor);
                            final LayerName dn = new LayerName(1107, "Guideline");
                            wgcopy.setObjectValue(timeLayerAttr, sTxId, dn);
                        }
                        prevNodeId = nodeId;
                    }
                });
            }
        }
    } finally {
        wgcopy.commit();
    }
}
Also used : Attribute(au.gov.asd.tac.constellation.graph.Attribute) GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) HashMap(java.util.HashMap) GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) ArrayList(java.util.ArrayList) ZonedDateTime(java.time.ZonedDateTime) List(java.util.List) ArrayList(java.util.ArrayList) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) ConstellationColor(au.gov.asd.tac.constellation.utilities.color.ConstellationColor) LayerName(au.gov.asd.tac.constellation.graph.schema.visual.attribute.objects.LayerName) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) Color(java.awt.Color) ConstellationColor(au.gov.asd.tac.constellation.utilities.color.ConstellationColor) BitSet(java.util.BitSet) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) CopyToNewGraphPlugin(au.gov.asd.tac.constellation.graph.interaction.plugins.clipboard.CopyToNewGraphPlugin) SimpleReadPlugin(au.gov.asd.tac.constellation.plugins.templates.SimpleReadPlugin) Plugin(au.gov.asd.tac.constellation.plugins.Plugin)

Example 2 with LayerName

use of au.gov.asd.tac.constellation.graph.schema.visual.attribute.objects.LayerName in project constellation by constellation-app.

the class LayerNameIOProvider method readObject.

/**
 * Deserialise an object from a JsonNode.
 * <p>
 * Refer to base class for detailed description.
 *
 * @param attributeId The id of the attribute being read.
 * @param elementId The id of the element being read.
 * @param jnode The JsonNode to read from.
 * @param graph The graph that the resulting object will be placed in. Provided in case
 * the object requires some graph data.
 * @param vertexMap (not used) A mapping from a vertex id in the file to the vertex id
 * in the graph.
 * @param transactionMap (not used) A mapping from a transaction id in the file to the
 * transaction id in the graph.
 * @param byteReader (not used) The byte reader containing ancillary data (e.g. images)
 * that doesn't easily fit into a JSON document.
 * @param cache (not used) cache A cache that can be used to dedup identical instances of the
 * same immutable objects.
 * @throws java.io.IOException If there's a problem reading the document.
 */
@Override
public void readObject(final int attributeId, final int elementId, final JsonNode jnode, final GraphWriteMethods graph, final Map<Integer, Integer> vertexMap, final Map<Integer, Integer> transactionMap, final GraphByteReader byteReader, ImmutableObjectCache cache) throws IOException {
    if (!jnode.isNull() && jnode.has(NAME_TAG) && jnode.has(LAYER_TAG)) {
        final String name = jnode.get(NAME_TAG).textValue();
        final int layer = jnode.get(LAYER_TAG).intValue();
        final LayerName ln = new LayerName(layer, name);
        graph.setObjectValue(attributeId, elementId, ln);
    }
}
Also used : LayerName(au.gov.asd.tac.constellation.graph.schema.visual.attribute.objects.LayerName)

Example 3 with LayerName

use of au.gov.asd.tac.constellation.graph.schema.visual.attribute.objects.LayerName in project constellation by constellation-app.

the class LayerNameIOProvider method writeObject.

/**
 * Write this object to the JSON generator.
 * <p>
 * Refer to base class for detailed description.
 *
 * @param attr The attribute being written.
 * @param elementId The id of the element being written.
 * @param jsonGenerator The JsonGenerator used to write to the JSON document.
 * @param graph The graph that the object belongs to. Provided in case the object requires some
 * graph data.
 * @param byteWriter (not used)  For ancillary data (e.g. images) that doesn't easily
 * fit into a JSON document.
 * @param verbose Determines whether to write default values of attributes or not.
 * @throws IOException
 */
@Override
public void writeObject(final Attribute attr, final int elementId, final JsonGenerator jsonGenerator, final GraphReadMethods graph, final GraphByteWriter byteWriter, final boolean verbose) throws IOException {
    if (verbose || !graph.isDefaultValue(attr.getId(), elementId)) {
        final LayerName ln = (LayerName) graph.getObjectValue(attr.getId(), elementId);
        if (ln != null) {
            jsonGenerator.writeObjectFieldStart(attr.getName());
            jsonGenerator.writeStringField(NAME_TAG, ln.getName());
            jsonGenerator.writeNumberField(LAYER_TAG, ln.getLayer());
            jsonGenerator.writeEndObject();
        } else {
            jsonGenerator.writeNullField(attr.getName());
        }
    }
}
Also used : LayerName(au.gov.asd.tac.constellation.graph.schema.visual.attribute.objects.LayerName)

Example 4 with LayerName

use of au.gov.asd.tac.constellation.graph.schema.visual.attribute.objects.LayerName in project constellation by constellation-app.

the class LayerNameIOProviderNGTest method testReadObject.

/**
 * Test of readObject method, of class LayerNameIOProvider.
 */
@Test
public void testReadObject() throws Exception {
    System.out.println("LayerNameIOProviderNGTest.testReadObject");
    // Call method under test with JsonNode.isNull=true, name and layer tags found
    resetMocking();
    when(mockJsonNode.isNull()).thenReturn(true);
    when(mockJsonNode.has(anyString())).thenReturn(true);
    instance.readObject(attributeId, elementId, mockJsonNode, mockGraphWriteMethods, null, null, null, null);
    Mockito.verify(mockJsonNode, times(0)).get(any());
    // Call method under test with JsonNode.isNull=true, name tag not found
    resetMocking();
    when(mockJsonNode.isNull()).thenReturn(true);
    when(mockJsonNode.has("name")).thenReturn(false);
    when(mockJsonNode.has("layer")).thenReturn(true);
    instance.readObject(attributeId, elementId, mockJsonNode, mockGraphWriteMethods, null, null, null, null);
    Mockito.verify(mockJsonNode, times(0)).get(any());
    // Call method under test with JsonNode.isNull=true, layer tag not found
    resetMocking();
    when(mockJsonNode.isNull()).thenReturn(true);
    when(mockJsonNode.has("name")).thenReturn(true);
    when(mockJsonNode.has("layer")).thenReturn(false);
    instance.readObject(attributeId, elementId, mockJsonNode, mockGraphWriteMethods, null, null, null, null);
    Mockito.verify(mockJsonNode, times(0)).get(any());
    // Call method under test with JsonNode.isNull=true, name and layers tag not found
    resetMocking();
    when(mockJsonNode.isNull()).thenReturn(true);
    when(mockJsonNode.has("name")).thenReturn(false);
    when(mockJsonNode.has("layer")).thenReturn(false);
    instance.readObject(attributeId, elementId, mockJsonNode, mockGraphWriteMethods, null, null, null, null);
    Mockito.verify(mockJsonNode, times(0)).get(any());
    // Call method under test with JsonNode.isNull=false, name and layers tag not found
    resetMocking();
    when(mockJsonNode.isNull()).thenReturn(false);
    when(mockJsonNode.has("name")).thenReturn(false);
    when(mockJsonNode.has("layer")).thenReturn(false);
    instance.readObject(attributeId, elementId, mockJsonNode, mockGraphWriteMethods, null, null, null, null);
    Mockito.verify(mockJsonNode, times(0)).get(any());
    // Call method under test with JsonNode.isNull=false, name tag not found
    resetMocking();
    when(mockJsonNode.isNull()).thenReturn(false);
    when(mockJsonNode.has("name")).thenReturn(false);
    when(mockJsonNode.has("layer")).thenReturn(true);
    instance.readObject(attributeId, elementId, mockJsonNode, mockGraphWriteMethods, null, null, null, null);
    Mockito.verify(mockJsonNode, times(0)).get(any());
    // Call method under test with JsonNode.isNull=false, layer tag not found
    resetMocking();
    when(mockJsonNode.isNull()).thenReturn(false);
    when(mockJsonNode.has("name")).thenReturn(true);
    when(mockJsonNode.has("layer")).thenReturn(false);
    instance.readObject(attributeId, elementId, mockJsonNode, mockGraphWriteMethods, null, null, null, null);
    Mockito.verify(mockJsonNode, times(0)).get(any());
    // Call method under test with JsonNode.isNull=false, name and layer tags found
    resetMocking();
    when(mockJsonNode.isNull()).thenReturn(false);
    when(mockJsonNode.has(anyString())).thenReturn(true);
    when(mockJsonNode.get("name")).thenReturn(new TextNode("name"));
    when(mockJsonNode.get("layer")).thenReturn(new IntNode(23));
    instance.readObject(attributeId, elementId, mockJsonNode, mockGraphWriteMethods, null, null, null, null);
    Mockito.verify(mockJsonNode, times(1)).get("name");
    Mockito.verify(mockJsonNode, times(1)).get("layer");
    Mockito.verify(mockGraphWriteMethods, times(1)).setObjectValue(attributeId, elementId, new LayerName(23, "name"));
}
Also used : IntNode(com.fasterxml.jackson.databind.node.IntNode) LayerName(au.gov.asd.tac.constellation.graph.schema.visual.attribute.objects.LayerName) TextNode(com.fasterxml.jackson.databind.node.TextNode) Test(org.testng.annotations.Test)

Example 5 with LayerName

use of au.gov.asd.tac.constellation.graph.schema.visual.attribute.objects.LayerName in project constellation by constellation-app.

the class LayerNameIOProviderNGTest method testWriteObject.

/**
 * Test of writeObject method, of class LayerNameIOProvider.
 */
@Test
public void testWriteObject() throws Exception {
    System.out.println("LayerNameIOProviderNGTest.testWriteObject");
    // Test not verbose and graph.IsDefaultValue is true skips all processing
    resetMocking();
    when(mockGraphReadMethods.isDefaultValue(anyInt(), anyInt())).thenReturn(true);
    instance.writeObject(attr, elementId, mockJsonGenerator, mockGraphReadMethods, null, false);
    Mockito.verify(mockGraphReadMethods, times(0)).getObjectValue(anyInt(), anyInt());
    // Test verbose, graph.IsDefaultValue is true, graph.getObjectValue returns null
    resetMocking();
    when(mockGraphReadMethods.isDefaultValue(anyInt(), anyInt())).thenReturn(true);
    when(mockGraphReadMethods.getObjectValue(anyInt(), anyInt())).thenReturn(null);
    instance.writeObject(attr, elementId, mockJsonGenerator, mockGraphReadMethods, null, true);
    Mockito.verify(mockGraphReadMethods, times(1)).getObjectValue(attributeId, elementId);
    Mockito.verify(mockJsonGenerator, times(1)).writeNullField(attr.getName());
    Mockito.verify(mockJsonGenerator, times(0)).writeObjectFieldStart(any());
    // Test verbose, graph.IsDefaultValue is false, graph.getObjectValue returns LayerName object
    resetMocking();
    when(mockGraphReadMethods.isDefaultValue(anyInt(), anyInt())).thenReturn(false);
    when(mockGraphReadMethods.getObjectValue(anyInt(), anyInt())).thenReturn(new LayerName(23, "name"));
    instance.writeObject(attr, elementId, mockJsonGenerator, mockGraphReadMethods, null, true);
    Mockito.verify(mockGraphReadMethods, times(1)).getObjectValue(attributeId, elementId);
    Mockito.verify(mockJsonGenerator, times(0)).writeNullField(attr.getName());
    Mockito.verify(mockJsonGenerator, times(1)).writeObjectFieldStart(any());
    Mockito.verify(mockJsonGenerator, times(1)).writeStringField("name", "name");
    Mockito.verify(mockJsonGenerator, times(1)).writeNumberField("layer", 23);
    Mockito.verify(mockJsonGenerator, times(1)).writeEndObject();
    // Test not verbose, graph.IsDefaultValue is false, graph.getObjectValue returns null
    resetMocking();
    when(mockGraphReadMethods.isDefaultValue(anyInt(), anyInt())).thenReturn(false);
    when(mockGraphReadMethods.getObjectValue(anyInt(), anyInt())).thenReturn(null);
    instance.writeObject(attr, elementId, mockJsonGenerator, mockGraphReadMethods, null, false);
    Mockito.verify(mockGraphReadMethods, times(1)).getObjectValue(attributeId, elementId);
    Mockito.verify(mockJsonGenerator, times(1)).writeNullField(attr.getName());
    Mockito.verify(mockJsonGenerator, times(0)).writeObjectFieldStart(any());
}
Also used : LayerName(au.gov.asd.tac.constellation.graph.schema.visual.attribute.objects.LayerName) Test(org.testng.annotations.Test)

Aggregations

LayerName (au.gov.asd.tac.constellation.graph.schema.visual.attribute.objects.LayerName)6 Test (org.testng.annotations.Test)3 Attribute (au.gov.asd.tac.constellation.graph.Attribute)1 Graph (au.gov.asd.tac.constellation.graph.Graph)1 GraphAttribute (au.gov.asd.tac.constellation.graph.GraphAttribute)1 ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)1 WritableGraph (au.gov.asd.tac.constellation.graph.WritableGraph)1 CopyToNewGraphPlugin (au.gov.asd.tac.constellation.graph.interaction.plugins.clipboard.CopyToNewGraphPlugin)1 Plugin (au.gov.asd.tac.constellation.plugins.Plugin)1 PluginException (au.gov.asd.tac.constellation.plugins.PluginException)1 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)1 SimpleReadPlugin (au.gov.asd.tac.constellation.plugins.templates.SimpleReadPlugin)1 ConstellationColor (au.gov.asd.tac.constellation.utilities.color.ConstellationColor)1 IntNode (com.fasterxml.jackson.databind.node.IntNode)1 TextNode (com.fasterxml.jackson.databind.node.TextNode)1 Color (java.awt.Color)1 ZonedDateTime (java.time.ZonedDateTime)1 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 HashMap (java.util.HashMap)1