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

org.semanticweb.elk.reasoner.taxonomy.impl.NonBottomGenericTaxonomyNode Maven / Gradle / Ivy

The newest version!
/*
 * #%L
 * ELK Reasoner
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2011 - 2016 Department of Computer Science, University of Oxford
 * %%
 * 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.
 * #L%
 */
package org.semanticweb.elk.reasoner.taxonomy.impl;

import java.util.Collections;
import java.util.Set;

import org.semanticweb.elk.owl.interfaces.ElkEntity;
import org.semanticweb.elk.reasoner.taxonomy.TaxonomyNodeUtils;
import org.semanticweb.elk.reasoner.taxonomy.model.GenericTaxonomyNode;
import org.semanticweb.elk.reasoner.taxonomy.model.Taxonomy;
import org.semanticweb.elk.util.collections.ArrayHashSet;
import org.semanticweb.elk.util.hashing.HashGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A generic implementation of a mutable non-bottom node of an
 * {@link AbstractDistinctBottomTaxonomy}.
 * 
 * @author Peter Skocovsky
 *
 * @param 
 *            The type of members of this nodes.
 * @param 
 *            The immutable type of nodes with which this node may be
 *            associated.
 * @param 
 *            The mutable type of nodes with which this node may be associated.
 */
public abstract class NonBottomGenericTaxonomyNode<
				T extends ElkEntity,
				N extends GenericTaxonomyNode,
				UN extends UpdateableTaxonomyNode
		>
		extends SimpleUpdateableNode
		implements GenericTaxonomyNode,
		UpdateableTaxonomyNode {
	
	private static final Logger LOGGER_ = LoggerFactory
			.getLogger(NonBottomGenericTaxonomyNode.class);

	/** The taxonomy of this node. */
	protected final AbstractDistinctBottomTaxonomy taxonomy_;
	
	/**
	 * ElkClass nodes whose members are direct super-classes of the members of
	 * this node.
	 */
	protected final Set directSuperNodes_;
	/**
	 * ElkClass nodes, except for the bottom node, whose members are direct
	 * sub-classes of the members of this node.
	 */
	protected final Set directSubNodes_;
	
	/**
	 * Constructs the node for the supplied equivalent members.
	 * 
	 * @param taxonomy
	 *            The taxonomy to which this node belongs.
	 * @param members
	 *            Non-empty sequence of equivalent members.
	 * @param size
	 *            The number of equivalent members.
	 */
	public NonBottomGenericTaxonomyNode(
			final AbstractDistinctBottomTaxonomy taxonomy,
			final Iterable members, final int size) {
		super(members, size, taxonomy.getKeyProvider());
		this.taxonomy_ = taxonomy;
		this.directSubNodes_ = new ArrayHashSet();
		this.directSuperNodes_ = new ArrayHashSet();
	}

	@Override
	public Set getDirectSuperNodes() {
		return Collections.unmodifiableSet(taxonomy_.toTaxonomyNodes(
				directSuperNodes_));
	}

	@Override
	public Set getDirectNonBottomSuperNodes() {
		return Collections.unmodifiableSet(directSuperNodes_);
	}
	
	@Override
	public Set getAllSuperNodes() {
		return TaxonomyNodeUtils.getAllSuperNodes(getDirectSuperNodes());
	}

	@Override
	public Set getDirectSubNodes() {
		if (!directSubNodes_.isEmpty()) {
			return Collections.unmodifiableSet(taxonomy_.toTaxonomyNodes(
					directSubNodes_));
		}
		// else
		return Collections.singleton(taxonomy_.getBottomNode());
	}

	@Override
	public Set getDirectNonBottomSubNodes() {
		return Collections.unmodifiableSet(directSubNodes_);
	}
	
	@Override
	public Set getAllSubNodes() {
		return TaxonomyNodeUtils.getAllSubNodes(getDirectSubNodes());
	}

	@Override
	public Taxonomy getTaxonomy() {
		return taxonomy_;
	}

	@Override
	public synchronized void addDirectSuperNode(final UN superNode) {
		LOGGER_.trace("{}: new direct super-node {}", this, superNode);

		directSuperNodes_.add(superNode);
	}

	@Override
	public synchronized void addDirectSubNode(final UN subNode) {
		LOGGER_.trace("{}: new direct sub-node {}", this, subNode);

		if (directSubNodes_.isEmpty()) {
			taxonomy_.countNodesWithSubClasses_.incrementAndGet();
		}

		directSubNodes_.add(subNode);
	}

	@Override
	public synchronized boolean removeDirectSubNode(final UN subNode) {
		boolean changed = directSubNodes_.remove(subNode);

		if (changed)
			LOGGER_.trace("{}: removed direct sub-node {}", this, subNode);

		if (directSubNodes_.isEmpty()) {
			taxonomy_.countNodesWithSubClasses_.decrementAndGet();
		}

		return changed;
	}

	@Override
	public synchronized boolean removeDirectSuperNode(final UN superNode) {
		boolean changed = directSuperNodes_.remove(superNode);

		LOGGER_.trace("{}: removed direct super-node {}", this, superNode);

		return changed;
	}

	private final int hashCode_ = HashGenerator.generateNextHashCode();

	@Override
	public final int hashCode() {
		return hashCode_;
	}

	/**
	 * A subclass with fixed type parameters.
	 * 
	 * @author Peter Skocovsky
	 *
	 * @param 
	 *            The type of members of this nodes.
	 */
	public static class Projection
			extends NonBottomGenericTaxonomyNode, Projection>
			implements GenericTaxonomyNode.Projection {

		/**
		 * Constructs the node for the supplied equivalent members.
		 * 
		 * @param taxonomy
		 *            The taxonomy to which this node belongs.
		 * @param members
		 *            Non-empty sequence of equivalent members.
		 * @param size
		 *            The number of equivalent members.
		 */
		public Projection(
				final AbstractDistinctBottomTaxonomy<
						T,
						GenericTaxonomyNode.Projection,
						NonBottomGenericTaxonomyNode.Projection
				> taxonomy,
				final Iterable members, final int size) {
			super(taxonomy, members, size);
		}
		
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy