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

org.drools.core.util.AbstractCodedHierarchyImpl Maven / Gradle / Ivy

There is a newer version: 9.44.0.Final
Show newest version
/*
 * Copyright 2015 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * 
 *      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 org.drools.core.util;

import java.io.Externalizable;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public abstract class AbstractCodedHierarchyImpl extends AbstractBitwiseHierarchyImpl> implements CodedHierarchy, Externalizable {

    protected abstract HierNode getNode( T name );


    public void addMember( T val, BitSet key ) {
        if ( hasKey( key ) ) {
            HierNode node = line.get( key );
            node.setValue( val );
        } else {
            HierNode node = new HierNode( val, key );
            Collection> infs = gcsBorderNodes( key, false );
            Collection> sups = lcsBorderNodes( key, false );

            for ( HierNode child : infs ) {
                if ( child != null ) {
                    child.getParents().add( node );
                    child.getParents().removeAll( sups );
                    node.getChildren().add( child );
                }
            }
            for ( HierNode parent : sups ) {
                if ( parent != null ) {
                    parent.getChildren().add( node );
                    parent.getChildren().removeAll( infs );
                    node.getParents().add( parent );
                }
            }
            add( node );
//            System.out.println( " Added inst node " + node );
//            System.out.println( " \t parents " + parents( key ) );
//            System.out.println( " \t children " + children( key ) );
        }
    }



    public void removeMember( T val ) {
        if ( val == null ) {
            return;
        }
        BitSet key = getCode( val );
        removeMember( key );
    }

    public void removeMember( BitSet key ) {
        if ( ! hasKey( key ) ) {
            return;
        } else {
            HierNode node = getNodeByKey( key );
            Collection> children = node.getChildren();
            Collection> parents = node.getParents();

            for ( HierNode child : children ) {
                child.getParents().remove( node );
                child.getParents().addAll( parents );
            }
            for ( HierNode parent : parents ) {
                parent.getChildren().remove( node );
                parent.getChildren().addAll( children );
            }
            remove( node );
        }
    }


    protected Collection parentValues( HierNode node ) {
        if ( node == null ) {
            return Collections.EMPTY_LIST;
        }
        List p = new ArrayList( node.getParents().size() );
        for ( HierNode parent : node.getParents() ) {
            p.add( parent.getValue() );
        }
        return p;
    }


    public Collection ancestors( T x ) {
        HierNode node = getNode(x);
        return ancestorValues( node );
    }

    public Collection ancestors( BitSet key ) {
        HierNode node = getNodeByKey(key);
        return ancestorValues( node );
    }

    protected Collection ancestorValues( HierNode node ) {
        if ( node == null ) {
            return Collections.EMPTY_SET;
        }

        Set ancestors = new HashSet();

        Collection> parents = node.getParents();
        for ( HierNode p : parents ) {
            ancestors.add( p.getValue() );
            ancestors.addAll( ancestors(p.getValue()) );
        }
        return ancestors;
    }

    protected Set> ancestorNodes( HierNode x ) {
        Set> ancestors = new HashSet>();

        Collection> parents = x.getParents();
        ancestors.addAll( parents );
        for ( HierNode p : parents ) {
            ancestors.addAll( ancestorNodes( p ) );
        }
        return ancestors;
    }








    protected Collection childrenValues( HierNode node ) {
        if ( node == null ) {
            return Collections.EMPTY_LIST;
        }
        List c = new ArrayList( node.getChildren().size() );
        for ( HierNode child : node.getChildren() ) {
            c.add( child.getValue() );
        }
        return c;
    }

    public Collection children( T x ) {
        HierNode node = getNode(x);
        return childrenValues( node );
    }

    public Collection children( BitSet key ) {
        HierNode node = getNodeByKey(key);
        return childrenValues( node );
    }



    protected Collection descendantValues( HierNode node ) {
        if ( node == null ) {
            return Collections.EMPTY_SET;
        }
        Set descendants = new HashSet();
        descendants.add( node.getValue() );

        Collection> children = node.getChildren();

        for ( HierNode c : children ) {
            descendants.add( c.getValue() );
            descendants.addAll(descendants(c.getValue()));
        }
        return descendants;
    }


    public Collection descendants( T y ) {
        HierNode node = getNode(y);
        return descendantValues( node );
    }

    public Collection descendants( BitSet key ) {
        HierNode node = getNodeByKey(key);
        return descendantValues( node );
    }

    protected Set> descendantNodes( HierNode y ) {
        Set> descendants = new HashSet>();
        descendants.add( y );

        Collection> children = y.getChildren();
        descendants.addAll( children );
        for ( HierNode c : children ) {
            descendants.addAll( descendantNodes( c ) );
        }
        return descendants;
    }




}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy