From 322169e823a6b3658759a892303a2de0a90591c4 Mon Sep 17 00:00:00 2001 From: George Mamais Date: Sun, 8 Sep 2024 20:46:02 +0300 Subject: [PATCH] ICD Improvements --- BackendAst/DAstACN.fs | 32 +++++++---- BackendAst/DAstUtilFunctions.fs | 73 +++++++++++++++++++++++++ BackendAst/GenerateAcnIcd.fs | 89 ++++++++++++++++++++----------- FrontEndAst/AcnCreateFromAntlr.fs | 2 +- StgVarious/icdtemplate_acn.stg | 2 +- asn1scc/Program.fs | 2 +- 6 files changed, 157 insertions(+), 43 deletions(-) diff --git a/BackendAst/DAstACN.fs b/BackendAst/DAstACN.fs index 6cb123d70..e8740744a 100644 --- a/BackendAst/DAstACN.fs +++ b/BackendAst/DAstACN.fs @@ -258,6 +258,8 @@ let private createAcnFunction (r: Asn1AcnAst.AstRoot) | _ -> None let errCodeName = ToC ("ERR_ACN" + (codec.suffix.ToUpper()) + "_" + ((t.id.AcnAbsPath |> Seq.skip 1 |> Seq.StrJoin("-")).Replace("#","elm"))) let errCode, ns = getNextValidErrorCode us errCodeName None + //if t.id.AsString.EndsWith "ALPHA-DELETE-DIAGNOSTIC-PARAMETER-REPORT-STRUCTURES-GENERIC" then + // printfn "debug" let nMaxBytesInACN = BigInteger (ceil ((double t.acnMaxSizeInBits)/8.0)) let nMinBytesInACN = BigInteger (ceil ((double t.acnMinSizeInBits)/8.0)) let soInitFuncName = getFuncNameGeneric typeDefinition (lm.init.methodNameSuffix()) @@ -1251,9 +1253,9 @@ let createSequenceOfFunction (r:Asn1AcnAst.AstRoot) (deps:Asn1AcnAst.AcnInserted lengthRow@chRows@[THREE_DOTS]@lastChRows@terminationPattern, [] | false -> let sType = TypeHash childIcdTas.hash - let a1 = {IcdRow.fieldName = fieldName; comments = comments; sPresent=sPresent;sType=sType; sConstraint=None; minLengthInBits = t.acnMinSizeInBits; maxLengthInBits=t.acnMaxSizeInBits;sUnits=None; rowType = IcdRowType.LengthDeterminantRow; idxOffset = Some (lengthRow.Length + 1)} - let a2 = {a1 with idxOffset = Some ((int o.maxSize.acn)+lengthRow.Length)} - [a1;THREE_DOTS;a2], [childIcdTas] + let a1 = {IcdRow.fieldName = "Item #1"; comments = comments; sPresent=sPresent;sType=sType; sConstraint=None; minLengthInBits = child.acnMinSizeInBits; maxLengthInBits=child.acnMaxSizeInBits;sUnits=None; rowType = IcdRowType.LengthDeterminantRow; idxOffset = Some (lengthRow.Length + 1)} + let a2 = {a1 with fieldName = $"Item #{o.maxSize.acn}"; idxOffset = Some ((int o.maxSize.acn)+lengthRow.Length)} + lengthRow@[a1;THREE_DOTS;a2], [childIcdTas] | None -> lengthRow@terminationPattern, [] let sExtraComment = match o.acnEncodingClass with @@ -1422,7 +1424,9 @@ let rec handleSingleUpdateDependency (r:Asn1AcnAst.AstRoot) (deps:Asn1AcnAst.Acn | Some prmUpdateStatement -> let updateFunc (child: AcnChild) (nestingScope: NestingScope) (vTarget : CallerScope) (pSrcRoot : CallerScope) = prmUpdateStatement.updateAcnChildFnc child nestingScope vTarget pSrcRoot - let icdComments = [] + let icdComments = + let aaa = sprintf "reference determinant for %s " (acnPrm.id.AsString) + [aaa] Some ({AcnChildUpdateResult.updateAcnChildFnc = updateFunc; icdComments=icdComments; errCodes=prmUpdateStatement.errCodes; testCaseFnc = prmUpdateStatement.testCaseFnc; localVariables=[]}), ns1 | AcnDepSizeDeterminant (minSize, maxSize, szAcnProp) -> let updateFunc (child: AcnChild) (nestingScope: NestingScope) (vTarget : CallerScope) (pSrcRoot : CallerScope) = @@ -1443,7 +1447,9 @@ let rec handleSingleUpdateDependency (r:Asn1AcnAst.AstRoot) (deps:Asn1AcnAst.Acn let testCaseFnc (atc:AutomaticTestCase) : TestCaseValue option = atc.testCaseTypeIDsMap.TryFind d.asn1Type - let icdComments = [] + let icdComments = + let aaa = sprintf "size determinant for %s " (d.asn1Type.AsString) + [aaa] Some ({AcnChildUpdateResult.updateAcnChildFnc = updateFunc; icdComments=icdComments; errCodes=[]; testCaseFnc=testCaseFnc; localVariables=[]}), us | AcnDepSizeDeterminant_bit_oct_str_contain o -> let baseTypeDefinitionName = @@ -1485,7 +1491,9 @@ let rec handleSingleUpdateDependency (r:Asn1AcnAst.AstRoot) (deps:Asn1AcnAst.Acn let testCaseFnc (atc:AutomaticTestCase) : TestCaseValue option = atc.testCaseTypeIDsMap.TryFind d.asn1Type let localVars = lm.lg.acn.getAcnDepSizeDeterminantLocVars sReqBytesForUperEncoding - let icdComments = [] + let icdComments = + let aaa = sprintf "size determinant for %s " (d.asn1Type.AsString) + [aaa] Some ({AcnChildUpdateResult.updateAcnChildFnc = updateFunc; icdComments=icdComments; errCodes=errCodes0; testCaseFnc=testCaseFnc; localVariables= localVariables0@localVars}), ns | AcnDepIA5StringSizeDeterminant (minSize, maxSize, szAcnProp) -> @@ -1498,7 +1506,9 @@ let rec handleSingleUpdateDependency (r:Asn1AcnAst.AstRoot) (deps:Asn1AcnAst.Acn | _ -> checkAccessPath checkPath updateStatement v (initExpr r lm m child.Type) let testCaseFnc (atc:AutomaticTestCase) : TestCaseValue option = atc.testCaseTypeIDsMap.TryFind d.asn1Type - let icdComments = [] + let icdComments = + let aaa = sprintf "size determinant for %s " (d.asn1Type.AsString) + [aaa] Some ({AcnChildUpdateResult.updateAcnChildFnc = updateFunc; icdComments=icdComments; errCodes=[]; testCaseFnc=testCaseFnc; localVariables=[]}), us | AcnDepPresenceBool -> let updateFunc (child: AcnChild) (nestingScope: NestingScope) (vTarget : CallerScope) (pSrcRoot : CallerScope) = @@ -1515,7 +1525,9 @@ let rec handleSingleUpdateDependency (r:Asn1AcnAst.AstRoot) (deps:Asn1AcnAst.Acn match atc.testCaseTypeIDsMap.TryFind(d.asn1Type) with | Some _ -> Some TcvComponentPresent | None -> Some TcvComponentAbsent - let icdComments = [] + let icdComments = + let aaa = sprintf "Used as a presence determinant for %s " (d.asn1Type.AsString) + [aaa] Some ({AcnChildUpdateResult.updateAcnChildFnc = updateFunc; icdComments=icdComments; errCodes=[]; testCaseFnc=testCaseFnc; localVariables=[]}), us | AcnDepPresence (relPath, chc) -> let icdComments = @@ -1613,7 +1625,9 @@ let rec handleSingleUpdateDependency (r:Asn1AcnAst.AstRoot) (deps:Asn1AcnAst.Acn | _ -> checkAccessPath checkPath updateStatement2 v (initExpr r lm m child.Type) let testCaseFnc (atc:AutomaticTestCase) : TestCaseValue option = atc.testCaseTypeIDsMap.TryFind d.asn1Type - let icdComments = [] + let icdComments = + let aaa = sprintf "Used as a presence determinant for %s " (chc.typeDef[C].asn1Name) + [aaa] Some ({AcnChildUpdateResult.updateAcnChildFnc = updateFunc; icdComments=icdComments; errCodes=[] ; testCaseFnc=testCaseFnc; localVariables=[]}), us and getUpdateFunctionUsedInEncoding (r: Asn1AcnAst.AstRoot) (deps: Asn1AcnAst.AcnInsertedFieldDependencies) (lm: LanguageMacros) (m: Asn1AcnAst.Asn1Module) (acnChildOrAcnParameterId) (us:State) : (AcnChildUpdateResult option*State)= diff --git a/BackendAst/DAstUtilFunctions.fs b/BackendAst/DAstUtilFunctions.fs index 1ee92422f..6b72cb7e5 100644 --- a/BackendAst/DAstUtilFunctions.fs +++ b/BackendAst/DAstUtilFunctions.fs @@ -8,6 +8,9 @@ open Asn1AcnAst open Asn1AcnAstUtilFunctions open DAst open Language +open System.Xml.Serialization +open System.IO +//open Newtonsoft.Json let getAccessFromScopeNodeList (ReferenceToType nodes) (childTypeIsString: bool) (lm:LanguageMacros) (pVal : CallerScope) = let handleNode zeroBasedSequenceOfLevel (pVal : CallerScope) (n:ScopeNode) (childTypeIsString: bool) = @@ -1061,3 +1064,73 @@ let getBaseFuncName (lm:LanguageMacros) (typeDefinition:TypeDefinitionOrReferenc | true -> typeDefinitionName0 | false -> moduleName + "." + typeDefinitionName0 baseTypeDefinitionName, baseTypeDefinitionName + methodSuffix + codec.suffix + + +let serializeIcdTasToXml (icdTypeAss: IcdTypeAss) = + let serializer = XmlSerializer(typeof) + use stringWriter = new StringWriter() + serializer.Serialize(stringWriter, icdTypeAss) + stringWriter.ToString() + +//let serializeIcdTasToJson (icdTypeAss: IcdTypeAss) = +// JsonConvert.SerializeObject(icdTypeAss) + +(* +and IcdTypeAss = { + linkId : ReferenceToType + tasInfo : TypeAssignmentInfo option + asn1Link : string option + acnLink : string option + name : string + kind : string + comments : string list + rows : IcdRow list + compositeChildren : IcdTypeAss list + minLengthInBytes : BigInteger + maxLengthInBytes : BigInteger + hash : string + canBeEmbedded : bool + hasAcnDefinition : bool + +*) + +let md5 = System.Security.Cryptography.MD5.Create() +let calcIcdTypeAssHash (t1:IcdTypeAss) = + let rec calcIcdTypeAssHash_aux (t1:IcdTypeAss) = + let rws = + t1.rows |> + Seq.map(fun r -> sprintf "%A%A%A%A%A%A%A%A%A%A" r.idxOffset r.fieldName r.comments r.sPresent r.sType r.sConstraint r.minLengthInBits r.maxLengthInBits r.sUnits r.rowType) |> + Seq.StrJoin "" + let aa = sprintf"%A%A%A%A%A%A%A%A%A" t1.acnLink t1.asn1Link t1.name t1.kind t1.comments t1.minLengthInBytes t1.maxLengthInBytes (rws) ("") + let bytes = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes aa) + Convert.ToHexString bytes + calcIcdTypeAssHash_aux t1 + +let serializeIcdTasToText (icdTypeAss: IcdTypeAss) = + let printRow (r:IcdRow) = + sprintf """ + idxOffset = %A + fieldName = %A + comments = %A + sPresent = %A + sType = %A + sConstraint = %A + minLengthInBits = %A + maxLengthInBits = %A + sUnits = %A + rowType = %A + """ r.idxOffset r.fieldName r.comments r.sPresent r.sType r.sConstraint r.minLengthInBits r.maxLengthInBits r.sUnits r.rowType + + //let aa = sprintf"%A%A%A%A%A%A%A%A%A" t1.acnLink t1.asn1Link t1.name t1.kind t1.comments t1.minLengthInBytes t1.maxLengthInBytes (rws) ("") + let rws = icdTypeAss.rows |> Seq.map printRow |> Seq.StrJoin "\n" + sprintf """ + acnLink = %A + asn1Link = %A + name = %A + kind = %A + comments = %A + minLengthInBytes = %A + maxLengthInBytes = %A + rows = + %A + """ icdTypeAss.acnLink icdTypeAss.asn1Link icdTypeAss.name icdTypeAss.kind icdTypeAss.comments icdTypeAss.minLengthInBytes icdTypeAss.maxLengthInBytes rws diff --git a/BackendAst/GenerateAcnIcd.fs b/BackendAst/GenerateAcnIcd.fs index 950e43517..ecf39c5c2 100644 --- a/BackendAst/GenerateAcnIcd.fs +++ b/BackendAst/GenerateAcnIcd.fs @@ -625,11 +625,12 @@ let emitIcdRow stgFileName (r:AstRoot) _i (rw:IcdRow) = | _ -> icd_acn.EmitSeqOrChoiceRow stgFileName sClass (BigInteger i) rw.fieldName sComment rw.sPresent (emitTypeCol stgFileName r rw.sType) sConstraint (rw.minLengthInBits.ToString()) (rw.maxLengthInBits.ToString()) None rw.sUnits let emitTas2 stgFileName (r:AstRoot) myParams (icdTas:IcdTypeAss) = - let sCommentLine = icdTas.hash::icdTas.comments |> Seq.StrJoin (icd_uper.NewLine stgFileName ()) + let sCommentLine = icdTas.comments |> Seq.StrJoin (icd_uper.NewLine stgFileName ()) let arRows = icdTas.rows |> List.mapi (fun i rw -> emitIcdRow stgFileName r (i+1) rw) let bHasAcnDef = icdTas.hasAcnDefinition - icd_acn.EmitSequenceOrChoice stgFileName false icdTas.name icdTas.hash bHasAcnDef (icdTas.kind) (icdTas.minLengthInBytes.ToString()) (icdTas.maxLengthInBytes.ToString()) "sMaxBitsExplained" sCommentLine arRows (myParams 4I) (sCommentLine.Split [|'\n'|]) + let sMaxBitsExplained = "" + icd_acn.EmitSequenceOrChoice stgFileName false icdTas.name icdTas.hash bHasAcnDef (icdTas.kind) (icdTas.minLengthInBytes.ToString()) (icdTas.maxLengthInBytes.ToString()) sMaxBitsExplained sCommentLine arRows (myParams 4I) (sCommentLine.Split [|'\n'|]) (* let rec PrintType2 stgFileName (r:AstRoot) acnParams (icdTas:IcdTypeAss): string list = @@ -717,6 +718,15 @@ let printTasses3 stgFileName (r:DAst.AstRoot) : (string list)*(string list) = (files, icdHashesToPrint) let PrintAsn1FileInColorizedHtml (stgFileName:string) (r:AstRoot) (icdHashesToPrint:string list) (f:Asn1File) = + let debug (tsName:string) = + r.icdHashes.Values |> + Seq.collect id |> + Seq.filter(fun ts -> ts.name = tsName) |> + Seq.iter(fun ts -> + let content = DAstUtilFunctions.serializeIcdTasToText ts + let fileName = sprintf "%s_%s.txt" tsName ts.hash + File.WriteAllText(fileName, content)) + //debug("ALPHA-TC-SECONDARY-HEADER") //let tryCreateRefType = CreateAsn1AstFromAntlrTree.CreateRefTypeContent let icdHashesToPrintSet = icdHashesToPrint |> Set.ofList let fileModules = f.Modules |> List.map(fun m -> m.Name.Value) |> Set.ofList @@ -724,10 +734,15 @@ let PrintAsn1FileInColorizedHtml (stgFileName:string) (r:AstRoot) (icdHashesToPr r.icdHashes.Values |> Seq.collect id |> Seq.choose(fun z -> - match z.tasInfo with - | None -> None - | Some ts when icdHashesToPrintSet.Contains z.hash && fileModules.Contains ts.modName -> Some (ts.tasName, z.hash) - | Some _ -> None ) |> Seq.toList + //match z.tasInfo with + //| None -> None + //| Some ts when icdHashesToPrintSet.Contains z.hash && fileModules.Contains ts.modName -> Some (ts.tasName, z.hash) + //| Some _ -> None ) |> + match icdHashesToPrintSet.Contains z.hash with + | true -> Some (z.name, z.hash) + | false -> None ) |> + Seq.distinct |> + Seq.toList //let blueTasses = f.Modules |> Seq.collect(fun m -> getModuleBlueTasses m) @@ -749,37 +764,49 @@ let PrintAsn1FileInColorizedHtml (stgFileName:string) (r:AstRoot) (icdHashesToPr let isAsn1Token = GenerateUperIcd.asn1Tokens.Contains t.Text //let isType = containedIn tasses let safeText = t.Text.Replace("<",lt).Replace(">",gt) - let checkWsCmt (tok: IToken) = - match tok.Type with - |asn1Lexer.WS - |asn1Lexer.COMMENT - |asn1Lexer.COMMENT2 -> false - |_ -> true - let findToken = Array.tryFind(fun tok -> checkWsCmt tok) - let findNextToken = f.Tokens.[idx+1..] |> findToken - let findPrevToken = Array.rev f.Tokens.[0..idx-1] |> findToken - let nextToken = - let size = Seq.length(f.Tokens) - 1 - match findNextToken with - |Some(tok) -> tok - |None -> if idx = size then t else f.Tokens.[idx+1] - let prevToken = - match findPrevToken with - |Some(tok) -> tok - |None -> if idx = 0 then t else f.Tokens.[idx-1] - let uid = - //match fileTypeAssignments.TryFind t.Text with - match fileTypeAssignments |> List.filter(fun (tasName,_) -> tasName = t.Text) with + let uid () = + let checkWsCmt (tok: IToken) = + match tok.Type with + |asn1Lexer.WS + |asn1Lexer.COMMENT + |asn1Lexer.COMMENT2 -> false + |_ -> true + let findToken = Array.tryFind(fun tok -> checkWsCmt tok) + let findNextToken = f.Tokens.[idx+1..] |> findToken + let findPrevToken = Array.rev f.Tokens.[0..idx-1] |> findToken + let nextToken = + let size = Seq.length(f.Tokens) - 1 + match findNextToken with + |Some(tok) -> tok + |None -> if idx = size then t else f.Tokens.[idx+1] + let prevToken = + match findPrevToken with + |Some(tok) -> tok + |None -> if idx = 0 then t else f.Tokens.[idx-1] + let tasfileTypeAssignments = fileTypeAssignments |> List.filter(fun (tasName,_) -> tasName = t.Text) + match tasfileTypeAssignments with | [] -> safeText - //|Some tasHash -> | (_,tasHash)::[] -> if nextToken.Type = asn1Lexer.ASSIG_OP && prevToken.Type <> asn1Lexer.LID then icd_uper.TasName stgFileName safeText tasHash else icd_uper.TasName2 stgFileName safeText tasHash | _ -> - //printfn "Warning: %s is not unique" t.Text - //printfn "Warning: %A" (fileTypeAssignments |> List.filter(fun (tasName,_) -> tasName = t.Text)) + let debug () = + printfn "Warning: %s is not unique. %d" t.Text tasfileTypeAssignments.Length + + printfn "Warning: %A" tasfileTypeAssignments + //print to jso the type assignments that are not unique + tasfileTypeAssignments |> + Seq.iter (fun (tasName, tasHash) -> + let icdTas = r.icdHashes.[tasHash] + icdTas |> + Seq.iter(fun icdTas -> + let content = DAstUtilFunctions.serializeIcdTasToText icdTas + let fileName = sprintf "%s_%s.txt" tasName icdTas.hash + File.WriteAllText(fileName, content))) + + //debug () safeText //|None -> safeText let colored = @@ -787,7 +814,7 @@ let PrintAsn1FileInColorizedHtml (stgFileName:string) (r:AstRoot) (icdHashesToPr |asn1Lexer.StringLiteral |asn1Lexer.OctectStringLiteral |asn1Lexer.BitStringLiteral -> icd_uper.StringLiteral stgFileName safeText - |asn1Lexer.UID -> uid + |asn1Lexer.UID -> uid () |asn1Lexer.COMMENT |asn1Lexer.COMMENT2 -> icd_uper.Comment stgFileName safeText |_ -> safeText diff --git a/FrontEndAst/AcnCreateFromAntlr.fs b/FrontEndAst/AcnCreateFromAntlr.fs index 66fc6c265..a4b12ca68 100644 --- a/FrontEndAst/AcnCreateFromAntlr.fs +++ b/FrontEndAst/AcnCreateFromAntlr.fs @@ -935,7 +935,7 @@ let rec private mergeType (asn1:Asn1Ast.AstRoot) (acn:AcnAst) (m:Asn1Ast.Asn1Mo let combinedProperties = acnProps let allCons = t.Constraints@refTypeCons@withCons let debug = ReferenceToType curPath - //if debug.AsString = "RW90-DATAVIEW.UART-Config" then + //if debug.AsString.EndsWith "ALPHA-DELETE-DIAGNOSTIC-PARAMETER-REPORT-STRUCTURES-GENERIC" then // printfn "%s" debug.AsString //if debug.AsString = "RW90-DATAVIEW.UART-Config.timeout" then // printfn "%s" debug.AsString diff --git a/StgVarious/icdtemplate_acn.stg b/StgVarious/icdtemplate_acn.stg index ef1559a97..992b05abf 100644 --- a/StgVarious/icdtemplate_acn.stg +++ b/StgVarious/icdtemplate_acn.stg @@ -42,7 +42,7 @@ RelativeOid() ::= "RELATIVE-OID" EmitRowWith3Dots() ::= << -

. . .

+

. . .

>> diff --git a/asn1scc/Program.fs b/asn1scc/Program.fs index d82c8e8f4..e166105bf 100644 --- a/asn1scc/Program.fs +++ b/asn1scc/Program.fs @@ -119,7 +119,7 @@ let printVersion () = //let fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location); //let version = fvi.FileVersion; - let version = "4.5.2.1" + let version = "4.5.2.2" printfn "asn1scc version %s\n" version ()