diff --git a/src/ARCTokenization/Address.fs b/src/ARCTokenization/Address.fs index 4bf4d2a..3e5ac9a 100644 --- a/src/ARCTokenization/Address.fs +++ b/src/ARCTokenization/Address.fs @@ -4,11 +4,11 @@ open ControlledVocabulary module Address = - let column : CvTerm = "http://purl.obolibrary.org/obo/NCIT_C43379","Column","NCIT" + let column = CvTerm.create(accession = "http://purl.obolibrary.org/obo/NCIT_C43379", name = "Column", ref = "NCIT") - let row : CvTerm = "http://purl.obolibrary.org/obo/NCIT_C43378","Row","NCIT" + let row = CvTerm.create(accession = "http://purl.obolibrary.org/obo/NCIT_C43378", name = "Row", ref = "NCIT") - let worksheet : CvTerm = "http://purl.obolibrary.org/obo/NCIT_C73541","Worksheet","NCIT" + let worksheet = CvTerm.create(accession = "http://purl.obolibrary.org/obo/NCIT_C73541", name = "Worksheet", ref = "NCIT") //"isa_investigation!A1" diff --git a/src/ARCTokenization/AnnotationTable.fs b/src/ARCTokenization/AnnotationTable.fs index 0d28248..2f63ac1 100644 --- a/src/ARCTokenization/AnnotationTable.fs +++ b/src/ARCTokenization/AnnotationTable.fs @@ -151,7 +151,7 @@ module AnnotationTable = let cvPars = List.map4 ( fun (vl : FsCell) unt tan tsr -> - let valTerm = CvUnit(tan.Value, vl.Value, tsr.Value) + let valTerm = CvUnit.create(accession = tan.Value, name = vl.Value, ref = tsr.Value) CvParam(d.Value, a.Value, c.Value, WithCvUnitAccession (unt.Value, valTerm)) ) dataCellsVal dataCellsUnt dataCellsTan dataCellsTsr yield! cvPars @@ -166,7 +166,7 @@ module AnnotationTable = (dataCellsVal, dataCellsTsr, dataCellsTan) |||> List.map3 ( fun vl tsr tan -> - let valTerm = CvTerm(tan.Value, vl.Value, tsr.Value) + let valTerm = CvTerm.create(accession = tan.Value, name = vl.Value, ref = tsr.Value) CvParam(c.Value, a.Value, b.Value, CvValue valTerm) ) yield! cvPars @@ -182,7 +182,7 @@ module AnnotationTable = (dataCellsVal, dataCellsTsr) ||> List.map2 ( fun vl tsr -> - let valTerm = CvTerm("(n/a)", vl.Value, tsr.Value) + let valTerm = CvTerm.create (accession = "(n/a)", name = vl.Value, ref = tsr.Value) CvParam("n/a", a.Value, b.Value, CvValue valTerm) ) yield! cvPars @@ -195,7 +195,7 @@ module AnnotationTable = (dataCellsTsr, dataCellsTan) ||> List.map2 ( fun tsr tan -> - let valTerm = CvTerm(tan.Value, "n/a", tsr.Value) + let valTerm = CvTerm.create(accession = tan.Value, name = "n/a", ref = tsr.Value) CvParam(b.Value, "(n/a)", a.Value, CvValue valTerm) ) yield! cvPars diff --git a/src/ARCTokenization/MetadataSheet.fs b/src/ARCTokenization/MetadataSheet.fs index 0d92de5..8137c08 100644 --- a/src/ARCTokenization/MetadataSheet.fs +++ b/src/ARCTokenization/MetadataSheet.fs @@ -9,7 +9,7 @@ module MetadataSheet = let (|Term|_|) (terms : CvTerm list) (key : string) : CvTerm Option = terms - |> List.tryFind (fun (term) -> CvTerm.getName term = key) + |> List.tryFind (fun (term) -> term.Name = key) let (|UnMatchable|) (key : string) : string = key diff --git a/src/ARCTokenization/Terms.fs b/src/ARCTokenization/Terms.fs index c9453bc..f96688c 100644 --- a/src/ARCTokenization/Terms.fs +++ b/src/ARCTokenization/Terms.fs @@ -24,7 +24,7 @@ module InvestigationMetadata = let cvTerms = ontology.Terms - |> List.map (fun t -> CvTerm(t.Id,t.Name,"INVMSO")) + |> List.map (fun t -> CvTerm.create(accession = t.Id, name = t.Name, ref = "INVMSO")) module StudyMetadata = @@ -34,7 +34,7 @@ module StudyMetadata = let cvTerms = ontology.Terms - |> List.map (fun t -> CvTerm(t.Id,t.Name,"STDMSO")) + |> List.map (fun t -> CvTerm.create(accession = t.Id, name = t.Name, ref ="STDMSO")) module AssayMetadata = @@ -44,8 +44,8 @@ module AssayMetadata = let cvTerms = ontology.Terms - |> List.map (fun t -> CvTerm(t.Id,t.Name,"ASSMSO")) + |> List.map (fun t -> CvTerm.create(accession = t.Id, name = t.Name, ref ="ASSMSO")) module StructuralTerms = - let metadataSectionKey = CvTerm("AGMO:00000001","Metadata Section Key","AGMO") \ No newline at end of file + let metadataSectionKey = CvTerm.create(accession = "AGMO:00000001", name = "Metadata Section Key", ref = "AGMO") \ No newline at end of file diff --git a/src/ControlledVocabulary/CvAttributeCollection.fs b/src/ControlledVocabulary/CvAttributeCollection.fs index cdd8cd5..ec55752 100644 --- a/src/ControlledVocabulary/CvAttributeCollection.fs +++ b/src/ControlledVocabulary/CvAttributeCollection.fs @@ -39,7 +39,7 @@ type CvAttributeCollection(attributes : IDictionary) = /// Retrieves an IParam attribute by its term, if it exists, else returns None member this.TryGetAttribute (term : CvTerm) = - Dictionary.tryFind (CvTerm.getName term) this + Dictionary.tryFind term.Name this |> Option.bind (fun param -> if CvBase.equalsTerm term param then Some param @@ -51,7 +51,7 @@ type CvAttributeCollection(attributes : IDictionary) = /// Retrieves an IParam attribute by its term, if it exists, else fails member this.GetAttribute (term : CvTerm) = - Dictionary.item (CvTerm.getName term) this + Dictionary.item term.Name this /// Returns true, if an attribute with the given name exists in the collection member this.ContainsAttribute (name : string) = @@ -59,7 +59,7 @@ type CvAttributeCollection(attributes : IDictionary) = /// Returns true, if an attribute with the given term exists in the collection member this.ContainsAttribute (term : CvTerm) = - match Dictionary.tryFind (CvTerm.getName term) this with + match Dictionary.tryFind term.Name this with | Some param when CvBase.equalsTerm term param -> true | _ -> false diff --git a/src/ControlledVocabulary/CvContainer.fs b/src/ControlledVocabulary/CvContainer.fs index a48d77c..7dfc830 100644 --- a/src/ControlledVocabulary/CvContainer.fs +++ b/src/ControlledVocabulary/CvContainer.fs @@ -13,18 +13,18 @@ module internal Dictionary = /// Represents a collection of structured properties, annotated with a controlled vocabulary term. type CvContainer ( cvAccession : string, - cvName : string, - cvRefUri : string, - attributes : IDictionary, - properties : IDictionary> + cvName : string, + cvRef : string, + attributes : IDictionary, + properties : IDictionary> ) = inherit CvAttributeCollection(attributes) interface ICvBase with - member this.ID = cvAccession - member this.Name = cvName - member this.RefUri = cvRefUri + member this.Accession = cvAccession + member this.Name = cvName + member this.RefUri = cvRef member this.HasAttributes with get() = this.Attributes |> Seq.isEmpty |> not @@ -37,8 +37,8 @@ type CvContainer ( CvContainer(cvAccession, cvName, cvRefUri, Seq.empty) - new ((id,name,ref) : CvTerm, attributes : IDictionary) = - CvContainer(id,name,ref, attributes, Dictionary()) + new (term : CvTerm, attributes : IDictionary) = + CvContainer(term.Accession, term.Name, term.RefUri, attributes, Dictionary()) new (term : CvTerm,attributes : seq) = let dict = CvAttributeCollection(attributes) CvContainer(term, dict) @@ -79,7 +79,7 @@ type CvContainer ( /// Fails if the propertyName cannot be found. member this.GetManyParams propertyName = Dictionary.item propertyName this.Properties - |> Seq.choose Param.tryParam + |> Seq.choose CvBase.tryParam /// Retrieves child with the given name of the CvContainer. /// @@ -109,7 +109,7 @@ type CvContainer ( /// Fails if there is not exactly one child with the given name or if the propertyName cannot be found. member this.GetSingleParam propertyName = Dictionary.item propertyName this.Properties - |> Seq.choose Param.tryParam + |> Seq.choose CvBase.tryParam |> Seq.exactlyOne @@ -146,7 +146,7 @@ type CvContainer ( Dictionary.tryFind propertyName this.Properties |> Option.bind (fun many -> many - |> Seq.choose Param.tryParam + |> Seq.choose CvBase.tryParam |> fun items -> if Seq.isEmpty items then None else Some items ) @@ -176,7 +176,7 @@ type CvContainer ( /// Returns None if there is not exactly one child with the given name or if the propertyName cannot be found. member this.TryGetSingleParam propertyName = Dictionary.tryFind propertyName this.Properties - |> Option.bind (Seq.choose Param.tryParam >> Seq.tryExactlyOne) + |> Option.bind (Seq.choose CvBase.tryParam >> Seq.tryExactlyOne) @@ -330,4 +330,4 @@ type CvContainer ( Dictionary.addOrUpdateInPlace value.Name (Seq.singleton value) cvContainer.Properties override this.ToString() = - $"CvContainer: {(this :> ICvBase).Name}\n\tID: {(this :> ICvBase).ID}\n\tRefUri: {(this :> ICvBase).RefUri}\n\tProperties: {this.Properties.Keys |> Seq.toList}" \ No newline at end of file + $"CvContainer: {(this :> ICvBase).Name}\n\tID: {(this :> ICvBase).Accession}\n\tRefUri: {(this :> ICvBase).RefUri}\n\tProperties: {this.Properties.Keys |> Seq.toList}" \ No newline at end of file diff --git a/src/ControlledVocabulary/CvObject.fs b/src/ControlledVocabulary/CvObject.fs index fc7aad4..861b46d 100644 --- a/src/ControlledVocabulary/CvObject.fs +++ b/src/ControlledVocabulary/CvObject.fs @@ -3,20 +3,20 @@ open System.Collections.Generic /// Represents a generic object, annotated with a controlled vocabulary term -type CvObject<'T>(cvAccession : string, cvName : string, cvRefUri : string, object : 'T, attributes : Dictionary) = +type CvObject<'T>(cvAccession : string, cvName : string, cvRef : string, object : 'T, attributes : Dictionary) = inherit CvAttributeCollection(attributes) interface ICvBase with - member this.ID = cvAccession - member this.Name = cvName - member this.RefUri = cvRefUri + member this.Accession = cvAccession + member this.Name = cvName + member this.RefUri = cvRef member this.HasAttributes with get() = this.Attributes |> Seq.isEmpty |> not - new ((id,name,ref) : CvTerm,object : 'T, attributes) = CvObject (id,name,ref,object,attributes) + new (term: CvTerm, object : 'T, attributes) = CvObject (term.Accession, term.Name, term.RefUri, object, attributes) member this.Object = object override this.ToString() = - $"Name: {(this :> ICvBase).Name}\n\tID: {(this :> ICvBase).ID}\n\tRefUri: {(this :> ICvBase).RefUri}" \ No newline at end of file + $"Name: {(this :> ICvBase).Name}\n\tID: {(this :> ICvBase).Accession}\n\tRefUri: {(this :> ICvBase).RefUri}" \ No newline at end of file diff --git a/src/ControlledVocabulary/CvParam.fs b/src/ControlledVocabulary/CvParam.fs index 10cb3d6..ef0766c 100644 --- a/src/ControlledVocabulary/CvParam.fs +++ b/src/ControlledVocabulary/CvParam.fs @@ -10,16 +10,22 @@ open FSharpAux /// /// Attributes can be used to further describe the CvParam [] -type CvParam(cvAccession : string, cvName : string, cvRefUri : string, paramValue : ParamValue, attributes : IDictionary) = +type CvParam(cvAccession : string, cvName : string, cvRef : string, paramValue : ParamValue, attributes : IDictionary) = inherit CvAttributeCollection(attributes) + member this.Accession = cvAccession + member this.Name = cvName + member this.RefUri = cvRef + member this.Value = paramValue + member this.WithValue(v : ParamValue) = CvParam(cvAccession,cvName,cvRef,v,attributes) + interface IParam with - member this.ID = cvAccession - member this.Name = cvName - member this.RefUri = cvRefUri - member this.Value = paramValue - member this.WithValue(v : ParamValue) = CvParam(cvAccession,cvName,cvRefUri,v,attributes) + member this.Accession = this.Accession + member this.Name = this.Name + member this.RefUri = this.RefUri + member this.Value = this.Value + member this.WithValue(v : ParamValue) = CvParam(cvAccession,cvName,cvRef,v,attributes) member this.HasAttributes with get() = this.Attributes |> Seq.isEmpty |> not @@ -31,15 +37,15 @@ type CvParam(cvAccession : string, cvName : string, cvRefUri : string, paramValu new (id,name,ref,v : IConvertible) = CvParam (id,name,ref,ParamValue.Value v) - new ((id,name,ref) : CvTerm,pv,attributes : seq) = - CvParam (id,name,ref,pv,attributes) + new (term : CvTerm, pv, attributes : seq) = + CvParam (term.Accession, term.Name, term.RefUri, pv, attributes) new (cvTerm,pv : ParamValue) = CvParam (cvTerm,pv,Seq.empty) new (cvTerm,v : IConvertible) = CvParam (cvTerm,ParamValue.Value v) member this.Equals (term : CvTerm) = - CvBase.equalsTerm term this + Param.equalsTerm term this member this.Equals (cv : ICvBase) = CvBase.equals cv this @@ -47,27 +53,87 @@ type CvParam(cvAccession : string, cvName : string, cvRefUri : string, paramValu member this.Equals (cvp : CvParam) = this.Equals(cvp :> ICvBase) - /// Returns Some Param if the given cv item can be downcast, else returns None - static member tryCvParam (cv : ICvBase) = - match cv with - | :? CvParam as param -> Some param - | _ -> None + //---------------------- IParam implementations ----------------------// + + /// Returns the value of the Param as a ParamValue + static member getParamValue (cvp: CvParam) = Param.getParamValue cvp + + /// Returns the value of the Param as IConvertible + static member getValue (cvp: CvParam) = Param.getValue cvp + + /// Returns the value of the Param as string + static member getValueAsString (cvp: CvParam) = Param.getValueAsString cvp + + /// Returns the value of the Param as int if possible, else fails + static member getValueAsInt (cvp: CvParam) = Param.getValueAsInt cvp + + /// Returns the value of the Param as a term + static member getValueAsTerm (cvp: CvParam) = Param.getValueAsTerm cvp + + static member tryGetValueAccession (cvp: CvParam) = Param.tryGetValueAccession cvp + + static member tryGetValueRef (cvp: CvParam) = Param.tryGetValueRef cvp + + static member tryGetCvUnit (cvp: CvParam) : CvUnit option = Param.tryGetCvUnit cvp + + static member tryGetCvUnitValue (cvp: CvParam) = Param.tryGetCvUnitValue cvp + + static member tryGetCvUnitTermName (cvp: CvParam) = Param.tryGetCvUnitTermName cvp + + static member tryGetCvUnitTermAccession (cvp: CvParam) = Param.tryGetCvUnitTermAccession cvp + + static member tryGetCvUnitTermRef (cvp: CvParam) = Param.tryGetCvUnitTermRef cvp + + static member mapValue (f : ParamValue -> ParamValue) (cvp: CvParam) = Param.mapValue f cvp :?> CvParam + + static member tryMapValue (f : ParamValue -> ParamValue option) (cvp: CvParam) = + Param.tryMapValue f cvp + |> Option.map (fun v -> v :?> CvParam) + + static member tryAddName (name : string) (cvp: CvParam) = + Param.tryAddName name cvp + |> Option.map (fun v -> v :?> CvParam) + + static member tryAddAccession (acc : string) (cvp: CvParam) = + Param.tryAddAccession acc cvp + |> Option.map (fun v -> v :?> CvParam) - /// Returns Some Param if the given value item can be downcast, else returns None - static member tryCvParam (cv : IParamBase) = - match cv with - | :? CvParam as param -> Some param - | _ -> None + static member tryAddReference (ref : string) (cvp: CvParam) = + Param.tryAddReference ref cvp + |> Option.map (fun v -> v :?> CvParam) - /// Returns Some Param if the given param item can be downcast, else returns None - static member tryCvParam (cv : IParam) = - match cv with - | :? CvParam as param -> Some param - | _ -> None + static member tryAddUnit (unit : CvUnit) (cvp: CvParam) = + Param.tryAddUnit unit cvp + |> Option.map (fun v -> v :?> CvParam) - /// Returns true, if the given value item can be downcast to CvParam - static member isCvParam (cv : ICvBase) = - CvBase.is cv + /// Returns the id of the cv item + static member getCvAccession (cvp: CvParam) = Param.getCvAccession cvp + + /// Returns the name of the cv item + static member getCvName (cvp: CvParam) = Param.getCvName cvp + + /// Returns the reference of the cv item + static member getCvRef (cvp: CvParam) = Param.getCvRef cvp + + /// Returns the full term of the cv item + static member getTerm (cvp: CvParam) = Param.getTerm cvp + + /// Returns true, if the given term matches the term of the cv item + static member equalsTerm (term : CvTerm) (cvp: CvParam) = Param.equalsTerm term cvp + + /// Returns true, if the terms of the given param items match + static member equals (cvp1 : CvParam) (cvp2 : CvParam) = Param.equals cvp1 cvp2 + + /// Returns true, if the names of the given param items match + static member equalsName (cvp1 : CvParam) (cvp2 : CvParam) = Param.equalsName cvp1 cvp1 + + /// Returns Some Value of type 'T, if the given param item can be downcast, else returns None + static member inline tryAs<'T when 'T :> IParam> (cvp: CvParam) = Param.tryAs<'T> cvp + + /// Returns true, if the given param item can be downcast + static member inline is<'T when 'T :> IParam> (cvp: CvParam) = Param.is<'T> cvp + + //---------------------- CvParam specific implementations ----------------------// /// Create a CvParam from a category and a simple value static member fromValue (category : CvTerm) (v : 'T) = @@ -81,30 +147,39 @@ type CvParam(cvAccession : string, cvName : string, cvRefUri : string, paramValu static member fromValueWithUnit (category : CvTerm) (v : 'T) (unit : CvUnit) = CvParam(category, ParamValue.WithCvUnitAccession (v :> IConvertible,unit)) - static member mapValue (f : ParamValue -> ParamValue) (param : CvParam) = - Param.mapValue f param - - static member tryMapValue (f : ParamValue -> ParamValue option) (param : CvParam) = - Param.tryMapValue f param - - static member tryAddName (name : string) (param : CvParam) = - Param.tryAddName name param - - static member tryAddAnnotationID (id : string) (param : CvParam) = - Param.tryAddAnnotationID id param - - static member tryAddReference (ref : string) (param : CvParam) = - Param.tryAddReference ref param - - static member tryAddUnit (unit : CvUnit) (param : CvParam) = - Param.tryAddUnit unit param - static member getAttributes (param : CvParam) = param.Values |> Seq.cast - override this.ToString() = - $"CvParam: {(this :> ICvBase).Name}\n\tID: {(this :> ICvBase).ID}\n\tRefUri: {(this :> ICvBase).RefUri}\n\tValue: {(this :> IParamBase).Value}\n\tAttributes: {this.Keys |> Seq.toList}" + $"CvParam: {(this :> ICvBase).Name}\n\tID: {(this :> ICvBase).Accession}\n\tRefUri: {(this :> ICvBase).RefUri}\n\tValue: {(this :> IParamBase).Value}\n\tAttributes: {this.Keys |> Seq.toList}" member this.DisplayText = - this.ToString() \ No newline at end of file + this.ToString() + +[] +module CvParamExtensions = + + type ParamBase with + /// Returns Some Param if the given value item can be downcast, else returns None + static member tryCvParam (cv : IParamBase) = + match cv with + | :? CvParam as param -> Some param + | _ -> None + + type Param with + /// Returns Some Param if the given param item can be downcast, else returns None + static member tryCvParam (cv : IParam) = + match cv with + | :? CvParam as param -> Some param + | _ -> None + + type CvBase with + /// Returns Some Param if the given cv item can be downcast, else returns None + static member tryCvParam (cv : ICvBase) = + match cv with + | :? CvParam as param -> Some param + | _ -> None + + /// Returns true, if the given value item can be downcast to CvParam + static member isCvParam (cv : ICvBase) = + CvBase.is cv diff --git a/src/ControlledVocabulary/CvTerm.fs b/src/ControlledVocabulary/CvTerm.fs index ddf0796..a6dbcea 100644 --- a/src/ControlledVocabulary/CvTerm.fs +++ b/src/ControlledVocabulary/CvTerm.fs @@ -1,37 +1,31 @@ namespace ControlledVocabulary /// Represents a term from a controlled vocabulary (Cv) -/// in the form of: id|accession * name|value * refUri +/// in the form of: id|accession ; name|value ; refUri // ?Maybe [] //[] -type CvTerm = string * string * string +type CvTerm = { + Accession: string + Name: string + RefUri: string +} with + static member create( + accession: string, + name: string, + ref : string + ) = + {Accession = accession; Name = name; RefUri = ref} -module CvTerm = - - /// gets the name of the CvTerm - let getName (cvTerm : CvTerm) = - match cvTerm with - | id, name, refUri -> name - - /// gets the name of the CvTerm - let getId (cvTerm : CvTerm) = - match cvTerm with - | id, name, refUri -> id + static member create( + name: string + ) = + CvTerm.create( + name = name, + accession = "", + ref = "" + ) - /// gets the source reference of the CvTerm - let getRef (cvTerm : CvTerm) = - match cvTerm with - | id, name, refUri -> refUri - - /// creates a CvTerm from name - let fromName (name : string) : CvTerm= - "", name, "" - - /// creates a CvTerm from a term triplet - let create id name ref : CvTerm = - id,name,ref - /// Represents a unit term from the unit ontology /// in the form of: id|accession * name * refUri // ?Maybe [] -type CvUnit = string * string * string \ No newline at end of file +type CvUnit = CvTerm \ No newline at end of file diff --git a/src/ControlledVocabulary/ICvBase.fs b/src/ControlledVocabulary/ICvBase.fs index 73cb879..3f79927 100644 --- a/src/ControlledVocabulary/ICvBase.fs +++ b/src/ControlledVocabulary/ICvBase.fs @@ -8,53 +8,53 @@ type IAttributeCollection = /// Interface ensures the propterties necessary for CvTerm and ICvBase = - abstract member ID : string - abstract member Name : string - abstract member RefUri : string + abstract member Accession : string + abstract member Name : string + abstract member RefUri : string inherit IAttributeCollection //override this.ToString() = // $"Name: {this.Name}\nID: {this.ID}\nRefUri: {this.RefUri}" -module CvBase = +type CvBase = /// Returns the id of the cv item - let getCvAccession (cv : #ICvBase) = - cv.ID + static member getCvAccession (cv : #ICvBase) = + cv.Accession /// Returns the name of the cv item - let getCvName (cv : #ICvBase) = + static member getCvName (cv : #ICvBase) = cv.Name /// Returns the reference of the cv item - let getCvRef (cv : #ICvBase) = + static member getCvRef (cv : #ICvBase) = cv.RefUri /// Returns the full term of the cv item - let getTerm (cv : #ICvBase) : CvTerm = - CvTerm.create cv.ID cv.Name cv.RefUri + static member getTerm (cv : #ICvBase) : CvTerm = + CvTerm.create(accession = cv.Accession, name = cv.Name, ref = cv.RefUri) /// Returns true, if the given term matches the term of the cv item - let equalsTerm (term : CvTerm) (cv : #ICvBase) = - getTerm cv = term + static member equalsTerm (term : CvTerm) (cv : #ICvBase) = + CvBase.getTerm cv = term /// Returns true, if the terms of the given cv items match - let equals (cv1 : #ICvBase) (cv2 : #ICvBase) = - getTerm cv1 = getTerm cv2 + static member equals (cv1 : #ICvBase) (cv2 : #ICvBase) = + CvBase.getTerm cv1 = CvBase.getTerm cv2 /// Returns true, if the names of the given cv items match - let equalsName (cv1 : #ICvBase) (cv2 : #ICvBase) = - getCvName cv1 = getCvName cv2 + static member equalsName (cv1 : #ICvBase) (cv2 : #ICvBase) = + CvBase.getCvName cv1 = CvBase.getCvName cv2 /// Returns Some Value of type 'T, if the given cv item can be downcast, else returns None - let inline tryAs<'T when 'T :> ICvBase> (cv : ICvBase) = + static member inline tryAs<'T when 'T :> ICvBase> (cv : ICvBase) = match cv with | :? 'T as cv -> Some cv | _ -> None /// Returns true, if the given cv item can be downcast - let inline is<'T when 'T :> ICvBase> (cv : ICvBase) = + static member inline is<'T when 'T :> ICvBase> (cv : ICvBase) = match cv with | :? 'T as cv -> true | _ -> false diff --git a/src/ControlledVocabulary/IParam.fs b/src/ControlledVocabulary/IParam.fs index c1624b5..deea3b7 100644 --- a/src/ControlledVocabulary/IParam.fs +++ b/src/ControlledVocabulary/IParam.fs @@ -9,84 +9,105 @@ type IParam = inherit ICvBase inherit IParamBase - type Param = - /// Returns true, if the given term matches the term of the param - static member equalsTerm (term : CvTerm) (param : IParam) = - CvBase.equalsTerm term param + //---------------------- IParamBase implementations ----------------------// + + /// Returns the value of the Param as a ParamValue + static member getParamValue (param: IParam) = ParamBase.getParamValue param + + /// Returns the value of the Param as IConvertible + static member getValue (param: IParam) = ParamBase.getValue param + + /// Returns the value of the Param as string + static member getValueAsString (param: IParam) = ParamBase.getValueAsString param + + /// Returns the value of the Param as int if possible, else fails + static member getValueAsInt (param: IParam) = ParamBase.getValueAsInt param + + /// Returns the value of the Param as a term + static member getValueAsTerm (param: IParam) = ParamBase.getValueAsTerm param + + static member tryGetValueAccession (param: IParam) = ParamBase.tryGetValueAccession param + + static member tryGetValueRef (param: IParam) = ParamBase.tryGetValueRef param + + static member tryGetCvUnit (param: IParam) : CvUnit option = ParamBase.tryGetCvUnit param + + static member tryGetCvUnitValue (param: IParam) = ParamBase.tryGetCvUnitValue param + + static member tryGetCvUnitTermName (param: IParam) = ParamBase.tryGetCvUnitTermName param + + static member tryGetCvUnitTermAccession (param: IParam) = ParamBase.tryGetCvUnitTermAccession param + + static member tryGetCvUnitTermRef (param: IParam) = ParamBase.tryGetCvUnitTermRef param + + static member mapValue (f : ParamValue -> ParamValue) (param : IParam) = ParamBase.mapValue f param :?> IParam - /// Returns true, if the terms of the given params match - static member equals (param1 : IParam) (param2 : IParam) = - CvBase.equals param1 param2 + static member tryMapValue (f : ParamValue -> ParamValue option) (param : IParamBase) = + ParamBase.tryMapValue f param + |> Option.map (fun v -> v :?> IParam) - /// Returns true, if the name of the given params match - static member equalsName (param1 : IParam) (param2 : IParam) = - CvBase.equalsName param1 param2 + static member tryAddName (name : string) (param : IParamBase) = + ParamBase.tryAddName name param + |> Option.map (fun v -> v :?> IParam) - /// Returns Some Param, if the given cv item can be downcast, else returns None - static member tryParam (cv : ICvBase) = - match cv with - | :? IParam as param -> Some param - | _ -> None + static member tryAddAccession (acc : string) (param : IParamBase) = + ParamBase.tryAddAccession acc param + |> Option.map (fun v -> v :?> IParam) - /// Returns Some Param, if the given value item can be downcast, else returns None - static member tryParam (cv : IParamBase) = - match cv with - | :? IParam as param -> Some param - | _ -> None + static member tryAddReference (ref : string) (param : IParamBase) = + ParamBase.tryAddReference ref param + |> Option.map (fun v -> v :?> IParam) - /// Returns Some Value of type 'T, if the given param can be downcast, else returns None - static member inline tryAs<'T when 'T :> IParam> (cv : IParam) = - match cv with - | :? 'T as cv -> Some cv - | _ -> None + static member tryAddUnit (unit : CvUnit) (param : IParamBase) = + ParamBase.tryAddUnit unit param + |> Option.map (fun v -> v :?> IParam) - /// Returns true, if the given param can be downcast - static member inline is<'T when 'T :> IParam> (cv : IParam) = - match cv with - | :? 'T as cv -> true - | _ -> false + //------------------------ ICvBase implementations -----------------------// + + /// Returns the id of the cv item + static member getCvAccession (param: IParam) = CvBase.getCvAccession param - /// Returns the typed value of the param. - static member getParamValue (param : IParam) = - param |> ParamBase.getParamValue + /// Returns the name of the cv item + static member getCvName (param: IParam) = CvBase.getCvName param - /// Returns the value of the Param as a IConvertiböe - static member getValue (param:IParamBase) = - param |> ParamBase.getValue + /// Returns the reference of the cv item + static member getCvRef (param: IParam) = CvBase.getCvRef param + /// Returns the full term of the cv item + static member getTerm (param: IParam) = CvBase.getTerm param - /// Returns the value of the param as a string. - static member getValueAsString (cvParam : IParam) = - (cvParam :> IParamBase).Value - |> ParamValue.getValueAsString + /// Returns true, if the given term matches the term of the cv item + static member equalsTerm (term : CvTerm) (param: IParam) = CvBase.equalsTerm term param - /// Returns the value of the param as an int if possible, else fails. - static member getValueAsInt (cvParam : IParam) = - (cvParam :> IParamBase).Value - |> ParamValue.getValueAsInt + /// Returns true, if the terms of the given param items match + static member equals (param1 : IParam) (param2 : IParam) = CvBase.equals param1 param2 - /// Returns the value of the param as a CvTerm. - static member getValueAsTerm (cvParam : IParam) = - (cvParam :> IParamBase).Value - |> ParamValue.getValueAsTerm + /// Returns true, if the names of the given param items match + static member equalsName (param1 : IParam) (param2 : IParam) = CvBase.equalsName param1 param2 - static member mapValue (f : ParamValue -> ParamValue) (param : IParam) = - ParamBase.mapValue f param :?> IParam + /// Returns Some Value of type 'T, if the given param item can be downcast, else returns None + static member inline tryAs<'T when 'T :> IParam> (param: IParam) = CvBase.tryAs<'T> param - static member tryMapValue (f : ParamValue -> ParamValue option) (param : IParam) = - ParamBase.tryMapValue f param |> Option.map (fun v -> v :?> IParam) + /// Returns true, if the given param item can be downcast + static member inline is<'T when 'T :> IParam> (param : IParam) = CvBase.is<'T> param - static member tryAddName (name : string) (param : IParam) = - ParamBase.tryAddName name param |> Option.map (fun v -> v :?> IParam) + //-------------------- IParam specific implementations -------------------// - static member tryAddAnnotationID (id : string) (param : IParam) = - ParamBase.tryAddAnnotationID id param |> Option.map (fun v -> v :?> IParam) - static member tryAddReference (ref : string) (param : IParam) = - ParamBase.tryAddReference ref param |> Option.map (fun v -> v :?> IParam) +[] +module IParamExtensions = + type CvBase with + /// Returns Some Param, if the given param item can be downcast, else returns None + static member tryParam (cv : ICvBase) = + match cv with + | :? IParam as param -> Some param + | _ -> None - static member tryAddUnit (unit : CvUnit) (param : IParam) = - ParamBase.tryAddUnit unit param |> Option.map (fun v -> v :?> IParam) -// TO DO: create-Funktionen in UserParam und CvParam sollen IParam zurückgeben statt ihren eigenen Typ + type ParamBase with + /// Returns Some Param, if the given value item can be downcast, else returns None + static member tryParam (cv : IParamBase) = + match cv with + | :? IParam as param -> Some param + | _ -> None \ No newline at end of file diff --git a/src/ControlledVocabulary/IParamBase.fs b/src/ControlledVocabulary/IParamBase.fs index ed9a319..5e4482c 100644 --- a/src/ControlledVocabulary/IParamBase.fs +++ b/src/ControlledVocabulary/IParamBase.fs @@ -7,88 +7,88 @@ type IParamBase = abstract member Value : ParamValue abstract member WithValue : ParamValue -> IParamBase -module ParamBase = +type ParamBase = /// Returns the value of the Param as a ParamValue - let getParamValue (param:IParamBase) = + static member getParamValue (param:IParamBase) = param.Value /// Returns the value of the Param as IConvertible - let getValue (param:IParamBase) = + static member getValue (param:IParamBase) = ParamValue.getValue param.Value /// Returns the value of the Param as string - let getValueAsString (param:IParamBase) = + static member getValueAsString (param:IParamBase) = ParamValue.getValueAsString param.Value /// Returns the value of the Param as int if possible, else fails - let getValueAsInt (param:IParamBase) = + static member getValueAsInt (param:IParamBase) = ParamValue.getValueAsInt param.Value /// Returns the value of the Param as a term - let getValueAsTerm (param:IParamBase) = + static member getValueAsTerm (param:IParamBase) = ParamValue.getValueAsTerm param.Value - let tryGetValueAccession (param : #IParamBase) = + static member tryGetValueAccession (param : #IParamBase) = match param.Value with - | CvValue (a,_,_) -> Some a + | CvValue cv -> Some cv.Accession | Value _ -> None // mere Value has no accession number | WithCvUnitAccession _ -> None // use tryGetCvUnitAccession instead //| WithCvUnitAccession (_,(a,_,_)) -> Some a - let tryGetValueRef (param : #IParamBase) = + static member tryGetValueRef (param : #IParamBase) = match param.Value with - | CvValue (_,_,r) -> Some r + | CvValue cv -> Some cv.RefUri | Value _ -> None // mere Value has no ref | WithCvUnitAccession _ -> None // use tryGetCvUnitRef instead - let tryGetCvUnit (param : #IParamBase) : CvUnit option = + static member tryGetCvUnit (param : #IParamBase) : CvUnit option = match param.Value with | Value _ -> None | CvValue _ -> None | WithCvUnitAccession (_,u) -> Some u - let tryGetCvUnitValue (param : #IParamBase) : #IConvertible option = + static member tryGetCvUnitValue (param : #IParamBase) : #IConvertible option = match param.Value with | Value _ -> None | CvValue _ -> None | WithCvUnitAccession (v,_) -> Some v - let tryGetCvUnitName (param : #IParamBase) = + static member tryGetCvUnitTermName (param : #IParamBase) = match param.Value with | Value _ -> None | CvValue _ -> None - | WithCvUnitAccession (_,(_,n,_)) -> Some n + | WithCvUnitAccession (_,cvu) -> Some cvu.Name - let tryGetCvUnitAccession (param : #IParamBase) = + static member tryGetCvUnitTermAccession (param : #IParamBase) = match param.Value with | Value _ -> None | CvValue _ -> None - | WithCvUnitAccession (_,(a,_,_)) -> Some a + | WithCvUnitAccession (_,cvu) -> Some cvu.Accession - let tryGetCvUnitRef (param : #IParamBase) = + static member tryGetCvUnitTermRef (param : #IParamBase) = match param.Value with | Value _ -> None | CvValue _ -> None - | WithCvUnitAccession (_,(_,_,r)) -> Some r + | WithCvUnitAccession (_,cvu) -> Some cvu.RefUri - let mapValue (f : ParamValue -> ParamValue) (param : IParamBase) = + static member mapValue (f : ParamValue -> ParamValue) (param : IParamBase) = param.WithValue(f param.Value) - let tryMapValue (f : ParamValue -> ParamValue option) (param : IParamBase) = + static member tryMapValue (f : ParamValue -> ParamValue option) (param : IParamBase) = match f param.Value with | Some value -> Some (param.WithValue(value)) | None -> None - let tryAddName (name : string) (param : IParamBase) = - tryMapValue (ParamValue.tryAddName name) param + static member tryAddName (value : string) (param : IParamBase) = + ParamBase.tryMapValue (ParamValue.tryAddName value) param - let tryAddAnnotationID (id : string) (param : IParamBase) = - tryMapValue (ParamValue.tryAddAnnotationID id) param + static member tryAddAccession (acc : string) (param : IParamBase) = + ParamBase.tryMapValue (ParamValue.tryAddAccession acc) param - let tryAddReference (ref : string) (param : IParamBase) = - tryMapValue (ParamValue.tryAddReference ref) param + static member tryAddReference (ref : string) (param : IParamBase) = + ParamBase.tryMapValue (ParamValue.tryAddReference ref) param - let tryAddUnit (unit : CvUnit) (param : IParamBase) = - tryMapValue (ParamValue.tryAddUnit unit) param \ No newline at end of file + static member tryAddUnit (unit : CvUnit) (param : IParamBase) = + ParamBase.tryMapValue (ParamValue.tryAddUnit unit) param \ No newline at end of file diff --git a/src/ControlledVocabulary/ParamValue.fs b/src/ControlledVocabulary/ParamValue.fs index e75d639..e6fb1dd 100644 --- a/src/ControlledVocabulary/ParamValue.fs +++ b/src/ControlledVocabulary/ParamValue.fs @@ -12,39 +12,33 @@ type ParamValue = // value * CvUnit | WithCvUnitAccession of cvu : IConvertible * CvUnit - //static member getValueAsStringWithUnit (param:ParamValue) = - // match param with - // | Value v -> string v - // | CvValue (_,v,_) -> v - // | WithCvUnitAccession (v,_) -> string v - /// Returns the value of the Param as IConvertible static member getValue (param:ParamValue) = match param with | Value v -> v - | CvValue (_,v,_) -> v :> IConvertible + | CvValue cv -> cv.Name :> IConvertible | WithCvUnitAccession (v,_) -> v /// Returns the value of the Param as string static member getValueAsString (param:ParamValue) = match param with | Value v -> string v - | CvValue (_,v,_) -> v + | CvValue cv -> cv.Name | WithCvUnitAccession (v,_) -> string v /// Returns the value of the Param as int if possible, else fails static member getValueAsInt (param:ParamValue) = match param with | Value v -> v :?> int - | CvValue (_,v,_) -> v |> int + | CvValue cv -> cv.Name |> int | WithCvUnitAccession (v,_) -> v :?> int /// Returns the value of the Param as a term static member getValueAsTerm (param:ParamValue) = match param with - | Value v -> CvTerm.fromName (v |> string) - | CvValue term -> term - | WithCvUnitAccession (v,_) -> CvTerm.fromName (v |> string) + | Value v -> CvTerm.create(name = (v |> string)) + | CvValue term -> term + | WithCvUnitAccession (v,_) -> CvTerm.create(name = (v |> string)) /// Returns the unit of the Param if it exists, else returns None static member tryGetUnit (param:ParamValue) : CvUnit option = @@ -56,28 +50,28 @@ type ParamValue = /// Returns a new paramValue with the given name if possible, else returns None static member tryAddName (name : string) (param : ParamValue) = match param with - | Value v -> Some (Value name) - | CvValue (id,"",ref) -> Some (CvValue (id,name,ref)) - | CvValue term -> None - | WithCvUnitAccession (_,u) -> None + | Value v -> Some (Value name) + | CvValue cv when cv.Name = "" -> Some (CvValue {cv with Name = name}) + | CvValue term -> None + | WithCvUnitAccession (_,u) -> None /// Returns a new paramValue with the given annotationID if possible, else returns None - static member tryAddAnnotationID (id : string) (param : ParamValue) = + static member tryAddAccession (accession : string) (param : ParamValue) = match param with - | Value (:? string as v) -> Some (CvValue (id,v,"")) - | Value v -> None - | CvValue ("",name,ref) -> Some (CvValue (id,name,ref)) - | CvValue term -> None - | WithCvUnitAccession (_,u) -> None + | Value (:? string as v) -> Some (CvValue (CvTerm.create(accession = accession, name = v, ref = ""))) + | Value v -> None + | CvValue cv when cv.Accession = "" -> Some (CvValue {cv with Accession = accession}) + | CvValue term -> None + | WithCvUnitAccession (_,u) -> None /// Returns a new paramValue with the given reference if possible, else returns None static member tryAddReference (ref : string) (param : ParamValue) = match param with - | Value (:? string as v) -> Some (CvValue ("",v,ref)) - | Value v -> None - | CvValue (id,name,"") -> Some (CvValue (id,name,ref)) - | CvValue term -> None - | WithCvUnitAccession (_,u) -> None + | Value (:? string as v) -> Some (CvValue (CvTerm.create(accession = "", name = v, ref = ref))) + | Value v -> None + | CvValue cv when cv.RefUri = "" -> Some (CvValue {cv with RefUri = ref}) + | CvValue term -> None + | WithCvUnitAccession (_,u) -> None /// Returns a new paramValue with the given unit if possible, else returns None static member tryAddUnit (unit : CvUnit) (param : ParamValue) = diff --git a/src/ControlledVocabulary/UserParam.fs b/src/ControlledVocabulary/UserParam.fs index 4b889a8..6ecc493 100644 --- a/src/ControlledVocabulary/UserParam.fs +++ b/src/ControlledVocabulary/UserParam.fs @@ -9,11 +9,16 @@ type UserParam(name : string, paramValue : ParamValue, attributes : IDictionary< inherit CvAttributeCollection(attributes) + member this.Accession = name + member this.Name = name + member this.RefUri = "UserTerm" + member this.Value = paramValue + interface IParam with - member this.ID = name - member this.Name = name - member this.RefUri = "UserTerm" - member this.Value = paramValue + member this.Accession = this.Accession + member this.Name = this.Name + member this.RefUri = this.RefUri + member this.Value = this.Value member this.WithValue(v : ParamValue) = UserParam(name,v,attributes) member this.HasAttributes with get() = this.Attributes |> Seq.isEmpty |> not @@ -24,26 +29,112 @@ type UserParam(name : string, paramValue : ParamValue, attributes : IDictionary< new (name,pv) = UserParam (name,pv,Seq.empty) - /// Returns Some UserParam, if the given value item can be downcast, else returns None - static member tryUserParam (cv : ICvBase) = - match cv with - | :? UserParam as param -> Some param - | _ -> None + //---------------------- IParam implementations ----------------------// + + /// Returns the value of the Param as a ParamValue + static member getParamValue (up: UserParam) = Param.getParamValue up + + /// Returns the value of the Param as IConvertible + static member getValue (up: UserParam) = Param.getValue up + + /// Returns the value of the Param as string + static member getValueAsString (up: UserParam) = Param.getValueAsString up + + /// Returns the value of the Param as int if possible, else fails + static member getValueAsInt (up: UserParam) = Param.getValueAsInt up + + /// Returns the value of the Param as a term + static member getValueAsTerm (up: UserParam) = Param.getValueAsTerm up + + static member tryGetValueAccession (up: UserParam) = Param.tryGetValueAccession up + + static member tryGetValueRef (up: UserParam) = Param.tryGetValueRef up + + static member tryGetCvUnit (up: UserParam) : CvUnit option = Param.tryGetCvUnit up + + static member tryGetCvUnitValue (up: UserParam) = Param.tryGetCvUnitValue up + + static member tryGetCvUnitTermName (up: UserParam) = Param.tryGetCvUnitTermName up + + static member tryGetCvUnitTermAccession (up: UserParam) = Param.tryGetCvUnitTermAccession up + + static member tryGetCvUnitTermRef (up: UserParam) = Param.tryGetCvUnitTermRef up + + static member mapValue (f : ParamValue -> ParamValue) (up: UserParam) = Param.mapValue f up :?> CvParam + + static member tryMapValue (f : ParamValue -> ParamValue option) (up: UserParam) = + Param.tryMapValue f up + |> Option.map (fun v -> v :?> UserParam) + + static member tryAddName (name : string) (up: UserParam) = + Param.tryAddName name up + |> Option.map (fun v -> v :?> UserParam) - /// Returns Some Param if the given value item can be downcast, else returns None - static member tryUserParam (cv : IParamBase) = - match cv with - | :? UserParam as param -> Some param - | _ -> None + static member tryAddAccession (acc : string) (up: UserParam) = + Param.tryAddAccession acc up + |> Option.map (fun v -> v :?> UserParam) - /// Returns Some Param if the given param item can be downcast, else returns None - static member tryUserParam (cv : IParam) = - match cv with - | :? UserParam as param -> Some param - | _ -> None + static member tryAddReference (ref : string) (up: UserParam) = + Param.tryAddReference ref up + |> Option.map (fun v -> v :?> UserParam) + + static member tryAddUnit (unit : CvUnit) (up: UserParam) = + Param.tryAddUnit unit up + |> Option.map (fun v -> v :?> UserParam) + + /// Returns the id of the cv item + static member getCvAccession (up: UserParam) = Param.getCvAccession up + + /// Returns the name of the cv item + static member getCvName (up: UserParam) = Param.getCvName up + + /// Returns the reference of the cv item + static member getCvRef (up: UserParam) = Param.getCvRef up + + /// Returns the full term of the cv item + static member getTerm (up: UserParam) = Param.getTerm up + + /// Returns true, if the given term matches the term of the cv item + static member equalsTerm (term : CvTerm) (up: UserParam) = Param.equalsTerm term up + + /// Returns true, if the terms of the given param items match + static member equals (up1: UserParam) (up2: UserParam) = Param.equals up1 up2 + + /// Returns true, if the names of the given param items match + static member equalsName (up1: UserParam) (up2: UserParam) = Param.equalsName up1 up2 + + /// Returns Some Value of type 'T, if the given param item can be downcast, else returns None + static member inline tryAs<'T when 'T :> IParam> (up: UserParam) = Param.tryAs<'T> up + + /// Returns true, if the given param item can be downcast + static member inline is<'T when 'T :> IParam> (up: UserParam) = Param.is<'T> up override this.ToString() = $"Name: {(this :> ICvBase).Name}\n\tValue: {(this :> IParamBase).Value}\n\tQualifiers: {this.Keys |> Seq.toList}" member this.DisplayText = - this.ToString() \ No newline at end of file + this.ToString() + +[] +module UserParamExtensions = + + type ParamBase with + /// Returns Some Param if the given value item can be downcast, else returns None + static member tryUserParam (cv : IParamBase) = + match cv with + | :? UserParam as param -> Some param + | _ -> None + + type Param with + /// Returns Some Param if the given param item can be downcast, else returns None + static member tryUserParam (cv : IParam) = + match cv with + | :? UserParam as param -> Some param + | _ -> None + + type CvBase with + /// Returns Some UserParam, if the given value item can be downcast, else returns None + static member tryUserParam (cv : ICvBase) = + match cv with + | :? UserParam as param -> Some param + | _ -> None \ No newline at end of file diff --git a/tests/ARCTokenization.Tests/IntegrationTests/AssayAnnotationTable.fs b/tests/ARCTokenization.Tests/IntegrationTests/AssayAnnotationTable.fs index e474994..e2eb019 100644 --- a/tests/ARCTokenization.Tests/IntegrationTests/AssayAnnotationTable.fs +++ b/tests/ARCTokenization.Tests/IntegrationTests/AssayAnnotationTable.fs @@ -112,7 +112,7 @@ module Correct = id = "Term Accession Number (OBI:0100026)", name = "Characteristic [organism]", ref = "Term Source REF (OBI:0100026)", - pv = (ParamValue.CvValue ("http://purl.obolibrary.org/obo/NCBITaxon_3702","Arabidopsis thaliana","NCBITaxon")), + pv = ParamValue.CvValue (CvTerm.create(accession = "http://purl.obolibrary.org/obo/NCBITaxon_3702", name = "Arabidopsis thaliana", ref = "NCBITaxon")), attributes = [] ) ] diff --git a/tests/ARCTokenization.Tests/IntegrationTests/InvestigationMetadata.fs b/tests/ARCTokenization.Tests/IntegrationTests/InvestigationMetadata.fs index e38c82e..a5a14b7 100644 --- a/tests/ARCTokenization.Tests/IntegrationTests/InvestigationMetadata.fs +++ b/tests/ARCTokenization.Tests/IntegrationTests/InvestigationMetadata.fs @@ -108,11 +108,11 @@ let allExpectedMetadataTermsEmpty = //] Terms.InvestigationMetadata.cvTerms |> List.skip 1 //(ignore root term) - |> List.map (fun p -> CvParam(p, ParamValue.CvValue (CvTerm("AGMO:00000001", "Metadata Section Key", "AGMO")), [])) + |> List.map (fun p -> CvParam(p, ParamValue.CvValue (CvTerm.create(accession = "AGMO:00000001", name = "Metadata Section Key", ref = "AGMO")), [])) [] let ``First Param is CvParam`` () = - Assert.True (parsedInvestigationMetadataEmpty.Head |> CvParam.tryCvParam).IsSome + Assert.True (parsedInvestigationMetadataEmpty.Head |> Param.tryCvParam).IsSome [] let ``First CvParam`` () = CvParam.structuralEquality (parsedInvestigationMetadataEmpty.Head :?> CvParam) allExpectedMetadataTermsEmpty[0] @@ -226,7 +226,7 @@ let allExpectedMetadataTermsFull = values |> List.mapi (fun i v -> if i = 0 then - CvParam(term, ParamValue.CvValue (CvTerm("AGMO:00000001", "Metadata Section Key", "AGMO")), []) + CvParam(term, ParamValue.CvValue (CvTerm.create(accession = "AGMO:00000001", name = "Metadata Section Key", ref = "AGMO")), []) else CvParam(term, ParamValue.Value v, []) ) diff --git a/tests/ARCTokenization.Tests/TestUtils.fs b/tests/ARCTokenization.Tests/TestUtils.fs index 0978b5e..c1c6857 100644 --- a/tests/ARCTokenization.Tests/TestUtils.fs +++ b/tests/ARCTokenization.Tests/TestUtils.fs @@ -13,9 +13,9 @@ module CvParam = (CvBase.getCvName cvpActual) ) - let hasTermName (expectedName : string) (cvpActual : CvParam) = + let hasTermValue (expectedValue : string) (cvpActual : CvParam) = Assert.Equal( - expectedName, + expectedValue, (CvBase.getCvName cvpActual) ) diff --git a/tests/ControlledVocabulary.Tests/ControlledVocabulary.Tests.fsproj b/tests/ControlledVocabulary.Tests/ControlledVocabulary.Tests.fsproj index 15cdadb..3c313ae 100644 --- a/tests/ControlledVocabulary.Tests/ControlledVocabulary.Tests.fsproj +++ b/tests/ControlledVocabulary.Tests/ControlledVocabulary.Tests.fsproj @@ -7,6 +7,8 @@ + + diff --git a/tests/ControlledVocabulary.Tests/CvBaseTests.fs b/tests/ControlledVocabulary.Tests/CvBaseTests.fs index 3c4d44e..d51493c 100644 --- a/tests/ControlledVocabulary.Tests/CvBaseTests.fs +++ b/tests/ControlledVocabulary.Tests/CvBaseTests.fs @@ -3,7 +3,7 @@ open ControlledVocabulary open Xunit -let assayTerm = "ARCO:1234","Assay","ARCO" +let assayTerm = CvTerm.create(accession = "ARCO:1234", name = "Assay",ref = "ARCO") [] let ``ICvBase can be cast to CvParam using generic tryAs`` () = @@ -19,7 +19,7 @@ let ``ICvBase can be cast to CvParam using generic tryAs`` () = [] let ``ICvBase can be cast to CvParam using tryCvParam`` () = let v = CvParam(assayTerm,ParamValue.Value 5) :> ICvBase - let result = CvParam.tryCvParam v + let result = CvBase.tryCvParam v Assert.True(result.IsSome) let result2 = @@ -41,7 +41,7 @@ let ``ICvBase can be cast to UserParam using generic tryAs`` () = [] let ``ICvBase can be cast to UserParam using tryUserParam`` () = let v = UserParam("MyParam",ParamValue.Value 5) :> ICvBase - let result = UserParam.tryUserParam v + let result = CvBase.tryUserParam v Assert.True(result.IsSome) let result2 = diff --git a/tests/ControlledVocabulary.Tests/CvParamTests.fs b/tests/ControlledVocabulary.Tests/CvParamTests.fs new file mode 100644 index 0000000..012c731 --- /dev/null +++ b/tests/ControlledVocabulary.Tests/CvParamTests.fs @@ -0,0 +1,213 @@ +namespace CvParamTests + +open ControlledVocabulary +open ReferenceObjects +open Xunit + +module InstanceMemberTests = + + [] + let ``Accession`` () = + let expected = [testAccession1; testAccession1; testAccession2] + let actual = testCvParams |> List.map (fun x -> x.Accession) + Assert.Equal(expected, actual) + + [] + let ``Name`` () = + let expected = [testName1; testName1; testName2] + let actual = testCvParams |> List.map (fun x -> x.Name) + Assert.Equal(expected, actual) + + [] + let ``RefUri`` () = + let expected = [testRef1; testRef1; testRef2] + let actual = testCvParams |> List.map (fun x -> x.RefUri) + Assert.Equal(expected, actual) + + [] + let ``Value`` () = + let expected = [ParamValue.Value 5; ParamValue.CvValue testTerm2; ParamValue.WithCvUnitAccession (5, testTerm1)] + let actual = testCvParams |> List.map (fun x -> x.Value) + Assert.Equal(expected, actual) + + +module StaticMemberTests = + + [] + let ``getParamValue`` () = + let expected = [ParamValue.Value 5; ParamValue.CvValue testTerm2; ParamValue.WithCvUnitAccession (5, testTerm1)] + let actual = testCvParams |> List.map CvParam.getParamValue + Assert.Equal(expected, actual) + + [] + let ``getValue`` () = + let expected : System.IConvertible list = [5; testTerm2.Name; 5] + let actual = testCvParams |> List.map CvParam.getValue + Assert.Equal(expected, actual) + + [] + let ``getValueAsString`` () = + let expected = ["5"; testTerm2.Name; "5"] + let actual = testCvParams |> List.map CvParam.getValueAsString + Assert.Equal(expected, actual) + + [] + let ``getValueAsInt`` () = + let expected = [5; 5; 5] + let actual = testCvParams |> List.map CvParam.getValueAsInt + Assert.Equal(expected, actual) + + [] + let ``getValueAsTerm`` () = + let expected = [ + CvTerm.create(name = "5") + testTerm2 + CvTerm.create(name = "5") + ] + let actual = testCvParams |> List.map CvParam.getValueAsTerm + Assert.Equal(expected, actual) + + [] + let ``tryGetValueAccession`` () = + let expected = [None; Some testAccession2; None] + let actual = testCvParams |> List.map CvParam.tryGetValueAccession + Assert.Equal List>(expected, actual) + + [] + let ``tryGetValueRef`` () = + let expected = [None; Some testRef2; None] + let actual = testCvParams |> List.map CvParam.tryGetValueRef + Assert.Equal List>(expected, actual) + + [] + let ``tryGetCvUnit`` () = + let expected = [None; None; Some testTerm1] + let actual = testCvParams |> List.map CvParam.tryGetCvUnit + Assert.Equal List>(expected, actual) + + [] + let ``tryGetCvUnitValue`` () = + let expected : (System.IConvertible option) list = [None; None; Some 5] + let actual = testCvParams |> List.map CvParam.tryGetCvUnitValue + Assert.Equal List>(expected, actual) + + [] + let ``tryGetCvUnitTermName`` () = + let expected = [None; None; Some testName1] + let actual = testCvParams |> List.map CvParam.tryGetCvUnitTermName + Assert.Equal List>(expected, actual) + + [] + let ``tryGetCvUnitTermAccession`` () = + let expected = [None; None; Some testAccession1] + let actual = testCvParams |> List.map CvParam.tryGetCvUnitTermAccession + Assert.Equal List>(expected, actual) + + [] + let ``tryGetCvUnitTermRef`` () = + let expected = [None; None; Some testRef2] + let actual = testCvParams |> List.map CvParam.tryGetCvUnitTermRef + Assert.Equal List>(expected, actual) + + [] + let ``mapValue`` () = + let expected = [ParamValue.Value 1; ParamValue.Value 1; ParamValue.Value 1] + let actual = testCvParams |> List.map (CvParam.mapValue (fun _ -> ParamValue.Value 1) >> CvParam.getParamValue) + Assert.Equal(expected, actual) + + [] + let ``tryMapValue`` () = + let expected = [Some (ParamValue.Value 1); Some (ParamValue.Value 1); Some (ParamValue.Value 1)] + let actual = testCvParams |> List.map (CvParam.tryMapValue (fun _ -> Some (ParamValue.Value 1)) >> Option.map CvParam.getParamValue) + Assert.Equal<(ParamValue option) List>(expected, actual) + + [] + let ``tryAddName`` () = + let expected = [Some testName1; None; None] + let actual = testCvParams |> List.map (CvParam.tryAddName testName1 >> Option.map CvParam.getCvName) + Assert.Equal<(string option) List>(expected, actual) + + [] + let ``tryAddAccession`` () = + let expected = [None; None; None] + let actual = testCvParams |> List.map (CvParam.tryAddAccession testAccession1 >> Option.map CvParam.getCvAccession) + Assert.Equal<(string option) List>(expected, actual) + + [] + let ``tryAddReference`` () = + let expected = [None; None; None] + let actual = testCvParams |> List.map (CvParam.tryAddReference testRef1 >> Option.map CvParam.getCvRef) + Assert.Equal<(string option) List>(expected, actual) + + [] + let ``tryAddUnit`` () = + let expected = [Some (ParamValue.WithCvUnitAccession (5, testTerm1)); None; None] + let actual = testCvParams |> List.map (CvParam.tryAddUnit testTerm1 >> Option.map CvParam.getParamValue) + Assert.Equal<(ParamValue option) List>(expected, actual) + + [] + let ``getCvAccession`` () = + let expected = [testAccession1; testAccession1; testAccession2] + let actual = testCvParams |> List.map CvParam.getCvAccession + Assert.Equal(expected, actual) + + [] + let ``getCvName`` () = + let expected = [testName1; testName1; testName2] + let actual = testCvParams |> List.map CvParam.getCvName + Assert.Equal(expected, actual) + + [] + let ``getCvRef`` () = + let expected = [testRef1; testRef1; testRef2] + let actual = testCvParams |> List.map CvParam.getCvRef + Assert.Equal(expected, actual) + + [] + let ``getTerm`` () = + let expected = [testTerm1; testTerm1; testTerm2] + let actual = testCvParams |> List.map CvParam.getTerm + Assert.Equal(expected, actual) + + [] + let ``equalsTerm`` () = + Assert.All( + ( + List.zip + [testTerm1; testTerm1; testTerm2] + testCvParams + ), + (fun (x,y) -> CvParam.equalsTerm x y |> Assert.True) + ) + + [] + let ``equals`` () = + Assert.All( + ( + List.zip + [ + CvParam(testTerm1, ParamValue.Value 5) + CvParam(testTerm1, ParamValue.CvValue testTerm2) + CvParam(testTerm2, ParamValue.WithCvUnitAccession (5, testTerm1)) + ] + testCvParams + ), + (fun (x,y) -> CvParam.equals x y |> Assert.True) + ) + + [] + let ``equalsName`` () = + Assert.All( + ( + List.zip + [ + CvParam(testTerm1, ParamValue.Value 5) + CvParam(testTerm1, ParamValue.CvValue testTerm2) + CvParam(testTerm2, ParamValue.WithCvUnitAccession (5, testTerm1)) + ] + testCvParams + ), + (fun (x,y) -> CvParam.equalsName x y |> Assert.True) + ) + + diff --git a/tests/ControlledVocabulary.Tests/ReferenceObjects.fs b/tests/ControlledVocabulary.Tests/ReferenceObjects.fs new file mode 100644 index 0000000..950cc81 --- /dev/null +++ b/tests/ControlledVocabulary.Tests/ReferenceObjects.fs @@ -0,0 +1,26 @@ +module ReferenceObjects + +open ControlledVocabulary + +let testAccession1 = "TO:00000001" +let testName1 = "Test" +let testRef1 = "TO" + +let testTerm1 = CvTerm.create(accession = testAccession1, name = testName1, ref = testRef1) + +let testAccession2 = "TO:00000002" +let testName2 = "5" +let testRef2 = "TO" + +let testTerm2 = CvTerm.create(accession = testAccession2, name = testName2, ref = testRef2) + +let ``CvParam with ParamValue.Value`` = CvParam(testTerm1, ParamValue.Value 5) +let ``CvParam with ParamValue.CvValue`` = CvParam(testTerm1, ParamValue.CvValue testTerm2) +let ``CvParam with ParamValue.WithCvUnitAccession`` = CvParam(testTerm2, ParamValue.WithCvUnitAccession (5, testTerm1)) + +let testCvParams = + [ + ``CvParam with ParamValue.Value`` + ``CvParam with ParamValue.CvValue`` + ``CvParam with ParamValue.WithCvUnitAccession`` + ] \ No newline at end of file