use of au.gov.asd.tac.constellation.graph.GraphWriteMethods in project constellation by constellation-app.
the class SpanningTree method createSpanningTree.
/**
* Create a new graph representing a spanning tree of the original graph.
*
* @param isMinimal true for a minimal spanning tree, False for a maximal
* spanning tree.
* @param selectTxs if True, select the transactions in the tree in the
* original graph.
* @param rootVxId if specified (!=Graph.NOT_FOUND), make the tree's
* transactions directed.
*
* @return A new GraphWriteMethods representing the spanning tree.
*/
public GraphWriteMethods createSpanningTree(final boolean isMinimal, final boolean selectTxs, final int rootVxId) {
final GraphWriteMethods tree = new StoreGraph();
final int nradiusTreeAttr = VisualConcept.VertexAttribute.NODE_RADIUS.ensure(tree, false);
final int nradiusAttr = VisualConcept.VertexAttribute.NODE_RADIUS.ensure(wg);
final int vxCount = wg.getVertexCount();
// Get a list of all links sorted by weight.
final int linkCount = wg.getLinkCount();
final ArrayList<Integer> links = new ArrayList<>(linkCount);
for (int position = 0; position < linkCount; position++) {
final int linkId = wg.getLink(position);
links.add(linkId);
}
Collections.sort(links, new LinkSorter(wg, isMinimal));
// Put each vertex in its own tree (where a tree is conveniently named after its root.
final Map<Integer, Set<Integer>> treeVxs = new HashMap<>();
final int[] vxTrees = new int[wg.getVertexCapacity()];
Arrays.fill(vxTrees, Graph.NOT_FOUND);
for (int position = 0; position < vxCount; position++) {
final int vxId = wg.getVertex(position);
final Set<Integer> s = new HashSet<>();
s.add(vxId);
treeVxs.put(vxId, s);
vxTrees[vxId] = vxId;
}
// Build the new graph to contain the tree we'll build from the original graph.
origVxToTree = new int[wg.getVertexCapacity()];
Arrays.fill(origVxToTree, Graph.NOT_FOUND);
treeVxToOrig = new int[wg.getVertexCapacity()];
Arrays.fill(treeVxToOrig, Graph.NOT_FOUND);
for (int position = 0; position < vxCount; position++) {
final int vxId = wg.getVertex(position);
final int treeVxId = tree.addVertex();
final float nradius = nradiusAttr != Graph.NOT_FOUND ? wg.getFloatValue(nradiusAttr, vxId) : 1;
tree.setFloatValue(nradiusTreeAttr, treeVxId, nradius);
origVxToTree[vxId] = treeVxId;
treeVxToOrig[treeVxId] = vxId;
}
// Iterate through the sorted links, looking for vertices in different trees.
final Set<Integer> spanningLinks = new HashSet<>();
for (final Integer linkId : links) {
final int vx0Id = wg.getLinkLowVertex(linkId);
final int vx1Id = wg.getLinkHighVertex(linkId);
final int tree1Name = vxTrees[vx0Id];
final int tree2Name = vxTrees[vx1Id];
if (tree1Name != tree2Name) {
final Set<Integer> s1 = treeVxs.get(tree1Name);
final Set<Integer> s2 = treeVxs.get(tree2Name);
treeVxs.remove(tree2Name);
s1.addAll(s2);
for (final Integer vx2 : s2) {
vxTrees[vx2] = tree1Name;
}
spanningLinks.add(linkId);
final int tvx0Id = origVxToTree[vx0Id];
final int tvx1Id = origVxToTree[vx1Id];
tree.addTransaction(tvx0Id, tvx1Id, false);
}
}
// Make sure we have all of the vertices.
assert tree.getVertexCount() == wg.getVertexCount();
// This assert will fail if the graph has more than one component.
assert treeVxs.size() == 1;
if (rootVxId != Graph.NOT_FOUND) {
final int treeRootVxId = origVxToTree[rootVxId];
rootTree(tree, treeRootVxId);
}
if (selectTxs) {
for (final Iterator<Integer> e = spanningLinks.iterator(); e.hasNext(); ) {
final int slinkId = e.next();
final int ltxCount = wg.getLinkTransactionCount(slinkId);
for (int lpos = 0; lpos < ltxCount; lpos++) {
final int txId = wg.getLinkTransaction(slinkId, lpos);
wg.setBooleanValue(txSelectedId, txId, true);
}
}
}
return tree;
}
use of au.gov.asd.tac.constellation.graph.GraphWriteMethods in project constellation by constellation-app.
the class ArrangeByNodeAttributePlugin method edit.
@Override
public void edit(final GraphWriteMethods wg, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException {
final boolean threeD = parameters.getParameters().get(THREE_D_PARAMETER_ID).getBooleanValue();
final String attribute = parameters.getParameters().get(ATTRIBUTE_PARAMETER_ID).getStringValue();
if (threeD) {
if (attribute != null) {
final SelectedInclusionGraph selectedGraph = new SelectedInclusionGraph(wg, SelectedInclusionGraph.Connections.NONE);
final Attribute attr = new GraphAttribute(wg, wg.getAttribute(GraphElementType.VERTEX, attribute));
selectedGraph.addAttributeToCopy(attr);
final GraphWriteMethods ig = selectedGraph.getInclusionGraph();
final int iattrId = ig.getAttribute(GraphElementType.VERTEX, attribute);
final LayerArranger arranger = new LayerArranger();
arranger.setLevelAttr(iattrId);
arranger.setMaintainMean(!selectedGraph.isArrangingAll());
arranger.arrange(ig);
selectedGraph.retrieveCoords();
}
} else {
if (attribute != null) {
final Arranger inner = new GridArranger();
final Arranger outer = new GridArranger();
final GraphTaxonomyArranger arranger = new ArrangeByGroupTaxonomy(inner, outer, Connections.NONE, attribute);
arranger.setMaintainMean(true);
final SelectedInclusionGraph selectedGraph = new SelectedInclusionGraph(wg, Connections.NONE);
selectedGraph.addAttributeToCopy(new GraphAttribute(wg, wg.getAttribute(GraphElementType.VERTEX, attribute)));
arranger.arrange(selectedGraph.getInclusionGraph());
selectedGraph.retrieveCoords();
}
}
}
use of au.gov.asd.tac.constellation.graph.GraphWriteMethods in project constellation by constellation-app.
the class DataAccessStateIoProviderNGTest method readObjectObjectJson.
@Test
public void readObjectObjectJson() throws IOException {
final ObjectMapper objectMapper = new ObjectMapper();
JsonNode root = objectMapper.readTree("{}");
final GraphWriteMethods graph = mock(GraphWriteMethods.class);
dataAccessStateIoProvider.readObject(ATTRIBUTE_ID, ELEMENT_ID, root, graph, null, null, null, null);
final ArgumentCaptor<DataAccessState> captor = ArgumentCaptor.forClass(DataAccessState.class);
verify(graph).setObjectValue(eq(ATTRIBUTE_ID), eq(ELEMENT_ID), captor.capture());
final List<Map<String, String>> state = captor.getValue().getState();
assertTrue(state.isEmpty());
}
use of au.gov.asd.tac.constellation.graph.GraphWriteMethods in project constellation by constellation-app.
the class DataAccessStateIoProviderNGTest method writeObjectIsDefaultValue.
@Test
public void writeObjectIsDefaultValue() throws IOException {
final Attribute attribute = mock(Attribute.class);
when(attribute.getId()).thenReturn(ATTRIBUTE_ID);
when(attribute.getName()).thenReturn("ATTR NAME");
final GraphWriteMethods graph = mock(GraphWriteMethods.class);
when(graph.isDefaultValue(ATTRIBUTE_ID, ELEMENT_ID)).thenReturn(true);
final JsonFactory factory = new JsonFactory();
final ByteArrayOutputStream output = new ByteArrayOutputStream();
final JsonGenerator jsonGenerator = factory.createGenerator(output);
dataAccessStateIoProvider.writeObject(attribute, ELEMENT_ID, jsonGenerator, graph, null, false);
jsonGenerator.flush();
assertEquals(new String(output.toByteArray(), StandardCharsets.UTF_8), "");
}
use of au.gov.asd.tac.constellation.graph.GraphWriteMethods in project constellation by constellation-app.
the class DataAccessStateIoProviderNGTest method readObjectNullJson.
@Test
public void readObjectNullJson() throws IOException {
final ObjectMapper objectMapper = new ObjectMapper();
JsonNode root = objectMapper.readTree("null");
final GraphWriteMethods graph = mock(GraphWriteMethods.class);
dataAccessStateIoProvider.readObject(ATTRIBUTE_ID, ELEMENT_ID, root, graph, null, null, null, null);
final ArgumentCaptor<DataAccessState> captor = ArgumentCaptor.forClass(DataAccessState.class);
verify(graph).setObjectValue(eq(ATTRIBUTE_ID), eq(ELEMENT_ID), captor.capture());
final List<Map<String, String>> state = captor.getValue().getState();
assertTrue(state.isEmpty());
}
Aggregations