use of au.gov.asd.tac.constellation.plugins.Plugin in project constellation by constellation-app.
the class LayerByTimePlugin method read.
@Override
public void read(final GraphReadMethods rg, final PluginInteraction interaction, final PluginParameters parameters) throws PluginException, InterruptedException {
// We have the dtAttr from the original wg: we should have been passed the label, but never mind.
// We need to get the label from the original, so we can get the dtAttr for the copy.
final String dtAttrOrig = parameters.getParameters().get(DATETIME_ATTRIBUTE_PARAMETER_ID).getStringValue();
if (dtAttrOrig == null) {
interaction.notify(PluginNotificationLevel.ERROR, "A date-time attribute must be specified.");
return;
}
final int dtAttrOrigId = rg.getAttribute(GraphElementType.TRANSACTION, dtAttrOrig);
if (dtAttrOrigId == Graph.NOT_FOUND) {
interaction.notify(PluginNotificationLevel.ERROR, "A valid date-time attribute must be specified.");
return;
}
Graph copy;
try {
final Plugin copyGraphPlugin = PluginRegistry.get(InteractiveGraphPluginRegistry.COPY_TO_NEW_GRAPH);
final PluginParameters copyParams = copyGraphPlugin.createParameters();
copyParams.getParameters().get(CopyToNewGraphPlugin.NEW_SCHEMA_NAME_PARAMETER_ID).setStringValue(rg.getSchema().getFactory().getName());
copyParams.getParameters().get(CopyToNewGraphPlugin.COPY_ALL_PARAMETER_ID).setBooleanValue(true);
copyParams.getParameters().get(CopyToNewGraphPlugin.COPY_KEYS_PARAMETER_ID).setBooleanValue(false);
PluginExecution.withPlugin(copyGraphPlugin).withParameters(copyParams).executeNow(rg);
copy = (Graph) copyParams.getParameters().get(CopyToNewGraphPlugin.NEW_GRAPH_OUTPUT_PARAMETER_ID).getObjectValue();
} catch (final PluginException ex) {
copy = null;
LOGGER.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
}
if (copy == null) {
// The copy failed, drop out now.
return;
}
final Attribute dt = new GraphAttribute(rg, dtAttrOrigId);
final WritableGraph wgcopy = copy.getWritableGraph("Layer by time", true);
try {
final int dtAttr = wgcopy.getAttribute(GraphElementType.TRANSACTION, dt.getName());
final boolean useIntervals = parameters.getParameters().get(LAYER_BY_PARAMETER_ID).getStringValue().equals(INTERVAL_METHOD);
final ZonedDateTime[] startEnd = parameters.getParameters().get(DATE_RANGE_PARAMETER_ID).getDateTimeRangeValue().getZonedStartEnd();
final ZonedDateTime start = startEnd[0];
final ZonedDateTime end = startEnd[1];
final boolean isTransactionLayers = parameters.getParameters().get(TRANSACTION_AS_LAYER_PARAMETER_ID).getBooleanValue();
// Establish new attributes.
// Create and store graph attributes.
final LayerName defaultName = new LayerName(Graph.NOT_FOUND, "Default");
final int timeLayerAttr = wgcopy.addAttribute(GraphElementType.TRANSACTION, LayerNameAttributeDescription.ATTRIBUTE_NAME, LAYER_NAME, "time layer", defaultName, null);
wgcopy.addAttribute(GraphElementType.GRAPH, IntegerAttributeDescription.ATTRIBUTE_NAME, NLAYERS, "The number of layers to layer by time", 1, null);
final int txColorAttr = wgcopy.getAttribute(GraphElementType.TRANSACTION, "color");
final int txGuideline = wgcopy.addAttribute(GraphElementType.TRANSACTION, BooleanAttributeDescription.ATTRIBUTE_NAME, "layer_guideline", "This transaction is a layer guideline", false, null);
final ConstellationColor guidelineColor = ConstellationColor.getColorValue(0.25F, 0.25F, 0.25F, 1F);
wgcopy.addAttribute(GraphElementType.VERTEX, IntegerAttributeDescription.ATTRIBUTE_NAME, ORIGINAL_ID_LABEL, "Original Node Id", -1, null);
final List<Float> values = new ArrayList<>();
final Map<Integer, List<Float>> remappedLayers = new HashMap<>();
final Map<Integer, String> displayNames = new HashMap<>();
if (useIntervals) {
final int intervalUnit = LAYER_INTERVALS.get(parameters.getParameters().get(UNIT_PARAMETER_ID).getStringValue());
final int intervalAmount = parameters.getParameters().get(AMOUNT_PARAMETER_ID).getIntegerValue();
buildIntervals(wgcopy, values, remappedLayers, displayNames, dtAttr, start.toInstant(), end.toInstant(), intervalUnit, intervalAmount);
} else {
final int calendarUnit = BIN_CALENDAR_UNITS.get(parameters.getParameters().get(UNIT_PARAMETER_ID).getStringValue());
final int binAmount = parameters.getParameters().get(AMOUNT_PARAMETER_ID).getIntegerValue();
buildBins(wgcopy, values, remappedLayers, displayNames, dtAttr, start.toInstant(), end.toInstant(), calendarUnit, binAmount);
}
final boolean keepTxColors = parameters.getParameters().get(KEEP_TX_COLORS_PARAMETER_ID).getBooleanValue();
final boolean drawTxGuides = parameters.getParameters().get(DRAW_TX_GUIDES_PARAMETER_ID).getBooleanValue();
// Modify the copied graph to show our layers.
int z = 0;
float step = getWidth(wgcopy) / values.size();
for (final Entry<Integer, List<Float>> entry : remappedLayers.entrySet()) {
for (final Entry<Float, List<Integer>> currentLayer : transactionLayers.entrySet()) {
if (entry.getValue().contains(currentLayer.getKey())) {
for (final int txId : currentLayer.getValue()) {
final float origLayer = currentLayer.getKey();
int newLayer = 0;
if (entry.getValue().contains(origLayer)) {
// Overwrite value
newLayer = entry.getKey();
}
final LayerName dn = new LayerName(newLayer, displayNames.get(newLayer));
wgcopy.setObjectValue(timeLayerAttr, txId, dn);
final float normLayer = newLayer / (remappedLayers.keySet().size() * 1F);
if (!keepTxColors) {
final Color heatmap = new Color(Color.HSBtoRGB((1 - normLayer) * 2F / 3F, 0.5F, 1));
final ConstellationColor color = ConstellationColor.getColorValue(heatmap.getRed() / 255F, heatmap.getGreen() / 255F, heatmap.getBlue() / 255F, 1F);
wgcopy.setObjectValue(txColorAttr, txId, color);
}
if (isTransactionLayers) {
transactionsAsLayers(wgcopy, txId, z, step);
} else {
nodesAsLayers(wgcopy, txId, newLayer);
}
}
}
}
if (isTransactionLayers) {
srcVxMap = dstVxMap;
dstVxMap = new HashMap<>();
z += step;
}
}
// Remove any outstanding transactions flagged for deletion
for (int txId = txToDelete.nextSetBit(0); txId >= 0; txId = txToDelete.nextSetBit(txId + 1)) {
wgcopy.removeTransaction(txId);
}
// Get rid of all of the nodes that don't have any transactions.
// By definition, the duplicates will have transactions between them, including the original layer
// (because we just deleted transactions that belong in different layers, leaving only the transactions
// that belong in the original layer).
final List<Integer> vertices = new ArrayList<>();
for (int position = 0; position < wgcopy.getVertexCount(); position++) {
final int vertexId = wgcopy.getVertex(position);
final int nTx = wgcopy.getVertexTransactionCount(vertexId);
if (nTx == 0) {
vertices.add(vertexId);
}
}
vertices.stream().forEach(wgcopy::removeVertex);
if (drawTxGuides) {
interaction.setProgress(5, 6, "Draw guide lines", false);
// We have to do this after the "remove node without transactions" step because we're adding more transactions.
if (!isTransactionLayers && remappedLayers.keySet().size() > 1) {
nodeIdToLayers.keySet().stream().forEach(origNodeId -> {
int prevNodeId = -1;
final BitSet layers = nodeIdToLayers.get(origNodeId);
for (int layer = layers.nextSetBit(0); layer >= 0; layer = layers.nextSetBit(layer + 1)) {
final int nodeId = layer == 0 ? origNodeId : nodeDups.get(String.format("%s/%s", origNodeId, layer));
if (prevNodeId != -1) {
final int sTxId = wgcopy.addTransaction(prevNodeId, nodeId, false);
wgcopy.setBooleanValue(txGuideline, sTxId, true);
wgcopy.setObjectValue(txColorAttr, sTxId, guidelineColor);
final LayerName dn = new LayerName(1107, "Guideline");
wgcopy.setObjectValue(timeLayerAttr, sTxId, dn);
}
prevNodeId = nodeId;
}
});
}
}
} finally {
wgcopy.commit();
}
}
use of au.gov.asd.tac.constellation.plugins.Plugin in project constellation by constellation-app.
the class DataAccessPaneNGTest method update_pass_non_null_graph.
@Test
public void update_pass_non_null_graph() {
final DataAccessTabPane dataAccessTabPane = mock(DataAccessTabPane.class);
final Tab currentTab = mock(Tab.class);
final QueryPhasePane currentQueryPhasePane = mock(QueryPhasePane.class);
final DataSourceTitledPane dataSourceTitledPane = mock(DataSourceTitledPane.class);
final Plugin plugin = mock(Plugin.class);
final PluginParameters pluginParameters = mock(PluginParameters.class);
final Graph graph = mock(Graph.class);
when(dataAccessPane.getDataAccessTabPane()).thenReturn(dataAccessTabPane);
when(dataAccessTabPane.getCurrentTab()).thenReturn(currentTab);
when(dataAccessTabPane.getQueryPhasePaneOfCurrentTab()).thenReturn(currentQueryPhasePane);
when(currentQueryPhasePane.getDataAccessPanes()).thenReturn(List.of(dataSourceTitledPane));
when(dataSourceTitledPane.getPlugin()).thenReturn(plugin);
when(dataSourceTitledPane.getParameters()).thenReturn(pluginParameters);
when(graph.getId()).thenReturn("graphId");
dataAccessPane.update(graph);
verify(plugin).updateParameters(graph, pluginParameters);
verify(dataAccessPane).update("graphId");
}
use of au.gov.asd.tac.constellation.plugins.Plugin in project constellation by constellation-app.
the class QueryPhasePaneNGTest method testRunPlugins.
@Test
public void testRunPlugins() {
// Initialize the current graph in the state.
DataAccessPaneState.setCurrentGraphId("GraphId");
final QueryPhasePane instance = spy(new QueryPhasePane(new HashMap<>(), null, null));
// Setup the graph manager
final GraphManager graphManager = mock(GraphManager.class);
final Graph graph = mock(Graph.class);
when(graphManager.getActiveGraph()).thenReturn(graph);
// Setup the global parameters
final GlobalParametersPane globalParametersPane = mock(GlobalParametersPane.class);
final PluginParameters globalPluginParameters = mock(PluginParameters.class);
final PluginParameter globalPluginParameter1 = mock(PluginParameter.class);
final PluginParameter globalPluginParameter2 = mock(PluginParameter.class);
doReturn(globalParametersPane).when(instance).getGlobalParametersPane();
when(globalParametersPane.getParams()).thenReturn(globalPluginParameters);
when(globalPluginParameters.getParameters()).thenReturn(Map.of("abc.parameter1", globalPluginParameter1, "global.parameter1", globalPluginParameter2));
when(globalPluginParameter1.getObjectValue()).thenReturn("GLOBAL PARAMETER 1");
// Setup data access panes
final DataSourceTitledPane dataSourceTitledPane1 = mock(DataSourceTitledPane.class);
final DataSourceTitledPane dataSourceTitledPane2 = mock(DataSourceTitledPane.class);
doReturn(List.of(dataSourceTitledPane1, dataSourceTitledPane2)).when(instance).getDataAccessPanes();
// Pane 1 is disabled so will not be run
when(dataSourceTitledPane1.isQueryEnabled()).thenReturn(false);
when(dataSourceTitledPane2.isQueryEnabled()).thenReturn(true);
// Setup the plugin for pane 2
final Plugin plugin = mock(Plugin.class);
when(plugin.getName()).thenReturn("Plugin Name");
when(dataSourceTitledPane2.getPlugin()).thenReturn(plugin);
// Pane 2 has two parameters. One of them matches in name to one of
// the global parameters which means its value will be overriden with
// the global value.
final PluginParameters pluginParameters = mock(PluginParameters.class);
final PluginParameter pluginParameter1 = mock(PluginParameter.class);
final PluginParameter pluginParameter2 = mock(PluginParameter.class);
when(pluginParameters.getParameters()).thenReturn(Map.of("abc.parameter1", pluginParameter1, "abc.parameter2", pluginParameter2));
when(pluginParameters.copy()).thenReturn(pluginParameters);
when(dataSourceTitledPane2.getParameters()).thenReturn(pluginParameters);
try (final MockedStatic<PluginRegistry> pluginRegistry = Mockito.mockStatic(PluginRegistry.class);
final MockedStatic<PluginExecution> pluginExecutionMockedStatic = Mockito.mockStatic(PluginExecution.class);
final MockedStatic<GraphManager> graphManagerMockedStatic = Mockito.mockStatic(GraphManager.class);
final MockedConstruction<PluginSynchronizer> pluginSynchMocks = Mockito.mockConstruction(PluginSynchronizer.class, (pluginSyncMock, cnxt) -> {
assertEquals(cnxt.arguments(), List.of(1));
})) {
// Not sure why this is being done but just retuning the same plugin
// to save creating another mock.
pluginRegistry.when(() -> PluginRegistry.get(plugin.getClass().getName())).thenReturn(plugin);
graphManagerMockedStatic.when(GraphManager::getDefault).thenReturn(graphManager);
// This is the future that will be returned when the plugin begins execution
final Future future = CompletableFuture.completedFuture("Plugin Complete!");
// This is the future of a plugin that was run previously
final Future existingFuture = CompletableFuture.completedFuture("Previous Plugin Complete!");
final PluginExecution pluginExecution = mock(PluginExecution.class);
pluginExecutionMockedStatic.when(() -> PluginExecution.withPlugin(plugin)).thenReturn(pluginExecution);
// We don't need to verify any of these again (with the exception of the plugin synchronizer)
// because a null pointer will happen if any of the params don't match up.
when(pluginExecution.withParameters(pluginParameters)).thenReturn(pluginExecution);
when(pluginExecution.waitingFor(List.of(existingFuture))).thenReturn(pluginExecution);
when(pluginExecution.synchronizingOn(any(PluginSynchronizer.class))).thenReturn(pluginExecution);
when(pluginExecution.executeLater(graph)).thenReturn(future);
// Verify that the return contains the plugin future as defined above
assertEquals(instance.runPlugins(List.of(existingFuture)), List.of(future));
// Verify the state's running plugin list has been updated
assertEquals(DataAccessPaneState.getRunningPlugins(), Map.of(future, "Plugin Name"));
// Verify the local plugin parameter was updated with the global parameter value
verify(pluginParameter1).setObjectValue("GLOBAL PARAMETER 1");
// Verify the created plugin synchronizer is passed to the plugin execution
verify(pluginExecution).synchronizingOn(pluginSynchMocks.constructed().get(0));
}
}
use of au.gov.asd.tac.constellation.plugins.Plugin in project constellation by constellation-app.
the class SaveResultsFileWriterNGTest method testWriteRecordStoreInvalidDataAccessResultsDir.
/**
* Test of writeRecordStore method for case when
* DataAccessPreferenceKeys.getDataAccessResultsDir() returns an invalid
* directory. Because directory (and hence generated file path) is invalid,
* and exception will be thrown and
* ConstellationLoggerHelper.exportPropertyBuilder called with appropriate
* ERROR status.
*/
@Test
public void testWriteRecordStoreInvalidDataAccessResultsDir() {
Plugin plugin = new ExampleClass();
TabularRecordStore tabularRecordStore = new TabularRecordStore();
mockedDataAccessPreferenceKeys.when(() -> DataAccessPreferenceUtilities.getDataAccessResultsDir()).thenReturn(new File("/BADDIR/"));
mockedConstellationLoggerHelper.when(() -> ConstellationLoggerHelper.exportPropertyBuilder(any(), any(), any(), any())).thenAnswer(invocation -> {
Object[] args = invocation.getArguments();
File passedFile = (File) args[2];
constellationLoggerHelperStatus = (String) args[3];
// Ensure the file was not created
fileCreated = passedFile.exists();
if (fileCreated) {
passedFile.delete();
}
// Return properties (which are ignored)
Properties properties = new Properties();
return properties;
});
try {
SaveResultsFileWriter.writeRecordStore(plugin, tabularRecordStore);
// Execution should not make it this far, an invalid file should result in an exception.
Assert.fail("PluginException is expected.");
} catch (Exception ex) {
mockedRecordStoreUtilities.verify(times(0), () -> RecordStoreUtilities.toCsv(Mockito.any(), Mockito.any()));
Assert.assertFalse(fileCreated, "Record store file was not created.");
Assert.assertEquals(constellationLoggerHelperStatus, ConstellationLoggerHelper.FAILURE, "ConstellationLoggerHelper passed status = FAILURE.");
}
}
use of au.gov.asd.tac.constellation.plugins.Plugin in project constellation by constellation-app.
the class SaveResultsFileWriterNGTest method testWriteRecordStore.
/**
* Test of writeRecordStore method for case when
* DataAccessPreferenceKeys.getDataAccessResultsDir() returns a valid
* directory. Confirm file is created with expected contents and that no
* exception is thrown. confirm
* ConstellationLoggerHelper.exportPropertyBuilder called with appropriate
* SUCCESS status. This test executes a code path through
* SaveResultsFileWriter.writeRecordStore that does not throw an exception.
*
* @throws java.lang.Exception
*/
@Test
public void testWriteRecordStore() throws Exception {
Plugin plugin = new ExampleClass();
TabularRecordStore tabularRecordStore = new TabularRecordStore();
String key = "TEST1KEY";
String value = "TEST1VALUE";
tabularRecordStore.add();
tabularRecordStore.set(key, value);
// This test actually creates the output file in user.home directory, checks its contents, and then removes it.
mockedDataAccessPreferenceKeys.when(DataAccessPreferenceUtilities::getDataAccessResultsDir).thenReturn(new File(System.getProperty("user.home")));
mockedRecordStoreUtilities.when(() -> RecordStoreUtilities.toCsv(Mockito.any(), Mockito.any())).thenCallRealMethod();
mockedConstellationLoggerHelper.when(() -> ConstellationLoggerHelper.exportPropertyBuilder(any(), any(), any(), any())).thenAnswer((var invocation) -> {
Object[] args = invocation.getArguments();
File passedFile = (File) args[2];
constellationLoggerHelperStatus = (String) args[3];
// Ensure the file was created
fileCreated = passedFile.exists();
if (fileCreated) {
// Read file and confirm it contains 3 rows
int rows = 0;
Scanner reader = new Scanner(passedFile);
while (reader.hasNextLine()) {
rows++;
String data = reader.nextLine();
if (rows == 1) {
dataValid = dataValid && (data.equals(key));
}
if (rows == 2) {
dataValid = dataValid && (data.equals(value));
}
if (rows >= 3) {
dataValid = false;
}
}
if (rows == 0) {
dataValid = false;
}
passedFile.delete();
}
// Return properties (which are ignored)
Properties properties = new Properties();
return properties;
});
SaveResultsFileWriter.writeRecordStore(plugin, tabularRecordStore);
mockedRecordStoreUtilities.verify(times(1), () -> RecordStoreUtilities.toCsv(Mockito.any(), Mockito.any()));
Assert.assertTrue(fileCreated, "Record store file was created.");
Assert.assertEquals(constellationLoggerHelperStatus, ConstellationLoggerHelper.SUCCESS, "ConstellationLoggerHelper passed status = SUCCESS.");
Assert.assertTrue(dataValid, "Record contents matches expected.");
}
Aggregations