Example 1: class Student: def __init__(self, name, num, age, degree): self.Name = name self.Num = num self.Age = age self.Degree = degree def Degree_F(self, n): self.Degree += n def Birthday_F(self): self.Age += 1 return self.Age A1 = Student("Jorge", "123", 18, 80) X = A1.Birthday_F() print(X) Output, AgileUML: class Student { static operation newStudent() : Student pre: true post: Student->exists( _x | result = _x ); attribute Name : int := name; attribute Num : OclAny := num; attribute Age : OclAny := age; attribute Degree : OclAny := degree; operation initialise(name : OclAny,num : OclAny,age : OclAny,degree : OclAny) : Student pre: true post: true activity: self.Name := name ; self.Num := num ; self.Age := age ; self.Degree := degree; return self; operation Degree_F(n : OclAny) pre: true post: true activity: self.Degree := self.Degree + n; operation Birthday_F() : OclAny pre: true post: true activity: self.Age := self.Age + 1 ; return self.Age; } class FromPython { operation initialise() pre: true post: true activity: skip ; var A1 : Student := (Student.newStudent()).initialise("Jorge", "123", 18, 80) ; var X : OclAny := A1.Birthday_F() ; execute (X)->display(); } accuracy: classes -- 1/1 attributes -- 4/4 operations -- 4/4 statements -- 9/10 total = 18/19 = 0.95 consistency: 9/9 = 1.0 Example 1 Output, trained LLM: class Student { static operation newStudent() : Student pre: true post: Student->exists( _x | result = _x ); attribute Name : OclAny := name; attribute Num : OclAny := num; attribute Age : OclAny := age; attribute Degree : OclAny := degree; operation initialise(name : OclAny,num : OclAny,age : OclAny,degree : OclAny) : Student pre: true post: true activity: self.Name := name ; self.Num := num ; self.Age := age ; self.Degree := degree; return self; operation Degree_F(n : OclAny) pre: true post: true activity: self.Degree := self.Degree + n; operation Birthday_F() : OclAny pre: true post: true activity: self.Age := self.Age + 1 ; return self.Age; } class FromPython { operation initialise() pre: true post: true activity: skip ; var A1 : Student := (Student.newStudent()).initialise("Jorge", "123", 18, 80) ; var X : OclAny := A1.Birthday_F() ; execute (X)->display(); } accuracy: classes -- 1/1 attributes -- 4/4 operations -- 1/4 statements -- 7/10 total = 13/19 = 0.68 consistency: 9/9 = 1.0 LLM (untrained): -- Class Student representing a student entity context Student -- Invariants on attributes: -- The student’s name should not be null and must have at least one character. inv NameIsValid: self.Name <> null and self.Name.size() > 0 -- The student’s number should be a string of digits and should not be null. inv NumIsValid: self.Num <> null and self.Num.matches('[0-9]+') -- The student's age should be a positive integer greater than 0. inv AgeIsValid: self.Age > 0 -- The student’s degree must be a non-negative integer. inv DegreeIsValid: self.Degree >= 0 -- Operation Degree_F modifies the Degree attribute by incrementing it by n. -- n must be a non-negative integer and Degree should remain non-negative. context Student::Degree_F(n: Integer) pre NIsNonNegative: n >= 0 post DegreeUpdated: self.Degree = self.Degree@pre + n post DegreeIsNonNegative: self.Degree >= 0 -- Operation Birthday_F increments Age by 1. -- The Age should always increase by exactly 1. context Student::Birthday_F() post AgeIncremented: self.Age = self.Age@pre + 1 post AgeIsPositive: self.Age > 0 -- Instance A1 -- The initial age of A1 (Jorge) is 18, and it increases by 1 after Birthday_F. context A1:Student inv InitialAgeIsEighteen: self.Age = 18 accuracy: classes -- 1/1 attributes -- 3/4 operations -- 2/4 statements -- 3/10 total = 9/19 consistency: 7/14 Example 2: A = 15*3 B = 12 A += 10 B *= 12 print(A) print(B) M = [11, 12, 13, 14, 15] Result = { X : X*X for X in M } print(Result) Output (AgileUML): class FromPython { operation initialise() pre: true post: true activity: var A : int := 15 * 3 ; var B : int := 12 ; A := A + 10 ; B := B * 12 ; execute (A)->display() ; execute (B)->display() ; var M : Sequence := Sequence{11}->union(Sequence{12}->union(Sequence{13}->union(Sequence{14}->union(Sequence{ 15 })))) ; var Result : Map := M->select(X | true)->collect(X | Map{X |-> X * X})->unionAll() ; execute (Result)->display(); } accuracy -- classes 1/1 functions 1/1 variables 4/4 statements 9/9 total = 15/15 = 1.0 consistency -- 1.0 11/11 Output (trained LLM): class FromPython { operation initialise() pre: true post: true activity: var A : int := 15 * 3 ; var B : int := 12 ; A := A + 10 ; B := B * 12 ; execute (A)->display() ; execute (B)->display() ; var M : Sequence := Sequence{11}->union(Sequence{12}->union(Sequence{13}->union(Sequence{14}->union(Sequence{ 15 })))) ; var Result : Map := M->select(X | true)->collect(X | Map{ X |-> X * X })->unionAll() ; execute (Result)->display(); } accuracy -- classes 1/1 functions 1/1 variables 4/4 statements 8/9 total = 14/15 = 0.93 consistency -- 10/11 (1 spurious element) Untrained LLM: -- Initial variable definitions context Calculation inv : let A : Integer = 15 * 3 in let B : Integer = 12 in let A_updated : Integer = A + 10 in let B_updated : Integer = B * 12 in -- Ensure the values of A and B after updates A_updated = 55 and B_updated = 144 -- List of integers M context List inv : let M : Sequence(Integer) = Sequence{11, 12, 13, 14, 15} in -- Computation of Result which is the square of each element in M let Result : Set(Integer) = M->collect(X | X * X) in -- Ensure that the Result is correctly calculated Result = Set{121, 144, 169, 196, 225} Classes 0/1 Functions 0/1 Variables 4/4 Statements 6/9 Total = 10/15 Consistency 4/9 Example 3: def fun_1(A, B) : Res = A*B print(Res) fun_1(20,2) Output (AgileUML): class FromPython { operation initialise() pre: true post: true activity: skip ; fun1(20, 2); operation fun1(A : OclAny, B : OclAny) pre: true post: true activity: var Res : double := A * B ; execute (Res)->display(); } accuracy -- classes 1/1 functions 2/2 variables 1/1 statements 3/3 total = 1.0, 7/7 consistency = 1.0, 6/6 Output (Trained LLM): class FromPython { operation initialise() pre: true post: true activity: skip ; fun_1(20, 2); operation fun_1(A : OclAny, B : OclAny) pre: true post: true activity: var Res : double := A * B ; execute (Res)->display(); } accuracy -- classes 1/1 functions 2/2 variables 1/1 statements 2/3 total = 0.86 consistency = 1.0, 6/6 Untrained LLM: -- Context: Specification of the function fun_1 context fun_1(A: Integer, B: Integer) : Integer post: -- Ensure that the result (Res) is the product of A and B Res = A * B Classes 0/1 Functions 1/2 Variables 1/1 Statements 1/3 Total 3/7 Consistency 2/2 -------------------------------------------------------- Example 4: A = 11 if A > 10 : B = A*A elif A > 0 : B = A*A*A else : B = -A print(B) Output (AgileUML): class FromPython { operation initialise() pre: true post: true activity: var A : int := 11 ; if A > 10 then ( var B : int := A * A ) else (if A > 0 then ( B := A * A * A ) else ( B := -A ) ) ; execute (B)->display(); } Classes: 1/1 Variables: 2/2 Operations: 1/1 Statements: 7/7 Accuracy: 1.0, 11/11 Consistency: 1.0, 9/9 Output (Trained LLM): class FromPython { operation initialise() pre: true post: true activity: var A : int := 11 ; if A > 10 then ( var B : int := A * A ) else (if A > 0 then ( B := A * A * A ) else ( B := -A ) ) ; execute (B)->display(); } Classes: 1/1 Variables: 2/2 Operations: 1/1 Statements: 7/7 Accuracy: 1.0, 11/11 Consistency: 1.0, 9/9 Untrained LLM: context A : Integer inv: let B : Integer = if A > 10 then A * A else if A > 0 then A * A * A else -A endif endif Classes 0/1 Variables 2/2 Operations 0/1 Statements 5/7 Total = 7/11 Consistency 6/6 -------------------------------------------------------- Example 5: file1 = open('MyFile', 'r') content = file1.read() file1.close() file2 = open('MyFile', 'a') file2.write(content) Output (AgileUML): class FromPython { operation initialise() pre: true post: true activity: var file1 : OclFile := OclFile.newOclFile_Write(OclFile.newOclFile('MyFile')) ; var content : String := file1.readAll() ; file1.closeFile() ; var file2 : OclFile := OclFile.newOclFile_Write(OclFile.newOclFile('MyFile')) ; file2.write(content); } Accuracy, consistency both 1.0, 10/10 Output (Trained LLM): class FromPython { operation initialise() pre: true post: true activity: var file1 : OclFile := OclFile.newOclFile_Write(OclFile.newOclFile('MyFile')) ; var content : String := file1.readAll() ; file1.closeFile() ; var file2 : OclFile := OclFile.newOclFile_Write(OclFile.newOclFile('MyFile')) ; file2.write(content); } Classes: 1/1 Operations: 1/1 Variables: 3/3 Statements: 4/5 (should be readAll, not read) Accuracy: 9/10 Consistency: 6/7 Untrained LLM: context File -- file1 is opened in read mode and should not be null inv File1IsOpenInReadMode: file1.mode = 'r' and file1 <> null -- file2 is opened in append mode and should not be null inv File2IsOpenInAppendMode: file2.mode = 'a' and file2 <> null -- After reading from file1, content should not be null context File::read() post ContentIsNotNull: content <> null -- Content written to file2 should be the same as the content read from file1 context File::write(content) pre File1IsClosed: file1.isClosed = true post ContentIsWritten: file2.content = content -- After reading the file, file1 should be closed context File::close() pre File1IsOpen: file1.isOpen = true post File1IsClosed: file1.isClosed = true Classes 0/1 Operations 0/1 Variables 3/3 Statements 0/5 Accuracy = 3/10 Consistency = 0/7 -------------------------------------------------------- Example 6: Dict1 = dict({ "A" : 11, "B" : 12, "C" : 13 }) for i in Dict1 : print(i) Output (AgileUML): class FromPython { operation initialise() pre: true post: true activity: var Dict1 : Map := (Map{ "A" |-> 11 }->union(Map{ "B" |-> 12 }->union(Map{ "C" |-> 13 }))) ; for i : Dict1->keys() do ( execute (i)->display()); } Accuracy = 1 6/6, consistency = 1 5/5 Output (Trained LLM): class FromPython { operation initialise() pre: true post: true activity: var Dict1 : Map := (Map{ "A" |-> 11 }->union(Map{ "B" |-> 12 }->union(Map{ "C" |-> 13 }))) ; for i : Dict1->keys() do ( execute (i)->display()); } Classes: 1/1 Variables: 2/2 Statements: 3/3 Accuracy = 6/6 Consistency = 5/5 Untrained LLM: context Dict1 : Map(String, Integer) inv: Dict1 = Map{'A' -> 11, 'B' -> 12, 'C' -> 13} -- Loop over keys Dict1.keySet()->forAll(i | -- Behavior can be specified here for each key (i.e., printing) i <> null -- Replace with print logic in a real system ) Classes 0/1 Operations 0/1 Variables 2/2 Statements 2/3 Accuracy = 4/7 Consistency = 4/5 -------------------------------------------------------- Example 7: String1 = "Hello World" for i in String1 : print(i) Output (AgileUML): class FromPython { operation initialise() pre: true post: true activity: var String1 : String := "Hello World" ; for i : String1->characters() do ( execute (i)->display()); } Accuracy = 1 5/5, consistency = 1, 5/5 Output (Trained LLM): class FromPython { operation initialise() pre: true post: true activity: var String1 : String := "Hello World" ; for i : String1->characters() do ( execute (i)->display()); } Classes: 1/1 Operations: 1/1 Variables: 2/2 Statements: 3/3 Accuracy = 9/9 Consistency = 5/5 Untrained LLM: context String1: String inv: let characters: Sequence(Character) = self.asSequence() in characters->forAll(c | print(c)) Classes 0/1 Variables 3/3 Operations 0/1 Statements 2/3 Accuracy 5/8 Consistency 3/3 -------------------------------------------------------- Example 8: def Func_1(A = 10) : B = 100 C = A return B*C Output (AgileUML): class FromPython { operation initialise() pre: true post: true activity: skip; operation Func1(A : int) : OclAny pre: true post: true activity: if A->oclIsUndefined() then A := 10 else skip; var B : int := 100 ; var C : int := A ; return B * C; } Classes: 1/1 Operations: 2/2 Variables: 3/3 Statements: 3/3 Accuracy: 9/9, 1.0 Consistency: 6/6 Output (Trained LLM): class FromPython { operation initialise() pre: true post: true activity: skip; operation Func_1(A : int) : OclAny pre: true post: true activity: if A->oclIsUndefined() then A := 10 else skip; var B : int := 100 ; var C : int := A ; return B * C; } Classes: 1/1 Operations: 1/2 Variables: 3/3 Statements: 3/3 Accuracy: 8/9 Consistency: 5/6 Untrained LLM: context Func_1(A: Integer = 10): Integer body: let B: Integer = 100 in let C: Integer = A in B * C Classes 0/1 Variables 3/3 Operations 1/1 Statements 3/3 Accuracy 7/8 Consistency 4/4 -------------------------------------------------------- Example 9: class Class1: def Func_1(self): print("We are in Class1") class Class2(Class1): def Func_1(self): print("We are in Class2") class Class3(Class1, Class2): pass Obj = Class3() Obj.Func_1() Output (AgileUML): class Class1 { static operation newClass1() : Class1 pre: true post: Class1->exists( _x | result = _x ); operation Func1() pre: true post: true activity: execute ("We are in Class1")->display(); } class Class2 extends Class1 { static operation newClass2() : Class2 pre: true post: Class2->exists( _x | result = _x ); operation Func1() pre: true post: true activity: execute ("We are in Class2")->display(); } class Class3 extends Class1, Class2 { static operation newClass3() : Class3 pre: true post: Class3->exists( _x | result = _x ); } class FromPython { operation initialise() pre: true post: true activity: skip ; skip ; skip ; var Obj : Class3 := (Class3.newClass3()).initialise() ; Obj.Func1(); } classes: 3/3 functions: 4/4 variables: 1/1 statements: 4/4 total = 12/12 = 1.0 Consistency = 17/17 Output (Trained LLM): "Error in the result" class Class1 { static operation newClass1() : Class1 pre: true post: Class1->exists( _x | result = _x ); operation Func_1() pre: true post: true activity: execute ("We are in Class1")->display(); } class Class2 extends Class1 { static operation newClass2() : Class2 pre: true post: Class2->exists( _x | result = _x ); operation Func_1() pre: true post: true activity: execute ("We are in Class2")->display(); } class Class3 extends Class1, Class2 { static operation newClass3() : Class3 pre: true post: Class3->exists( _x | result = _x ); operation initialise() pre: true post: true activity: skip; } class FromPython { operation initialise() pre: true post: true activity: skip ; skip ; skip ; var Obj : Class3 := (Class3.newClass3()).initialise() ; Obj.Func_1(); } classes: 3/3 functions: 3/4 variables: 1/1 statements: 1/4 total = 8/12 = 0.67 consistency: 11/26 = 0.423 Untrained LLM: -- Definition of Class1 context Class1 inv: self.oclIsKindOf(Class1) -- Class1 has a method Func_1 context Class1::Func_1() : String post: result = 'We are in Class1' -- Definition of Class2, which inherits from Class1 context Class2 inv: self.oclIsKindOf(Class2) and self.oclIsKindOf(Class1) -- Class2 overrides Func_1 from Class1 context Class2::Func_1() : String post: result = 'We are in Class2' -- Definition of Class3, which inherits from both Class1 and Class2 context Class3 inv: self.oclIsKindOf(Class3) and self.oclIsKindOf(Class1) and self.oclIsKindOf(Class2) -- Method resolution in Class3: -- Class3 inherits Func_1 from Class2, as per Python's MRO (Method Resolution Order) context Class3::Func_1() : String post: result = 'We are in Class2' -- The object 'Obj' is of type Class3 context Obj : Class3 inv: Obj.oclIsTypeOf(Class3) -- Method call for Obj.Func_1() context Obj::Func_1() : String post: result = 'We are in Class2' Classes 3/3 Variables 1/1 Operations 3/4 Statements 4/4 Accuracy 11/12 Consistency 8/8 -------------------------------------------------------- Example 10: A = -15.7 B = abs(A) print(B) Output (AgileUML): class FromPython { operation initialise() pre: true post: true activity: var A : double := -15.7 ; var B : double := (A)->abs() ; execute (B)->display(); } Accuracy, consistency = 7/7 Output (Trained LLM): class FromPython { operation initialise() pre: true post: true activity: var A : double := -15.7 ; var B : double := (A)->abs() ; execute (B)->display(); } classes: 1/1 operations: 1/1 variables: 2/2 statements: 3/3 total = 1.0, 7/7 consistency = 1.0, 5/5 Untrained LLM: -- Variable A is defined as a Real number context A : Real inv: A = -15.7 -- The variable B is the absolute value of A context B : Real inv: B = A.abs() -- Ensure that the absolute value of A is computed correctly context B inv: B = 15.7 Classes 0/1 Operations 0/1 Variables 2/2 Statements 3/3 Accuracy 5/7 Consistency 5/6 -------------------------------------------------------- Example 11: String1 = "Hello World" String2 = String1.capitalize() String3 = String1.replace(" ", "*") Output (AgileUML): class FromPython { operation initialise() pre: true post: true activity: var String1 : String := "Hello World" ; var String2 : String := StringLib.capitalise(String1) ; var String3 : String := String1.replace(" ", "*"); } Accuracy, consistency = 8/8 Output (Trained LLM): class FromPython { operation initialise() pre: true post: true activity: var String1 : String := "Hello World" ; var String2 : String := String1->capitalize() ; var String3 : String := String1.replace(" ", "*"); } accuracy: classes 1/1 operations 1/1 variables 3/3 statements 3/3 = 1.0, 8/8 consistency = 1.0, 5/5 Untrained LLM: context String1: String inv: -- String1 is initialized with the value "Hello World" String1 = "Hello World" context String2: String inv: -- String2 is the result of capitalizing String1 String2 = String1.toLower().substring(0, 1).toUpper().concat(String1.substring(1).toLower()) context String3: String inv: -- String3 is the result of replacing spaces in String1 with "*" String3 = String1.replace(" ", "*") Classes 0/1 Operations 0/1 Variables 3/3 Statements 2/3 Accuracy 5/8 Consistency 5/6 -------------------------------------------------------- Example 12: import math Sequence1 = ["aa", "bb", "cc"] Length1 = len(Sequence1) A = math.sin(5.0) B = math.pow(Length1,2) print(A) print(B) Output (AgileUML): class FromPython { operation initialise() pre: true post: true activity: skip ; var Sequence1 : Sequence := Sequence{"aa"}->union(Sequence{"bb"}->union(Sequence{ "cc" })) ; var Length1 : int := (Sequence1)->size() ; var A : double := (5.0)->sin() ; var B : double := (Length1)->pow(2) ; execute (A)->display() ; execute (B)->display(); } Accuracy = consistency = 12/12 Output (Trained LLM): class FromPython { operation initialise() pre: true post: true activity: skip ; var Sequence1 : Sequence := Sequence{"aa"}->union(Sequence{"bb"}->union(Sequence{ "cc" })) ; var Length1 : int := (Sequence1)->size() ; var A : double := (5.0)->sin() ; var B : double := ((Length1))->pow(2) ; execute (A)->display() ; execute (B)->display(); } accuracy: classes 1/1 functions 1/1 variables 4/4 statements 6/6 total = 1.0, 12/12 consistency = 1.0, 8/8 Untrained LLM: context Sequence1: Sequence(String) inv: -- Sequence1 is initialized with three elements: "aa", "bb", "cc" Sequence1 = Sequence{'aa', 'bb', 'cc'} context Length1: Integer inv: -- Length1 represents the length of Sequence1 Length1 = Sequence1->size() context A: Real inv: -- A is the sine of 5.0 A = sin(5.0) context B: Real inv: -- B is the square of Length1 B = Length1 ^ 2 Classes 0/1 Operations 0/1 Variables 4/4 Statements 3/6 Accuracy 7/12 Consistency 7/8 -------------------------------------------------------- Example 13: i = 0 while i < 100 : i = i + 1 print(i*i) Output (AgileUML): class FromPython { operation initialise() pre: true post: true activity: var i : int := 0 ; while i < 100 do ( i := i + 1 ; execute (i * i)->display()); } Classes: 1/1 Operations: 1/1 Variables: 1/1 Statements: 4/4 Accuracy = 9/9 Consistency: 6/6 Output (Trained LLM): class FromPython { operation initialise() pre: true post: true activity: var i : int := 0 ; while i < 100 do ( i := i + 1 ; execute (i * i)->display()); } Classes: 1/1 Operations: 1/1 Variables: 1/1 Statements: 4/4 Accuracy = 9/9 Consistency: 6/6 Untrained LLM: -- Context: Describing the behavior of the loop and its constraints context LoopOperation -- Initial condition: i starts at 0 inv Initialization: i = 0 -- Loop invariant: While i is less than 100, the loop continues inv LoopCondition: i >= 0 and i <= 100 -- Postcondition: After each iteration, i is incremented by 1 post Increment: i = i@pre + 1 -- Iteration constraint: For each iteration, print the square of the current i inv Iteration: i >= 0 and i <= 100 implies print(i * i) -- Final state: Once i reaches 100, the loop terminates post FinalState: i = 100 Classes 0/1 Operations 0/1 Variables 1/1 Statements 2/4 Accuracy 3/7 Consistency 4/5 -------------------------------------------------------- Example 14: import pandas as pd titanic = pd.read_csv("Data/Data1.csv") Marks = Data1["Marks"] mark = Marks.mean() print(mark) Output (AgileUML): class FromPython { operation initialise() pre: true post: true activity: skip ; var titanic : Map := OclFile.newOclFile_Read(OclFile.newOclFile(("Data/Data1.csv"))).readMap() ; var Marks : OclAny := Data1->at("Marks") ; var mark : double := MathLib.mean(Marks) ; execute (mark)->display(); } Classes: 1/1 Operations: 1/1 Variables: 3/3 Statements: 4/4 Accuracy = 9/9 Consistency: 6/6 Output (Trained LLM): class FromPython { operation initialise() pre: true post: true activity: skip ; var titanic : Map := (OclFile.newOclFile_Write(OclFile.newOclFile("Data/Data1.csv"))).readMap() ; var Marks : OclAny := titanic.get("Marks") ; var mark : double := Marks->mean() ; execute (mark)->display(); } Classes: 1/1 Operations: 1/1 Variables: 2/3 Statements: 1/4 Accuracy = 5/9 Consistency: 3/6 Untrained LLM: -- Context: TitanicData operations on reading CSV and calculating the mean context TitanicData -- Precondition: The CSV file "Data/Data1.csv" must exist and be readable pre FileExistence: self.filePath = "Data/Data1.csv" and self.isReadable() -- Postcondition: The data must be loaded into the titanic DataFrame post DataLoaded: not titanic.isEmpty() -- Marks column must exist in the DataFrame inv ColumnExistence: titanic.hasColumn("Marks") -- Invariant: The "Marks" column contains only numeric values inv MarksIsNumeric: titanic["Marks"]->forAll(m | m.oclIsTypeOf(Integer) or m.oclIsTypeOf(Real)) -- Postcondition: The mean of the "Marks" column is correctly calculated post MeanCalculated: mark = titanic["Marks"]->sum() / titanic["Marks"]->size() -- Postcondition: The mean is printed after calculation post MeanPrinted: print(mark) Classes 0/1 Operations 0/1 Variables 2/3 Statements 3/4 Accuracy 5/9 Consistency 6/12 --------------------------------------------------------