All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uk.ac.manchester.cs.owl.explanation.ordering.MutableTree Maven / Gradle / Ivy

There is a newer version: 5.5.0
Show newest version
/* This file is part of the OWL API.
 * The contents of this file are subject to the LGPL License, Version 3.0.
 * Copyright 2014, The University of Manchester
 * 
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with this program.  If not, see http://www.gnu.org/licenses/.
 *
 * Alternatively, the contents of this file may be used under the terms of the Apache License, Version 2.0 in which case, the provisions of the Apache License Version 2.0 are applicable instead of those above.
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */
package uk.ac.manchester.cs.owl.explanation.ordering;

import static org.semanticweb.owlapi.util.OWLAPIPreconditions.verifyNotNull;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;

/**
 * @param  type of elements
 * @author Matthew Horridge, The University Of Manchester, Bio-Health Informatics Group
 * @since 2.2.0
 */
public class MutableTree implements Tree {

    @Nullable
    private final N userObject;
    private final List> children;
    private final Map, Object> child2EdgeMap;
    @Nullable
    private MutableTree parent;
    private NodeRenderer toStringRenderer;

    /**
     * @param userObject the user object
     */
    public MutableTree(N userObject) {
        this.userObject = userObject;
        children = new ArrayList<>();
        child2EdgeMap = new HashMap<>();
        toStringRenderer = Object::toString;
    }

    @Override
    @Nullable
    public N getUserObject() {
        return userObject;
    }

    /**
     * @param child child to add
     */
    public void addChild(MutableTree child) {
        children.add(child);
        child.parent = this;
    }

    /**
     * @param child child to add
     * @param edge the edge
     */
    public void addChild(MutableTree child, Object edge) {
        addChild(child);
        child2EdgeMap.put(child, edge);
    }

    /**
     * @param child child to remove
     */
    public void removeChild(MutableTree child) {
        children.remove(child);
        child.parent = null;
    }

    @Override
    @Nullable
    public Object getEdge(Tree child) {
        return child2EdgeMap.get(child);
    }

    @Override
    public void sortChildren(Comparator> comparator) {
        Collections.sort(children, comparator);
    }

    /**
     * remove all children.
     */
    public void clearChildren() {
        children.forEach(c -> c.parent = null);
        children.clear();
    }

    @Override
    @Nullable
    public Tree getParent() {
        return parent;
    }

    /**
     * @param parent the new parent
     */
    public void setParent(MutableTree parent) {
        if (this.parent != null) {
            this.parent.children.remove(this);
        }
        this.parent = parent;
        this.parent.children.add(this);
    }

    @Override
    public List> getChildren() {
        return new ArrayList<>(children);
    }

    @Override
    public int getChildCount() {
        return children.size();
    }

    @Override
    public boolean isRoot() {
        return parent == null;
    }

    @Override
    public boolean isLeaf() {
        return children.isEmpty();
    }

    @Override
    public Tree getRoot() {
        if (parent == null) {
            return this;
        }
        return verifyNotNull(parent).getRoot();
    }

    @Override
    public List> getPathToRoot() {
        List> path = new ArrayList<>();
        path.add(0, this);
        Tree par = parent;
        while (par != null) {
            path.add(0, par);
            par = par.getParent();
        }
        return path;
    }

    @Override
    public List getUserObjectPathToRoot() {
        List path = new ArrayList<>();
        path.add(0, this.getUserObject());
        Tree par = parent;
        while (par != null) {
            path.add(0, par.getUserObject());
            par = par.getParent();
        }
        return path;
    }

    @Override
    public Set getUserObjectClosure() {
        Set objects = new HashSet<>();
        getUserObjectClosure(this, objects);
        return objects;
    }

    private void getUserObjectClosure(Tree tree, Set bin) {
        bin.add(tree.getUserObject());
        tree.getChildren().forEach(c -> getUserObjectClosure(c, bin));
    }

    @Override
    public void dump(PrintWriter writer) {
        dump(writer, 0);
    }

    @Override
    public void dump(PrintWriter writer, int indent) {
        int depth = getPathToRoot().size();
        char[] chars = new char[depth + indent];
        Arrays.fill(chars, '\t');
        String tabs = new String(chars);
        writer.print(tabs);
        String ren = toStringRenderer.render(this);
        ren = ren.replace("\n", "\n" + tabs);
        writer.println(ren);
        for (Tree child : getChildren()) {
            Object edge = getEdge(child);
            if (edge != null) {
                writer.print("\t--- ");
                writer.print(edge);
                writer.print(" ---\n\n");
            }
            child.dump(writer, indent);
        }
        writer.flush();
    }

    @Override
    public void setNodeRenderer(NodeRenderer renderer) {
        this.toStringRenderer = renderer;
        children.forEach(c -> c.setNodeRenderer(toStringRenderer));
    }

    @Override
    public List fillDepthFirst() {
        List results = new ArrayList<>();
        fillDepthFirst(this, results);
        return results;
    }

    private void fillDepthFirst(Tree tree, List bin) {
        bin.add(tree.getUserObject());
        tree.getChildren().forEach(c -> fillDepthFirst(c, bin));
    }

    /**
     * @param tree the node to put in place of this one
     */
    public void replace(MutableTree tree) {
        verifyNotNull(parent, "parent cannot be null here").children.remove(this);
        verifyNotNull(parent, "parent cannot be null here").children.add(tree);
        parent = null;
        tree.children.clear();
        tree.children.addAll(children);
        children.clear();
    }

    @Override
    public String toString() {
        if (userObject != null) {
            return userObject.toString();
        }
        return "";
    }

    /**
     * @return the size
     */
    public int getSize() {
        return getUserObjectClosure().size();
    }

    /**
     * @return the max depth
     */
    public int getMaxDepth() {
        return getMaxDepth(this);
    }

    private int getMaxDepth(Tree tree) {
        int maxChildDepth = tree.getPathToRoot().size();
        return tree.getChildren().stream().mapToInt(this::getMaxDepth).max().orElse(maxChildDepth);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy