Attribute VB_Name = "pm"
Public Sub SF()
'enable and disable buttons
'in option window according to data provided
Dim i As Integer
Dim maxact As Integer
Dim pos(1 To 1000) As Integer
Dim logic(1 To 1000, 1 To 1000) As Integer
Dim activity(1 To 1000) As String
Dim pid(1 To 1000, 1 To 1000) As String
Dim actid(1 To 1000) As String
Dim time(1 To 1000) As Single
Dim cost(1 To 1000) As Single
Dim resource(1 To 1000) As Single
Dim es(1 To 1000) As Single
Dim ef(1 To 1000) As Single
Dim ls(1 To 1000) As Single
Dim lf(1 To 1000) As Single
Dim j As Integer
Dim k As Integer

i = 0
Open "project.data" For Input As #4
  Do While Not EOF(4)        ' Loop until end of file
   i = i + 1
   Line Input #4, activity(i)
   Line Input #4, actid(i)
  Line Input #4, temp
   data = Split(temp)
   j = UBound(data)
   For k = 0 To j
   pid(i, k + 1) = data(k)
   logic(i, k + 1) = 1
      Next k
   Line Input #4, temp
   time(i) = CSng(temp)
   Line Input #4, temp
   cost(i) = CSng(temp)
   Line Input #4, temp
   resource(i) = CSng(temp)
    Loop
Close #4
maxact = i


Dim sumresource As Single
Dim sumtime As Single
Dim sumcost As Single
sumresource = 0
sumcost = 0
sumtime = 0
For i = 1 To maxact
sumresource = resource(i) + sumresource
sumtime = sumtime + time(i)
sumcost = sumcost + cost(i)
Next i

If sumresource = 0 Then
'disable resourceprofile,
'resource allocation,resource leveling
options.Command4.Enabled = False
options.Command5.Enabled = False
options.Command6.Enabled = False
Else
options.Command4.Enabled = True
options.Command5.Enabled = True
options.Command6.Enabled = True
End If
If sumcost = 0 Then
'disable project cost button
options.Command8.Enabled = False
Else
options.Command8.Enabled = True
End If
If sumtime = 0 Then
'disable CPM button
'disable Grantt chart
options.Command1.Enabled = False
options.Command2.Enabled = False
Else
''here we calculate start time and
''finish time of activity and store in a file named SF.
    
    options.Command1.Enabled = True
    options.Command2.Enabled = True
   Dim logical(1 To 1000, 1 To 1000) As Integer
    For i = 1 To maxact
  For j = 1 To maxact
  For k = 1 To maxact
  If pid(i, j) = actid(k) Then
  logical(i, k) = -1
  logical(k, i) = 1
  End If
  Next k
  Next j
  Next i
  
  'finding and recording topological order
Dim counterpos As Integer
Dim counter As Integer
Dim count As Integer
Dim l As Integer
Dim m As Integer
Dim sum As Integer
Dim track As Integer
Dim trackcurrent As Integer
Dim temparray(1 To 1001) As String
Dim X As Integer
Dim x1 As Integer
Dim b As Integer

trackcurrent = 1
counterpos = 1
count = 1
temparray(1) = 0
X = 1
x1 = 0
b = 0
k = 1
            
Do While X > 0
X = X - 1
temp = temparray(k)
k = k + 1
For i = 1 To maxact
For j = 1 To maxact
If logic(i, j) = 1 Then
If pid(i, j) = temp Then
logic(i, j) = 0
sum = 0
For l = 1 To maxact
sum = sum + logic(i, l)
Next l
If sum = 0 Then
count = count + 1
pos(i) = counterpos
temparray(count) = actid(i)
x1 = x1 + 1
      
End If
End If
End If
Next j
Next i
b = x1 + b
x1 = 0
If X = 0 Then
X = b
b = 0
counterpos = counterpos + 1
End If
Loop
   
'topological ordering ends


'code to get the max position ,that is to get max col. in graph
Dim temp1 As Single
 For i = 1 To maxact
    If temp1 < pos(i) Then
    temp1 = pos(i)
    End If
    Next i
    
'For m = 1 To maxact

For l = 1 To temp1 'for pos
  For i = 1 To maxact
  For j = 1 To maxact
  If pos(i) = l Then 'pos
  If logical(i, j) = -1 Then
  If ef(j) > es(i) Then
  es(i) = ef(j)
  End If
  End If
  End If 'pos
  Next j
  If pos(i) = l Then 'pos
  ef(i) = es(i) + time(i)
  End If 'pos
  Next i
  Next l
  
  Open "SF" For Output As #100
  For i = 1 To maxact
        Print #100, es(i)
        Print #100, ef(i)
        Next i
    Close #100
    i = 0

End If

End Sub
