Package com.storedobject.ai
Class Knowledge
java.lang.Object
com.storedobject.ai.Knowledge
- All Implemented Interfaces:
DataRetriever
- Direct Known Subclasses:
Test
The Knowledge class serves as a repository to manage entities and related attributes within the scope of a transactional context.
It also provides tools and utility methods for managing stored objects, entities, and their associated attributes.
- Author:
- Syam
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionCreates an instance of the Knowledge class using the specified Device.Creates an instance of the Knowledge class with the specified TransactionManager. -
Method Summary
Modifier and TypeMethodDescriptionfinal void
addDataClass
(Class<? extends StoredObject> c, String... attributes) Adds a data class to the internal configuration using the specified class type and optional attributes.final void
addDataClass
(String friendlyName, Class<? extends StoredObject> c, String... attributes) Adds a data class to the knowledge base with an optional list of attributes.final void
addModules
(KnowledgeModule... modules) Adds one or more knowledge modules to the knowledge.<T extends StoredObject>
intCounts the number of instances of the specified class that exist in storage.final Chat
Creates a new chat instance using a default memory retention limit of 10 messages.final Chat
createChat
(int rememberHistory) Creates a new chat instance with a specified memory retention limit for recent chat messages.<T extends StoredObject>
TRetrieves a stored object of the specified type and purpose.getAttributes
(Class<? extends StoredObject> c) Retrieves a list of attributes associated with the specified class of typeStoredObject
.final <T extends StoredObject>
Class<T> Retrieves the class of a specified entity by its name.final Id
Retrieves anId
object based on the provided string representation of the identifier.final TransactionManager
Retrieves the TransactionManager instance associated with this Knowledge object.boolean
Indicates whether logging is currently enabled for the Knowledge instance.<T extends StoredObject>
ObjectIterator<T> Lists all stored objects of the specified type.void
Logs the specified object if logging is enabled in the current configuration.final <T extends StoredObject>
voidsave
(JSONMap map, String name, ObjectIterator<T> objects) Saves a collection ofStoredObject
instances to a specifiedJSONMap
under the given name.final <T extends StoredObject>
voidSaves the provided object using the specified parameters and updates the given JSONMap.void
setDataRetriever
(DataRetriever dataRetriever) void
setLogging
(boolean logging) Sets the logging state for the Knowledge object.
-
Constructor Details
-
Knowledge
Creates an instance of the Knowledge class using the specified Device. The TransactionManager instance required by the Knowledge class is retrieved from the provided Device's server.- Parameters:
device
- the Device instance from which the associated TransactionManager is retrieved to initialize the Knowledge object
-
Knowledge
Creates an instance of the Knowledge class with the specified TransactionManager.- Parameters:
tm
- the TransactionManager instance to be used with this Knowledge object
-
-
Method Details
-
setDataRetriever
-
getAttributes
Retrieves a list of attributes associated with the specified class of typeStoredObject
. The attributes are determined based on user roles and access permissions.- Parameters:
c
- The class of typeStoredObject
for which attributes are to be retrieved.- Returns:
- A
StringList
containing the attributes of the specified class, ornull
if access is restricted.
-
getTransactionManager
Retrieves the TransactionManager instance associated with this Knowledge object.- Returns:
- The
TransactionManager
instance used by this Knowledge object.
-
addModules
Adds one or more knowledge modules to the knowledge. Modules provide additional functionality or capabilities to enhance the knowledge interaction.- Parameters:
modules
- The knowledge modules to be added. Each module can be any non-null object implementingKnowledgeModule
. If any module is null, it is ignored.
-
addDataClass
public final void addDataClass(String friendlyName, Class<? extends StoredObject> c, String... attributes) Adds a data class to the knowledge base with an optional list of attributes. The data class is associated with a friendly name for easier identification. Optionally, attributes can be specified to define additional metadata.- Parameters:
friendlyName
- The user-friendly name to associate with the data class.c
- The class type that extendsStoredObject
to add to the knowledge base.attributes
- Optional attributes to associate with the data class.
-
addDataClass
Adds a data class to the internal configuration using the specified class type and optional attributes. This method automatically generates a friendly name for the class by converting its name to a label format without spaces.- Parameters:
c
- The class type to be added, extendingStoredObject
.attributes
- Optional attributes associated with the class, represented as an array of strings.
-
createChat
Creates a new chat instance using a default memory retention limit of 10 messages. This method is a convenience overload of thecreateChat(int rememberHistory)
method and delegates to it with a predefined value forrememberHistory
.- Returns:
- A new
Chat
instance with default parameters. - Throws:
Exception
- If there is an issue, creating the chat instance or accessing required resources.
-
createChat
Creates a new chat instance with a specified memory retention limit for recent chat messages. This method delegates the creation of the chat instance to the AI associated with the current session of the TransactionManager.- Parameters:
rememberHistory
- The number of recent chat messages to retain in memory. Values below 1 default to 10, and values above 100 are capped at 100.- Returns:
- A new
Chat
instance configured with the current knowledge and memory parameters. - Throws:
Exception
- If an error occurs during the creation of the chat instance.
-
log
Logs the specified object if logging is enabled in the current configuration. This method uses the associated TransactionManager's logging mechanism.- Parameters:
anything
- the object to be logged. Can represent any information or data the caller wishes to record in the log.
-
setLogging
public void setLogging(boolean logging) Sets the logging state for the Knowledge object.- Parameters:
logging
- a boolean value indicating whether logging should be enabled (true) or disabled (false)
-
isLogging
public boolean isLogging()Indicates whether logging is currently enabled for the Knowledge instance.- Returns:
true
if logging is enabled,false
otherwise.
-
getId
Retrieves anId
object based on the provided string representation of the identifier. If the string is invalid, logs an error in the givenJSONMap
and returnsnull
.- Parameters:
map
- TheJSONMap
instance to store error details in case of an invalid ID.idString
- The string representation of the identifier to be converted into anId
.- Returns:
- An
Id
object constructed from the provided string, ornull
if the string is invalid.
-
getClass
Retrieves the class of a specified entity by its name. If the entity name is invalid or not found in the available entities, an error message is added to the provided JSONMap andnull
is returned.- Type Parameters:
T
- the type of the class that extends StoredObject- Parameters:
map
- the JSONMap used to store error messages if the entity name is invalidentityName
- the name of the entity for which the class is to be retrieved- Returns:
- the class of the specified entity if found; otherwise,
null
-
save
public final <T extends StoredObject> void save(JSONMap map, String name, ObjectIterator<T> objects) Saves a collection ofStoredObject
instances to a specifiedJSONMap
under the given name. Each object in the collection is individually processed and saved. Errors during the save process for individual objects are ignored.- Type Parameters:
T
- The type of theStoredObject
.- Parameters:
map
- TheJSONMap
in which the objects are to be saved.name
- The name of the key under which the array of objects will be saved in the map.objects
- AnObjectIterator
containing the objects to be saved. Each object must extendStoredObject
.
-
save
Saves the provided object using the specified parameters and updates the given JSONMap. If the object is null, the JSONMap will report an error with the provided name.- Type Parameters:
T
- the type of the object extendingStoredObject
to be saved.- Parameters:
map
- theJSONMap
where the save operation results or errors will be recorded.name
- the name or identifier used to associate with the saved object or error message.object
- theStoredObject
to be saved. If null, an error message is added to the map.
-
list
Description copied from interface:DataRetriever
Lists all stored objects of the specified type.- Specified by:
list
in interfaceDataRetriever
- Type Parameters:
T
- the type of stored objects to be listed; it must extendStoredObject
.- Parameters:
c
- theClass
object representing the type of stored objects to be listed.- Returns:
- an
ObjectIterator<T>
containing all stored objects of the specified type.
-
count
Description copied from interface:DataRetriever
Counts the number of instances of the specified class that exist in storage.- Specified by:
count
in interfaceDataRetriever
- Type Parameters:
T
- the type of the class extendingStoredObject
.- Parameters:
c
- the class whose instances need to be counted.- Returns:
- the number of instances of the specified class in storage.
-
get
Description copied from interface:DataRetriever
Retrieves a stored object of the specified type and purpose.- Specified by:
get
in interfaceDataRetriever
- Type Parameters:
T
- The type of the stored object to retrieve, extendingStoredObject
.- Parameters:
c
- The class object representing the type of the stored object to retrieve.purpose
- The purpose or reason for retrieving the stored object, represented as a string.- Returns:
- The stored object of the specified type.
-