Search in sources :

Example 96 with Graph

use of au.gov.asd.tac.constellation.graph.Graph in project constellation by constellation-app.

the class ListeningTopComponent method graphChanged.

@Override
public final void graphChanged(final GraphChangeEvent event) {
    LOGGER.finer("GraphChange");
    if (event != null && ignoredEvents.contains(event.getDescription())) {
        LOGGER.log(Level.FINER, "IgnoringEvent::{0}", event.getDescription());
        return;
    }
    ReadableGraph readableGraph = currentGraph.getReadableGraph();
    try {
        final Map<GlobalMonitor, Consumer<Graph>> globalMonitorsCopy;
        synchronized (globalMonitors) {
            globalMonitorsCopy = new HashMap<>(globalMonitors);
        }
        globalMonitorsCopy.forEach((monitor, handler) -> {
            LOGGER.finer("GraphChanged::CheckGlobal");
            if (monitor.update(readableGraph) == MonitorTransition.CHANGED) {
                LOGGER.finer("GraphChanged::UpdateGlobal");
                if (handler != null) {
                    handler.accept(currentGraph);
                }
            }
        });
        final Map<StructureMonitor, Consumer<Graph>> structureMonitorsCopy;
        synchronized (globalMonitors) {
            structureMonitorsCopy = new HashMap<>(structureMonitors);
        }
        structureMonitorsCopy.forEach((monitor, handler) -> {
            LOGGER.finer("GraphChanged::CheckStructure");
            if (monitor.update(readableGraph) == MonitorTransition.CHANGED) {
                LOGGER.finer("GraphChanged::UpdateStructure");
                if (handler != null) {
                    handler.accept(currentGraph);
                }
            }
        });
        final Map<AttributeCountMonitor, Consumer<Graph>> attributeCountMonitorsCopy;
        synchronized (globalMonitors) {
            attributeCountMonitorsCopy = new HashMap<>(attributeCountMonitors);
        }
        attributeCountMonitorsCopy.forEach((monitor, handler) -> {
            LOGGER.finer("GraphChanged::CheckAttributeCount");
            if (monitor.update(readableGraph) == MonitorTransition.CHANGED) {
                LOGGER.finer("GraphChanged::UpdateAttributeCount");
                if (handler != null) {
                    handler.accept(currentGraph);
                }
            }
        });
        final Map<AttributeValueMonitor, Tuple<Consumer<Graph>, MonitorTransitionFilter>> attributeMonitorsCopy;
        synchronized (globalMonitors) {
            attributeMonitorsCopy = new HashMap<>(attributeValueMonitors);
        }
        attributeMonitorsCopy.forEach((monitor, handlerPair) -> {
            LOGGER.finer("GraphChanged::CheckAttribute");
            final Consumer<Graph> handler = handlerPair.getFirst();
            final MonitorTransitionFilter transitionFilter = handlerPair.getSecond();
            monitor.update(readableGraph);
            if (transitionFilter.matchesTransitions(monitor)) {
                LOGGER.log(Level.FINER, "GraphChanged::UpdateAttribute::{0}", monitor.getName());
                if (handler != null) {
                    handler.accept(currentGraph);
                }
            }
        });
    } finally {
        readableGraph.release();
    }
    handleGraphChange(event);
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) AttributeCountMonitor(au.gov.asd.tac.constellation.graph.monitor.AttributeCountMonitor) AttributeValueMonitor(au.gov.asd.tac.constellation.graph.monitor.AttributeValueMonitor) GlobalMonitor(au.gov.asd.tac.constellation.graph.monitor.GlobalMonitor) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) MonitorTransitionFilter(au.gov.asd.tac.constellation.graph.monitor.MonitorTransitionFilter) Consumer(java.util.function.Consumer) StructureMonitor(au.gov.asd.tac.constellation.graph.monitor.StructureMonitor) Tuple(au.gov.asd.tac.constellation.utilities.datastructure.Tuple)

Example 97 with Graph

use of au.gov.asd.tac.constellation.graph.Graph in project constellation by constellation-app.

the class RestServiceUtilities method waitForGraphChange.

/**
 * Wait for another graph to become active and return the graph id.
 *
 * Creating and opening a graph are asynchronous operations, but we don't
 * want to put the burden of figuring out when the graph is ready on the
 * caller. Instead we wait for the asynchronous operation to finish by
 * comparing the id of the active graph to that of an existing graph
 * (possibly null). This could be fooled by the user changing graphs, but
 * what are you going to do?
 *
 * @param existingId
 *
 * @return The new graph id.
 */
