Search in sources :

Example 61 with DualGraph

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

the class CopyToNewGraphPlugin method makeGraph.

/**
 * Created a new graph containing the selected elements of an existing
 * graph.
 *
 * @param original The graph to copy elements from.
 * @param newSchemaName If not null, create the new graph with this schema;
 * otherwise, use the schema of the original graph.
 * @param copyKeys If true, copy the keys.
 * @param copyAll If true, copy everything regardless of whether it is
 * selected or not.
 *
 * @return The new graph.
 *
 * @throws java.lang.InterruptedException if the process is canceled while
 * it is running.
 */
public static Graph makeGraph(final GraphReadMethods original, final String newSchemaName, final boolean copyKeys, final boolean copyAll) throws InterruptedException {
    final Schema schema = StringUtils.isNotBlank(newSchemaName) ? SchemaFactoryUtilities.getSchemaFactory(newSchemaName).createSchema() : original.getSchema();
    final Graph dualGraph = new DualGraph(schema == null ? null : schema.getFactory().createSchema());
    final WritableGraph graph = dualGraph.getWritableGraph("Make Graph", true);
    try {
        int vertexSelected = original.getAttribute(GraphElementType.VERTEX, VisualConcept.VertexAttribute.SELECTED.getName());
        int transactionSelected = original.getAttribute(GraphElementType.TRANSACTION, VisualConcept.TransactionAttribute.SELECTED.getName());
        int[] attributeTranslation = new int[1024];
        // Copy the attributes.
        for (GraphElementType type : GraphElementType.values()) {
            int attributeCount = original.getAttributeCount(type);
            for (int attributePosition = 0; attributePosition < attributeCount; attributePosition++) {
                int originalAttributeId = original.getAttribute(type, attributePosition);
                Attribute attribute = new GraphAttribute(original, originalAttributeId);
                int newAttributeId = graph.addAttribute(type, attribute.getAttributeType(), attribute.getName(), attribute.getDescription(), attribute.getDefaultValue(), null);
                if (originalAttributeId >= attributeTranslation.length) {
                    attributeTranslation = Arrays.copyOf(attributeTranslation, originalAttributeId * 2);
                }
                attributeTranslation[originalAttributeId] = newAttributeId;
                if (type == GraphElementType.GRAPH) {
                    graph.setObjectValue(newAttributeId, 0, original.getObjectValue(originalAttributeId, 0));
                }
            }
            if (copyKeys) {
                final int[] keyAttributes = original.getPrimaryKey(type);
                if (keyAttributes != null && keyAttributes.length > 0) {
                    for (int i = 0; i < keyAttributes.length; i++) {
                        keyAttributes[i] = attributeTranslation[keyAttributes[i]];
                    }
                    graph.setPrimaryKey(type, keyAttributes);
                }
            }
        }
        // Copy the named selection state.
        final int namedSelectionAttr = original.getAttribute(GraphElementType.META, NamedSelectionState.ATTRIBUTE_NAME);
        if (namedSelectionAttr != Graph.NOT_FOUND) {
            final Object possibleState = original.getObjectValue(namedSelectionAttr, 0);
            if (possibleState instanceof NamedSelectionState) {
                final NamedSelectionState state = new NamedSelectionState((NamedSelectionState) possibleState);
                graph.setObjectValue(attributeTranslation[namedSelectionAttr], 0, state);
            }
        }
        // Copy the vertices.
        int[] vertexTranslation = new int[original.getVertexCapacity()];
        for (int position = 0; position < original.getVertexCount(); position++) {
            int originalVertex = original.getVertex(position);
            if (copyAll || vertexSelected == Graph.NOT_FOUND || original.getBooleanValue(vertexSelected, originalVertex)) {
                int newVertex = graph.addVertex();
                vertexTranslation[originalVertex] = newVertex;
                for (int attributePosition = 0; attributePosition < original.getAttributeCount(GraphElementType.VERTEX); attributePosition++) {
                    int originalAttributeId = original.getAttribute(GraphElementType.VERTEX, attributePosition);
                    int newAttributeId = attributeTranslation[originalAttributeId];
                    graph.setObjectValue(newAttributeId, newVertex, original.getObjectValue(originalAttributeId, originalVertex));
                }
            }
        }
        // Copy the transactions.
        for (int position = 0; position < original.getTransactionCount(); position++) {
            int originalTransaction = original.getTransaction(position);
            if (!copyAll) {
                if (transactionSelected != Graph.NOT_FOUND && !original.getBooleanValue(transactionSelected, originalTransaction)) {
                    continue;
                }
                if (vertexSelected != Graph.NOT_FOUND) {
                    if (!original.getBooleanValue(vertexSelected, original.getTransactionSourceVertex(originalTransaction))) {
                        continue;
                    }
                    if (!original.getBooleanValue(vertexSelected, original.getTransactionDestinationVertex(originalTransaction))) {
                        continue;
                    }
                }
            }
            int sourceVertex = vertexTranslation[original.getTransactionSourceVertex(originalTransaction)];
            int destinationVertex = vertexTranslation[original.getTransactionDestinationVertex(originalTransaction)];
            boolean directed = original.getTransactionDirection(originalTransaction) < 2;
            int newTransaction = graph.addTransaction(sourceVertex, destinationVertex, directed);
            for (int attributePosition = 0; attributePosition < original.getAttributeCount(GraphElementType.TRANSACTION); attributePosition++) {
                int originalAttributeId = original.getAttribute(GraphElementType.TRANSACTION, attributePosition);
                int newAttributeId = attributeTranslation[originalAttributeId];
                graph.setObjectValue(newAttributeId, newTransaction, original.getObjectValue(originalAttributeId, originalTransaction));
            }
        }
    } finally {
        graph.commit();
    }
    return dualGraph;
}
Also used : WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) Attribute(au.gov.asd.tac.constellation.graph.Attribute) GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) Schema(au.gov.asd.tac.constellation.graph.schema.Schema) GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) GraphElementType(au.gov.asd.tac.constellation.graph.GraphElementType) NamedSelectionState(au.gov.asd.tac.constellation.views.namedselection.state.NamedSelectionState)

