Search in sources :

Example 1 with Edge

use of org.codice.alliance.nsili.common.UCO.Edge in project alliance by codice.

the class ResultDAGConverter method getNodeEdgeDirectedAcyclicGraph.

private static DirectedAcyclicGraph<Node, Edge> getNodeEdgeDirectedAcyclicGraph(DAG dag, Map<Integer, Node> nodeMap) {
    DirectedAcyclicGraph<Node, Edge> graph = new DirectedAcyclicGraph<>(Edge.class);
    for (Node node : dag.nodes) {
        graph.addVertex(node);
    }
    for (Edge edge : dag.edges) {
        Node node1 = nodeMap.get(edge.start_node);
        Node node2 = nodeMap.get(edge.end_node);
        if (node1 != null && node2 != null) {
            graph.addEdge(node1, node2);
        }
    }
    return graph;
}
Also used : Node(org.codice.alliance.nsili.common.UCO.Node) Edge(org.codice.alliance.nsili.common.UCO.Edge) DirectedAcyclicGraph(org.jgrapht.experimental.dag.DirectedAcyclicGraph)

Example 2 with Edge

use of org.codice.alliance.nsili.common.UCO.Edge in project alliance by codice.

the class ResultDAGConverter method convertResult.

public static DAG convertResult(Result result, ORB orb, POA poa, List<String> resultAttributes, Map<String, List<String>> mandatoryAttributes) throws DagParsingException {
    Metacard metacard = result.getMetacard();
    DAG dag = new DAG();
    DirectedAcyclicGraph<Node, Edge> graph = new DirectedAcyclicGraph<>(Edge.class);
    ProductImpl productImpl = new ProductImpl();
    String id = result.getMetacard().getId();
    if (!CorbaUtils.isIdActive(poa, id.getBytes(Charset.forName(ENCODING)))) {
        try {
            poa.activate_object_with_id(id.getBytes(Charset.forName(ENCODING)), productImpl);
        } catch (ServantAlreadyActive | ObjectAlreadyActive | WrongPolicy e) {
            LOGGER.debug("Convert DAG : Unable to activate product impl object ({}): {}", result.getMetacard().getId(), e.getLocalizedMessage());
        }
    }
    org.omg.CORBA.Object obj = poa.create_reference_with_id(id.getBytes(Charset.forName(ENCODING)), ProductHelper.id());
    Product product = ProductHelper.narrow(obj);
    Node productNode = createRootNode(orb);
    String attributeName = NsiliConstants.NSIL_PRODUCT;
    Any productAny = orb.create_any();
    ProductHelper.insert(productAny, product);
    productNode.value = productAny;
    graph.addVertex(productNode);
    List<String> addedAttributes = new ArrayList<>();
    addedAttributes.addAll(addCardNodeWithAttributes(graph, productNode, metacard, orb, attributeName + ":", resultAttributes));
    addedAttributes.addAll(addFileNodeWithAttributes(graph, productNode, metacard, orb, attributeName + ":", resultAttributes));
    addedAttributes.addAll(addSecurityNodeWithAttributes(graph, productNode, metacard, orb, attributeName + ":", resultAttributes));
    addedAttributes.addAll(addMetadataSecurityNodeWithAttributes(graph, productNode, metacard, orb, attributeName + ":", resultAttributes));
    addedAttributes.addAll(addParts(graph, productNode, metacard, orb, attributeName + ":", resultAttributes));
    if (metacard.getThumbnail() != null && metacard.getThumbnail().length > 0) {
        addedAttributes.addAll(addThumbnailRelatedFile(graph, productNode, metacard, orb, attributeName + ":", resultAttributes));
    }
    if (mandatoryAttributes != null && !mandatoryAttributes.isEmpty()) {
        final ThreadLocal<Boolean> dataIsValid = new ThreadLocal<>();
        dataIsValid.set(true);
        Map<String, List<String>> addedAttrMap = getAttrMap(addedAttributes);
        addedAttrMap.entrySet().forEach(entry -> dataIsValid.set(dataIsValid.get() && processEntry(entry.getKey(), mandatoryAttributes.get(entry.getKey()), entry.getValue())));
        if (!dataIsValid.get()) {
            throw new DagParsingException("One or more mandatory attributes is missing on outgoing data");
        }
    }
    graph.addVertex(productNode);
    NsiliCommonUtils.setUCOEdgeIds(graph);
    NsiliCommonUtils.setUCOEdges(productNode, graph);
    dag.edges = NsiliCommonUtils.getEdgeArrayFromGraph(graph);
    dag.nodes = NsiliCommonUtils.getNodeArrayFromGraph(graph);
    return dag;
}
Also used : Node(org.codice.alliance.nsili.common.UCO.Node) ArrayList(java.util.ArrayList) Product(org.codice.alliance.nsili.common.UID.Product) Any(org.omg.CORBA.Any) DirectedAcyclicGraph(org.jgrapht.experimental.dag.DirectedAcyclicGraph) WrongPolicy(org.omg.PortableServer.POAPackage.WrongPolicy) List(java.util.List) ArrayList(java.util.ArrayList) ServantAlreadyActive(org.omg.PortableServer.POAPackage.ServantAlreadyActive) ObjectAlreadyActive(org.omg.PortableServer.POAPackage.ObjectAlreadyActive) DAG(org.codice.alliance.nsili.common.UCO.DAG) Metacard(ddf.catalog.data.Metacard) Edge(org.codice.alliance.nsili.common.UCO.Edge)

Example 3 with Edge

use of org.codice.alliance.nsili.common.UCO.Edge in project alliance by codice.

the class NsiliCommonUtils method setUCOEdgeIds.

/**
 * Set the UCO.Node IDs in DFS order to conform to the NSILI spec. The root of the node will be 0.
 *
 * @param graph - the graph representation of the DAG
 */
public static void setUCOEdgeIds(Graph<Node, Edge> graph) {
    if (graph != null) {
        int id = 0;
        DepthFirstIterator<Node, Edge> depthFirstIterator = new DepthFirstIterator<>(graph);
        while (depthFirstIterator.hasNext()) {
            Node node = depthFirstIterator.next();
            node.id = id;
            id++;
        }
    }
}
Also used : DepthFirstIterator(org.jgrapht.traverse.DepthFirstIterator) Node(org.codice.alliance.nsili.common.UCO.Node) Edge(org.codice.alliance.nsili.common.UCO.Edge)

Example 4 with Edge

use of org.codice.alliance.nsili.common.UCO.Edge in project alliance by codice.

the class DAGGenerator method generateNSILDAG.

private static DAG generateNSILDAG(ORB orb, String partType, String commonType, String title) {
    DAG metacard = new DAG();
    Graph<Node, Edge> graph = new DirectedAcyclicGraph<>(Edge.class);
    Node[] nodeRefs = constructNSILProduct(orb, graph, 1, commonType);
    constructNSILPart(nodeRefs[0], nodeRefs[1], nodeRefs[3], orb, graph, partType, title);
    constructNSILPart(nodeRefs[0], nodeRefs[1], nodeRefs[3], orb, graph, NsiliConstants.NSIL_COVERAGE, title);
    constructNSILPart(nodeRefs[0], nodeRefs[1], nodeRefs[3], orb, graph, NsiliConstants.NSIL_EXPLOITATION_INFO, title);
    constructNSILAssociation(nodeRefs[0], nodeRefs[2], orb, graph, 1);
    NsiliCommonUtils.setUCOEdgeIds(graph);
    NsiliCommonUtils.setUCOEdges(nodeRefs[0], graph);
    metacard.nodes = NsiliCommonUtils.getNodeArrayFromGraph(graph);
    metacard.edges = NsiliCommonUtils.getEdgeArrayFromGraph(graph);
    return metacard;
}
Also used : Node(org.codice.alliance.nsili.common.UCO.Node) DAG(org.codice.alliance.nsili.common.UCO.DAG) Edge(org.codice.alliance.nsili.common.UCO.Edge) DirectedAcyclicGraph(org.jgrapht.experimental.dag.DirectedAcyclicGraph)

Example 5 with Edge

use of org.codice.alliance.nsili.common.UCO.Edge in project alliance by codice.

the class DAGConverter method parseGraph.

private MetacardImpl parseGraph(DirectedAcyclicGraph<Node, Edge> graph, boolean swapCoordinates) {
    MetacardImpl metacard = new MetacardImpl(nsiliMetacardType);
    List<Serializable> associatedCards = new ArrayList<>();
    // Traverse the graph
    DepthFirstIterator<Node, Edge> depthFirstIterator = new DepthFirstIterator<>(graph);
    Node parentEntity = null;
    Node assocNode = null;
    while (depthFirstIterator.hasNext()) {
        Node node = depthFirstIterator.next();
        if (node.node_type == NodeType.ROOT_NODE && node.attribute_name.equals(NsiliConstants.NSIL_PRODUCT)) {
        // Nothing to process from root node
        } else if (node.node_type == NodeType.ENTITY_NODE) {
            parentEntity = node;
            assocNode = getAssocNode(graph, assocNode, node);
        } else if (node.node_type == NodeType.RECORD_NODE) {
        // Nothing to process from record node
        } else if (parentEntity != null && node.node_type == NodeType.ATTRIBUTE_NODE && node.value != null) {
            addNsiliAttribute(swapCoordinates, metacard, associatedCards, parentEntity, assocNode, node);
        }
    }
    // Add associated data
    if (!associatedCards.isEmpty()) {
        boolean firstAssoc = true;
        AttributeImpl attribute = null;
        for (Serializable association : associatedCards) {
            if (firstAssoc) {
                attribute = new AttributeImpl(Associations.RELATED, association);
                metacard.setAttribute(attribute);
                firstAssoc = false;
            } else {
                attribute.addValue(association);
            }
        }
    }
    return metacard;
}
Also used : DepthFirstIterator(org.jgrapht.traverse.DepthFirstIterator) Serializable(java.io.Serializable) Node(org.codice.alliance.nsili.common.UCO.Node) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ArrayList(java.util.ArrayList) Edge(org.codice.alliance.nsili.common.UCO.Edge) MetacardImpl(ddf.catalog.data.impl.MetacardImpl)

Aggregations

Edge (org.codice.alliance.nsili.common.UCO.Edge)31 Node (org.codice.alliance.nsili.common.UCO.Node)31 DirectedAcyclicGraph (org.jgrapht.experimental.dag.DirectedAcyclicGraph)23 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)21 DAG (org.codice.alliance.nsili.common.UCO.DAG)21 Test (org.junit.Test)19 File (java.io.File)11 IOException (java.io.IOException)11 PrintStream (java.io.PrintStream)11 DepthFirstIterator (org.jgrapht.traverse.DepthFirstIterator)6 ArrayList (java.util.ArrayList)4 Metacard (ddf.catalog.data.Metacard)1 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)1 Serializable (java.io.Serializable)1 ArrayDeque (java.util.ArrayDeque)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Product (org.codice.alliance.nsili.common.UID.Product)1 DijkstraShortestPath (org.jgrapht.alg.DijkstraShortestPath)1 ConnectedComponentTraversalEvent (org.jgrapht.event.ConnectedComponentTraversalEvent)1