|
17 | 17 | */ |
18 | 18 | package ca.uqac.lif.spreadsheet.relation; |
19 | 19 |
|
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import ca.uqac.lif.dag.LabelledNode; |
| 25 | +import ca.uqac.lif.petitpoucet.NodeFactory; |
| 26 | +import ca.uqac.lif.petitpoucet.OrNode; |
| 27 | +import ca.uqac.lif.petitpoucet.Part; |
| 28 | +import ca.uqac.lif.petitpoucet.PartNode; |
20 | 29 | import ca.uqac.lif.petitpoucet.function.AtomicFunction; |
| 30 | +import ca.uqac.lif.petitpoucet.function.NthOutput; |
| 31 | +import ca.uqac.lif.spreadsheet.Cell; |
21 | 32 | import ca.uqac.lif.spreadsheet.Spreadsheet; |
22 | 33 |
|
23 | 34 | /** |
24 | 35 | * Common ancestor to functions specific to relational algebra. These functions |
25 | 36 | * may have a varying input arity, but their output arity is always 1. |
| 37 | + * <p> |
| 38 | + * A relational operator <strong>assumes</strong> that its input arguments are |
| 39 | + * spreadsheets with no duplicate rows. In turn, it <strong>guarantees</strong> |
| 40 | + * that the spreadsheet it produces as its output contains no duplicate rows. |
26 | 41 | * @author Sylvain Hallé |
27 | 42 | */ |
28 | 43 | public abstract class RelationalOperator extends AtomicFunction |
29 | 44 | { |
30 | 45 | /** |
31 | | - * Creates a new instance of the relational operator. |
| 46 | + * A mapping associating each row of the output spreadsheet with the row(s) |
| 47 | + * of the input spreadsheet(s) where it is found. |
| 48 | + */ |
| 49 | + /*@ non_null @*/ protected final List<List<Integer[]>> m_mapping; |
| 50 | + |
| 51 | + /** |
| 52 | + * A flag indicating whether the rows of the output spreadsheet should be |
| 53 | + * sorted. |
| 54 | + */ |
| 55 | + protected boolean m_sortOutput = false; |
| 56 | + |
| 57 | + /** |
| 58 | + * Creates a new instance of the relational operator, assuming that the rows |
| 59 | + * of its output will be unsorted. |
32 | 60 | * @param in_arity The input arity of the function |
33 | 61 | */ |
34 | 62 | public RelationalOperator(int in_arity) |
| 63 | + { |
| 64 | + this(in_arity, false); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Creates a new instance of the relational operator. |
| 69 | + * @param in_arity The input arity of the function |
| 70 | + * @param sort_output Set to <tt>true</tt> to sort rows of the output, |
| 71 | + * <tt>false</tt> otherwise |
| 72 | + */ |
| 73 | + public RelationalOperator(int in_arity, boolean sort_output) |
35 | 74 | { |
36 | 75 | super(in_arity, 1); |
| 76 | + m_mapping = new ArrayList<List<Integer[]>>(); |
| 77 | + m_sortOutput = sort_output; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Sets whether the the rows of the output spreadsheet should be sorted. |
| 82 | + * @param b Set to <tt>true</tt> to sort rows, <tt>false</tt> otherwise |
| 83 | + * @return This relational operator |
| 84 | + */ |
| 85 | + /*@ non_null @*/ public RelationalOperator sortOutput(boolean b) |
| 86 | + { |
| 87 | + m_sortOutput = b; |
| 88 | + return this; |
37 | 89 | } |
38 | 90 |
|
| 91 | + @Override |
| 92 | + public PartNode getExplanation(Part d, NodeFactory f) |
| 93 | + { |
| 94 | + Cell c = Cell.mentionedCell(d); |
| 95 | + if (c == null) |
| 96 | + { |
| 97 | + return super.getExplanation(d, f); |
| 98 | + } |
| 99 | + int c_row = c.getRow(); |
| 100 | + int c_col = c.getColumn(); |
| 101 | + PartNode root = f.getPartNode(d, this); |
| 102 | + if (c_row == 0) |
| 103 | + { |
| 104 | + // First row is made of labels |
| 105 | + LabelledNode to_add = root; |
| 106 | + int in_arity = getInputArity(); |
| 107 | + if (in_arity > 1) |
| 108 | + { |
| 109 | + OrNode or = f.getOrNode(); |
| 110 | + to_add.addChild(or); |
| 111 | + to_add = or; |
| 112 | + } |
| 113 | + for (int s_index = 0; s_index < in_arity; s_index++) |
| 114 | + { |
| 115 | + Part p = NthOutput.replaceOutByIn(d, s_index); |
| 116 | + to_add.addChild(f.getPartNode(p, this)); |
| 117 | + } |
| 118 | + return root; |
| 119 | + } |
| 120 | + if (c_row < 1 || c_row > m_mapping.size()) |
| 121 | + { |
| 122 | + root.addChild(f.getUnknownNode()); |
| 123 | + return root; |
| 124 | + } |
| 125 | + List<Integer[]> positions = m_mapping.get(c_row - 1); |
| 126 | + LabelledNode to_add = root; |
| 127 | + if (positions.size() > 1) |
| 128 | + { |
| 129 | + LabelledNode conn = getConnectorNode(f); |
| 130 | + to_add.addChild(conn); |
| 131 | + to_add = conn; |
| 132 | + } |
| 133 | + for (Integer[] pos : positions) |
| 134 | + { |
| 135 | + Part p = NthOutput.replaceOutByIn(d, pos[0]); |
| 136 | + p = Cell.replaceCellBy(p, Cell.get(c_col, pos[1])); |
| 137 | + to_add.addChild(f.getPartNode(p, this)); |
| 138 | + } |
| 139 | + return root; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Creates the output spreadsheet out of a list of rows gathered from the |
| 144 | + * input spreadsheets. This method is called by {@link #getValue(Object...)} |
| 145 | + * in each of the descendant classes. |
| 146 | + * @param top_row The values of the top row, containing the names of each |
| 147 | + * column of the output spreadsheet. The size of this array determines the |
| 148 | + * width of the output spreadsheet. |
| 149 | + * @param row_list The list of rows to be appended to the output spreadsheet. |
| 150 | + * The size of this list determines the height of the output spreadsheet |
| 151 | + * (which is size of list + 1). |
| 152 | + * @return The output spreadsheet |
| 153 | + */ |
| 154 | + protected Spreadsheet createOutput(Object[] top_row, List<Row> row_list) |
| 155 | + { |
| 156 | + Spreadsheet out = new Spreadsheet(top_row.length, row_list.size() + 1); |
| 157 | + for (int col = 0; col < top_row.length; col++) |
| 158 | + { |
| 159 | + // First row |
| 160 | + out.set(col, 0, top_row[col]); |
| 161 | + } |
| 162 | + if (m_sortOutput) |
| 163 | + { |
| 164 | + Collections.sort(row_list); |
| 165 | + } |
| 166 | + for (int i = 0; i < row_list.size(); i++) |
| 167 | + { |
| 168 | + Row r = row_list.get(i); |
| 169 | + r.set(out, i + 1); |
| 170 | + } |
| 171 | + return out; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Creates a labelled node for an explanation involving more than one |
| 176 | + * spreadsheet. What type of labelled node this corresponds to depends on the |
| 177 | + * actual operator providing the explanation. For example, {@link Union} |
| 178 | + * produces an {@link OrNode}, while {@link Intersection} produces an |
| 179 | + * {@link AndNode}. Classes where this method is not used can simply return |
| 180 | + * <tt>null</tt>. |
| 181 | + * @param f The node factory used to obtain a node instance |
| 182 | + * @return The node (or null) |
| 183 | + */ |
| 184 | + /*@ null @*/ protected abstract LabelledNode getConnectorNode(NodeFactory f); |
| 185 | + |
39 | 186 | /** |
40 | 187 | * Checks if two spreadsheets, when interpreted as relations, have the same |
41 | 188 | * signature. For two spreadsheets to have the same signature, they must have |
|
0 commit comments