Search in sources :

Example 21 with SchemaTransactionType

use of au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionType in project constellation by constellation-app.

the class SelectTopNPlugin method edit.

@Override
protected void edit(final GraphWriteMethods graph, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
    final String mode = parameters.getParameters().get(MODE_PARAMETER_ID).getStringValue();
    final String typeCategory = parameters.getParameters().get(TYPE_CATEGORY_PARAMETER_ID).getStringValue();
    final List<String> subTypes = parameters.getParameters().get(TYPE_PARAMETER_ID).getMultiChoiceValue().getChoices();
    final int limit = parameters.getParameters().get(LIMIT_PARAMETER_ID).getIntegerValue();
    if (mode == null || (!mode.equals(NODE) && !mode.equals(TRANSACTION))) {
        throw new PluginException(PluginNotificationLevel.ERROR, "Invalid mode value provided");
    }
    if (typeCategory == null) {
        throw new PluginException(PluginNotificationLevel.ERROR, "Select a type category");
    }
    if (subTypes.isEmpty()) {
        throw new PluginException(PluginNotificationLevel.ERROR, "Select some types to perform the calculation");
    }
    final int vertexLabelAttribute = VisualConcept.VertexAttribute.LABEL.get(graph);
    if (vertexLabelAttribute == Graph.NOT_FOUND) {
        throw new PluginException(PluginNotificationLevel.ERROR, String.format(MISSING_PROPERTY_FORMAT, VisualConcept.VertexAttribute.LABEL.getName()));
    }
    final int vertexSelectedAttribute = VisualConcept.VertexAttribute.SELECTED.get(graph);
    if (vertexSelectedAttribute == Graph.NOT_FOUND) {
        throw new PluginException(PluginNotificationLevel.ERROR, String.format(MISSING_PROPERTY_FORMAT, VisualConcept.VertexAttribute.SELECTED.getName()));
    }
    final int vertexTypeAttribute = AnalyticConcept.VertexAttribute.TYPE.get(graph);
    if (vertexTypeAttribute == Graph.NOT_FOUND) {
        throw new PluginException(PluginNotificationLevel.ERROR, String.format(MISSING_PROPERTY_FORMAT, AnalyticConcept.VertexAttribute.TYPE.getName()));
    }
    final int transactionTypeAttribute = AnalyticConcept.TransactionAttribute.TYPE.get(graph);
    if (transactionTypeAttribute == Graph.NOT_FOUND) {
        throw new PluginException(PluginNotificationLevel.ERROR, String.format(MISSING_PROPERTY_FORMAT, AnalyticConcept.TransactionAttribute.TYPE.getName()));
    }
    // make a set of the highlighted nodes
    final Set<Integer> selectedNodes = new HashSet<>();
    final int vertexCount = graph.getVertexCount();
    for (int position = 0; position < vertexCount; position++) {
        final int vxId = graph.getVertex(position);
        if (graph.getBooleanValue(vertexSelectedAttribute, vxId)) {
            selectedNodes.add(vxId);
        }
    }
    if (selectedNodes.isEmpty()) {
        throw new PluginException(PluginNotificationLevel.ERROR, "Select at least 1 node");
    }
    // calculate the occurrences of destination vertices
    int txId;
    int sourceVxId;
    int destinationVxId;
    int targetVxId;
    int step = 0;
    SchemaVertexType destinationVertexType;
    SchemaTransactionType transactionType;
    final Map<Integer, Integer> occurrences = new HashMap<>();
    for (final Integer vxId : selectedNodes) {
        final String label = graph.getStringValue(vertexLabelAttribute, vxId);
        interaction.setProgress(++step, selectedNodes.size(), String.format("Calculating top %s for %s", limit, label), true);
        final int transactionCount = graph.getVertexTransactionCount(vxId);
        for (int position = 0; position < transactionCount; position++) {
            txId = graph.getVertexTransaction(vxId, position);
            sourceVxId = graph.getTransactionSourceVertex(txId);
            destinationVxId = graph.getTransactionDestinationVertex(txId);
            targetVxId = vxId == sourceVxId ? destinationVxId : sourceVxId;
            switch(mode) {
                case NODE:
                    destinationVertexType = graph.getObjectValue(vertexTypeAttribute, targetVxId);
                    if (destinationVertexType != null && subTypes.contains(destinationVertexType.getName())) {
                        if (!occurrences.containsKey(targetVxId)) {
                            occurrences.put(targetVxId, 0);
                        }
                        occurrences.put(targetVxId, occurrences.get(targetVxId) + 1);
                    }
                    break;
                case TRANSACTION:
                    transactionType = graph.getObjectValue(transactionTypeAttribute, txId);
                    if (transactionType != null && subTypes.contains(transactionType.getName())) {
                        if (!occurrences.containsKey(targetVxId)) {
                            occurrences.put(targetVxId, 0);
                        }
                        occurrences.put(targetVxId, occurrences.get(targetVxId) + 1);
                    }
                    break;
                default:
                    break;
            }
        }
        // make a map sorted by the count in descending order
        final LinkedHashMap<Integer, Integer> sortedMap = occurrences.entrySet().stream().sorted(Map.Entry.comparingByValue(Collections.reverseOrder())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
        sortedMap.keySet().stream().limit(limit).forEach(id -> graph.setBooleanValue(vertexSelectedAttribute, id, true));
        interaction.setProgress(1, 0, "Selected " + sortedMap.size() + " nodes.", true);
    }
    interaction.setProgress(1, 0, "Completed successfully", true);
}
Also used : GraphWriteMethods(au.gov.asd.tac.constellation.graph.GraphWriteMethods) SchemaTransactionType(au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionType) SchemaVertexTypeUtilities(au.gov.asd.tac.constellation.graph.schema.type.SchemaVertexTypeUtilities) IntegerParameterValue(au.gov.asd.tac.constellation.plugins.parameters.types.IntegerParameterType.IntegerParameterValue) SchemaTransactionTypeUtilities(au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionTypeUtilities) ParameterChange(au.gov.asd.tac.constellation.plugins.parameters.ParameterChange) SingleChoiceParameterType(au.gov.asd.tac.constellation.plugins.parameters.types.SingleChoiceParameterType) MultiChoiceParameterValue(au.gov.asd.tac.constellation.plugins.parameters.types.MultiChoiceParameterType.MultiChoiceParameterValue) PluginType(au.gov.asd.tac.constellation.plugins.PluginType) HashMap(java.util.HashMap) VisualConcept(au.gov.asd.tac.constellation.graph.schema.visual.concept.VisualConcept) ArrayList(java.util.ArrayList) Graph(au.gov.asd.tac.constellation.graph.Graph) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) DataAccessPlugin(au.gov.asd.tac.constellation.views.dataaccess.plugins.DataAccessPlugin) 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) ServiceProvider(org.openide.util.lookup.ServiceProvider) PluginTags(au.gov.asd.tac.constellation.plugins.templates.PluginTags) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) MultiChoiceParameterType(au.gov.asd.tac.constellation.plugins.parameters.types.MultiChoiceParameterType) Set(java.util.Set) Logger(java.util.logging.Logger) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) Collectors(java.util.stream.Collectors) PluginNotificationLevel(au.gov.asd.tac.constellation.plugins.PluginNotificationLevel) SchemaVertexType(au.gov.asd.tac.constellation.graph.schema.type.SchemaVertexType) PluginInfo(au.gov.asd.tac.constellation.plugins.PluginInfo) 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) SimpleQueryPlugin(au.gov.asd.tac.constellation.plugins.templates.SimpleQueryPlugin) DataAccessPluginCoreType(au.gov.asd.tac.constellation.views.dataaccess.plugins.DataAccessPluginCoreType) Messages(org.openide.util.NbBundle.Messages) SingleChoiceParameterValue(au.gov.asd.tac.constellation.plugins.parameters.types.SingleChoiceParameterType.SingleChoiceParameterValue) Collections(java.util.Collections) SchemaVertexType(au.gov.asd.tac.constellation.graph.schema.type.SchemaVertexType) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) SchemaTransactionType(au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionType) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 22 with SchemaTransactionType

