All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gaia.sdk.atlas.AtlasClient.kt Maven / Gradle / Ivy

package gaia.sdk.atlas

import com.fasterxml.jackson.databind.ObjectMapper
import gaia.sdk.api.ClientOptions
import gaia.sdk.api.ITransporter
import org.slf4j.LoggerFactory
import java.security.SecureRandom
import java.util.*
import java.util.concurrent.CompletableFuture
import java.util.concurrent.atomic.AtomicLong
import kotlin.collections.ArrayList

class AtlasClient(override val options: ClientOptions, override val transporter: ITransporter) : IAtlasClient {
    companion object {
        private val log = LoggerFactory.getLogger(this::class.java.enclosingClass)
    }

    private val json = ObjectMapper()
    private val nonce = AtomicLong(SecureRandom().nextLong())

    override fun executeNative(statement: String, variables: Map, preprocessors: List)
            : CompletableFuture {
        val body = HashMap()
        body.put("statement", statement)
        body.put("variables", variables)
        body.put("timestamp", System.currentTimeMillis())
        body.put("nonce", java.lang.Long.toString(nonce.getAndIncrement()))
        body.put("preprocessors", preprocessors)

        if (log.isDebugEnabled) {
            log.debug("Statement: $statement")
            log.debug("Variables: $variables")
        }
        val payload = json.writeValueAsBytes(body)
        if (log.isTraceEnabled) {
            log.trace("Payload: ${String(payload)}")
        }
        return transporter.transport(options, AtlasResponse::class.java, payload);
    }

    override fun execute(request: AtlasRequest): CompletableFuture {
        val preprocessors = ArrayList()
        if (request.preprocessors.addPunctuation) preprocessors.add("addPunctuation")
        if (request.preprocessors.removeSignature) preprocessors.add("removeSignature")

        val statement = "query atlas(\$text: String!, \$merge: Boolean!) { nlu(text: \$text, merge: \$merge) { ${request.fields.joinToString(" ")} }}"
        return executeNative(statement, mapOf("text" to request.text, "merge" to request.merge), preprocessors)
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy