👨‍💻 dev porting Auxiliary modules Java->kotlin

development

owly

闇の伝説
Staff member
戦闘 コーダー
project progress at 100%
 
0% 100%

1. AlgDispenser +
2. UniqueItemsPriorityQue: LGFIFO +
3. UniqueItemSizeLimitedPriorityQueue +
4. RefreshQ +
5. AnnoyedQue +
6. AXCmdBreaker +
7. AXFunnel +
8. AXGamification +
9. AXKeyValuePair +
10. TrGEV3 +
11. TrgTolerance +
12. AXLearnability +
13. AXLHousing +
14. LGTypeConverter +
15. DrawRnd +
16. DrawRndDigits +
17. Responder +
18. AXNeuroSama +
19. PercentDripper +
20. AXNPC +
21. AXNPC2 +
22. AXPassword +
23. Prompt +
24. AXPrompt +
25. AXShoutOut +
26. Strategy +
27. AXStrategy +
28. AXStringSplit +
29. AXStrOrDefault +
30. TrgTime +
31. Cron +
32. Cycler +
33. Differ +
34. Eliza +
35. InputFilter +
36. Magic8Ball +
37. OnOffSwitch +
38. ElizaDeducer, ElizaDeducerInitializer(ElizaDeducer) +
39. RailChatBot +
40. SkillHubAlgDispenser +
41. SpiderSense +
42. TimeAccumulator +
43. TODOListManager +
44. TrgArgue +
45. TrgEveryNMinutes +
46. TrgMinute +
47. TrgSnooze +
48. AXContextCmd +
49. AXConvince
50. AXSkillBundle
@fukurou

 
Last edited by a moderator:

fukurou

the supreme coder
ADMIN
Kotlin:
import livinGrimoire.Algorithm
import java.util.*

// (*)Algorithm Dispensers
class AlgDispenser(vararg algorithms: Algorithm) {
    // super class to output an algorithm out of a selection of algorithms
    private val algs: ArrayList<Algorithm> = ArrayList<Algorithm>()
    private var activeAlg = 0
    private val rand = Random()

    init {
        for (alg in algorithms) {
            algs.add(alg)
        }
    }

    fun addAlgorithm(alg: Algorithm): AlgDispenser {
        // builder pattern
        algs.add(alg)
        return this
    }

    fun dispenseAlgorithm(): Algorithm {
        return algs[activeAlg]
    }

    fun rndAld(): Algorithm {
        // return a random algorithm
        return algs[rand.nextInt(algs.size)]
    }

    fun moodAlg(mood: Int) {
        // set output algorithm based on number representing mood
        if (mood > -1 && mood < algs.size) {
            activeAlg = mood
        }
    }

    fun algUpdate(mood: Int, alg: Algorithm) {
        // update an algorithm
        if (!(mood > -1 && mood < algs.size)) {
            return
        }
        algs[mood] = alg
    }

    fun algRemove(mood: Int) {
        // remove an algorithm
        if (!(mood > -1 && mood < algs.size)) {
            return
        }
        algs.removeAt(mood)
    }

    fun cycleAlg() {
        activeAlg++
        if (activeAlg == algs.size) {
            activeAlg = 0
        }
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
open class LGFIFO<T> {
    //first in first out queue
    var elements = ArrayList<T>()

    open fun add(item: T) {
        elements.add(item)
    }

    fun size(): Int {
        return elements.size
    }

    open fun peak(): T? {
        return if (size() == 0) {
            null
        } else elements[0]
    }

    open fun poll(): T? {
        if (size() == 0) {
            return null
        }
        val result = elements[0]
        elements.removeAt(0)
        return result
    }

    open fun removeItem(item: T) {
        if (elements.contains(item)) {
            elements.remove(item)
        }
    }

    fun clear() {
        elements.clear()
    }

    private val rand = Random()
    open val rNDElement: T?
        get() = if (elements.isEmpty()) {
            null
        } else elements[rand.nextInt(elements.size)]

    operator fun contains(input: T): Boolean {
        return elements.contains(input)
    }

    val isEmpty: Boolean
        get() = elements.isEmpty()

    fun remove(element: T) {
        elements.remove(element)
    }

    operator fun iterator(): Iterator<T> {
        return elements.iterator()
    }
}
 
Last edited:

fukurou

the supreme coder
ADMIN
Kotlin:
open class LGFIFO<T> {
    //first in first out queue
    var elements = ArrayList<T>()

    open fun add(item: T) {
        elements.add(item)
    }

    fun size(): Int {
        return elements.size
    }

    open fun peak(): T? {
        return if (size() == 0) {
            null
        } else elements[0]
    }

    fun poll(): T? {
        if (size() == 0) {
            return null
        }
        val result = elements[0]
        elements.removeAt(0)
        return result
    }

    fun removeItem(item: T) {
        if (elements.contains(item)) {
            elements.remove(item)
        }
    }

    fun clear() {
        elements.clear()
    }

    private val rand = Random()
    val rNDElement: T?
        get() = if (elements.isEmpty()) {
            null
        } else elements[rand.nextInt(elements.size)]

    operator fun contains(input: T): Boolean {
        return elements.contains(input)
    }

    val isEmpty: Boolean
        get() = elements.isEmpty()

    fun remove(element: T) {
        elements.remove(element)
    }

    operator fun iterator(): Iterator<T> {
        return elements.iterator()
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
class UniqueItemsPriorityQue : LGFIFO<String>() {
    // a priority queue without repeating elements
    override fun add(item: String) {
        if (!super.contains(item)) {
            super.add(item)
        }
    }

    override fun peak(): String {
        return super.peak() ?: return ""
    }

    fun strContainsResponse(str: String): Boolean {
        var result = false
        for (tempStr in elements) {
            if (str.contains(tempStr)) {
                result = true
                break
            }
        }
        return result
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
package auxiliary_modules

open class UniqueItemSizeLimitedPriorityQueue : UniqueItemsPriorityQue() {
// items in the queue are unique and do not repeat
    // the size of the queue is limited
    var limit = 5

    override fun add(item: String) {
if (super.size() == limit) {
super.poll()
        }
super.add(item)
    }

override fun poll(): String {
return super.poll() ?: return ""
    }

override val rNDElement: String
get() = super.rNDElement ?: ""
    val asList: ArrayList<String>
get() = elements
}
 
Last edited:

fukurou

the supreme coder
ADMIN
Kotlin:
class RefreshQ : UniqueItemSizeLimitedPriorityQueue() {
override fun removeItem(item: String) {
super.elements.remove(item)
    }

override fun add(item: String) {
// FILO
        if (super.contains(item)) {
            removeItem(item)
        }
super.add(item)
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
class AnnoyedQue(queLim: Int) {
    private val q1 = RefreshQ()
    private val q2 = RefreshQ()

    init {
        q1.limit = queLim
        q2.limit = queLim
    }

    fun learn(ear: String) {
        if (q1.contains(ear)) {
            q2.add(ear)
            return
        }
        q1.add(ear)
    }

    fun isAnnoyed(ear: String): Boolean {
        return q2.strContainsResponse(ear)
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
class AXCmdBreaker( // separate command parameter from the command
    var conjuration: String
) {
    fun extractCmdParam(s1: String): String {
        return if (s1.contains(conjuration)) {
            s1.replace(conjuration, "").trim { it <= ' ' }
        } else ""
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
class AXFunnel {
    // funnel many inputs to fewer or one input
    // allows using command variations in skills
    private val dic: MutableMap<String, String>
    private var defaultStr: String

    init {
        dic = HashMap()
        defaultStr = "default"
    }

    fun setDefault(defaultStr: String) {
        this.defaultStr = defaultStr
    }

    fun addKV(key: String, value: String): AXFunnel {
        // add key-value pair
        dic[key] = value
        return this
    }

    fun addK(key: String): AXFunnel {
        // add key with default value
        dic[key] = defaultStr
        return this
    }

    fun funnel(key: String): String {
        // get value from dictionary or return the key itself as default
        return dic.getOrDefault(key, key)
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
class AXGamification {
    // this auxiliary module can add fun to tasks, skills, and abilities simply by
    // tracking their usage, and maximum use count.
    var counter = 0
        private set
    var max = 0
        private set

    fun resetCount() {
        counter = 0
    }

    fun resetAll() {
        max = 0
        counter = 0
    }

    fun increment() {
        counter++
        if (counter > max) {
            max = counter
        }
    }

    fun incrementBy(n: Int) {
        counter += n
        if (counter > max) {
            max = counter
        }
    }

    fun reward(cost: Int): Boolean {
        // game grind points used for rewards
        // consumables, items or upgrades this makes games fun
        if (cost > counter) {
            return false
        }
        counter -= cost
        return true
    }

    fun surplus(cost: Int): Boolean {
        // has surplus for reward?
        return cost <= counter
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
class AXKeyValuePair {
    var key = ""
    var value = ""

    constructor()
    constructor(key: String, value: String) {
        this.key = key
        this.value = value
    }

    override fun toString(): String {
        return "$key;$value"
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
open class TrGEV3 {
    // advanced boolean gates with internal logic
    // these ease connecting common logic patterns, as triggers
    open fun reset() {}
    fun input(ear: String, skin: String, eye: String) {}
    open fun trigger(): Boolean {
        return false
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
public class TrgTolerance extends TrGEV3{
    // this boolean gate will return true till depletion or reset()
    private int repeats = 0;
    private int maxrepeats; //2 recomended
    public TrgTolerance(int maxrepeats) {
        this.maxrepeats = maxrepeats;
    }

    public void setMaxrepeats(int maxrepeats) {
        this.maxrepeats = maxrepeats;
        reset();
    }

    @Override
    public void reset() {
        // refill trigger
        repeats = maxrepeats;
    }

    @Override
    public Boolean trigger() {
        // will return true till depletion or reset()
        repeats--;
        return repeats > 0;
    }
    public void disable(){
        repeats = 0;
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
class AXLearnability(tolerance: Int) {
    private var algSent = false

    // problems that may result because of the last deployed algorithm:
    var defcons = UniqueItemSizeLimitedPriorityQueue() // default size = 5

    // major chaotic problems that may result because of the last deployed algorithm:
    var defcon5 = UniqueItemSizeLimitedPriorityQueue()

    // goals the last deployed algorithm aims to achieve:
    var goals = UniqueItemSizeLimitedPriorityQueue()

    // how many failures / problems till the algorithm needs to mutate (change)
    var trgTolerance: TrgTolerance

    init {
        trgTolerance = TrgTolerance(tolerance)
        trgTolerance.reset()
    }

    fun pendAlg() {
        // an algorithm has been deployed
        // call this method when an algorithm is deployed (in a DiSkillV2 object)
        algSent = true
        trgTolerance.trigger()
    }

    fun pendAlgWithoutConfirmation() {
        // an algorithm has been deployed
        algSent = true
        //no need to await for a thank you or check for goal manifestation :
        // trgTolerance.trigger();
        // using this method instead of the default "pendAlg" is the same as
        // giving importance to the stick and not the carrot when learning
        // this method is mosly fitting work place situations
    }

    fun mutateAlg(input: String): Boolean {
        // recommendation to mutate the algorithm ? true/ false
        if (!algSent) {
            return false
        } // no alg sent=> no reason to mutate
        if (goals.contains(input)) {
            trgTolerance.reset()
            algSent = false
            return false
        }
        // goal manifested the sent algorithm is good => no need to mutate the alg
        if (defcon5.contains(input)) {
            trgTolerance.reset()
            algSent = false
            return true
        }
        // ^ something bad happend probably because of the sent alg
        // recommend alg mutation
        if (defcons.contains(input)) {
            algSent = false
            val mutate = !trgTolerance.trigger()
            if (mutate) {
                trgTolerance.reset()
            }
            return mutate
        }
        // ^ negative result, mutate the alg if this occures too much
        return false
    }

    fun resetTolerance() {
        // use when you run code to change algorithms regardless of learnability
        trgTolerance.reset()
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
class AXLHousing {
    fun decorate(str1: String?): String {
        // override me
        return ""
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
enum class enumRegexGrimoire {
    Email, TimeStamp, Integer, Double_num, RepeatedWord, Phone, TrackingID, IPV4, Domain, Number, SecondlessTimeStamp, Date, FullDate, SimpleTimeStamp
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
// returns expression of type theRegex from the string str2Check
class RegexUtil {
    var regexDictionary: Hashtable<enumRegexGrimoire, String> = Hashtable<enumRegexGrimoire, String>()

    init {
        regexDictionary[enumRegexGrimoire.Email] = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
        regexDictionary[enumRegexGrimoire.TimeStamp] = "[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}"
        regexDictionary[enumRegexGrimoire.SimpleTimeStamp] = "[0-9]{1,2}:[0-9]{1,2}"
        regexDictionary[enumRegexGrimoire.SecondlessTimeStamp] = "[0-9]{1,2}:[0-9]{1,2}"
        regexDictionary[enumRegexGrimoire.FullDate] =
            "[0-9]{1,4}/[0-9]{1,2}/[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}"
        regexDictionary[enumRegexGrimoire.Date] = "[0-9]{1,4}/[0-9]{1,2}/[0-9]{1,2}"
        regexDictionary[enumRegexGrimoire.Double_num] = "[-+]?[0-9]*[.,][0-9]*"
        regexDictionary[enumRegexGrimoire.Integer] = "[-+]?[0-9]{1,13}"
        regexDictionary[enumRegexGrimoire.RepeatedWord] = "\\b([\\w\\s']+) \\1\\b"
        regexDictionary[enumRegexGrimoire.Phone] = "[0]\\d{9}"
        regexDictionary[enumRegexGrimoire.TrackingID] = "[A-Z]{2}[0-9]{9}[A-Z]{2}"
        regexDictionary[enumRegexGrimoire.IPV4] = "([0-9].){4}[0-9]*"
        regexDictionary[enumRegexGrimoire.Domain] = "[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
        regexDictionary[enumRegexGrimoire.Number] = "\\d+(\\.\\d+)?"
    }

    fun extractRegex(theRegex: String, str2Check: String): String {
        val checkRegex = Pattern.compile(theRegex)
        val regexMatcher = checkRegex.matcher(str2Check)
        while (regexMatcher.find()) {
            if (regexMatcher.group().isNotEmpty()) {
                return regexMatcher.group().trim { it <= ' ' }
            }
        }
        return ""
    }

    fun extractRegex(theRegex: enumRegexGrimoire, str2Check: String): String {
        val checkRegex = regexDictionary[theRegex]?.let { Pattern.compile(it) } ?: return ""
        val regexMatcher = checkRegex.matcher(str2Check)
        while (regexMatcher.find()) {
            if (regexMatcher.group().isNotEmpty()) {
                return regexMatcher.group().trim { it <= ' ' }
            }
        }
        return ""
    }

    fun extractAllRegexes(theRegex: String, str2Check: String): ArrayList<String> {
        // return a list of all matches
        val list = ArrayList<String>()
        val checkRegex = Pattern.compile(theRegex)
        val regexMatcher = checkRegex.matcher(str2Check)
        while (regexMatcher.find()) {
            if (regexMatcher.group().isNotEmpty()) {
                list.add(regexMatcher.group().trim { it <= ' ' })
            }
        }
        return list
    }

    fun extractAllRegexes(theRegex: enumRegexGrimoire, str2Check: String): ArrayList<String> {
        // return a list of all matches
        val list = ArrayList<String>()
        val checkRegex = regexDictionary[theRegex]?.let { Pattern.compile(it) } ?: return list
        val regexMatcher = checkRegex.matcher(str2Check)
        while (regexMatcher.find()) {
            if (regexMatcher.group().isNotEmpty()) {
                list.add(regexMatcher.group().trim { it <= ' ' })
            }
        }
        return list
    }

    fun pointRegex(str2Check: String): Point {
        // "[-+]?[0-9]{1,13}(\\.[0-9]*)?" for double numbers
        val theRegex = "[-+]?[0-9]{1,13}"
        val result = Point(0, 0)
        val list = ArrayList<String>()
        val checkRegex = Pattern.compile(theRegex)
        val regexMatcher = checkRegex.matcher(str2Check)
        while (regexMatcher.find()) {
            if (regexMatcher.group().isNotEmpty()) {
                result.y = regexMatcher.group().trim { it <= ' ' }.toInt()
            }
        }
        var phase2 = str2Check.replace(result.y.toString() + "", "")
        phase2 = extractRegex(enumRegexGrimoire.Number, phase2)
        result.x = phase2.toInt()
        return result
    }

    fun contactRegex(str2Check: String): String {
        // return a list of all matches
        val theRegex = "(?<=contact)(.*)"
        val list = ArrayList<String>()
        val checkRegex = Pattern.compile(theRegex)
        val regexMatcher = checkRegex.matcher(str2Check)
        while (regexMatcher.find()) {
            if (regexMatcher.group().isNotEmpty()) {
                return regexMatcher.group().trim { it <= ' ' }
            }
        }
        return ""
    }

    fun duplicateRegex(str2Check: String): String {
        // return a list of all matches
        // String theRegex = "\\b(\\w+)(\\b\\W+\\b\\1\\b)*";
        val theRegex = "\\b([\\w\\s']+) \\1\\b" // set to 1 repeat of a word like hadoken hadoken
        val list = ArrayList<String>()
        val checkRegex = Pattern.compile(theRegex)
        val regexMatcher = checkRegex.matcher(str2Check)
        while (regexMatcher.find()) {
            if (regexMatcher.group().isNotEmpty()) {
                return uniqueWord(regexMatcher.group().trim { it <= ' ' })
            }
        }
        return ""
    }

    fun uniqueWord(str: String): String {
        val list = ArrayList<String>()
        val s = str.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        var p = s[0]
        list.add(p)
        for (i in 1 until s.size) {
            if (p !== s[i]) {
                list.add(s[i])
            }
            p = s[i]
        } // i
        return list[0]
    }

    fun afterWord(word: String, str2Check: String): String {
        // return a list of all matches
        val theRegex = "(?<=$word)(.*)"
        val list = ArrayList<String>()
        val checkRegex = Pattern.compile(theRegex)
        val regexMatcher = checkRegex.matcher(str2Check)
        while (regexMatcher.find()) {
            if (regexMatcher.group().isNotEmpty()) {
                return regexMatcher.group().trim { it <= ' ' }
            }
        }
        return ""
    }

    fun firstWord(str2Check: String): String {
        val arr = str2Check.split(" ".toRegex(), limit = 2).toTypedArray()
        return arr[0]
    }
}
 

fukurou

the supreme coder
ADMIN
Kotlin:
class LGTypeConverter {
    private val r1: RegexUtil = RegexUtil()
    fun convertToInt(v1: String): Int {
        val temp: String = r1.extractRegex(enumRegexGrimoire.Integer, v1)
        return if (temp.isEmpty()) {
            0
        } else temp.toInt()
    }

    fun convertToDouble(v1: String): Double {
        val temp: String = r1.extractRegex(enumRegexGrimoire.Double_num, v1)
        return if (temp.isEmpty()) {
            0.0
        } else temp.toDouble()
    }
}
 
Top