Class KnowledgeBaseServiceLocator
KnowledgeBase.
Provides a way to implement a one-to-one relationship between a KnowledgeBase and its services. i.e. A
KnowledgeBase can be associated with one, and only one, RecordedDatabase - and a
RecordedDatabase can be associated with one, and only one, KnowledgeBase.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Map<KnowledgeBase, KnowledgeBaseServiceLocator> private final KnowledgeBase -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddInstance(Class<?> referenceType, Object instance) Adds the specifiedinstancewith the specifiedreferenceTypeas its key.private voidassertAssignableFrom(Class<?> referenceType, Class<?> instanceType) private voidassertInstanceOf(Class<?> referenceType, Object instance) private ObjectcreateInstance(Class<?> referenceType, Class<?> instanceType) private static KnowledgeBaseServiceLocator<T> TgetInstance(Class<?> instanceType) Returns theObjectassociated the specifiedinstanceType.<T> TgetInstance(Class<?> referenceType, Class<?> instanceType) Returns theObjectassociated the specifiedreferenceType.private Constructor<?> static KnowledgeBaseServiceLocatorReturns theKnowledgeBaseServiceLocatorassociated with the specifiedKnowledgeBase.private ObjectnewInstance(Class<?> c) Returns a new instance of the specified class.
-
Field Details
-
CACHE
-
kb
-
services
-
-
Constructor Details
-
KnowledgeBaseServiceLocator
- See Also:
-
-
Method Details
-
getServiceLocator
Returns theKnowledgeBaseServiceLocatorassociated with the specifiedKnowledgeBase.If no
KnowledgeBaseServiceLocatoris already associated with the specifiedKnowledgeBasethen a newKnowledgeBaseServiceLocatorwill be created. -
createServiceLocator
-
addInstance
Adds the specifiedinstancewith the specifiedreferenceTypeas its key.- Throws:
IllegalArgumentException- Ifinstanceis not an instance ofReferenceType.IllegalStateException- If there is already a service associated withreferenceType.
-
getInstance
Returns theObjectassociated the specifiedinstanceType.If no
Objectis already associated withinstanceTypethen a new instance ofinstanceTypewill be created and associated withinstanceTypefor future use.- Throws:
RuntimeException- if an attempt to instantiate a new instance of theinstanceTypefails. e.g. If it does not have a public constructor that accepts either no arguments or a singleKnowledgeBaseargument.
-
getInstance
Returns theObjectassociated the specifiedreferenceType.If no
Objectis already associated withreferenceTypethen a new instance ofinstanceTypewill be created and associated withreferenceTypefor future use.- Parameters:
referenceType- The class to use as the key to retrieve an existing service.instanceType- The class to create a new instance of if there is no existing service associated withreferenceType.- Throws:
RuntimeException- If an attempt to instantiate a new instance of theinstanceTypefails. e.g. IfinstanceTypedoes not have a public constructor that accepts either no arguments or a singleKnowledgeBaseargument - or ifreferenceTypeis not the same as, nor is a superclass or superinterface of,instanceType.
-
createInstance
-
assertAssignableFrom
-
assertInstanceOf
-
newInstance
Returns a new instance of the specified class.If the class has a constructor that takes a KnowledgeBase as its single argument then an attempt is made to use that to construct the new instance - else an attempt is made to construct a new instance using the no-arg constructor.
-
getKnowledgeBaseArgumentConstructor
private Constructor<?> getKnowledgeBaseArgumentConstructor(Class<?> c) throws InstantiationException, IllegalAccessException, InvocationTargetException
-