com.hammurapi.reasoning.impl
Class InMemoryKnowledgeBase<F>

java.lang.Object
  extended by com.hammurapi.reasoning.impl.InMemoryKnowledgeBase<F>
All Implemented Interfaces:
ForwardReasoningSession<F>, CollectionManager<Object>, KnowledgeBase<F>, ReasoningSession

public class InMemoryKnowledgeBase<F>
extends Object
implements KnowledgeBase<F>

Knowledge base which uses in-memory collecitons.

Author:
Pavel Vlasov

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.hammurapi.reasoning.impl.KnowledgeBase
KnowledgeBase.PutDerivedResult<F>
 
Constructor Summary
InMemoryKnowledgeBase(String name, String description, boolean weak)
          Creates a new simple knowledge base.
 
Method Summary
 void cleanup()
          Removes invalid entries.
 void close()
           
 boolean contains(Handle<F> handle)
           
 boolean contains(Object fact)
           
 void executeRules()
           
 F get(Handle<F> handle)
           
 Map<Class<F>,Set<Class<F>>> getConclusionMap()
           
 Collection<Derivation<F>> getDerivations(F obj)
           
 Collection<Derivation<F>> getDerivations(Handle<F> handle)
           
 String getDescription()
           
 ExceptionHandler getExceptionHandler()
           
 Executor getExecutor()
           
 Handle<F> getHandle(Object fact)
           
 Collection<Handle<F>> getHandles()
           
 InferenceListener<F> getInferenceListener()
           
<T> List<T>
getList(String id, Class<T> elementType)
          Lists of handles are array lists, for other element types lists are facades for handle lists.
 String getName()
           
 Collection<F> getObjects()
           
<T> Set<? extends T>
getSet(String id, Class<T> elementType)
          Sets of handles are hash sets, for other element types sets are facades for handle sets.
 Handle<F> put(F fact)
           
 Handle<F>[] putAll(F... facts)
           
 List<Handle<F>> putAll(List<? extends F> facts)
           
 KnowledgeBase.PutDerivedResult<F> putConclusion(F conclusion, Object rule, String ruleName, String ruleDescription, Method method, List<Handle<F>> handleList)
           
 void remove(F obj, boolean strict)
           
 void remove(Handle<F> handle, boolean strict)
           
 void reset()
           
 void setExceptionHandler(ExceptionHandler handler)
           
 void setExecutor(Executor executor)
           
 void setInferenceListener(InferenceListener<F> listener)
           
 void updateObject(Handle<F> handle, F fact)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

InMemoryKnowledgeBase

public InMemoryKnowledgeBase(String name,
                             String description,
                             boolean weak)
Creates a new simple knowledge base.

Parameters:
weak - If true, a weak hash map is used for fact storage. It can be useful for long running sessions with inference listener. Use of weak hash map ensures that the session will not run out of memory. When a weak hash map is used, some derivations may become invalid when source facts of conclusions get garbage collected.
Method Detail

setExecutor

public void setExecutor(Executor executor)
Parameters:
executor - Executor for delegation of execution of undo tasks.

getExecutor

public Executor getExecutor()

getList

public <T> List<T> getList(String id,
                           Class<T> elementType)
Lists of handles are array lists, for other element types lists are facades for handle lists.

Specified by:
getList in interface CollectionManager<Object>
Parameters:
id - Set id.
elementType - Element type.
Returns:

getSet

public <T> Set<? extends T> getSet(String id,
                                   Class<T> elementType)
Sets of handles are hash sets, for other element types sets are facades for handle sets.

Specified by:
getSet in interface CollectionManager<Object>
Parameters:
id - Set id.
elementType - Element type.
Returns:

put

public Handle<F> put(F fact)
              throws ReasoningException
Specified by:
put in interface ForwardReasoningSession<F>
Throws:
ReasoningException

getDerivations

public Collection<Derivation<F>> getDerivations(F obj)
                                         throws ReasoningException
Specified by:
getDerivations in interface ForwardReasoningSession<F>
Throws:
ReasoningException

getDerivations

public Collection<Derivation<F>> getDerivations(Handle<F> handle)
                                         throws ReasoningException
