From 505c64d16b2b981dde48c08ddbdb9977b809e7e5 Mon Sep 17 00:00:00 2001
From: Ruud Overeem <overeem@astron.nl>
Date: Wed, 2 Feb 2011 15:07:31 +0000
Subject: [PATCH] Bug 809: Implemented complete new algorithm for finding the
 sort order. It works top down iso bottom up.

---
 MAC/Deployment/data/OTDB/StationControl.comp |   4 -
 MAC/Deployment/data/OTDB/StorageNode.base    |   2 -
 MAC/Deployment/data/OTDB/create_OTDB_comps   |   1 +
 MAC/Deployment/data/OTDB/load_components     | 110 ++++++++-----------
 4 files changed, 48 insertions(+), 69 deletions(-)

diff --git a/MAC/Deployment/data/OTDB/StationControl.comp b/MAC/Deployment/data/OTDB/StationControl.comp
index 058337f5934..295bcc4d9f7 100644
--- a/MAC/Deployment/data/OTDB/StationControl.comp
+++ b/MAC/Deployment/data/OTDB/StationControl.comp
@@ -10,10 +10,6 @@ node  StationControl   4.0.0  development 'node constraint'  "Main controller fo
 #--------------------------------------------------------------------------------------------------------
 par  _hostname     I    vtext   -      10    100  '>>VirtualInstrument.stationList'    -        "Machine(s) the program should run on, eg. [CS010C,CS016C]"
 
-
-#########################################################################################################
-# -- StationControl --
 #      name              vers   qual  constr.            descr.
 #--------------------------------------------------------------------------------------------------------
-node  StationControl     4.0.0  development  'node constraint'  "Machine(s) the program should run on, eg. [CS010C,CS016C]"
 uses  TBBControl         4.0.0  development  'node constraint'  "TBBControl settings"
diff --git a/MAC/Deployment/data/OTDB/StorageNode.base b/MAC/Deployment/data/OTDB/StorageNode.base
index 0d3dfcbf2af..528afd0bf59 100644
--- a/MAC/Deployment/data/OTDB/StorageNode.base
+++ b/MAC/Deployment/data/OTDB/StorageNode.base
@@ -12,5 +12,3 @@ par  IPAddress               I    text   -      10    0    '@IPaddress@'
 par  RAM                     I    int    RAM    10    0    1024                    -        'RAM space on this node'
 par  Storage                 I    int    DISK   10    0    2048                    -        'Storage space on this node'
 
-
-
diff --git a/MAC/Deployment/data/OTDB/create_OTDB_comps b/MAC/Deployment/data/OTDB/create_OTDB_comps
index 7c090f29153..af6d83553ec 100755
--- a/MAC/Deployment/data/OTDB/create_OTDB_comps
+++ b/MAC/Deployment/data/OTDB/create_OTDB_comps
@@ -174,6 +174,7 @@ substitute_station()
 	done
 
 	substitute_marker /tmp/foo $2
+	rm -f /tmp/foo.list
 }
 
 
diff --git a/MAC/Deployment/data/OTDB/load_components b/MAC/Deployment/data/OTDB/load_components
index 9179af3a132..495ca1ef4a7 100755
--- a/MAC/Deployment/data/OTDB/load_components
+++ b/MAC/Deployment/data/OTDB/load_components
@@ -2,7 +2,7 @@
 #
 # load_components : loads all components in an OTDB database
 #
-# Copyright (C) 2006
+# Copyright (C) 2006-2011
 # ASTRON (Netherlands Foundation for Research in Astronomy)
 # P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, seg@astron.nl
 #
@@ -24,7 +24,7 @@
 #
 # $Id$
 #
-VERSION="v1.1"
+VERSION="v2.0"		# Implemented complete new algorithm that works top down iso bottom up.
 
 # SyntaxError msg
 #
@@ -42,22 +42,29 @@ SyntaxError()
 }
 
 #
-# Add all nodes that are not 'used' to the resultfile (COMPORDERFILE)
-# and update the 'node' and 'uses' file by deleting the nodes or childs
-# from these files.
-solve_layer()
+# Recursive solve the childs of the current node and construct a order-file
+# during the descent into the tree.
+# 
+solveNode() 
 {
 	echo -n "." 
-	comm -23 ${NODEFILE} ${USESFILE} >>${COMPORDERFILE}
-	comm -23 ${NODEFILE} ${USESFILE} | while read component
+	nodeFile=`grep "^$1 " ${NODELISTFILE} | cut -d' ' -f2`
+
+	# update file admin
+	grep -v ^${nodeFile} ${UNUSEDFILES} >${WORKFILE}
+	mv ${WORKFILE} ${UNUSEDFILES}
+
+	grep "^uses" $nodeFile | sed "s/\\t/ /g" | sed "s/   / /g" | sed "s/  / /g" | cut -d' ' -f2 | while read nodeName
 	do
-		grep -v ^${component} ${NODEFILE} >tmp
-		mv tmp ${NODEFILE}
-		grep ^${component} ${RELFILE} | while read parent child
-		do
-			grep -v ^${child} ${USESFILE} >tmp
-			mv tmp ${USESFILE}
-		done
+		nodeFile=`grep "^$nodeName " ${NODELISTFILE} | cut -d' ' -f2`
+		grep "^$nodeFile" ${REVERSORDERFILE} >> /dev/null
+		if [ $? == 1 ]; then
+#			echo $nodeName
+			echo $nodeFile >>${REVERSORDERFILE}
+			solveNode $nodeName
+#		else
+#			echo "Endnode: $nodeName , file=$nodeFile"
+		fi
 	done
 }
 
