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

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

There is a newer version: 0.4.3
Show newest version
/*
 * #%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 org.semanticweb.elk.reasoner.saturation.rulesystem.Context;
import org.semanticweb.elk.reasoner.saturation.rulesystem.IllegalInferenceMethodException;
import org.semanticweb.elk.reasoner.saturation.rulesystem.InferenceRule;
import org.semanticweb.elk.reasoner.saturation.rulesystem.InferenceSystem;
import org.semanticweb.elk.reasoner.saturation.rulesystem.InferenceSystemInvocationManager;
import org.semanticweb.elk.reasoner.saturation.rulesystem.Queueable;
import org.semanticweb.elk.reasoner.saturation.rulesystem.RuleApplicationFactory;

/**
 * An optimized implementation of the InferenceSystemInvocationManager that does
 * not use Java reflection for inference rules that have a
 * {@link SuperClassExpression} as one of the arguments. Instead, all methods
 * applicable to negative and positive {@link SuperClassExpression}s are stored
 * in separate fields.
 * 
 * @author Frantisek Simancik
 * 
 * @param 
 *            the type of contexts that can be used with this invocation manager
 */
public class InferenceSystemInvocationManagerSCE
		extends InferenceSystemInvocationManager {

	public InferenceSystemInvocationManagerSCE() {
	}

	protected class RuleMethodListNegSCE {
		InferenceRuleNegSCE firstInferenceRule;
		RuleMethodListNegSCE rest;

		public RuleMethodListNegSCE(InferenceRuleNegSCE firstInferenceRule,
				RuleMethodListNegSCE rest) {
			this.firstInferenceRule = firstInferenceRule;
			this.rest = rest;
		}

		public RuleMethodListNegSCE(
				final InferenceRuleSCE firstInferenceRule,
				RuleMethodListNegSCE rest) {
			this.firstInferenceRule = new InferenceRuleNegSCE() {

				@Override
				public void applySCE(NegativeSuperClassExpression argument,
						C context, RuleApplicationFactory.Engine engine) {
					firstInferenceRule.applySCE(argument, context, engine);
				}
			};
			this.rest = rest;
		}

		public void invoke(NegativeSuperClassExpression argument, C context,
				RuleApplicationFactory.Engine engine) {
			firstInferenceRule.applySCE(argument, context, engine);
			if (rest != null) {
				rest.invoke(argument, context, engine);
			}
		}
	}

	protected class RuleMethodListPosSCE {
		InferenceRulePosSCE firstInferenceRule;
		RuleMethodListPosSCE rest;

		public RuleMethodListPosSCE(InferenceRulePosSCE firstInferenceRule,
				RuleMethodListPosSCE rest) {
			this.firstInferenceRule = firstInferenceRule;
			this.rest = rest;
		}

		public RuleMethodListPosSCE(
				final InferenceRuleSCE firstInferenceRule,
				RuleMethodListPosSCE rest) {
			this.firstInferenceRule = new InferenceRulePosSCE() {

				@Override
				public void applySCE(PositiveSuperClassExpression argument,
						C context, RuleApplicationFactory.Engine engine) {
					firstInferenceRule.applySCE(argument, context, engine);
				}
			};
			this.rest = rest;
		}

		public void invoke(PositiveSuperClassExpression argument, C context,
				RuleApplicationFactory.Engine engine) {
			firstInferenceRule.applySCE(argument, context, engine);
			if (rest != null) {
				rest.invoke(argument, context, engine);
			}
		}
	}

	protected RuleMethodListNegSCE rulesNegSCE = null;
	protected RuleMethodListPosSCE rulesPosSCE = null;

	protected void addInferenceRuleNegSCE(InferenceRuleNegSCE inferenceRule) {
		rulesNegSCE = new RuleMethodListNegSCE(inferenceRule, rulesNegSCE);
	}

	protected void addInferenceRulePosSCE(InferenceRulePosSCE inferenceRule) {
		rulesPosSCE = new RuleMethodListPosSCE(inferenceRule, rulesPosSCE);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void addInferenceSystem(InferenceSystem inferenceSystem)
			throws IllegalInferenceMethodException, NoSuchMethodException {
		for (InferenceRule inferenceRule : inferenceSystem
				.getInferenceRules()) {
			addInferenceRule(inferenceRule);

			if (inferenceRule instanceof InferenceRuleNegSCE)
				rulesNegSCE = new RuleMethodListNegSCE(
						(InferenceRuleNegSCE) inferenceRule, rulesNegSCE);

			if (inferenceRule instanceof InferenceRulePosSCE)
				rulesPosSCE = new RuleMethodListPosSCE(
						(InferenceRulePosSCE) inferenceRule, rulesPosSCE);

			if (inferenceRule instanceof InferenceRuleSCE) {
				rulesNegSCE = new RuleMethodListNegSCE(
						(InferenceRuleSCE) inferenceRule, rulesNegSCE);
				rulesPosSCE = new RuleMethodListPosSCE(
						(InferenceRuleSCE) inferenceRule, rulesPosSCE);
			}
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void applyAdditionalMethodsToItem(Queueable queueable,
			Context context, RuleApplicationFactory.Engine engine) {
		if (rulesNegSCE != null
				&& queueable instanceof NegativeSuperClassExpression)
			rulesNegSCE.invoke((NegativeSuperClassExpression) queueable,
					(C) context, engine);
		if (rulesPosSCE != null
				&& queueable instanceof PositiveSuperClassExpression)
			rulesPosSCE.invoke((PositiveSuperClassExpression) queueable,
					(C) context, engine);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy