use of au.gov.asd.tac.constellation.plugins.PluginException in project constellation by constellation-app.
the class ExportToExcelFilePlugin method execute.
@Override
public void execute(final PluginGraphs graphs, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
try (final SXSSFWorkbook workbook = new SXSSFWorkbook(SXSSFWorkbook.DEFAULT_WINDOW_SIZE)) {
final Sheet sheet = workbook.createSheet(sheetName);
// get the indexes of all visible columns
final List<Integer> visibleIndices = table.getColumns().stream().filter(column -> column.isVisible()).map(column -> table.getColumns().indexOf(column)).collect(Collectors.toList());
// iterate through the visible columns and print each ones name to the sheet
final Row headerRow = sheet.createRow(0);
visibleIndices.forEach(index -> {
final TableColumn<ObservableList<String>, ?> column = table.getColumns().get(index);
final Cell headerCell = headerRow.createCell(visibleIndices.indexOf(index));
headerCell.setCellValue(column.getText());
});
// store the current page so we can reset it after the export
final int currentPage = pagination.getCurrentPageIndex();
if (selectedOnly) {
// iterate through all the pages in the table and write the selected rows to the sheet
for (int i = 0; i < pagination.getPageCount(); i++) {
pagination.getPageFactory().call(i);
// Calculates the start index in the sheet based on the current table page
// Because only selected rows are included this could leave a gap in the sheet
// + 1 to skip the header
// + 1 to skip the header
final int startIndex = rowsPerPage * i + 1;
final Thread writeSheetThread = new Thread("Export to Excel File: Writing Sheet") {
@Override
public void run() {
// get a copy of the table data so that users can continue working
final List<ObservableList<String>> data = getTable().getSelectionModel().getSelectedItems();
writeRecords(sheet, visibleIndices, data, startIndex);
}
};
writeSheetThread.start();
writeSheetThread.join();
}
} else {
// iterate through all the pages in the table and write their rows to the sheet
for (int i = 0; i < pagination.getPageCount(); i++) {
pagination.getPageFactory().call(i);
// Calculates the start index in the sheet based on the current table page
// + 1 to skip the header
final int startIndex = rowsPerPage * i + 1;
final Thread writeSheetThread = new Thread("Export to Excel File: Writing Sheet") {
@Override
public void run() {
// get a copy of the table data so that users can continue working
final List<ObservableList<String>> data = getTable().getItems();
writeRecords(sheet, visibleIndices, data, startIndex);
}
};
writeSheetThread.start();
writeSheetThread.join();
}
}
// call the page factory function once more to go back to the original page index
pagination.getPageFactory().call(currentPage);
// The sheet has now been created. Time to write it to the file
final Thread outputThread = new Thread("Export to Excel File: Writing File") {
@Override
public void run() {
try (final FileOutputStream fileStream = new FileOutputStream(getFile())) {
workbook.write(fileStream);
LOGGER.log(Level.INFO, "Table View data written to Excel file");
} catch (final IOException ex) {
interaction.notify(PluginNotificationLevel.ERROR, ex.getLocalizedMessage());
}
workbook.dispose();
}
};
outputThread.start();
outputThread.join();
} catch (final IOException ex) {
throw new PluginException(PluginNotificationLevel.ERROR, ex);
}
}
use of au.gov.asd.tac.constellation.plugins.PluginException in project constellation by constellation-app.
the class CompleteGraphBuilderPlugin method edit.
@Override
public void edit(final GraphWriteMethods graph, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
interaction.setProgress(0, 0, "Building...", true);
final Map<String, PluginParameter<?>> params = parameters.getParameters();
final int n = params.get(N_PARAMETER_ID).getIntegerValue();
final boolean randomWeights = params.get(RANDOM_WEIGHTS_PARAMETER_ID).getBooleanValue();
final List<String> nodeTypes = params.get(NODE_TYPES_PARAMETER_ID).getMultiChoiceValue().getChoices();
final List<String> transactionTypes = params.get(TRANSACTION_TYPES_PARAMETER_ID).getMultiChoiceValue().getChoices();
// Random countries to put in the graph
final List<String> countries = new ArrayList<>();
countries.add("Australia");
countries.add("Brazil");
countries.add("China");
countries.add("France");
countries.add("Japan");
countries.add("New Zealand");
countries.add("South Africa");
countries.add("United Arab Emirates");
countries.add("United Kingdom");
countries.add("United States");
final int vxIdentifierAttr = VisualConcept.VertexAttribute.IDENTIFIER.ensure(graph);
final int vxTypeAttr = AnalyticConcept.VertexAttribute.TYPE.ensure(graph);
final int vxIsGoodAttr = graph.addAttribute(GraphElementType.VERTEX, BooleanAttributeDescription.ATTRIBUTE_NAME, "isGood", null, false, null);
final int vxCountryAttr = SpatialConcept.VertexAttribute.COUNTRY.ensure(graph);
final int vxPinnedAttr = VisualConcept.VertexAttribute.PINNED.ensure(graph);
final int txIdAttr = VisualConcept.TransactionAttribute.IDENTIFIER.ensure(graph);
final int txTypeAttr = AnalyticConcept.TransactionAttribute.TYPE.ensure(graph);
final int txDateTimeAttr = TemporalConcept.TransactionAttribute.DATETIME.ensure(graph);
final VertexDecorators decorators;
decorators = new VertexDecorators(graph.getAttributeName(vxCountryAttr), graph.getAttributeName(vxPinnedAttr), null, graph.getAttributeName(vxIsGoodAttr));
final int decoratorsAttr = VisualConcept.GraphAttribute.DECORATORS.ensure(graph);
graph.setObjectValue(decoratorsAttr, 0, decorators);
final int[] vxIds = new int[n];
int vx = 0;
while (vx < n) {
final int vxId = graph.addVertex();
final String label = "Node_" + vxId;
graph.setStringValue(vxIdentifierAttr, vxId, label);
graph.setStringValue(vxTypeAttr, vxId, nodeTypes.get(r.nextInt(nodeTypes.size())));
graph.setBooleanValue(vxIsGoodAttr, vxId, r.nextInt(10) == 0);
graph.setStringValue(vxCountryAttr, vxId, countries.get(r.nextInt(countries.size())));
if (graph.getSchema() != null) {
graph.getSchema().completeVertex(graph, vxId);
}
vxIds[vx] = vxId;
vx++;
if (Thread.interrupted()) {
throw new InterruptedException();
}
}
// Create transactions between the nodes.
final Date d = new Date();
final int fourDays = 4 * 24 * 60 * 60 * 1000;
for (final int x : vxIds) {
for (final int y : vxIds) {
if (x == y) {
continue;
}
final int reciprocity = r.nextInt(3);
int numTimes = 1;
if (randomWeights) {
numTimes = r.nextInt(1 + r.nextInt(100));
}
for (int i = 0; i < numTimes; i++) {
int sxId = x;
int dxId = y;
if (randomWeights) {
switch(reciprocity) {
case 0:
final boolean random0 = r.nextBoolean();
if (random0) {
sxId = y;
dxId = x;
}
break;
case 1:
final int random1 = r.nextInt(5);
if (random1 == 0) {
sxId = y;
dxId = x;
}
break;
default:
final int randomDefault = r.nextInt(5);
if (randomDefault != 0) {
sxId = y;
dxId = x;
}
break;
}
}
final int e = graph.addTransaction(sxId, dxId, true);
graph.setLongValue(txDateTimeAttr, e, d.getTime() - r.nextInt(fourDays));
graph.setStringValue(txTypeAttr, e, transactionTypes.get(r.nextInt(transactionTypes.size())));
graph.setIntValue(txIdAttr, e, e);
if (graph.getSchema() != null) {
graph.getSchema().completeTransaction(graph, e);
}
if (Thread.interrupted()) {
throw new InterruptedException();
}
}
if (Thread.interrupted()) {
throw new InterruptedException();
}
}
if (Thread.interrupted()) {
throw new InterruptedException();
}
}
try {
if (n < 10000) {
// Do a trees layout.
PluginExecutor.startWith(ArrangementPluginRegistry.TREES).followedBy(InteractiveGraphPluginRegistry.RESET_VIEW).executeNow(graph);
} else {
// Do a grid layout.
PluginExecutor.startWith(ArrangementPluginRegistry.GRID_COMPOSITE).followedBy(InteractiveGraphPluginRegistry.RESET_VIEW).executeNow(graph);
}
} catch (final PluginException ex) {
LOGGER.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
}
interaction.setProgress(1, 0, "Completed successfully", true);
}
use of au.gov.asd.tac.constellation.plugins.PluginException in project constellation by constellation-app.
the class ImageGraphBuilderPlugin method edit.
@Override
protected void edit(final GraphWriteMethods graph, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
interaction.setProgress(0, 0, "Building...", true);
// imageFiles will be a list of files which extends from object type
@SuppressWarnings("unchecked") final List<File> imageFiles = (List<File>) parameters.getObjectValue(IMAGE_FILE_PARAMETER_ID);
for (final File imageFile : imageFiles) {
final ArrayList<BufferedImage> images = new ArrayList<>();
if (StringUtils.endsWithIgnoreCase(imageFile.getName(), FileExtensionConstants.GIF)) {
final ThreeTuple<List<BufferedImage>, List<Integer>, List<Integer>> loadedImageData;
try {
loadedImageData = loadImagesFromStream(imageFile);
} catch (final IOException ex) {
throw new PluginException(PluginNotificationLevel.ERROR, ex);
}
final BufferedImage firstImage = loadedImageData.getFirst().get(0);
images.add(firstImage);
final AffineTransform identity = new AffineTransform();
identity.setToIdentity();
for (int i = 1; i < loadedImageData.getFirst().size(); i++) {
final BufferedImage currentImage = loadedImageData.getFirst().get(i);
final BufferedImage image = new BufferedImage(firstImage.getWidth(), firstImage.getHeight(), firstImage.getType());
final Graphics2D g2d = image.createGraphics();
g2d.drawImage(images.get(i - 1), identity, null);
g2d.drawImage(currentImage, new AffineTransform(1, 0, 0, 1, loadedImageData.getSecond().get(i), loadedImageData.getThird().get(i)), null);
g2d.dispose();
images.add(image);
}
} else {
final BufferedImage loadedImageData;
try {
loadedImageData = loadImage(imageFile);
images.add(loadedImageData);
} catch (final IOException ex) {
LOGGER.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
}
}
final int vertexVisibilityAttributeId;
final boolean multipleFrames = images.size() > 1;
if (multipleFrames) {
final int attrLayers = graph.addAttribute(GraphElementType.GRAPH, IntegerAttributeDescription.ATTRIBUTE_NAME, "layers", "layers", 0, null);
graph.setIntValue(attrLayers, 0, images.size());
vertexVisibilityAttributeId = VisualConcept.VertexAttribute.VISIBILITY.get(graph);
} else {
vertexVisibilityAttributeId = Graph.NOT_FOUND;
}
final int vertexIdentifierAttributeId = VisualConcept.VertexAttribute.IDENTIFIER.get(graph);
final int vertexColorAttributeId = VisualConcept.VertexAttribute.COLOR.get(graph);
final int vertexBackgroundIconAttributeId = VisualConcept.VertexAttribute.BACKGROUND_ICON.get(graph);
final int vertexXAttributeId = VisualConcept.VertexAttribute.X.get(graph);
final int vertexYAttributeId = VisualConcept.VertexAttribute.Y.get(graph);
final int vertexZAttributeId = VisualConcept.VertexAttribute.Z.get(graph);
final int vertexX2AttributeId = VisualConcept.VertexAttribute.X2.get(graph);
final int vertexY2AttributeId = VisualConcept.VertexAttribute.Y2.get(graph);
final int vertexZ2AttributeId = VisualConcept.VertexAttribute.Z2.get(graph);
final int pixelXAttributeId = graph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, "pixelX", "pixelX", "", null);
final int pixelYAttributeId = graph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, "pixelY", "pixelY", "", null);
final int redAttributeId = graph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, "red", "red", "", null);
final int greenAttributeId = graph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, "green", "green", "", null);
final int blueAttributeId = graph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, "blue", "blue", "", null);
final int alphaAttributeId = graph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, "alpha", "alpha", "", null);
final int diffSouthAttributeId = graph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, "diffSouth", "diffSouth", "", null);
final int diffNorthAttributeId = graph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, "diffNorth", "diffNorth", "", null);
final int diffEastAttributeId = graph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, "diffEast", "diffEast", "", null);
final int diffWestAttributeId = graph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, "diffWest", "diffWest", "", null);
final int transactionWeightAttributeId = AnalyticConcept.TransactionAttribute.WEIGHT.get(graph);
final boolean useVertexAttributes = pixelXAttributeId != Graph.NOT_FOUND && pixelYAttributeId != Graph.NOT_FOUND && greenAttributeId != Graph.NOT_FOUND && redAttributeId != Graph.NOT_FOUND && blueAttributeId != Graph.NOT_FOUND && alphaAttributeId != Graph.NOT_FOUND && diffSouthAttributeId != Graph.NOT_FOUND && diffNorthAttributeId != Graph.NOT_FOUND && diffEastAttributeId != Graph.NOT_FOUND && diffWestAttributeId != Graph.NOT_FOUND;
final boolean useTransAttributes = transactionWeightAttributeId != Graph.NOT_FOUND;
int frame = 0;
for (BufferedImage image : images) {
final int w = image.getWidth();
final int h = image.getHeight();
int[][] vertexIds = new int[w][h];
final float zlen = multipleFrames ? 0 : Math.min(w, h) / 4F;
final float vis = frame / (float) (images.size() - 1);
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
final int rgb = image.getRGB(x, y);
final int a = (rgb >> 24) & 0xff;
final int r = (rgb >> 16) & 0xff;
final int g = (rgb >> 8) & 0xff;
final int b = rgb & 0xff;
// Generate Z using grayscale.
final float gray = 0;
final int vxId = vertexIds[x][y] = graph.addVertex();
graph.setStringValue(vertexIdentifierAttributeId, vxId, String.format("%d,%d", x, y));
final int yinv = h - y;
ConstructionUtilities.setxyz(graph, vxId, vertexXAttributeId, vertexYAttributeId, vertexZAttributeId, x * 2, yinv * 2, -gray * zlen / 255F);
ConstructionUtilities.setxyz(graph, vxId, vertexX2AttributeId, vertexY2AttributeId, vertexZ2AttributeId, x * 2, yinv * 2, 0);
graph.setStringValue(vertexBackgroundIconAttributeId, vxId, "Background.Flat Square");
ConstellationColor color = ConstellationColor.getColorValue(r / 255F, g / 255F, b / 255F, a / 255F);
graph.setObjectValue(vertexColorAttributeId, vxId, color);
if (multipleFrames) {
graph.setFloatValue(vertexVisibilityAttributeId, vxId, vis);
}
if (useVertexAttributes) {
graph.setFloatValue(pixelXAttributeId, vxId, x);
graph.setFloatValue(pixelYAttributeId, vxId, y);
graph.setFloatValue(redAttributeId, vxId, r);
graph.setFloatValue(greenAttributeId, vxId, g);
graph.setFloatValue(blueAttributeId, vxId, b);
graph.setFloatValue(alphaAttributeId, vxId, a);
if (x > 0 && x < w - 1 && y > 0 && y < h - 1) {
final int southRGB = image.getRGB(x, y + 1);
final int northRGB = image.getRGB(x, y - 1);
final int eastRGB = image.getRGB(x + 1, y);
final int westRGB = image.getRGB(x - 1, y);
graph.setFloatValue(diffSouthAttributeId, vxId, calculateDifference(r, g, b, southRGB));
graph.setFloatValue(diffNorthAttributeId, vxId, calculateDifference(r, g, b, northRGB));
graph.setFloatValue(diffEastAttributeId, vxId, calculateDifference(r, g, b, eastRGB));
graph.setFloatValue(diffWestAttributeId, vxId, calculateDifference(r, g, b, westRGB));
}
}
if (useTransAttributes) {
if (x > 0) {
final int transactionId = graph.addTransaction(vxId, vertexIds[x - 1][y], false);
ConstellationColor otherColor = (ConstellationColor) graph.getObjectValue(vertexColorAttributeId, vertexIds[x - 1][y]);
graph.setFloatValue(transactionWeightAttributeId, transactionId, calculateWeight(color, otherColor));
}
if (y > 0) {
final int transactionId = graph.addTransaction(vxId, vertexIds[x][y - 1], false);
ConstellationColor otherColor = (ConstellationColor) graph.getObjectValue(vertexColorAttributeId, vertexIds[x][y - 1]);
graph.setFloatValue(transactionWeightAttributeId, transactionId, calculateWeight(color, otherColor));
}
}
}
}
frame++;
}
}
PluginExecution.withPlugin(InteractiveGraphPluginRegistry.RESET_VIEW).executeNow(graph);
interaction.setProgress(1, 0, "Completed successfully", true);
}
use of au.gov.asd.tac.constellation.plugins.PluginException in project constellation by constellation-app.
the class AddRecordStore method addToGraph.
private static void addToGraph(final Graph graph, final RecordStore recordStore, final boolean completeWithSchema, final String arrange, final boolean resetView) {
final Plugin p = new ImportFromRestApiPlugin(recordStore, completeWithSchema, arrange);
PluginExecutor pe = PluginExecutor.startWith(p);
if (resetView) {
pe = pe.followedBy(InteractiveGraphPluginRegistry.RESET_VIEW);
}
try {
pe.executeNow(graph);
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
throw new RestServiceException(ex);
} catch (final PluginException ex) {
throw new RestServiceException(ex);
}
}
use of au.gov.asd.tac.constellation.plugins.PluginException in project constellation by constellation-app.
the class SetGraphValues method setGraphAttributes.
private static void setGraphAttributes(final Graph graph, final ArrayNode columns, final ArrayNode row) {
final Plugin p = new SetGraphAttributesFromRestApiPlugin(columns, row);
final PluginExecution pe = PluginExecution.withPlugin(p);
try {
pe.executeNow(graph);
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
throw new RestServiceException(ex);
} catch (final PluginException ex) {
throw new RestServiceException(ex);
}
}
Aggregations