@@ -71,65 +78,42 @@ if [ $# -eq 0 ]; then
 fi
 
 # We use a lot of tmpfile to make life easier.
-NODEFILE=/tmp/node
-USESFILE=/tmp/uses
-RELFILE=/tmp/relation
-COMPORDERFILE=/tmp/comporder
-COMPDEFFILE=/tmp/compdef
-FILERELFILE=/tmp/filerel
-RESULTFILE=fileOrder
+NODELISTFILE=/tmp/nodeList
+REVERSORDERFILE=/tmp/reverseOrder
+RESULTFILE=/tmp/fileOrder
+UNUSEDFILES=/tmp/unusedFiles
+WORKFILE=/tmp/tmpFile4load_components
 
+# First construct gcompfile using .base-files.
 ./create_OTDB_comps
 
-# construct a file with 'node uses file' relations
-grep -e '^node' -e '^uses' *comp | cut -d":" -f2 | awk '
-	BEGIN {
-		node="";
-	};
-	{
-		if ($1 == "node") {
-			node = $2;
-		}
-		else {
-			print node"	"$2;
-		}
-	} ' | sort | uniq >${RELFILE}
-
-# make file with all node-names and all uses-names.
-grep -e '^node' *comp | awk '{ print $2 }' | sort | uniq >${NODEFILE}
-grep -e '^uses' *comp | awk '{ print $2 }' | sort | uniq >${USESFILE}
+# construct a file with 'node file' relations
+grep "^node" *comp | sed "s/:/ /" | sed "s/\\t/ /g" | sed "s/   / /g" | sed "s/  / /g" | cut -d' ' -f1,3 | awk '{ print $2" "$1 }' >${NODELISTFILE}
+
+# construct a file with all componentfiles
+ls -1 *comp >${UNUSEDFILES}
 
 # strip off layer for layer
->${COMPORDERFILE}
 echo -n "Analysing load order: "
-while [ -s ${USESFILE} ]
-do
-	solve_layer
-done
-cat ${NODEFILE} >>${COMPORDERFILE}
-rm ${RELFILE} ${NODEFILE} ${USESFILE}
+echo LOFAR.comp >${REVERSORDERFILE}
+solveNode LOFAR
 echo "."
 
-# we now have a file with components (in reverse) order
-# add the filenames in which the components are defined, remove duplicates
-# and finally make sure each name only exists once in the resultfile
-grep -e '^node' *comp | sed "s/:/ /" | awk '{ print $3"	"$1 }' | sort | uniq >${FILERELFILE}
-tac ${COMPORDERFILE} | while read component
-do
-	grep ^${component} ${FILERELFILE} 
-done | \
-awk '{ print $2 }' | uniq | \
-awk '{
-	if (!($1 in names)) {
-		print $1;
-		names[$1]=$1;
-	}
-}' >${RESULTFILE}
-rm ${COMPORDERFILE} ${FILERELFILE} 
+# we now have a file with components (in reverse) order, swap the order
+tac ${REVERSORDERFILE} >${RESULTFILE}
+rm -f ${NODELISTFILE} ${REVERSORDERFILE}
 
 databasename=$1
 shift
 echo ../../build/gnunew_debug/data/bin/load_OTDB_comps $databasename ${RESULTFILE} $*
 #../../build/gnunew_debug/data/bin/load_OTDB_comps $databasename ${RESULTFILE} $*
 ../../../../../LOFAR/installed/gnu_debug/bin/load_OTDB_comps $databasename ${RESULTFILE} -h RS005 $*
-rm ${RESULTFILE}
+
+# when there are files we didn't use report that to the user
+if [ -s ${UNUSEDFILES} ]; then
+	echo
+	echo "  The following files are NOT LOADED into the database because they are NOT part of the current defined LOFAR tree:"
+	cat ${UNUSEDFILES}
+fi
+rm -f ${UNUSEDFILES} ${RESULTFILE}
+
-- 
GitLab