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

org.semanticweb.elk.reasoner.taxonomy.TaxonomyNodeUtils Maven / Gradle / Ivy

The newest version!
/*
 * #%L
 * ELK Reasoner
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2011 - 2013 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;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

import org.semanticweb.elk.owl.interfaces.ElkEntity;
import org.semanticweb.elk.reasoner.taxonomy.model.GenericInstanceNode;
import org.semanticweb.elk.reasoner.taxonomy.model.GenericTaxonomyNode;
import org.semanticweb.elk.reasoner.taxonomy.model.GenericTypeNode;
import org.semanticweb.elk.util.collections.ArrayHashSet;
import org.semanticweb.elk.util.collections.Operations;
import org.semanticweb.elk.util.collections.Operations.Functor;

/**
 * A collection of utility methods, mostly for the frequent use case of
 * recursive traversal
 * 
 * @author Pavel Klinov
 * 
 *         [email protected]
 * @author Peter Skocovsky
 */
public class TaxonomyNodeUtils {
	
	public static  Set getAllReachable(
					final Collection direct,
					final Functor> succ) {
		
		final Set result = new ArrayHashSet(direct.size());
		result.addAll(direct);
		final Queue todo = new LinkedList(direct);
		
		while (!todo.isEmpty()) {
			final N next = todo.poll();
			
			for (final N succNode : succ.apply(next)) {
				if (result.add(succNode)) {
					todo.add(succNode);
				}
			}
		}
		
		return Collections.unmodifiableSet(result);
	}
	
	public static  Set collectFromAllReachable(
					final Collection direct,
					final Collection init,
					final Functor> succ,
					final Functor> collect) {
		
		final Set result = new ArrayHashSet();
		result.addAll(init);
		final Set queued = new ArrayHashSet();
		queued.addAll(direct);
		final Queue todo = new LinkedList(direct);
		
		while (!todo.isEmpty()) {
			final N next = todo.poll();
			
			result.addAll(collect.apply(next));
			
			for (final N succNode : succ.apply(next)) {
				if (queued.add(succNode)) {
					todo.add(succNode);
				}
			}
		}
		
		return Collections.unmodifiableSet(result);
	}
	
	/**
	 * Returns all super-nodes of a node whose direct super-nodes are in
	 * direct.
	 * 
	 * @param 
	 *            The type of nodes
	 * @param 
	 *            The type of members of the nodes. 
	 * @param direct
	 *            The direct super-nodes.
	 * @return all super-nodes of a node whose direct super-nodes are
	 *         direct.
	 */
	public static >
			Set getAllSuperNodes(final Collection direct) {
		return getAllReachable(direct, new Functor>() {

			@Override
			public Set apply(final N node) {
				return node.getDirectSuperNodes();
			}});
	}
	
	/**
	 * Returns all sub-nodes of a node whose direct sub-nodes are
	 * direct.
	 * 
	 * @param 
	 *            The type of nodes
	 * @param 
	 *            The type of members of the nodes.
	 * @param direct
	 *            The direct sub-nodes.
	 * @return all sub-nodes of a node whose direct sub-nodes are
	 *         direct.
	 */
	public static >
			Set getAllSubNodes(final Collection direct) {
		return getAllReachable(direct, new Functor>() {

			@Override
			public Set apply(final N node) {
				return node.getDirectSubNodes();
			}});
	}
	
	/**
	 * Returns all instance nodes of the specified type node and all its
	 * sub-nodes.
	 *
	 * @param 
	 *            The type of type nodes
	 * @param 
	 *            The type of instance nodes
	 * @param 
	 *            The type of members of the type nodes.
	 * @param 
	 *            The type of members of the instance nodes.
	 * @param node
	 *            The type node whose instances should be returned.
	 * @return all instance nodes of the specified type node and all its
	 *         sub-nodes.
	 */
	public static , IN extends GenericInstanceNode>
			Set getAllInstanceNodes(final GenericTypeNode node) {
		return TaxonomyNodeUtils.collectFromAllReachable(
				node.getDirectSubNodes(),
				node.getDirectInstanceNodes(),
				new Operations.Functor, Set>>() {
					@Override
					public Set apply(final GenericTypeNode n) {
						return n.getDirectSubNodes();
					}
				},
				new Operations.Functor, Set>() {
					@Override
					public Set apply(final GenericTypeNode n) {
						return n.getDirectInstanceNodes();
					}
				});
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy