Search in sources :

Example 56 with PluginException

use of au.gov.asd.tac.constellation.plugins.PluginException in project constellation by constellation-app.

the class SimpleQueryPlugin method run.

@Override
public final void run(final PluginGraphs graphs, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
    boolean inControlOfProgress = true;
    final Graph graph = graphs.getGraph();
    // If the graph no longer exists
    if (graph == null) {
        return;
    }
    // Make the graph appear busy
    interaction.setBusy(graph.getId(), true);
    try {
        try {
            boolean cancelled = false;
            Object description = null;
            final ReadableGraph readableGraph = graph.getReadableGraph();
            try {
                interaction.setProgress(0, 0, READING_INTERACTION, true);
                read(readableGraph, interaction, parameters);
                if (!READING_INTERACTION.equals(interaction.getCurrentMessage())) {
                    inControlOfProgress = false;
                }
            } finally {
                readableGraph.release();
            }
            // Wait for all plugins to finish reading and querying
            if (inControlOfProgress) {
                interaction.setProgress(0, 0, "Waiting For Other Plugins...", true);
            }
            // Wait at gate 1 for any CascadingQueryPlugins to finish reading
            graphs.waitAtGate(1);
            if (inControlOfProgress) {
                interaction.setProgress(0, 0, QUERYING_INTERACTION, true);
            }
            query(interaction, parameters);
            if (inControlOfProgress && !QUERYING_INTERACTION.equals(interaction.getCurrentMessage())) {
                inControlOfProgress = false;
            }
            if (inControlOfProgress) {
                interaction.setProgress(0, 0, "Waiting to Edit Graph...", true);
            }
            WritableGraph writableGraph = graph.getWritableGraph(getName(), isSignificant(), this);
            try {
                if (inControlOfProgress) {
                    interaction.setProgress(0, 0, EDITING_INTERACTION, true);
                }
                try {
                    description = describedEdit(writableGraph, interaction, parameters);
                    if (inControlOfProgress && !EDITING_INTERACTION.equals(interaction.getCurrentMessage())) {
                        inControlOfProgress = false;
                    }
                } catch (final Exception e) {
                    // describedEdit raised an exception, cancel the plugin and throw the exception back to the caller for generic handling.
                    cancelled = true;
                    throw e;
                }
            } finally {
                if (cancelled) {
                    writableGraph.rollBack();
                } else {
                    writableGraph.commit(description);
                }
            }
        } finally {
            interaction.setProgress(2, 1, inControlOfProgress ? "Finished" : interaction.getCurrentMessage(), false);
        }
    } finally {
        interaction.setBusy(graph.getId(), false);
    }
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) PluginException(au.gov.asd.tac.constellation.plugins.PluginException)

Example 57 with PluginException

use of au.gov.asd.tac.constellation.plugins.PluginException in project constellation by constellation-app.

the class ConstellationLoggerHelper method addFileStatistics.

/**
 * Enrich with file information
 *
 * @param properties the properties to enrich.
 * @param files a collection of files to include in the properties.
 * @throws PluginException if an anticipated exception occurs.
 */
private static void addFileStatistics(final Properties properties, final Collection<File> files) throws PluginException {
    if (files != null) {
        Integer counter = files.size();
        for (File file : files) {
            properties.setProperty(String.format(STRING_NUMBER_FORMAT, FILE_NAME, counter), file.getAbsolutePath());
            properties.setProperty(String.format(STRING_NUMBER_FORMAT, FILE_SIZE, counter), Long.toString(file.length()));
            try {
                final String hash = createHash(file);
                if (hash != null) {
                    properties.setProperty(String.format(STRING_NUMBER_FORMAT, FILE_HASH, counter), hash);
                }
            } catch (final IOException | NoSuchAlgorithmException ex) {
                throw new PluginException(PluginNotificationLevel.FATAL, "Error creating hash: " + ex.getMessage());
            }
            counter++;
        }
        properties.setProperty(FILES_COUNT, Integer.toString(files.size()));
    }
}
Also used : PluginException(au.gov.asd.tac.constellation.plugins.PluginException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) File(java.io.File)

Example 58 with PluginException

use of au.gov.asd.tac.constellation.plugins.PluginException in project constellation by constellation-app.

the class SmallWorldGraphBuilderPlugin method edit.

@Override
public void edit(final GraphWriteMethods graph, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
    interaction.setProgress(0, 0, "Building...", true);
    final Map<String, PluginParameter<?>> params = parameters.getParameters();
    final int n = params.get(N_PARAMETER_ID).getIntegerValue();
    final int k = params.get(K_PARAMETER_ID).getIntegerValue();
    final float p = params.get(P_PARAMETER_ID).getFloatValue();
    final int t = params.get(T_PARAMETER_ID).getIntegerValue();
    final String buildMode = params.get(BUILD_MODE_PARAMETER_ID).getStringValue();
    final boolean randomWeights = params.get(RANDOM_WEIGHTS_PARAMETER_ID).getBooleanValue();
    final List<String> nodeTypes = params.get(NODE_TYPES_PARAMETER_ID).getMultiChoiceValue().getChoices();
    final List<String> transactionTypes = params.get(TRANSACTION_TYPES_PARAMETER_ID).getMultiChoiceValue().getChoices();
    assert k < n : String.format("[Number of attached neighbours '%s' must be smaller than the number of nodes '%s']", k, n);
    // Random countries to put in the graph
    final List<String> countries = new ArrayList<>();
    countries.add("Australia");
    countries.add("Brazil");
    countries.add("China");
    countries.add("France");
    countries.add("Japan");
    countries.add("New Zealand");
    countries.add("South Africa");
    countries.add("United Arab Emirates");
    countries.add("United Kingdom");
    countries.add("United States");
    final int vxIdentifierAttr = VisualConcept.VertexAttribute.IDENTIFIER.ensure(graph);
    final int vxTypeAttr = AnalyticConcept.VertexAttribute.TYPE.ensure(graph);
    final int vxIsGoodAttr = graph.addAttribute(GraphElementType.VERTEX, BooleanAttributeDescription.ATTRIBUTE_NAME, "isGood", null, false, null);
    final int vxCountryAttr = SpatialConcept.VertexAttribute.COUNTRY.ensure(graph);
    final int vxPinnedAttr = VisualConcept.VertexAttribute.PINNED.ensure(graph);
    final int txIdAttr = VisualConcept.TransactionAttribute.IDENTIFIER.ensure(graph);
    final int txTypeAttr = AnalyticConcept.TransactionAttribute.TYPE.ensure(graph);
    final int txDateTimeAttr = TemporalConcept.TransactionAttribute.DATETIME.ensure(graph);
    final VertexDecorators decorators;
    decorators = new VertexDecorators(graph.getAttributeName(vxCountryAttr), graph.getAttributeName(vxPinnedAttr), null, graph.getAttributeName(vxIsGoodAttr));
    final int decoratorsAttr = VisualConcept.GraphAttribute.DECORATORS.ensure(graph);
    graph.setObjectValue(decoratorsAttr, 0, decorators);
    // Create transactions between the nodes.
    final Date d = new Date();
    final int fourDays = 4 * 24 * 60 * 60 * 1000;
    int initialComponents = 0;
    if (buildMode.equals(CONNECTED)) {
        initialComponents = componentCount(graph);
    }
    for (int s = 1; s <= t; s++) {
        final int[] vxIds = new int[n];
        final Set<Integer> transactions = new HashSet<>();
        int vx = 0;
        while (vx < n) {
            final int vxId = graph.addVertex();
            final String label = "Node_" + vxId;
            graph.setStringValue(vxIdentifierAttr, vxId, label);
            graph.setStringValue(vxTypeAttr, vxId, nodeTypes.get(r.nextInt(nodeTypes.size())));
            graph.setBooleanValue(vxIsGoodAttr, vxId, r.nextInt(10) == 0);
            graph.setStringValue(vxCountryAttr, vxId, countries.get(r.nextInt(countries.size())));
            if (graph.getSchema() != null) {
                graph.getSchema().completeVertex(graph, vxId);
            }
            vxIds[vx] = vxId;
            vx++;
            if (Thread.interrupted()) {
                throw new InterruptedException();
            }
        }
        for (int j = 1; j < (k / 2) + 1; j++) {
            final List<Integer> destinations = new ArrayList<>();
            for (int i = j; i < n; i++) {
                destinations.add(vxIds[i]);
            }
            for (int i = 0; i < j; i++) {
                destinations.add(vxIds[i]);
            }
            for (int z = 0; z < n; z++) {
                final int e = graph.addTransaction(vxIds[z], destinations.get(z), true);
                transactions.add(e);
            }
            if (Thread.interrupted()) {
                throw new InterruptedException();
            }
        }
        for (int j = 1; j < (k / 2) + 1; j++) {
            final List<Integer> destinations = new ArrayList<>();
            for (int i = j; i < n; i++) {
                destinations.add(vxIds[i]);
            }
            for (int i = 0; i < j; i++) {
                destinations.add(vxIds[i]);
            }
            for (int z = 0; z < n; z++) {
                final int u = vxIds[z];
                final int v = destinations.get(z);
                if (r.nextDouble() < p) {
                    int w = vxIds[r.nextInt(n)];
                    boolean skip = false;
                    while (w == u || graph.getLink(u, w) != Graph.NOT_FOUND) {
                        w = vxIds[r.nextInt(n)];
                        if (graph.getVertexNeighbourCount(u) >= n - 1) {
                            skip = true;
                            break;
                        }
                        if (Thread.interrupted()) {
                            throw new InterruptedException();
                        }
                    }
                    if (!skip) {
                        if ("Newman".equals(buildMode)) {
                            final int exId = graph.getLinkTransaction(graph.getLink(u, v), 0);
                            graph.removeTransaction(exId);
                            transactions.remove(exId);
                        }
                        final int e = graph.addTransaction(u, w, true);
                        transactions.add(e);
                    }
                }
            }
            if (Thread.interrupted()) {
                throw new InterruptedException();
            }
        }
        for (final int txId : transactions) {
            final int reciprocity = r.nextInt(3);
            int numTimes = 1;
            if (randomWeights) {
                numTimes = r.nextInt(1 + r.nextInt(100));
            }
            for (int i = 0; i < numTimes; i++) {
                int sxId = graph.getTransactionSourceVertex(txId);
                int dxId = graph.getTransactionDestinationVertex(txId);
                if (randomWeights) {
                    switch(reciprocity) {
                        case 0:
                            final boolean random0 = r.nextBoolean();
                            if (random0) {
                                sxId = graph.getTransactionDestinationVertex(txId);
                                dxId = graph.getTransactionSourceVertex(txId);
                            }
                            break;
                        case 1:
                            final int random1 = r.nextInt(5);
                            if (random1 == 0) {
                                sxId = graph.getTransactionDestinationVertex(txId);
                                dxId = graph.getTransactionSourceVertex(txId);
                            }
                            break;
                        default:
                            final int randomDefault = r.nextInt(5);
                            if (randomDefault != 0) {
                                sxId = graph.getTransactionDestinationVertex(txId);
                                dxId = graph.getTransactionSourceVertex(txId);
                            }
                            break;
                    }
                }
                final int e = graph.addTransaction(sxId, dxId, true);
                graph.setLongValue(txDateTimeAttr, e, d.getTime() - r.nextInt(fourDays));
                graph.setStringValue(txTypeAttr, e, transactionTypes.get(r.nextInt(transactionTypes.size())));
                graph.setIntValue(txIdAttr, e, e);
                if (graph.getSchema() != null) {
                    graph.getSchema().completeTransaction(graph, e);
                }
                if (Thread.interrupted()) {
                    throw new InterruptedException();
                }
            }
            graph.removeTransaction(txId);
        }
        if (buildMode.equals(CONNECTED) && componentCount(graph) != initialComponents + 1 && s != t) {
            for (final int vxId : vxIds) {
                graph.removeVertex(vxId);
            }
        } else {
            break;
        }
    }
    try {
        if (n < 10000) {
            // Do a trees layout.
            PluginExecutor.startWith(ArrangementPluginRegistry.TREES).followedBy(InteractiveGraphPluginRegistry.RESET_VIEW).executeNow(graph);
        } else {
            // Do a grid layout.
            PluginExecutor.startWith(ArrangementPluginRegistry.GRID_COMPOSITE).followedBy(InteractiveGraphPluginRegistry.RESET_VIEW).executeNow(graph);
        }
    } catch (final PluginException ex) {
        LOGGER.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    }
    interaction.setProgress(1, 0, "Completed successfully", true);
}
Also used : VertexDecorators(au.gov.asd.tac.constellation.graph.schema.visual.VertexDecorators) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) ArrayList(java.util.ArrayList) Date(java.util.Date) PluginParameter(au.gov.asd.tac.constellation.plugins.parameters.PluginParameter) HashSet(java.util.HashSet)

Example 59 with PluginException

use of au.gov.asd.tac.constellation.plugins.PluginException in project constellation by constellation-app.

the class SphereGraphBuilderPlugin method edit.

@Override
public void edit(final GraphWriteMethods graph, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
    interaction.setProgress(0, 0, "Building...", true);
    // The default icon.
    // 
    final String DEFAULT_ICON = "HAL-9000";
    // The first of a sequence of random characters..
    // 
    final int PLAY_CHARS = 0x261A;
    // The number of random characters.
    // 
    final int PLAY_CHARS_LEN = 86;
    final int vertexCount = graph.getVertexCount();
    // Parameter values.
    // 
    final Map<String, PluginParameter<?>> params = parameters.getParameters();
    final int nVx = Math.max(params.get(N_PARAMETER_ID).getIntegerValue() - 6, 0);
    final int nTx = params.get(T_PARAMETER_ID).getIntegerValue();
    final String option = params.get(OPTION_PARAMETER_ID).getStringValue();
    final boolean addChars = params.get(ADD_CHARS_PARAMETER_ID).getBooleanValue();
    final boolean useLabels = params.get(USE_LABELS_PARAMETER_ID).getBooleanValue();
    final boolean allDisplayableChars = params.get(USE_ALL_DISPLAYABLE_CHARS_PARAMETER_ID).getBooleanValue();
    final boolean drawManyTx = params.get(DRAW_MANY_TX_PARAMETER_ID).getBooleanValue();
    final boolean drawManyDecorators = params.get(DRAW_MANY_DECORATORS_PARAMETER_ID).getBooleanValue();
    final boolean useRandomIcons = params.get(USE_RANDOM_ICONS_PARAMETER_ID).getBooleanValue();
    final boolean explicitLoops = params.get(EXPLICIT_LOOPS).getBooleanValue();
    // select some icons to put in the graph
    final List<String> iconLabels = new ArrayList<>();
    if (useRandomIcons) {
        IconManager.getIconNames(null).forEach(iconLabels::add);
    }
    // Select some countries to put in the graph.
    // Since it's entirely possible that the country names will be used for decorators,
    // we only want countries that have flags.
    // Therefore we'll look for flags, not countries.
    // 
    final List<String> countries = IconManager.getIcons().stream().filter(icon -> icon.getExtendedName().startsWith("Flag.")).map(icon -> icon.getName()).collect(Collectors.toUnmodifiableList());
    final int maxTxId = VisualConcept.GraphAttribute.MAX_TRANSACTIONS.ensure(graph);
    if (drawManyTx) {
        graph.setIntValue(maxTxId, 0, 100);
    }
    final int vxLabelAttr = VisualConcept.VertexAttribute.LABEL.ensure(graph);
    final int vxIdentifierAttr = VisualConcept.VertexAttribute.IDENTIFIER.ensure(graph);
    final int vxTypeAttr = AnalyticConcept.VertexAttribute.TYPE.ensure(graph);
    final int vxRadiusAttr = VisualConcept.VertexAttribute.NODE_RADIUS.ensure(graph);
    final int vxSelectedAttr = VisualConcept.VertexAttribute.SELECTED.ensure(graph);
    final int vxDimmedAttr = VisualConcept.VertexAttribute.DIMMED.ensure(graph);
    final int vxVisibilityAttr = VisualConcept.VertexAttribute.VISIBILITY.ensure(graph);
    final int vxColorAttr = VisualConcept.VertexAttribute.COLOR.ensure(graph);
    final int vxForegroundIconAttr = VisualConcept.VertexAttribute.FOREGROUND_ICON.ensure(graph);
    final int vxBackgroundIconAttr = VisualConcept.VertexAttribute.BACKGROUND_ICON.ensure(graph);
    final int attrBlaze = VisualConcept.VertexAttribute.BLAZE.ensure(graph);
    final int vxXAttr = VisualConcept.VertexAttribute.X.ensure(graph);
    final int vxYAttr = VisualConcept.VertexAttribute.Y.ensure(graph);
    final int vxZAttr = VisualConcept.VertexAttribute.Z.ensure(graph);
    final int vxX2Attr = VisualConcept.VertexAttribute.X2.ensure(graph);
    final int vxY2Attr = VisualConcept.VertexAttribute.Y2.ensure(graph);
    final int vxZ2Attr = VisualConcept.VertexAttribute.Z2.ensure(graph);
    final int vxIsGoodAttr = graph.addAttribute(GraphElementType.VERTEX, BooleanAttributeDescription.ATTRIBUTE_NAME, "isGood", null, false, null);
    final int vxPinnedAttr = VisualConcept.VertexAttribute.PINNED.ensure(graph);
    final int vxCountry1Attr = SpatialConcept.VertexAttribute.COUNTRY.ensure(graph);
    final int vxCountry2Attr = graph.addAttribute(GraphElementType.VERTEX, StringAttributeDescription.ATTRIBUTE_NAME, "Geo.Country2", null, null, null);
    final int vxDecoratorAttr = graph.addAttribute(GraphElementType.VERTEX, StringAttributeDescription.ATTRIBUTE_NAME, "Custom Decorator", null, null, null);
    final int vxNormalisedAttr = graph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, "Normalised", null, 0.0F, null);
    final int txIdAttr = VisualConcept.TransactionAttribute.IDENTIFIER.ensure(graph);
    final int txDirectedAttr = VisualConcept.TransactionAttribute.DIRECTED.ensure(graph);
    final int txWidthAttr = VisualConcept.TransactionAttribute.WIDTH.ensure(graph);
    final int txDimmedAttr = VisualConcept.TransactionAttribute.DIMMED.ensure(graph);
    final int txVisibilityAttr = VisualConcept.TransactionAttribute.VISIBILITY.ensure(graph);
    final int txColorAttr = VisualConcept.TransactionAttribute.COLOR.ensure(graph);
    final int txLineStyleAttr = VisualConcept.TransactionAttribute.LINE_STYLE.ensure(graph);
    final int txDateTimeAttr = TemporalConcept.TransactionAttribute.DATETIME.ensure(graph);
    // Add various labels and decorators.
    final List<GraphLabel> bottomLabels = new ArrayList<>();
    final List<GraphLabel> topLabels = new ArrayList<>();
    final List<GraphLabel> transactionLabels = new ArrayList<>();
    if (useLabels) {
        bottomLabels.add(new GraphLabel(VisualConcept.VertexAttribute.IDENTIFIER.getName(), ConstellationColor.WHITE));
        bottomLabels.add(new GraphLabel(VisualConcept.VertexAttribute.FOREGROUND_ICON.getName(), ConstellationColor.DARK_GREEN, 0.5F));
        topLabels.add(new GraphLabel(AnalyticConcept.VertexAttribute.TYPE.getName(), ConstellationColor.MAGENTA, 0.5F));
        topLabels.add(new GraphLabel(SpatialConcept.VertexAttribute.COUNTRY.getName(), ConstellationColor.DARK_ORANGE, 0.5F));
        transactionLabels.add(new GraphLabel(VisualConcept.TransactionAttribute.VISIBILITY.getName(), ConstellationColor.LIGHT_GREEN));
    }
    final VertexDecorators decorators;
    if (drawManyDecorators) {
        decorators = new VertexDecorators(graph.getAttributeName(vxIsGoodAttr), graph.getAttributeName(vxPinnedAttr), SpatialConcept.VertexAttribute.COUNTRY.getName(), graph.getAttributeName(vxDecoratorAttr));
    } else {
        decorators = new VertexDecorators(graph.getAttributeName(vxIsGoodAttr), graph.getAttributeName(vxPinnedAttr), null, null);
    }
    final int bottomLabelsAttr = VisualConcept.GraphAttribute.BOTTOM_LABELS.ensure(graph);
    final int topLabelsAttr = VisualConcept.GraphAttribute.TOP_LABELS.ensure(graph);
    final int transactionLabelsAttr = VisualConcept.GraphAttribute.TRANSACTION_LABELS.ensure(graph);
    graph.setObjectValue(bottomLabelsAttr, 0, new GraphLabels(bottomLabels));
    graph.setObjectValue(topLabelsAttr, 0, new GraphLabels(topLabels));
    graph.setObjectValue(transactionLabelsAttr, 0, new GraphLabels(transactionLabels));
    final int decoratorsAttr = VisualConcept.GraphAttribute.DECORATORS.ensure(graph);
    graph.setObjectValue(decoratorsAttr, 0, decorators);
    final ConstellationColor.PalettePhiIterator palette = new ConstellationColor.PalettePhiIterator(0, 0.75F, 0.75F);
    // Displayable characters for type.
    int c = 33;
    final Font font = FontUtilities.getOutputFont();
    final StringBuilder type = new StringBuilder();
    final int[] vxIds = new int[nVx];
    int vx = 0;
    while (vx < nVx) {
        final float v = nVx > 1 ? vx / (float) (nVx - 1) : 1F;
        final int vxId = graph.addVertex();
        // A label with a random CJK glyph.
        final String chars;
        if (addChars && vx == 0) {
            // "Hello world" in arabic, demonstrates unicode rendering
            chars = "مرحبا عالم";
        } else if (addChars) {
            chars = " " + (char) (PLAY_CHARS + random.nextInt(PLAY_CHARS_LEN)) + " " + (char) (0x4e00 + random.nextInt(256));
        } else {
            chars = "";
        }
        final String label = "Node " + vxId + chars;
        if (allDisplayableChars) {
            type.setLength(0);
            while (type.length() < 10) {
                if (font.canDisplay(c)) {
                    type.append((char) c);
                }
                // Unicode BMP only has so many characters.
                c = (c + 1) % 65536;
            }
        }
        graph.setStringValue(vxLabelAttr, vxId, label);
        graph.setStringValue(vxIdentifierAttr, vxId, String.valueOf(vxId));
        graph.setStringValue(vxTypeAttr, vxId, type.toString());
        graph.setFloatValue(vxRadiusAttr, vxId, 1);
        graph.setFloatValue(vxVisibilityAttr, vxId, v);
        graph.setObjectValue(vxColorAttr, vxId, palette.next());
        if (useRandomIcons) {
            graph.setStringValue(vxForegroundIconAttr, vxId, iconLabels.get(random.nextInt(iconLabels.size())));
            graph.setStringValue(vxDecoratorAttr, vxId, iconLabels.get(random.nextInt(iconLabels.size())));
        } else {
            // If icons are non-random, make the first one null.
            graph.setStringValue(vxForegroundIconAttr, vxId, DEFAULT_ICON);
            graph.setStringValue(vxDecoratorAttr, vxId, null);
        }
        graph.setStringValue(vxBackgroundIconAttr, vxId, (useRandomIcons && random.nextBoolean()) ? "Background.Flat Square" : "Background.Flat Circle");
        if (vx == 0) {
            graph.setStringValue(vxForegroundIconAttr, vxId, null);
            graph.setObjectValue(attrBlaze, vxId, new Blaze(45, ConstellationColor.LIGHT_BLUE));
        }
        graph.setBooleanValue(vxIsGoodAttr, vxId, vx % 2 == 0);
        graph.setStringValue(vxCountry1Attr, vxId, countries.get(vx % countries.size()));
        graph.setStringValue(vxCountry2Attr, vxId, countries.get((vx + 1) % countries.size()));
        graph.setFloatValue(vxNormalisedAttr, vxId, random.nextFloat());
        vxIds[vx] = vxId;
        vx++;
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }
    }
    // Dimmed nodes.
    if (nVx == 2) {
        graph.setBooleanValue(vxDimmedAttr, vxIds[nVx - 1], true);
    } else if (nVx > 2) {
        graph.setBooleanValue(vxDimmedAttr, vxIds[nVx - 1], true);
        graph.setBooleanValue(vxDimmedAttr, vxIds[nVx - 2], true);
    } else {
    // Do nothing
    }
    if (nVx > 0) {
        // Create transactions between the nodes.
        final Date d = new Date();
        final int fourDays = 4 * 24 * 60 * 60 * 1000;
        if ("Random vertices".equals(option)) {
            // Draw some lines between random nodes, but don't draw multiple lines between the same two nodes.
            for (int i = 0; i < nTx; i++) {
                // Choose random positions, convert to correct vxIds.
                // Concentrate most sources to just a few vertices; it looks a bit nicer than plain random.
                int fromPosition = vertexCount + (random.nextFloat() < 0.9F ? random.nextInt((int) (Math.log10(nVx) * 5) + 1) : random.nextInt(nVx));
                int toPosition;
                do {
                    toPosition = vertexCount + random.nextInt(nVx);
                } while (toPosition == fromPosition);
                assert fromPosition != toPosition;
                int fromVx = graph.getVertex(fromPosition);
                int toVx = graph.getVertex(toPosition);
                // Always draw from the lower numbered vertex to the higher numbered vertex.
                if (toVx < fromVx) {
                    int tmp = fromVx;
                    fromVx = toVx;
                    toVx = tmp;
                }
                final int e = graph.addTransaction(fromVx, toVx, true);
                graph.setLongValue(txDateTimeAttr, e, d.getTime() - random.nextInt(fourDays));
                graph.setIntValue(txIdAttr, e, e);
                graph.setObjectValue(txColorAttr, e, randomColor3(random));
                graph.setFloatValue(txVisibilityAttr, e, (float) i / (nTx - 1));
                if (Thread.interrupted()) {
                    throw new InterruptedException();
                }
            }
        } else {
            if ("1 path, random vertices".equals(option)) {
                // Shuffle the vertices.
                for (int i = nVx - 1; i > 0; i--) {
                    final int ix = random.nextInt(i);
                    final int t = vxIds[i];
                    vxIds[i] = vxIds[ix];
                    vxIds[ix] = t;
                }
            }
            // Transactions from/to each vertex in ascending order, modulo nVx.
            for (int i = 0; i < nTx; i++) {
                final int fromNode = vxIds[i % nVx];
                final int toNode = vxIds[(i + 1) % nVx];
                final int e = graph.addTransaction(fromNode, toNode, true);
                graph.setLongValue(txDateTimeAttr, e, d.getTime() - random.nextInt(fourDays));
                graph.setIntValue(txIdAttr, e, e);
                graph.setObjectValue(txColorAttr, e, randomColor3(random));
                graph.setFloatValue(txVisibilityAttr, e, (float) i / (nTx - 1));
                if (Thread.interrupted()) {
                    throw new InterruptedException();
                }
            }
        }
    }
    // Do a spherical layout.
    try {
        PluginExecution.withPlugin(ArrangementPluginRegistry.SPHERE).executeNow(graph);
    } catch (final PluginException ex) {
        LOGGER.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    }
    // Find out where the extremes of the new sphere are and select every 10th vertex.
    // Provide a lower limit on the radius in case the existing nodes are too close together.
    // 
    final BBoxf box = new BBoxf();
    box.add(8, 8, 8);
    box.add(-8, -8, -8);
    for (int position = 0; position < nVx; position++) {
        final int vxId = graph.getVertex(position);
        final float x = graph.getFloatValue(vxXAttr, vxId);
        final float y = graph.getFloatValue(vxYAttr, vxId);
        final float z = graph.getFloatValue(vxZAttr, vxId);
        box.add(x, y, z);
        graph.setBooleanValue(vxSelectedAttr, vxId, vxId % 10 == 0);
    }
    // Create nodes just outside the centres of the bounding box's sides.
    final float[] min = box.getMin();
    final float[] max = box.getMax();
    if (box.isEmpty()) {
        Arrays.fill(min, 0);
        Arrays.fill(max, 0);
    }
    char circleChar = 0x2460;
    final int nodeRadius = 3;
    final int vx0 = graph.addVertex();
    ConstructionUtilities.setxyz(graph, vx0, vxXAttr, vxYAttr, vxZAttr, min[BBoxf.X], max[BBoxf.Y], max[BBoxf.Z]);
    ConstructionUtilities.setxyz(graph, vx0, vxX2Attr, vxY2Attr, vxZ2Attr, min[BBoxf.X] / 2, max[BBoxf.Y] / 2, max[BBoxf.Z] / 2);
    graph.setStringValue(vxLabelAttr, vx0, NODE + vx0 + " " + circleChar++);
    graph.setStringValue(vxIdentifierAttr, vx0, String.valueOf(vx0));
    graph.setStringValue(vxTypeAttr, vx0, TYPE + vx0);
    graph.setFloatValue(vxRadiusAttr, vx0, nodeRadius);
    graph.setFloatValue(vxVisibilityAttr, vx0, 1);
    graph.setObjectValue(vxColorAttr, vx0, randomColor3(random));
    graph.setStringValue(vxForegroundIconAttr, vx0, DEFAULT_ICON);
    graph.setStringValue(vxBackgroundIconAttr, vx0, BACKGROUND_ROUND_CIRCLE);
    graph.setStringValue(vxCountry1Attr, vx0, countries.get(vx0 % countries.size()));
    graph.setStringValue(vxCountry2Attr, vx0, countries.get((vx0 + 1) % countries.size()));
    graph.setFloatValue(vxNormalisedAttr, vx0, random.nextFloat());
    graph.setObjectValue(attrBlaze, vx0, new Blaze(random.nextInt(360), randomColor3(random)));
    final int vx1 = graph.addVertex();
    ConstructionUtilities.setxyz(graph, vx1, vxXAttr, vxYAttr, vxZAttr, max[BBoxf.X], max[BBoxf.Y], max[BBoxf.Z]);
    ConstructionUtilities.setxyz(graph, vx1, vxX2Attr, vxY2Attr, vxZ2Attr, max[BBoxf.X] / 2, max[BBoxf.Y] / 2, max[BBoxf.Z] / 2);
    graph.setStringValue(vxLabelAttr, vx1, NODE + vx1 + " " + circleChar++);
    graph.setStringValue(vxIdentifierAttr, vx1, String.valueOf(vx1));
    graph.setStringValue(vxTypeAttr, vx1, TYPE + vx1);
    graph.setFloatValue(vxRadiusAttr, vx1, nodeRadius);
    graph.setFloatValue(vxVisibilityAttr, vx1, 1);
    graph.setObjectValue(vxColorAttr, vx1, randomColor3(random));
    graph.setStringValue(vxForegroundIconAttr, vx1, DEFAULT_ICON);
    graph.setStringValue(vxBackgroundIconAttr, vx1, BACKGROUND_ROUND_CIRCLE);
    graph.setStringValue(vxCountry1Attr, vx1, countries.get(vx1 % countries.size()));
    graph.setStringValue(vxCountry2Attr, vx1, countries.get((vx1 + 1) % countries.size()));
    graph.setFloatValue(vxNormalisedAttr, vx1, random.nextFloat());
    graph.setObjectValue(attrBlaze, vx1, new Blaze(random.nextInt(360), randomColor3(random)));
    final int vx2 = graph.addVertex();
    ConstructionUtilities.setxyz(graph, vx2, vxXAttr, vxYAttr, vxZAttr, max[BBoxf.X], min[BBoxf.Y], max[BBoxf.Z]);
    ConstructionUtilities.setxyz(graph, vx2, vxX2Attr, vxY2Attr, vxZ2Attr, max[BBoxf.X] / 2, min[BBoxf.Y] / 2, max[BBoxf.Z] / 2);
    graph.setStringValue(vxLabelAttr, vx2, NODE + vx2 + " " + circleChar++);
    graph.setStringValue(vxIdentifierAttr, vx2, String.valueOf(vx2));
    graph.setStringValue(vxTypeAttr, vx2, TYPE + vx2);
    graph.setFloatValue(vxRadiusAttr, vx2, nodeRadius);
    graph.setFloatValue(vxVisibilityAttr, vx2, 1);
    graph.setObjectValue(vxColorAttr, vx2, randomColor3(random));
    graph.setStringValue(vxForegroundIconAttr, vx2, DEFAULT_ICON);
    graph.setStringValue(vxBackgroundIconAttr, vx2, BACKGROUND_ROUND_CIRCLE);
    graph.setStringValue(vxCountry1Attr, vx2, countries.get(vx2 % countries.size()));
    graph.setStringValue(vxCountry2Attr, vx2, countries.get((vx2 + 1) % countries.size()));
    graph.setFloatValue(vxNormalisedAttr, vx2, random.nextFloat());
    graph.setObjectValue(attrBlaze, vx2, new Blaze(random.nextInt(360), randomColor3(random)));
    final int vx3 = graph.addVertex();
    ConstructionUtilities.setxyz(graph, vx3, vxXAttr, vxYAttr, vxZAttr, min[BBoxf.X], min[BBoxf.Y], min[BBoxf.Z]);
    ConstructionUtilities.setxyz(graph, vx3, vxX2Attr, vxY2Attr, vxZ2Attr, min[BBoxf.X] / 2, min[BBoxf.Y] / 2, min[BBoxf.Z] / 2);
    graph.setStringValue(vxLabelAttr, vx3, NODE + vx3 + " " + circleChar++);
    graph.setStringValue(vxIdentifierAttr, vx3, String.valueOf(vx3));
    graph.setStringValue(vxTypeAttr, vx3, TYPE + vx3);
    graph.setFloatValue(vxRadiusAttr, vx3, nodeRadius);
    graph.setFloatValue(vxVisibilityAttr, vx3, 1);
    graph.setObjectValue(vxColorAttr, vx3, randomColor3(random));
    graph.setStringValue(vxForegroundIconAttr, vx3, DEFAULT_ICON);
    graph.setStringValue(vxBackgroundIconAttr, vx2, BACKGROUND_ROUND_CIRCLE);
    graph.setStringValue(vxCountry1Attr, vx3, countries.get(vx3 % countries.size()));
    graph.setStringValue(vxCountry2Attr, vx3, countries.get((vx3 + 1) % countries.size()));
    graph.setFloatValue(vxNormalisedAttr, vx3, random.nextFloat());
    graph.setObjectValue(attrBlaze, vx3, new Blaze(random.nextInt(360), randomColor3(random)));
    final int vx4 = graph.addVertex();
    ConstructionUtilities.setxyz(graph, vx4, vxXAttr, vxYAttr, vxZAttr, min[BBoxf.X], max[BBoxf.Y], min[BBoxf.Z]);
    ConstructionUtilities.setxyz(graph, vx4, vxX2Attr, vxY2Attr, vxZ2Attr, min[BBoxf.X] / 2, max[BBoxf.Y] / 2, min[BBoxf.Z] / 2);
    graph.setStringValue(vxLabelAttr, vx4, NODE + vx4 + " " + circleChar++);
    graph.setStringValue(vxIdentifierAttr, vx4, String.valueOf(vx4));
    graph.setStringValue(vxTypeAttr, vx4, TYPE + vx4);
    graph.setFloatValue(vxRadiusAttr, vx4, nodeRadius);
    graph.setFloatValue(vxVisibilityAttr, vx4, 1);
    graph.setObjectValue(vxColorAttr, vx4, randomColor3(random));
    graph.setStringValue(vxForegroundIconAttr, vx4, DEFAULT_ICON);
    graph.setStringValue(vxBackgroundIconAttr, vx2, BACKGROUND_ROUND_CIRCLE);
    graph.setStringValue(vxCountry1Attr, vx4, countries.get(vx4 % countries.size()));
    graph.setStringValue(vxCountry2Attr, vx4, countries.get((vx4 + 1) % countries.size()));
    graph.setFloatValue(vxNormalisedAttr, vx4, random.nextFloat());
    graph.setObjectValue(attrBlaze, vx4, new Blaze(random.nextInt(360), randomColor3(random)));
    final int vx5 = graph.addVertex();
    ConstructionUtilities.setxyz(graph, vx5, vxXAttr, vxYAttr, vxZAttr, max[BBoxf.X], max[BBoxf.Y], min[BBoxf.Z]);
    ConstructionUtilities.setxyz(graph, vx5, vxX2Attr, vxY2Attr, vxZ2Attr, max[BBoxf.X] / 2, max[BBoxf.Y] / 2, min[BBoxf.Z] / 2);
    graph.setStringValue(vxLabelAttr, vx5, NODE + vx5 + " " + circleChar++);
    graph.setStringValue(vxIdentifierAttr, vx5, String.valueOf(vx5));
    graph.setStringValue(vxTypeAttr, vx5, TYPE + vx5);
    graph.setFloatValue(vxRadiusAttr, vx5, nodeRadius);
    graph.setFloatValue(vxVisibilityAttr, vx5, 1);
    graph.setObjectValue(vxColorAttr, vx5, randomColor3(random));
    graph.setStringValue(vxForegroundIconAttr, vx5, DEFAULT_ICON);
    graph.setStringValue(vxBackgroundIconAttr, vx2, BACKGROUND_ROUND_CIRCLE);
    graph.setStringValue(vxCountry1Attr, vx5, countries.get(vx5 % countries.size()));
    graph.setStringValue(vxCountry2Attr, vx5, countries.get((vx5 + 1) % countries.size()));
    graph.setFloatValue(vxNormalisedAttr, vx5, random.nextFloat());
    graph.setObjectValue(attrBlaze, vx5, new Blaze(random.nextInt(360), randomColor3(random)));
    // Draw multiple lines with offsets between two fixed nodes.
    int txId;
    // Too many transactions to draw; different colors.
    final int lim1 = 16;
    for (int i = 0; i < lim1; i++) {
        final ConstellationColor rgb = ConstellationColor.getColorValue((float) i / lim1, 0, 1.0F - (float) i / lim1, 1F);
        if (i % 2 == 0) {
            txId = graph.addTransaction(vx0, vx1, true);
        } else {
            txId = graph.addTransaction(vx1, vx0, true);
        }
        graph.setIntValue(txIdAttr, txId, txId);
        graph.setObjectValue(txColorAttr, txId, rgb);
        graph.setFloatValue(txVisibilityAttr, txId, 1);
    }
    // Not too many transactions to draw; different colors.
    final int lim2 = 8;
    for (int i = 0; i < lim2; i++) {
        final ConstellationColor rgb = ConstellationColor.getColorValue(0.25F, 1.0F - (float) i / lim2, (float) i / lim2, 1F);
        if (i % 2 == 0) {
            txId = graph.addTransaction(vx1, vx2, true);
        } else {
            txId = graph.addTransaction(vx2, vx1, true);
        }
        graph.setIntValue(txIdAttr, txId, txId);
        graph.setObjectValue(txColorAttr, txId, rgb);
        graph.setFloatValue(txVisibilityAttr, txId, (i + 1) / (float) lim2);
        graph.setFloatValue(txWidthAttr, txId, 2F);
    }
    // Too many transactions to draw: same color.
    final int lim3 = 9;
    final ConstellationColor rgb3 = ConstellationColor.ORANGE;
    for (int i = 0; i < lim3; i++) {
        if (i == 0) {
            txId = graph.addTransaction(vx3, vx4, true);
            graph.setFloatValue(txVisibilityAttr, txId, 0.3F);
        } else if (i < 4) {
            txId = graph.addTransaction(vx3, vx4, false);
            graph.setFloatValue(txVisibilityAttr, txId, 0.6F);
        } else {
            txId = graph.addTransaction(vx4, vx3, true);
            graph.setFloatValue(txVisibilityAttr, txId, 0.9F);
        }
        graph.setIntValue(txIdAttr, txId, txId);
        graph.setObjectValue(txColorAttr, txId, rgb3);
    }
    // Undirected transactions.
    txId = graph.addTransaction(vx4, vx5, false);
    graph.setIntValue(txIdAttr, txId, txId);
    graph.setObjectValue(txColorAttr, txId, ConstellationColor.getColorValue(1F, 0F, 1F, 1F));
    graph.setBooleanValue(txDirectedAttr, txId, false);
    graph.setObjectValue(txLineStyleAttr, txId, LineStyle.DASHED);
    txId = graph.addTransaction(vx5, vx4, false);
    graph.setIntValue(txIdAttr, txId, txId);
    graph.setObjectValue(txColorAttr, txId, ConstellationColor.getColorValue(1F, 1F, 0F, 1F));
    graph.setBooleanValue(txDirectedAttr, txId, false);
    graph.setObjectValue(txLineStyleAttr, txId, LineStyle.DOTTED);
    // Directed diamond.
    txId = graph.addTransaction(vx1, vx5, true);
    graph.setIntValue(txIdAttr, txId, txId);
    graph.setObjectValue(txColorAttr, txId, ConstellationColor.PINK);
    graph.setObjectValue(txLineStyleAttr, txId, LineStyle.DIAMOND);
    // Loops.
    if (explicitLoops) {
        txId = graph.addTransaction(vx2, vx2, true);
        graph.setIntValue(txIdAttr, txId, txId);
        graph.setObjectValue(txColorAttr, txId, ConstellationColor.PINK);
        txId = graph.addTransaction(vx4, vx4, false);
        graph.setIntValue(txIdAttr, txId, txId);
        graph.setBooleanValue(txDirectedAttr, txId, false);
        graph.setObjectValue(txColorAttr, txId, ConstellationColor.LIGHT_BLUE);
        txId = graph.addTransaction(vx5, vx5, true);
        graph.setIntValue(txIdAttr, txId, txId);
        graph.setObjectValue(txColorAttr, txId, ConstellationColor.ORANGE);
        txId = graph.addTransaction(vx5, vx5, false);
        graph.setIntValue(txIdAttr, txId, txId);
        graph.setObjectValue(txColorAttr, txId, ConstellationColor.GREEN);
    }
    // Dimmed transactions.
    txId = graph.addTransaction(vx0, vx5, true);
    graph.setObjectValue(txColorAttr, txId, ConstellationColor.RED);
    graph.setBooleanValue(txDimmedAttr, txId, true);
    txId = graph.addTransaction(vx0, vx5, false);
    graph.setObjectValue(txColorAttr, txId, ConstellationColor.GREEN);
    graph.setBooleanValue(txDirectedAttr, txId, false);
    graph.setBooleanValue(txDimmedAttr, txId, true);
    txId = graph.addTransaction(vx5, vx0, true);
    graph.setObjectValue(txColorAttr, txId, ConstellationColor.BLUE);
    graph.setBooleanValue(txDimmedAttr, txId, true);
    PluginExecution.withPlugin(InteractiveGraphPluginRegistry.RESET_VIEW).executeNow(graph);
    interaction.setProgress(1, 0, "Completed successfully", true);
}
Also used : GraphWriteMethods(au.gov.asd.tac.constellation.graph.GraphWriteMethods) Arrays(java.util.Arrays) Blaze(au.gov.asd.tac.constellation.graph.schema.visual.attribute.objects.Blaze) IntegerParameterValue(au.gov.asd.tac.constellation.plugins.parameters.types.IntegerParameterType.IntegerParameterValue) Date(java.util.Date) SingleChoiceParameterType(au.gov.asd.tac.constellation.plugins.parameters.types.SingleChoiceParameterType) SimpleEditPlugin(au.gov.asd.tac.constellation.plugins.templates.SimpleEditPlugin) StringAttributeDescription(au.gov.asd.tac.constellation.graph.attribute.StringAttributeDescription) SpatialConcept(au.gov.asd.tac.constellation.graph.schema.analytic.concept.SpatialConcept) PluginType(au.gov.asd.tac.constellation.plugins.PluginType) VisualConcept(au.gov.asd.tac.constellation.graph.schema.visual.concept.VisualConcept) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) BBoxf(au.gov.asd.tac.constellation.graph.visual.graphics.BBoxf) SecureRandom(java.security.SecureRandom) ArrangementPluginRegistry(au.gov.asd.tac.constellation.plugins.arrangements.ArrangementPluginRegistry) FontUtilities(au.gov.asd.tac.constellation.utilities.font.FontUtilities) BooleanAttributeDescription(au.gov.asd.tac.constellation.graph.attribute.BooleanAttributeDescription) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) PluginInteraction(au.gov.asd.tac.constellation.plugins.PluginInteraction) ServiceProviders(org.openide.util.lookup.ServiceProviders) PluginParameter(au.gov.asd.tac.constellation.plugins.parameters.PluginParameter) Map(java.util.Map) GraphLabel(au.gov.asd.tac.constellation.graph.schema.visual.GraphLabel) ServiceProvider(org.openide.util.lookup.ServiceProvider) PluginTags(au.gov.asd.tac.constellation.plugins.templates.PluginTags) PluginExecution(au.gov.asd.tac.constellation.plugins.PluginExecution) GraphLabels(au.gov.asd.tac.constellation.graph.schema.visual.GraphLabels) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) IconManager(au.gov.asd.tac.constellation.utilities.icon.IconManager) InteractiveGraphPluginRegistry(au.gov.asd.tac.constellation.graph.interaction.InteractiveGraphPluginRegistry) ConstellationColor(au.gov.asd.tac.constellation.utilities.color.ConstellationColor) Font(java.awt.Font) GraphElementType(au.gov.asd.tac.constellation.graph.GraphElementType) BooleanParameterValue(au.gov.asd.tac.constellation.plugins.parameters.types.BooleanParameterType.BooleanParameterValue) Logger(java.util.logging.Logger) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) Collectors(java.util.stream.Collectors) BooleanParameterType(au.gov.asd.tac.constellation.plugins.parameters.types.BooleanParameterType) PluginInfo(au.gov.asd.tac.constellation.plugins.PluginInfo) FloatAttributeDescription(au.gov.asd.tac.constellation.graph.attribute.FloatAttributeDescription) List(java.util.List) AnalyticConcept(au.gov.asd.tac.constellation.graph.schema.analytic.concept.AnalyticConcept) IntegerParameterType(au.gov.asd.tac.constellation.plugins.parameters.types.IntegerParameterType) TemporalConcept(au.gov.asd.tac.constellation.graph.schema.analytic.concept.TemporalConcept) LineStyle(au.gov.asd.tac.constellation.utilities.visual.LineStyle) Messages(org.openide.util.NbBundle.Messages) VertexDecorators(au.gov.asd.tac.constellation.graph.schema.visual.VertexDecorators) VertexDecorators(au.gov.asd.tac.constellation.graph.schema.visual.VertexDecorators) ArrayList(java.util.ArrayList) Font(java.awt.Font) PluginParameter(au.gov.asd.tac.constellation.plugins.parameters.PluginParameter) GraphLabels(au.gov.asd.tac.constellation.graph.schema.visual.GraphLabels) BBoxf(au.gov.asd.tac.constellation.graph.visual.graphics.BBoxf) GraphLabel(au.gov.asd.tac.constellation.graph.schema.visual.GraphLabel) ConstellationColor(au.gov.asd.tac.constellation.utilities.color.ConstellationColor) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) Date(java.util.Date) Blaze(au.gov.asd.tac.constellation.graph.schema.visual.attribute.objects.Blaze)

Example 60 with PluginException

use of au.gov.asd.tac.constellation.plugins.PluginException in project constellation by constellation-app.

the class PreferentialAttachmentGraphBuilderPlugin method edit.

@Override
public void edit(final GraphWriteMethods graph, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
    interaction.setProgress(0, 0, "Building...", true);
    final Map<String, PluginParameter<?>> params = parameters.getParameters();
    final int n = params.get(N_PARAMETER_ID).getIntegerValue();
    final int m = params.get(M_PARAMETER_ID).getIntegerValue();
    final boolean randomWeights = params.get(RANDOM_WEIGHTS_PARAMETER_ID).getBooleanValue();
    final List<String> nodeTypes = params.get(NODE_TYPES_PARAMETER_ID).getMultiChoiceValue().getChoices();
    final List<String> transactionTypes = params.get(TRANSACTION_TYPES_PARAMETER_ID).getMultiChoiceValue().getChoices();
    assert m >= 1 && m < n : String.format("[Number of edges to attach '%s' must be at least 1 and less than the number of nodes '%s']", m, n);
    // Random countries to put in the graph
    final List<String> countries = new ArrayList<>();
    countries.add("Australia");
    countries.add("Brazil");
    countries.add("China");
    countries.add("France");
    countries.add("Japan");
    countries.add("New Zealand");
    countries.add("South Africa");
    countries.add("United Arab Emirates");
    countries.add("United Kingdom");
    countries.add("United States");
    final int vxIdentifierAttr = VisualConcept.VertexAttribute.IDENTIFIER.ensure(graph);
    final int vxTypeAttr = AnalyticConcept.VertexAttribute.TYPE.ensure(graph);
    final int vxIsGoodAttr = graph.addAttribute(GraphElementType.VERTEX, BooleanAttributeDescription.ATTRIBUTE_NAME, "isGood", null, false, null);
    final int vxCountryAttr = SpatialConcept.VertexAttribute.COUNTRY.ensure(graph);
    final int vxPinnedAttr = VisualConcept.VertexAttribute.PINNED.ensure(graph);
    final int txIdAttr = VisualConcept.TransactionAttribute.IDENTIFIER.ensure(graph);
    final int txTypeAttr = AnalyticConcept.TransactionAttribute.TYPE.ensure(graph);
    final int txDateTimeAttr = TemporalConcept.TransactionAttribute.DATETIME.ensure(graph);
    final int[] startVxIds = new int[m];
    final VertexDecorators decorators;
    decorators = new VertexDecorators(graph.getAttributeName(vxCountryAttr), graph.getAttributeName(vxPinnedAttr), null, graph.getAttributeName(vxIsGoodAttr));
    final int decoratorsAttr = VisualConcept.GraphAttribute.DECORATORS.ensure(graph);
    graph.setObjectValue(decoratorsAttr, 0, decorators);
    int vx = 0;
    while (vx < m) {
        final int vxId = graph.addVertex();
        final String label = "Node_" + vxId;
        graph.setStringValue(vxIdentifierAttr, vxId, label);
        graph.setStringValue(vxTypeAttr, vxId, nodeTypes.get(r.nextInt(nodeTypes.size())));
        graph.setBooleanValue(vxIsGoodAttr, vxId, r.nextInt(10) == 0);
        graph.setStringValue(vxCountryAttr, vxId, countries.get(r.nextInt(countries.size())));
        if (graph.getSchema() != null) {
            graph.getSchema().completeVertex(graph, vxId);
        }
        startVxIds[vx] = vxId;
        vx++;
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }
    }
    // Create transactions between the nodes.
    final Date d = new Date();
    final int fourDays = 4 * 24 * 60 * 60 * 1000;
    Set<Integer> destinations = Arrays.stream(startVxIds).boxed().collect(Collectors.toSet());
    final List<Integer> repeats = new ArrayList<>();
    while (vx < n) {
        final int vxId = graph.addVertex();
        final String label = "Node_" + vxId;
        graph.setStringValue(vxIdentifierAttr, vxId, label);
        graph.setStringValue(vxTypeAttr, vxId, nodeTypes.get(r.nextInt(nodeTypes.size())));
        graph.setBooleanValue(vxIsGoodAttr, vxId, r.nextInt(10) == 0);
        graph.setStringValue(vxCountryAttr, vxId, countries.get(r.nextInt(countries.size())));
        if (graph.getSchema() != null) {
            graph.getSchema().completeVertex(graph, vxId);
        }
        final int reciprocity = r.nextInt(3);
        for (final int destination : destinations) {
            int numTimes = 1;
            if (randomWeights) {
                numTimes = r.nextInt(1 + r.nextInt(100));
            }
            for (int i = 0; i < numTimes; i++) {
                int sxId = vxId;
                int dxId = destination;
                if (randomWeights) {
                    switch(reciprocity) {
                        case 0:
                            final boolean random0 = r.nextBoolean();
                            if (random0) {
                                sxId = destination;
                                dxId = vxId;
                            }
                            break;
                        case 1:
                            final int random1 = r.nextInt(5);
                            if (random1 == 0) {
                                sxId = destination;
                                dxId = vxId;
                            }
                            break;
                        default:
                            final int randomDefault = r.nextInt(5);
                            if (randomDefault != 0) {
                                sxId = destination;
                                dxId = vxId;
                            }
                            break;
                    }
                }
                final int e = graph.addTransaction(sxId, dxId, true);
                graph.setLongValue(txDateTimeAttr, e, d.getTime() - r.nextInt(fourDays));
                graph.setStringValue(txTypeAttr, e, transactionTypes.get(r.nextInt(transactionTypes.size())));
                graph.setIntValue(txIdAttr, e, e);
                if (graph.getSchema() != null) {
                    graph.getSchema().completeTransaction(graph, e);
                }
                if (Thread.interrupted()) {
                    throw new InterruptedException();
                }
            }
            repeats.add(vxId);
            repeats.add(destination);
        }
        destinations = generateDestinations(repeats, m, r);
        vx++;
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }
    }
    try {
        if (n < 10000) {
            // Do a trees layout.
            PluginExecutor.startWith(ArrangementPluginRegistry.TREES).followedBy(InteractiveGraphPluginRegistry.RESET_VIEW).executeNow(graph);
        } else {
            // Do a grid layout.
            PluginExecutor.startWith(ArrangementPluginRegistry.GRID_COMPOSITE).followedBy(InteractiveGraphPluginRegistry.RESET_VIEW).executeNow(graph);
        }
    } catch (final PluginException ex) {
        LOGGER.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
    }
    interaction.setProgress(1, 0, "Completed successfully", true);
}
Also used : VertexDecorators(au.gov.asd.tac.constellation.graph.schema.visual.VertexDecorators) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) ArrayList(java.util.ArrayList) Date(java.util.Date) PluginParameter(au.gov.asd.tac.constellation.plugins.parameters.PluginParameter)

Aggregations

PluginException (au.gov.asd.tac.constellation.plugins.PluginException)60 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)28 Plugin (au.gov.asd.tac.constellation.plugins.Plugin)23 Graph (au.gov.asd.tac.constellation.graph.Graph)21 ArrayList (java.util.ArrayList)20 PluginInteraction (au.gov.asd.tac.constellation.plugins.PluginInteraction)19 Test (org.testng.annotations.Test)16 GraphRecordStore (au.gov.asd.tac.constellation.graph.processing.GraphRecordStore)14 ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)12 IOException (java.io.IOException)12 List (java.util.List)12 GraphWriteMethods (au.gov.asd.tac.constellation.graph.GraphWriteMethods)10 WritableGraph (au.gov.asd.tac.constellation.graph.WritableGraph)10 File (java.io.File)10 PluginNotificationLevel (au.gov.asd.tac.constellation.plugins.PluginNotificationLevel)9 Map (java.util.Map)9 StoreGraph (au.gov.asd.tac.constellation.graph.StoreGraph)8 DualGraph (au.gov.asd.tac.constellation.graph.locking.DualGraph)8 VisualConcept (au.gov.asd.tac.constellation.graph.schema.visual.concept.VisualConcept)8 PluginGraphs (au.gov.asd.tac.constellation.plugins.PluginGraphs)8