use of au.gov.asd.tac.constellation.graph.schema.type.SchemaVertexType in project constellation by constellation-app.
the class VertexTypeIOProviderNGTest method testWriteTypeObject.
@Test
public void testWriteTypeObject() throws Exception {
final SchemaVertexType type = new SchemaVertexType.Builder("type").setDescription("description").setColor(ConstellationColor.GREEN).setForegroundIcon(CharacterIconProvider.CHAR_0020).setBackgroundIcon(DefaultIconProvider.FLAT_CIRCLE).setDetectionRegex(Pattern.compile("\\+?([0-9]{8,13})", Pattern.CASE_INSENSITIVE)).setValidationRegex(Pattern.compile("\\+?([0-9]{8,15})", Pattern.CASE_INSENSITIVE)).setProperty("my key", "my value").setIncomplete(true).build();
final ByteArrayOutputStream actual = new ByteArrayOutputStream();
JsonGenerator jsonGenerator = new JsonFactory().createGenerator(actual, JsonEncoding.UTF8);
jsonGenerator.writeStartObject();
VertexTypeIOProvider.writeTypeObject(type, jsonGenerator);
jsonGenerator.writeEndObject();
jsonGenerator.close();
final ByteArrayOutputStream expected = new ByteArrayOutputStream();
jsonGenerator = new JsonFactory().createGenerator(expected, JsonEncoding.UTF8);
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("Name", "type");
jsonGenerator.writeStringField("Description", "description");
jsonGenerator.writeObjectFieldStart("Color");
jsonGenerator.writeStringField("name", "Green");
jsonGenerator.writeEndObject();
jsonGenerator.writeStringField("Foreground Icon", "Character.Space");
jsonGenerator.writeStringField("Background Icon", "Background.Flat Circle");
jsonGenerator.writeStringField("Detection Regex", "\\+?([0-9]{8,13})");
jsonGenerator.writeStringField("Validation Regex", "\\+?([0-9]{8,15})");
jsonGenerator.writeObjectFieldStart("Properties");
jsonGenerator.writeStringField("my key", "my value");
jsonGenerator.writeEndObject();
jsonGenerator.writeBooleanField("Incomplete", true);
jsonGenerator.writeEndObject();
jsonGenerator.close();
Assert.assertEquals(actual.toString(), expected.toString());
}
use of au.gov.asd.tac.constellation.graph.schema.type.SchemaVertexType in project constellation by constellation-app.
the class VertexTypeIOProviderNGTest method testWriteTypeObjectWithParent.
@Test
public void testWriteTypeObjectWithParent() throws Exception {
final SchemaVertexType parent = new SchemaVertexType.Builder("parent").setDescription("parent description").setColor(ConstellationColor.GREEN).setForegroundIcon(CharacterIconProvider.CHAR_0020).setBackgroundIcon(DefaultIconProvider.FLAT_CIRCLE).setDetectionRegex(Pattern.compile("\\+?([0-9]{8,13})", Pattern.CASE_INSENSITIVE)).setValidationRegex(Pattern.compile("\\+?([0-9]{8,15})", Pattern.CASE_INSENSITIVE)).setProperty("my key", "my value").setIncomplete(true).build();
final SchemaVertexType child = new SchemaVertexType.Builder("child").setDescription("child description").setColor(ConstellationColor.GREEN).setForegroundIcon(CharacterIconProvider.CHAR_0020).setBackgroundIcon(DefaultIconProvider.FLAT_CIRCLE).setDetectionRegex(Pattern.compile("\\+?([0-9]{8,13})", Pattern.CASE_INSENSITIVE)).setValidationRegex(Pattern.compile("\\+?([0-9]{8,15})", Pattern.CASE_INSENSITIVE)).setSuperType(parent).setProperty("my key", "my value").setIncomplete(true).build();
final ByteArrayOutputStream actual = new ByteArrayOutputStream();
JsonGenerator jsonGenerator = new JsonFactory().createGenerator(actual, JsonEncoding.UTF8);
// jsonGenerator.useDefaultPrettyPrinter();
jsonGenerator.writeStartObject();
VertexTypeIOProvider.writeTypeObject(child, jsonGenerator);
jsonGenerator.writeEndObject();
jsonGenerator.close();
final ByteArrayOutputStream expected = new ByteArrayOutputStream();
jsonGenerator = new JsonFactory().createGenerator(expected, JsonEncoding.UTF8);
// jsonGenerator.useDefaultPrettyPrinter();
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("Name", "child");
jsonGenerator.writeStringField("Description", "child description");
jsonGenerator.writeObjectFieldStart("Color");
jsonGenerator.writeStringField("name", "Green");
jsonGenerator.writeEndObject();
jsonGenerator.writeStringField("Foreground Icon", "Character.Space");
jsonGenerator.writeStringField("Background Icon", "Background.Flat Circle");
jsonGenerator.writeStringField("Detection Regex", "\\+?([0-9]{8,13})");
jsonGenerator.writeStringField("Validation Regex", "\\+?([0-9]{8,15})");
// supertype start
jsonGenerator.writeObjectFieldStart("Super Type");
jsonGenerator.writeStringField("Name", "parent");
jsonGenerator.writeStringField("Description", "parent description");
jsonGenerator.writeObjectFieldStart("Color");
jsonGenerator.writeStringField("name", "Green");
jsonGenerator.writeEndObject();
jsonGenerator.writeStringField("Foreground Icon", "Character.Space");
jsonGenerator.writeStringField("Background Icon", "Background.Flat Circle");
jsonGenerator.writeStringField("Detection Regex", "\\+?([0-9]{8,13})");
jsonGenerator.writeStringField("Validation Regex", "\\+?([0-9]{8,15})");
jsonGenerator.writeObjectFieldStart("Properties");
jsonGenerator.writeStringField("my key", "my value");
jsonGenerator.writeEndObject();
jsonGenerator.writeBooleanField("Incomplete", true);
jsonGenerator.writeEndObject();
// supertype end
jsonGenerator.writeObjectFieldStart("Properties");
jsonGenerator.writeStringField("my key", "my value");
jsonGenerator.writeEndObject();
jsonGenerator.writeBooleanField("Incomplete", true);
jsonGenerator.writeEndObject();
jsonGenerator.close();
Assert.assertEquals(actual.toString(), expected.toString());
}
use of au.gov.asd.tac.constellation.graph.schema.type.SchemaVertexType in project constellation by constellation-app.
the class VertexTypeIOProvider method writeObject.
@Override
public void writeObject(final Attribute attribute, final int elementId, final JsonGenerator jsonGenerator, final GraphReadMethods graph, final GraphByteWriter byteWriter, final boolean verbose) throws IOException {
if (verbose || !graph.isDefaultValue(attribute.getId(), elementId)) {
final SchemaVertexType attributeValue = graph.getObjectValue(attribute.getId(), elementId);
if (attributeValue == null) {
jsonGenerator.writeNullField(attribute.getName());
} else {
jsonGenerator.writeObjectFieldStart(attribute.getName());
writeTypeObject(attributeValue, jsonGenerator);
jsonGenerator.writeEndObject();
}
}
}
use of au.gov.asd.tac.constellation.graph.schema.type.SchemaVertexType in project constellation by constellation-app.
the class AnalyticSchemaV4UpdateProvider method schemaUpdate.
@Override
protected void schemaUpdate(final StoreGraph graph) {
final int typeAttribute = AnalyticConcept.VertexAttribute.TYPE.get(graph);
final Map<String, SchemaVertexType> typesToUpgrade = new HashMap<>();
typesToUpgrade.put(AnalyticConcept.VertexType.MD5.getName(), AnalyticConcept.VertexType.MD5);
typesToUpgrade.put(AnalyticConcept.VertexType.SHA1.getName(), AnalyticConcept.VertexType.SHA1);
typesToUpgrade.put(AnalyticConcept.VertexType.SHA256.getName(), AnalyticConcept.VertexType.SHA256);
typesToUpgrade.put(AnalyticConcept.VertexType.COUNTRY.getName(), AnalyticConcept.VertexType.COUNTRY);
typesToUpgrade.put(AnalyticConcept.VertexType.GEOHASH.getName(), AnalyticConcept.VertexType.GEOHASH);
typesToUpgrade.put(AnalyticConcept.VertexType.MGRS.getName(), AnalyticConcept.VertexType.MGRS);
typesToUpgrade.put(AnalyticConcept.VertexType.IPV4.getName(), AnalyticConcept.VertexType.IPV4);
typesToUpgrade.put(AnalyticConcept.VertexType.IPV6.getName(), AnalyticConcept.VertexType.IPV6);
typesToUpgrade.put(AnalyticConcept.VertexType.EMAIL_ADDRESS.getName(), AnalyticConcept.VertexType.EMAIL_ADDRESS);
typesToUpgrade.put(AnalyticConcept.VertexType.HOST_NAME.getName(), AnalyticConcept.VertexType.HOST_NAME);
typesToUpgrade.put(AnalyticConcept.VertexType.URL.getName(), AnalyticConcept.VertexType.URL);
typesToUpgrade.put(AnalyticConcept.VertexType.TELEPHONE_IDENTIFIER.getName(), AnalyticConcept.VertexType.TELEPHONE_IDENTIFIER);
for (int vertex = 0; vertex < graph.getVertexCount(); vertex++) {
final int vertexId = graph.getVertex(vertex);
final SchemaVertexType oldType = graph.getObjectValue(typeAttribute, vertexId);
if (oldType != null && typesToUpgrade.containsKey(oldType.getName())) {
graph.setObjectValue(typeAttribute, vertexId, typesToUpgrade.get(oldType.getName()));
}
}
}
use of au.gov.asd.tac.constellation.graph.schema.type.SchemaVertexType in project constellation by constellation-app.
the class PlaceholderUtilities method collapsePlaceholders.
public static void collapsePlaceholders(final GraphWriteMethods graph, final Comparator<SchemaVertexType> dominanceComparator, final boolean debug) throws PluginException, InterruptedException {
final List<Integer> placeholderIds = new ArrayList<>();
final Map<Integer, List<Integer>> placeholderCorrelations = new HashMap<>();
final Map<Integer, List<Integer>> placeholderActivity = new HashMap<>();
final Map<Integer, Integer> placeholderNeighbours = new HashMap<>();
final int vertexTypeAttributeId = AnalyticConcept.VertexAttribute.TYPE.get(graph);
final int transactionTypeAttributeId = AnalyticConcept.TransactionAttribute.TYPE.get(graph);
// collect placeholders, their transactions and their neighbours
final int vertexCount = graph.getVertexCount();
for (int vertexPosition = 0; vertexPosition < vertexCount; vertexPosition++) {
final int vertexId = graph.getVertex(vertexPosition);
final SchemaVertexType vertexType = graph.getObjectValue(vertexTypeAttributeId, vertexId);
if (vertexType.isSubTypeOf(AnalyticConcept.VertexType.PLACEHOLDER)) {
placeholderIds.add(vertexId);
final List<Integer> placeholderCorrelationList = new ArrayList<>();
final List<Integer> placeholderActivityList = new ArrayList<>();
final int transactionCount = graph.getVertexTransactionCount(vertexId);
for (int transactionPosition = 0; transactionPosition < transactionCount; transactionPosition++) {
final int transactionId = graph.getVertexTransaction(vertexId, transactionPosition);
final SchemaTransactionType transactionType = graph.getObjectValue(transactionTypeAttributeId, transactionId);
if (transactionType.isSubTypeOf(AnalyticConcept.TransactionType.CORRELATION)) {
placeholderCorrelationList.add(transactionId);
} else {
placeholderActivityList.add(transactionId);
}
final int neighbourId = graph.getTransactionSourceVertex(transactionId) == vertexId ? graph.getTransactionDestinationVertex(transactionId) : graph.getTransactionSourceVertex(transactionId);
placeholderNeighbours.put(transactionId, neighbourId);
}
placeholderCorrelations.put(vertexId, placeholderCorrelationList);
placeholderActivity.put(vertexId, placeholderActivityList);
}
}
if (debug) {
GraphOpener.getDefault().openGraph(new DualGraph((StoreGraph) graph.copy(), true), GraphNode.getGraphNode(graph.getId()).getName() + "-debug-stage1");
}
// choose lead vertices to replace placeholders
placeholderIds.forEach(placeholderId -> {
final int leadVertex;
final List<Integer> placeholderCorrelationList = placeholderCorrelations.get(placeholderId);
if (!placeholderCorrelationList.isEmpty()) {
// calculate lead vertex
final SchemaVertexType leadVertexType = placeholderCorrelationList.stream().map(placeholderNeighbours::get).map(neighbourId -> (SchemaVertexType) graph.getObjectValue(vertexTypeAttributeId, neighbourId)).sorted(dominanceComparator).findFirst().get();
leadVertex = placeholderCorrelationList.stream().map(placeholderNeighbours::get).filter(neighbourId -> graph.getObjectValue(vertexTypeAttributeId, neighbourId).equals(leadVertexType)).findFirst().get();
// move correlations from the placeholder to the lead vertex of the entity
placeholderCorrelationList.forEach(correlationId -> {
if (graph.getTransactionSourceVertex(correlationId) == placeholderId && graph.getTransactionDestinationVertex(correlationId) != leadVertex) {
graph.setTransactionSourceVertex(correlationId, leadVertex);
} else if (graph.getTransactionDestinationVertex(correlationId) == placeholderId && graph.getTransactionSourceVertex(correlationId) != leadVertex) {
graph.setTransactionDestinationVertex(correlationId, leadVertex);
} else {
// Do nothing
}
});
// move activity from the placeholder to the lead vertex of the entity
final List<Integer> placeholderActivityList = placeholderActivity.get(placeholderId);
placeholderActivityList.forEach(activityId -> {
if (graph.getTransactionSourceVertex(activityId) == placeholderId) {
graph.setTransactionSourceVertex(activityId, leadVertex);
} else {
graph.setTransactionDestinationVertex(activityId, leadVertex);
}
});
}
});
if (debug) {
GraphOpener.getDefault().openGraph(new DualGraph((StoreGraph) graph.copy(), true), GraphNode.getGraphNode(graph.getId()).getName() + "-debug-stage2");
}
// remove all placeholders
placeholderIds.forEach(graph::removeVertex);
}
Aggregations