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

org.semanticweb.elk.reasoner.saturation.classes.RuleRoleComposition Maven / Gradle / Ivy

/*
 * #%L
 * ELK Reasoner
 * 
 * $Id$
 * $HeadURL$
 * %%
 * Copyright (C) 2011 - 2012 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.saturation.classes;

import java.util.Collection;

import org.semanticweb.elk.reasoner.indexing.hierarchy.IndexedPropertyChain;
import org.semanticweb.elk.reasoner.saturation.rulesystem.InferenceRule;
import org.semanticweb.elk.reasoner.saturation.rulesystem.RuleApplicationFactory;
import org.semanticweb.elk.util.collections.LazySetIntersection;
import org.semanticweb.elk.util.collections.Multimap;

/**
 * TODO: documentation
 * 
 * @author Frantisek Simancik
 * 
 * @param 
 *            the type of contexts that can be used with this inference rule
 */
public class RuleRoleComposition extends
		RuleWithBackwardLinks implements InferenceRule {

	public void apply(BackwardLink argument, C context,
			RuleApplicationFactory.Engine engine) {

		final IndexedPropertyChain linkRelation = argument.getRelation();
		final C target = argument.getTarget();

		/*
		 * if composeBackwardLinks, then add a forward copy of the link to
		 * consider the link in property compositions
		 */
		if (context.composeBackwardLinks
				&& linkRelation.getSaturated()
						.getCompositionsByLeftSubProperty() != null)
			engine.enqueue(target, new ForwardLink(linkRelation, context));

		/* compose the link with all forward links */
		final Multimap comps = linkRelation
				.getSaturated().getCompositionsByRightSubProperty();
		final Multimap forwLinks = context.forwardLinksByObjectProperty;

		if (comps != null && forwLinks != null) {
			for (IndexedPropertyChain forwardRelation : new LazySetIntersection(
					comps.keySet(), forwLinks.keySet())) {

				Collection compositions = comps
						.get(forwardRelation);
				Collection forwardTargets = forwLinks
						.get(forwardRelation);

				for (IndexedPropertyChain composition : compositions)
					for (ContextElClassSaturation forwardTarget : forwardTargets)
						engine.enqueue(forwardTarget, new BackwardLink(
								composition, target));
			}
		}

	}

	public void apply(ForwardLink argument, C context,
			RuleApplicationFactory.Engine engine) {

		// start deriving backward links for composition
		initializeCompositionOfBackwardLinks(context, engine);

		final IndexedPropertyChain linkRelation = argument.getRelation();
		final C target = argument.getTarget();

		/* compose the link with all backward links */
		final Multimap comps = linkRelation
				.getSaturated().getCompositionsByLeftSubProperty();
		final Multimap backLinks = context.backwardLinksByObjectProperty;

		// assert comps != null
		if (backLinks != null) {
			for (IndexedPropertyChain backwardRelation : new LazySetIntersection(
					comps.keySet(), backLinks.keySet())) {

				Collection compositions = comps
						.get(backwardRelation);
				Collection backwardTargets = backLinks
						.get(backwardRelation);

				for (IndexedPropertyChain composition : compositions)
					for (ContextElClassSaturation backwardTarget : backwardTargets)
						engine.enqueue(target,
								new BackwardLink(
										composition, backwardTarget));
			}
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy