Search in sources :

Example 41 with GraphRecordStore

use of au.gov.asd.tac.constellation.graph.processing.GraphRecordStore in project constellation by constellation-app.

the class CompareGraphPluginNGTest method testCompareGraphsWhenEverythingIsAnAddChange.

@Test
public void testCompareGraphsWhenEverythingIsAnAddChange() throws Exception {
    // mimic GraphRecordStoreUtilities.getAll()
    final GraphRecordStore original = new GraphRecordStore();
    final GraphRecordStore compare = new GraphRecordStore();
    compare.add();
    compare.set(GraphRecordStoreUtilities.SOURCE + VisualConcept.VertexAttribute.LABEL, "vx0");
    compare.add();
    compare.set(GraphRecordStoreUtilities.SOURCE + VisualConcept.VertexAttribute.LABEL, "vx1");
    compare.add();
    compare.set(GraphRecordStoreUtilities.SOURCE + VisualConcept.VertexAttribute.LABEL, "vx0");
    compare.set(GraphRecordStoreUtilities.DESTINATION + VisualConcept.VertexAttribute.LABEL, "vx1");
    final Set<String> vertexPrimaryKeys = new HashSet<>();
    vertexPrimaryKeys.add(VisualConcept.VertexAttribute.LABEL.getName());
    final List<String> ignoreVertexAttributes = new ArrayList<>();
    final List<String> ignoreTransactionAttributes = new ArrayList<>();
    ignoreVertexAttributes.add("[id]");
    ignoreTransactionAttributes.add("[id]");
    final CompareGraphPlugin instance = new CompareGraphPlugin();
    final GraphRecordStore changes = instance.compareGraphs("", original, compare, vertexPrimaryKeys, new HashSet<>(), ignoreVertexAttributes, ignoreTransactionAttributes, ADDED_COLOUR, REMOVED_COLOUR, CHANGED_COLOUR, UNCHANGED_COLOUR);
    System.out.println("original=>" + original.toStringVerbose());
    System.out.println("compare=>" + compare.toStringVerbose());
    System.out.println("changes=>" + changes.toStringVerbose());
    assertEquals(changes.size(), 3);
    changes.reset();
    changes.next();
    assertEquals("vx0", changes.get(GraphRecordStoreUtilities.SOURCE + VisualConcept.VertexAttribute.LABEL));
    assertEquals(CompareGraphPlugin.ADDED, changes.get(GraphRecordStoreUtilities.SOURCE + CompareGraphPlugin.COMPARE_ATTRIBUTE));
    changes.next();
    assertEquals("vx1", changes.get(GraphRecordStoreUtilities.SOURCE + VisualConcept.VertexAttribute.LABEL));
    assertEquals(CompareGraphPlugin.ADDED, changes.get(GraphRecordStoreUtilities.SOURCE + CompareGraphPlugin.COMPARE_ATTRIBUTE));
    changes.next();
    assertEquals("vx0", changes.get(GraphRecordStoreUtilities.SOURCE + VisualConcept.VertexAttribute.LABEL));
    assertEquals("vx1", changes.get(GraphRecordStoreUtilities.DESTINATION + VisualConcept.VertexAttribute.LABEL));
    assertEquals(CompareGraphPlugin.ADDED, changes.get(GraphRecordStoreUtilities.TRANSACTION + CompareGraphPlugin.COMPARE_ATTRIBUTE));
}
Also used : ArrayList(java.util.ArrayList) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 42 with GraphRecordStore

use of au.gov.asd.tac.constellation.graph.processing.GraphRecordStore in project constellation by constellation-app.

the class AttributeUtilities method getTypesUsedByGraph.

/**
 * Return a set of types used by a graph
 *
 * @param graph The graph
 * @return Set of types used by a graph
 */
public static Set<String> getTypesUsedByGraph(Graph graph) {
    final List<String> types;
    final ReadableGraph rg = graph.getReadableGraph();
    try {
        final GraphRecordStore recordstore = GraphRecordStoreUtilities.getVertices(rg, false, false, false);
        types = recordstore.getAll(GraphRecordStoreUtilities.SOURCE + AnalyticConcept.VertexAttribute.TYPE);
    } finally {
        rg.release();
    }
    return types != null ? new TreeSet<>(types) : new TreeSet<>();
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore)

Example 43 with GraphRecordStore

use of au.gov.asd.tac.constellation.graph.processing.GraphRecordStore in project constellation by constellation-app.

the class GetRecordStore 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 selected = parameters.getBooleanValue(SELECTED_PARAMETER_ID);
    final boolean vx = parameters.getBooleanValue(VX_PARAMETER_ID);
    final boolean tx = parameters.getBooleanValue(TX_PARAMETER_ID);
    final String attrsParam = parameters.getStringValue(ATTRS_PARAMETER_ID);
    // Allow the user to specify a specific set of attributes,
    // cutting down data transfer and processing a lot,
    // particularly on the Python side.
    final String[] attrsArray = attrsParam != null ? attrsParam.split(",") : new String[0];
    // Maintain the order specified by the user.
    final Set<String> attrs = new LinkedHashSet<>();
    Collections.addAll(attrs, attrsArray);
    // Build the JSON in a form suitable for passing to pandas.DataFrame.from_items().
    // This includes the datatypes with the names, so the client can do transforms
    // where required (for example, converting strings to timestamps).
    // 
    final IoProgress ioph = new HandleIoProgress("External script: get RecordStore");
    ioph.start();
    ioph.progress("Building RecordStore...");
    final GraphRecordStore recordStore;
    final Graph graph = graphId == null ? RestUtilities.getActiveGraph() : GraphNode.getGraph(graphId);
    if (graph == null) {
        throw new RestServiceException(HTTP_UNPROCESSABLE_ENTITY, "No graph with id " + graphId);
    }
    final ReadableGraph rg = graph.getReadableGraph();
    try {
        if ((vx && tx) || !(vx || tx)) {
            // We're getting the entire graph. We don't want all
            // of the vertices: since all of the vertices with
            // transactions are already included, we only want
            // the rest of the vertices, ie the singletons.
            recordStore = GraphRecordStoreUtilities.getAll(rg, true, selected, false);
        } else if (vx) {
            recordStore = GraphRecordStoreUtilities.getVertices(rg, false, selected, false);
        } else {
            recordStore = GraphRecordStoreUtilities.getTransactions(rg, selected, false);
        }
    } finally {
        rg.release();
    }
    // Create a mapping from "attrname" to "attrname<type>" for
    // all of the RecordStore attributes.
    final Map<String, String> attrToTypedAttr = new HashMap<>();
    for (final String kt : recordStore.keysWithType()) {
        final int ix = kt.lastIndexOf('<');
        final String key = kt.substring(0, ix);
        attrToTypedAttr.put(key, kt);
    }
    if (!attrs.isEmpty() && recordStore.size() > 0) {
        // Check that all of the user-specified attributes exist.
        final StringJoiner buf = new StringJoiner(",");
        for (final String key : attrs) {
            if (!attrToTypedAttr.containsKey(key)) {
                buf.add(key);
            }
        }
        if (buf.length() != 0) {
            throw new RestServiceException("The following attributes do not exist in the record store: " + buf.toString());
        }
    } else {
        // The user didn't specify any attributes, so use all of
        // the RecordStore keys.
        attrs.addAll(attrToTypedAttr.keySet());
    }
    final ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.INDENT_OUTPUT, false);
    mapper.configure(SerializationFeature.CLOSE_CLOSEABLE, false);
    final ObjectNode root = mapper.createObjectNode();
    // We want to build a JSON document that looks like:
    // 
    // {"columns":["A","B"],"data":[[1,"a"],[2,"b"],[3,"c"]]}
    // 
    // which can be read by pandas.read_json(..., orient="split").
    // (It appears that the index parameter is not required.)
    final ArrayNode columns = root.putArray("columns");
    final ArrayNode data = root.putArray("data");
    if (recordStore.size() > 0) {
        ioph.progress("Building DataFrame...");
        for (final String attr : attrs) {
            final String kt = attrToTypedAttr.get(attr);
            columns.add(keyedName(kt));
        }
        recordStore.reset();
        while (recordStore.next()) {
            final ArrayNode row = data.addArray();
            for (final String attr : attrs) {
                final String kt = attrToTypedAttr.get(attr);
                final String type = kt.substring(kt.lastIndexOf('<') + 1, kt.length() - 1);
                final String value = recordStore.get(kt);
                RestUtilities.addData(row, type, value);
            }
        }
    }
    mapper.writeValue(out, root);
    ioph.finish();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HashMap(java.util.HashMap) HandleIoProgress(au.gov.asd.tac.constellation.utilities.gui.HandleIoProgress) RestServiceException(au.gov.asd.tac.constellation.webserver.restapi.RestServiceException) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) StringJoiner(java.util.StringJoiner) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HandleIoProgress(au.gov.asd.tac.constellation.utilities.gui.HandleIoProgress) IoProgress(au.gov.asd.tac.constellation.utilities.gui.IoProgress)

