Package au.csiro.ontology

Examples of au.csiro.ontology.Node


    protected Concept getNecessary(IConceptMap<Context> contextIndex, Map<String, Node> taxonomy, int key) {
        final Object id = factory.lookupConceptId(key);
        final List<Concept> result = new ArrayList<Concept>();

        final Node node = taxonomy.get(id);
        if (node != null) {
            for (final Node parent: node.getParents()) {
                final String parentId = parent.getEquivalentConcepts().iterator().next();
                if (!NamedConcept.TOP.equals(parentId)) {      // Top is redundant
                    result.add(new NamedConcept(parentId));
                }
            }
View Full Code Here


        start = System.currentTimeMillis();
       
        // Part 2 - Creates a node per equivalent concepts
        conceptNodeIndex = new ConcurrentHashMap<String, Node>();
       
        Node top = null;
        Node bottom = null;
       
        IConceptSet processed = new FastConceptHashSet();
        Set<Node> nodeSet = new HashSet<Node>();
       
        for(int key : equiv.keySet()) {
            if(processed.contains(key)) continue;
            IConceptSet equivs = equiv.get(key);
            processed.addAll(equivs);
           
            Node n = new Node();
            for(IntIterator it = equivs.iterator(); it.hasNext(); ) {
                int val = it.next();
                String tval = factory.lookupConceptId(val).toString();
                n.getEquivalentConcepts().add(tval);
                conceptNodeIndex.put(tval, n);

                if (val == CoreFactory.TOP_CONCEPT)
                    top = n;
                if (val == CoreFactory.BOTTOM_CONCEPT)
                    bottom = n;
            }
            nodeSet.add(n);
        }
           
        if(top == null) {
            top = new Node();
            top.getEquivalentConcepts().add(au.csiro.ontology.model.NamedConcept.TOP);
        }
       
        if(bottom == null) {
            bottom = new Node();
            bottom.getEquivalentConcepts().add(au.csiro.ontology.model.NamedConcept.BOTTOM);
        }
       
        Statistics.INSTANCE.setTime("taxonomy 2",
                System.currentTimeMillis() - start);
        start = System.currentTimeMillis();
       
        // Step 3 - Connects nodes
        Queue<Node> todo2 = new ConcurrentLinkedQueue<Node>(nodeSet);
        executor = Executors.newFixedThreadPool(numThreads);
        for (int j = 0; j < numThreads; j++) {
            Runnable worker = new TaxonomyWorker2(factory,
                    conceptNodeIndex, direc, todo2, nodeSet);
            executor.execute(worker);
        }

        executor.shutdown();
        while (!executor.isTerminated()) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        assert (todo2.isEmpty());
       
        Statistics.INSTANCE.setTime("taxonomy 3",
                System.currentTimeMillis() - start);
        start = System.currentTimeMillis();
       
        // Connect bottom
        nodeSet.remove(bottom);
        bottom.getParents().addAll(nodeSet);
        for(Node n : nodeSet) {
            n.getChildren().add(bottom);
        }
       
        Statistics.INSTANCE.setTime("taxonomy connect bottom",
                System.currentTimeMillis() - start);
        start = System.currentTimeMillis();
       
        // Connect top
        for (String key : conceptNodeIndex.keySet()) {
            if (key.equals(au.csiro.ontology.model.NamedConcept.TOP) ||
                    key.equals(au.csiro.ontology.model.NamedConcept.BOTTOM))
                continue;
            Node node = conceptNodeIndex.get(key);
            if (node.getParents().isEmpty()) {
                node.getParents().add(top);
                top.getChildren().add(node);
            }
        }

        // TODO: deal with special case where only top and bottom are present.
View Full Code Here

           
            // 2. Create nodes for new concepts and connect to node hierarchy
            // a. First create the nodes and add to index
            for (IntIterator itr = allNew.keyIterator(); itr.hasNext();) {
                final String key = factory.lookupConceptId(itr.next()).toString();
                Node cn = new Node();
                cn.getEquivalentConcepts().add(key);
                conceptNodeIndex.put(key, cn);
            }

            // b. Now connect the nodes disregarding redundant connections
            Node bottomNode = conceptNodeIndex.get(au.csiro.ontology.model.NamedConcept.BOTTOM);
            for (IntIterator itr = allNew.keyIterator(); itr.hasNext();) {
                int id = itr.next();
                final String key = factory.lookupConceptId(id).toString();
                Node cn = conceptNodeIndex.get(key);
                IConceptSet parents = allNew.get(id);
                for (IntIterator itr2 = parents.iterator(); itr2.hasNext();) {
                    // Create a connection to each parent
                    int parentId = itr2.next();
                    if (parentId == id)
                        continue;
                    Node parent = conceptNodeIndex.get(factory.lookupConceptId(parentId));
                    cn.getParents().add(parent);
                    parent.getChildren().add(cn);
                    // All nodes that get new children and are connected to BOTTOM
                    // must be disconnected
                    if (parent.getChildren().contains(bottomNode)) {
                        parent.getChildren().remove(bottomNode);
                        bottomNode.getParents().remove(parent);
                    }
                }
            }
           
            Set<Integer> toRemoveFromAffected = new HashSet<Integer>();
            for (IntIterator itr = allAffected.keyIterator(); itr.hasNext();) {
                final int id = itr.next();
                final String key = factory.lookupConceptId(id).toString();
                Node cn = conceptNodeIndex.get(key);
                IConceptSet parents = allAffected.get(id);
               
                if(parents.contains(IFactory.BOTTOM_CONCEPT)) {
                    // Special case - bottom is parent
                   
                    // a. add equivalents to bottom node
                    bottomNode.getEquivalentConcepts().addAll(cn.getEquivalentConcepts());
                   
                    Set<Node> tempParents = cn.getParents();
                    Set<Node> tempChildren = cn.getChildren();
                   
                    // b. reconnect parents to children
                    for(Node parent : tempParents) {
                        parent.getChildren().remove(cn);
                        parent.getChildren().addAll(tempChildren);
                    }
                   
                    for(Node child : tempChildren) {
                        child.getParents().remove(cn);
                        child.getParents().addAll(tempParents);
                    }
                   
                    for(String k : cn.getEquivalentConcepts()) {
                        conceptNodeIndex.remove(k);
                        conceptNodeIndex.put(key, bottomNode);
                    }
                    toRemoveFromAffected.add(id);
                } else {
                    for (IntIterator itr2 = parents.iterator(); itr2.hasNext();) {
                        // Create a connection to each parent
                        int parentId = itr2.next();
                        if (parentId == id)
                            continue;
                        Node parent = conceptNodeIndex.get(factory.lookupConceptId(parentId));
                        cn.getParents().add(parent);
                        parent.getChildren().add(cn);
                        // All nodes that get new children and are connected to BOTTOM must be disconnected
                        if (parent.getChildren().contains(bottomNode)) {
                            parent.getChildren().remove(bottomNode);
                            bottomNode.getParents().remove(parent);
                        }
                    }
                }
            }
           
            for(Integer i : toRemoveFromAffected) {
                allAffected.remove(i.intValue());
                allNew.remove(i.intValue());
            }

            // 3. Connect new nodes without parents to TOP
            Node topNode = conceptNodeIndex.get(au.csiro.ontology.model.NamedConcept.TOP);

            for (IntIterator itr = allNew.keyIterator(); itr.hasNext();) {
                final String key = factory.lookupConceptId(itr.next()).toString();
                Node cn = conceptNodeIndex.get(key);
                if (cn.getParents().isEmpty()) {
                    cn.getParents().add(topNode);
                    topNode.getChildren().add(cn);
                }
            }

            // 4. Fix connections for new and affected concepts
            // a. Check for equivalents
            Set<Pair> pairsToMerge = new HashSet<Pair>();
            for (IntIterator itr = allNew.keyIterator(); itr.hasNext();) {
                final String key = factory.lookupConceptId(itr.next()).toString();
                Node cn = conceptNodeIndex.get(key);
                for (Node parent : cn.getParents()) {
                    if (parent.getParents().contains(cn)) {
                        pairsToMerge.add(new Pair(cn, parent));
                    }
                }
            }
            for (IntIterator itr = allAffected.keyIterator(); itr.hasNext();) {
                final String key = factory.lookupConceptId(itr.next()).toString();
                Node cn = conceptNodeIndex.get(key);
                for (Node parent : cn.getParents()) {
                    if (parent.getParents().contains(cn)) {
                        pairsToMerge.add(new Pair(cn, parent));
                    }
                }
            }

            Set<Node> affectedByMerge = new HashSet<Node>();

            // Merge equivalents
            for (Pair p : pairsToMerge) {
                Node cn1 = p.getA();
                Node cn2 = p.getB();

                affectedByMerge.addAll(cn1.getChildren());
                affectedByMerge.addAll(cn2.getChildren());

                // Merge into cn1 - remove cn2 from index and replace with cn1
                for (String n : cn2.getEquivalentConcepts()) {
                    conceptNodeIndex.put(n, cn1);
                }

                cn1.getEquivalentConcepts().addAll(cn2.getEquivalentConcepts());

                // Remove relationships between merged concepts
                cn1.getParents().remove(cn2);
                cn2.getChildren().remove(cn1);
                cn2.getParents().remove(cn1);
                cn1.getChildren().remove(cn2);

                // Taxonomy is bidirectional
                cn1.getParents().addAll(cn2.getParents());
                for (Node parent : cn2.getParents()) {
                    parent.getChildren().remove(cn2);
                    parent.getChildren().add(cn1);
                }
                cn1.getChildren().addAll(cn2.getChildren());
                for (Node child : cn2.getChildren()) {
                    child.getParents().remove(cn2);
                    child.getParents().add(cn1);
                }

                cn2 = null; // nothing should reference cn2 now
            }

            // b. Fix all new and affected nodes
            Set<Node> all = new HashSet<Node>();
            for (IntIterator it = allNew.keyIterator(); it.hasNext();) {
                all.add(conceptNodeIndex.get(factory.lookupConceptId(it.next())));
            }

            for (IntIterator it = allAffected.keyIterator(); it.hasNext();) {
                all.add(conceptNodeIndex.get(factory.lookupConceptId(it.next())));
            }

            for (Node cn : affectedByMerge) {
                all.add(cn);
            }

            // Add also the children of the affected nodes
            Set<Node> childrenToAdd = new HashSet<Node>();
            for (Node cn : all) {
                for (Node ccn : cn.getChildren()) {
                    if (ccn.equals(bottomNode))
                        continue;
                    childrenToAdd.add(ccn);
                }
            }
            all.addAll(childrenToAdd);

            // Find redundant relationships
            for (Node cn : all) {
                Set<Node> ps = cn.getParents();

                Object[] parents = ps.toArray(new Object[ps.size()]);
                Set<Node> toRemove = new HashSet<Node>();
                for (int i = 0; i < parents.length; i++) {
                    for (int j = i + 1; j < parents.length; j++) {
                        if (isChild((Node)parents[j], (Node)parents[i])) {
                            toRemove.add((Node)parents[i]);
                            continue;
                        }
                        if (isChild((Node)parents[i], (Node)parents[j])) {
                            toRemove.add((Node)parents[j]);
                            continue;
                        }
                    }
                }
                for (Node tr : toRemove) {
                    cn.getParents().remove(tr);
                    tr.getChildren().remove(cn);
                }
            }

            // 5. Connect bottom to new and affected concepts with no children
            for (IntIterator itr = allNew.keyIterator(); itr.hasNext();) {
                final int key = itr.next();
                Node cn = conceptNodeIndex.get(factory.lookupConceptId(key));
                if (cn.getChildren().isEmpty()) {
                    cn.getChildren().add(bottomNode);
                    bottomNode.getParents().add(cn);
                }
            }
            for (IntIterator itr = allAffected.keyIterator(); itr.hasNext();) {
                final int key = itr.next();
                Node cn = conceptNodeIndex.get(factory.lookupConceptId(key));
                if (cn.getChildren().isEmpty()) {
                    cn.getChildren().add(bottomNode);
                    bottomNode.getParents().add(cn);
                }
            }

            // 6. Connect the top node to new and affected concepts with no parents
            for (IntIterator itr = allNew.keyIterator(); itr.hasNext();) {
                final int key = itr.next();
                Node cn = conceptNodeIndex.get(factory.lookupConceptId(key));
                if (cn.getParents().isEmpty()) {
                    cn.getParents().add(topNode);
                    topNode.getChildren().add(cn);
                }
            }
            for (IntIterator itr = allAffected.keyIterator(); itr.hasNext();) {
                final int key = itr.next();
                Node cn = conceptNodeIndex.get(factory.lookupConceptId(key));
                if (cn.getParents().isEmpty()) {
                    cn.getParents().add(topNode);
                    topNode.getChildren().add(cn);
                }
            }
        } 
    }
View Full Code Here

        Queue<Node> toProcess = new LinkedList<Node>();
        toProcess.addAll(cn.getParents());

        while (!toProcess.isEmpty()) {
            Node tcn = toProcess.poll();
            if (tcn.equals(cn2))
                return true;
            Set<Node> parents = tcn.getParents();
            if (parents != null && !parents.isEmpty())
                toProcess.addAll(parents);
        }

        return false;
View Full Code Here

        this.nodeSet = nodeSet;
    }

    public void run() {
        while(true) {
            Node node = todo.poll();
            if(node == null) break;
            if(node.getEquivalentConcepts().contains(NamedConcept.BOTTOM)) continue;
           
            for (String c : node.getEquivalentConcepts()) {
                // Get direct super-concepts
                IConceptSet dc = direc.get(factory.getConcept(c));
                if (dc != null) {
                    for(IntIterator it = dc.iterator(); it.hasNext(); ) {
                        int d = it.next();
                        Node parent = conceptNodeIndex.get(
                                factory.lookupConceptId(d));
                        if (parent != null) {
                            node.getParents().add(parent);
                            parent.getChildren().add(node);
                            nodeSet.remove(parent);
                        }
                    }
                }
            }
View Full Code Here

        // and a CriticalDisease
        Ontology t = reasoner.getClassifiedOntology();
       
        // We use the same id that was used to create the concept to look for
        // the corresponding node in the taxonomy
        Node endocarditisNode = t.getNode("Endocarditis");
        System.out.println("Node for endocarditis:\n  "+
                endocarditisNode.getEquivalentConcepts());
       
       
        // We can now print the equivalent concepts in the node and the parent
        // nodes
        Set<Node> parentNodes = endocarditisNode.getParents();
        System.out.println("Parents of endocarditis:");
        for(Node parentNode : parentNodes) {
            System.out.println("  "+parentNode.getEquivalentConcepts());
        }
       
        // We can now add more axioms to the ontology and re-run the
        // classification
        Set<Axiom> additionalAxioms = new HashSet<Axiom>();
       
        Concept heartInflammation = f.createNamedConcept("HeartInflammation");
       
        lhs = heartInflammation;
        rhs = inflammation;
        additionalAxioms.add(f.createConceptInclusion(lhs, rhs));
       
        lhs = endocarditis;
        rhs = f.createConjunction(
                heartInflammation,
                f.createExistential(hasLoc, endocardium)
        );
        additionalAxioms.add(f.createConceptInclusion(lhs, rhs));
       
        // Subsequent invocations will trigger an incremental classification
        System.out.println("Running incremental classification:");
        reasoner.loadAxioms(additionalAxioms);
        reasoner.classify();
       
        // Now Endocarditis should be a HeartInflammation instead of an
        // Inflammation
        t = reasoner.getClassifiedOntology();
        endocarditisNode = t.getNode("Endocarditis");
        System.out.println("Node for endocarditis:\n  "+
                endocarditisNode.getEquivalentConcepts());

        parentNodes = endocarditisNode.getParents();
        System.out.println("Parents of endocarditis:");
        for(Node parentNode : parentNodes) {
            System.out.println("  "+parentNode.getEquivalentConcepts());
        }
    }
View Full Code Here

       
        // The taxonomy contains the inferred hierarchy
        Ontology t = reasoner.getClassifiedOntology();
       
        // We can look for nodes using the concept ids.
        Node newNode = t.getNode("pce_24220");
        System.out.println("Node for HISTORY_CARDIO_Standard_Non_Anginal_" +
            "Chest_Pain_Exertion:\n  "+
                newNode.getEquivalentConcepts());
       
       
        // We can now look for the parent and child nodes
        Set<Node> parentNodes = newNode.getParents();
        System.out.println("Parents:");
        for(Node parentNode : parentNodes) {
            System.out.println("  "+parentNode.getEquivalentConcepts());
        }
       
        Set<Node> childNodes = newNode.getChildren();
        System.out.println("Children:");
        for(Node childNode : childNodes) {
            System.out.println("  "+childNode.getEquivalentConcepts());
        }
View Full Code Here

        // 5. Retrieve taxonomy
        System.out.println("Retrieving taxonomy");
        Ontology ont = reasoner.getClassifiedOntology();
       
        // 6. Get node for new concept
        Node specialAppendicitisNode = ont.getNodeMap().get(appendicitsUuid);
       
        // 7. Print the new node
        Utils.printTaxonomy(
                specialAppendicitisNode.getParents().iterator().next(),
                ont.getBottomNode(),
                uuidToDescMap
        );
    }
View Full Code Here

    protected Concept getNecessary(IConceptMap<Context> contextIndex, Map<String, Node> taxonomy, int key) {
        final Object id = factory.lookupConceptId(key);
        final List<Concept> result = new ArrayList<Concept>();

        final Node node = taxonomy.get(id);
        if (node != null) {
            for (final Node parent: node.getParents()) {
                final String parentId = parent.getEquivalentConcepts().iterator().next();
                if (!NamedConcept.TOP.equals(parentId)) {      // Top is redundant
                    result.add(new NamedConcept(parentId));
                }
            }
View Full Code Here

        start = System.currentTimeMillis();
       
        // Part 2 - Creates a node per equivalent concepts
        conceptNodeIndex = new ConcurrentHashMap<String, Node>();
       
        Node top = null;
        Node bottom = null;
       
        IConceptSet processed = new FastConceptHashSet();
        Set<Node> nodeSet = new HashSet<Node>();
       
        for(int key : equiv.keySet()) {
            if(processed.contains(key)) continue;
            IConceptSet equivs = equiv.get(key);
            processed.addAll(equivs);
           
            Node n = new Node();
            for(IntIterator it = equivs.iterator(); it.hasNext(); ) {
                int val = it.next();
                String tval = factory.lookupConceptId(val).toString();
                n.getEquivalentConcepts().add(tval);
                conceptNodeIndex.put(tval, n);

                if (val == CoreFactory.TOP_CONCEPT)
                    top = n;
                if (val == CoreFactory.BOTTOM_CONCEPT)
                    bottom = n;
            }
            nodeSet.add(n);
        }
           
        if(top == null) {
            top = new Node();
            top.getEquivalentConcepts().add(au.csiro.ontology.model.NamedConcept.TOP);
        }
       
        if(bottom == null) {
            bottom = new Node();
            bottom.getEquivalentConcepts().add(au.csiro.ontology.model.NamedConcept.BOTTOM);
        }
       
        Statistics.INSTANCE.setTime("taxonomy 2",
                System.currentTimeMillis() - start);
        start = System.currentTimeMillis();
       
        // Step 3 - Connects nodes
        Queue<Node> todo2 = new ConcurrentLinkedQueue<Node>(nodeSet);
        executor = Executors.newFixedThreadPool(numThreads);
        for (int j = 0; j < numThreads; j++) {
            Runnable worker = new TaxonomyWorker2(factory,
                    conceptNodeIndex, direc, todo2, nodeSet);
            executor.execute(worker);
        }

        executor.shutdown();
        while (!executor.isTerminated()) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        assert (todo2.isEmpty());
       
        Statistics.INSTANCE.setTime("taxonomy 3",
                System.currentTimeMillis() - start);
        start = System.currentTimeMillis();
       
        // Connect bottom
        nodeSet.remove(bottom);
        bottom.getParents().addAll(nodeSet);
        for(Node n : nodeSet) {
            n.getChildren().add(bottom);
        }
       
        Statistics.INSTANCE.setTime("taxonomy connect bottom",
                System.currentTimeMillis() - start);
        start = System.currentTimeMillis();
       
        // Connect top
        for (String key : conceptNodeIndex.keySet()) {
            if (key.equals(au.csiro.ontology.model.NamedConcept.TOP) ||
                    key.equals(au.csiro.ontology.model.NamedConcept.BOTTOM))
                continue;
            Node node = conceptNodeIndex.get(key);
            if (node.getParents().isEmpty()) {
                node.getParents().add(top);
                top.getChildren().add(node);
            }
        }

        // TODO: deal with special case where only top and bottom are present.
View Full Code Here

TOP

Related Classes of au.csiro.ontology.Node

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.