use of au.gov.asd.tac.constellation.graph.ReadableGraph 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.graph.ReadableGraph in project constellation by constellation-app.
the class SelectAllPluginNGTest method testEdit1.
/**
* Test of edit method, of class SelectAllPlugin. select all edit with only
* vx on graph
*/
@Test
public void testEdit1() throws Exception {
System.out.println("select all edit with only vx on graph");
// Open a new graph
graph = new DualGraph(SchemaFactoryUtilities.getSchemaFactory(VisualSchemaFactory.VISUAL_SCHEMA_ID).createSchema());
int vxCount = 0;
final WritableGraph wg = graph.getWritableGraph("TEST", true);
try {
// Create Selected Attributes
selectedV = VisualConcept.VertexAttribute.SELECTED.ensure(wg);
selectedT = VisualConcept.TransactionAttribute.SELECTED.ensure(wg);
// Add vertices
vxId1 = wg.addVertex();
vxId2 = wg.addVertex();
// check default value is unselected
assertFalse(wg.getBooleanValue(selectedV, vxId1));
assertFalse(wg.getBooleanValue(selectedV, vxId2));
vxCount = wg.getVertexCount();
} finally {
wg.commit();
}
// run select all plugin
PluginExecution.withPlugin(new SelectAllPlugin()).executeNow(graph);
// Verify both selected and same amount of vx are present
final ReadableGraph rg = graph.getReadableGraph();
try {
assertTrue(rg.getBooleanValue(selectedV, vxId1));
assertTrue(rg.getBooleanValue(selectedV, vxId2));
assertEquals(rg.getVertexCount(), vxCount);
} finally {
rg.close();
}
// rerun plugin to ensure values are not only toggled, but are set explicitly as selected
// run select all plugin
PluginExecution.withPlugin(new SelectAllPlugin()).executeNow(graph);
// Verify both selected and same amount of vx are present
final ReadableGraph rg2 = graph.getReadableGraph();
try {
assertTrue(rg2.getBooleanValue(selectedV, vxId1));
assertTrue(rg2.getBooleanValue(selectedV, vxId2));
assertEquals(rg2.getVertexCount(), vxCount);
} finally {
rg2.close();
}
}
use of au.gov.asd.tac.constellation.graph.ReadableGraph in project constellation by constellation-app.
the class FindTopComponent method graphChanged.
/**
* Listen to attribute changes in the graph so we can reflect them across
* the find rules.
* <p>
* The event may be null, since we call this manually from setNode() after a
* graph change.
*
* @param evt PropertyChangeEvent.
*/
@Override
public void graphChanged(final GraphChangeEvent evt) {
if (graphNode != null) {
final Graph graph = graphNode.getGraph();
ReadableGraph rg = graph.getReadableGraph();
try {
final long amc = rg.getAttributeModificationCounter();
if (amc != attributeModificationCounter) {
determineAttributes();
}
} finally {
rg.release();
}
}
}
use of au.gov.asd.tac.constellation.graph.ReadableGraph in project constellation by constellation-app.
the class FindTopComponent method setNode.
/**
* Make the graph in the specified node the source for the Find Component.
* <p>
* If another graph is attached to the model, it is detached first.
*
* @param node The GraphNode containing the graph to be displayed.
*/
private void setNode(final GraphNode node) {
if (graphNode != null) {
final Graph graph = graphNode.getGraph();
// As we are navigating away from this graph, save the state and remove the listeners.
saveStateToGraph();
graph.removeGraphChangeListener(this);
replacePanel.reset();
toggleUI(false);
}
if (node != null) {
graphNode = node;
final Graph graph = graphNode.getGraph();
// Force a refresh as we are moving graphs!
attributeModificationCounter = -1;
determineAttributes();
graph.addGraphChangeListener(this);
ReadableGraph rg = graph.getReadableGraph();
try {
readStateFromGraph(rg);
} finally {
rg.release();
}
toggleUI(true);
} else {
graphNode = null;
// Hide interface:
toggleUI(false);
}
}
use of au.gov.asd.tac.constellation.graph.ReadableGraph in project constellation by constellation-app.
the class QueryServices method quickQuery.
/**
* Performs a 'quick query'.
* <p>
* This method determines the maximum number of needed threads, and assigns
* a work package to each thread. Upon all child threads completing their
* queries, it joins and returns the results.
*
* @param type The <code>GraphElementType</code> to perform a quick query
* on.
* @param content The string to find instances of across the graph.
* @return List of <code>FindResults</code>, with each
* <code>FindResult</code> representing an individual positive result to the
* query.
*
* @see ArrayList
* @see FindResult
* @see GraphElementType
*/
public List<FindResult> quickQuery(final GraphElementType type, final String content) {
ReadableGraph rg = graph.getReadableGraph();
try {
final int sampleSpaceSize = type.getElementCount(rg);
if (sampleSpaceSize > 0) {
final int neededThreads = Math.min(AVAILABLE_THREADS, (int) Math.ceil((double) sampleSpaceSize / (double) MAX_THRESHOLD));
final int loadPerThread = (int) Math.ceil((double) sampleSpaceSize / (double) neededThreads);
final CyclicBarrier barrier = new CyclicBarrier(neededThreads + 1);
try {
ThreadedFind[] worker = new ThreadedFind[neededThreads];
// Create the requisite number of workers:
for (int i = 0; i < neededThreads; i++) {
final int workloadLBound = i * loadPerThread;
final int workloadUBound = Math.min((sampleSpaceSize - 1), ((i + 1) * loadPerThread) - 1);
worker[i] = new ThreadedFind(rg, barrier, this, type, content, i, workloadLBound, workloadUBound);
// Start the worker now that it knows its workload:
Thread t = new Thread(worker[i]);
t.start();
}
} finally {
try {
barrier.await();
} catch (final InterruptedException ex) {
LOGGER.log(Level.SEVERE, THREAD_INTERRUPTED, ex);
Thread.currentThread().interrupt();
} catch (final BrokenBarrierException ex) {
LOGGER.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
}
}
// Successfully found content.
return findResults;
}
return findResults;
} finally {
rg.release();
}
}
Aggregations