Example 62 with DualGraph

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

the class AddModeWelcomePlugin method run.

/**
 * This method describes what action should be taken when the link is
 * clicked on the Welcome Page
 */
@Override
public void run() {
    final Schema schema = SchemaFactoryUtilities.getSchemaFactory(AnalyticSchemaFactory.ANALYTIC_SCHEMA_ID).createSchema();
    final StoreGraph sg = new StoreGraph(schema);
    schema.newGraph(sg);
    final int drawModeAttribute = VisualConcept.GraphAttribute.DRAWING_MODE.ensure(sg);
    sg.setBooleanValue(drawModeAttribute, 0, true);
    final Graph dualGraph = new DualGraph(sg, false);
    final String graphName = SchemaFactoryUtilities.getSchemaFactory(AnalyticSchemaFactory.ANALYTIC_SCHEMA_ID).getLabel().trim().toLowerCase();
    GraphOpener.getDefault().openGraph(dualGraph, graphName);
}
Also used : StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) Schema(au.gov.asd.tac.constellation.graph.schema.Schema) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph)

Example 63 with DualGraph

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

the class SelectionModeWelcomePlugin method run.

/**
 * This method describes what action should be taken when the link is
 * clicked on the Welcome Page
 */
@Override
public void run() {
    final Schema schema = SchemaFactoryUtilities.getSchemaFactory(AnalyticSchemaFactory.ANALYTIC_SCHEMA_ID).createSchema();
    final StoreGraph sg = new StoreGraph(schema);
    schema.newGraph(sg);
    final Graph dualGraph = new DualGraph(sg, false);
    final String graphName = SchemaFactoryUtilities.getSchemaFactory(AnalyticSchemaFactory.ANALYTIC_SCHEMA_ID).getLabel().trim().toLowerCase();
    GraphOpener.getDefault().openGraph(dualGraph, graphName);
}
Also used : StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) Schema(au.gov.asd.tac.constellation.graph.schema.Schema) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph)

