Class KnowledgeBaseServiceLocator


  • public class KnowledgeBaseServiceLocator
    extends java.lang.Object
    Associates arbitrary objects with a 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.

    • Method Detail

      • getServiceLocator

        public static KnowledgeBaseServiceLocator getServiceLocator​(KnowledgeBase kb)
        Returns the KnowledgeBaseServiceLocator associated with the specified KnowledgeBase.

        If no KnowledgeBaseServiceLocator is already associated with the specified KnowledgeBase then a new KnowledgeBaseServiceLocator will be created.

      • addInstance

        public void addInstance​(java.lang.Class<?> referenceType,
                                java.lang.Object instance)
        Adds the specified instance with the specified referenceType as its key.
        Throws:
        java.lang.IllegalArgumentException - If instance is not an instance of ReferenceType.
        java.lang.IllegalStateException - If there is already a service associated with referenceType.
      • getInstance

        public <T> T getInstance​(java.lang.Class<?> instanceType)
        Returns the Object associated the specified instanceType.

        If no Object is already associated with instanceType then a new instance of instanceType will be created and associated with instanceType for future use.

        Throws:
        java.lang.RuntimeException - if an attempt to instantiate a new instance of the instanceType fails. e.g. If it does not have a public constructor that accepts either no arguments or a single KnowledgeBase argument.
      • getInstance

        public <T> T getInstance​(java.lang.Class<?> referenceType,
                                 java.lang.Class<?> instanceType)
        Returns the Object associated the specified referenceType.

        If no Object is already associated with referenceType then a new instance of instanceType will be created and associated with referenceType for 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 with referenceType.
        Throws:
        java.lang.RuntimeException - If an attempt to instantiate a new instance of the instanceType fails. e.g. If instanceType does not have a public constructor that accepts either no arguments or a single KnowledgeBase argument - or if referenceType is 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.InstantiationException
        java.lang.IllegalAccessException
        java.lang.reflect.InvocationTargetException