use of au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionType in project constellation by constellation-app.

the class GraphRecordStoreUtilities method copyValues.

private static void copyValues(final GraphWriteMethods graph, final GraphElementType elementType, final int element, final Map<String, String> values) {
    /**
     * check whether a transaction type is inconsistent with the direction
     * attribute, if so make a custom type
     */
    if (GraphElementType.TRANSACTION.equals(elementType)) {
        final String requestedDirected = values.remove(DIRECTED_KEY);
        if (requestedDirected != null) {
            final String type = values.get(TYPE_KEY);
            final SchemaTransactionType currentType = SchemaTransactionTypeUtilities.getType(type);
            if (currentType != null) {
                final boolean directed = Boolean.parseBoolean(requestedDirected);
                // if the requested direction is different to the type's direction then make a new type
                if (currentType.isDirected() != directed) {
                    final String typeName = String.format("%s (%s)", currentType, directed ? "directed" : "undirected");
                    SchemaTransactionType modifiedType = new SchemaTransactionType.Builder(currentType, typeName).setDirected(Boolean.parseBoolean(requestedDirected)).build();
                    if (!SchemaTransactionTypeUtilities.containsType(modifiedType)) {
                        SchemaTransactionTypeUtilities.addCustomType(modifiedType, false);
                    }
                    values.put(TYPE_KEY, modifiedType.getName());
                }
            }
        }
    }
    values.entrySet().stream().forEach(entry -> {
        String key = entry.getKey();
        String type = "string";
        if (key.endsWith(">")) {
            int typeStart = key.lastIndexOf('<');
            if (typeStart > 0) {
                type = key.substring(typeStart + 1, key.length() - 1);
                key = key.substring(0, typeStart);
            }
        }
        // TODO: look at ensure(true/fale)
        int attribute = graph.getAttribute(elementType, key);
        if (attribute == Graph.NOT_FOUND) {
            attribute = graph.getSchema() != null ? graph.getSchema().getFactory().ensureAttribute(graph, elementType, key) : Graph.NOT_FOUND;
            if (attribute == Graph.NOT_FOUND) {
                attribute = graph.addAttribute(elementType, type, key, key, null, null);
            }
        }
        try {
            graph.setStringValue(attribute, element, entry.getValue());
        } catch (final Exception ex) {
            // keeping this as an Exception to catch broad exceptions that can be thrown due to bad data
            LOGGER.log(Level.SEVERE, "Discarding unexpected value {0} seen in attribute {1}", new Object[] { entry.getValue(), graph.getAttributeName(attribute) });
        }
    });
}
Also used : SchemaTransactionType(au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionType) IOException(java.io.IOException)

