Search in sources :

Example 6 with CompositeNodeState

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

the class CompositesNGTest method makeExpandDeleteNodeContractCompositeTest.

@Test
public void makeExpandDeleteNodeContractCompositeTest() throws InterruptedException, PluginException {
    final WritableGraph wg = graph.getWritableGraph("test", true);
    wg.getSchema().newGraph(wg);
    final int v0, v1, v2;
    final String v0name, v1name, v2name;
    final int nameAttr;
    final int selectedAttr;
    final int compositeAttr;
    try {
        // Add three vertices
        v0 = wg.addVertex();
        wg.getSchema().newVertex(wg, v0);
        v1 = wg.addVertex();
        wg.getSchema().newVertex(wg, v1);
        v2 = wg.addVertex();
        wg.getSchema().newVertex(wg, v2);
        // Store the names of these vertices
        nameAttr = VisualConcept.VertexAttribute.LABEL.get(wg);
        v0name = wg.getStringValue(nameAttr, v0);
        v1name = wg.getStringValue(nameAttr, v1);
        v2name = wg.getStringValue(nameAttr, v2);
        // Add transactions from v0 to v1, and from v1 to v2
        final int t0_1 = wg.addTransaction(v0, v1, true);
        wg.getSchema().newTransaction(wg, t0_1);
        final int t1_2 = wg.addTransaction(v1, v2, true);
        wg.getSchema().newTransaction(wg, t1_2);
        // Select v0 and v1, but not v2
        selectedAttr = VisualConcept.VertexAttribute.SELECTED.get(wg);
        wg.setBooleanValue(selectedAttr, v0, true);
        wg.setBooleanValue(selectedAttr, v1, true);
        wg.setBooleanValue(selectedAttr, v2, false);
        // Make a composite from the selection then expand all composites
        PluginExecutor.startWith(PluginRegistry.get(InteractiveGraphPluginRegistry.CREATE_COMPOSITE_FROM_SELECTION)).followedBy(PluginRegistry.get(InteractiveGraphPluginRegistry.EXPAND_ALL_COMPOSITES)).executeNow(wg);
        compositeAttr = AnalyticConcept.VertexAttribute.COMPOSITE_STATE.get(wg);
        // Delete the expanded vertex corresponding to v0 then contract all composites
        final int v2pos = wg.getVertexPosition(v2);
        final int comp0 = v2pos != 0 ? wg.getVertex(0) : wg.getVertex(2);
        final int comp1 = v2pos != 1 ? wg.getVertex(1) : wg.getVertex(2);
        final int expanded_v0 = wg.getStringValue(nameAttr, comp0).equals(v0name) ? comp0 : comp1;
        wg.removeVertex(expanded_v0);
        PluginExecution.withPlugin(PluginRegistry.get(InteractiveGraphPluginRegistry.CONTRACT_ALL_COMPOSITES)).executeNow(wg);
    } finally {
        wg.commit();
    }
    final ReadableGraph rg = graph.getReadableGraph();
    try {
        // Assert that there are two vertices, that the composite node has a composite state containing one node,
        // and that the non-composite node has a null composite state and its original name.
        assertEquals(rg.getVertexCount(), 2);
        assertEquals(rg.getStringValue(nameAttr, v2), v2name);
        assertNull(rg.getObjectValue(compositeAttr, v2));
        final int compositeNode = rg.getVertex(0) != v2 ? rg.getVertex(0) : rg.getVertex(1);
        final CompositeNodeState compositeState = (CompositeNodeState) rg.getObjectValue(compositeAttr, compositeNode);
        assertNotNull(compositeState);
        assertEquals(compositeState.getNumberOfNodes(), 1);
        assertTrue(compositeState.isComposite());
        // Assert that the link between the composite node and the non-composite node still exists,
        // and contains one transaction from the composite node to the non-composite node
        final int compLink = rg.getLink(compositeNode, v2);
        assertEquals(rg.getLinkTransactionCount(compLink), 1);
        final int compTrans = rg.getLinkTransaction(compLink, 0);
        assertFalse(rg.getTransactionDirection(compTrans) == Graph.FLAT);
        assertEquals(rg.getTransactionSourceVertex(compTrans), compositeNode);
    } finally {
        rg.release();
    }
    final WritableGraph wg2 = graph.getWritableGraph("test", true);
    try {
        PluginExecution.withPlugin(PluginRegistry.get(InteractiveGraphPluginRegistry.EXPAND_ALL_COMPOSITES)).executeNow(wg2);
    } finally {
        wg2.commit();
    }
    final ReadableGraph rg2 = graph.getReadableGraph();
    final int expanded_v1;
    try {
        // Assert that there are two nodes, that v2 is the same with a null composite state,
        // and that the other node corresponds to v1 with an expanded composite states
        assertEquals(rg2.getVertexCount(), 2);
        assertEquals(rg2.getStringValue(nameAttr, v2), v2name);
        assertNull(rg2.getObjectValue(compositeAttr, v2));
        final int v2pos = rg2.getVertexPosition(v2);
        expanded_v1 = v2pos != 0 ? rg2.getVertex(0) : rg2.getVertex(1);
        final CompositeNodeState compositeState = (CompositeNodeState) rg2.getObjectValue(compositeAttr, expanded_v1);
        assertNotNull(compositeState);
        assertEquals(compositeState.getNumberOfNodes(), 1);
        assertTrue(compositeState.comprisesAComposite());
        assertEquals(rg2.getStringValue(nameAttr, expanded_v1), v1name);
        // Assert that the transaction between exapnded v1 and v2 exist, and no others.
        assertEquals(rg2.getTransactionCount(), 1);
        final int l0_1 = rg2.getLink(expanded_v1, v2);
        assertEquals(rg2.getLinkTransactionCount(l0_1), 1);
        final int t0_1 = rg2.getLinkTransaction(l0_1, 0);
        assertEquals(rg2.getTransactionSourceVertex(t0_1), expanded_v1);
        assertFalse(rg2.getTransactionDirection(t0_1) == Graph.FLAT);
    } finally {
        rg2.release();
    }
    final WritableGraph wg3 = graph.getWritableGraph("test", true);
    try {
        // Delete v1 and then contract all composites
        wg.removeVertex(expanded_v1);
        PluginExecution.withPlugin(PluginRegistry.get(InteractiveGraphPluginRegistry.CONTRACT_ALL_COMPOSITES)).executeNow(wg3);
    } finally {
        wg3.commit();
    }
    final ReadableGraph rg3 = graph.getReadableGraph();
    try {
        // Assert that there is only one vertex - v2 with a null composite state
        assertEquals(rg3.getVertexCount(), 1);
        assertEquals(rg3.getStringValue(nameAttr, v2), v2name);
        assertNull(rg3.getObjectValue(compositeAttr, v2));
        // Assert that there are no transactions
        assertEquals(rg3.getTransactionCount(), 0);
    } finally {
        rg3.release();
    }
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) CompositeNodeState(au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.CompositeNodeState) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) Test(org.testng.annotations.Test)

Example 7 with CompositeNodeState

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

the class CompositesNGTest method makeExpandDeleteNodeWithTransactionContractCompositeTest.

@Test
public void makeExpandDeleteNodeWithTransactionContractCompositeTest() throws InterruptedException, PluginException {
    final WritableGraph wg = graph.getWritableGraph("test", true);
    wg.getSchema().newGraph(wg);
    final int v0, v1, v2;
    final String v0name, v1name, v2name;
    final int nameAttr;
    final int selectedAttr;
    final int compositeAttr;
    try {
        // Add three vertices
        v0 = wg.addVertex();
        wg.getSchema().newVertex(wg, v0);
        v1 = wg.addVertex();
        wg.getSchema().newVertex(wg, v1);
        v2 = wg.addVertex();
        wg.getSchema().newVertex(wg, v2);
        // Store the names of these vertices
        nameAttr = VisualConcept.VertexAttribute.LABEL.get(wg);
        v0name = wg.getStringValue(nameAttr, v0);
        v1name = wg.getStringValue(nameAttr, v1);
        v2name = wg.getStringValue(nameAttr, v2);
        // Add transactions from v0 to v1, and from v1 to v2
        final int t0_1 = wg.addTransaction(v0, v1, true);
        wg.getSchema().newTransaction(wg, t0_1);
        final int t1_2 = wg.addTransaction(v1, v2, true);
        wg.getSchema().newTransaction(wg, t1_2);
        // Select v0 and v1, but not v2
        selectedAttr = VisualConcept.VertexAttribute.SELECTED.get(wg);
        wg.setBooleanValue(selectedAttr, v0, true);
        wg.setBooleanValue(selectedAttr, v1, true);
        wg.setBooleanValue(selectedAttr, v2, false);
        // Make a composite from the selection then expand all composites
        PluginExecutor.startWith(PluginRegistry.get(InteractiveGraphPluginRegistry.CREATE_COMPOSITE_FROM_SELECTION)).followedBy(PluginRegistry.get(InteractiveGraphPluginRegistry.EXPAND_ALL_COMPOSITES)).executeNow(wg);
        compositeAttr = AnalyticConcept.VertexAttribute.COMPOSITE_STATE.get(wg);
        // Delete the expanded vertex corresponding to v1 then contract all composites
        final int v2pos = wg.getVertexPosition(v2);
        final int comp0 = v2pos != 0 ? wg.getVertex(0) : wg.getVertex(2);
        final int comp1 = v2pos != 1 ? wg.getVertex(1) : wg.getVertex(2);
        final int expanded_v1 = wg.getStringValue(nameAttr, comp0).equals(v1name) ? comp0 : comp1;
        wg.removeVertex(expanded_v1);
        PluginExecution.withPlugin(PluginRegistry.get(InteractiveGraphPluginRegistry.CONTRACT_ALL_COMPOSITES)).executeNow(wg);
    } finally {
        wg.commit();
    }
    final ReadableGraph rg = graph.getReadableGraph();
    try {
        // Assert that there are two vertices, that the composite node has a composite state containing one node,
        // and that the non-composite node has a null composite state and its original name.
        assertEquals(rg.getVertexCount(), 2);
        assertEquals(rg.getStringValue(nameAttr, v2), v2name);
        assertNull(rg.getObjectValue(compositeAttr, v2));
        final int compositeNode = rg.getVertex(0) != v2 ? rg.getVertex(0) : rg.getVertex(1);
        final CompositeNodeState compositeState = (CompositeNodeState) rg.getObjectValue(compositeAttr, compositeNode);
        assertNotNull(compositeState);
        assertEquals(compositeState.getNumberOfNodes(), 1);
        assertTrue(compositeState.isComposite());
        // Assert that there are no transactions in the graph.
        assertEquals(rg.getTransactionCount(), 0);
    } finally {
        rg.release();
    }
    final WritableGraph wg2 = graph.getWritableGraph("test", true);
    try {
        PluginExecution.withPlugin(PluginRegistry.get(InteractiveGraphPluginRegistry.EXPAND_ALL_COMPOSITES)).executeNow(wg2);
    } finally {
        wg2.commit();
    }
    final ReadableGraph rg2 = graph.getReadableGraph();
    final int expanded_v0;
    try {
        // Assert that there are two nodes, that v2 is the same with a null composite state,
        // and that the other node corresponds to v0 with an expanded composite states
        assertEquals(rg2.getVertexCount(), 2);
        assertEquals(rg2.getStringValue(nameAttr, v2), v2name);
        assertNull(rg2.getObjectValue(compositeAttr, v2));
        final int v2pos = rg2.getVertexPosition(v2);
        expanded_v0 = v2pos != 0 ? rg2.getVertex(0) : rg2.getVertex(1);
        final CompositeNodeState compositeState = (CompositeNodeState) rg2.getObjectValue(compositeAttr, expanded_v0);
        assertNotNull(compositeState);
        assertEquals(compositeState.getNumberOfNodes(), 1);
        assertTrue(compositeState.comprisesAComposite());
        assertEquals(rg2.getStringValue(nameAttr, expanded_v0), v0name);
        // Assert that there are no transactions in the graph
        assertEquals(rg.getTransactionCount(), 0);
    } finally {
        rg2.release();
    }
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) CompositeNodeState(au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.CompositeNodeState) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) Test(org.testng.annotations.Test)

