Skip to content

Commit

Permalink
feat: Init kotlin worker
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 26, 2023
1 parent 6c9bdc3 commit 301af44
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ import kotlinx.coroutines.coroutineScope
import org.slf4j.Logger
import java.io.File

/**
* The `LangWorker` interface represents a worker responsible for preparing and executing instruction file jobs.
*
* It provides access to the context, file tree, logger, and list of jobs.
* The `prepareJob` method is used to prepare a given instruction file job for execution.
*
* The `start` method is responsible for starting the execution of the job processing. It processes each job in the `jobs` list
* and writes the output to a file specified in the `pureDataFileName` property of the `context`.
*
* @property context The worker context associated with the LangWorker.
* @property fileTree A hashmap representing the file tree associated with the LangWorker.
* @property logger The logger to be used for logging.
* @property jobs A mutable list of instruction file jobs to be processed.
*
* @see WorkerContext
* @see InstructionFileJob
* @see Logger
* @see TypedIns
*/
interface LangWorker {
val context: WorkerContext
val fileTree: HashMap<String, InstructionFileJob>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,16 @@ import java.io.File
import java.util.EnumMap

/**
* A repository will be like this:
*
* | CommitID | AController | AService | BRepository| ARepository|
* |------------|-------------|----------|------------|------------|
* | a67c24fd | 1 | 1 | 1 | 1 |
* | b05d38f6 | 1 | 1 | 1 | 1 |
* | 99ac469e | 1 | 1 | 1 | 1 |
*
* We have different strategies to build the pick datasets.
*
* - by Horizontal (with Import File):
* - by Vertical (with History Change):
* The JavaWorker class is an implementation of the LangWorker interface.
* It provides functionality for handling Java based instruction file jobs.
*/
class JavaWorker(override val context: WorkerContext) : LangWorker {
open class JavaWorker(override val context: WorkerContext) : LangWorker {
override val jobs: MutableList<InstructionFileJob> = mutableListOf()
override val fileTree: HashMap<String, InstructionFileJob> = hashMapOf()
override val logger: Logger = org.slf4j.LoggerFactory.getLogger(JavaWorker::class.java)

private val packageRegex = Regex("package\\s+([a-zA-Z0-9_.]+);")
private val extLength = ".java".length
protected open val packageRegex = Regex("package\\s+([a-zA-Z0-9_.]+);")
protected open val extLength = ".java".length

/**
* Adds a job to the list of instruction file jobs.
Expand All @@ -47,7 +37,7 @@ class JavaWorker(override val context: WorkerContext) : LangWorker {
override fun prepareJob(job: InstructionFileJob) {
this.jobs.add(job)

try{
try {
tryAddClassToTree(job.code, job)
// since the Java Analyser imports will be in data structures
val container = JavaAnalyser().analysis(job.code, job.fileSummary.location)
Expand All @@ -61,7 +51,7 @@ class JavaWorker(override val context: WorkerContext) : LangWorker {
}
}

private fun tryAddClassToTree(code: String, job: InstructionFileJob) {
fun tryAddClassToTree(code: String, job: InstructionFileJob) {
val packageMatch = packageRegex.find(code)
if (packageMatch != null) {
val packageName = packageMatch.groupValues[1]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cc.unitmesh.pick.worker.lang

import cc.unitmesh.pick.ext.buildSourceCode
import cc.unitmesh.pick.worker.WorkerContext
import cc.unitmesh.pick.worker.base.LangWorker
import cc.unitmesh.pick.worker.job.InstructionFileJob
import chapi.ast.kotlinast.KotlinAnalyser
import org.slf4j.Logger

class KotlinWorker(override val context: WorkerContext) : JavaWorker(context), LangWorker {
override val jobs: MutableList<InstructionFileJob> = mutableListOf()

override val fileTree: HashMap<String, InstructionFileJob> = hashMapOf()
override val logger: Logger = org.slf4j.LoggerFactory.getLogger(JavaWorker::class.java)

override val packageRegex = Regex("package\\s+([a-zA-Z0-9_.]+)")
override val extLength = ".kt".length

override fun prepareJob(job: InstructionFileJob) {
this.jobs.add(job)

try {
tryAddClassToTree(job.code, job)
// since the Java Analyser imports will be in data structures
val container = KotlinAnalyser().analysis(job.code, job.fileSummary.location)
job.codeLines = job.code.lines()
container.buildSourceCode(job.codeLines)

job.container = container
} catch (e: Exception) {
logger.error("failed to prepare job: ${job.fileSummary.location}")
e.printStackTrace()
}
}
}

0 comments on commit 301af44

Please sign in to comment.