public static String waitForGraphChange(final String existingId) {
    // Now we wait for the new graph to become active.
    // 
    int waits = 0;
    while (true) {
        // 
        try {
            Thread.sleep(1000);
        } catch (final InterruptedException ex) {
            Thread.currentThread().interrupt();
            throw new RestServiceException(ex);
        }
        final Graph newGraph = GraphManager.getDefault().getActiveGraph();
        final String newId = newGraph != null ? newGraph.getId() : null;
        if ((existingId == null && newId != null) || (existingId != null && newId != null && !existingId.equals(newId))) {
            // 
            return newId;
        }
        // 
        if (++waits > 10) {
            throw new RestServiceException("The new graph has taken too long to become active");
        }
    }
}
Also used : Graph(au.gov.asd.tac.constellation.graph.Graph)

Example 98 with Graph

use of au.gov.asd.tac.constellation.graph.Graph in project constellation by constellation-app.

the class AddRecordStore method callService.

@Override
public void callService(final PluginParameters parameters, final InputStream in, final OutputStream out) throws IOException {
    final String graphId = parameters.getStringValue(GRAPH_ID_PARAMETER_ID);
    final boolean completeWithSchema = parameters.getBooleanValue(COMPLETE_PARAMETER_ID);
    final String arrange = parameters.getStringValue(ARRANGE_PARAMETER_ID);
    final boolean resetView = parameters.getBooleanValue(RESET_PARAMETER_ID);
    final RecordStore rs = new GraphRecordStore();
    final ObjectMapper mapper = new ObjectMapper();
    final JsonNode json = mapper.readTree(in);
    final Graph graph = graphId == null ? RestUtilities.getActiveGraph() : GraphNode.getGraph(graphId);
    if (graph == null) {
        throw new RestServiceException(HTTP_UNPROCESSABLE_ENTITY, "No graph with id " + graphId);
    }
    // (We ignore the index array.)
    if (!json.hasNonNull(COLUMNS) || !json.get(COLUMNS).isArray()) {
        throw new RestServiceException("Could not find columns object containing column names");
    }
    if (!json.hasNonNull("data") || !json.get("data").isArray()) {
        throw new RestServiceException("Could not find data object containing data rows");
    }
    final ArrayNode columns = (ArrayNode) json.get(COLUMNS);
    final String[] headers = new String[columns.size()];
    for (int i = 0; i < headers.length; i++) {
        headers[i] = columns.get(i).asText();
    }
    final ArrayNode data = (ArrayNode) json.get("data");
    for (final Iterator<JsonNode> i = data.elements(); i.hasNext(); ) {
        final ArrayNode jrow = (ArrayNode) i.next();
        rs.add();
        boolean txFound = false;
        boolean txSourceFound = false;
        for (int ix = 0; ix < headers.length; ix++) {
            final String h = headers[ix];
            final JsonNode jn = jrow.get(ix);
            if (!jn.isNull()) {
                if (jn.getNodeType() == JsonNodeType.ARRAY) {
                    rs.set(h, RestServiceUtilities.toList((ArrayNode) jn));
                } else {
                    rs.set(h, jn.asText());
                }
            }
            txFound |= h.startsWith(GraphRecordStoreUtilities.TRANSACTION);
            txSourceFound |= TX_SOURCE.equals(h);
        }
        if (txFound && !txSourceFound) {
            rs.set(TX_SOURCE, API_SOURCE);
        }
    }
    addToGraph(graph, rs, completeWithSchema, arrange, resetView);
}
Also used : RestServiceException(au.gov.asd.tac.constellation.webserver.restapi.RestServiceException) Graph(au.gov.asd.tac.constellation.graph.Graph) RecordStore(au.gov.asd.tac.constellation.graph.processing.RecordStore) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 99 with Graph

use of au.gov.asd.tac.constellation.graph.Graph in project constellation by constellation-app.

the class GetGraphImage method callService.

