Skip to content

Commit c5346aa

Browse files
authored
Merge pull request #27 from wangchaoHZ/dev
fix model generator print data attribute definitions
2 parents 48093f4 + 59b3962 commit c5346aa

File tree

1 file changed

+85
-14
lines changed

1 file changed

+85
-14
lines changed

scl/static_model_generator.go

Lines changed: 85 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -948,13 +948,14 @@ func (s *StaticModelGenerator) printDataObjectDefinitions(lnName string, dataObj
948948
s.printDataObjectDefinitions(doName, dataObject.SubDataObjects, firstDataAttributeName, isDoTransient)
949949
}
950950
if len(dataObject.DataAttributes) > 0 {
951-
s.printDataAttributeDefinitions(doName, dataObject.DataAttributes, isDoTransient)
951+
s.printDataAttributeDefinitions(doName, dataObject.DataAttributes, isDoTransient, -1)
952952
}
953953
}
954954
}
955955

956-
func (s *StaticModelGenerator) printDataAttributeDefinitions(doName string, dataAttributes []*DataAttribute, isTransient bool) {
956+
func (s *StaticModelGenerator) printDataAttributeDefinitions(doName string, dataAttributes []*DataAttribute, isTransient bool, arrayIdx int) {
957957
for i, dataAttribute := range dataAttributes {
958+
isArray := dataAttribute.Count > 0
958959
daName := doName + "_" + dataAttribute.Name
959960

960961
// Handle FunctionalConstraint "SE"
@@ -972,7 +973,7 @@ func (s *StaticModelGenerator) printDataAttributeDefinitions(doName string, data
972973
s.cOut.println(" \"%s\",", dataAttribute.Name)
973974
s.cOut.println(" (ModelNode*) &%s,", doName)
974975

975-
// Sibling node
976+
// Print pointer to sibling node
976977
if i < len(dataAttributes)-1 {
977978
sibling := dataAttributes[i+1]
978979
siblingDoName := doName
@@ -986,15 +987,23 @@ func (s *StaticModelGenerator) printDataAttributeDefinitions(doName string, data
986987
s.cOut.println(" NULL,")
987988
}
988989

989-
// First sub-data attribute
990-
if len(dataAttribute.SubDataAttributes) > 0 {
991-
s.cOut.println(" (ModelNode*) &%s_%s,", daName, dataAttribute.SubDataAttributes[0].Name)
990+
if isArray {
991+
// For arrays, the first child is array element 0
992+
s.cOut.println(" (ModelNode*) &%s_0,", daName)
993+
s.cOut.println(" %d,", dataAttribute.Count)
994+
s.cOut.println(" -1,") // arrayIdx
992995
} else {
993-
s.cOut.println(" NULL,")
996+
// For non-arrays, print pointer to the first sub-data attribute if exists
997+
if len(dataAttribute.SubDataAttributes) > 0 {
998+
s.cOut.println(" (ModelNode*) &%s_%s,", daName, dataAttribute.SubDataAttributes[0].Name)
999+
} else {
1000+
s.cOut.println(" NULL,")
1001+
}
1002+
s.cOut.println(" %d,", dataAttribute.Count)
1003+
s.cOut.println(" -1,") // arrayIdx
9941004
}
9951005

996-
// Print Count, FunctionalConstraint, and Type
997-
s.cOut.println(" %d,", dataAttribute.Count)
1006+
// Print FunctionalConstraint and Type
9981007
s.cOut.println(" IEC61850_FC_%s,", dataAttribute.FC)
9991008
s.cOut.println(" IEC61850_%s,", dataAttribute.AttributeType.ToString())
10001009

@@ -1019,7 +1028,7 @@ func (s *StaticModelGenerator) printDataAttributeDefinitions(doName string, data
10191028

10201029
s.cOut.println(" NULL,")
10211030

1022-
// Short address
1031+
// Print short address if valid
10231032
shortAddr := int64(0)
10241033
if addr, err := cast.ToInt64E(dataAttribute.ShortAddress); err == nil {
10251034
shortAddr = addr
@@ -1029,12 +1038,74 @@ func (s *StaticModelGenerator) printDataAttributeDefinitions(doName string, data
10291038
s.cOut.println(" %d", shortAddr)
10301039
s.cOut.println("};\n")
10311040

1032-
// Recursive call for sub-data attributes
1033-
if len(dataAttribute.SubDataAttributes) > 0 {
1034-
s.printDataAttributeDefinitions(daName, dataAttribute.SubDataAttributes, isTransient)
1041+
// If array, print each array element definition
1042+
if isArray {
1043+
for idx := 0; idx < dataAttribute.Count; idx++ {
1044+
arrayElementdaName := fmt.Sprintf("%s_%d", daName, idx)
1045+
s.variablesList = append(s.variablesList, arrayElementdaName)
1046+
1047+
s.cOut.println("DataAttribute %s = {", arrayElementdaName)
1048+
s.cOut.println(" DataAttributeModelType,")
1049+
s.cOut.println(" NULL,")
1050+
s.cOut.println(" (ModelNode*) &%s,", daName)
1051+
1052+
// Print pointer to next array element or NULL if last
1053+
if idx != dataAttribute.Count-1 {
1054+
nextArrayElementdaName := fmt.Sprintf("%s_%d", daName, idx+1)
1055+
s.cOut.println(" (ModelNode*) &%s,", nextArrayElementdaName)
1056+
} else {
1057+
s.cOut.println(" NULL,")
1058+
}
1059+
1060+
// Print pointer to first sub-data attribute if exists
1061+
if len(dataAttribute.SubDataAttributes) > 0 {
1062+
s.cOut.println(" (ModelNode*) &%s_%s,", arrayElementdaName, dataAttribute.SubDataAttributes[0].Name)
1063+
} else {
1064+
s.cOut.println(" NULL,")
1065+
}
1066+
1067+
s.cOut.println(" 0,") // count for array element
1068+
s.cOut.println(" %d,", idx) // index of array element
1069+
1070+
s.cOut.println(" IEC61850_FC_%s,", dataAttribute.FC)
1071+
s.cOut.println(" IEC61850_%s,", dataAttribute.AttributeType.ToString())
1072+
1073+
// Print trigger options for array element
1074+
s.cOut.print(" 0")
1075+
if trgOps != nil {
1076+
if trgOps.Dchg {
1077+
s.cOut.print(" + TRG_OPT_DATA_CHANGED")
1078+
}
1079+
if trgOps.Dupd {
1080+
s.cOut.print(" + TRG_OPT_DATA_UPDATE")
1081+
}
1082+
if trgOps.Qchg {
1083+
s.cOut.print(" + TRG_OPT_QUALITY_CHANGED")
1084+
}
1085+
}
1086+
if isTransient {
1087+
s.cOut.print(" + TRG_OPT_TRANSIENT")
1088+
}
1089+
s.cOut.println(",")
1090+
1091+
s.cOut.println(" NULL,")
1092+
// sAddr is always zero for array element
1093+
s.cOut.print(" 0")
1094+
s.cOut.println("};\n")
1095+
1096+
// Recursively print sub-data attributes for each array element
1097+
if len(dataAttribute.SubDataAttributes) > 0 {
1098+
s.printDataAttributeDefinitions(arrayElementdaName, dataAttribute.SubDataAttributes, isTransient, -1)
1099+
}
1100+
}
1101+
} else {
1102+
// Recursively print sub-data attributes for non-array attribute
1103+
if len(dataAttribute.SubDataAttributes) > 0 {
1104+
s.printDataAttributeDefinitions(daName, dataAttribute.SubDataAttributes, isTransient, -1)
1105+
}
10351106
}
10361107

1037-
// Process value
1108+
// Process value if present or fetch default value from definition
10381109
value := dataAttribute.Value
10391110
if value == nil && dataAttribute.Definition != nil {
10401111
value = dataAttribute.Definition.Value

0 commit comments

Comments
 (0)