    001    package com.hammurapi.reasoning.impl;
    002
    003    import java.lang.reflect.Method;
    004    import java.util.Collection;
    005    import java.util.Map;
    006
    007    import com.hammurapi.reasoning.ReasoningException;
    008    import com.hammurapi.reasoning.spi.InferenceContext;
    009
    010    /**
    011     * Delegates calls to thread local
    012     * @author Pavel Vlasov
    013     *
    014     */
    015    public class InferenceContextDispatcher implements InferenceContext {
    016
    017            private static ThreadLocal<InferenceContext> ictl = new ThreadLocal<InferenceContext>();
    018
    019            static void setThreadInferenceContext(InferenceContext ic) {
    020                    ictl.set(ic);
    021            }
    022
    023            static void unsetThreadInferenceContext() {
    024                    ictl.set(null);
    025            }
    026
    027            private InferenceContext getDelegate() {
    028                    InferenceContext delegate = ictl.get();
    029                    if (delegate==null) {
    030                            throw new IllegalStateException("No inference context is set for current thread.");
    031                    }
    032                    return delegate;
    033            }
    034
    035            public void addUndo(Runnable undoCommand) {
    036                    getDelegate().addUndo(undoCommand);
    037            }
    038
    039            public boolean consumeInput(int position, boolean local) {
    040                    return getDelegate().consumeInput(position, local);
    041            }
    042
    043            public boolean contains(Object fact) throws ReasoningException {
    044                    return getDelegate().contains(fact);
    045            }
    046
    047            public Collection<?> getObjects() throws ReasoningException {
    048                    return getDelegate().getObjects();
    049            }
    050
    051            public Map<String, ?> getProperties() throws ReasoningException {
    052                    return getDelegate().getProperties();
    053            }
    054
    055            public int[] parameterIndices() {
    056                    return getDelegate().parameterIndices();
    057            }
    058
    059            public void post(Object... conclusions) {
    060                    getDelegate().post(conclusions);
    061            }
    062
    063            public void remove(Object fact) throws ReasoningException {
    064                    getDelegate().remove(fact);
    065            }
    066
    067            public Method targetMethod() {
    068                    return getDelegate().targetMethod();
    069            }
    070
    071            @Override
    072            public void put(Object fact) throws ReasoningException {
    073                    getDelegate().put(fact);
    074            }
    075
    076            @Override
    077            public void update(Object fact) throws ReasoningException {
    078                    getDelegate().update(fact);
    079            }
    080
    081            @Override
    082            public void update(Object originalFact, Object newFact) throws ReasoningException {
    083                    getDelegate().update(originalFact, newFact);
    084            }
    085    }