Example 64 with DualGraph

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

the class LayersViewControllerNGTest method setupGraph.

/**
 * Set up a graph with two vertices and two transactions on layer 1.
 */
private void setupGraph() {
    graph = new DualGraph(SchemaFactoryUtilities.getSchemaFactory(VisualSchemaFactory.VISUAL_SCHEMA_ID).createSchema());
    try {
        WritableGraph wg = graph.getWritableGraph("", true);
        // Create LayerMask attributes
        layerMaskV = LayersConcept.VertexAttribute.LAYER_MASK.ensure(wg);
        layerMaskT = LayersConcept.TransactionAttribute.LAYER_MASK.ensure(wg);
        // Create LayerVisilibity Attributes
        layerVisibilityV = LayersConcept.VertexAttribute.LAYER_VISIBILITY.ensure(wg);
        layerVisibilityT = LayersConcept.TransactionAttribute.LAYER_VISIBILITY.ensure(wg);
        // Create Selected Attributes
        selectedV = VisualConcept.VertexAttribute.SELECTED.ensure(wg);
        selectedT = VisualConcept.TransactionAttribute.SELECTED.ensure(wg);
        // Adding 2 Vertices - not selected, layer 1, visible
        vxId1 = wg.addVertex();
        wg.setIntValue(layerMaskV, vxId1, 1);
        wg.setFloatValue(layerVisibilityV, vxId1, 1.0f);
        wg.setBooleanValue(selectedV, vxId1, false);
        vxId2 = wg.addVertex();
        wg.setIntValue(layerMaskV, vxId2, 1);
        wg.setFloatValue(layerVisibilityV, vxId2, 1.0f);
        wg.setBooleanValue(selectedV, vxId2, false);
        // Adding 2 Transactions - not selected, layer 1, visible
        txId1 = wg.addTransaction(vxId1, vxId2, true);
        wg.setIntValue(layerMaskT, txId1, 1);
        wg.setFloatValue(layerVisibilityT, txId1, 1.0f);
        wg.setBooleanValue(selectedT, txId1, false);
        txId2 = wg.addTransaction(vxId1, vxId2, false);
        wg.setIntValue(layerMaskT, txId2, 1);
        wg.setFloatValue(layerVisibilityT, vxId2, 1.0f);
        wg.setBooleanValue(selectedT, vxId2, false);
        wg.commit();
    } catch (final InterruptedException ex) {
        Exceptions.printStackTrace(ex);
        Thread.currentThread().interrupt();
    }
}
Also used : DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph)

Example 65 with DualGraph

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

the class LayersDualGraphSyncNGTest method setUpMethod.

@BeforeMethod
public void setUpMethod() throws Exception {
    queries = new ArrayList();
    graph = new DualGraph(null);
    final WritableGraph writableGraph = graph.getWritableGraph("test", true);
    try {
        bitmaskAttributeId = LayersViewConcept.GraphAttribute.LAYER_MASK_SELECTED.ensure(writableGraph);
        // Create LayerMask attributes
        layerMaskV = LayersConcept.VertexAttribute.LAYER_MASK.ensure(writableGraph);
        if (layerMaskV == Graph.NOT_FOUND) {
            fail();
        }
        layerMaskT = LayersConcept.TransactionAttribute.LAYER_MASK.ensure(writableGraph);
        if (layerMaskT == Graph.NOT_FOUND) {
            fail();
        }
        // Create LayerVisilibity Attributes
        layerVisibilityV = LayersConcept.VertexAttribute.LAYER_VISIBILITY.ensure(writableGraph);
        if (layerVisibilityV == Graph.NOT_FOUND) {
            fail();
        }
        layerVisibilityT = LayersConcept.TransactionAttribute.LAYER_VISIBILITY.ensure(writableGraph);
        if (layerVisibilityT == Graph.NOT_FOUND) {
            fail();
        }
        // Create Selected Attributes
        selectedV = VisualConcept.VertexAttribute.SELECTED.ensure(writableGraph);
        if (selectedV == Graph.NOT_FOUND) {
            fail();
        }
        selectedT = VisualConcept.TransactionAttribute.SELECTED.ensure(writableGraph);
        if (selectedT == Graph.NOT_FOUND) {
            fail();
        }
        // Create Color Attributes
        colorV = VisualConcept.VertexAttribute.COLOR.ensure(writableGraph);
        if (colorV == Graph.NOT_FOUND) {
            fail();
        }
        colorT = VisualConcept.TransactionAttribute.COLOR.ensure(writableGraph);
        if (colorT == Graph.NOT_FOUND) {
            fail();
        }
        vx1Color = ConstellationColor.getColorValue("#ed76b1");
        vx2Color = ConstellationColor.getColorValue("#eb78b2");
        vx3Color = ConstellationColor.getColorValue("#ee71b3");
        // Adding 2 Vertices layer 1, visible
        vxId1 = writableGraph.addVertex();
        vxId2 = writableGraph.addVertex();
        vxId3 = writableGraph.addVertex();
        writableGraph.setObjectValue(colorV, vxId1, vx1Color);
        writableGraph.setObjectValue(colorV, vxId2, vx2Color);
        writableGraph.setObjectValue(colorV, vxId3, vx3Color);
    } finally {
        writableGraph.commit();
    }
    final WritableGraph writableGraph2 = graph.getWritableGraph("test2", true);
    try {
        writableGraph2.setLongValue(layerMaskV, vxId1, 1);
        writableGraph2.setFloatValue(layerVisibilityV, vxId1, 1.0f);
        writableGraph2.setBooleanValue(selectedV, vxId1, false);
        vxId2 = writableGraph2.addVertex();
        writableGraph2.setLongValue(layerMaskV, vxId2, 1);
        writableGraph2.setFloatValue(layerVisibilityV, vxId2, 1.0f);
        writableGraph2.setBooleanValue(selectedV, vxId2, false);
    } finally {
        writableGraph2.commit();
    }
}
Also used : ArrayList(java.util.ArrayList) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

DualGraph (au.gov.asd.tac.constellation.graph.locking.DualGraph)66 WritableGraph (au.gov.asd.tac.constellation.graph.WritableGraph)41 Test (org.testng.annotations.Test)30 StoreGraph (au.gov.asd.tac.constellation.graph.StoreGraph)21 Graph (au.gov.asd.tac.constellation.graph.Graph)19 Schema (au.gov.asd.tac.constellation.graph.schema.Schema)18 ArrayList (java.util.ArrayList)13 ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)10 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)10 BeforeMethod (org.testng.annotations.BeforeMethod)9 PluginException (au.gov.asd.tac.constellation.plugins.PluginException)7 GraphRecordStore (au.gov.asd.tac.constellation.graph.processing.GraphRecordStore)6 PluginParameter (au.gov.asd.tac.constellation.plugins.parameters.PluginParameter)5 ConstellationColor (au.gov.asd.tac.constellation.utilities.color.ConstellationColor)4 GraphElementType (au.gov.asd.tac.constellation.graph.GraphElementType)3 GraphWriteMethods (au.gov.asd.tac.constellation.graph.GraphWriteMethods)3 SchemaFactory (au.gov.asd.tac.constellation.graph.schema.SchemaFactory)3 VisualConcept (au.gov.asd.tac.constellation.graph.schema.visual.concept.VisualConcept)3 PluginInteraction (au.gov.asd.tac.constellation.plugins.PluginInteraction)3 MultiChoiceParameterValue (au.gov.asd.tac.constellation.plugins.parameters.types.MultiChoiceParameterType.MultiChoiceParameterValue)3