use of au.gov.asd.tac.constellation.graph.WritableGraph in project constellation by constellation-app.
the class SaveGraphUtilities method saveGraphToTemporaryDirectory.
/**
* Save the graph to the temporary directory for debugging.
* <p>
* The main use case of this method is debugging unit tests and so this
* method does extra checks to make sure one can interact with the graph
* build from a crude unit test.
*
* @param graph The graph
* @param filename The filename excluding the file extension
* @param makeInteractable If true, will modify the graph and add attributes
* to make sure the graph can be opened
* @throws IOException
* @throws InterruptedException
*/
public static void saveGraphToTemporaryDirectory(final Graph graph, final String filename, boolean makeInteractable) throws IOException, InterruptedException {
final File saveCreatedGraph = File.createTempFile(filename, FileExtensionConstants.STAR);
final WritableGraph wg = graph.getWritableGraph("make graph interactable", true);
try {
if (makeInteractable) {
makeGraphInteractable(wg);
}
} finally {
wg.commit();
}
final ReadableGraph rg = graph.getReadableGraph();
try {
final GraphJsonWriter writer = new GraphJsonWriter();
writer.writeGraphToZip(rg, saveCreatedGraph.getPath(), new TextIoProgress(false));
} finally {
rg.release();
}
}
use of au.gov.asd.tac.constellation.graph.WritableGraph in project constellation by constellation-app.
the class SaveGraphUtilities method saveGraphToTemporaryDirectory.
/**
* Save the graph to the temporary directory for debugging.
* <p>
* The main use case of this method is debugging unit tests and so this
* method does extra checks to make sure one can interact with the graph
* build from a crude unit test.
*
* @param graph The graph
* @param file The graph file
* @param makeInteractable If true, will modify the graph and add attributes
* to make sure the graph can be opened
* @throws IOException
* @throws InterruptedException
*/
public static void saveGraphToTemporaryDirectory(final Graph graph, final File file, boolean makeInteractable) throws IOException, InterruptedException {
final WritableGraph wg = graph.getWritableGraph("make graph interactable", true);
try {
if (makeInteractable) {
makeGraphInteractable(wg);
}
} finally {
wg.commit();
}
final ReadableGraph rg = graph.getReadableGraph();
try {
final GraphJsonWriter writer = new GraphJsonWriter();
writer.writeGraphToZip(rg, file.getPath(), new TextIoProgress(false));
} finally {
rg.release();
}
}
use of au.gov.asd.tac.constellation.graph.WritableGraph in project constellation by constellation-app.
the class CopyToNewGraphPlugin method makeGraph.
/**
* Created a new graph containing the selected elements of an existing
* graph.
*
* @param original The graph to copy elements from.
* @param newSchemaName If not null, create the new graph with this schema;
* otherwise, use the schema of the original graph.
* @param copyKeys If true, copy the keys.
* @param copyAll If true, copy everything regardless of whether it is
* selected or not.
*
* @return The new graph.
*
* @throws java.lang.InterruptedException if the process is canceled while
* it is running.
*/
public static Graph makeGraph(final GraphReadMethods original, final String newSchemaName, final boolean copyKeys, final boolean copyAll) throws InterruptedException {
final Schema schema = StringUtils.isNotBlank(newSchemaName) ? SchemaFactoryUtilities.getSchemaFactory(newSchemaName).createSchema() : original.getSchema();
final Graph dualGraph = new DualGraph(schema == null ? null : schema.getFactory().createSchema());
final WritableGraph graph = dualGraph.getWritableGraph("Make Graph", true);
try {
int vertexSelected = original.getAttribute(GraphElementType.VERTEX, VisualConcept.VertexAttribute.SELECTED.getName());
int transactionSelected = original.getAttribute(GraphElementType.TRANSACTION, VisualConcept.TransactionAttribute.SELECTED.getName());
int[] attributeTranslation = new int[1024];
// Copy the attributes.
for (GraphElementType type : GraphElementType.values()) {
int attributeCount = original.getAttributeCount(type);
for (int attributePosition = 0; attributePosition < attributeCount; attributePosition++) {
int originalAttributeId = original.getAttribute(type, attributePosition);
Attribute attribute = new GraphAttribute(original, originalAttributeId);
int newAttributeId = graph.addAttribute(type, attribute.getAttributeType(), attribute.getName(), attribute.getDescription(), attribute.getDefaultValue(), null);
if (originalAttributeId >= attributeTranslation.length) {
attributeTranslation = Arrays.copyOf(attributeTranslation, originalAttributeId * 2);
}
attributeTranslation[originalAttributeId] = newAttributeId;
if (type == GraphElementType.GRAPH) {
graph.setObjectValue(newAttributeId, 0, original.getObjectValue(originalAttributeId, 0));
}
}
if (copyKeys) {
final int[] keyAttributes = original.getPrimaryKey(type);
if (keyAttributes != null && keyAttributes.length > 0) {
for (int i = 0; i < keyAttributes.length; i++) {
keyAttributes[i] = attributeTranslation[keyAttributes[i]];
}
graph.setPrimaryKey(type, keyAttributes);
}
}
}
// Copy the named selection state.
final int namedSelectionAttr = original.getAttribute(GraphElementType.META, NamedSelectionState.ATTRIBUTE_NAME);
if (namedSelectionAttr != Graph.NOT_FOUND) {
final Object possibleState = original.getObjectValue(namedSelectionAttr, 0);
if (possibleState instanceof NamedSelectionState) {
final NamedSelectionState state = new NamedSelectionState((NamedSelectionState) possibleState);
graph.setObjectValue(attributeTranslation[namedSelectionAttr], 0, state);
}
}
// Copy the vertices.
int[] vertexTranslation = new int[original.getVertexCapacity()];
for (int position = 0; position < original.getVertexCount(); position++) {
int originalVertex = original.getVertex(position);
if (copyAll || vertexSelected == Graph.NOT_FOUND || original.getBooleanValue(vertexSelected, originalVertex)) {
int newVertex = graph.addVertex();
vertexTranslation[originalVertex] = newVertex;
for (int attributePosition = 0; attributePosition < original.getAttributeCount(GraphElementType.VERTEX); attributePosition++) {
int originalAttributeId = original.getAttribute(GraphElementType.VERTEX, attributePosition);
int newAttributeId = attributeTranslation[originalAttributeId];
graph.setObjectValue(newAttributeId, newVertex, original.getObjectValue(originalAttributeId, originalVertex));
}
}
}
// Copy the transactions.
for (int position = 0; position < original.getTransactionCount(); position++) {
int originalTransaction = original.getTransaction(position);
if (!copyAll) {
if (transactionSelected != Graph.NOT_FOUND && !original.getBooleanValue(transactionSelected, originalTransaction)) {
continue;
}
if (vertexSelected != Graph.NOT_FOUND) {
if (!original.getBooleanValue(vertexSelected, original.getTransactionSourceVertex(originalTransaction))) {
continue;
}
if (!original.getBooleanValue(vertexSelected, original.getTransactionDestinationVertex(originalTransaction))) {
continue;
}
}
}
int sourceVertex = vertexTranslation[original.getTransactionSourceVertex(originalTransaction)];
int destinationVertex = vertexTranslation[original.getTransactionDestinationVertex(originalTransaction)];
boolean directed = original.getTransactionDirection(originalTransaction) < 2;
int newTransaction = graph.addTransaction(sourceVertex, destinationVertex, directed);
for (int attributePosition = 0; attributePosition < original.getAttributeCount(GraphElementType.TRANSACTION); attributePosition++) {
int originalAttributeId = original.getAttribute(GraphElementType.TRANSACTION, attributePosition);
int newAttributeId = attributeTranslation[originalAttributeId];
graph.setObjectValue(newAttributeId, newTransaction, original.getObjectValue(originalAttributeId, originalTransaction));
}
}
} finally {
graph.commit();
}
return dualGraph;
}
use of au.gov.asd.tac.constellation.graph.WritableGraph in project constellation by constellation-app.
the class Animation method run.
private void run(final Graph graph) {
if (GraphNode.getGraphNode(graph) != null) {
final VisualManager manager = GraphNode.getGraphNode(graph).getVisualManager();
animationThread = new Thread(() -> {
WritableGraph wg;
while (true) {
try {
wg = graph.getWritableGraph(getName(), isSignificant());
break;
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
try {
editGraph(manager, wg);
} finally {
wg.commit();
}
});
animationThread.setName("Animation");
animationThread.start();
}
}
use of au.gov.asd.tac.constellation.graph.WritableGraph in project constellation by constellation-app.
the class LayersViewControllerNGTest method testRemoveBitmaskFromElements.
/**
* Test of removeBitmaskFromElements method, of class LayersViewController.
*/
@Test
public void testRemoveBitmaskFromElements() {
System.out.println("removeBitmaskFromElements");
// Setup a graph with elements
setupGraph();
final GraphManager gm = Mockito.mock(GraphManager.class);
when(gm.getActiveGraph()).thenReturn(graph);
// Create mock of DataAccessPane to return the query phase pane mock
try (MockedStatic<GraphManager> mockedStatic = Mockito.mockStatic(GraphManager.class)) {
mockedStatic.when(() -> GraphManager.getDefault()).thenReturn(gm);
WritableGraph wg;
try {
wg = graph.getWritableGraph("", true);
// Check Vertex set correctly
assertEquals(wg.getIntValue(layerMaskV, vxId1), 1);
assertEquals(wg.getFloatValue(layerVisibilityV, vxId1), 1.0f);
assertFalse(wg.getBooleanValue(selectedV, vxId1));
assertEquals(wg.getIntValue(layerMaskV, vxId2), 1);
assertEquals(wg.getFloatValue(layerVisibilityV, vxId2), 1.0f);
assertFalse(wg.getBooleanValue(selectedV, vxId2));
// Check Transaction set correctly
assertEquals(wg.getIntValue(layerMaskT, vxId1), 1);
assertEquals(wg.getFloatValue(layerVisibilityT, vxId1), 1.0f);
assertFalse(wg.getBooleanValue(selectedT, vxId1));
assertEquals(wg.getIntValue(layerMaskT, vxId2), 1);
assertEquals(wg.getFloatValue(layerVisibilityT, vxId2), 1.0f);
assertFalse(wg.getBooleanValue(selectedT, vxId2));
wg.commit();
} catch (final InterruptedException ex) {
Exceptions.printStackTrace(ex);
Thread.currentThread().interrupt();
}
// remove bitmask from elements - nothing should be on layer 5
int currentIndex = 5;
LayersViewController instance = LayersViewController.getDefault().init(null);
instance.removeBitmaskFromElements(currentIndex);
try {
wg = graph.getWritableGraph("", true);
// Check Vertex unchanged
assertEquals(wg.getIntValue(layerMaskV, vxId1), 1);
assertEquals(wg.getFloatValue(layerVisibilityV, vxId1), 1.0f);
assertFalse(wg.getBooleanValue(selectedV, vxId1));
assertEquals(wg.getIntValue(layerMaskV, vxId2), 1);
assertEquals(wg.getFloatValue(layerVisibilityV, vxId2), 1.0f);
assertFalse(wg.getBooleanValue(selectedV, vxId2));
// Check Transaction set correctly
assertEquals(wg.getIntValue(layerMaskT, vxId1), 1);
assertEquals(wg.getFloatValue(layerVisibilityT, vxId1), 1.0f);
assertFalse(wg.getBooleanValue(selectedT, vxId1));
assertEquals(wg.getIntValue(layerMaskT, vxId2), 1);
assertEquals(wg.getFloatValue(layerVisibilityT, vxId2), 1.0f);
assertFalse(wg.getBooleanValue(selectedT, vxId2));
// Set vx2 and tx2 into layer 2. Both are visibility 0 because current layer is 1.
wg.setIntValue(layerMaskV, vxId2, 0b11);
wg.setFloatValue(layerVisibilityV, vxId2, 0.0f);
wg.setIntValue(layerMaskT, txId2, 0b11);
wg.setFloatValue(layerVisibilityT, txId2, 0.0f);
wg.commit();
} catch (final InterruptedException ex) {
Exceptions.printStackTrace(ex);
Thread.currentThread().interrupt();
}
// remove bitmask from elements - should only remove the elements vx2 and tx2
currentIndex = 1;
instance.removeBitmaskFromElements(currentIndex);
try {
wg = graph.getWritableGraph("", true);
// Check Vertex 1 unchanged
assertEquals(wg.getIntValue(layerMaskV, vxId1), 1);
assertEquals(wg.getFloatValue(layerVisibilityV, vxId1), 1.0f);
assertFalse(wg.getBooleanValue(selectedV, vxId1));
// Check Vertex 2 changed - visibility 0.0 because no visibility update
assertEquals(wg.getIntValue(layerMaskV, vxId2), 1);
assertEquals(wg.getFloatValue(layerVisibilityV, vxId2), 0.0f);
assertFalse(wg.getBooleanValue(selectedV, vxId2));
// Check Transaction 1 unchanged
assertEquals(wg.getIntValue(layerMaskT, vxId1), 1);
assertEquals(wg.getFloatValue(layerVisibilityT, vxId1), 1.0f);
assertFalse(wg.getBooleanValue(selectedT, vxId1));
assertEquals(wg.getIntValue(layerMaskT, vxId2), 1);
assertEquals(wg.getFloatValue(layerVisibilityT, vxId2), 0.0f);
assertFalse(wg.getBooleanValue(selectedT, vxId2));
wg.commit();
} catch (final InterruptedException ex) {
Exceptions.printStackTrace(ex);
Thread.currentThread().interrupt();
}
}
}
Aggregations