Skip to content
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

#19 Add equality #32

Merged
merged 5 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build.cmd
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
@echo off
cls

dotnet run --project ./build/Build.fsproj %*
41 changes: 38 additions & 3 deletions playground.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,52 @@ open System.Collections.Generic
//#r @"C:\Repos\nfdi4plants\ArcGraphModel\src\ArcGraphModel\bin\Debug\netstandard2.0\ArcGraphModel.dll"
//#r @"C:\Repos\nfdi4plants\ArcGraphModel\src\ArcGraphModel.IO\bin\Debug\netstandard2.0\ArcGraphModel.IO.dll"
//#r @"C:/Users/olive/.nuget/packages/fsharpaux/1.1.0/lib/net5.0/FSharpAux.dll"
#r "src/ArcGraphModel/bin/Release/netstandard2.0/ArcGraphModel.dll"
#r "src/ControlledVocabulary/bin/Release/netstandard2.0/ControlledVocabulary.dll"
//#r "src/ArcGraphModel/bin/Release/netstandard2.0/ArcGraphModel.dll"
//#r "src/ControlledVocabulary/bin/Release/netstandard2.0/ControlledVocabulary.dll"
#I "src/ControlledVocabulary/bin/Debug/netstandard2.0"
#I "src/ControlledVocabulary/bin/Release/netstandard2.0"
#r "ControlledVocabulary.dll"
#I "src/ARCTokenization/bin/Debug/netstandard2.0"
#I "src/ARCTokenization/bin/Release/netstandard2.0"
#r "ARCTokenization.dll"

open FsSpreadsheet
open FsSpreadsheet.ExcelIO
open FsOboParser
//open FsSpreadsheet.DSL
open ControlledVocabulary
open ControlledVocabulary.ParamBase
open type ControlledVocabulary.ParamBase
open ARCTokenization

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``
]
let testCvp1 = CvParam("test", "test", "test", ParamValue.Value "test", Dictionary<string,IParam>() |> fun d -> d.Add("test", testCvParams.Head); d)
let testCvp2 = CvParam("test", "test", "test", ParamValue.Value "test", Dictionary<string,IParam>() |> fun d -> d.Add("test", testCvParams.Head); d)
let actual = testCvp1 = testCvp2
testCvp1.GetHashCode()
testCvp2.GetHashCode()



let expectedTermValuesSimple =
[
Expand Down
16 changes: 16 additions & 0 deletions src/ControlledVocabulary/CvContainer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ type CvContainer (
CvContainer(term, dict)
new (term : CvTerm) = CvContainer (term, Seq.empty)

/// Serves as the default hash function.
override this.GetHashCode() =
hash (cvAccession, cvName, cvRef, attributes, properties)

/// Determines whether the specified object is equals to the current object.
override this.Equals(o) =
match o with
| :? CvContainer as cvc ->
(cvc :> ICvBase).Accession = (this :> ICvBase).Accession &&
(cvc :> ICvBase).Name = (this :> ICvBase).Name &&
(cvc :> ICvBase).RefUri = (this :> ICvBase).RefUri &&
cvc.Attributes = this.Attributes &&
cvc.Properties = this.Properties
| :? ICvBase as cvb -> CvBase.equals cvb this
| _ -> false

/// Returns Some CvContainer, if the given cv item can be downcast, else returns None
static member tryCvContainer (cv : ICvBase) =
match cv with
Expand Down
10 changes: 10 additions & 0 deletions src/ControlledVocabulary/CvObject.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ type CvObject<'T>(cvAccession : string, cvName : string, cvRef : string, object

new (term: CvTerm, object : 'T, attributes) = CvObject (term.Accession, term.Name, term.RefUri, object, attributes)

override this.Equals(o) =
match o with
| :? CvObject<'T> as cvo -> this.GetHashCode() = cvo.GetHashCode()
| :? ICvBase as cvb -> CvBase.equals cvb this
| _ -> false

/// Serves as the default hash function.
override this.GetHashCode() =
hash (cvAccession, cvName, cvRef, attributes)

member this.Object = object

override this.ToString() =
Expand Down
30 changes: 22 additions & 8 deletions src/ControlledVocabulary/CvParam.fs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,28 @@ type CvParam(cvAccession : string, cvName : string, cvRef : string, paramValue :
new (cvTerm,v : IConvertible) =
CvParam (cvTerm,ParamValue.Value v)

member this.Equals (term : CvTerm) =
Param.equalsTerm term this

member this.Equals (cv : ICvBase) =
CvBase.equals cv this

member this.Equals (cvp : CvParam) =
this.Equals(cvp :> ICvBase)
/// Serves as the default hash function.
override this.GetHashCode() =
hash (cvAccession, cvName, cvRef, paramValue, attributes)

/// Determines whether the specified object is equals to the current object.
override this.Equals(o) =
match o with
| :? CvTerm as cvt -> Param.equalsTerm cvt this
| :? CvParam as cvp ->
cvp.Name = this.Name &&
cvp.Accession = this.Accession &&
cvp.RefUri = this.RefUri &&
cvp.Value = this.Value &&
cvp.Attributes = this.Attributes // careful bc of Dictionary! Comment out if necessary!
| :? IParam as p ->
p.Name = this.Name &&
p.Accession = this.Accession &&
p.RefUri = this.Name &&
p.Value = this.Value
| :? ICvBase as cvb -> CvBase.equals cvb this
| :? IParamBase as pb -> pb.Value = this.Value
| _ -> false

//---------------------- IParam implementations ----------------------//

Expand Down
19 changes: 19 additions & 0 deletions src/ControlledVocabulary/UserParam.fs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@ type UserParam(name : string, paramValue : ParamValue, attributes : IDictionary<
new (name,pv) =
UserParam (name,pv,Seq.empty)

/// Serves as the default hash function.
override this.GetHashCode() =
hash (name, paramValue, attributes)

/// Determines whether the specified object is equals to the current object.
override this.Equals(o) =
match o with
| :? CvTerm as cvt -> Param.equalsTerm cvt this
| :? UserParam as up ->
up.Name = this.Name &&
up.Value = this.Value &&
up.Attributes = this.Attributes
| :? IParam as p ->
p.Name = this.Name &&
p.Value = this.Value
| :? ICvBase as cvb -> CvBase.equals cvb this
| :? IParamBase as pb -> pb.Value = this.Value
| _ -> false

//---------------------- IParam implementations ----------------------//

/// Returns the value of the Param as a ParamValue
Expand Down
11 changes: 11 additions & 0 deletions tests/ControlledVocabulary.Tests/CvParamTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

open ControlledVocabulary
open ReferenceObjects

open Xunit

open System.Collections


module InstanceMemberTests =

[<Fact>]
Expand All @@ -30,6 +34,13 @@ module InstanceMemberTests =
let actual = testCvParams |> List.map (fun x -> x.Value)
Assert.Equal<ParamValue List>(expected, actual)

[<Fact>]
let ``Equals`` () =
let testCvp1 = CvParam("test", "test", "test", ParamValue.Value "test", Generic.Dictionary<string,IParam>() |> fun d -> d.Add("test", testCvParams.Head); d)
let testCvp2 = CvParam("test", "test", "test", ParamValue.Value "test", Generic.Dictionary<string,IParam>() |> fun d -> d.Add("test", testCvParams.Head); d)
Copy link
Member

@kMutagene kMutagene Aug 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some tests for CvParams with attributes please? I am thinking of one where we expect them to be equal, and one where we expect them not to be equal

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, clarification: Attributes where the attributes themselves have attributes

let actual = testCvp1 = testCvp2
Assert.True(actual)


module StaticMemberTests =

Expand Down
Loading