Example 8 with CompositeNodeState

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

the class CompositeUtilities method expandComposite.

/**
 * Expands a single composite node.
 *
 * @param graph The graph.
 * @param compositeStateAttr The graph ID of the composite state attribute
 * for nodes.
 * @param vxId The id of the specified node.
 * @return A list of the id's of the expanded constituent nodes. This will
 * be empty if the specified node was not a composite.
 */
public static List<Integer> expandComposite(final GraphWriteMethods graph, final int compositeStateAttr, final int vxId) {
    final List<Integer> expandedNodes = new ArrayList<>();
    final CompositeNodeState compositeState = (CompositeNodeState) graph.getObjectValue(compositeStateAttr, vxId);
    if (compositeState != null && compositeState.isComposite()) {
        final ContractedCompositeNodeState contractedState = compositeState.contractedState;
        expandedNodes.addAll(contractedState.expand(graph, vxId));
        while (true) {
            try {
                graph.validateKey(GraphElementType.VERTEX, false);
                break;
            } catch (final DuplicateKeyException ex) {
                LOGGER.log(Level.INFO, "Duplicate Key has been found. Merging duplicate nodes");
                final GraphElementMerger merger = new PrioritySurvivingGraphElementMerger();
                merger.mergeElement(graph, GraphElementType.VERTEX, ex.getNewId(), ex.getExistingId());
            }
        }
    }
    return expandedNodes;
}
Also used : CompositeNodeState(au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.CompositeNodeState) ExpandedCompositeNodeState(au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.ExpandedCompositeNodeState) ContractedCompositeNodeState(au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.ContractedCompositeNodeState) PrioritySurvivingGraphElementMerger(au.gov.asd.tac.constellation.graph.mergers.PrioritySurvivingGraphElementMerger) ArrayList(java.util.ArrayList) ContractedCompositeNodeState(au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.ContractedCompositeNodeState) DuplicateKeyException(au.gov.asd.tac.constellation.graph.DuplicateKeyException) GraphElementMerger(au.gov.asd.tac.constellation.graph.GraphElementMerger) PrioritySurvivingGraphElementMerger(au.gov.asd.tac.constellation.graph.mergers.PrioritySurvivingGraphElementMerger)

