Class KnowledgeBaseServiceLocator

java.lang.Object
org.projog.core.kb.KnowledgeBaseServiceLocator

public class KnowledgeBaseServiceLocator extends 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.

  • Field Details

  • Constructor Details

  • Method Details

    • 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.

    • createServiceLocator

      private static KnowledgeBaseServiceLocator createServiceLocator(KnowledgeBase kb)
    • addInstance

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

      public <T> T getInstance(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:
      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(Class<?> referenceType, 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:
      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 Object createInstance(Class<?> referenceType, Class<?> instanceType)
    • assertAssignableFrom

      private void assertAssignableFrom(Class<?> referenceType, Class<?> instanceType)
    • assertInstanceOf

      private void assertInstanceOf(Class<?> referenceType, Object instance)
    • newInstance

      private Object newInstance(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