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);
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations