use of au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionType in project constellation by constellation-app.
the class AnalyticSchemaV2UpdateProvider method schemaUpdate.
@Override
protected void schemaUpdate(final StoreGraph graph) {
boolean updateVertexKeys = false;
// update vertex raw attribute
final int oldVertexRawAttributeId = AnalyticConcept.VertexAttribute.RAW.get(graph);
final Attribute oldVertexRawAttribute = new GraphAttribute(graph, oldVertexRawAttributeId);
if (!oldVertexRawAttribute.getAttributeType().equals(RawAttributeDescription.ATTRIBUTE_NAME)) {
graph.setPrimaryKey(GraphElementType.VERTEX);
final int newVertexRawAttribute = AnalyticConcept.VertexAttribute.RAW.ensure(graph);
final int vertexIdentifierAttribute = VisualConcept.VertexAttribute.IDENTIFIER.ensure(graph);
for (int vertexPosition = 0; vertexPosition < graph.getVertexCount(); vertexPosition++) {
final int vertexId = graph.getVertex(vertexPosition);
final String rawValue = graph.getStringValue(oldVertexRawAttributeId, vertexId);
graph.setObjectValue(newVertexRawAttribute, vertexId, new RawData(rawValue, null));
if (graph.getStringValue(vertexIdentifierAttribute, vertexId) == null) {
graph.setObjectValue(vertexIdentifierAttribute, vertexId, rawValue);
}
}
graph.removeAttribute(oldVertexRawAttributeId);
updateVertexKeys = true;
}
// update vertex type attribute
if (AnalyticConcept.VertexAttribute.TYPE.get(graph) == Graph.NOT_FOUND) {
updateVertexKeys = true;
}
final int oldVertexTypeAttributeId = graph.getAttribute(GraphElementType.VERTEX, "Type");
final Attribute oldVertexTypeAttribute = new GraphAttribute(graph, oldVertexTypeAttributeId);
if (!oldVertexTypeAttribute.getAttributeType().equals(VertexTypeAttributeDescription.ATTRIBUTE_NAME)) {
graph.setPrimaryKey(GraphElementType.VERTEX);
final int newVertexTypeAttributeId = AnalyticConcept.VertexAttribute.TYPE.ensure(graph);
final int vertexRawAttributeId = AnalyticConcept.VertexAttribute.RAW.ensure(graph);
for (int vertexPosition = 0; vertexPosition < graph.getVertexCount(); vertexPosition++) {
final int vertexId = graph.getVertex(vertexPosition);
final String typeValue = graph.getStringValue(oldVertexTypeAttributeId, vertexId);
final SchemaVertexType type = SchemaVertexTypeUtilities.getType(typeValue);
graph.setObjectValue(newVertexTypeAttributeId, graph.getVertex(vertexPosition), type);
final RawData rawValue = graph.getObjectValue(vertexRawAttributeId, graph.getVertex(vertexPosition));
graph.setObjectValue(vertexRawAttributeId, vertexId, rawValue != null ? new RawData(rawValue.getRawIdentifier(), typeValue) : new RawData(null, null));
}
graph.removeAttribute(oldVertexTypeAttributeId);
updateVertexKeys = true;
}
// update vertex keys and complete vertices
if (updateVertexKeys && graph.getSchema() != null) {
final List<SchemaAttribute> keyAttributes = graph.getSchema().getFactory().getKeyAttributes(GraphElementType.VERTEX);
final int[] keyAttributeIds = keyAttributes.stream().map(keyAttribute -> keyAttribute.ensure(graph)).mapToInt(keyAttributeId -> keyAttributeId).toArray();
graph.setPrimaryKey(GraphElementType.VERTEX, keyAttributeIds);
}
boolean updateTransactionKeys = false;
// update transaction type attribute
if (AnalyticConcept.TransactionAttribute.TYPE.get(graph) == Graph.NOT_FOUND) {
updateTransactionKeys = true;
}
final int oldTransactionTypeAttributeId = graph.getAttribute(GraphElementType.TRANSACTION, "Type");
final Attribute oldTransactionTypeAttribute = new GraphAttribute(graph, oldTransactionTypeAttributeId);
if (!oldTransactionTypeAttribute.getAttributeType().equals(TransactionTypeAttributeDescription.ATTRIBUTE_NAME)) {
graph.setPrimaryKey(GraphElementType.TRANSACTION);
final int newTransactionTypeAttributeId = AnalyticConcept.TransactionAttribute.TYPE.ensure(graph);
for (int transactionPosition = 0; transactionPosition < graph.getTransactionCount(); transactionPosition++) {
final int transactionId = graph.getTransaction(transactionPosition);
final String typeValue = graph.getStringValue(oldTransactionTypeAttributeId, transactionId);
final SchemaTransactionType type = SchemaTransactionTypeUtilities.getType(typeValue);
graph.setObjectValue(newTransactionTypeAttributeId, transactionId, type);
}
graph.removeAttribute(oldTransactionTypeAttributeId);
updateTransactionKeys = true;
}
// update transaction datetime attribute
if (TemporalConcept.TransactionAttribute.DATETIME.get(graph) == Graph.NOT_FOUND) {
updateTransactionKeys = true;
}
// update transaction keys and complete transactions
if (updateTransactionKeys && graph.getSchema() != null) {
final List<SchemaAttribute> keyAttributes = graph.getSchema().getFactory().getKeyAttributes(GraphElementType.TRANSACTION);
final int[] keyAttributeIds = keyAttributes.stream().map(keyAttribute -> keyAttribute.ensure(graph)).mapToInt(keyAttributeId -> keyAttributeId).toArray();
graph.setPrimaryKey(GraphElementType.TRANSACTION, keyAttributeIds);
}
}
use of au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionType in project constellation by constellation-app.
the class TransactionTypeIOProvider method readTypeObject.
private static SchemaTransactionType readTypeObject(final JsonNode type) {
final JsonNode name = type.get(NAME_FIELD);
final JsonNode description = type.get(DESCRIPTION_FIELD);
final JsonNode color = type.get(COLOR_FIELD);
final JsonNode style = type.get(STYLE_FIELD);
final JsonNode directed = type.get(DIRECTED_FIELD);
final JsonNode superType = type.get(SUPERTYPE_FIELD);
final JsonNode overriddenType = type.get(OVERRIDDEN_TYPE_FIELD);
final JsonNode properties = type.get(PROPERTIES_FIELD);
final JsonNode incomplete = type.get(INCOMPLETE_FIELD);
final Map<String, String> props = new HashMap<>();
if (!properties.isNull() && properties.isObject()) {
final Iterator<String> keys = properties.fieldNames();
while (keys.hasNext()) {
final String key = keys.next();
final JsonNode value = properties.get(key);
props.put(key, value.isNull() ? null : value.textValue());
}
}
final SchemaTransactionType schemaTransactionType = new SchemaTransactionType.Builder(name.textValue()).setDescription(description == null ? null : description.textValue()).setColor(color == null ? null : readColorObject(color)).setStyle(style == null ? null : LineStyle.valueOf(style.textValue())).setDirected(directed == null ? null : directed.booleanValue()).setSuperType(superType == null ? null : readTypeObject(superType)).setOverridenType(overriddenType == null ? null : SchemaTransactionTypeUtilities.getType(overriddenType.textValue())).setProperties(props).setIncomplete(incomplete == null ? null : incomplete.booleanValue()).build();
final SchemaTransactionType singletonType = SchemaTransactionTypeUtilities.getType(schemaTransactionType.getName());
return schemaTransactionType.equals(singletonType) ? singletonType : schemaTransactionType;
}
use of au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionType in project constellation by constellation-app.
the class ScoreAnalyticPlugin method onPrerequisiteAttributeChange.
@Override
public void onPrerequisiteAttributeChange(final Graph graph, final PluginParameters parameters) {
final Set<TransactionTypeParameterValue> transactionTypes = new HashSet<>();
if (graph != null) {
final ReadableGraph readableGraph = graph.getReadableGraph();
try {
final int typeAttributeId = AnalyticConcept.TransactionAttribute.TYPE.get(readableGraph);
final int transactionCount = readableGraph.getTransactionCount();
for (int transactionPosition = 0; transactionPosition < transactionCount; transactionPosition++) {
final int transactionId = readableGraph.getTransaction(transactionPosition);
if (typeAttributeId != Graph.NOT_FOUND) {
final SchemaTransactionType type = readableGraph.getObjectValue(typeAttributeId, transactionId);
transactionTypes.add(new TransactionTypeParameterValue(type));
}
}
} finally {
readableGraph.release();
}
}
// TRANSACTION_TYPES_PARAMETER always of type MultiChoiceParameter
@SuppressWarnings("unchecked") final PluginParameter<MultiChoiceParameterValue> transactionTypesParam = (PluginParameter<MultiChoiceParameterValue>) parameters.getParameters().get(TRANSACTION_TYPES_PARAMETER_ID);
MultiChoiceParameterType.setOptionsData(transactionTypesParam, new ArrayList<>(transactionTypes));
MultiChoiceParameterType.setChoicesData(transactionTypesParam, new ArrayList<>(transactionTypes));
}
use of au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionType in project constellation by constellation-app.
the class MergeTransactionsByDateTime method getTransactionsToMerge.
@Override
public Map<Integer, Set<Integer>> getTransactionsToMerge(GraphWriteMethods graph, Comparator<Long> leadTransactionChooser, int threshold, boolean selectedOnly) throws MergeException {
final Map<SchemaTransactionType, List<Integer>> typeMap = new HashMap<>();
final Map<Integer, Set<Integer>> transactionsToMerge = new HashMap<>();
final int identifierAttribute = VisualConcept.TransactionAttribute.IDENTIFIER.get(graph);
final int typeAttribute = AnalyticConcept.TransactionAttribute.TYPE.get(graph);
final int dateTimeAttribute = TemporalConcept.TransactionAttribute.DATETIME.get(graph);
final int selectedAttribute = selectedOnly ? VisualConcept.TransactionAttribute.SELECTED.get(graph) : Graph.NOT_FOUND;
if (typeAttribute == Graph.NOT_FOUND || dateTimeAttribute == Graph.NOT_FOUND || identifierAttribute == Graph.NOT_FOUND) {
return transactionsToMerge;
}
final int edgeCount = graph.getEdgeCount();
for (int edgePosition = 0; edgePosition < edgeCount; edgePosition++) {
final int edgeId = graph.getEdge(edgePosition);
final int transactionCount = graph.getEdgeTransactionCount(edgeId);
if (transactionCount <= 1) {
continue;
}
for (int transactionPosition = 0; transactionPosition < transactionCount; transactionPosition++) {
final int transactionId = graph.getEdgeTransaction(edgeId, transactionPosition);
if (!(selectedAttribute == Graph.NOT_FOUND || graph.getBooleanValue(selectedAttribute, transactionId))) {
continue;
}
SchemaTransactionType type = (SchemaTransactionType) graph.getObjectValue(typeAttribute, transactionId);
if (type != null) {
type = (SchemaTransactionType) type.getTopLevelType();
}
List<Integer> typeTransactions = typeMap.get(type);
if (typeTransactions == null) {
typeTransactions = new ArrayList<>();
}
typeTransactions.add(transactionId);
typeMap.put(type, typeTransactions);
}
// If transactions of a particular type match and fall within threshold then add to transactions to merge
for (SchemaTransactionType type : typeMap.keySet()) {
final List<Integer> transactionsList = typeMap.get(type);
final Integer[] transactions = transactionsList.toArray(new Integer[transactionsList.size()]);
sortTransactions(transactions, graph, typeAttribute, dateTimeAttribute, leadTransactionChooser);
Set<Integer> mergeGroup = new HashSet<>();
Integer currentLead = null;
for (int arrayPosition = 0; arrayPosition < transactions.length - 1; arrayPosition++) {
final int transactionA = transactions[arrayPosition];
final int transactionB = transactions[arrayPosition + 1];
final long transactionADateTime = graph.getLongValue(dateTimeAttribute, transactionA);
final long transactionBDateTime = graph.getLongValue(dateTimeAttribute, transactionB);
final long timeDifference = transactionADateTime - transactionBDateTime > 0 ? transactionADateTime - transactionBDateTime : transactionBDateTime - transactionADateTime;
if (timeDifference <= threshold * 1000) {
// will only be successfully added for the first element added to the empty set
mergeGroup.add(transactionA);
mergeGroup.add(transactionB);
// We know the latest edition to the merge group will be the lead transaction
currentLead = transactionB;
// this check is so it adds the last mergeGroup to the map of transactions to merge (it wouldn't add it otherwise)
if (arrayPosition == transactions.length - 2) {
transactionsToMerge.put(currentLead, mergeGroup);
}
} else if (currentLead != null) {
transactionsToMerge.put(currentLead, mergeGroup);
// reset the values
currentLead = null;
mergeGroup = new HashSet<>();
} else {
// Do nothing
}
}
}
typeMap.clear();
}
return transactionsToMerge;
}
use of au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionType in project constellation by constellation-app.
the class SelectTopNPlugin method createParameters.
@Override
public PluginParameters createParameters() {
final PluginParameters params = new PluginParameters();
final List<String> modes = new ArrayList<>();
modes.add(NODE);
modes.add(TRANSACTION);
final PluginParameter<SingleChoiceParameterValue> modeParameter = SingleChoiceParameterType.build(MODE_PARAMETER_ID);
modeParameter.setName("Mode");
modeParameter.setDescription("Select either the Node or Transaction mode");
SingleChoiceParameterType.setOptions(modeParameter, modes);
params.addParameter(modeParameter);
final PluginParameter<SingleChoiceParameterValue> typeCategoryParameter = SingleChoiceParameterType.build(TYPE_CATEGORY_PARAMETER_ID);
typeCategoryParameter.setName("Type Category");
typeCategoryParameter.setDescription("The high level type category");
params.addParameter(typeCategoryParameter);
final PluginParameter<MultiChoiceParameterValue> typeParameter = MultiChoiceParameterType.build(TYPE_PARAMETER_ID);
typeParameter.setName("Specific Types");
typeParameter.setDescription("The specific types to include when calculating the top N");
params.addParameter(typeParameter);
final PluginParameter<IntegerParameterValue> limitParameter = IntegerParameterType.build(LIMIT_PARAMETER_ID);
limitParameter.setName("Limit");
limitParameter.setDescription("The limit, default being 10");
limitParameter.setIntegerValue(10);
params.addParameter(limitParameter);
params.addController(MODE_PARAMETER_ID, (PluginParameter<?> master, Map<String, PluginParameter<?>> parameters, ParameterChange change) -> {
if (change == ParameterChange.VALUE) {
final String mode = parameters.get(MODE_PARAMETER_ID).getStringValue();
if (mode != null) {
final List<String> types = new ArrayList<>();
switch(mode) {
case NODE:
for (final SchemaVertexType type : SchemaVertexTypeUtilities.getTypes()) {
if (type.isTopLevelType()) {
types.add(type.getName());
}
}
break;
case TRANSACTION:
for (final SchemaTransactionType type : SchemaTransactionTypeUtilities.getTypes()) {
if (type.isTopLevelType()) {
types.add(type.getName());
}
}
break;
default:
LOGGER.severe("Invalid mode provided. Mode values accepted are " + NODE + " or " + TRANSACTION);
}
// TYPE_CATEGORY_PARAMETER will always be of type SingleChoiceParameter
@SuppressWarnings("unchecked") final PluginParameter<SingleChoiceParameterValue> typeCategoryParamter = (PluginParameter<SingleChoiceParameterValue>) parameters.get(TYPE_CATEGORY_PARAMETER_ID);
types.sort(String::compareTo);
SingleChoiceParameterType.setOptions(typeCategoryParamter, types);
}
}
});
params.addController(TYPE_CATEGORY_PARAMETER_ID, (PluginParameter<?> master, Map<String, PluginParameter<?>> parameters, ParameterChange change) -> {
if (change == ParameterChange.VALUE) {
final String mode = parameters.get(MODE_PARAMETER_ID).getStringValue();
final String typeCategory = parameters.get(TYPE_CATEGORY_PARAMETER_ID).getStringValue();
if (mode != null && typeCategory != null) {
final List<String> types = new ArrayList<>();
switch(mode) {
case NODE:
final SchemaVertexType typeCategoryVertexType = SchemaVertexTypeUtilities.getType(typeCategory);
for (final SchemaVertexType type : SchemaVertexTypeUtilities.getTypes()) {
if (type.getSuperType().equals(typeCategoryVertexType)) {
types.add(type.getName());
}
}
break;
case TRANSACTION:
final SchemaTransactionType typeCategoryTransactionType = SchemaTransactionTypeUtilities.getType(typeCategory);
for (final SchemaTransactionType type : SchemaTransactionTypeUtilities.getTypes()) {
if (type.getSuperType().equals(typeCategoryTransactionType)) {
types.add(type.getName());
}
}
break;
default:
break;
}
// update the sub level types
// TYPE_PARAMETER will always be of type MultiChoiceParameter
@SuppressWarnings("unchecked") final PluginParameter<MultiChoiceParameterValue> typeParamter = (PluginParameter<MultiChoiceParameterValue>) parameters.get(TYPE_PARAMETER_ID);
types.sort(String::compareTo);
MultiChoiceParameterType.setOptions(typeParamter, types);
MultiChoiceParameterType.setChoices(typeParamter, types);
}
}
});
return params;
}
Aggregations