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