@Override
public void callService(final PluginParameters parameters, final InputStream in, final OutputStream out) throws IOException {
    final Graph graph = GraphManager.getDefault().getActiveGraph();
    if (graph == null) {
        throw new RestServiceException(HTTP_UNPROCESSABLE_ENTITY, "No graph is opened in Constellation");
    }
    // This is asynchronous, so we need a Semaphore.
    // 
    final GraphNode graphNode = GraphNode.getGraphNode(graph);
    final VisualManager visualManager = graphNode.getVisualManager();
    final BufferedImage[] img1 = new BufferedImage[1];
    if (visualManager != null) {
        final Semaphore waiter = new Semaphore(0);
        visualManager.exportToBufferedImage(img1, waiter);
        waiter.acquireUninterruptibly();
        ImageIO.write(img1[0], "png", out);
    } else {
        throw new IOException("Graph image unavailable");
    }
}
Also used : RestServiceException(au.gov.asd.tac.constellation.webserver.restapi.RestServiceException) VisualManager(au.gov.asd.tac.constellation.utilities.visual.VisualManager) Graph(au.gov.asd.tac.constellation.graph.Graph) GraphNode(au.gov.asd.tac.constellation.graph.node.GraphNode) Semaphore(java.util.concurrent.Semaphore) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage)

Example 100 with Graph

use of au.gov.asd.tac.constellation.graph.Graph in project constellation by constellation-app.

the class NewGraph method callService.

@Override
public void callService(final PluginParameters parameters, final InputStream in, final OutputStream out) throws IOException {
    final String schemaParam = parameters.getStringValue(SCHEMA_PARAMETER_ID);
    String schemaName = null;
    for (final SchemaFactory schemaFactory : SchemaFactoryUtilities.getSchemaFactories().values()) {
        if (schemaFactory.isPrimarySchema() && (schemaParam == null || schemaParam.equals(schemaFactory.getName()))) {
            schemaName = schemaFactory.getName();
            break;
        }
    }
    if (schemaName == null) {
        throw new RestServiceException(HTTP_UNPROCESSABLE_ENTITY, String.format("Unknown schema %s", schemaParam));
    }
    // Creating a new graph is asynchronous; we want to hide this from the client.
    // 
    final Graph existingGraph = GraphManager.getDefault().getActiveGraph();
    final String existingId = existingGraph != null ? existingGraph.getId() : null;
    final Schema schema = SchemaFactoryUtilities.getSchemaFactory(schemaName).createSchema();
    final StoreGraph sg = new StoreGraph(schema);
    schema.newGraph(sg);
    final Graph dualGraph = new DualGraph(sg, false);
    final String graphName = SchemaFactoryUtilities.getSchemaFactory(schemaName).getLabel().trim().toLowerCase();
    GraphOpener.getDefault().openGraph(dualGraph, graphName);
    final String newId = RestServiceUtilities.waitForGraphChange(existingId);
    final ObjectMapper mapper = new ObjectMapper();
    final ObjectNode root = mapper.createObjectNode();
    root.put("id", newId);
    root.put("name", GraphNode.getGraphNode(newId).getDisplayName());
    root.put("schema", schemaName);
    mapper.writeValue(out, root);
}
Also used : SchemaFactory(au.gov.asd.tac.constellation.graph.schema.SchemaFactory) RestServiceException(au.gov.asd.tac.constellation.webserver.restapi.RestServiceException) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Schema(au.gov.asd.tac.constellation.graph.schema.Schema) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StoreGraph(au.gov.asd.tac.constellation.graph.StoreGraph)

Aggregations

Graph (au.gov.asd.tac.constellation.graph.Graph)211 ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)86 Test (org.testng.annotations.Test)65 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)40 WritableGraph (au.gov.asd.tac.constellation.graph.WritableGraph)36 ArrayList (java.util.ArrayList)32 Plugin (au.gov.asd.tac.constellation.plugins.Plugin)31 PluginException (au.gov.asd.tac.constellation.plugins.PluginException)30 StoreGraph (au.gov.asd.tac.constellation.graph.StoreGraph)26 DualGraph (au.gov.asd.tac.constellation.graph.locking.DualGraph)26 TableViewState (au.gov.asd.tac.constellation.views.tableview.state.TableViewState)21 List (java.util.List)21 ExecutionException (java.util.concurrent.ExecutionException)21 PluginExecution (au.gov.asd.tac.constellation.plugins.PluginExecution)20 PluginInteraction (au.gov.asd.tac.constellation.plugins.PluginInteraction)20 IOException (java.io.IOException)20 Schema (au.gov.asd.tac.constellation.graph.schema.Schema)17 VisualConcept (au.gov.asd.tac.constellation.graph.schema.visual.concept.VisualConcept)17 File (java.io.File)17 GraphElementType (au.gov.asd.tac.constellation.graph.GraphElementType)16