-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Code generation #32
Code generation #32
Changes from 4 commits
a8dff3e
a804343
7e603b2
2874a9d
2144050
e81e2ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
namespace OBO.NET.CodeGeneration | ||
|
||
|
||
open OBO.NET | ||
open FSharpAux | ||
open type System.Environment | ||
|
||
|
||
module CodeGeneration = | ||
|
||
[<Literal>] | ||
let baseString = """namespace ARCTokenization.StructuralOntology | ||
|
||
open ControlledVocabulary | ||
|
||
module <name> = | ||
|
||
""" | ||
|
||
/// Takes an OboTerm and returns its name but with all spaces replaced by underscores. | ||
let toUnderscoredName (term : OboTerm) = | ||
term.Name | ||
|> String.replace " " "_" | ||
|
||
/// Returns true if a string contains special characters or starts with a number. | ||
let checkForSpecialCharacters str = | ||
let spChs = System.Text.RegularExpressions.Regex(@"(^\d|[^a-zA-Z0-9_])") | ||
(spChs.Match str).Success | ||
|
||
/// Takes a string and returns it with back ticks ("``") at the beginning and the end. | ||
let addBackTicks str = | ||
$"``{str}``" | ||
|
||
/// Takes an OboTerm and returns its TermSourceRef as string. | ||
let toTermSourceRef (term : OboTerm) = | ||
term.Id | ||
|> String.takeWhile ((<>) ':') | ||
|
||
/// Takes an OboTerm and transforms it into an F# code string for structural ontology libraries. | ||
let toCodeString (term : OboTerm) = | ||
let underscoredName = toUnderscoredName term | ||
let curatedName = | ||
if checkForSpecialCharacters underscoredName then | ||
addBackTicks underscoredName | ||
else underscoredName | ||
$" let {curatedName} = CvTerm.create(\"{term.Id}\", \"{term.Name}\", \"{toTermSourceRef term}\"){NewLine}{NewLine}" | ||
|
||
/// Takes a module name and an OboOntology and returns the F# code of the whole term list for structural ontology libraries. | ||
let toSourceCode moduleName (onto : OboOntology) = | ||
let concattedSingleValues = String.init onto.Terms.Length (fun i -> $"{toCodeString onto.Terms[i]}") | ||
let updatedBaseString = String.replace "<name>" moduleName baseString | ||
$"{updatedBaseString}{concattedSingleValues}" | ||
|
||
/// Takes a module name and an OboOntology and writes the ontology's terms as F# code for structural ontology libraries as a source file at the given path. | ||
let toFile moduleName (onto : OboOntology) path = | ||
System.IO.File.WriteAllText(path, toSourceCode moduleName onto) | ||
|
||
/// Takes a module name and the path to an OBO file and writes the ontology's terms as F# code for structural ontology libraries as a source file at the given output path. | ||
let fromOboFileToSourceFile moduleName inputPath outputPath = | ||
OboOntology.fromFile false inputPath | ||
|> fun o -> toFile moduleName o outputPath |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<!-- Optional: Publish the repository URL in the built .nupkg (in the NuSpec <Repository> element) --> | ||
<PublishRepositoryUrl>true</PublishRepositoryUrl> | ||
<!-- Optional: Embed source files that are not tracked by the source control manager in the PDB --> | ||
<EmbedUntrackedSources>true</EmbedUntrackedSources> | ||
<!-- Optional: Build symbol package (.snupkg) to distribute the PDB containing Source Link --> | ||
<IncludeSymbols>true</IncludeSymbols> | ||
<SymbolPackageFormat>snupkg</SymbolPackageFormat> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\OBO.NET\OBO.NET.fsproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="CodeGeneration.fs" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup> | ||
<Authors>Oliver Maus, F# open source contributors</Authors> | ||
<Description>An OBO file format to F# source code generator, written in F#.</Description> | ||
<Summary>An OBO file format to F# source code generator, written in F#.</Summary> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<PackageProjectUrl>https://github.com/CSBiology/OBO.NET</PackageProjectUrl> | ||
<PackageTags>ontology fsharp file obo codegeneration code generation</PackageTags> | ||
<RepositoryUrl>https://github.com/CSBiology/OBO.NET</RepositoryUrl> | ||
<RepositoryType>git</RepositoryType> | ||
<FsDocsLicenseLink>https://github.com/CSBiology/OBO.NET/blob/main/LICENSE</FsDocsLicenseLink> | ||
<FsDocsReleaseNotesLink>https://github.com/CSBiology/OBO.NET/blob/main/RELEASE_NOTES.md</FsDocsReleaseNotesLink> | ||
</PropertyGroup> | ||
|
||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
namespace OBO.NET.CodeGeneration.Tests | ||
|
||
open Expecto | ||
open OBO.NET | ||
open OBO.NET.CodeGeneration | ||
open FSharpAux | ||
open ARCTokenization.StructuralOntology | ||
open ARCTokenization.Terms | ||
open type System.Environment | ||
|
||
|
||
module CodeGenerationTests = | ||
|
||
let refObo = OboOntology.fromFile false (System.IO.Path.Combine(__SOURCE_DIRECTORY__, "References/ReferenceOboFile.obo")) | ||
|
||
let toUnderscoredNameTest = | ||
testList "toUnderscoredName" [ | ||
testCase "returns correct underscored name" <| fun _ -> | ||
let expected = "Investigation_Metadata" | ||
let actual = List.head refObo.Terms |> CodeGeneration.toUnderscoredName | ||
Expect.equal actual expected "underscored name is not correct" | ||
] | ||
|
||
let toTermSourceRefTest = | ||
testList "toTermSourceRef" [ | ||
testCase "returns correct TermSourceRef" <| fun _ -> | ||
let expected = "INVMSO" | ||
let actual = List.head refObo.Terms |> CodeGeneration.toTermSourceRef | ||
Expect.equal actual expected "TermSourceRef is not correct" | ||
] | ||
|
||
let toCodeStringTest = | ||
testList "toCodeString" [ | ||
testCase "returns correct F# code" <| fun _ -> | ||
let expected = $" let Investigation_Metadata = CvTerm.create(\"INVMSO:00000001\", \"Investigation Metadata\", \"INVMSO\"){NewLine}{NewLine}" | ||
let actual = List.head refObo.Terms |> CodeGeneration.toCodeString | ||
Expect.equal actual expected "F# code is not correct" | ||
] | ||
|
||
let toSourceCodeTest = | ||
testList "toSourceCode" [ | ||
testCase "returns correct source code" <| fun _ -> | ||
let expected = | ||
$"namespace ARCTokenization.StructuralOntology{NewLine}{NewLine} open ControlledVocabulary{NewLine}{NewLine} module Investigation ={NewLine}{NewLine} let Investigation_Metadata = CvTerm.create(\"INVMSO:00000001\", \"Investigation Metadata\", \"INVMSO\"){NewLine}{NewLine} let ONTOLOGY_SOURCE_REFERENCE = CvTerm.create(\"INVMSO:00000002\", \"ONTOLOGY SOURCE REFERENCE\", \"INVMSO\"){NewLine}{NewLine} let Term_Source_Name = CvTerm.create(\"INVMSO:00000003\", \"Term Source Name\", \"INVMSO\")" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here, i would also just parse a fully correct fsharp source file as reference, and check wether the full output is correct. |
||
|> String.replace "\r" "" | ||
let actual = | ||
CodeGeneration.toSourceCode "Investigation" refObo | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh lol, didn't know about that! 👍🏻😄 |
||
|> String.splitS NewLine | ||
|> Array.take 11 | ||
|> String.concat "\n" | ||
|> String.replace "\r" "" | ||
Expect.equal actual expected "Source code is not correct" | ||
] | ||
|
||
[<Tests>] | ||
let all = testList "CodeGeneration" [toUnderscoredNameTest; toTermSourceRefTest; toCodeStringTest; toSourceCodeTest] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
open OBO.NET.CodeGeneration.Tests | ||
|
||
open Expecto | ||
|
||
|
||
[<EntryPoint>] | ||
let main argv = Tests.runTestsInAssemblyWithCLIArgs [] argv |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
<GenerateProgramFile>false</GenerateProgramFile> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="references\ReferenceOboFile.obo" /> | ||
<Compile Include="References\ReferenceSourceFile.fs" /> | ||
<Compile Include="CodeGeneration.Tests.fs" /> | ||
<Compile Include="Main.fs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup /> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\OBO.NET\OBO.NET.fsproj" /> | ||
<ProjectReference Include="..\..\src\OBO.NET.CodeGeneration\OBO.NET.CodeGeneration.fsproj" /> | ||
<PackageReference Include="Expecto" Version="10.1.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" /> | ||
<PackageReference Include="coverlet.collector" Version="3.2.0" /> | ||
<PackageReference Include="YoloDev.Expecto.TestSdk" Version="0.14.1" /> | ||
<PackageReference Include="ARCTokenization" Version="6.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i would not use these ontologies but static files in this repo as references for tests