Example 44 with GraphRecordStore

use of au.gov.asd.tac.constellation.graph.processing.GraphRecordStore in project constellation by constellation-app.

the class GraphRecordStoreUtilitiesNGTest method addedRecordStoreSupportsTypesWithOverriddenDirectionWithInitialiseAndCompleteWithSchema.

@Test
public void addedRecordStoreSupportsTypesWithOverriddenDirectionWithInitialiseAndCompleteWithSchema() {
    final RecordStore recordStore = new GraphRecordStore();
    recordStore.add();
    recordStore.set(GraphRecordStoreUtilities.SOURCE + VisualConcept.VertexAttribute.IDENTIFIER, "vx0");
    recordStore.set(GraphRecordStoreUtilities.DESTINATION + VisualConcept.VertexAttribute.IDENTIFIER, "vx1");
    recordStore.set(GraphRecordStoreUtilities.TRANSACTION + GraphRecordStoreUtilities.DIRECTED_KEY, false);
    // the communication type is directed
    recordStore.set(GraphRecordStoreUtilities.TRANSACTION + AnalyticConcept.TransactionAttribute.TYPE, AnalyticConcept.TransactionType.COMMUNICATION);
    final boolean initializeWithSchema = true;
    final boolean completeWithSchema = true;
    final List<String> vertexIdAttributes = new ArrayList<>();
    final Map<String, Integer> vertexMap = new HashMap<>();
    final Map<String, Integer> transactionMap = new HashMap<>();
    final List<Integer> veritices = GraphRecordStoreUtilities.addRecordStoreToGraph(graph, recordStore, initializeWithSchema, completeWithSchema, vertexIdAttributes, vertexMap, transactionMap);
    assertEquals(2, veritices.size());
    assertEquals(2, graph.getVertexCount());
    assertEquals(1, graph.getTransactionCount());
    // try {
    // SaveGraphUtilities.saveGraphToTemporaryDirectory(graph, "testAddedRecordStoreSupportsTypesWithOverriddenDirectionWithInitialiseAndCompleteWithSchema");
    // } catch (IOException ex) {
    // Assert.fail(ex.getLocalizedMessage());
    // }
    final int transactionTypeId = AnalyticConcept.TransactionAttribute.TYPE.get(graph);
    SchemaTransactionType type = (SchemaTransactionType) graph.getObjectValue(transactionTypeId, 0);
    Assert.assertFalse(type.isDirected());
}
Also used : SchemaTransactionType(au.gov.asd.tac.constellation.graph.schema.type.SchemaTransactionType) HashMap(java.util.HashMap) RecordStore(au.gov.asd.tac.constellation.graph.processing.RecordStore) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) ArrayList(java.util.ArrayList) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) Test(org.testng.annotations.Test)

Example 45 with GraphRecordStore

use of au.gov.asd.tac.constellation.graph.processing.GraphRecordStore in project constellation by constellation-app.

