Class KnowledgeBaseServiceLocator
- java.lang.Object
-
- org.projog.core.kb.KnowledgeBaseServiceLocator
-
public class KnowledgeBaseServiceLocator extends java.lang.ObjectAssociates arbitrary objects with aKnowledgeBase.Provides a way to implement a one-to-one relationship between a
KnowledgeBaseand its services. i.e. AKnowledgeBasecan be associated with one, and only one,RecordedDatabase- and aRecordedDatabasecan be associated with one, and only one,KnowledgeBase.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.util.Map<KnowledgeBase,KnowledgeBaseServiceLocator>CACHEprivate KnowledgeBasekbprivate java.util.Map<java.lang.Class<?>,java.lang.Object>services
-
Constructor Summary
Constructors Modifier Constructor Description privateKnowledgeBaseServiceLocator(KnowledgeBase kb)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddInstance(java.lang.Class<?> referenceType, java.lang.Object instance)Adds the specifiedinstancewith the specifiedreferenceTypeas its key.private voidassertAssignableFrom(java.lang.Class<?> referenceType, java.lang.Class<?> instanceType)private voidassertInstanceOf(java.lang.Class<?> referenceType, java.lang.Object instance)private java.lang.ObjectcreateInstance(java.lang.Class<?> referenceType, java.lang.Class<?> instanceType)private static KnowledgeBaseServiceLocatorcreateServiceLocator(KnowledgeBase kb)<T> TgetInstance(java.lang.Class<?> instanceType)Returns theObjectassociated the specifiedinstanceType.<T> TgetInstance(java.lang.Class<?> referenceType, java.lang.Class<?> instanceType)Returns theObjectassociated the specifiedreferenceType.private java.lang.reflect.Constructor<?>getKnowledgeBaseArgumentConstructor(java.lang.Class<?> c)static KnowledgeBaseServiceLocatorgetServiceLocator(KnowledgeBase kb)Returns theKnowledgeBaseServiceLocatorassociated with the specifiedKnowledgeBase.private java.lang.ObjectnewInstance(java.lang.Class<?> c)Returns a new instance of the specified class.
-
-
-
Field Detail
-
CACHE
private static final java.util.Map<KnowledgeBase,KnowledgeBaseServiceLocator> CACHE
-
kb
private final KnowledgeBase kb
-
services
private final java.util.Map<java.lang.Class<?>,java.lang.Object> services
-
-
Constructor Detail
-
KnowledgeBaseServiceLocator
private KnowledgeBaseServiceLocator(KnowledgeBase kb)
-
-
Method Detail
-
getServiceLocator
public static KnowledgeBaseServiceLocator getServiceLocator(KnowledgeBase kb)
Returns theKnowledgeBaseServiceLocatorassociated with the specifiedKnowledgeBase.If no
KnowledgeBaseServiceLocatoris already associated with the specifiedKnowledgeBasethen a newKnowledgeBaseServiceLocatorwill be created.
-
createServiceLocator
private static KnowledgeBaseServiceLocator createServiceLocator(KnowledgeBase kb)
-
addInstance
public void addInstance(java.lang.Class<?> referenceType, java.lang.Object instance)Adds the specifiedinstancewith the specifiedreferenceTypeas its key.- Throws:
java.lang.IllegalArgumentException- Ifinstanceis not an instance ofReferenceType.java.lang.IllegalStateException- If there is already a service associated withreferenceType.
-
getInstance
public <T> T getInstance(java.lang.Class<?> instanceType)
Returns theObjectassociated the specifiedinstanceType.If no
Objectis already associated withinstanceTypethen a new instance ofinstanceTypewill be created and associated withinstanceTypefor future use.- Throws:
java.lang.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
public <T> T getInstance(java.lang.Class<?> referenceType, java.lang.Class<?> instanceType)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:
java.lang.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
private java.lang.Object createInstance(java.lang.Class<?> referenceType, java.lang.Class<?> instanceType)
-
assertAssignableFrom
private void assertAssignableFrom(java.lang.Class<?> referenceType, java.lang.Class<?> instanceType)
-
assertInstanceOf
private void assertInstanceOf(java.lang.Class<?> referenceType, java.lang.Object instance)
-
newInstance
private java.lang.Object newInstance(java.lang.Class<?> c)
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 java.lang.reflect.Constructor<?> getKnowledgeBaseArgumentConstructor(java.lang.Class<?> c) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException- Throws:
java.lang.InstantiationExceptionjava.lang.IllegalAccessExceptionjava.lang.reflect.InvocationTargetException
-
-