use of au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.CompositeNodeState in project constellation by constellation-app.
the class CompositeUtilities method destroyComposite.
/**
* Destroys a single composite node which the specified node either is, or
* is a constituent of.
*
* @param graph The graph.
* @param compositeStateAttr The graph ID of the composite state attribute
* for nodes.
* @param uniqueIdAttr The graph ID of the uniqueId attribute for
* transactions.
* @param vxId The ID of the specified node.
* @return A list of IDs of any newly expanded nodes. This will be an empty
* list if no composite was destroyed, or if the destroyed composite was
* already expanded.
*/
public static List<Integer> destroyComposite(final GraphWriteMethods graph, final int compositeStateAttr, final int uniqueIdAttr, final int vxId) {
final List<Integer> resultingNodes = new ArrayList<>();
final CompositeNodeState compositeState = (CompositeNodeState) graph.getObjectValue(compositeStateAttr, vxId);
if (compositeState != null) {
if (compositeState.isComposite()) {
final ContractedCompositeNodeState contractedState = compositeState.contractedState;
resultingNodes.addAll(contractedState.expand(graph, vxId));
while (true) {
try {
graph.validateKey(GraphElementType.VERTEX, false);
break;
} catch (final DuplicateKeyException ex) {
LOGGER.log(Level.INFO, "Duplicate Key has been found. Merging duplicate nodes");
final GraphElementMerger merger = new PrioritySurvivingGraphElementMerger();
merger.mergeElement(graph, GraphElementType.VERTEX, ex.getNewId(), ex.getExistingId());
}
}
resultingNodes.forEach(id -> {
simplifyCompositeTransactions(graph, uniqueIdAttr, id);
graph.setObjectValue(compositeStateAttr, id, null);
});
} else {
final int vertexCount = graph.getVertexCount();
for (int i = 0; i < vertexCount; i++) {
final int nxId = graph.getVertex(i);
final CompositeNodeState cns = ((CompositeNodeState) graph.getObjectValue(compositeStateAttr, nxId));
if (cns != null && cns.expandedState != null && cns.expandedState.getCompositeId().equals(compositeState.expandedState.getCompositeId())) {
simplifyCompositeTransactions(graph, uniqueIdAttr, nxId);
graph.setObjectValue(compositeStateAttr, nxId, null);
}
}
}
}
return resultingNodes;
}
use of au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.CompositeNodeState in project constellation by constellation-app.
the class CreateCompositeFromSelectionPlugin method edit.
@Override
public void edit(final GraphWriteMethods graph, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
final int selectedAttr = VisualConcept.VertexAttribute.SELECTED.get(graph);
if (selectedAttr != Graph.NOT_FOUND) {
final Set<Integer> selectedVerts = new HashSet<>();
for (int i = 0; i < graph.getVertexCount(); i++) {
final int vxId = graph.getVertex(i);
if (graph.getBooleanValue(selectedAttr, vxId)) {
selectedVerts.add(vxId);
}
}
if (selectedVerts.size() > 1) {
final int compositeStateAttr = AnalyticConcept.VertexAttribute.COMPOSITE_STATE.ensure(graph);
final int uniqueIdAttr = VisualConcept.TransactionAttribute.IDENTIFIER.get(graph);
final int identifierAttr = VisualConcept.VertexAttribute.IDENTIFIER.get(graph);
// We first destroy any composites which are selected or expanded and have constituents selected.
// For any composites, we also add any expanded ids and remove the destroyed composite id from the list of selected ids.
final Set<Integer> addedVerts = new HashSet<>();
final Set<Integer> removedVerts = new HashSet<>();
selectedVerts.forEach(id -> {
final List<Integer> resultingVerts = CompositeUtilities.destroyComposite(graph, compositeStateAttr, uniqueIdAttr, id);
if (!resultingVerts.isEmpty()) {
removedVerts.add(id);
addedVerts.addAll(resultingVerts);
}
});
// NOTE:: Remove before adding, because of id reuse!
selectedVerts.removeAll(removedVerts);
selectedVerts.addAll(addedVerts);
final String compositeIdentifier = String.format("%s + %d more...", graph.getStringValue(identifierAttr, selectedVerts.iterator().next()), selectedVerts.size() - 1);
String copyId = "";
for (int primarykeyAttr : graph.getPrimaryKey(GraphElementType.VERTEX)) {
final String val = graph.getStringValue(primarykeyAttr, selectedVerts.iterator().next());
copyId += graph.getAttributeName(primarykeyAttr) + "<" + StringUtils.defaultString(val) + ">";
}
final String compositeId = copyId;
// Make a record store representing the new composite that is about to be added
RecordStore newCompositeStore = new GraphRecordStore();
newCompositeStore.add();
newCompositeStore.set(GraphRecordStoreUtilities.SOURCE + GraphRecordStoreUtilities.ID, compositeId);
newCompositeStore.set(GraphRecordStoreUtilities.SOURCE + VisualConcept.VertexAttribute.IDENTIFIER, compositeIdentifier);
newCompositeStore.set(GraphRecordStoreUtilities.SOURCE + AnalyticConcept.VertexAttribute.TYPE, SchemaVertexTypeUtilities.getDefaultType());
// Construct and set an expanded composite node state for each of the nodes that will constitute the composite.
selectedVerts.forEach(id -> {
ExpandedCompositeNodeState expandedState = new ExpandedCompositeNodeState(newCompositeStore, compositeId, true, selectedVerts.size());
graph.setObjectValue(compositeStateAttr, id, new CompositeNodeState(id, expandedState));
});
// Create the composite by calling contract on the first node's expanded composite state.
((CompositeNodeState) graph.getObjectValue(compositeStateAttr, selectedVerts.iterator().next())).expandedState.contract(graph);
}
}
PluginExecution.withPlugin(VisualSchemaPluginRegistry.COMPLETE_SCHEMA).executeNow(graph);
}
use of au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.CompositeNodeState in project constellation by constellation-app.
the class CompositesNGTest method compositeCompositeAndNonCompositeTest.
@Test
public void compositeCompositeAndNonCompositeTest() throws InterruptedException, PluginException {
final WritableGraph wg = graph.getWritableGraph("test", true);
wg.getSchema().newGraph(wg);
int v0, v1, v2, v3;
final String v0name, v1name, v2name, v3name;
final int nameAttr;
final int selectedAttr;
final int compositeAttr;
try {
// Add four vertices
v0 = wg.addVertex();
wg.getSchema().newVertex(wg, v0);
v1 = wg.addVertex();
wg.getSchema().newVertex(wg, v1);
v2 = wg.addVertex();
wg.getSchema().newVertex(wg, v2);
v3 = wg.addVertex();
wg.getSchema().newVertex(wg, v3);
// Store the names of these vertices
nameAttr = VisualConcept.VertexAttribute.LABEL.get(wg);
v0name = wg.getStringValue(nameAttr, v0);
v1name = wg.getStringValue(nameAttr, v1);
v2name = wg.getStringValue(nameAttr, v2);
v3name = wg.getStringValue(nameAttr, v3);
// Add transactions from v0 to v1, and from v1 to v2
final int t0_1 = wg.addTransaction(v0, v1, true);
wg.getSchema().newTransaction(wg, t0_1);
final int t1_2 = wg.addTransaction(v1, v2, true);
wg.getSchema().newTransaction(wg, t1_2);
// Select v0 and v1, but not v2 or v3
selectedAttr = VisualConcept.VertexAttribute.SELECTED.get(wg);
wg.setBooleanValue(selectedAttr, v0, true);
wg.setBooleanValue(selectedAttr, v1, true);
wg.setBooleanValue(selectedAttr, v2, false);
wg.setBooleanValue(selectedAttr, v3, false);
// Make a composite from the selection then deselect all
PluginExecutor.startWith(PluginRegistry.get(InteractiveGraphPluginRegistry.CREATE_COMPOSITE_FROM_SELECTION)).followedBy(PluginRegistry.get(VisualGraphPluginRegistry.DESELECT_ALL)).executeNow(wg);
compositeAttr = AnalyticConcept.VertexAttribute.COMPOSITE_STATE.get(wg);
// Select v2 and the newly made composite
int compNode = wg.getVertex(0);
if (compNode == v2 || compNode == v3) {
compNode = wg.getVertex(1);
}
if (compNode == v2 || compNode == v3) {
compNode = wg.getVertex(2);
}
wg.setBooleanValue(selectedAttr, v2, true);
wg.setBooleanValue(selectedAttr, compNode, true);
// Make a composite from the selection
PluginExecution.withPlugin(PluginRegistry.get(InteractiveGraphPluginRegistry.CREATE_COMPOSITE_FROM_SELECTION)).executeNow(wg);
} finally {
wg.commit();
}
final ReadableGraph rg = graph.getReadableGraph();
try {
// Assert that there is one vertex that is a composite containing three nodes,
// and v3 as the non-composite
assertEquals(rg.getVertexCount(), 2);
final int compositeNode = rg.getVertex(0) == v3 ? rg.getVertex(1) : rg.getVertex(0);
final CompositeNodeState compositeState = (CompositeNodeState) rg.getObjectValue(compositeAttr, compositeNode);
assertNotNull(compositeState);
assertEquals(compositeState.getNumberOfNodes(), 3);
assertTrue(compositeState.isComposite());
assertEquals(rg.getStringValue(nameAttr, v3), v3name);
final CompositeNodeState compositeState0 = (CompositeNodeState) rg.getObjectValue(compositeAttr, v3);
assertNull(compositeState0);
// Assert that there are no transactions on the graph
assertEquals(rg.getTransactionCount(), 0);
} finally {
rg.release();
}
final WritableGraph wg2 = graph.getWritableGraph("test", true);
try {
// Expand all composites
PluginExecution.withPlugin(PluginRegistry.get(InteractiveGraphPluginRegistry.EXPAND_ALL_COMPOSITES)).executeNow(wg2);
} finally {
wg2.commit();
}
final ReadableGraph rg2 = graph.getReadableGraph();
try {
// Assert that the original four vertices exist; all with expanded composite states
// except v3
assertEquals(rg2.getVertexCount(), 4);
v0 = Graph.NOT_FOUND;
v1 = Graph.NOT_FOUND;
v2 = Graph.NOT_FOUND;
v3 = Graph.NOT_FOUND;
for (int i = 0; i < 4; i++) {
final int id = rg.getVertex(i);
final String name = rg.getStringValue(nameAttr, id);
if (name.equals(v0name)) {
v0 = id;
} else if (name.equals(v1name)) {
v1 = id;
} else if (name.equals(v2name)) {
v2 = id;
} else {
v3 = id;
}
}
assertEquals(rg2.getStringValue(nameAttr, v0), v0name);
final CompositeNodeState compositeState0 = (CompositeNodeState) rg2.getObjectValue(compositeAttr, v0);
assertNotNull(compositeState0);
assertEquals(compositeState0.getNumberOfNodes(), 3);
assertTrue(compositeState0.comprisesAComposite());
assertEquals(rg2.getStringValue(nameAttr, v1), v1name);
final CompositeNodeState compositeState1 = (CompositeNodeState) rg2.getObjectValue(compositeAttr, v1);
assertNotNull(compositeState1);
assertEquals(compositeState1.getNumberOfNodes(), 3);
assertTrue(compositeState1.comprisesAComposite());
assertEquals(rg2.getStringValue(nameAttr, v2), v2name);
final CompositeNodeState compositeState2 = (CompositeNodeState) rg2.getObjectValue(compositeAttr, v2);
assertNotNull(compositeState2);
assertEquals(compositeState2.getNumberOfNodes(), 3);
assertTrue(compositeState2.comprisesAComposite());
assertEquals(rg2.getStringValue(nameAttr, v3), v3name);
final CompositeNodeState compositeState3 = (CompositeNodeState) rg2.getObjectValue(compositeAttr, v3);
assertNull(compositeState3);
// assert that the original transactions exist and none others
assertEquals(rg.getTransactionCount(), 2);
final int l0_1 = rg2.getLink(v0, v1);
assertEquals(rg2.getLinkTransactionCount(l0_1), 1);
final int t0_1 = rg2.getLinkTransaction(l0_1, 0);
assertFalse(rg2.getTransactionDirection(t0_1) == Graph.FLAT);
assertEquals(rg2.getTransactionSourceVertex(t0_1), v0);
final int l1_2 = rg2.getLink(v1, v2);
assertEquals(rg2.getLinkTransactionCount(l1_2), 1);
final int t1_2 = rg2.getLinkTransaction(l1_2, 0);
assertFalse(rg2.getTransactionDirection(t1_2) == Graph.FLAT);
assertEquals(rg2.getTransactionSourceVertex(t1_2), v1);
} finally {
rg2.release();
}
}
use of au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.CompositeNodeState in project constellation by constellation-app.
the class CompositesNGTest method makeDeleteTransactionExpandTest.
@Test
public void makeDeleteTransactionExpandTest() throws InterruptedException, PluginException {
final WritableGraph wg = graph.getWritableGraph("test", true);
wg.getSchema().newGraph(wg);
final int v0, v1, v2;
final String v0name, v1name, v2name;
final int nameAttr;
final int selectedAttr;
final int compositeAttr;
try {
// Add three vertices
v0 = wg.addVertex();
wg.getSchema().newVertex(wg, v0);
v1 = wg.addVertex();
wg.getSchema().newVertex(wg, v1);
v2 = wg.addVertex();
wg.getSchema().newVertex(wg, v2);
// Store the names of these vertices
nameAttr = VisualConcept.VertexAttribute.LABEL.get(wg);
v0name = wg.getStringValue(nameAttr, v0);
v1name = wg.getStringValue(nameAttr, v1);
v2name = wg.getStringValue(nameAttr, v2);
// Add transactions from v0 to v1, and from v1 to v2
final int t0_1 = wg.addTransaction(v0, v1, true);
wg.getSchema().newTransaction(wg, t0_1);
final int t1_2 = wg.addTransaction(v1, v2, true);
wg.getSchema().newTransaction(wg, t1_2);
// Select v0 and v1, but not v2
selectedAttr = VisualConcept.VertexAttribute.SELECTED.get(wg);
wg.setBooleanValue(selectedAttr, v0, true);
wg.setBooleanValue(selectedAttr, v1, true);
wg.setBooleanValue(selectedAttr, v2, false);
// Make a composite from the selection
PluginExecution.withPlugin(PluginRegistry.get(InteractiveGraphPluginRegistry.CREATE_COMPOSITE_FROM_SELECTION)).executeNow(wg);
compositeAttr = AnalyticConcept.VertexAttribute.COMPOSITE_STATE.get(wg);
// Delete the transaction between the composite and the non-composite then expand all composites
final int compositeNode = wg.getVertex(0) != v2 ? wg.getVertex(0) : wg.getVertex(1);
final int compositeLink = wg.getLink(compositeNode, v2);
final int compositeTransaction = wg.getLinkTransaction(compositeLink, 0);
wg.removeTransaction(compositeTransaction);
PluginExecution.withPlugin(PluginRegistry.get(InteractiveGraphPluginRegistry.EXPAND_ALL_COMPOSITES)).executeNow(wg);
} finally {
wg.commit();
}
final ReadableGraph rg = graph.getReadableGraph();
try {
// Assert that there are three nodes, that v2 is the same with a null composite state,
// and that the other two have expanded composite states
assertEquals(rg.getVertexCount(), 3);
assertEquals(rg.getStringValue(nameAttr, v2), v2name);
assertNull(rg.getObjectValue(compositeAttr, v2));
final int v2pos = rg.getVertexPosition(v2);
final int comp0 = v2pos != 0 ? rg.getVertex(0) : rg.getVertex(2);
final int comp1 = v2pos != 1 ? rg.getVertex(1) : rg.getVertex(2);
final int expanded_v0 = rg.getStringValue(nameAttr, comp0).equals(v0name) ? comp0 : comp1;
final int expanded_v1 = expanded_v0 == comp0 ? comp1 : comp0;
final CompositeNodeState compositeState0 = (CompositeNodeState) rg.getObjectValue(compositeAttr, expanded_v0);
final CompositeNodeState compositeState1 = (CompositeNodeState) rg.getObjectValue(compositeAttr, expanded_v1);
assertNotNull(compositeState0);
assertEquals(compositeState0.getNumberOfNodes(), 2);
assertTrue(compositeState0.comprisesAComposite());
assertEquals(rg.getStringValue(nameAttr, expanded_v0), v0name);
assertNotNull(compositeState1);
assertEquals(compositeState1.getNumberOfNodes(), 2);
assertTrue(compositeState1.comprisesAComposite());
assertEquals(rg.getStringValue(nameAttr, expanded_v1), v1name);
// Assert that only the transaction between v0 and v1 exists and no others.
assertEquals(rg.getTransactionCount(), 1);
final int l0_1 = rg.getLink(expanded_v0, expanded_v1);
assertEquals(rg.getLinkTransactionCount(l0_1), 1);
final int t0_1 = rg.getLinkTransaction(l0_1, 0);
assertEquals(rg.getTransactionSourceVertex(t0_1), expanded_v0);
assertFalse(rg.getTransactionDirection(t0_1) == Graph.FLAT);
final int l0_2 = rg.getLink(expanded_v0, v2);
assertEquals(l0_2, Graph.NOT_FOUND);
final int l1_2 = rg.getLink(expanded_v1, v2);
assertEquals(l1_2, Graph.NOT_FOUND);
} finally {
rg.release();
}
}
use of au.gov.asd.tac.constellation.graph.schema.analytic.attribute.objects.CompositeNodeState in project constellation by constellation-app.
the class CompositesNGTest method compositeTwoCompositesTest.
@Test
public void compositeTwoCompositesTest() throws InterruptedException, PluginException {
final WritableGraph wg = graph.getWritableGraph("test", true);
wg.getSchema().newGraph(wg);
int v0, v1, v2, v3;
final String v0name, v1name, v2name, v3name;
final int nameAttr;
final int selectedAttr;
final int compositeAttr;
try {
// Add four vertices
v0 = wg.addVertex();
wg.getSchema().newVertex(wg, v0);
v1 = wg.addVertex();
wg.getSchema().newVertex(wg, v1);
v2 = wg.addVertex();
wg.getSchema().newVertex(wg, v2);
v3 = wg.addVertex();
wg.getSchema().newVertex(wg, v3);
// Store the names of these vertices
nameAttr = VisualConcept.VertexAttribute.LABEL.get(wg);
v0name = wg.getStringValue(nameAttr, v0);
v1name = wg.getStringValue(nameAttr, v1);
v2name = wg.getStringValue(nameAttr, v2);
v3name = wg.getStringValue(nameAttr, v3);
// Add transactions from v0 to v1, and from v1 to v2
final int t0_1 = wg.addTransaction(v0, v1, true);
wg.getSchema().newTransaction(wg, t0_1);
final int t1_2 = wg.addTransaction(v1, v2, true);
wg.getSchema().newTransaction(wg, t1_2);
// Select v0 and v1, but not v2 or v3
selectedAttr = VisualConcept.VertexAttribute.SELECTED.get(wg);
wg.setBooleanValue(selectedAttr, v0, true);
wg.setBooleanValue(selectedAttr, v1, true);
wg.setBooleanValue(selectedAttr, v2, false);
wg.setBooleanValue(selectedAttr, v3, false);
// Make a composite from the selection then deselect all
PluginExecutor.startWith(PluginRegistry.get(InteractiveGraphPluginRegistry.CREATE_COMPOSITE_FROM_SELECTION)).followedBy(PluginRegistry.get(DeselectAllPlugin.class.getName())).executeNow(wg);
compositeAttr = AnalyticConcept.VertexAttribute.COMPOSITE_STATE.get(wg);
// Select v2 and v3
wg.setBooleanValue(selectedAttr, v2, true);
wg.setBooleanValue(selectedAttr, v3, true);
// Make a composite from the selection
PluginExecution.withPlugin(PluginRegistry.get(InteractiveGraphPluginRegistry.CREATE_COMPOSITE_FROM_SELECTION)).executeNow(wg);
// Select all and composite the two composites
PluginExecutor.startWith(VisualGraphPluginRegistry.SELECT_ALL).followedBy(PluginRegistry.get(InteractiveGraphPluginRegistry.CREATE_COMPOSITE_FROM_SELECTION)).executeNow(wg);
} finally {
wg.commit();
}
final ReadableGraph rg = graph.getReadableGraph();
try {
// Assert that there is one vertex that is a composite node containing four nodes,
assertEquals(rg.getVertexCount(), 1);
final int compositeNode = rg.getVertex(0);
final CompositeNodeState compositeState = (CompositeNodeState) rg.getObjectValue(compositeAttr, compositeNode);
assertNotNull(compositeState);
assertEquals(compositeState.getNumberOfNodes(), 4);
assertTrue(compositeState.isComposite());
// Assert that there are no transactions on the graph
assertEquals(rg.getTransactionCount(), 0);
} finally {
rg.release();
}
final WritableGraph wg2 = graph.getWritableGraph("test", true);
try {
// Expand all composites
PluginExecution.withPlugin(PluginRegistry.get(InteractiveGraphPluginRegistry.EXPAND_ALL_COMPOSITES)).executeNow(wg2);
} finally {
wg2.commit();
}
final ReadableGraph rg2 = graph.getReadableGraph();
try {
// Assert that the original four vertices exist in expanded composite form
assertEquals(rg2.getVertexCount(), 4);
v0 = Graph.NOT_FOUND;
v1 = Graph.NOT_FOUND;
v2 = Graph.NOT_FOUND;
v3 = Graph.NOT_FOUND;
for (int i = 0; i < 4; i++) {
final int id = rg.getVertex(i);
final String name = rg.getStringValue(nameAttr, id);
if (name.equals(v0name)) {
v0 = id;
} else if (name.equals(v1name)) {
v1 = id;
} else if (name.equals(v2name)) {
v2 = id;
} else {
v3 = id;
}
}
assertEquals(rg2.getStringValue(nameAttr, v0), v0name);
final CompositeNodeState compositeState0 = (CompositeNodeState) rg2.getObjectValue(compositeAttr, v0);
assertNotNull(compositeState0);
assertEquals(compositeState0.getNumberOfNodes(), 4);
assertTrue(compositeState0.comprisesAComposite());
assertEquals(rg2.getStringValue(nameAttr, v1), v1name);
final CompositeNodeState compositeState1 = (CompositeNodeState) rg2.getObjectValue(compositeAttr, v1);
assertNotNull(compositeState1);
assertEquals(compositeState1.getNumberOfNodes(), 4);
assertTrue(compositeState1.comprisesAComposite());
assertEquals(rg2.getStringValue(nameAttr, v2), v2name);
final CompositeNodeState compositeState2 = (CompositeNodeState) rg2.getObjectValue(compositeAttr, v2);
assertNotNull(compositeState2);
assertEquals(compositeState2.getNumberOfNodes(), 4);
assertTrue(compositeState2.comprisesAComposite());
assertEquals(rg2.getStringValue(nameAttr, v3), v3name);
final CompositeNodeState compositeState3 = (CompositeNodeState) rg2.getObjectValue(compositeAttr, v3);
assertNotNull(compositeState3);
assertEquals(compositeState3.getNumberOfNodes(), 4);
assertTrue(compositeState3.comprisesAComposite());
// assert that the original transactions exist and none others
assertEquals(rg.getTransactionCount(), 2);
final int l0_1 = rg2.getLink(v0, v1);
assertEquals(rg2.getLinkTransactionCount(l0_1), 1);
final int t0_1 = rg2.getLinkTransaction(l0_1, 0);
assertFalse(rg2.getTransactionDirection(t0_1) == Graph.FLAT);
assertEquals(rg2.getTransactionSourceVertex(t0_1), v0);
final int l1_2 = rg2.getLink(v1, v2);
assertEquals(rg2.getLinkTransactionCount(l1_2), 1);
final int t1_2 = rg2.getLinkTransaction(l1_2, 0);
assertFalse(rg2.getTransactionDirection(t1_2) == Graph.FLAT);
assertEquals(rg2.getTransactionSourceVertex(t1_2), v1);
} finally {
rg2.release();
}
}
Aggregations