use of au.gov.asd.tac.constellation.graph.processing.RecordStore 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.processing.RecordStore in project constellation by constellation-app.
the class AddRecordStore method callService.
@Override
public void callService(final PluginParameters parameters, final InputStream in, final OutputStream out) throws IOException {
final String graphId = parameters.getStringValue(GRAPH_ID_PARAMETER_ID);
final boolean completeWithSchema = parameters.getBooleanValue(COMPLETE_PARAMETER_ID);
final String arrange = parameters.getStringValue(ARRANGE_PARAMETER_ID);
final boolean resetView = parameters.getBooleanValue(RESET_PARAMETER_ID);
final RecordStore rs = new GraphRecordStore();
final ObjectMapper mapper = new ObjectMapper();
final JsonNode json = mapper.readTree(in);
final Graph graph = graphId == null ? RestUtilities.getActiveGraph() : GraphNode.getGraph(graphId);
if (graph == null) {
throw new RestServiceException(HTTP_UNPROCESSABLE_ENTITY, "No graph with id " + graphId);
}
// (We ignore the index array.)
if (!json.hasNonNull(COLUMNS) || !json.get(COLUMNS).isArray()) {
throw new RestServiceException("Could not find columns object containing column names");
}
if (!json.hasNonNull("data") || !json.get("data").isArray()) {
throw new RestServiceException("Could not find data object containing data rows");
}
final ArrayNode columns = (ArrayNode) json.get(COLUMNS);
final String[] headers = new String[columns.size()];
for (int i = 0; i < headers.length; i++) {
headers[i] = columns.get(i).asText();
}
final ArrayNode data = (ArrayNode) json.get("data");
for (final Iterator<JsonNode> i = data.elements(); i.hasNext(); ) {
final ArrayNode jrow = (ArrayNode) i.next();
rs.add();
boolean txFound = false;
boolean txSourceFound = false;
for (int ix = 0; ix < headers.length; ix++) {
final String h = headers[ix];
final JsonNode jn = jrow.get(ix);
if (!jn.isNull()) {
if (jn.getNodeType() == JsonNodeType.ARRAY) {
rs.set(h, RestServiceUtilities.toList((ArrayNode) jn));
} else {
rs.set(h, jn.asText());
}
}
txFound |= h.startsWith(GraphRecordStoreUtilities.TRANSACTION);
txSourceFound |= TX_SOURCE.equals(h);
}
if (txFound && !txSourceFound) {
rs.set(TX_SOURCE, API_SOURCE);
}
}
addToGraph(graph, rs, completeWithSchema, arrange, resetView);
}
use of au.gov.asd.tac.constellation.graph.processing.RecordStore in project constellation by constellation-app.
the class CompositeUtilities method makeComposite.
/**
* Make a composite node by specifying a lead node id and a set of the
* comprising node id's
*
* @param graph The graph
* @param comprisingIds A Set of the comprising node id's to be composited
* @param leaderId The lead node id to contain the composited nodes
*/
public static void makeComposite(final GraphWriteMethods graph, final Set<Integer> comprisingIds, final int leaderId) {
final int compositeStateAttr = AnalyticConcept.VertexAttribute.COMPOSITE_STATE.ensure(graph);
final int uniqueIdAttr = VisualConcept.TransactionAttribute.IDENTIFIER.get(graph);
// We first destroy any composites or composite constituents about to be composited.
// 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<>();
comprisingIds.forEach(vxId -> {
final List<Integer> resultingVerts = CompositeUtilities.destroyComposite(graph, compositeStateAttr, uniqueIdAttr, vxId);
if (!resultingVerts.isEmpty()) {
removedVerts.add(vxId);
addedVerts.addAll(resultingVerts);
}
});
// NOTE:: Remove before adding, because of id reuse!
comprisingIds.removeAll(removedVerts);
comprisingIds.addAll(addedVerts);
// Make a record store representing the new composite that is about to be added
final String[] compositeId = new String[1];
final RecordStore newCompositeStore = new GraphRecordStore();
GraphRecordStoreUtilities.copySpecifiedVertex(graph, newCompositeStore, leaderId, compositeId);
// Construct and set an expanded composite node state for each of the nodes that will constitute the composite.
comprisingIds.forEach(vxId -> {
final ExpandedCompositeNodeState expandedState = new ExpandedCompositeNodeState(newCompositeStore, compositeId[0], true, comprisingIds.size());
graph.setObjectValue(compositeStateAttr, vxId, new CompositeNodeState(vxId, expandedState));
});
// Create the composite by calling contract on the first node's expanded composite state.
((CompositeNodeState) graph.getObjectValue(compositeStateAttr, comprisingIds.iterator().next())).expandedState.contract(graph);
}
use of au.gov.asd.tac.constellation.graph.processing.RecordStore in project constellation by constellation-app.
the class ContractedCompositeNodeState method expand.
/**
* Expand the composite represented by this state, using the supplied graph
* write lock.
*
* @param wg The graph write lock with which to perform the expansion.
* @param vxId The Graph ID of the node that is being expanded. The caller
* must ensure that this is the state object corresponding to this node!
* @return A list of graph IDs of the constituent nodes of the now expanded
* composite.
*/
public List<Integer> expand(final GraphWriteMethods wg, final int vxId) {
// Create rows in the expansion record store for transactions that are currently connected to the composite node and should be connected to expnaded nodes.
final int uniqueIdAttr = VisualConcept.TransactionAttribute.IDENTIFIER.get(wg);
final int compositeStateAttr = AnalyticConcept.VertexAttribute.COMPOSITE_STATE.get(wg);
final int selectedAttr = VisualConcept.VertexAttribute.SELECTED.get(wg);
final int xAttr = VisualConcept.VertexAttribute.X.get(wg);
final int yAttr = VisualConcept.VertexAttribute.Y.get(wg);
final int zAttr = VisualConcept.VertexAttribute.Z.get(wg);
// Clear the composite state we are currently expanding!
wg.setObjectValue(compositeStateAttr, vxId, null);
// Create the cotnraction store and get the stored id of the contracted composite ndoe
final String[] contractedId = new String[1];
final RecordStore contractionStore = GraphRecordStoreUtilities.copySpecifiedVertex(wg, null, vxId, contractedId);
final RecordStore addToGraphStore = new GraphRecordStore();
addToGraphStore.add(constituentNodeStore);
// Copy the relevant transactions from the composite to the constituent node record store
GraphRecordStoreUtilities.copyTransactionsFromComposite(wg, addToGraphStore, vxId, contractedId[0], affectedExpandedIds, new GraphAttribute(wg, uniqueIdAttr));
// Add the now complete expanded composite node store to the graph (do not initialise or complete with schema)
final Map<String, Integer> vertexMap = new HashMap<>();
final List<Integer> expandedVerts = GraphRecordStoreUtilities.addRecordStoreToGraph(wg, addToGraphStore, false, false, null, vertexMap, null);
// Create the expanded composite state for each expanded node and add it to those nodes.
vertexMap.entrySet().forEach(entry -> {
final int id = entry.getValue();
final ExpandedCompositeNodeState expansionState = new ExpandedCompositeNodeState(contractionStore, contractedId[0], affectedExpandedIds.contains(entry.getKey()), expandedVerts.size());
wg.setObjectValue(compositeStateAttr, id, new CompositeNodeState(id, expansionState));
});
// Correct the x,y,z coordinates of the nodes in the zonsitutentNodeStore in case the composite node has been moved.
final float x = wg.getFloatValue(xAttr, vxId);
final float y = wg.getFloatValue(yAttr, vxId);
final float z = wg.getFloatValue(zAttr, vxId);
final boolean selected = wg.getBooleanValue(selectedAttr, vxId);
vertexMap.values().forEach(id -> {
final float currentX = wg.getFloatValue(xAttr, id);
final float currentY = wg.getFloatValue(yAttr, id);
final float currentZ = wg.getFloatValue(zAttr, id);
wg.setFloatValue(xAttr, id, currentX - mean[0] + x);
wg.setFloatValue(yAttr, id, currentY - mean[1] + y);
wg.setFloatValue(zAttr, id, currentZ - mean[2] + z);
wg.setBooleanValue(selectedAttr, id, selected);
});
// Delete the vertex represented by this composite node state
wg.removeVertex(vxId);
return new ArrayList<>(vertexMap.values());
}
use of au.gov.asd.tac.constellation.graph.processing.RecordStore in project constellation by constellation-app.
the class ExpandedCompositeNodeState method contract.
/**
* Contract the composite represented by this state, using the supplied
* graph write lock.
*
* @param wg The graph write lock with which to perform the expansion.
* @return The graph ID of the now contracted composite node.
*/
public int contract(final GraphWriteMethods wg) {
final int uniqueIdAttr = VisualConcept.TransactionAttribute.IDENTIFIER.get(wg);
final int compositeStateAttr = AnalyticConcept.VertexAttribute.COMPOSITE_STATE.get(wg);
final int selectedAttr = VisualConcept.VertexAttribute.SELECTED.get(wg);
final int xAttr = VisualConcept.VertexAttribute.X.get(wg);
final int yAttr = VisualConcept.VertexAttribute.Y.get(wg);
final int zAttr = VisualConcept.VertexAttribute.Z.get(wg);
final RecordStore constituentNodeStore = new GraphRecordStore();
final List<String> expandedIds = new ArrayList<>();
final List<String> affectedExpandedIds = new ArrayList<>();
// Iterate through each vertex on the graph, and if it has an expanded composite state with the same composite id
// as this state, perform the single vertex contraction. Keep track of x, y and z as we go so that we can set
// the coordinates of the contracted composite to be at the centre of the expanded constituents.
float x = 0;
float y = 0;
float z = 0;
final Map<Integer, String> idToCopiedId = new HashMap<>();
final int vertexCount = wg.getVertexCount();
int numConstituents = 0;
boolean constitutentsSelected = false;
for (int i = 0; i < vertexCount; i++) {
final int vxId = wg.getVertex(i);
final CompositeNodeState cns = ((CompositeNodeState) wg.getObjectValue(compositeStateAttr, vxId));
if (cns != null && cns.expandedState != null && cns.expandedState.getCompositeId().equals(compositeId)) {
x += wg.getFloatValue(xAttr, vxId);
y += wg.getFloatValue(yAttr, vxId);
z += wg.getFloatValue(zAttr, vxId);
constitutentsSelected |= wg.getBooleanValue(selectedAttr, vxId);
// Clear the composite state we are about to expand
wg.setObjectValue(compositeStateAttr, vxId, null);
// Expand the composite state
cns.expandedState.contractSingleVertex(wg, vxId, constituentNodeStore, expandedIds, affectedExpandedIds, idToCopiedId);
numConstituents++;
}
}
if (numConstituents != 0) {
x /= numConstituents;
y /= numConstituents;
z /= numConstituents;
}
// The contracted state of this composite node, to be set on the composite node after it has been created.
final float[] mean = new float[] { x, y, z };
final ContractedCompositeNodeState contractionState = new ContractedCompositeNodeState(constituentNodeStore, expandedIds, affectedExpandedIds, mean);
final RecordStore addToGraphStore = new GraphRecordStore();
addToGraphStore.add(compositeNodeStore);
// Add the transactions from the expanded nodes to non-constituent nodes to the contraction record store
idToCopiedId.entrySet().forEach(entry -> GraphRecordStoreUtilities.copyTransactionsToComposite(wg, addToGraphStore, entry.getKey(), entry.getValue(), idToCopiedId.keySet(), compositeId, new GraphAttribute(wg, uniqueIdAttr)));
// Remove each expanded vertex from the graph
idToCopiedId.keySet().forEach(wg::removeVertex);
// Add all the transactions between the expanded nodes to the expansion record store
// Add the contraction record store to the graph, which creates the composite node and all its relevant transactions
int contractedVert = GraphRecordStoreUtilities.addRecordStoreToGraph(wg, addToGraphStore, false, false, null).get(0);
// Set the x,y,z and composite node state for the newly added composite node.
wg.setFloatValue(xAttr, contractedVert, x);
wg.setFloatValue(yAttr, contractedVert, y);
wg.setFloatValue(zAttr, contractedVert, z);
wg.setBooleanValue(selectedAttr, contractedVert, constitutentsSelected);
wg.setObjectValue(compositeStateAttr, contractedVert, new CompositeNodeState(contractedVert, contractionState));
return contractedVert;
}
Aggregations