Example 9 with CompositeNodeState

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

the class CompositeUtilities method makeComposite.

/**
 * Make a composite node by specifying a lead node id and a set of the
 * comprising node id's
 *
 * @param graph The graph
 * @param comprisingIds A Set of the comprising node id's to be composited
 * @param leaderId The lead node id to contain the composited nodes
 */
public static void makeComposite(final GraphWriteMethods graph, final Set<Integer> comprisingIds, final int leaderId) {
    final int compositeStateAttr = AnalyticConcept.VertexAttribute.COMPOSITE_STATE.ensure(graph);
    final int uniqueIdAttr = VisualConcept.TransactionAttribute.IDENTIFIER.get(graph);
    // We first destroy any composites or composite constituents about to be composited.
    // For any composites, we also add any expanded ids and remove the destroyed composite id from the list of selected ids.
    final Set<Integer> addedVerts = new HashSet<>();
    final Set<Integer> removedVerts = new HashSet<>();
    comprisingIds.forEach(vxId -> {
        final List<Integer> resultingVerts = CompositeUtilities.destroyComposite(graph, compositeStateAttr, uniqueIdAttr, vxId);
        if (!resultingVerts.isEmpty()) {
            removedVerts.add(vxId);
            addedVerts.addAll(resultingVerts);
        }
    });
    // NOTE:: Remove before adding, because of id reuse!
    comprisingIds.removeAll(removedVerts);
    comprisingIds.addAll(addedVerts);
    // Make a record store representing the new composite that is about to be added
    final String[] compositeId = new String[1];
    final RecordStore newCompositeStore = new GraphRecordStore();
    GraphRecordStoreUtilities.copySpecifiedVertex(graph, newCompositeStore, leaderId, compositeId);
    // Construct and set an expanded composite node state for each of the nodes that will constitute the composite.
    comprisingIds.forEach(vxId -> {
        final ExpandedCompositeNodeState expandedState = new ExpandedCompositeNodeState(newCompositeStore, compositeId[0], true, comprisingIds.size());
        graph.setObjectValue(compositeStateAttr, vxId, new CompositeNodeState(vxId, expandedState));
    });
    // Create the composite by calling contract on the first node's expanded composite state.
    ((CompositeNodeState) graph.getObjectValue(compositeStateAttr, comprisingIds.iterator().next())).expandedState.contract(graph);
}
Also used : CompositeNodeState(au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.CompositeNodeState) ExpandedCompositeNodeState(au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.ExpandedCompositeNodeState) ContractedCompositeNodeState(au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.ContractedCompositeNodeState) RecordStore(au.gov.asd.tac.constellation.graph.processing.RecordStore) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) ExpandedCompositeNodeState(au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.ExpandedCompositeNodeState) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) HashSet(java.util.HashSet)

Example 10 with CompositeNodeState

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

the class CompositeNodeStateAttributeInteraction method getDisplayText.

@Override
public String getDisplayText(final Object value) {
    if (value == null) {
        return null;
    }
    final CompositeNodeState state = (CompositeNodeState) value;
    final CompositeStatus status = state.getStatus();
    if (status.equals(CompositeStatus.NOT_A_COMPOSITE)) {
        return "";
    } else if (status.equals(CompositeStatus.IS_A_COMPOSITE)) {
        return String.format("%s comprising %d nodes.", status.compositeName, state.getNumberOfNodes());
    } else {
        return String.format("%s with %d other node%s.", status.compositeName, state.getNumberOfNodes() - 1, state.getNumberOfNodes() == 1 ? "" : "s");
    }
}
Also used : CompositeNodeState(au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.CompositeNodeState) CompositeStatus(au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.CompositeStatus)

Aggregations

CompositeNodeState (au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.CompositeNodeState)13 ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)8 WritableGraph (au.gov.asd.tac.constellation.graph.WritableGraph)8 Test (org.testng.annotations.Test)8 ExpandedCompositeNodeState (au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.ExpandedCompositeNodeState)4 ContractedCompositeNodeState (au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.ContractedCompositeNodeState)3 DuplicateKeyException (au.gov.asd.tac.constellation.graph.DuplicateKeyException)2 GraphElementMerger (au.gov.asd.tac.constellation.graph.GraphElementMerger)2 PrioritySurvivingGraphElementMerger (au.gov.asd.tac.constellation.graph.mergers.PrioritySurvivingGraphElementMerger)2 GraphRecordStore (au.gov.asd.tac.constellation.graph.processing.GraphRecordStore)2 RecordStore (au.gov.asd.tac.constellation.graph.processing.RecordStore)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 CompositeStatus (au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.CompositeStatus)1 DeselectAllPlugin (au.gov.asd.tac.constellation.graph.visual.plugins.select.DeselectAllPlugin)1