use of au.gov.asd.tac.constellation.plugins.PluginException in project constellation by constellation-app.
the class WorkflowQueryPlugin method execute.
@Override
protected void execute(final PluginGraphs pluginGraphs, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
final Graph graph = pluginGraphs.getGraph();
final ReadableGraph readableGraph = graph.getReadableGraph();
// buildId batches
try {
queryBatches = GraphRecordStoreUtilities.getSelectedVerticesBatches(readableGraph, parameters.getIntegerValue(BATCH_SIZE_PARAMETER_ID));
} finally {
readableGraph.release();
}
pluginGraphs.waitAtGate(1);
// create a service for executing jobs, limiting concurrent executions to the max concurrent plugins parameter.
final int maxConcurrentPlugins = parameters.getIntegerValue(MAX_CONCURRENT_PLUGINS_PARAMETER_ID);
final ExecutorService workflowExecutor = Executors.newFixedThreadPool(maxConcurrentPlugins);
// schedule a job for each batch, where the job is to execute the defined workflow
final List<Future<?>> workerPlugins = new ArrayList<>();
final List<PluginException> exceptions = new ArrayList<>();
if (queryBatches.isEmpty()) {
queryBatches.add(new GraphRecordStore());
}
// run plugin once for every batch record store
queryBatches.forEach(batch -> {
final StoreGraph batchGraph = new StoreGraph(graph.getSchema() != null ? graph.getSchema().getFactory().createSchema() : null);
batchGraph.getSchema().newGraph(batchGraph);
CopyGraphUtilities.copyGraphTypeElements(readableGraph, batchGraph);
GraphRecordStoreUtilities.addRecordStoreToGraph(batchGraph, batch, true, true, null);
final WorkerQueryPlugin worker = new WorkerQueryPlugin(getWorkflow(), batchGraph, exceptions, getErrorHandlingPlugin(), addPartialResults());
workerPlugins.add(workflowExecutor.submit(() -> {
Thread.currentThread().setName(THREAD_POOL_NAME);
try {
PluginExecution.withPlugin(worker).withParameters(parameters).executeNow(graph);
} catch (InterruptedException | PluginException ex) {
throw new RuntimeException(ex);
}
}));
});
final int[] workerFailCount = { 0 };
for (Future<?> worker : workerPlugins) {
try {
worker.get();
} catch (InterruptedException ex) {
workerPlugins.forEach(workerToInterrupt -> workerToInterrupt.cancel(true));
throw ex;
} catch (ExecutionException ex) {
workerFailCount[0]++;
}
}
workflowExecutor.shutdown();
// if there were any errors, collect them and display them to the user
if (!exceptions.isEmpty()) {
final StringBuilder entireException = new StringBuilder();
entireException.append(workerFailCount[0]).append(" workers failed.").append(SeparatorConstants.NEWLINE);
exceptions.forEach(ex -> entireException.append(ex.getMessage()).append(SeparatorConstants.NEWLINE));
throw new PluginException(PluginNotificationLevel.ERROR, entireException.toString());
}
}
use of au.gov.asd.tac.constellation.plugins.PluginException in project constellation by constellation-app.
the class SelectTopNPlugin method edit.
@Override
protected void edit(final GraphWriteMethods graph, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
final String mode = parameters.getParameters().get(MODE_PARAMETER_ID).getStringValue();
final String typeCategory = parameters.getParameters().get(TYPE_CATEGORY_PARAMETER_ID).getStringValue();
final List<String> subTypes = parameters.getParameters().get(TYPE_PARAMETER_ID).getMultiChoiceValue().getChoices();
final int limit = parameters.getParameters().get(LIMIT_PARAMETER_ID).getIntegerValue();
if (mode == null || (!mode.equals(NODE) && !mode.equals(TRANSACTION))) {
throw new PluginException(PluginNotificationLevel.ERROR, "Invalid mode value provided");
}
if (typeCategory == null) {
throw new PluginException(PluginNotificationLevel.ERROR, "Select a type category");
}
if (subTypes.isEmpty()) {
throw new PluginException(PluginNotificationLevel.ERROR, "Select some types to perform the calculation");
}
final int vertexLabelAttribute = VisualConcept.VertexAttribute.LABEL.get(graph);
if (vertexLabelAttribute == Graph.NOT_FOUND) {
throw new PluginException(PluginNotificationLevel.ERROR, String.format(MISSING_PROPERTY_FORMAT, VisualConcept.VertexAttribute.LABEL.getName()));
}
final int vertexSelectedAttribute = VisualConcept.VertexAttribute.SELECTED.get(graph);
if (vertexSelectedAttribute == Graph.NOT_FOUND) {
throw new PluginException(PluginNotificationLevel.ERROR, String.format(MISSING_PROPERTY_FORMAT, VisualConcept.VertexAttribute.SELECTED.getName()));
}
final int vertexTypeAttribute = AnalyticConcept.VertexAttribute.TYPE.get(graph);
if (vertexTypeAttribute == Graph.NOT_FOUND) {
throw new PluginException(PluginNotificationLevel.ERROR, String.format(MISSING_PROPERTY_FORMAT, AnalyticConcept.VertexAttribute.TYPE.getName()));
}
final int transactionTypeAttribute = AnalyticConcept.TransactionAttribute.TYPE.get(graph);
if (transactionTypeAttribute == Graph.NOT_FOUND) {
throw new PluginException(PluginNotificationLevel.ERROR, String.format(MISSING_PROPERTY_FORMAT, AnalyticConcept.TransactionAttribute.TYPE.getName()));
}
// make a set of the highlighted nodes
final Set<Integer> selectedNodes = new HashSet<>();
final int vertexCount = graph.getVertexCount();
for (int position = 0; position < vertexCount; position++) {
final int vxId = graph.getVertex(position);
if (graph.getBooleanValue(vertexSelectedAttribute, vxId)) {
selectedNodes.add(vxId);
}
}
if (selectedNodes.isEmpty()) {
throw new PluginException(PluginNotificationLevel.ERROR, "Select at least 1 node");
}
// calculate the occurrences of destination vertices
int txId;
int sourceVxId;
int destinationVxId;
int targetVxId;
int step = 0;
SchemaVertexType destinationVertexType;
SchemaTransactionType transactionType;
final Map<Integer, Integer> occurrences = new HashMap<>();
for (final Integer vxId : selectedNodes) {
final String label = graph.getStringValue(vertexLabelAttribute, vxId);
interaction.setProgress(++step, selectedNodes.size(), String.format("Calculating top %s for %s", limit, label), true);
final int transactionCount = graph.getVertexTransactionCount(vxId);
for (int position = 0; position < transactionCount; position++) {
txId = graph.getVertexTransaction(vxId, position);
sourceVxId = graph.getTransactionSourceVertex(txId);
destinationVxId = graph.getTransactionDestinationVertex(txId);
targetVxId = vxId == sourceVxId ? destinationVxId : sourceVxId;
switch(mode) {
case NODE:
destinationVertexType = graph.getObjectValue(vertexTypeAttribute, targetVxId);
if (destinationVertexType != null && subTypes.contains(destinationVertexType.getName())) {
if (!occurrences.containsKey(targetVxId)) {
occurrences.put(targetVxId, 0);
}
occurrences.put(targetVxId, occurrences.get(targetVxId) + 1);
}
break;
case TRANSACTION:
transactionType = graph.getObjectValue(transactionTypeAttribute, txId);
if (transactionType != null && subTypes.contains(transactionType.getName())) {
if (!occurrences.containsKey(targetVxId)) {
occurrences.put(targetVxId, 0);
}
occurrences.put(targetVxId, occurrences.get(targetVxId) + 1);
}
break;
default:
break;
}
}
// make a map sorted by the count in descending order
final LinkedHashMap<Integer, Integer> sortedMap = occurrences.entrySet().stream().sorted(Map.Entry.comparingByValue(Collections.reverseOrder())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
sortedMap.keySet().stream().limit(limit).forEach(id -> graph.setBooleanValue(vertexSelectedAttribute, id, true));
interaction.setProgress(1, 0, "Selected " + sortedMap.size() + " nodes.", true);
}
interaction.setProgress(1, 0, "Completed successfully", true);
}
use of au.gov.asd.tac.constellation.plugins.PluginException in project constellation by constellation-app.
the class MergeNodesPlugin method edit.
@Override
public void edit(final GraphWriteMethods graph, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
int mergedCount = 0;
interaction.setProgress(0, 0, "Merging nodes...", true);
final String mergeNodeTypeName = parameters.getParameters().get(MERGE_TYPE_PARAMETER_ID).getStringValue();
if (mergeNodeTypeName == null) {
throw new PluginException(PluginNotificationLevel.ERROR, "Select a Merge By option.");
}
if (!MERGE_TYPES.containsKey(mergeNodeTypeName)) {
throw new PluginException(PluginNotificationLevel.FATAL, String.format("Merge node type %s not found.", mergeNodeTypeName));
}
final int threshold = parameters.getParameters().get(THRESHOLD_PARAMETER_ID).getIntegerValue();
final GraphElementMerger merger = MERGERS.get(parameters.getParameters().get(MERGER_PARAMETER_ID).getStringValue());
final Comparator<String> leadNodeChooser = VERTEX_CHOOSER.get(parameters.getParameters().get(LEAD_PARAMETER_ID).getStringValue());
final boolean selectedOnly = parameters.getParameters().get(SELECTED_PARAMETER_ID).getBooleanValue();
final MergeNodeType mergeNodeType = MERGE_TYPES.get(mergeNodeTypeName);
final Map<Integer, Set<Integer>> nodesToMerge;
try {
nodesToMerge = mergeNodeType.getNodesToMerge(graph, leadNodeChooser, threshold, selectedOnly);
} catch (MergeException ex) {
throw new PluginException(PluginNotificationLevel.ERROR, ex);
}
// perform the merge
for (final Map.Entry<Integer, Set<Integer>> entry : nodesToMerge.entrySet()) {
mergedCount += mergeVertices(graph, entry.getValue(), entry.getKey(), merger);
}
interaction.setProgress(1, 0, "Merged " + mergedCount + " nodes.", true);
PluginExecution.withPlugin(VisualSchemaPluginRegistry.COMPLETE_SCHEMA).executeNow(graph);
}
use of au.gov.asd.tac.constellation.plugins.PluginException in project constellation by constellation-app.
the class TestParametersPluginNGTest method testQueryException1.
/**
* Test of query method, of class TestParametersPlugin. Tests throwing of
* debug pluginException
*/
@Test(expectedExceptions = PluginException.class)
public void testQueryException1() throws Exception {
System.out.println("throw pluginexception1");
final TestParametersPlugin instance = new TestParametersPlugin();
final PluginParameters result = instance.createParameters();
final GraphRecordStore recordStore = new GraphRecordStore();
final DefaultPluginInteraction interaction = new DefaultPluginInteraction(null, null);
// Set plugin query name here before execution
result.getParameters().get(CoreGlobalParameters.QUERY_NAME_PARAMETER_ID).setStringValue("TESTPARAMETERSPLUGIN");
// Set plugin parameters here before execution
result.getParameters().get(TestParametersPlugin.LEVEL_PARAMETER_ID).setStringValue("Debug");
try {
instance.query(recordStore, interaction, result);
} catch (final PluginException ex) {
assertEquals(ex.getNotificationLevel(), PluginNotificationLevel.DEBUG);
throw ex;
}
}
use of au.gov.asd.tac.constellation.plugins.PluginException in project constellation by constellation-app.
the class TestParametersPluginNGTest method testQueryException3.
/**
* Test of query method, of class TestParametersPlugin. Tests throwing of
* warning pluginException
*/
@Test(expectedExceptions = PluginException.class)
public void testQueryException3() throws Exception {
System.out.println("throw pluginexception3");
final TestParametersPlugin instance = new TestParametersPlugin();
final PluginParameters result = instance.createParameters();
final GraphRecordStore recordStore = new GraphRecordStore();
final DefaultPluginInteraction interaction = new DefaultPluginInteraction(null, null);
// Set plugin query name here before execution
result.getParameters().get(CoreGlobalParameters.QUERY_NAME_PARAMETER_ID).setStringValue("TESTPARAMETERSPLUGIN");
// Set plugin parameters here before execution
result.getParameters().get(TestParametersPlugin.LEVEL_PARAMETER_ID).setStringValue("Warning");
try {
instance.query(recordStore, interaction, result);
} catch (final PluginException ex) {
assertEquals(ex.getNotificationLevel(), PluginNotificationLevel.WARNING);
throw ex;
}
}
Aggregations