use of au.gov.asd.tac.constellation.graph.GraphWriteMethods in project constellation by constellation-app.
the class TableViewStateIoProviderNGTest method writeObjectStateIsNull.
@Test
public void writeObjectStateIsNull() 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(false);
when(graph.getObjectValue(ATTRIBUTE_ID, ELEMENT_ID)).thenReturn(null);
final JsonFactory factory = new JsonFactory();
final ByteArrayOutputStream output = new ByteArrayOutputStream();
JsonGenerator jsonGenerator = factory.createGenerator(output);
// The code is written with the assumption that it is called within a document
// that has already started being written. Without starting the object in the test
// the code would throw invalid json exceptions.
jsonGenerator.writeStartObject();
tableViewStateIoProvider.writeObject(attribute, ELEMENT_ID, jsonGenerator, graph, null, false);
jsonGenerator.writeEndObject();
jsonGenerator.flush();
final ObjectMapper objectMapper = new ObjectMapper();
final JsonNode expected = objectMapper.readTree("{\"ATTR NAME\": null}");
final JsonNode actual = objectMapper.readTree(new String(output.toByteArray(), StandardCharsets.UTF_8));
assertEquals(actual, expected);
}
use of au.gov.asd.tac.constellation.graph.GraphWriteMethods in project constellation by constellation-app.
the class TableViewStateIoProviderNGTest method readObject.
@Test
public void readObject() throws IOException {
final ObjectMapper objectMapper = new ObjectMapper();
final JsonNode jsonNode = objectMapper.readTree(new FileInputStream(getClass().getResource("resources/tableViewStateRead.json").getPath()));
final GraphWriteMethods graph = mock(GraphWriteMethods.class);
when(graph.getAttribute(GraphElementType.TRANSACTION, "My Transaction")).thenReturn(1);
when(graph.getAttribute(GraphElementType.VERTEX, "My Vertex")).thenReturn(2);
tableViewStateIoProvider.readObject(ATTRIBUTE_ID, ELEMENT_ID, jsonNode, graph, null, null, null, null);
// Capture the call on the graph setter, pulling out the state
final ArgumentCaptor<TableViewState> captor = ArgumentCaptor.forClass(TableViewState.class);
verify(graph, times(1)).setObjectValue(eq(ATTRIBUTE_ID), eq(ELEMENT_ID), captor.capture());
final TableViewState actual = captor.getValue();
final TableViewState expected = new TableViewState();
assertTrue(actual.isSelectedOnly());
assertEquals(actual.getElementType(), GraphElementType.VERTEX);
assertEquals(actual.getTransactionColumnAttributes().size(), 1);
final Tuple<String, Attribute> transactionColumnAttr = actual.getTransactionColumnAttributes().get(0);
assertEquals(transactionColumnAttr.getFirst(), "transactionPrefix");
assertEquals(transactionColumnAttr.getSecond(), new GraphAttribute(graph, 1));
assertEquals(actual.getVertexColumnAttributes().size(), 1);
final Tuple<String, Attribute> vertexColumnAttr = actual.getVertexColumnAttributes().get(0);
assertEquals(vertexColumnAttr.getFirst(), "vertexPrefix");
assertEquals(vertexColumnAttr.getSecond(), new GraphAttribute(graph, 2));
}
use of au.gov.asd.tac.constellation.graph.GraphWriteMethods in project constellation by constellation-app.
the class GraphTaxonomy method getCondensedGraph.
/**
* Create a graph that represents the relationship between the taxa.
* <p>
* Vertices in the new graph are the taxa keys. A transaction in the new
* graph from vertex A to vertex B represents all transactions from any
* vertex in taxon A to any vertex in taxon B.
* <p>
* @throws java.lang.InterruptedException If the thread is interrupted.
*
* @return A Condensation representing the relationship between the taxa.
*
* TODO: sometimes its not worth adding the transactions.
*/
public Condensation getCondensedGraph() throws InterruptedException {
final Map<Integer, Integer> cVxIdToTaxonKey = new HashMap<>();
final Map<Integer, Integer> taxonKeyToVxId = new HashMap<>();
final GraphWriteMethods condensedGraph = new StoreGraph(wg.getSchema());
final int cxAttr = VisualConcept.VertexAttribute.X.ensure(condensedGraph);
final int cyAttr = VisualConcept.VertexAttribute.Y.ensure(condensedGraph);
final int czAttr = VisualConcept.VertexAttribute.Z.ensure(condensedGraph);
final int cxOrigId = condensedGraph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, X_ORIG, X_ORIG, null, null);
final int cyOrigId = condensedGraph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, Y_ORIG, Y_ORIG, null, null);
final int czOrigId = condensedGraph.addAttribute(GraphElementType.VERTEX, FloatAttributeDescription.ATTRIBUTE_NAME, Z_ORIG, Z_ORIG, null, null);
final int cnRadiusAttr = VisualConcept.VertexAttribute.NODE_RADIUS.ensure(condensedGraph);
final int cRadiusAttr = VisualConcept.VertexAttribute.LABEL_RADIUS.ensure(condensedGraph);
// TODO: do these need to be sorted?
for (final Integer k : getSortedTaxaKeys()) {
if (Thread.interrupted()) {
throw new InterruptedException();
}
if (taxa.get(k).isEmpty()) {
continue;
}
final int cVxId = condensedGraph.addVertex();
final BitSet vertices = new BitSet();
for (final Integer member : taxa.get(k)) {
vertices.set(member);
}
// Figure out where and how big the new vertex will be.
final Extent extent = Extent.getExtent(wg, vertices);
// The condensation has the positions of the extents and the positions of the taxon key vertices.
// The x,y,z will be modified, so remember them for repositioning later.
condensedGraph.setFloatValue(cxAttr, cVxId, extent.getX());
condensedGraph.setFloatValue(cyAttr, cVxId, extent.getY());
condensedGraph.setFloatValue(czAttr, cVxId, extent.getZ());
condensedGraph.setFloatValue(cxOrigId, cVxId, extent.getX());
condensedGraph.setFloatValue(cyOrigId, cVxId, extent.getY());
condensedGraph.setFloatValue(czOrigId, cVxId, extent.getZ());
condensedGraph.setFloatValue(cnRadiusAttr, cVxId, extent.getNRadius());
condensedGraph.setFloatValue(cRadiusAttr, cVxId, extent.getLRadius());
cVxIdToTaxonKey.put(cVxId, k);
taxonKeyToVxId.put(k, cVxId);
}
// Add the transactions.
// final long t0 = System.currentTimeMillis();
// We search through all of the taxa to find sources,
// but only look in the remaining taxa for destinations,
// otherwise we end up with two transactions between each vertex.
final ArrayDeque<Integer> sources = new ArrayDeque<>();
sources.addAll(taxa.keySet());
while (!sources.isEmpty()) {
// If we already have a transaction from the current source to a particular destination,
// don't add another one.
final Set<Integer> found = new HashSet<>();
final Integer src = sources.removeFirst();
found.add(src);
final Set<Integer> members = taxa.get(src);
for (final Integer mm : members) {
if (Thread.interrupted()) {
throw new InterruptedException();
}
final int m = mm;
final int nNeighbours = wg.getVertexNeighbourCount(m);
for (int position = 0; position < nNeighbours; position++) {
final int nbId = wg.getVertexNeighbour(m, position);
final Integer dst = nodeToTaxa != null ? nodeToTaxa.get(nbId) : findTaxonContainingVertex(sources, nbId);
// Found the test for null was required to avoid issues
if (dst != null && dst != Graph.NOT_FOUND && !found.contains(dst)) {
condensedGraph.addTransaction(taxonKeyToVxId.get(src), taxonKeyToVxId.get(dst), true);
found.add(dst);
}
}
}
}
return new Condensation(condensedGraph, cVxIdToTaxonKey);
}
use of au.gov.asd.tac.constellation.graph.GraphWriteMethods in project constellation by constellation-app.
the class ExtractWordsFromTextPlugin method edit.
@Override
public void edit(final GraphWriteMethods wg, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
interaction.setProgress(0, 0, "Extracting...", true);
/*
Retrieving attributes
*/
final Map<String, PluginParameter<?>> extractEntityParameters = parameters.getParameters();
final String contentAttribute = extractEntityParameters.get(ATTRIBUTE_PARAMETER_ID).getStringValue();
final String words = extractEntityParameters.get(WORDS_PARAMETER_ID).getStringValue() == null ? null : extractEntityParameters.get(WORDS_PARAMETER_ID).getStringValue().trim();
final boolean useRegex = extractEntityParameters.get(USE_REGEX_PARAMETER_ID).getBooleanValue();
final boolean wholeWordOnly = extractEntityParameters.get(WHOLE_WORDS_ONLY_PARAMETER_ID).getBooleanValue();
final int wordLength = parameters.getParameters().get(MIN_WORD_LENGTH_PARAMETER_ID).getIntegerValue();
final boolean removeSpecialChars = extractEntityParameters.get(REMOVE_SPECIAL_CHARS_PARAMETER_ID).getBooleanValue();
final boolean toLowerCase = extractEntityParameters.get(LOWER_CASE_PARAMETER_ID).getBooleanValue();
final boolean types = extractEntityParameters.get(SCHEMA_TYPES_PARAMETER_ID).getBooleanValue();
final String inOrOut = extractEntityParameters.get(IN_OR_OUT_PARAMETER_ID).getStringValue();
final boolean selectedOnly = extractEntityParameters.get(SELECTED_ONLY_PARAMETER_ID).getBooleanValue();
final boolean regexOnly = extractEntityParameters.get(REGEX_ONLY_PARAMETER_ID).getBooleanValue();
if (!OUTGOING.equals(inOrOut) && !INCOMING.equals(inOrOut)) {
var msg = String.format("Parameter %s must be '%s' or '%s'", REGEX_ONLY_PARAMETER_ID, OUTGOING, INCOMING);
throw new PluginException(PluginNotificationLevel.ERROR, msg);
}
final boolean outgoing = OUTGOING.equals(inOrOut);
/*
Retrieving attribute IDs
*/
final int vertexIdentifierAttributeId = VisualConcept.VertexAttribute.IDENTIFIER.ensure(wg);
final int vertexTypeAttributeId = AnalyticConcept.VertexAttribute.TYPE.ensure(wg);
final int transactionTypeAttributeId = AnalyticConcept.TransactionAttribute.TYPE.ensure(wg);
final int transactionDatetimeAttributeId = TemporalConcept.TransactionAttribute.DATETIME.ensure(wg);
final int transactionContentAttributeId = wg.getAttribute(GraphElementType.TRANSACTION, contentAttribute);
final int transactionSelectedAttributeId = VisualConcept.TransactionAttribute.SELECTED.ensure(wg);
//
if (transactionContentAttributeId == Graph.NOT_FOUND) {
final NotifyDescriptor nd = new NotifyDescriptor.Message(String.format("The specified attribute %s does not exist.", contentAttribute), NotifyDescriptor.WARNING_MESSAGE);
DialogDisplayer.getDefault().notify(nd);
return;
}
final int transactionCount = wg.getTransactionCount();
if (regexOnly) {
// This choice ignores several other parameters, so is a bit simpler
// even if there code commonalities, but combining the if/else
// code would make things even more complex.
//
// The input words are treated as trusted regular expressions,
// so the caller has to know what they're doing.
// This is power-use mode.
//
// Each line of the input words is a regex.
// Use them as-is for the power users.
//
final List<Pattern> patterns = new ArrayList<>();
if (StringUtils.isNotBlank(words)) {
for (String word : words.split(SeparatorConstants.NEWLINE)) {
word = word.strip();
if (!word.isEmpty()) {
final Pattern pattern = Pattern.compile(word);
patterns.add(pattern);
}
}
}
if (!patterns.isEmpty()) {
// Use a set to hold the words.
// If a word is found multiple times, there's no point adding multiple nodes.
//
final Set<String> matched = new HashSet<>();
//
for (int transactionPosition = 0; transactionPosition < transactionCount; transactionPosition++) {
final int transactionId = wg.getTransaction(transactionPosition);
final boolean selectedTx = wg.getBooleanValue(transactionSelectedAttributeId, transactionId);
if (selectedOnly && !selectedTx) {
continue;
}
final String content = wg.getStringValue(transactionContentAttributeId, transactionId);
/*
Does the transaction have content?
*/
if (StringUtils.isBlank(content)) {
continue;
}
/*
Ignore other "referenced" transactions because that's not useful
*/
if (wg.getObjectValue(transactionTypeAttributeId, transactionId) != null && wg.getObjectValue(transactionTypeAttributeId, transactionId).equals(AnalyticConcept.TransactionType.REFERENCED)) {
continue;
}
patterns.stream().map(pattern -> pattern.matcher(content)).forEach(matcher -> {
while (matcher.find()) {
if (matcher.groupCount() == 0) {
// The regex doesn't have an explicit capture group, so capture the lot.
//
final String g = matcher.group();
matched.add(toLowerCase ? g.toLowerCase() : g);
} else {
//
for (int i = 1; i <= matcher.groupCount(); i++) {
final String g = matcher.group(i);
matched.add(toLowerCase ? g.toLowerCase() : g);
}
}
}
});
//
if (!matched.isEmpty()) {
/*
Retrieving information needed to create new transactions
*/
final int sourceVertexId = wg.getTransactionSourceVertex(transactionId);
final int destinationVertexId = wg.getTransactionDestinationVertex(transactionId);
final ZonedDateTime datetime = wg.getObjectValue(transactionDatetimeAttributeId, transactionId);
matched.forEach(word -> {
final int newVertexId = wg.addVertex();
wg.setStringValue(vertexIdentifierAttributeId, newVertexId, word);
wg.setObjectValue(vertexTypeAttributeId, newVertexId, AnalyticConcept.VertexType.WORD);
final int newTransactionId = outgoing ? wg.addTransaction(sourceVertexId, newVertexId, true) : wg.addTransaction(newVertexId, destinationVertexId, true);
wg.setObjectValue(transactionDatetimeAttributeId, newTransactionId, datetime);
wg.setObjectValue(transactionTypeAttributeId, newTransactionId, AnalyticConcept.TransactionType.REFERENCED);
wg.setStringValue(transactionContentAttributeId, newTransactionId, content);
});
}
}
}
// End of regexOnly.
} else {
// The original logic.
final List<Pattern> patterns = patternsFromWords(words, useRegex, wholeWordOnly);
/*
Iterating over all the transactions in the graph
*/
final List<String> foundWords = new ArrayList<>();
for (int transactionPosition = 0; transactionPosition < transactionCount; transactionPosition++) {
foundWords.clear();
final int transactionId = wg.getTransaction(transactionPosition);
final boolean selectedTx = wg.getBooleanValue(transactionSelectedAttributeId, transactionId);
if (selectedOnly && !selectedTx) {
continue;
}
String content = wg.getStringValue(transactionContentAttributeId, transactionId);
/*
Does the transaction have content?
*/
if (StringUtils.isBlank(content)) {
continue;
}
/*
Ignore other "referenced" transactions because that's not useful
*/
if (wg.getObjectValue(transactionTypeAttributeId, transactionId) != null && wg.getObjectValue(transactionTypeAttributeId, transactionId).equals(AnalyticConcept.TransactionType.REFERENCED)) {
continue;
}
/*
Retrieving information needed to create new transactions
*/
final int sourceVertexId = wg.getTransactionSourceVertex(transactionId);
final int destinationVertexId = wg.getTransactionDestinationVertex(transactionId);
final ZonedDateTime datetime = wg.getObjectValue(transactionDatetimeAttributeId, transactionId);
final HashSet<String> typesExtracted = new HashSet<>();
/*
Extracting Schema Types
*/
if (types) {
final List<ExtractedVertexType> extractedTypes = SchemaVertexTypeUtilities.extractVertexTypes(content);
final Map<String, SchemaVertexType> identifiers = new HashMap<>();
extractedTypes.forEach(extractedType -> identifiers.put(extractedType.getIdentifier(), extractedType.getType()));
for (String identifier : identifiers.keySet()) {
final int newVertexId = wg.addVertex();
wg.setStringValue(vertexIdentifierAttributeId, newVertexId, identifier);
wg.setObjectValue(vertexTypeAttributeId, newVertexId, identifiers.get(identifier));
final int newTransactionId = outgoing ? wg.addTransaction(sourceVertexId, newVertexId, true) : wg.addTransaction(newVertexId, destinationVertexId, true);
wg.setObjectValue(transactionDatetimeAttributeId, newTransactionId, datetime);
wg.setObjectValue(transactionTypeAttributeId, newTransactionId, AnalyticConcept.TransactionType.REFERENCED);
wg.setStringValue(transactionContentAttributeId, newTransactionId, content);
typesExtracted.add(identifier.toLowerCase());
}
}
if (StringUtils.isBlank(words)) {
/*
Extracting all words of the specified length if no word list has been provided
*/
for (String word : content.split(" ")) {
if (toLowerCase) {
word = word.toLowerCase();
}
if (removeSpecialChars) {
word = word.replaceAll("\\W", "");
}
if (word.length() < wordLength) {
continue;
}
foundWords.add(word);
}
} else {
patterns.stream().map(pattern -> pattern.matcher(content)).forEach(matcher -> {
while (matcher.find()) {
final String g = matcher.group();
foundWords.add(toLowerCase ? g.toLowerCase() : g);
}
});
}
/*
Add words to graph
*/
for (String word : foundWords) {
if (types && typesExtracted.contains(word.toLowerCase())) {
continue;
}
final int newVertexId = wg.addVertex();
wg.setStringValue(vertexIdentifierAttributeId, newVertexId, word);
wg.setObjectValue(vertexTypeAttributeId, newVertexId, AnalyticConcept.VertexType.WORD);
final int newTransactionId = outgoing ? wg.addTransaction(sourceVertexId, newVertexId, true) : wg.addTransaction(newVertexId, destinationVertexId, true);
wg.setObjectValue(transactionDatetimeAttributeId, newTransactionId, datetime);
wg.setObjectValue(transactionTypeAttributeId, newTransactionId, AnalyticConcept.TransactionType.REFERENCED);
wg.setStringValue(transactionContentAttributeId, newTransactionId, content);
}
}
}
PluginExecutor.startWith(VisualSchemaPluginRegistry.COMPLETE_SCHEMA).followedBy(InteractiveGraphPluginRegistry.RESET_VIEW).executeNow(wg);
interaction.setProgress(1, 0, "Completed successfully", true);
}
use of au.gov.asd.tac.constellation.graph.GraphWriteMethods in project constellation by constellation-app.
the class SplitNodesPlugin method edit.
@Override
public void edit(final GraphWriteMethods graph, final PluginInteraction interaction, final PluginParameters parameters) throws InterruptedException, PluginException {
final Map<String, PluginParameter<?>> splitParameters = parameters.getParameters();
final String character = splitParameters.get(SPLIT_PARAMETER_ID) != null && splitParameters.get(SPLIT_PARAMETER_ID).getStringValue() != null ? splitParameters.get(SPLIT_PARAMETER_ID).getStringValue() : "";
final ParameterValue transactionTypeChoice = splitParameters.get(TRANSACTION_TYPE_PARAMETER_ID).getSingleChoice();
final String linkType = transactionTypeChoice != null ? transactionTypeChoice.toString() : AnalyticConcept.TransactionType.CORRELATION.getName();
final boolean allOccurrences = splitParameters.get(ALL_OCCURRENCES_PARAMETER_ID).getBooleanValue();
final boolean splitIntoSameLevel = splitParameters.get(DUPLICATE_TRANSACTIONS_PARAMETER_ID).getBooleanValue();
final int vertexSelectedAttributeId = VisualConcept.VertexAttribute.SELECTED.ensure(graph);
final int vertexIdentifierAttributeId = VisualConcept.VertexAttribute.IDENTIFIER.ensure(graph);
final List<Integer> newVertices = new ArrayList<>();
final int graphVertexCount = graph.getVertexCount();
for (int position = 0; position < graphVertexCount; position++) {
final int currentVertexId = graph.getVertex(position);
if (graph.getBooleanValue(vertexSelectedAttributeId, currentVertexId)) {
final String identifier = graph.getStringValue(vertexIdentifierAttributeId, currentVertexId);
if (identifier != null && identifier.contains(character) && identifier.indexOf(character) < identifier.length() - character.length()) {
String leftNodeIdentifier = "";
if (allOccurrences) {
final String[] substrings = Arrays.stream(identifier.split(character)).filter(value -> value != null && value.length() > 0).toArray(size -> new String[size]);
if (substrings.length <= 0) {
continue;
}
leftNodeIdentifier = substrings[0];
for (int i = 1; i < substrings.length; i++) {
newVertices.add(createNewNode(graph, position, substrings[i], linkType, splitIntoSameLevel));
}
} else {
final int i = identifier.indexOf(character);
leftNodeIdentifier = identifier.substring(0, i);
if (StringUtils.isNotBlank(leftNodeIdentifier)) {
newVertices.add(createNewNode(graph, position, identifier.substring(i + 1), linkType, splitIntoSameLevel));
} else {
leftNodeIdentifier = identifier.substring(i + 1);
}
}
// Rename the selected node
if (StringUtils.isNotBlank(leftNodeIdentifier)) {
graph.setStringValue(vertexIdentifierAttributeId, currentVertexId, leftNodeIdentifier);
newVertices.add(currentVertexId);
}
}
}
}
if (!newVertices.isEmpty()) {
// Reset the view
graph.validateKey(GraphElementType.VERTEX, true);
graph.validateKey(GraphElementType.TRANSACTION, true);
final PluginExecutor arrangement = completionArrangement();
if (arrangement != null) {
// run the arrangement
final VertexListInclusionGraph vlGraph = new VertexListInclusionGraph(graph, AbstractInclusionGraph.Connections.NONE, newVertices);
arrangement.executeNow(vlGraph.getInclusionGraph());
vlGraph.retrieveCoords();
}
if (splitParameters.get(COMPLETE_WITH_SCHEMA_OPTION_ID).getBooleanValue()) {
PluginExecution.withPlugin(VisualSchemaPluginRegistry.COMPLETE_SCHEMA).executeNow(graph);
}
PluginExecutor.startWith(InteractiveGraphPluginRegistry.RESET_VIEW).executeNow(graph);
}
}
Aggregations