Skip to content

Commit

Permalink
Added code generator infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
arminzavada committed Oct 15, 2024
1 parent c9f9cd1 commit fe43e68
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 0 deletions.
1 change: 1 addition & 0 deletions similarity/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/src/gen/
45 changes: 45 additions & 0 deletions similarity/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import com.pswidersk.gradle.python.VenvTask

plugins {
id("hu.bme.mit.ase.shingler.gradle.application")
id("com.pswidersk.python-plugin") version "2.7.2"
}

application {
mainClass = "hu.bme.mit.ase.shingler.similarity.SimilarityApp"
}

val srcGenJava = "src/gen/java"

sourceSets.main {
java.srcDir(srcGenJava)
}

dependencies {
implementation(project(":workflow"))

Expand All @@ -19,3 +28,39 @@ dependencies {

testRuntimeOnly(libs.junit.jupiter.engine)
}

val installPythonPackages by tasks.registering(VenvTask::class) {
venvExec = "pip3"

args = listOf(
"install",
"jinja2",
)
}

val generateSimilarityWorkflow by tasks.registering(VenvTask::class) {
dependsOn(installPythonPackages)

inputs.files(
"src/main/python/generate.py",
"src/main/jinja/workflow.java.j2",
"model.json",
)

args = listOf(
"src/main/python/generate.py",
"model.json",
"src/main/jinja/workflow.java.j2",
"$srcGenJava/hu/bme/mit/ase/shingler/similarity/SimilarityWorkflow.java",
)

outputs.dir(srcGenJava)
}

tasks.compileJava {
inputs.files(generateSimilarityWorkflow.get().outputs)
}

tasks.clean {
delete(srcGenJava)
}
129 changes: 129 additions & 0 deletions similarity/model.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
{
"name": "Similarity",
"parameters": [
{
"name": "granularity",
"type": "boolean"
},
{
"name": "size",
"type": "int"
}
],
"workers": [
{
"name": "tokenizerA",
"type": "Tokenizer",
"arguments": [
"granularity"
]
},
{
"name": "shinglerA",
"type": "Shingler",
"arguments": [
"size"
]
},
{
"name": "tokenizerB",
"type": "Tokenizer",
"arguments": [
"granularity"
]
},
{
"name": "shinglerB",
"type": "Shingler",
"arguments": [
"size"
]
},
{
"name": "vectorAA",
"type": "VectorMultiplier"
},
{
"name": "vectorAB",
"type": "VectorMultiplier"
},
{
"name": "vectorBB",
"type": "VectorMultiplier"
},
{
"name": "cosine",
"type": "CosineSimilarity"
}
],
"channels": [
{
"fromWorker": "tokenizerA",
"toWorker": "shinglerA",
"toPin": "input"
},
{
"fromWorker": "tokenizerB",
"toWorker": "shinglerB",
"toPin": "input"
},
{
"fromWorker": "shinglerA",
"toWorker": "vectorAA",
"toPin": "a"
},
{
"fromWorker": "shinglerA",
"toWorker": "vectorAA",
"toPin": "b"
},
{
"fromWorker": "shinglerA",
"toWorker": "vectorAB",
"toPin": "a"
},
{
"fromWorker": "shinglerB",
"toWorker": "vectorAB",
"toPin": "b"
},
{
"fromWorker": "shinglerB",
"toWorker": "vectorBB",
"toPin": "a"
},
{
"fromWorker": "shinglerB",
"toWorker": "vectorBB",
"toPin": "b"
},
{
"fromWorker": "vectorAA",
"toWorker": "cosine",
"toPin": "aa"
},
{
"fromWorker": "vectorAB",
"toWorker": "cosine",
"toPin": "ab"
},
{
"fromWorker": "vectorBB",
"toWorker": "cosine",
"toPin": "bb"
}
],
"inPins": [
{
"worker": "tokenizerA",
"pin": "input"
},
{
"worker": "tokenizerB",
"pin": "input"
}
],
"outPin": {
"worker": "cosine"
}
}
1 change: 1 addition & 0 deletions similarity/src/main/jinja/workflow.java.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO: create Workflow template!
27 changes: 27 additions & 0 deletions similarity/src/main/python/generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import argparse
import jinja2
import json
from pathlib import Path

parser = argparse.ArgumentParser(description='Generate Java file from template using a JSON model.')
parser.add_argument('input_model', type=str, help='Path to the input JSON model file.')
parser.add_argument('template_file', type=str, help='Path to the template file.')
parser.add_argument('output_file', type=str, help='Path to the output Java file.')

args = parser.parse_args()

output_file = Path(args.output_file)
output_file.parent.mkdir(exist_ok=True, parents=True)

with open(args.input_model, 'r') as file:
model = json.load(file)

for in_pin in model['inPins']:
in_pin['name'] = f"{in_pin['worker']}{in_pin['pin'].capitalize()}"
for channel in model['channels']:
channel['name'] = f"{channel['fromWorker']}_{channel['toWorker']}_{channel['toPin']}"

template_loader = jinja2.FileSystemLoader(searchpath="./")
template_env = jinja2.Environment(loader=template_loader)
template = template_env.get_template(args.template_file)
template.stream(model).dump(args.output_file)

0 comments on commit fe43e68

Please sign in to comment.