Specified by:
getDerivations in interface ForwardReasoningSession<F>
Throws:
ReasoningException

get

public F get(Handle<F> handle)
      throws ReasoningException
Specified by:
get in interface ForwardReasoningSession<F>
Throws:
ReasoningException

getObjects

public Collection<F> getObjects()
                         throws ReasoningException
Specified by:
getObjects in interface ForwardReasoningSession<F>
Throws:
ReasoningException

remove

public void remove(F obj,
                   boolean strict)
            throws ReasoningException
Specified by:
remove in interface ForwardReasoningSession<F>
Throws:
ReasoningException

remove

public void remove(Handle<F> handle,
                   boolean strict)
            throws ReasoningException
Specified by:
remove in interface ForwardReasoningSession<F>
Throws:
ReasoningException

getDescription

public String getDescription()
Specified by:
getDescription in interface ReasoningSession

getName

public String getName()
Specified by:
getName in interface ReasoningSession

close

public void close()
           throws ReasoningException
Specified by:
close in interface ReasoningSession
Throws:
ReasoningException

reset

public void reset()
           throws ReasoningException
Specified by:
reset in interface ReasoningSession
Throws:
ReasoningException

executeRules

public void executeRules()
                  throws ReasoningException
Specified by:
executeRules in interface ForwardReasoningSession<F>
Throws:
ReasoningException

getInferenceListener

public InferenceListener<F> getInferenceListener()
                                          throws ReasoningException
Specified by:
getInferenceListener in interface ForwardReasoningSession<F>
Throws:
ReasoningException

setInferenceListener

public void setInferenceListener(InferenceListener<F> listener)
                          throws ReasoningException
Specified by:
setInferenceListener in interface ForwardReasoningSession<F>
Throws:
ReasoningException

putConclusion

public KnowledgeBase.PutDerivedResult<F> putConclusion(F conclusion,
                                                       Object rule,
                                                       String ruleName,
                                                       String ruleDescription,
                                                       Method method,
                                                       List<Handle<F>> handleList)
Specified by:
putConclusion in interface KnowledgeBase<F>

getHandle

public Handle<F> getHandle(Object fact)
Specified by:
getHandle in interface ForwardReasoningSession<F>

cleanup

public void cleanup()
             throws ReasoningException
Removes invalid entries. Similar to garbage collection. Can be used with long running sessions with significant rate of removals.

Throws:
ReasoningException

contains

public boolean contains(Object fact)
                 throws ReasoningException
Specified by:
contains in interface ForwardReasoningSession<F>
Throws:
ReasoningException

getExceptionHandler

public ExceptionHandler getExceptionHandler()
                                     throws ReasoningException
Specified by:
getExceptionHandler in interface ReasoningSession
Throws:
ReasoningException

setExceptionHandler

public void setExceptionHandler(ExceptionHandler handler)
                         throws ReasoningException
Specified by:
setExceptionHandler in interface ReasoningSession
Throws:
ReasoningException

contains

public boolean contains(Handle<F> handle)
                 throws ReasoningException
Specified by:
contains in interface ForwardReasoningSession<F>
Throws:
ReasoningException

getHandles

public Collection<Handle<F>> getHandles()
                                 throws ReasoningException
Specified by:
getHandles in interface ForwardReasoningSession<F>
Throws:
ReasoningException

putAll

public List<Handle<F>> putAll(List<? extends F> facts)
                       throws ReasoningException
Specified by:
putAll in interface ForwardReasoningSession<F>
Throws:
ReasoningException

putAll

public Handle<F>[] putAll(F... facts)
                   throws ReasoningException
Specified by:
putAll in interface ForwardReasoningSession<F>
Throws:
ReasoningException

updateObject

public void updateObject(Handle<F> handle,
                         F fact)
                  throws ReasoningException
Specified by:
updateObject in interface ForwardReasoningSession<F>
Throws:
ReasoningException

getConclusionMap

public Map<Class<F>,Set<Class<F>>> getConclusionMap()
Specified by:
getConclusionMap in interface ForwardReasoningSession<F>