Search in sources :

Example 1 with GraphJsonReader

use of au.gov.asd.tac.constellation.graph.file.io.GraphJsonReader in project constellation by constellation-app.

the class LoadTemplatePlugin method loadTemplate.

private void loadTemplate(final File loadFile, final String templateName) throws PluginException {
    try {
        final Graph graph = new GraphJsonReader().readGraphZip(loadFile, new HandleIoProgress("Loading Template..."));
        GraphOpener.getDefault().openGraph(graph, templateName);
    } catch (GraphParseException | IOException ex) {
        throw new PluginException(this, PluginNotificationLevel.ERROR, "Failed to open template", ex);
    }
}
Also used : Graph(au.gov.asd.tac.constellation.graph.Graph) HandleIoProgress(au.gov.asd.tac.constellation.utilities.gui.HandleIoProgress) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) GraphParseException(au.gov.asd.tac.constellation.graph.file.io.GraphParseException) IOException(java.io.IOException) GraphJsonReader(au.gov.asd.tac.constellation.graph.file.io.GraphJsonReader)

Example 2 with GraphJsonReader

use of au.gov.asd.tac.constellation.graph.file.io.GraphJsonReader in project constellation by constellation-app.

the class AutosaveStartup method run.

@Override
public void run() {
    synchronized (String.class) {
        // Look for existing autosaved in-memory graphs.
        final File[] saveFiles = AutosaveUtilities.getAutosaves(FileExtensionConstants.STAR_AUTOSAVE);
        final long now = new Date().getTime();
        for (final File f : saveFiles) {
            try {
                final Properties props = new Properties();
                try (InputStream in = new FileInputStream(f)) {
                    props.load(in);
                }
                final String dtprop = props.getProperty(AutosaveUtilities.DT);
                final String name = props.getProperty(AutosaveUtilities.NAME);
                final boolean unsaved = "true".equals(props.getProperty(AutosaveUtilities.UNSAVED));
                if (dtprop != null) {
                    if (unsaved) {
                        final String msg = String.format("Graph %s (autosaved at %s).%nDo you want to recover it?", name != null ? name : "<unknown>", dtprop);
                        final NotifyDescriptor nd = new NotifyDescriptor.Confirmation(msg, "Autosaved graph", NotifyDescriptor.YES_NO_OPTION, NotifyDescriptor.QUESTION_MESSAGE);
                        if (DialogDisplayer.getDefault().notify(nd) == NotifyDescriptor.YES_OPTION) {
                            // Load the autosaved graph away from the EDT.
                            new Thread() {

                                @Override
                                public void run() {
                                    setName(AUTOSAVE_THREAD_NAME);
                                    final String loading = String.format("Loading autosaved graph %s", name);
                                    try {
                                        // Remove the "_auto" from the end and load the matching graph.
                                        String path = f.getPath();
                                        path = path.substring(0, path.length() - 5);
                                        final Graph g = new GraphJsonReader().readGraphZip(new File(path), new HandleIoProgress(loading));
                                        GraphOpener.getDefault().openGraph(g, name, false);
                                        AutosaveUtilities.deleteAutosave(f);
                                    } catch (GraphParseException | IOException ex) {
                                        LOGGER.log(Level.WARNING, "Error loading graph", ex);
                                        NotifyDisplayer.display("Error loading graph: " + ex.getMessage(), NotifyDescriptor.ERROR_MESSAGE);
                                    }
                                }
                            }.start();
                        } else {
                            // If the user doesn't want it we get rid of it.
                            AutosaveUtilities.deleteAutosave(f);
                        }
                    } else if (now - f.lastModified() > PURGE_PERIOD_MS) {
                        // This autosave is old enough to be purged; the user won't remember the details of the graph.
                        AutosaveUtilities.deleteAutosave(f);
                    } else {
                    // Do nothing
                    }
                } else {
                    // Some information about this autosave is missing so get rid of it.
                    AutosaveUtilities.deleteAutosave(f);
                }
            } catch (IOException ex) {
                LOGGER.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
            }
        }
        // Start autosaving in the background.
        Autosaver.schedule(0);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) HandleIoProgress(au.gov.asd.tac.constellation.utilities.gui.HandleIoProgress) IOException(java.io.IOException) Properties(java.util.Properties) Date(java.util.Date) FileInputStream(java.io.FileInputStream) GraphJsonReader(au.gov.asd.tac.constellation.graph.file.io.GraphJsonReader) NotifyDescriptor(org.openide.NotifyDescriptor) Graph(au.gov.asd.tac.constellation.graph.Graph) File(java.io.File)

Example 3 with GraphJsonReader

use of au.gov.asd.tac.constellation.graph.file.io.GraphJsonReader in project constellation by constellation-app.

the class AutosaveGraphPluginNGTest method testExecute.

/**
 * Test of execute method, of class AutosaveGraphPlugin.
 *
 * @throws java.lang.Exception
 */
@Test
public void testExecute() throws Exception {
    final File saveDir = AutosaveUtilities.getAutosaveDir();
    final File saveFile = new File(saveDir, graph.getId() + FileExtensionConstants.STAR);
    // check the autosave file doesn't exist before running the plugin
    assertEquals(saveFile.exists(), false);
    TopComponent tc = new TopComponent();
    tc.setName("TestName");
    final GraphDataObject gdo = GraphObjectUtilities.createMemoryDataObject("graph", true);
    final GraphNode graphNode = new GraphNode(graph, gdo, tc, null);
    AutosaveGraphPlugin instance = new AutosaveGraphPlugin();
    PluginExecution.withPlugin(instance).executeNow(graph);
    // check that the autosave file does now exist
    assertEquals(saveFile.exists(), true);
    final Graph openSavedGraph = new GraphJsonReader().readGraphZip(saveFile, new TextIoProgress(false));
    final ReadableGraph rg = openSavedGraph.getReadableGraph();
    try {
        // check that the graph from the autosave matches the original graph
        assertEquals(rg.getVertexCount(), 7);
        assertEquals(rg.getStringValue(vAttrId, vxId1), "false");
        assertEquals(rg.getStringValue(vAttrId, vxId2), "true");
        assertEquals(rg.getStringValue(vAttrId, vxId3), "false");
        assertEquals(rg.getStringValue(vAttrId, vxId4), "false");
        assertEquals(rg.getStringValue(vAttrId, vxId5), "true");
        assertEquals(rg.getTransactionCount(), 5);
    } finally {
        rg.release();
        // deleting the file afterwards
        AutosaveUtilities.deleteAutosave(saveFile);
        graphNode.destroy();
    }
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) GraphDataObject(au.gov.asd.tac.constellation.graph.file.GraphDataObject) GraphNode(au.gov.asd.tac.constellation.graph.node.GraphNode) TextIoProgress(au.gov.asd.tac.constellation.utilities.gui.TextIoProgress) File(java.io.File) TopComponent(org.openide.windows.TopComponent) GraphJsonReader(au.gov.asd.tac.constellation.graph.file.io.GraphJsonReader) Test(org.testng.annotations.Test)

Example 4 with GraphJsonReader

use of au.gov.asd.tac.constellation.graph.file.io.GraphJsonReader in project constellation by constellation-app.

the class InfoMapMainNGTest method testMain.

/**
 * Test of InfoMapMain.
 *
 * @throws java.lang.Exception
 */
@Test
public void testMain() throws Exception {
    final String fnam = "ninetriangles" + GraphDataObject.FILE_EXTENSION;
    // A test graph.
    Graph graph;
    try (InputStream inStream = InfoMapMainNGTest.class.getResourceAsStream(fnam)) {
        graph = new GraphJsonReader().readGraphZip(fnam, inStream, new TextIoProgress(true));
    }
    final Config conf = new Config();
    conf.setNoFileOutput(false);
    conf.setVerbosity(1);
    conf.setNetworkFile(fnam);
    conf.setOutDirectory(System.getProperty("java.io.tmpdir"));
    conf.setPrintClu(true);
    conf.setPrintNodeRanks(true);
    conf.setPrintFlowNetwork(true);
    conf.setVerbosity(1);
    conf.setNumTrials(5);
    System.out.printf("fastHierarchicalSolution %d\n", conf.getFastHierarchicalSolution());
    System.out.printf("Parsing %s network from file '%s'... ", conf.isUndirected() ? "undirected" : "directed", conf.getNetworkFile());
    conf.setConnectionType(Config.ConnectionType.TRANSACTIONS);
    final ReadableGraph rg = graph.getReadableGraph();
    try {
        System.out.printf("vertices=%d, Transactions=%d edges=%d links=%d\n", rg.getVertexCount(), rg.getTransactionCount(), rg.getEdgeCount(), rg.getLinkCount());
        runInfoMap(conf, rg);
    } 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) InputStream(java.io.InputStream) Config(au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.io.Config) TextIoProgress(au.gov.asd.tac.constellation.utilities.gui.TextIoProgress) GraphJsonReader(au.gov.asd.tac.constellation.graph.file.io.GraphJsonReader) Test(org.testng.annotations.Test)

Example 5 with GraphJsonReader

use of au.gov.asd.tac.constellation.graph.file.io.GraphJsonReader in project constellation by constellation-app.

the class OpenGraph method callService.

@Override
public void callService(final PluginParameters parameters, final InputStream in, final OutputStream out) throws IOException {
    final String filePath = parameters.getStringValue(FILE_PARAMETER_ID);
    final String existingId = RestServiceUtilities.activeGraphId();
    final File fnam = new File(filePath).getAbsoluteFile();
    String name = fnam.getName();
    if (StringUtils.endsWithIgnoreCase(name, GraphDataObject.FILE_EXTENSION)) {
        name = name.substring(0, name.length() - GraphDataObject.FILE_EXTENSION.length());
    }
    try {
        final Graph g = new GraphJsonReader().readGraphZip(fnam, new HandleIoProgress(String.format("Loading graph %s...", fnam)));
        GraphOpener.getDefault().openGraph(g, name, false);
        final String newId = RestServiceUtilities.waitForGraphChange(existingId);
        final Graph graph = GraphNode.getGraphNode(newId).getGraph();
        final ObjectMapper mapper = new ObjectMapper();
        final ObjectNode root = mapper.createObjectNode();
        root.put("id", graph.getId());
        root.put("name", GraphNode.getGraphNode(graph.getId()).getDisplayName());
        root.put("schema", graph.getSchema().getFactory().getName());
        mapper.writeValue(out, root);
    } catch (final GraphParseException | FileNotFoundException ex) {
        throw new RestServiceException(HTTP_UNPROCESSABLE_ENTITY, ex.getMessage());
    }
}
Also used : RestServiceException(au.gov.asd.tac.constellation.webserver.restapi.RestServiceException) Graph(au.gov.asd.tac.constellation.graph.Graph) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HandleIoProgress(au.gov.asd.tac.constellation.utilities.gui.HandleIoProgress) FileNotFoundException(java.io.FileNotFoundException) GraphParseException(au.gov.asd.tac.constellation.graph.file.io.GraphParseException) File(java.io.File) GraphJsonReader(au.gov.asd.tac.constellation.graph.file.io.GraphJsonReader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

Graph (au.gov.asd.tac.constellation.graph.Graph)5 GraphJsonReader (au.gov.asd.tac.constellation.graph.file.io.GraphJsonReader)5 HandleIoProgress (au.gov.asd.tac.constellation.utilities.gui.HandleIoProgress)3 File (java.io.File)3 ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)2 GraphParseException (au.gov.asd.tac.constellation.graph.file.io.GraphParseException)2 TextIoProgress (au.gov.asd.tac.constellation.utilities.gui.TextIoProgress)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Test (org.testng.annotations.Test)2 WritableGraph (au.gov.asd.tac.constellation.graph.WritableGraph)1 GraphDataObject (au.gov.asd.tac.constellation.graph.file.GraphDataObject)1 DualGraph (au.gov.asd.tac.constellation.graph.locking.DualGraph)1 GraphNode (au.gov.asd.tac.constellation.graph.node.GraphNode)1 PluginException (au.gov.asd.tac.constellation.plugins.PluginException)1 Config (au.gov.asd.tac.constellation.plugins.algorithms.clustering.infomap.io.Config)1 RestServiceException (au.gov.asd.tac.constellation.webserver.restapi.RestServiceException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 FileInputStream (java.io.FileInputStream)1