Example 23 with SchemaTransactionType

use of au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionType in project constellation by constellation-app.

the class GraphRecordStoreUtilities method addTransaction.

private static int addTransaction(final GraphWriteMethods graph, final int source, final int destination, final Map<String, String> values, final Map<String, Integer> transactionMap, final boolean initializeWithSchema, boolean completeWithSchema) {
    final String type = values.get(TYPE_KEY);
    final String directedValue = values.get(DIRECTED_KEY);
    boolean directed = true;
    if (directedValue != null) {
        directed = !"False".equalsIgnoreCase(directedValue);
    } else {
        final SchemaTransactionType transactionType = SchemaTransactionTypeUtilities.getType(type);
        if (transactionType != null) {
            directed = transactionType.isDirected();
        }
    }
    final String completeWithSchemaValue = values.remove(COMPLETE_WITH_SCHEMA_KEY);
    if (completeWithSchemaValue != null) {
        completeWithSchema = Boolean.parseBoolean(completeWithSchemaValue);
    }
    final String idValue = values.remove(ID);
    final int transaction = getTransaction(graph, idValue, source, destination, directed, transactionMap, initializeWithSchema);
    if (values.remove(DELETE_KEY) != null) {
        graph.removeTransaction(transaction);
        return NO_ELEMENT;
    }
    if (transaction == NO_ELEMENT) {
        // TODO: should this check be done before the delete?
        return NO_ELEMENT;
    }
    copyValues(graph, GraphElementType.TRANSACTION, transaction, values);
    if (completeWithSchema && graph.getSchema() != null) {
        graph.getSchema().completeTransaction(graph, transaction);
    }
    return transaction;
}
Also used : SchemaTransactionType(au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionType)

Example 24 with SchemaTransactionType

use of au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionType in project constellation by constellation-app.

the class TransactionTypeNodeProvider method newActiveGraph.

@Override
public void newActiveGraph(final Graph graph) {
    // TODO: if the old graph and the new graph have the same schema, don't recalculate.
    Platform.runLater(() -> {
        detailsView.getChildren().clear();
        final Label nameLabel = new Label("No type selected");
        detailsView.getChildren().add(nameLabel);
        transactionTypes.clear();
        if (graph != null && graph.getSchema() != null && GraphNode.getGraphNode(graph) != null) {
            final SchemaFactory schemaFactory = graph.getSchema().getFactory();
            final Set<Class<? extends SchemaConcept>> concepts = schemaFactory.getRegisteredConcepts();
            schemaLabel.setText(String.format("%s - %s", schemaFactory.getLabel(), GraphNode.getGraphNode(graph).getDisplayName()));
            transactionTypes.addAll(SchemaTransactionTypeUtilities.getTypes(concepts));
            Collections.sort(transactionTypes, (final SchemaTransactionType a, final SchemaTransactionType b) -> a.getName().compareToIgnoreCase(b.getName()));
        } else {
            schemaLabel.setText("No schema available");
        }
        populateTree();
    });
}
Also used : SchemaFactory(au.gov.asd.tac.constellation.graph.schema.SchemaFactory) SchemaTransactionType(au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionType) Label(javafx.scene.control.Label) SchemaConcept(au.gov.asd.tac.constellation.graph.schema.concept.SchemaConcept)

Example 25 with SchemaTransactionType

use of au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionType in project constellation by constellation-app.

the class SubgraphUtilities method getSubgraph.

/**
 * Return a new graph which represents a subgraph of the given graph
 * filtered by transaction type.
 *
 * @param graph
 * @param schema
 * @param types
 * @param isExclusive
 * @return
 */
public static StoreGraph getSubgraph(final GraphReadMethods graph, final Schema schema, final Set<SchemaTransactionType> types, final boolean isExclusive) {
    final StoreGraph subgraph = new StoreGraph(schema);
    final int transactionTypeAttributeId = AnalyticConcept.TransactionAttribute.TYPE.get(graph);
    assert transactionTypeAttributeId != GraphConstants.NOT_FOUND : "The transaction 'Type' attribute does not exist on this graph";
    // for each transaction
    final int transactionCount = graph.getTransactionCount();
    for (int transactionPosition = 0; transactionPosition < transactionCount; transactionPosition++) {
        final int transactionId = graph.getTransaction(transactionPosition);
        // check transaction is of desired type
        final SchemaTransactionType transactionType = graph.getObjectValue(transactionTypeAttributeId, transactionId);
        for (SchemaTransactionType type : types) {
            if ((!isExclusive && ((transactionType == null && type == null) || (transactionType != null && transactionType.isSubTypeOf(type)))) || (isExclusive && !((transactionType == null && type == null) || (transactionType != null && transactionType.isSubTypeOf(type))))) {
                final int sourceVertexId = graph.getTransactionSourceVertex(transactionId);
                final int destinationVertexId = graph.getTransactionDestinationVertex(transactionId);
                // copy vertices
                final int newSourceVertexId = subgraph.vertexExists(sourceVertexId) ? sourceVertexId : subgraph.addVertex(sourceVertexId);
                final int newDestinationVertexId = subgraph.vertexExists(destinationVertexId) ? destinationVertexId : subgraph.addVertex(destinationVertexId);
                // copy vertex attributes
                final int vertexAttributeCount = graph.getAttributeCount(GraphElementType.VERTEX);
                for (int vertexAttributePosition = 0; vertexAttributePosition < vertexAttributeCount; vertexAttributePosition++) {
                    final int vertexAttributeId = graph.getAttribute(GraphElementType.VERTEX, vertexAttributePosition);
                    final int newVertexAttributeId;
                    if (subgraph.getAttribute(graph.getAttributeElementType(vertexAttributeId), graph.getAttributeName(vertexAttributeId)) != Graph.NOT_FOUND) {
                        newVertexAttributeId = subgraph.getAttribute(graph.getAttributeElementType(vertexAttributeId), graph.getAttributeName(vertexAttributeId));
                    } else {
                        newVertexAttributeId = subgraph.addAttribute(graph.getAttributeElementType(vertexAttributeId), graph.getAttributeType(vertexAttributeId), graph.getAttributeName(vertexAttributeId), graph.getAttributeDescription(vertexAttributeId), graph.getAttributeDefaultValue(vertexAttributeId), null);
                    }
                    final Object sourceVertexAttributeValue = graph.getObjectValue(vertexAttributeId, sourceVertexId);
                    subgraph.setObjectValue(newVertexAttributeId, newSourceVertexId, sourceVertexAttributeValue);
                    final Object destinationVertexAttributeValue = graph.getObjectValue(vertexAttributeId, destinationVertexId);
                    subgraph.setObjectValue(newVertexAttributeId, newDestinationVertexId, destinationVertexAttributeValue);
                }
                // copy transaction
                final boolean transactionDirected = graph.getTransactionDirection(transactionId) != Graph.FLAT;
                final int newTransactionId = subgraph.addTransaction(transactionId, newSourceVertexId, newDestinationVertexId, transactionDirected);
                // copy transaction attributes
                final int transactionAttributeCount = graph.getAttributeCount(GraphElementType.TRANSACTION);
                for (int transactionAttributePosition = 0; transactionAttributePosition < transactionAttributeCount; transactionAttributePosition++) {
                    final int transactionAttributeId = graph.getAttribute(GraphElementType.TRANSACTION, transactionAttributePosition);
                    final int newTransactionAttributeId;
                    if (subgraph.getAttribute(graph.getAttributeElementType(transactionAttributeId), graph.getAttributeName(transactionAttributeId)) != Graph.NOT_FOUND) {
                        newTransactionAttributeId = subgraph.getAttribute(graph.getAttributeElementType(transactionAttributeId), graph.getAttributeName(transactionAttributeId));
                    } else {
                        newTransactionAttributeId = subgraph.addAttribute(graph.getAttributeElementType(transactionAttributeId), graph.getAttributeType(transactionAttributeId), graph.getAttributeName(transactionAttributeId), graph.getAttributeDescription(transactionAttributeId), graph.getAttributeDefaultValue(transactionAttributeId), null);
                    }
                    final Object transactionAttributeValue = graph.getObjectValue(transactionAttributeId, transactionId);
                    subgraph.setObjectValue(newTransactionAttributeId, newTransactionId, transactionAttributeValue);
                }
                // type, we don't need to check the other desired types
                break;
            }
        }
    }
    return subgraph;
}
Also used : SchemaTransactionType(au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionType) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph)

Aggregations

SchemaTransactionType (au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionType)29 PluginParameter (au.gov.asd.tac.constellation.plugins.parameters.PluginParameter)11 ArrayList (java.util.ArrayList)11 MultiChoiceParameterValue (au.gov.asd.tac.constellation.plugins.parameters.types.MultiChoiceParameterType.MultiChoiceParameterValue)10 SchemaVertexType (au.gov.asd.tac.constellation.graph.schema.type.SchemaVertexType)8 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)7 StoreGraph (au.gov.asd.tac.constellation.graph.StoreGraph)5 SchemaConcept (au.gov.asd.tac.constellation.graph.schema.concept.SchemaConcept)4 Map (java.util.Map)4 RecordStore (au.gov.asd.tac.constellation.graph.processing.RecordStore)3 AnalyticConcept (au.gov.asd.tac.constellation.graph.schema.analytic.concept.AnalyticConcept)3 SchemaTransactionTypeUtilities (au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionTypeUtilities)3 SchemaVertexTypeUtilities (au.gov.asd.tac.constellation.graph.schema.type.SchemaVertexTypeUtilities)3 VisualConcept (au.gov.asd.tac.constellation.graph.schema.visual.concept.VisualConcept)3 SingleChoiceParameterValue (au.gov.asd.tac.constellation.plugins.parameters.types.SingleChoiceParameterType.SingleChoiceParameterValue)3 List (java.util.List)3 Graph (au.gov.asd.tac.constellation.graph.Graph)2 GraphWriteMethods (au.gov.asd.tac.constellation.graph.GraphWriteMethods)2 ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)2