the class GraphRecordStoreUtilitiesNGTest method addRecordStoreToGraphWithExistingGraph.

@Test
public void addRecordStoreToGraphWithExistingGraph() {
    int vx0, vx1;
    int identifierAttribute, colorAttribute;
    identifierAttribute = VisualConcept.VertexAttribute.IDENTIFIER.ensure(graph);
    colorAttribute = VisualConcept.VertexAttribute.COLOR.ensure(graph);
    graph.setPrimaryKey(GraphElementType.VERTEX, identifierAttribute);
    vx0 = graph.addVertex();
    vx1 = graph.addVertex();
    graph.setStringValue(identifierAttribute, vx0, "vx0");
    graph.setStringValue(identifierAttribute, vx1, "vx1");
    final RecordStore recordStore = new GraphRecordStore();
    recordStore.add();
    recordStore.set(GraphRecordStoreUtilities.SOURCE + VisualConcept.VertexAttribute.IDENTIFIER, "vx0");
    recordStore.set(GraphRecordStoreUtilities.SOURCE + "color", "DarkGreen");
    recordStore.add();
    recordStore.set(GraphRecordStoreUtilities.SOURCE + VisualConcept.VertexAttribute.IDENTIFIER, "vx0");
    recordStore.set(GraphRecordStoreUtilities.DESTINATION + VisualConcept.VertexAttribute.IDENTIFIER, "vx1");
    final boolean initializeWithSchema = false;
    final boolean completeWithSchema = false;
    final List<String> vertexIdAttributes = new ArrayList<>();
    final Map<String, Integer> vertexMap = new HashMap<>();
    final Map<String, Integer> transactionMap = new HashMap<>();
    final List<Integer> verticies = GraphRecordStoreUtilities.addRecordStoreToGraph(graph, recordStore, initializeWithSchema, completeWithSchema, vertexIdAttributes, vertexMap, transactionMap);
    graph.validateKeys();
    assertEquals(3, verticies.size());
    assertEquals(2, graph.getVertexCount());
    assertEquals(1, graph.getTransactionCount());
    vx0 = graph.getVertex(vx0);
    vx1 = graph.getVertex(vx1);
    assertEquals("vx0", graph.getStringValue(identifierAttribute, vx0));
    assertEquals("DarkGreen", graph.getStringValue(colorAttribute, vx0));
    assertEquals("vx1", graph.getStringValue(identifierAttribute, vx1));
}
Also used : HashMap(java.util.HashMap) RecordStore(au.gov.asd.tac.constellation.graph.processing.RecordStore) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) ArrayList(java.util.ArrayList) GraphRecordStore(au.gov.asd.tac.constellation.graph.processing.GraphRecordStore) Test(org.testng.annotations.Test)

Aggregations

GraphRecordStore (au.gov.asd.tac.constellation.graph.processing.GraphRecordStore)47 Test (org.testng.annotations.Test)33 ArrayList (java.util.ArrayList)24 RecordStore (au.gov.asd.tac.constellation.graph.processing.RecordStore)19 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)19 PluginException (au.gov.asd.tac.constellation.plugins.PluginException)15 Graph (au.gov.asd.tac.constellation.graph.Graph)12 ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)11 StoreGraph (au.gov.asd.tac.constellation.graph.StoreGraph)10 HashSet (java.util.HashSet)10 DefaultPluginInteraction (au.gov.asd.tac.constellation.graph.node.plugins.DefaultPluginInteraction)9 WritableGraph (au.gov.asd.tac.constellation.graph.WritableGraph)8 PluginInteraction (au.gov.asd.tac.constellation.plugins.PluginInteraction)8 HashMap (java.util.HashMap)8 DualGraph (au.gov.asd.tac.constellation.graph.locking.DualGraph)7 Schema (au.gov.asd.tac.constellation.graph.schema.Schema)7 IOException (java.io.IOException)5 Plugin (au.gov.asd.tac.constellation.plugins.Plugin)3 TextPluginInteraction (au.gov.asd.tac.constellation.plugins.text.TextPluginInteraction)3 File (java.io.File)3