|
1 |
| -# Custom Constructors - AcuteML Backend |
2 |
| -## Making a Type and constructor from scratch |
| 1 | +# Custom Constructors |
| 2 | + |
| 3 | +## Adding Custom code to the structures |
| 4 | + |
| 5 | +```julia |
| 6 | +using AcuteML |
| 7 | +# Definition |
| 8 | +@aml mutable struct Student "student" |
| 9 | + |
| 10 | + # add custom code to the begining of creator function |
| 11 | + # the following automatically fills IQ based on the name |
| 12 | + @creator begin |
| 13 | + if occursin("smart", name) |
| 14 | + name = replace(name, "-smart" => "") # remove smart from name |
| 15 | + IQ = "smart" |
| 16 | + elseif occursin("genius", name) |
| 17 | + name = replace(name, "-genius" => "") # remove smart from name |
| 18 | + IQ = "genius" |
| 19 | + else |
| 20 | + error("Give a smart student.") |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + name::String, "~" |
| 25 | + GPA::Float64, "~" |
| 26 | + IQ::UN{String} = nothing, att"~" # default to nothing, but filled automatically by first @cretor macro |
| 27 | + # add custom code to the end of extractor function |
| 28 | + |
| 29 | + log::UN{String} = nothing, "~" |
| 30 | + |
| 31 | + @extractor begin |
| 32 | + if GPA > 4.0 |
| 33 | + log = "A genius with a GPA of $GPA is found" # setting fields using @extractor only changes the field and not the xml code |
| 34 | + end |
| 35 | + end |
| 36 | +end |
| 37 | + |
| 38 | +@aml mutable struct MathClass "math-class" |
| 39 | + @creator begin |
| 40 | + GPAsum = 0.0 |
| 41 | + for student in students |
| 42 | + GPAsum = GPAsum + student.GPA |
| 43 | + end |
| 44 | + average = GPAsum / length(students) # fill average field |
| 45 | + end |
| 46 | + |
| 47 | + students::Vector{Student}, "student" |
| 48 | + average::UN{Float64} = nothing, "average" # calculated automatically |
| 49 | +end |
| 50 | + |
| 51 | +################################################################ |
| 52 | +# Creation |
| 53 | +smarts = [Student(name = "Jack-smart", GPA = 2.0), Student(name = "Sara-genius", GPA = 5.0)] |
| 54 | +mathclass = MathClass(students = smarts) |
| 55 | + |
| 56 | +mathclass.students[1].name # "Jack" |
| 57 | +mathclass.students[2].name # "Sara" |
| 58 | +mathclass.average # 3.5 |
| 59 | + |
| 60 | +pprint(mathclass) |
| 61 | +# <math-class> |
| 62 | +# <student IQ="smart"> |
| 63 | +# <name>Jack</name> |
| 64 | +# <GPA>2.0</GPA> |
| 65 | +# </student> |
| 66 | +# <student IQ="genius"> |
| 67 | +# <name>Sara</name> |
| 68 | +# <GPA>5.0</GPA> |
| 69 | +# </student> |
| 70 | +# <average>3.5</average> |
| 71 | +# </math-class> |
| 72 | + |
| 73 | +################################################################ |
| 74 | +# Extraction |
| 75 | + |
| 76 | +xml = parsexml(""" |
| 77 | +<math-class> |
| 78 | + <student> |
| 79 | + <name>Jack</name> |
| 80 | + <GPA>2.0</GPA> |
| 81 | + </student> |
| 82 | + <student> |
| 83 | + <name>Sara</name> |
| 84 | + <GPA>5.0</GPA> |
| 85 | + </student> |
| 86 | + <average>3.5</average> |
| 87 | + </math-class> |
| 88 | +""") |
| 89 | + |
| 90 | +mathclass = MathClass(xml) |
| 91 | + |
| 92 | +mathclass.students[2].log # "A genius with a GPA of 5.0 is found" |
| 93 | +``` |
| 94 | + |
| 95 | +## Making a Type and constructor from scratch using AcuteML Backend |
3 | 96 |
|
4 | 97 | You can use AcuteML utilities to define custom type constructors from scratch or to override `@aml` defined constructors.
|
5 | 98 |
|
|
0 commit comments