Search in sources :

Example 96 with ReadableGraph

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());
    }
}
Also used : GraphWriteMethods(au.gov.asd.tac.constellation.graph.GraphWriteMethods) CopyGraphUtilities(au.gov.asd.tac.constellation.graph.utilities.io.CopyGraphUtilities) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) IntegerParameterValue(au.gov.asd.tac.constellation.plugins.parameters.types.IntegerParameterType.IntegerParameterValue) SimpleEditPlugin(au.gov.asd.tac.constellation.plugins.templates.SimpleEditPlugin) ArrayList(java.util.ArrayList) Graph(au.gov.asd.tac.constellation.graph.Graph) Future(java.util.concurrent.Future) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) PluginInteraction(au.gov.asd.tac.constellation.plugins.PluginInteraction) PluginParameter(au.gov.asd.tac.constellation.plugins.parameters.PluginParameter) PluginParametersPane(au.gov.asd.tac.constellation.plugins.gui.PluginParametersPane) PluginExecution(au.gov.asd.tac.constellation.plugins.PluginExecution) PluginRegistry(au.gov.asd.tac.constellation.plugins.PluginRegistry) ExecutorService(java.util.concurrent.ExecutorService) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) SeparatorConstants(au.gov.asd.tac.constellation.utilities.text.SeparatorConstants) Set(java.util.Set) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph) GraphRecordStoreUtilities(au.gov.asd.tac.constellation.graph.processing.GraphRecordStoreUtilities) PluginGraphs(au.gov.asd.tac.constellation.plugins.PluginGraphs) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) Executors(java.util.concurrent.Executors) PluginNotificationLevel(au.gov.asd.tac.constellation.plugins.PluginNotificationLevel) ExecutionException(java.util.concurrent.ExecutionException) GlobalParameters(au.gov.asd.tac.constellation.views.dataaccess.GlobalParameters) List(java.util.List) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) IntegerParameterType(au.gov.asd.tac.constellation.plugins.parameters.types.IntegerParameterType) VisualSchemaPluginRegistry(au.gov.asd.tac.constellation.graph.schema.visual.VisualSchemaPluginRegistry) SimplePlugin(au.gov.asd.tac.constellation.plugins.templates.SimplePlugin) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) ArrayList(java.util.ArrayList) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) ExecutionException(java.util.concurrent.ExecutionException) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph)

Example 97 with ReadableGraph

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();
    }
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) Test(org.testng.annotations.Test)

Example 98 with ReadableGraph

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();
        }
    }
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) Graph(au.gov.asd.tac.constellation.graph.Graph)

Example 99 with ReadableGraph

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);
    }
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) Graph(au.gov.asd.tac.constellation.graph.Graph)

Example 100 with ReadableGraph

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();
    }
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) CyclicBarrier(java.util.concurrent.CyclicBarrier)

Aggregations

ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)167 Test (org.testng.annotations.Test)62 Graph (au.gov.asd.tac.constellation.graph.Graph)57 WritableGraph (au.gov.asd.tac.constellation.graph.WritableGraph)43 ArrayList (java.util.ArrayList)40 GraphAttribute (au.gov.asd.tac.constellation.graph.GraphAttribute)26 HashMap (java.util.HashMap)16 Attribute (au.gov.asd.tac.constellation.graph.Attribute)14 GraphElementType (au.gov.asd.tac.constellation.graph.GraphElementType)12 StoreGraph (au.gov.asd.tac.constellation.graph.StoreGraph)12 GraphNode (au.gov.asd.tac.constellation.graph.node.GraphNode)12 TableViewState (au.gov.asd.tac.constellation.views.tableview.state.TableViewState)12 HashSet (java.util.HashSet)12 DualGraph (au.gov.asd.tac.constellation.graph.locking.DualGraph)11 List (java.util.List)10 GraphRecordStore (au.gov.asd.tac.constellation.graph.processing.GraphRecordStore)9 PluginParameter (au.gov.asd.tac.constellation.plugins.parameters.PluginParameter)9 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)9 FindRule (au.gov.asd.tac.constellation.views.find.advanced.FindRule)9 File (java.io.File)9