Search in sources :

Example 11 with GraphWriteMethods

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

the class MiscNGTest method testCreateTransactionToNonexistentDestinationInPluginTest.

@Test
public void testCreateTransactionToNonexistentDestinationInPluginTest() {
    try {
        final DualGraph graph = new DualGraph(null);
        graph.setUndoManager(new UndoRedo.Manager());
        PluginExecution.withPlugin(new SimpleEditPlugin() {

            @Override
            public void edit(final GraphWriteMethods wg, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
                for (int i = 0; i < 10; i++) {
                    final String s = String.format("x%d", i);
                    wg.addAttribute(GraphElementType.VERTEX, ObjectAttributeDescription.ATTRIBUTE_NAME, s, s, 99, null);
                    wg.addAttribute(GraphElementType.TRANSACTION, FloatAttributeDescription.ATTRIBUTE_NAME, s, s, 99, null);
                }
                int vx = 0;
                for (int i = 0; i < 100; i++) {
                    vx = wg.addVertex();
                }
                final int tx = wg.addTransaction(vx, vx + 1, true);
                Assert.fail("Shouldn't get here, wg.addTransaction() should fail.");
                System.out.printf("New transaction: %d\n", tx);
            }

            @Override
            public String getName() {
                return "Build graph + tx failure test";
            }
        }).executeNow(graph);
    } catch (InterruptedException ex) {
        Assert.fail("Nothing was interrupted.");
    } catch (PluginException ex) {
        Assert.fail("There shouldn't be a plugin exception.");
    } catch (RuntimeException ex) {
        final boolean containsIllegalArgumentException = ex.getLocalizedMessage().contains("Attempt to create transaction to destination vertex that does not exist");
        Assert.assertTrue(containsIllegalArgumentException);
    }
}
Also used : GraphWriteMethods(au.gov.asd.tac.constellation.graph.GraphWriteMethods) PluginInteraction(au.gov.asd.tac.constellation.plugins.PluginInteraction) SimpleEditPlugin(au.gov.asd.tac.constellation.plugins.templates.SimpleEditPlugin) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) UndoRedo(org.openide.awt.UndoRedo) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) Test(org.testng.annotations.Test)

Example 12 with GraphWriteMethods

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

the class DefaultPluginEnvironmentNGTest method testExecuteEditPluginNowThrowsInterruptedException.

@Test(expectedExceptions = InterruptedException.class)
public void testExecuteEditPluginNowThrowsInterruptedException() throws Exception {
    System.out.println("executeEditPluginNow");
    GraphWriteMethods graph = mock(GraphWriteMethods.class);
    Plugin plugin = mock(Plugin.class);
    InterruptedException interruptedException = mock(InterruptedException.class);
    PluginParameters parameters = mock(PluginParameters.class);
    boolean interactive = false;
    doThrow(interruptedException).when(plugin).run(any(GraphWriteMethods.class), any(PluginInteraction.class), any(PluginParameters.class));
    DefaultPluginEnvironment instance = new DefaultPluginEnvironment();
    Object expResult = null;
    Object result = instance.executeEditPluginNow(graph, plugin, parameters, interactive);
    assertEquals(result, expResult);
}
Also used : GraphWriteMethods(au.gov.asd.tac.constellation.graph.GraphWriteMethods) PluginInteraction(au.gov.asd.tac.constellation.plugins.PluginInteraction) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) Test(org.testng.annotations.Test)

Example 13 with GraphWriteMethods

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

the class DefaultPluginEnvironmentNGTest method testExecuteEditPluginNowThrowsRuntimeException.

@Test(expectedExceptions = RuntimeException.class)
public void testExecuteEditPluginNowThrowsRuntimeException() throws Exception {
    System.out.println("executeEditPluginNow");
    GraphWriteMethods graph = mock(GraphWriteMethods.class);
    Plugin plugin = mock(Plugin.class);
    RuntimeException runtimeException = mock(RuntimeException.class);
    PluginParameters parameters = mock(PluginParameters.class);
    boolean interactive = false;
    doThrow(runtimeException).when(plugin).run(any(GraphWriteMethods.class), any(PluginInteraction.class), any(PluginParameters.class));
    DefaultPluginEnvironment instance = new DefaultPluginEnvironment();
    instance.executeEditPluginNow(graph, plugin, parameters, interactive);
}
Also used : GraphWriteMethods(au.gov.asd.tac.constellation.graph.GraphWriteMethods) PluginInteraction(au.gov.asd.tac.constellation.plugins.PluginInteraction) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) Test(org.testng.annotations.Test)

Example 14 with GraphWriteMethods

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

the class DefaultPluginEnvironmentNGTest method testExecuteEditPluginNow.

/**
 * Test of executeEditPluginNow method, of class DefaultPluginEnvironment.
 */
@Test
public void testExecuteEditPluginNow() throws Exception {
    System.out.println("executeEditPluginNow");
    GraphWriteMethods graph = mock(GraphWriteMethods.class);
    Plugin plugin = mock(Plugin.class);
    PluginParameters parameters = mock(PluginParameters.class);
    boolean interactive = false;
    DefaultPluginEnvironment instance = new DefaultPluginEnvironment();
    Object expResult = null;
    Object result = instance.executeEditPluginNow(graph, plugin, parameters, interactive);
    assertEquals(result, expResult);
}
Also used : GraphWriteMethods(au.gov.asd.tac.constellation.graph.GraphWriteMethods) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) Test(org.testng.annotations.Test)

Example 15 with GraphWriteMethods

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

the class PlaceholderUtilities method collapsePlaceholders.

public static void collapsePlaceholders(final GraphWriteMethods graph, final Comparator<SchemaVertexType> dominanceComparator, final boolean debug) throws PluginException, InterruptedException {
    final List<Integer> placeholderIds = new ArrayList<>();
    final Map<Integer, List<Integer>> placeholderCorrelations = new HashMap<>();
    final Map<Integer, List<Integer>> placeholderActivity = new HashMap<>();
    final Map<Integer, Integer> placeholderNeighbours = new HashMap<>();
    final int vertexTypeAttributeId = AnalyticConcept.VertexAttribute.TYPE.get(graph);
    final int transactionTypeAttributeId = AnalyticConcept.TransactionAttribute.TYPE.get(graph);
    // collect placeholders, their transactions and their neighbours
    final int vertexCount = graph.getVertexCount();
    for (int vertexPosition = 0; vertexPosition < vertexCount; vertexPosition++) {
        final int vertexId = graph.getVertex(vertexPosition);
        final SchemaVertexType vertexType = graph.getObjectValue(vertexTypeAttributeId, vertexId);
        if (vertexType.isSubTypeOf(AnalyticConcept.VertexType.PLACEHOLDER)) {
            placeholderIds.add(vertexId);
            final List<Integer> placeholderCorrelationList = new ArrayList<>();
            final List<Integer> placeholderActivityList = new ArrayList<>();
            final int transactionCount = graph.getVertexTransactionCount(vertexId);
            for (int transactionPosition = 0; transactionPosition < transactionCount; transactionPosition++) {
                final int transactionId = graph.getVertexTransaction(vertexId, transactionPosition);
                final SchemaTransactionType transactionType = graph.getObjectValue(transactionTypeAttributeId, transactionId);
                if (transactionType.isSubTypeOf(AnalyticConcept.TransactionType.CORRELATION)) {
                    placeholderCorrelationList.add(transactionId);
                } else {
                    placeholderActivityList.add(transactionId);
                }
                final int neighbourId = graph.getTransactionSourceVertex(transactionId) == vertexId ? graph.getTransactionDestinationVertex(transactionId) : graph.getTransactionSourceVertex(transactionId);
                placeholderNeighbours.put(transactionId, neighbourId);
            }
            placeholderCorrelations.put(vertexId, placeholderCorrelationList);
            placeholderActivity.put(vertexId, placeholderActivityList);
        }
    }
    if (debug) {
        GraphOpener.getDefault().openGraph(new DualGraph((StoreGraph) graph.copy(), true), GraphNode.getGraphNode(graph.getId()).getName() + "-debug-stage1");
    }
    // choose lead vertices to replace placeholders
    placeholderIds.forEach(placeholderId -> {
        final int leadVertex;
        final List<Integer> placeholderCorrelationList = placeholderCorrelations.get(placeholderId);
        if (!placeholderCorrelationList.isEmpty()) {
            // calculate lead vertex
            final SchemaVertexType leadVertexType = placeholderCorrelationList.stream().map(placeholderNeighbours::get).map(neighbourId -> (SchemaVertexType) graph.getObjectValue(vertexTypeAttributeId, neighbourId)).sorted(dominanceComparator).findFirst().get();
            leadVertex = placeholderCorrelationList.stream().map(placeholderNeighbours::get).filter(neighbourId -> graph.getObjectValue(vertexTypeAttributeId, neighbourId).equals(leadVertexType)).findFirst().get();
            // move correlations from the placeholder to the lead vertex of the entity
            placeholderCorrelationList.forEach(correlationId -> {
                if (graph.getTransactionSourceVertex(correlationId) == placeholderId && graph.getTransactionDestinationVertex(correlationId) != leadVertex) {
                    graph.setTransactionSourceVertex(correlationId, leadVertex);
                } else if (graph.getTransactionDestinationVertex(correlationId) == placeholderId && graph.getTransactionSourceVertex(correlationId) != leadVertex) {
                    graph.setTransactionDestinationVertex(correlationId, leadVertex);
                } else {
                // Do nothing
                }
            });
            // move activity from the placeholder to the lead vertex of the entity
            final List<Integer> placeholderActivityList = placeholderActivity.get(placeholderId);
            placeholderActivityList.forEach(activityId -> {
                if (graph.getTransactionSourceVertex(activityId) == placeholderId) {
                    graph.setTransactionSourceVertex(activityId, leadVertex);
                } else {
                    graph.setTransactionDestinationVertex(activityId, leadVertex);
                }
            });
        }
    });
    if (debug) {
        GraphOpener.getDefault().openGraph(new DualGraph((StoreGraph) graph.copy(), true), GraphNode.getGraphNode(graph.getId()).getName() + "-debug-stage2");
    }
    // remove all placeholders
    placeholderIds.forEach(graph::removeVertex);
}
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) DatumProcessor(au.gov.asd.tac.constellation.graph.processing.DatumProcessor) ProcessingException(au.gov.asd.tac.constellation.graph.processing.ProcessingException) RecordStore(au.gov.asd.tac.constellation.graph.processing.RecordStore) SchemaTransactionTypeUtilities(au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionTypeUtilities) GraphOpener(au.gov.asd.tac.constellation.graph.file.opener.GraphOpener) GraphVertex(au.gov.asd.tac.constellation.graph.utilities.wrapper.GraphVertex) HashMap(java.util.HashMap) VisualConcept(au.gov.asd.tac.constellation.graph.schema.visual.concept.VisualConcept) ArrayList(java.util.ArrayList) Map(java.util.Map) Schema(au.gov.asd.tac.constellation.graph.schema.Schema) GraphDirection(au.gov.asd.tac.constellation.graph.utilities.wrapper.GraphDirection) SeparatorConstants(au.gov.asd.tac.constellation.utilities.text.SeparatorConstants) GraphWrapper(au.gov.asd.tac.constellation.graph.utilities.wrapper.GraphWrapper) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph) GraphRecordStoreUtilities(au.gov.asd.tac.constellation.graph.processing.GraphRecordStoreUtilities) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) Collectors(java.util.stream.Collectors) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) GraphTransaction(au.gov.asd.tac.constellation.graph.utilities.wrapper.GraphTransaction) PluginNotificationLevel(au.gov.asd.tac.constellation.plugins.PluginNotificationLevel) SchemaVertexType(au.gov.asd.tac.constellation.graph.schema.type.SchemaVertexType) GraphStep(au.gov.asd.tac.constellation.graph.utilities.wrapper.GraphStep) List(java.util.List) AnalyticConcept(au.gov.asd.tac.constellation.graph.schema.analytic.concept.AnalyticConcept) Record(au.gov.asd.tac.constellation.graph.processing.Record) Optional(java.util.Optional) GraphNode(au.gov.asd.tac.constellation.graph.node.GraphNode) Comparator(java.util.Comparator) SchemaVertexType(au.gov.asd.tac.constellation.graph.schema.type.SchemaVertexType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) SchemaTransactionType(au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionType) ArrayList(java.util.ArrayList) List(java.util.List) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph)

Aggregations

GraphWriteMethods (au.gov.asd.tac.constellation.graph.GraphWriteMethods)40 Test (org.testng.annotations.Test)26 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)16 PluginInteraction (au.gov.asd.tac.constellation.plugins.PluginInteraction)15 Plugin (au.gov.asd.tac.constellation.plugins.Plugin)10 HashMap (java.util.HashMap)9 PluginException (au.gov.asd.tac.constellation.plugins.PluginException)8 PluginParameter (au.gov.asd.tac.constellation.plugins.parameters.PluginParameter)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 Map (java.util.Map)8 Attribute (au.gov.asd.tac.constellation.graph.Attribute)7 VisualConcept (au.gov.asd.tac.constellation.graph.schema.visual.concept.VisualConcept)7 ArrayList (java.util.ArrayList)7 AnalyticConcept (au.gov.asd.tac.constellation.graph.schema.analytic.concept.AnalyticConcept)6 SimpleEditPlugin (au.gov.asd.tac.constellation.plugins.templates.SimpleEditPlugin)6 List (java.util.List)6 StoreGraph (au.gov.asd.tac.constellation.graph.StoreGraph)5 PluginInfo (au.gov.asd.tac.constellation.plugins.PluginInfo)5 PluginType (au.gov.asd.tac.constellation.plugins.PluginType)5