VisRecommenderBindingFallback.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2018
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['../../lib/@waca/core-client/js/core-client/ui/core/Class', 'underscore'], function (Class, _) {
  8. 'use strict';
  9. var VisRecommenderBindingFallback = Class.extend({
  10. init: function init(visualization) {
  11. VisRecommenderBindingFallback.inherited('init', this, arguments);
  12. this.visualization = visualization;
  13. },
  14. destroy: function destroy() {
  15. VisRecommenderBindingFallback.inherited('destroy', this, arguments);
  16. },
  17. /*
  18. Assign each unassigned column to a slot based on the following priority. The highest ranking slot will be chosen:
  19. 1) empty required slot with the same type
  20. 2) empty slot with the same type
  21. 3) empty required slot with another types
  22. 4) empty slot
  23. If the above conditions are still not satisfied, repeat the steps but with a slot that is not empty but has capacity for another item(rank 5-8)
  24. If a slot does not meet this criteria, it means that there is no slot that has capacity and is ranked 9 and will remain unmapped.
  25. */
  26. assignUnboundColumns: function assignUnboundColumns(recommendation, slotDefinitions) {
  27. var _this = this;
  28. var unboundColumnIds = recommendation.unbound.slice(0);
  29. _.each(unboundColumnIds, function (column) {
  30. column = _this.visualization.getDataSource().getMetadataColumn(column);
  31. var rankSlots = _.map(slotDefinitions, function (slotDef) {
  32. return _this._rankSlot(slotDef, recommendation.slots, column);
  33. });
  34. var bestRank = Math.min.apply(Math, rankSlots);
  35. if (bestRank > 0 && bestRank < 9) {
  36. var index = _.indexOf(rankSlots, bestRank);
  37. var recommendedSlotId = slotDefinitions[index].getId();
  38. var columnId = column.getId();
  39. if (recommendation.slots[recommendedSlotId]) {
  40. recommendation.slots[recommendedSlotId].push(columnId);
  41. } else {
  42. recommendation.slots[recommendedSlotId] = [columnId];
  43. }
  44. recommendation.unbound.splice(_.indexOf(recommendation.unbound, columnId), 1);
  45. }
  46. });
  47. return recommendation;
  48. },
  49. /*
  50. Used in assignUnboundColumns method to rank a columns affinity to be placed into a slot based
  51. on the priorities defined above. Rank 1 is a perfect match. Rank 9 means it cant be matched.
  52. */
  53. _rankSlot: function _rankSlot(slot, recommendedSlots, column) {
  54. if (!recommendedSlots[slot.getId()]) {
  55. if (this._slotMatchesColumn(slot, column)) {
  56. return !slot.isOptional() ? 1 : 2;
  57. } else if (!slot.isOptional()) {
  58. return 3;
  59. }
  60. return 4;
  61. } else if (this._slotHasCapacity(slot, recommendedSlots)) {
  62. if (this._slotMatchesColumn(slot, column)) {
  63. return !slot.isOptional() ? 5 : 6;
  64. } else if (!slot.isOptional()) {
  65. return 7;
  66. }
  67. return 8;
  68. }
  69. return 9;
  70. },
  71. /*
  72. Depending on whether the slot can be stacked or not, there will be 2 measures for capacity: maxStackedItems and
  73. maxItems, respectively. If slot.maxStackItems is -1, it means there is no limit. If slot.maxItems does not exist
  74. then it means no limit.
  75. */
  76. _slotHasCapacity: function _slotHasCapacity(slot, recommendedSlots) {
  77. var id = slot.getId();
  78. var maxItems = slot.getMaxItems();
  79. if (maxItems && maxItems > -1) {
  80. return (recommendedSlots[id] ? recommendedSlots[id].length : 0) < maxItems;
  81. }
  82. return true;
  83. },
  84. _slotMatchesColumn: function _slotMatchesColumn(slot, column) {
  85. var slotType = slot.getType();
  86. var columnType = column.getType();
  87. if (slotType === 'any') {
  88. return true;
  89. } else if (slotType === 'ordinal' && columnType === 'fact' || slotType === 'category' && columnType === 'attribute') {
  90. return true;
  91. }
  92. return false;
  93. }
  94. });
  95. return VisRecommenderBindingFallback;
  96. });
  97. //# sourceMappingURL=VisRecommenderBindingFallback.js.map