Search in sources :

Example 1 with WritableGraph

use of au.gov.asd.tac.constellation.graph.WritableGraph 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 WritableGraph

use of au.gov.asd.tac.constellation.graph.WritableGraph in project constellation by constellation-app.

the class ArrangeInSingleTreeAction method edit.

@Override
protected void edit(final PluginGraphs graphs, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
    final Graph graph = graphs.getGraph();
    final WritableGraph wg = graph.getWritableGraph(Bundle.CTL_ArrangeInSingleTreeAction(), true);
    try {
        final Worker worker = new Worker(wg);
        worker.run();
        PluginExecution.withPlugin(InteractiveGraphPluginRegistry.RESET_VIEW).executeNow(graphs.getGraph());
    } finally {
        wg.commit();
    }
}
Also used : WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph)

Example 3 with WritableGraph

use of au.gov.asd.tac.constellation.graph.WritableGraph in project constellation by constellation-app.

the class DataAccessUtilities method saveDataAccessState.

/**
 * Build a data access state from the passed tab pane and save it to the graph.
 * <p/>
 * Currently only global parameters are saved.
 *
 * @param tabs the tab pane to build the new data access state from
 * @param graph the active graph to save the state to
 */
public static void saveDataAccessState(final TabPane tabs, final Graph graph) {
    if (graph != null) {
        // Build a new data access state from the passed tabs
        final DataAccessState dataAccessState = new DataAccessState();
        tabs.getTabs().forEach(step -> {
            dataAccessState.newTab();
            DataAccessTabPane.getQueryPhasePane(step).getGlobalParametersPane().getParams().getParameters().entrySet().stream().filter(entry -> entry.getValue().getStringValue() != null).forEach(entry -> dataAccessState.add(entry.getKey(), entry.getValue().getStringValue()));
        });
        // Save the state onto the graph
        WritableGraph wg = null;
        try {
            wg = graph.getWritableGraph("Update Data Access State", true);
            final int dataAccessStateAttribute = DataAccessConcept.MetaAttribute.DATAACCESS_STATE.ensure(wg);
            wg.setObjectValue(dataAccessStateAttribute, 0, dataAccessState);
        } catch (final InterruptedException ex) {
            Exceptions.printStackTrace(ex);
            Thread.currentThread().interrupt();
        } finally {
            if (wg != null) {
                wg.commit();
            }
        }
    }
}
Also used : TopComponent(org.openide.windows.TopComponent) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) DataAccessPane(au.gov.asd.tac.constellation.views.dataaccess.panes.DataAccessPane) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) InvocationTargetException(java.lang.reflect.InvocationTargetException) Graph(au.gov.asd.tac.constellation.graph.Graph) DataAccessState(au.gov.asd.tac.constellation.views.dataaccess.state.DataAccessState) SwingUtilities(javax.swing.SwingUtilities) TabPane(javafx.scene.control.TabPane) Tab(javafx.scene.control.Tab) Exceptions(org.openide.util.Exceptions) WindowManager(org.openide.windows.WindowManager) PluginParameter(au.gov.asd.tac.constellation.plugins.parameters.PluginParameter) Map(java.util.Map) DataAccessViewTopComponent(au.gov.asd.tac.constellation.views.dataaccess.DataAccessViewTopComponent) DataAccessConcept(au.gov.asd.tac.constellation.views.dataaccess.state.DataAccessConcept) DataAccessTabPane(au.gov.asd.tac.constellation.views.dataaccess.components.DataAccessTabPane) DataAccessState(au.gov.asd.tac.constellation.views.dataaccess.state.DataAccessState) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph)

Example 4 with WritableGraph

use of au.gov.asd.tac.constellation.graph.WritableGraph in project constellation by constellation-app.

the class SelectAllPluginNGTest method testEdit0.

/**
 * Test of edit method, of class SelectAllPlugin. select all edit with only
 * attributes on graph
 */
