Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions LangChain/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
from io import BytesIO
import subprocess

os.environ["OPENAI_API_BASE"] = 'https://api.xiaoai.plus/v1'
os.environ["OPENAI_API_KEY"] = 'sk-8IdpOAGoECRFlaQR0656C1D88d374bEfA3B48bDaBbC21d95'

st.title('LLM-Based Java Concurrent Program to ArkTS Converter')

filename = "ThreadBridge.ets"
Expand All @@ -28,8 +25,8 @@


def run_jar_with_input():
command = 'java -jar target/maven-demo.jar'
os.system(command)
result = subprocess.run(['java', '-jar', 'target/maven-demo.jar'], capture_output=True, text=True)
return result.stdout


def deleteZhu(mycode):
Expand All @@ -47,8 +44,8 @@ def deleteZhu(mycode):


if st.button('run'):
run_jar_with_input()
st.write("use ast success")
ast_result = run_jar_with_input()
st.write(ast_result)
file_path = 'tmp.txt'
with open(file_path, 'r') as file:
code = file.read()
Expand All @@ -61,13 +58,14 @@ def deleteZhu(mycode):
code = ''
with open('classnum.txt', 'r') as file:
class_num = int(file.read())
refer = ("import { setValues, getSyc, getClass, getValues, SynStart, SynEnd, addFunc, Runnable, Thread } from "
refer = ("import { setValues, getSyc, getClass, getValues, SynStart, SynEnd, addFunc, Runnable, Thread, wait, notify } from "
"'./ThreadBridge';\n\n") + code
for i in range(class_num):
class_file_path = 'class'+str(i)+'.txt'
with open(class_file_path, 'r') as file:
class_code = file.read()
class_flag_path = 'class_flag' + str(i) + '.txt'
# st.code(class_code)
with open(class_flag_path, 'r') as file:
class_flag = int(file.read())
print(class_flag)
Expand All @@ -83,27 +81,34 @@ def deleteZhu(mycode):

if flag / 2 == 1:
code = """
import { setValues, getSyc, getClass, getValues, SynStart, SynEnd, addFunc, Runnable, Thread } from './ThreadBridge';
import { setValues, getSyc, getClass, getValues, SynStart, SynEnd, addFunc, Runnable, Thread, wait, notify } from './ThreadBridge';

export function sharedWash(threadId: number){
let archetype = Thread.runnableList[threadId];
archetype.run();
}

""" + code
code = ToTs().getClean(code)
code = deleteZhu(code)
print(code)
with open('Output.ts', 'w') as file:
file.write(code)
st.code(code)
last_code = code
error_flag = 0
for _ in range(3):
process = subprocess.Popen(['tsc', 'Output.ts'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
stdout, stderr = process.communicate()
if len(stdout.decode()) == 0:
error_flag = 0
break
# st.code(stdout.decode())
error_flag = 1
code = deleteZhu(ToTs().getFix(code, stdout.decode()))
# st.code(code)
if error_flag == 1:
code = last_code
print(code)
with open('Output.ts', 'w') as file:
file.write(code)
st.code(code)

st.code("convert finish")
filename = "Index.ets"
Expand Down
39 changes: 15 additions & 24 deletions LangChain/toTaskNew.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,14 @@ def transSyc(self, code):
{code}
你是一个十分擅长Java的程序员。
请将上述Java类做如下更改:找到被synchronized关键字标记的代码块,将synchronized标记删除。在其第一行加上
SynStart(synchronized关键字目前为止的数量, synArray);
在其最后加上SynEnd(synchronized关键字目前为止的数量, synArray); 例如
SynStart(synchronized关键字包裹的对象['synArray']);
在其最后加上SynEnd(synchronized关键字包裹的对象['synArray']);
中间如果有wait()函数,则改成wait(synchronized关键字包裹的对象['synArray'])
中间如果有notify()函数,则改成notify(synchronized关键字包裹的对象['synArray'])例如
{input}
改成
{output}
请注意SynStart(i, synArray),SynEnd(i, synArray)应该在代码块内.
请注意SynStart(synchronized关键字包裹的对象['synArray']),SynEnd(synchronized关键字包裹的对象['synArray'])应该在代码块内.

*********************************************
代码的上下文如下,禁止额外import:
Expand All @@ -138,14 +140,14 @@ def transSyc(self, code):
""")
chain = prompt | self.model | self.output_parser
return chain.invoke({"code": code, "input": """
synchronized(对象){
代码块体
synchronized(obj){
obj.wait();
}
""", "output": """
{
SynStart(1, synArray);
代码块体
SynEnd(1, synArray);
SynStart(obj['synArray']);
wait(obj['synArray']);
SynEnd(obj['synArray']);
}
""", "refer": self.refer})

Expand Down Expand Up @@ -207,7 +209,7 @@ def changeSetGet(self, code):
现有函数getValues(code: any),
作用是传入类型为any的成员变量,返回其对应的值。
只输出代码,不要输出其他分析。

例如
console.log(this.s+"ok");
改为
Expand All @@ -224,7 +226,7 @@ def changeSetGet(self, code):
this.son.begin(this.a, this.b);
改为:
getValues(this.son.begin(getValues(this.a), getValues(this.b)));

还有函数setValues(obj: any, tmp: any),作用是传入类型为any的成员变量obj,将其赋值为对应类型的变量tmp。
使用setValues改变成员变量被赋值的方式,例如
this.a = 1;
Expand All @@ -234,12 +236,12 @@ def changeSetGet(self, code):
this.a = this.b + 1;
改为
setValues(this.a, getValues(this.b) + 1);

*********************************************
代码的上下文如下,禁止额外import:
{refer}
*********************************************

不要额外编写函数。不要对非成员函数做这些。
输出直接输出更改后的类,不要有其他东西。
只输出代码!!!只输出代码!!!只输出代码!!!
Expand All @@ -259,14 +261,7 @@ def addCheng(self, code):
在用户传入的类中添加两个成员变量:
public synArray : any = getSyc();
public sharedType: string = "object";

不要额外编写函数,若已存在上述成员变量,将其替换为上述变量。
例如:
public synArray : any = [];
public sharedType: string = "object";
改为:
public synArray : any = getSyc();
public sharedType: string = "object";

输出直接输出更改后的类,请去除开头和结尾的```标记。不要输出其他信息。
只输出代码!!!只输出代码!!!只输出代码!!!
"""
Expand Down Expand Up @@ -307,7 +302,3 @@ def run(self, code):
# st.code(code)
return code


# with open('../tests/Syn.java', 'r') as file:
# mycode = file.read()
# print(ToTaskNew().changeSetGet(mycode))
2 changes: 1 addition & 1 deletion LangChain/toTs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class ToTs:
def __init__(self):
self.model = ChatOpenAI(temperature=0)
self.model = ChatOpenAI(temperature=0, model="gpt-4o")

self.template_start = """
请将user提供的Java代码格式化,并转换成typescript代码。
Expand Down