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);
}
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");
}
}
}
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);
}
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");
}
}
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);
}
Aggregations