@Test
public void testEdit0() throws Exception {
    System.out.println("select all edit with nothing on graph but attributes");
    // Open a new graph
    graph = new DualGraph(SchemaFactoryUtilities.getSchemaFactory(VisualSchemaFactory.VISUAL_SCHEMA_ID).createSchema());
    int vxCount = 0;
    int txCount = 0;
    final WritableGraph wg = graph.getWritableGraph("TEST", true);
    try {
        // Create Selected Attributes
        selectedV = VisualConcept.VertexAttribute.SELECTED.ensure(wg);
        selectedT = VisualConcept.TransactionAttribute.SELECTED.ensure(wg);
    } finally {
        wg.commit();
    }
    // run select all plugin
    PluginExecution.withPlugin(new SelectAllPlugin()).executeNow(graph);
    // Verify no extra elements are added
    final ReadableGraph rg = graph.getReadableGraph();
    try {
        assertEquals(rg.getVertexCount(), vxCount);
        assertEquals(rg.getTransactionCount(), txCount);
    } finally {
        rg.close();
    }
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) Test(org.testng.annotations.Test)

Example 5 with WritableGraph

use of au.gov.asd.tac.constellation.graph.WritableGraph in project constellation by constellation-app.

the class SelectAllPluginNGTest method testEdit4.

/**
 * Test of edit method, of class SelectAllPlugin. select all edit with a
 * graph that has no selected element via its schema, but contains elements.
 *
 * Should throw a pluginexception
 */
@Test(expectedExceptions = PluginException.class)
public void testEdit4() throws Exception {
    System.out.println("select all edit with no selected attribute on graph, but with elements");
    // Open a new graph
    graph = new DualGraph(SchemaFactoryUtilities.getSchemaFactory(BareSchemaFactory.NAME).createSchema());
    int vxCount = 0;
    int txCount = 0;
    final WritableGraph wg = graph.getWritableGraph("TEST", true);
    try {
        // Add vertices
        vxId1 = wg.addVertex();
        vxId2 = wg.addVertex();
        // Add transactions
        txId1 = wg.addTransaction(vxId1, vxId2, true);
        txId2 = wg.addTransaction(vxId2, vxId1, false);
        vxCount = wg.getVertexCount();
        txCount = wg.getTransactionCount();
        // ensure both values are stored
        assertEquals(vxCount, 2);
        assertEquals(txCount, 2);
    } finally {
        wg.commit();
    }
    // run select all plugin
    PluginExecution.withPlugin(new SelectAllPlugin()).executeNow(graph);
}
Also used : DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) Test(org.testng.annotations.Test)

Aggregations

WritableGraph (au.gov.asd.tac.constellation.graph.WritableGraph)116 Test (org.testng.annotations.Test)77 DualGraph (au.gov.asd.tac.constellation.graph.locking.DualGraph)39 ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)37 Graph (au.gov.asd.tac.constellation.graph.Graph)20 ArrayList (java.util.ArrayList)14 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)9 StoreGraph (au.gov.asd.tac.constellation.graph.StoreGraph)8 CompositeNodeState (au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.CompositeNodeState)8 Plugin (au.gov.asd.tac.constellation.plugins.Plugin)7 BeforeMethod (org.testng.annotations.BeforeMethod)7 GraphElementType (au.gov.asd.tac.constellation.graph.GraphElementType)6 Schema (au.gov.asd.tac.constellation.graph.schema.Schema)6 PluginException (au.gov.asd.tac.constellation.plugins.PluginException)6 Attribute (au.gov.asd.tac.constellation.graph.Attribute)5 GraphAttribute (au.gov.asd.tac.constellation.graph.GraphAttribute)5 CopyToNewGraphPlugin (au.gov.asd.tac.constellation.graph.interaction.plugins.clipboard.CopyToNewGraphPlugin)5 ConstellationColor (au.gov.asd.tac.constellation.utilities.color.ConstellationColor)4 DuplicateKeyException (au.gov.asd.tac.constellation.graph.DuplicateKeyException)3 GraphRecordStore (au.gov.asd.tac.constellation.graph.processing.GraphRecordStore)3