Advertisement
anchormodeling

The Iceberg Exercise

Sep 20th, 2015
28,171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 17.40 KB | None | 0 0
  1. /*
  2.     Lars Rönnbäck
  3.  
  4.     HISTORY
  5.     2015-09-16 Created the script.
  6.     2015-09-20 Added uni- and bi-temporally independent
  7.                and dependent joins.
  8.     2015-10-02 Updated to show positive assertions.
  9.                Added section on indecisiveness.
  10.                
  11.  
  12. -------------------------------------------------------
  13.     Create two different positors that may differ
  14.     in opinions about icebergs.
  15.     Note that a 'default' positor has already
  16.     been created with identity 0.
  17. -------------------------------------------------------
  18. */
  19. insert into _Positor
  20. values (1);
  21.  
  22. /*
  23. -------------------------------------------------------
  24.     Populate the knot values.
  25. -------------------------------------------------------
  26. */
  27. insert into ATT_Attachment (
  28.     ATT_ID, ATT_Attachment
  29. ) values
  30. (0, 'Not attached'),
  31. (1, 'Attached');
  32.  
  33. insert into FRZ_Frozen (
  34.     FRZ_ID, FRZ_Frozen
  35. ) values
  36. (0, 'Not frozen'),
  37. (1, 'Frozen');
  38.  
  39. insert into MIA_MissingInAction (
  40.     MIA_ID, MIA_MissingInAction
  41. ) values
  42. (0, 'Missing in action'),
  43. (1, 'Known whereabouts');
  44.  
  45. insert into SOM_SplitOrMerge (
  46.     SOM_ID, SOM_SplitOrMerge
  47. ) values
  48. (1, 'Split'),
  49. (2, 'Merge');
  50.  
  51. insert into SUB_Submerged (
  52.     SUB_ID, SUB_Submerged
  53. ) values
  54. (0, 'Not submerged'),
  55. (1, 'Submerged');
  56.  
  57. insert into EOE_EnteredOrExited (
  58.     EOE_ID, EOE_EnteredOrExited
  59. ) values
  60. (0, 'Exited'),
  61. (1, 'Entered');
  62.  
  63. /*
  64. -------------------------------------------------------
  65.     Insert a signal from one of the transmitters.
  66.     Resulting in a TR_ID = 1 being created.
  67. -------------------------------------------------------
  68. */
  69. insert into lTR_Transmitter (
  70.     Positor,
  71.     TR_LOC_ChangedAt, TR_LOC_PositedAt, TR_LOC_Reliability, TR_LOC_Transmitter_Location,
  72.     TR_MIA_ChangedAt, TR_MIA_PositedAt, TR_MIA_Reliability, TR_MIA_MIA_MissingInAction,
  73.                       TR_FSG_PositedAt, TR_FSG_Reliability, TR_FSG_Transmitter_FirstSignal,
  74.     TR_LSG_ChangedAt, TR_LSG_PositedAt, TR_LSG_Reliability, TR_LSG_Transmitter_LastSignal
  75. )
  76. values (
  77.     0,
  78.     '1912-04-15', '2015-01-01',  50, geography::STPointFromText('POINT(41.7325 49.9469)', 4326),
  79.     '1912-04-15', '2015-01-01', 100, 'Known whereabouts',
  80.                   '2015-01-01', 100, '1912-04-01',
  81.     '1912-04-15', '2015-01-01', 100, '1912-04-15 23:40'
  82. );
  83.  
  84. /*
  85. -------------------------------------------------------
  86.     Update, since we got another signal, but the
  87.     location remained the same.
  88.     Only historized attributes can be updated.
  89. -------------------------------------------------------
  90. */
  91. update lTR_Transmitter
  92. set
  93.     TR_LSG_ChangedAt = '1912-04-16',
  94.     TR_LSG_PositedAt = '2015-01-01',
  95.     TR_LSG_Reliability = 80,
  96.     TR_LSG_Transmitter_LastSignal = '1912-04-16 01:01'
  97. where
  98.     TR_ID = 1
  99. and
  100.     Positor = 0;
  101.  
  102. /*
  103. -------------------------------------------------------
  104.     Check the data.
  105.     The last signal should be different between the
  106.     two days.
  107. -------------------------------------------------------
  108. */
  109. select TR_LSG_Transmitter_LastSignal from pTR_Transmitter('1912-04-15') where Positor = 0 and TR_ID = 1;
  110. select TR_LSG_Transmitter_LastSignal from pTR_Transmitter('1912-04-16') where Positor = 0 and TR_ID = 1;
  111.  
  112. /*
  113. -------------------------------------------------------
  114.     Correct, that the first signal was in fact  
  115.     received a day later.
  116.     All attributes can be corrected.
  117.     In order to retract an assertion a new assertion
  118.     is made on the same posit but with zero
  119.     reliability.
  120. -------------------------------------------------------
  121. */
  122. insert into lTR_Transmitter (
  123.     Positor, TR_ID,
  124.     TR_FSG_PositedAt, TR_FSG_Reliability, TR_FSG_Transmitter_FirstSignal
  125. )
  126. values (
  127.     0, 1,
  128.    '2015-01-02',  0, '1912-04-01' -- set reliability to zero
  129. ), (
  130.     0, 1,
  131.    '2015-01-02', 80, '1912-04-02' -- assert another posit
  132. );
  133.  
  134. /*
  135. -------------------------------------------------------
  136.     Check the data.
  137.     The default positor should have made no assertion
  138.     about the first signal before 2015, and was 100%
  139.     sure about the signal on the first, but retracted
  140.     that and is 80% sure of another posit on the
  141.     second of February, 2015.
  142. -------------------------------------------------------
  143. */
  144. select TR_FSG_Transmitter_FirstSignal from tTR_Transmitter(0, DEFAULT, '2014-12-31', '+') where TR_ID = 1;
  145. select TR_FSG_Transmitter_FirstSignal from tTR_Transmitter(0, DEFAULT, '2015-01-01', '+') where TR_ID = 1;
  146. select TR_FSG_Transmitter_FirstSignal from tTR_Transmitter(0, DEFAULT, '2015-01-02', '+') where TR_ID = 1;
  147.  
  148. /*
  149. -------------------------------------------------------
  150.     Insert an iceberg adrift.
  151.     Resulting in a IB_ID = 1 being created.
  152. -------------------------------------------------------
  153. */
  154. insert into lIB_Iceberg (
  155.     Positor,
  156.                       IB_FDA_PositedAt, IB_FDA_Reliability, IB_FDA_Iceberg_FormationDate,
  157.     IB_FRZ_ChangedAt, IB_FRZ_PositedAt, IB_FRZ_Reliability, IB_FRZ_FRZ_Frozen,
  158.     IB_LOC_ChangedAt, IB_LOC_PositedAt, IB_LOC_Reliability, IB_LOC_Iceberg_Location,
  159.     IB_SUB_ChangedAt, IB_SUB_PositedAt, IB_SUB_Reliability, IB_SUB_SUB_Submerged,
  160.     IB_SIZ_ChangedAt, IB_SIZ_PositedAt, IB_SIZ_Reliability, IB_SIZ_Iceberg_Size,
  161.                       IB_DDA_PositedAt, IB_DDA_Reliability, IB_DDA_Iceberg_DisintegrationDate
  162. )
  163. values (
  164.     0,
  165.                   '2015-01-01', 100, '1912-02-13',
  166.     '1912-02-13', '2015-01-01', 100, 'Not frozen',
  167.     '1912-04-15', '2015-01-01',  50, geography::STPointFromText('POINT(42.7325 49.9469)', 4326),
  168.     '1912-02-13', '2015-01-01', 100, 'Not submerged',
  169.     '1912-02-13', '2015-01-01',  50, 555,
  170.                   '2015-01-01', 100, null -- not yet disintegrated
  171. );
  172.  
  173. /*
  174. -------------------------------------------------------
  175.     Let another positor (1, a boat) have a different
  176.     opinion about the whereabouts of the iceberg.
  177.     Note that this must use IB_ID = 1 to refer to
  178.     the same iceberg.
  179. -------------------------------------------------------
  180. */
  181. insert into lIB_Iceberg (
  182.     Positor, IB_ID,
  183.                       IB_FDA_PositedAt, IB_FDA_Reliability, IB_FDA_Iceberg_FormationDate,
  184.     IB_FRZ_ChangedAt, IB_FRZ_PositedAt, IB_FRZ_Reliability, IB_FRZ_FRZ_Frozen,
  185.     IB_LOC_ChangedAt, IB_LOC_PositedAt, IB_LOC_Reliability, IB_LOC_Iceberg_Location,
  186.     IB_SUB_ChangedAt, IB_SUB_PositedAt, IB_SUB_Reliability, IB_SUB_SUB_Submerged,
  187.     IB_SIZ_ChangedAt, IB_SIZ_PositedAt, IB_SIZ_Reliability, IB_SIZ_Iceberg_Size,
  188.                       IB_DDA_PositedAt, IB_DDA_Reliability, IB_DDA_Iceberg_DisintegrationDate
  189. )
  190. values (
  191.     1, 1,
  192.                   '2015-01-01', 100, '1912-02-13',
  193.     '1912-02-13', '2015-01-01', 100, 'Frozen', -- the boat claims the iceberg is stuck at another location
  194.     '1912-04-15', '2015-01-01',  50, geography::STPointFromText('POINT(41.7325 49.9469)', 4326),
  195.     '1912-02-13', '2015-01-01', 100, 'Not submerged',
  196.     '1912-02-13', '2015-01-01',  50, 911, -- and quite much larger
  197.                   '2015-01-01', 100, null -- not yet disintegrated
  198. );
  199.  
  200. /*
  201. -------------------------------------------------------
  202.     Check the data.
  203.     The positors differ in their opinion about the
  204.     size of the iceberg, whether it is frozen or not,
  205.     and its location.
  206. -------------------------------------------------------
  207. */
  208. select
  209.     Positor,
  210.     IB_SIZ_Iceberg_Size,
  211.     IB_FRZ_FRZ_Frozen,
  212.     IB_LOC_Iceberg_Location.STBuffer(IB_SIZ_Iceberg_Size)
  213. from
  214.     lIB_Iceberg where IB_ID = 1;
  215.  
  216. /*
  217. -------------------------------------------------------
  218.     Connect the transmitter to the iceberg.
  219. -------------------------------------------------------
  220. */
  221. insert into lIB_to_TR_attached_ATT_currently (
  222.     Positor,
  223.     IB_to_TR_attached_ATT_currently_ChangedAt,
  224.     IB_to_TR_attached_ATT_currently_PositedAt,
  225.     IB_to_TR_attached_ATT_currently_Reliability,
  226.     IB_ID_to,
  227.     TR_ID_attached,
  228.     currently_ATT_Attachment
  229. ) values (
  230.     0,
  231.     '1912-02-13',
  232.     '2015-01-01',
  233.     100,
  234.     1, -- IB_ID = 1 (our only iceberg)
  235.     1, -- TR_ID = 1 (our only transmitter)
  236.     'Attached'
  237. );
  238.  
  239. /*
  240. -------------------------------------------------------
  241.     Check the data.
  242.     Perform a join over the latest available data in
  243.     the two iceberg and transmitter anchors adjoined
  244.     by the tie modeling the attachment relationship.
  245.     Note that the boat (positor 1) has no concept
  246.     of attached transmitters.
  247.  
  248.     Temporally this corresponds to a join in a
  249.     regular non-temporal database, which can only
  250.     ever hold the latest available information.
  251. -------------------------------------------------------
  252. */
  253. select
  254.     *
  255. from
  256.     lIB_Iceberg ib
  257. left join
  258.     lIB_to_TR_attached_ATT_currently ibtr
  259. on
  260.     ibtr.IB_ID_to = ib.IB_ID
  261. and
  262.     ibtr.Positor = ib.Positor
  263. left join
  264.     lTR_Transmitter tr
  265. on
  266.     tr.TR_ID = ibtr.TR_ID_attached
  267. and
  268.     tr.Positor = ibtr.Positor
  269. where
  270.     ib.IB_ID = 1;
  271.  
  272. /*
  273. -------------------------------------------------------
  274.     Check the data.
  275.     Change to a temporally _independent_ join, a join
  276.     in which a temporal "snapshot" of each
  277.     participating _table_ can be eastablished _before_
  278.     joining the rows.
  279.    
  280.     The query below answers:
  281.     Where were the iceberg and attached transmitter on
  282.     the 13th of February 1912?
  283.  
  284.     Most use-cases of temporal data are covered by
  285.     this type of join.
  286. -------------------------------------------------------
  287. */
  288. select
  289.     *
  290. from
  291.     pIB_Iceberg('1912-02-13') ib
  292. left join
  293.     pIB_to_TR_attached_ATT_currently('1912-02-13') ibtr
  294. on
  295.     ibtr.IB_ID_to = ib.IB_ID
  296. and
  297.     ibtr.Positor = ib.Positor
  298. left join
  299.     pTR_Transmitter('1912-02-13') tr
  300. on
  301.     tr.TR_ID = ibtr.TR_ID_attached
  302. and
  303.     tr.Positor = ibtr.Positor
  304. where
  305.     ib.IB_ID = 1;
  306.  
  307. /*
  308. -------------------------------------------------------
  309.     Check the data.
  310.     Change to a temporally _dependent_ join, a join
  311.     in which a temporal "snapshot" of each
  312.     participating _instance_ must be eastablished
  313.     _during _ the join.
  314.    
  315.     The query below answers:
  316.     Where were the iceberg and attached transmitter at
  317.     the time of formation?
  318.  
  319.     All use-cases of joins can be expressed through
  320.     temporally independent or dependent joins.
  321. -------------------------------------------------------
  322. */
  323. select
  324.     *
  325. from
  326.     IB_FDA_Iceberg_FormationDate fda
  327. cross apply (
  328.     select
  329.         *
  330.     from
  331.         pIB_Iceberg(fda.IB_FDA_Iceberg_FormationDate) ib
  332.     where
  333.         ib.IB_ID = fda.IB_FDA_IB_ID
  334.     and
  335.         ib.Positor = fda.IB_FDA_Positor
  336. ) ib
  337. outer apply (
  338.     select
  339.         *
  340.     from
  341.         pIB_to_TR_attached_ATT_currently(fda.IB_FDA_Iceberg_FormationDate) ibtr
  342.     where
  343.         ibtr.IB_ID_to = ib.IB_ID
  344.     and
  345.         ibtr.Positor = ib.Positor
  346. ) ibtr
  347. outer apply (
  348.     select
  349.         *
  350.     from
  351.         pTR_Transmitter(fda.IB_FDA_Iceberg_FormationDate) tr
  352.     where
  353.         tr.TR_ID = ibtr.TR_ID_attached
  354.     and
  355.         tr.Positor = ibtr.Positor
  356. ) tr
  357. where
  358.     ib.IB_ID = 1;
  359.  
  360. /*
  361. -------------------------------------------------------
  362.     Check the data.
  363.     A bi-temporally dependent/independent join, with
  364.     the same conditions as in the join above.
  365.  
  366.     Note that changing time has a temporally dependent
  367.     join and that positing time is independent. Since
  368.     positing time is independent it is possible to
  369.     create a variable that holds the positing time
  370.     we are interested int.
  371. -------------------------------------------------------
  372. */
  373. declare @positingTimepoint date = '2015-01-01';
  374.  
  375. select
  376.     *
  377. from
  378.     IB_FDA_Iceberg_FormationDate fda
  379. cross apply (
  380.     select
  381.         *
  382.     from
  383.         tIB_Iceberg(fda.IB_FDA_Positor, fda.IB_FDA_Iceberg_FormationDate, @positingTimepoint, '+') ib
  384.     where
  385.         ib.IB_ID = fda.IB_FDA_IB_ID
  386. ) ib
  387. outer apply (
  388.     select
  389.         *
  390.     from
  391.         tIB_to_TR_attached_ATT_currently(ib.IB_FDA_Positor, fda.IB_FDA_Iceberg_FormationDate, @positingTimepoint, '+') ibtr
  392.     where
  393.         ibtr.IB_ID_to = ib.IB_ID
  394. ) ibtr
  395. outer apply (
  396.     select
  397.         *
  398.     from
  399.         tTR_Transmitter(ib.IB_FDA_Positor, fda.IB_FDA_Iceberg_FormationDate, @positingTimepoint, '+') tr
  400.     where
  401.         tr.TR_ID = ibtr.TR_ID_attached
  402. ) tr
  403. where
  404.     ib.IB_ID = 1;
  405.  
  406. /*
  407. -------------------------------------------------------
  408.     Check the data.
  409.     A bi-temporally dependent/dependent join.
  410.  
  411.     The added condition is that we want to see the
  412.     information as we knew it at time of when we
  413.     knew the formation date, for each instance.
  414. -------------------------------------------------------
  415. */
  416. select
  417.     *
  418. from
  419.     IB_FDA_Iceberg_FormationDate fda
  420. cross apply (
  421.     select
  422.         *
  423.     from
  424.         tIB_Iceberg(fda.IB_FDA_Positor, fda.IB_FDA_Iceberg_FormationDate, fda.IB_FDA_PositedAt, '+') ib
  425.     where
  426.         ib.IB_ID = fda.IB_FDA_IB_ID
  427. ) ib
  428. outer apply (
  429.     select
  430.         *
  431.     from
  432.         tIB_to_TR_attached_ATT_currently(ib.IB_FDA_Positor, fda.IB_FDA_Iceberg_FormationDate, fda.IB_FDA_PositedAt, '+') ibtr
  433.     where
  434.         ibtr.IB_ID_to = ib.IB_ID
  435. ) ibtr
  436. outer apply (
  437.     select
  438.         *
  439.     from
  440.         tTR_Transmitter(ib.IB_FDA_Positor, fda.IB_FDA_Iceberg_FormationDate, fda.IB_FDA_PositedAt, '+') tr
  441.     where
  442.         tr.TR_ID = ibtr.TR_ID_attached
  443. ) tr
  444. where
  445.     ib.IB_ID = 1;
  446.  
  447.  
  448. /*
  449. -------------------------------------------------------
  450.     Update of a static attribute is not done
  451.     using an update-statement, since being a
  452.     correction it involves both a retraction and
  453.     a new assertion.
  454.  
  455.     Note the resulting warning message.
  456. -------------------------------------------------------
  457. */
  458. update lTR_Transmitter
  459. set
  460.     TR_FSG_Transmitter_FirstSignal = '1912-01-31'
  461. where
  462.     TR_ID = 1;
  463.  
  464.  
  465. /*
  466. -------------------------------------------------------
  467.     Change the reliability of an existing posit.
  468.     Sometimes the degree of belief in a posit will
  469.     change over time. In this case the belief is
  470.     lowered from 80 (previous value) to 10.
  471. -------------------------------------------------------
  472. */
  473. insert into lTR_Transmitter (
  474.     Positor, TR_ID,
  475.     TR_FSG_PositedAt, TR_FSG_Reliability, TR_FSG_Transmitter_FirstSignal
  476. )
  477. values (
  478.     0, 1,
  479.    '2015-01-03', 10, '1912-04-02' -- set reliability 10
  480. );
  481.  
  482. /*
  483. -------------------------------------------------------
  484.     Check the data.
  485.     We now see the updated reliability, since this
  486.     is the latest available with respect to the
  487.     positing time.
  488. -------------------------------------------------------
  489. */
  490.  
  491. select TR_FSG_Reliability from lTR_Transmitter where Positor = 0 and TR_ID = 1;
  492.  
  493. /*
  494. -------------------------------------------------------
  495.     Add a second belief with the same positing time.
  496.     This can only be done if indecisiveness is allowed
  497.     in the implementation.
  498.  
  499.     Along with the fact that we believe with 10%
  500.     certainty that the first signal was received
  501.     on the 2nd of April, we at the same positing
  502.     time believe that it was receive on the 1st
  503.     of April with 90% certainty.
  504. -------------------------------------------------------
  505. */
  506. insert into lTR_Transmitter (
  507.     Positor, TR_ID,
  508.     TR_FSG_PositedAt, TR_FSG_Reliability, TR_FSG_Transmitter_FirstSignal
  509. )
  510. values (
  511.     0, 1,
  512.    '2015-01-03', 90, '1912-04-01' -- set reliability 90
  513. );
  514.  
  515. /*
  516. -------------------------------------------------------
  517.     Check the data.
  518.     Note that in order to always retrieve a single
  519.     row per attribute, the views select the one
  520.     with the highest reliability.
  521.  
  522.     Our best guess for the first signal is
  523.     on April 1st, with 90% certainty. Note that
  524.     the database still keeps a record of the
  525.     fact that another posit exists at the same
  526.     time, but with only 10% certainty.
  527. -------------------------------------------------------
  528. */
  529. select TR_FSG_Reliability from lTR_Transmitter where Positor = 0 and TR_ID = 1;
  530.  
  531. /*
  532. -------------------------------------------------------
  533.     Kill the transmitter.
  534.     This uses the overall Reliability column
  535.     to clear out the reliability of the columns
  536.     in the view.
  537. -------------------------------------------------------
  538. */
  539. update lTR_Transmitter
  540. set
  541.     Reliability = 0
  542. where
  543.     TR_ID = 1
  544. and
  545.     Positor = 0;
  546.  
  547. /*
  548. -------------------------------------------------------
  549.     Check the data.
  550.     Note that due to historization the previous
  551.     value in effect now became visible for the
  552.     last received signal.
  553. -------------------------------------------------------
  554. */
  555. select * from lTR_Transmitter where Positor = 0;
  556.  
  557. /*
  558. -------------------------------------------------------
  559.     Kill the transmitter (again).
  560.     This instead uses a delete on the view.
  561. -------------------------------------------------------
  562. */
  563. delete lTR_Transmitter
  564. where
  565.     TR_ID = 1;
  566.  
  567. /*
  568. -------------------------------------------------------
  569.     Check the data.
  570.     This removed the last version we had for the
  571.     last received signal. However, had there been
  572.     more versions we would have had to run more
  573.     deletes to get rid of them.
  574.  
  575.     The identity will, however, still live in the
  576.     database, since we are keeping its memory.
  577.     Therefore we usually have WHERE conditions in
  578.     CRT that checks for non-null values of the
  579.     particular columns we are interested in.
  580. -------------------------------------------------------
  581. */
  582. select * from lTR_Transmitter where Positor = 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement