edge_paths_with_PE_controll.xsl 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns:xs="http://www.w3.org/2001/XMLSchema"
  4. xmlns:bp="https://procesy5.pl/biale_plamy-schema.xsd"
  5. exclude-result-prefixes="xs"
  6. version="2.0">
  7. <xsl:import href="PE_wariant_bit_table.xsl"/>
  8. <xsl:output indent="yes"/>
  9. <xsl:strip-space elements="*"/>
  10. <xsl:param name="PE_Price" select="1500"/>
  11. <xsl:param name="Cable_Price" select="10"/>
  12. <xsl:param name="edge_paths_with_PE.xml"/>
  13. <!-- bedziemy od najdluzszej drogi starac sie rozmiescic PE w oparciu o rozmieszczenie klientow oraz odleglosci -->
  14. <xsl:template match="edge_joins_filled_paths">
  15. <xsl:variable name="edge_joins_filled_paths_len">
  16. <xsl:for-each select="item">
  17. <xsl:sort select="number(asText/@len)" order="descending"/>
  18. <Path>
  19. <xsl:attribute name="id" select="@id"/>
  20. <xsl:attribute name="len" select="asText/@len"/>
  21. <xsl:attribute name="Points_cnt" select="asText/@Points_cnt"/>
  22. </Path>
  23. </xsl:for-each>
  24. </xsl:variable>
  25. <xsl:variable name="edge_paths_with_PE_controll">
  26. <edge_paths_with_PE_controll>
  27. <xsl:apply-templates select="$edge_joins_filled_paths_len" mode="PointMatrixGroup"/>
  28. </edge_paths_with_PE_controll>
  29. </xsl:variable>
  30. <xsl:copy-of select="$edge_paths_with_PE_controll"/>
  31. <xsl:result-document href="edge_paths_with_PE.xml">
  32. <xsl:apply-templates select="$edge_paths_with_PE_controll" mode="edge_paths_with_PE.xml"/>
  33. </xsl:result-document>
  34. </xsl:template>
  35. <xsl:template match="edge_paths_with_PE_controll" mode="edge_paths_with_PE.xml">
  36. <PE_candidates_dump>
  37. <xsl:apply-templates mode="edge_paths_with_PE.xml"/>
  38. </PE_candidates_dump>
  39. </xsl:template>
  40. <xsl:template match="Path" mode="edge_paths_with_PE.xml">
  41. <LINESTRING>
  42. <xsl:attribute name="ID_Way" select="@id"/>
  43. <xsl:attribute name="Count" select="@Count"/>
  44. <xsl:attribute name="ID_Point_count" select="@Points_cnt"/>
  45. <xsl:attribute name="distance"/>
  46. <xsl:attribute name="dumb_frequency" select="''"/>
  47. <xsl:apply-templates mode="edge_paths_with_PE.xml"/>
  48. </LINESTRING>
  49. </xsl:template>
  50. <xsl:template match="PointMatrixGroupPE" mode="edge_paths_with_PE.xml">
  51. <xsl:apply-templates mode="edge_paths_with_PE.xml"/>
  52. </xsl:template>
  53. <xsl:template match="PointMatrixGroup" mode="edge_paths_with_PE.xml">
  54. <!--<test_matrux><xsl:copy-of select="."></xsl:copy-of></test_matrux>-->
  55. <xsl:apply-templates mode="edge_paths_with_PE.xml"/>
  56. </xsl:template>
  57. <xsl:template match="odpalam_liczenie" mode="edge_paths_with_PE.xml">
  58. <xsl:apply-templates mode="edge_paths_with_PE.xml"/>
  59. </xsl:template>
  60. <xsl:template match="koniec_wariantow" mode="edge_paths_with_PE.xml">
  61. <xsl:apply-templates mode="edge_paths_with_PE.xml"/>
  62. </xsl:template>
  63. <xsl:template match="bp:Path_Point" mode="edge_paths_with_PE.xml">
  64. <xsl:copy-of select="."/>
  65. </xsl:template>
  66. <xsl:template match="Path" mode="PointMatrixGroup">
  67. <Path>
  68. <xsl:variable name="edge_joins_filled_path" select=" doc(concat('edge_joins_filled_paths/edge_joins_filled_paths.',@id,'.xml'))"/>
  69. <xsl:attribute name="id" select="@id"/>
  70. <xsl:attribute name="Points_cnt" select="@Points_cnt"/>
  71. <xsl:variable name="Points_cnt" select="@Points_cnt"/>
  72. <xsl:attribute name="len" select="@len"/>
  73. <xsl:attribute name="Count" select="count($edge_joins_filled_path/edge_joins_filled_paths_xml/asText_XMl/LineString)"/>
  74. <xsl:variable name="len" select="@len"/>
  75. <!--<xsl:variable name="edge_joins_filled_paths" select="collection( 'edge_joins_filled_paths?select=*.xml;recurse=yes')"/>-->
  76. <!--<EdgeWayJoinsPair>
  77. <xsl:copy-of select="$edge_joins_filled_path"></xsl:copy-of>
  78. </EdgeWayJoinsPair>-->
  79. <!-- liczmy najwieksza roznice odleglosci miedzy punktami -->
  80. <!-- rozmieszczamy prawidlowo PE na danej trasie -->
  81. <xsl:variable name="EconomicDistanceCut">
  82. <EconomicDistanceCut>
  83. <xsl:for-each select="$edge_joins_filled_path/edge_joins_filled_paths_xml/asText_XMl/LineString">
  84. <distance>
  85. <xsl:attribute name="pos" select="position()"/>
  86. <xsl:attribute name="not_id" select="@not_id"/>
  87. <xsl:attribute name="len" select="@len"/>
  88. <xsl:attribute name="id_path" select="@id_path"/>
  89. <xsl:attribute name="Points" select="count(Points)"/>
  90. <xsl:variable name="Points" select="count(Points)"/>
  91. <xsl:variable name="Ways" select="count(Ways)"/>
  92. <xsl:choose>
  93. <xsl:when test="$Ways or $Points">
  94. <xsl:attribute name="point_count" select="$Points + $Ways"/>
  95. </xsl:when>
  96. </xsl:choose>
  97. </distance>
  98. </xsl:for-each>
  99. </EconomicDistanceCut>
  100. </xsl:variable>
  101. <!--<xsl:copy-of select="$EconomicDistanceCut"/>-->
  102. <xsl:variable name="EconomicDistanceCut2">
  103. <EconomicDistanceCut2>
  104. <xsl:for-each select="$EconomicDistanceCut/EconomicDistanceCut/distance">
  105. <distance2>
  106. <xsl:attribute name="distance" select="sum( $EconomicDistanceCut/EconomicDistanceCut/distance[number(@pos )&lt; number(current()/@pos )]/@len)"/>
  107. <!-- suma do poprzedniego pkt tylko dla tych co maja pkt-->
  108. <xsl:choose>
  109. <xsl:when test="@point_count&gt;0">
  110. <xsl:variable name="max_pos_z_pkt" select="max($EconomicDistanceCut/EconomicDistanceCut/distance[
  111. number(@pos )&lt; number(current()/@pos ) and number(@point_count)&gt;0
  112. ]/@pos)"/>
  113. <xsl:attribute name="max_pos_z_pkt" select="$max_pos_z_pkt"/>
  114. <xsl:variable name="disttopkt" select="sum( $EconomicDistanceCut/EconomicDistanceCut/distance[
  115. number(@pos )&lt; number(current()/@pos )
  116. and number(@pos) &gt;= number($max_pos_z_pkt)
  117. ]/@len)"/>
  118. <xsl:attribute name="disttopkt" select="$disttopkt" />
  119. <xsl:variable name="cost_dist" select="$Cable_Price * $disttopkt * @point_count"/>
  120. <xsl:attribute name="cost_dist" select="$cost_dist"/>
  121. <xsl:attribute name="point_count" select="@point_count"/>
  122. <xsl:if test="$cost_dist &gt; $PE_Price">
  123. <xsl:attribute name="cut_by_pe" select="1"/>
  124. </xsl:if>
  125. <xsl:attribute name="not_id" select="@not_id"/>
  126. </xsl:when>
  127. <xsl:otherwise>
  128. <xsl:attribute name="point_count" select="0"/>
  129. </xsl:otherwise>
  130. </xsl:choose>
  131. <xsl:attribute name="id_path" select="@id_path"/>
  132. <xsl:copy-of select="@point_count"/>
  133. <xsl:copy-of select="@pos"/>
  134. <xsl:copy-of select="@len"/>
  135. </distance2>
  136. </xsl:for-each>
  137. </EconomicDistanceCut2>
  138. </xsl:variable>
  139. <xsl:copy-of select="$EconomicDistanceCut2"/>
  140. <xsl:variable name="elements_cnt" select="count($EconomicDistanceCut2/EconomicDistanceCut2/distance2[@point_count &gt; 0])"/><!-- liczba wezlow potencjalnych licbza PE-->
  141. <xsl:variable name="bit_max" select="round($len div ( $PE_Price div $Cable_Price ) + count($EconomicDistanceCut2/EconomicDistanceCut2/distance2[@cost_dist &gt; $PE_Price]) )"/><!-- max licbza PE-->
  142. <!-- pogrupujemy elementy co 50 metrow -->
  143. <xsl:variable name="PointMatrixGroup">
  144. <PointMatrixGroup>
  145. <xsl:for-each-group select="$EconomicDistanceCut2/EconomicDistanceCut2/distance2" group-by="round( @distance div ( $PE_Price div $Cable_Price + 50 ))">
  146. <PointMatrixGroupPos>
  147. <xsl:variable name="elements_cnt_Pos" select="count(current-group()[@point_count &gt; 0])"/>
  148. <xsl:variable name="bit_max_Pos" select="round(sum( current-group()/@disttopkt ) div ( $PE_Price div 20 ) )"/>
  149. <xsl:attribute name="distanceGr" select=" current-grouping-key()"/>
  150. <xsl:attribute name="elements_cnt_Pos" select="$elements_cnt_Pos"/>
  151. <xsl:attribute name="bit_max_Pos" select="$bit_max_Pos"/>
  152. <xsl:attribute name="id_path" select="@id_path"/>
  153. <xsl:attribute name="point_count_Pos" select="sum(current-group()/@point_count)"/>
  154. <xsl:attribute name="LineStringCount" select="count( current-group())"/>
  155. <xsl:attribute name="len_sum" select="sum(current-group()/@len)"/>
  156. <xsl:attribute name="distanceBeg" select="min(current-group()/number(@distance))"/>
  157. <xsl:attribute name="distanceEnd" select="max(current-group()/number(@distance))"/>
  158. <!--<xsl:attribute name="GroupPos" select="position()"/>-->
  159. <xsl:for-each select="current-group()">
  160. <distance2>
  161. <xsl:attribute name="PosMatrGroup" select="position()"/>
  162. <xsl:copy-of select="@*"></xsl:copy-of>
  163. </distance2>
  164. </xsl:for-each>
  165. </PointMatrixGroupPos>
  166. </xsl:for-each-group>
  167. </PointMatrixGroup>
  168. </xsl:variable>
  169. <!-- wsadzamy PE w grupach i moze w kilku wariantach - najlepiej tam gdzie jest najwiecej klientow najpierw
  170. gdzie najwiecej klientow walimy wezly - grupujemy ilosciami klientow dzielac sume drogi na ilosc przewidzianych wezlow
  171. -->
  172. <!-- debug -->
  173. <xsl:copy-of select="$PointMatrixGroup"/>
  174. <PointMatrixGroupPE>
  175. <xsl:for-each select="$PointMatrixGroup/PointMatrixGroup">
  176. <xsl:for-each select="PointMatrixGroupPos">
  177. <xsl:variable name="bit_max_Pos" select="@bit_max_Pos"/>
  178. <xsl:variable name="point_count_Pos" select="@point_count_Pos"/>
  179. <xsl:variable name="distanceGr" select="@distanceGr"/>
  180. <PointMatrixGroup>
  181. <xsl:attribute name="bit_max_Pos" select="$bit_max_Pos"/><xsl:attribute name="point_count_Pos" select="$point_count_Pos"/><xsl:attribute name="distanceGr" select="$distanceGr"/>
  182. <xsl:attribute name="LineStringCount" select="@LineStringCount"/>
  183. <xsl:attribute name="len_sum" select="@len_sum"/>
  184. <xsl:attribute name="point_count_Pos" select="@point_count_Pos"/>
  185. <xsl:attribute name="distanceBeg" select="@distanceBeg"/>
  186. <xsl:attribute name="distanceEnd" select="@distanceEnd"/>
  187. <!-- ida kandydaci -->
  188. <xsl:variable name="candidates">
  189. <xsl:for-each select="distance2[( position() &lt;= $bit_max_Pos and @point_count &gt;0 ) or ( position()=1 and $point_count_Pos &gt; 0 ) ]"> <!-- jak jest maly wspolczynnik to chociaz jeden punkt jak sa punkty jakies w tej grupie -->
  190. <xsl:sort select="@cost_dist" order="descending"/>
  191. <bp:Path_Point>
  192. <xsl:attribute name="DebugPosPoint" select="position()"/>
  193. <xsl:attribute name="PosMatrGroup" select="@PosMatrGroup"/>
  194. <xsl:attribute name="distanceGr" select="$distanceGr"/>
  195. <xsl:attribute name="pos" select="@pos"/>
  196. <xsl:attribute name="len" select="@len"/>
  197. <xsl:attribute name="distance" select="@distance"/>
  198. <xsl:attribute name="cost_dist" select="@cost_dist"/>
  199. <xsl:attribute name="id_path" select="@id_path"/>
  200. <xsl:attribute name="point_count" select="@point_count"/>
  201. <xsl:attribute name="X" select="$edge_joins_filled_path/edge_joins_filled_paths_xml/asText_XMl/LineString[@id_path = current()/@id_path]/@X"/>
  202. <xsl:attribute name="Y" select="$edge_joins_filled_path/edge_joins_filled_paths_xml/asText_XMl/LineString[@id_path = current()/@id_path]/@Y"/>
  203. </bp:Path_Point>
  204. </xsl:for-each>
  205. </xsl:variable>
  206. <xsl:choose>
  207. <xsl:when test="count($candidates/bp:Path_Point) &lt; 9 and count($candidates/bp:Path_Point)&gt;1">
  208. <xsl:variable name="max_bit">
  209. <xsl:for-each select="1 to count($candidates/bp:Path_Point)"><xsl:value-of select="1"/></xsl:for-each>
  210. </xsl:variable>
  211. <odpalam_liczenie>
  212. <xsl:call-template name="calculate_optimum">
  213. <xsl:with-param name="candidates" select="$candidates"/>
  214. <xsl:with-param name="elements_cnt" select="count($candidates/bp:Path_Point)"/>
  215. <xsl:with-param name="max_bit" select="$max_bit"/>
  216. </xsl:call-template>
  217. </odpalam_liczenie>
  218. </xsl:when>
  219. <xsl:otherwise>
  220. <xsl:copy-of select="$candidates"></xsl:copy-of>
  221. </xsl:otherwise>
  222. </xsl:choose>
  223. </PointMatrixGroup>
  224. </xsl:for-each>
  225. </xsl:for-each>
  226. </PointMatrixGroupPE>
  227. <!--<PointMatrix>
  228. <xsl:attribute name="bit_max" select="$bit_max"/>
  229. <xsl:attribute name="elements_cnt" select="$elements_cnt"/>
  230. <xsl:attribute name="num_extr" select="count($EconomicDistanceCut2/EconomicDistanceCut2/distance2[@cost_dist &gt; $PE_Price])"/>
  231. <!-\-<xsl:call-template name="PE_wariant">
  232. <xsl:with-param name="elements_cnt" select="$elements_cnt"/>
  233. <xsl:with-param name="bit_max" select="$bit_max"/>
  234. </xsl:call-template>-\->
  235. </PointMatrix>-->
  236. </Path>
  237. </xsl:template>
  238. <xsl:template name="calculate_optimum">
  239. <xsl:param name="candidates" required="yes"/>
  240. <xsl:param name="elements_cnt" required="yes"/>
  241. <xsl:param name="variant" select="1"/>
  242. <xsl:param name="max_bit" required="yes"/>
  243. <xsl:param name="best_cost"/><xsl:param name="best_variant"/>
  244. <xsl:variable name="bit" select=" string-join(bp:decimalToBitFill($variant,$elements_cnt),'')"/>
  245. <xsl:variable name="cand_to_calc">
  246. <xsl:for-each select="$candidates/bp:Path_Point">
  247. <xsl:sort select="@distance"/>
  248. <bp:Path_Point>
  249. <xsl:attribute name="bit" select="$bit"/>
  250. <xsl:attribute name="bin_pos" select="substring($bit, position(),1)"/>
  251. <xsl:copy-of select="@*"/>
  252. <xsl:copy-of select="bp:Path_Point"></xsl:copy-of>
  253. </bp:Path_Point>
  254. </xsl:for-each>
  255. </xsl:variable>
  256. <!-- debug -->
  257. <!--<variant><xsl:attribute name="variant" select="$variant"/>
  258. <xsl:copy-of select="$cand_to_calc"/>
  259. </variant>-->
  260. <xsl:variable name="calc_cost_way">
  261. <xsl:call-template name="calc_cost_way"><xsl:with-param name="cand_to_calc" select="$cand_to_calc"/></xsl:call-template>
  262. </xsl:variable>
  263. <!--<xsl:copy-of select="$calc_cost_way"/>-->
  264. <xsl:variable name="best">
  265. <xsl:variable name="cur_Price" select="sum($calc_cost_way/cost/number(@Price))"/>
  266. <xsl:choose>
  267. <xsl:when test="number($cur_Price) &lt; number($best_cost) or not($best_cost) ">
  268. <best>
  269. <xsl:attribute name="best_cost" select="$cur_Price"/>
  270. <xsl:attribute name="best_variant" select="$bit"/>
  271. </best>
  272. </xsl:when>
  273. <xsl:otherwise>
  274. <best>
  275. <xsl:attribute name="best_cost" select="$best_cost"/>
  276. <xsl:attribute name="best_variant" select="$best_variant"/>
  277. </best>
  278. </xsl:otherwise>
  279. </xsl:choose>
  280. </xsl:variable>
  281. <!--<xsl:copy-of select="$best"/>-->
  282. <xsl:choose>
  283. <xsl:when test="$bit = $max_bit">
  284. <koniec_wariantow>
  285. <xsl:attribute name="best_variant" select="$best_variant"/>
  286. <xsl:for-each select="$candidates/bp:Path_Point">
  287. <xsl:sort select="@distance"/>
  288. <xsl:if test="substring($best_variant, position(),1)='1'"><!-- jak wystawiony w wariancie-->
  289. <bp:Path_Point>
  290. <xsl:attribute name="bit" select="$best_variant"/>
  291. <xsl:attribute name="bin_pos" select="substring($best_variant, position(),1)"/>
  292. <xsl:copy-of select="@*"/>
  293. <xsl:attribute name="Price" select="$best_cost"/>
  294. <xsl:copy-of select="bp:Path_Point"/>
  295. </bp:Path_Point>
  296. </xsl:if>
  297. </xsl:for-each>
  298. </koniec_wariantow></xsl:when>
  299. <xsl:otherwise>
  300. <xsl:call-template name="calculate_optimum">
  301. <xsl:with-param name="candidates" select="$candidates"/>
  302. <xsl:with-param name="max_bit" select="$max_bit"/>
  303. <xsl:with-param name="variant" select="$variant + 1"/>
  304. <xsl:with-param name="elements_cnt" select="$elements_cnt"/>
  305. <xsl:with-param name="best_cost" select="$best/best/@best_cost"/>
  306. <xsl:with-param name="best_variant" select="$best/best/@best_variant"/>
  307. </xsl:call-template>
  308. </xsl:otherwise>
  309. </xsl:choose>
  310. </xsl:template>
  311. <xsl:template name="calc_cost_way"><!-- do wyliczenia ile wyjda kable w takiej konfiguracji -->
  312. <xsl:param name="cand_to_calc" required="yes"/>
  313. <xsl:for-each select="$cand_to_calc/bp:Path_Point">
  314. <xsl:choose>
  315. <xsl:when test="@bin_pos=0"><!-- szukamy gdzie ma najblizej i za ile -->
  316. <xsl:variable name="way_up_id" select="max($cand_to_calc/bp:Path_Point[@bin_pos=1 and @id_path &lt; current()/@id_path ]/number(@id_path))"/>
  317. <xsl:variable name="way_up_len" >
  318. <xsl:choose>
  319. <xsl:when test="$way_up_id">
  320. <xsl:value-of select="number( current()/@distance) - number($cand_to_calc/bp:Path_Point[@id_path=$way_up_id]/@distance)"/></xsl:when>
  321. <xsl:otherwise><xsl:value-of select="-1"/></xsl:otherwise>
  322. </xsl:choose>
  323. </xsl:variable>
  324. <xsl:variable name="way_dn_id" select="max($cand_to_calc/bp:Path_Point[@bin_pos=1 and @id_path &gt; current()/@id_path ]/number(@id_path)) " />
  325. <xsl:variable name="way_dn_len">
  326. <xsl:choose>
  327. <xsl:when test="$way_dn_id">
  328. <xsl:value-of select=" number($cand_to_calc/bp:Path_Point[@id_path=$way_dn_id]/@distance ) - current()/@distance"/>
  329. </xsl:when>
  330. <xsl:otherwise><xsl:value-of select="-1"/></xsl:otherwise>
  331. </xsl:choose>
  332. </xsl:variable>
  333. <xsl:choose><!-- czy w gore czy w dol -->
  334. <xsl:when test=" number($way_up_len) &gt;= number($way_dn_len) and number($way_dn_len) &gt; 0 or $way_up_len=-1"> <!--and not($way_dn_len='NaN') -->
  335. <cost>
  336. <xsl:attribute name="Cable_Price" select="$Cable_Price * $way_dn_len "/> <!-- todo @point_count -->
  337. <xsl:attribute name="PE_Price" select="0"/>
  338. <xsl:attribute name="Price" select="$Cable_Price"/>
  339. <xsl:attribute name="choosed" select="'down'"/>
  340. <xsl:attribute name="way_up_id" select="$way_up_id"/><xsl:attribute name="way_up_len" select="$way_up_len"/>
  341. <xsl:attribute name="way_dn_id" select="$way_dn_id"/><xsl:attribute name="way_dn_len" select="$way_dn_len"/>
  342. </cost>
  343. </xsl:when>
  344. <xsl:otherwise>
  345. <cost>
  346. <xsl:attribute name="Cable_Price" select="$Cable_Price * $way_up_len "/> <!-- todo @point_count -->
  347. <xsl:attribute name="PE_Price" select="0"/>
  348. <xsl:attribute name="Price" select="$Cable_Price"/>
  349. <xsl:attribute name="choosed" select="'up'"/>
  350. <xsl:attribute name="way_up_id" select="$way_up_id"/><xsl:attribute name="way_up_len" select="$way_up_len"/>
  351. <xsl:attribute name="way_dn_id" select="$way_dn_id"/><xsl:attribute name="way_dn_len" select="$way_dn_len"/>
  352. </cost>
  353. </xsl:otherwise>
  354. </xsl:choose>
  355. </xsl:when>
  356. <xsl:otherwise>
  357. <cost>
  358. <xsl:attribute name="Cable_Price" select="0"/>
  359. <xsl:attribute name="PE_Price" select="$PE_Price"/>
  360. <xsl:attribute name="Price" select="$PE_Price"/>
  361. </cost>
  362. </xsl:otherwise>
  363. </xsl:choose>
  364. </xsl:for-each>
  365. </xsl:template>
  366. </xsl:stylesheet>