在ruby中使用哪种数据结构实现家谱?

时间:2019-10-09 19:54:09

标签: ruby data-structures graph tree

我正在尝试在ruby中创建一个简单的家谱,以便可以通过母节点添加子代。同样,当我输入姓名和关系作为输入时,我应该能够将输出作为与给定人物姓名相关的人物姓名。 例如,我应该能够执行以下操作 add_child('Tina', 'bob') // which will add bob as a child node to Tina get_relation(bob, maternal_uncles) // which should output all the siblings of Tina in this case.

哪种数据结构最适合实现此目标,以及如何在ruby中实现它?在我的研究中,我发现图是一种很好的方法,并且自两天以来我一直在研究它的实现,但是找不到任何解决方案。

我尝试了以下库

RubyTree https://github.com/evolve75/RubyTree-这帮助我建立了父母,兄弟姐妹,祖父母的关系,但我想不起来如何利用这种关系来获得父亲的兄弟((父),妻子的姐妹( in子)等

weighted graph https://github.com/msayson/weighted_graph-我用0代表配偶,用1代表孩子。我不能从这里到任何地方。我对如何让某个人的父母和孩子感到困惑。

我对ruby prefix treesrgl gem进行了一些探索,但无法将它们应用于我的应用程序。

请帮助。预先感谢!

1 个答案:

答案 0 :(得分:0)

我可以找出一种使用if(Get-Module -ListAvailable -Name SqlServer) {} else {Install-Module -Name SqlServer -AllowClobber -Confirm:$False -Force} #installs SqlServer module if not installed function Get-CallRecordDates { Invoke-Sqlcmd -Query “SELECT [Date],[Time],[Commentary] FROM [CallRecords].[dbo].[ForSQLload] order by [Date],[Time]” -ServerInstance "myserver" } $dates = @(Get-CallRecordDates) #declare where the csv files will be saved $csv = "G:\SQLDataLoad\Avaya" #declare where the .acsauto file will be saved $acsauto = "G:\SQLDataLoad\Avaya\CallRecordsJS.acsauto" #These will format the acsauto file just right $blockstart = "'LANGUAGE=ENU 'SERVERNAME=AvayaServerAddress Public Sub Main() On Error Resume Next cvsSrv.Reports.ACD = 1 Set Info = cvsSrv.Reports.Reports(`"Historical\Designer\Call Records SL`") If Info Is not Nothing Then " $blockend = "If Not cvsSrv.Interactive Then cvsSrv.ActiveTasks.Remove Rep.TaskID Set Rep = Nothing End If Set Info = Nothing End Sub" #Start creating the content for acsauto file $contentacsauto = $blockstart #looping through the missing records. If there are none, it will only download the latest data. for($i=0; $i -lt $dates.Count; $i++) { $maindate = Get-Date "$(Get-Date $dates.Item($i).Date -Format 'dd/MM/yyyy') $(Get-Date $dates.Item($i).Time -Format 'HH:mm')" if($dates.Item($i).Commentary -eq "Last"){ $startdatetime = $maindate.AddHours(-3) $enddatetime = Get-Date Write-Host "Adding time range to the script; from $startdatetime to $enddatetime `nIt will be exported to $csv\CallRecords-$i.csv`nTime is $(Get-Date) now." }else{ $startdatetime = $maindate.AddHours(-1) $enddatetime = $maindate.AddHours(1) Write-Host "Adding time range to the script as some CallID is missing in that period; from $startdatetime to $enddatetime `nIt will be exported to $csv\CallRecords-$i.csv" } #formating the dates and times for the parameters $startdate = Get-Date $startdatetime -Format "dd/MM/yyyy" $starttime = Get-Date $startdatetime -Format "HH:mm" $enddate = Get-Date $enddatetime -Format "dd/MM/yyyy" $endtime = Get-Date $enddatetime -Format "HH:mm" $MiddleBlock = " If cvsSrv.Reports.CreateReport(Info,Rep) Then Rep.TimeZone = `"default`" Rep.SetProperty `"Start Date`",`"$startdate`" Rep.SetProperty `"Start Time`",`"$starttime`" Rep.SetProperty `"Stop Date`",`"$enddate`" Rep.SetProperty `"Stop Time`",`"$endtime`" b = Rep.ExportData(`"$csv\CallRecords-$i.csv`", 44, 0, True, True, True) Rep.Quit End If " $contentacsauto = $contentacsauto + $MiddleBlock } $contentacsauto = $contentacsauto + $blockend $contentacsauto | Out-File $acsauto -Encoding ascii & $acsauto $stop = (Get-Date).AddMinutes(10) while (((Get-ChildItem $csv -Filter "CallRecord*.csv").Count -lt $i) -and ($(Get-Date) -lt $stop)) {Start-Sleep -Seconds 2} if($(Get-Date) -ge $stop) { Write-Host "This thing timed out as it took longer than 10 minutes to export, at $(Get-Date)" Get-Process "acs*" | Stop-Process -PassThru | Out-Null} else{ Write-Host "Saved $((Get-ChildItem $csv -Filter "CallRecord*.csv").Count) file(s) at $(Get-Date)" Start-Sleep -Seconds 2 $csvfiles = Get-ChildItem $csv -Filter "CallRecord*.csv" $callrecords = Import-Csv -Path $csvfiles | Sort-Object 'Call ID','Segment' -Unique Remove-Item $csvfiles -Force | Out-Null Invoke-Sqlcmd -Query "delete from CallRecords.dbo.TempCallDataTemp" -ServerInstance "myserver" Write-SqlTableData -InputData $callrecords -ServerInstance "myserver" -DatabaseName "CallRecords" -SchemaName "dbo" -TableName "TempCallDataTemp" -force } 本身来获得最小关系的方法。 RubyTree具有内置方法,例如父级,同级,子级等,我们也可以将内容传递给节点。 所以我用这些来得到我想要的。例如,为了创建一个配偶,我在根节点上创建了一个子节点,并在内容中传递了像RubyTree这样的哈希。这样,我就可以通过在此哈希上放置条件来应用逻辑并获得我想要的关系

示例:

{relation: spouse}