1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2007, 2009, 2010, 2011, 2013 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "data/dataset.h"
26 #include "data/case.h"
27 #include "data/case-map.h"
28 #include "data/caseinit.h"
29 #include "data/casereader.h"
30 #include "data/casereader-provider.h"
31 #include "data/casereader-shim.h"
32 #include "data/casewriter.h"
33 #include "data/dictionary.h"
34 #include "data/file-handle-def.h"
35 #include "data/session.h"
36 #include "data/transformations.h"
37 #include "data/variable.h"
38 #include "libpspp/deque.h"
39 #include "libpspp/hash-functions.h"
40 #include "libpspp/hmap.h"
41 #include "libpspp/misc.h"
42 #include "libpspp/str.h"
43 #include "libpspp/taint.h"
44 #include "libpspp/i18n.h"
46 #include "gl/minmax.h"
47 #include "gl/xalloc.h"
50 /* A dataset is usually part of a session. Within a session its name must
51 unique. The name must either be a valid PSPP identifier or the empty
52 string. (It must be unique within the session even if it is the empty
53 string; that is, there may only be a single dataset within a session with
54 the empty string as its name.) */
55 struct session *session;
57 enum dataset_display display;
59 /* Cases are read from source,
60 their transformation variables are initialized,
61 pass through permanent_trns_chain (which transforms them into
62 the format described by permanent_dict),
64 pass through temporary_trns_chain (which transforms them into
65 the format described by dict),
66 and are finally passed to the procedure. */
67 struct casereader *source;
68 struct caseinit *caseinit;
69 struct trns_chain permanent_trns_chain;
70 struct dictionary *permanent_dict;
71 struct casewriter *sink;
72 struct trns_chain temporary_trns_chain;
74 struct dictionary *dict;
76 /* Stack of transformation chains for DO IF and LOOP and INPUT PROGRAM. */
77 struct trns_chain *stack;
79 size_t allocated_stack;
81 /* If true, cases are discarded instead of being written to
85 /* The case map used to compact a case, if necessary;
86 otherwise a null pointer. */
87 struct case_map *compactor;
89 /* Time at which proc was last invoked. */
90 time_t last_proc_invocation;
92 /* Cases just before ("lagging") the current one. */
93 int n_lag; /* Number of cases to lag. */
94 struct deque lag; /* Deque of lagged cases. */
95 struct ccase **lag_cases; /* Lagged cases managed by deque. */
100 PROC_COMMITTED, /* No procedure in progress. */
101 PROC_OPEN, /* proc_open called, casereader still open. */
102 PROC_CLOSED /* casereader from proc_open destroyed,
103 but proc_commit not yet called. */
106 casenumber cases_written; /* Cases output so far. */
107 bool ok; /* Error status. */
108 struct casereader_shim *shim; /* Shim on proc_open() casereader. */
110 const struct dataset_callbacks *callbacks;
113 /* Uniquely distinguishes datasets. */
117 static void dataset_changed__ (struct dataset *);
118 static void dataset_transformations_changed__ (struct dataset *,
121 static void add_measurement_level_trns (struct dataset *, struct dictionary *);
122 static void cancel_measurement_level_trns (struct trns_chain *);
123 static void add_case_limit_trns (struct dataset *ds);
124 static void add_filter_trns (struct dataset *ds);
126 static void update_last_proc_invocation (struct dataset *ds);
129 dict_callback (struct dictionary *d UNUSED, void *ds_)
131 struct dataset *ds = ds_;
132 dataset_changed__ (ds);
136 dataset_create_finish__ (struct dataset *ds, struct session *session)
138 static unsigned int seqno;
140 dict_set_change_callback (ds->dict, dict_callback, ds);
141 proc_cancel_all_transformations (ds);
142 dataset_set_session (ds, session);
146 /* Creates a new dataset named NAME, adds it to SESSION, and returns it. If
147 SESSION already contains a dataset named NAME, it is deleted and replaced.
148 The dataset initially has an empty dictionary and no data source. */
150 dataset_create (struct session *session, const char *name)
152 struct dataset *ds = XMALLOC (struct dataset);
153 *ds = (struct dataset) {
154 .name = xstrdup (name),
155 .display = DATASET_FRONT,
156 .dict = dict_create (get_default_encoding ()),
157 .caseinit = caseinit_create (),
159 dataset_create_finish__ (ds, session);
164 /* Creates and returns a new dataset that has the same data and dictionary as
165 OLD named NAME, adds it to the same session as OLD, and returns the new
166 dataset. If SESSION already contains a dataset named NAME, it is deleted
169 OLD must not have any active transformations or temporary state and must
170 not be in the middle of a procedure.
172 Callbacks are not cloned. */
174 dataset_clone (struct dataset *old, const char *name)
178 assert (old->proc_state == PROC_COMMITTED);
179 assert (!old->permanent_trns_chain.n);
180 assert (old->permanent_dict == NULL);
181 assert (old->sink == NULL);
182 assert (!old->temporary);
183 assert (!old->temporary_trns_chain.n);
184 assert (!old->n_stack);
186 new = xzalloc (sizeof *new);
187 new->name = xstrdup (name);
188 new->display = DATASET_FRONT;
189 new->source = casereader_clone (old->source);
190 new->dict = dict_clone (old->dict);
191 new->caseinit = caseinit_clone (old->caseinit);
192 new->last_proc_invocation = old->last_proc_invocation;
195 dataset_create_finish__ (new, old->session);
202 dataset_destroy (struct dataset *ds)
206 dataset_set_session (ds, NULL);
208 dict_unref (ds->dict);
209 dict_unref (ds->permanent_dict);
210 caseinit_destroy (ds->caseinit);
211 trns_chain_uninit (&ds->permanent_trns_chain);
212 for (size_t i = 0; i < ds->n_stack; i++)
213 trns_chain_uninit (&ds->stack[i]);
215 dataset_transformations_changed__ (ds, false);
221 /* Discards the active dataset's dictionary, data, and transformations. */
223 dataset_clear (struct dataset *ds)
225 assert (ds->proc_state == PROC_COMMITTED);
227 dict_clear (ds->dict);
228 fh_set_default_handle (NULL);
232 casereader_destroy (ds->source);
235 proc_cancel_all_transformations (ds);
239 dataset_name (const struct dataset *ds)
245 dataset_set_name (struct dataset *ds, const char *name)
247 struct session *session = ds->session;
252 active = session_active_dataset (session) == ds;
254 session_set_active_dataset (session, NULL);
255 dataset_set_session (ds, NULL);
259 ds->name = xstrdup (name);
263 dataset_set_session (ds, session);
265 session_set_active_dataset (session, ds);
270 dataset_session (const struct dataset *ds)
276 dataset_set_session (struct dataset *ds, struct session *session)
278 if (session != ds->session)
280 if (ds->session != NULL)
281 session_remove_dataset (ds->session, ds);
283 session_add_dataset (session, ds);
287 /* Returns the dictionary within DS. This is always nonnull, although it
288 might not contain any variables. */
290 dataset_dict (const struct dataset *ds)
295 /* Replaces DS's dictionary by DICT, discarding any source and
298 dataset_set_dict (struct dataset *ds, struct dictionary *dict)
300 assert (ds->proc_state == PROC_COMMITTED);
301 assert (ds->dict != dict);
305 dict_unref (ds->dict);
307 dict_set_change_callback (ds->dict, dict_callback, ds);
310 /* Returns the casereader that will be read when a procedure is executed on
311 DS. This can be NULL if none has been set up yet. */
312 const struct casereader *
313 dataset_source (const struct dataset *ds)
318 /* Returns true if DS has a data source, false otherwise. */
320 dataset_has_source (const struct dataset *ds)
322 return dataset_source (ds) != NULL;
325 /* Replaces the active dataset's data by READER. READER's cases must have an
326 appropriate format for DS's dictionary. */
328 dataset_set_source (struct dataset *ds, struct casereader *reader)
330 casereader_destroy (ds->source);
333 caseinit_clear (ds->caseinit);
334 caseinit_mark_as_preinited (ds->caseinit, ds->dict);
336 return reader == NULL || !casereader_error (reader);
339 /* Returns the data source from DS and removes it from DS. Returns a null
340 pointer if DS has no data source. */
342 dataset_steal_source (struct dataset *ds)
344 struct casereader *reader = ds->source;
350 /* Returns a number unique to DS. It can be used to distinguish one dataset
351 from any other within a given program run, even datasets that do not exist
354 dataset_seqno (const struct dataset *ds)
360 dataset_set_callbacks (struct dataset *ds,
361 const struct dataset_callbacks *callbacks,
364 ds->callbacks = callbacks;
365 ds->cb_data = cb_data;
369 dataset_get_display (const struct dataset *ds)
375 dataset_set_display (struct dataset *ds, enum dataset_display display)
377 ds->display = display;
380 /* Returns the last time the data was read. */
382 time_of_last_procedure (struct dataset *ds)
386 if (ds->last_proc_invocation == 0)
387 update_last_proc_invocation (ds);
388 return ds->last_proc_invocation;
391 /* Regular procedure. */
393 /* Executes any pending transformations, if necessary.
394 This is not identical to the EXECUTE command in that it won't
395 always read the source data. This can be important when the
396 source data is given inline within BEGIN DATA...END FILE. */
398 proc_execute (struct dataset *ds)
402 if ((!ds->temporary || !ds->temporary_trns_chain.n)
403 && !ds->permanent_trns_chain.n)
406 ds->discard_output = false;
407 dict_set_case_limit (ds->dict, 0);
408 dict_clear_vectors (ds->dict);
412 ok = casereader_destroy (proc_open (ds));
413 return proc_commit (ds) && ok;
416 static const struct casereader_class proc_casereader_class;
418 /* Opens dataset DS for reading cases with proc_read. If FILTER is true, then
419 cases filtered out with FILTER BY will not be included in the casereader
420 (which is usually desirable). If FILTER is false, all cases will be
421 included regardless of FILTER BY settings.
423 proc_commit must be called when done. */
425 proc_open_filtering (struct dataset *ds, bool filter)
427 struct casereader *reader;
429 assert (ds->n_stack == 0);
430 assert (ds->source != NULL);
431 assert (ds->proc_state == PROC_COMMITTED);
433 update_last_proc_invocation (ds);
435 caseinit_mark_for_init (ds->caseinit, ds->dict);
437 /* Finish up the collection of transformations. */
438 add_case_limit_trns (ds);
440 add_filter_trns (ds);
441 if (!proc_in_temporary_transformations (ds))
442 add_measurement_level_trns (ds, ds->dict);
444 /* Make permanent_dict refer to the dictionary right before
445 data reaches the sink. */
446 if (ds->permanent_dict == NULL)
447 ds->permanent_dict = ds->dict;
450 if (!ds->discard_output)
452 struct dictionary *pd = ds->permanent_dict;
453 size_t compacted_n_values = dict_count_values (pd, 1u << DC_SCRATCH);
454 if (compacted_n_values < dict_get_next_value_idx (pd))
456 struct caseproto *compacted_proto;
457 compacted_proto = dict_get_compacted_proto (pd, 1u << DC_SCRATCH);
458 ds->compactor = case_map_to_compact_dict (pd, 1u << DC_SCRATCH);
459 ds->sink = autopaging_writer_create (compacted_proto);
460 caseproto_unref (compacted_proto);
464 ds->compactor = NULL;
465 ds->sink = autopaging_writer_create (dict_get_proto (pd));
470 ds->compactor = NULL;
474 /* Allocate memory for lagged cases. */
475 ds->lag_cases = deque_init (&ds->lag, ds->n_lag, sizeof *ds->lag_cases);
477 ds->proc_state = PROC_OPEN;
478 ds->cases_written = 0;
481 /* FIXME: use taint in dataset in place of `ok'? */
482 /* FIXME: for trivial cases we can just return a clone of
485 /* Create casereader and insert a shim on top. The shim allows us to
486 arbitrarily extend the casereader's lifetime, by slurping the cases into
487 the shim's buffer in proc_commit(). That is especially useful when output
488 table_items are generated directly from the procedure casereader (e.g. by
489 the LIST procedure) when we are using an output driver that keeps a
490 reference to the output items passed to it (e.g. the GUI output driver in
492 reader = casereader_create_sequential (NULL, dict_get_proto (ds->dict),
494 &proc_casereader_class, ds);
495 ds->shim = casereader_shim_insert (reader);
499 /* Opens dataset DS for reading cases with proc_read.
500 proc_commit must be called when done. */
502 proc_open (struct dataset *ds)
504 return proc_open_filtering (ds, true);
507 /* Returns true if a procedure is in progress, that is, if
508 proc_open has been called but proc_commit has not. */
510 proc_is_open (const struct dataset *ds)
512 return ds->proc_state != PROC_COMMITTED;
515 /* "read" function for procedure casereader. */
516 static struct ccase *
517 proc_casereader_read (struct casereader *reader UNUSED, void *ds_)
519 struct dataset *ds = ds_;
520 enum trns_result retval = TRNS_DROP_CASE;
523 assert (ds->proc_state == PROC_OPEN);
524 for (; ; case_unref (c))
526 assert (retval == TRNS_DROP_CASE || retval == TRNS_ERROR);
527 if (retval == TRNS_ERROR)
532 /* Read a case from source. */
533 c = casereader_read (ds->source);
536 c = case_unshare_and_resize (c, dict_get_proto (ds->dict));
537 caseinit_init_vars (ds->caseinit, c);
539 /* Execute permanent transformations. */
540 casenumber case_nr = ds->cases_written + 1;
541 retval = trns_chain_execute (&ds->permanent_trns_chain, case_nr, &c);
542 caseinit_update_left_vars (ds->caseinit, c);
543 if (retval != TRNS_CONTINUE)
546 /* Write case to collection of lagged cases. */
549 while (deque_count (&ds->lag) >= ds->n_lag)
550 case_unref (ds->lag_cases[deque_pop_back (&ds->lag)]);
551 ds->lag_cases[deque_push_front (&ds->lag)] = case_ref (c);
554 /* Write case to replacement dataset. */
556 if (ds->sink != NULL)
557 casewriter_write (ds->sink,
558 case_map_execute (ds->compactor, case_ref (c)));
560 /* Execute temporary transformations. */
561 if (ds->temporary_trns_chain.n)
563 retval = trns_chain_execute (&ds->temporary_trns_chain,
564 ds->cases_written, &c);
565 if (retval != TRNS_CONTINUE)
573 /* "destroy" function for procedure casereader. */
575 proc_casereader_destroy (struct casereader *reader, void *ds_)
577 struct dataset *ds = ds_;
580 /* We are always the subreader for a casereader_buffer, so if we're being
581 destroyed then it's because the casereader_buffer has read all the cases
582 that it ever will. */
585 /* Make sure transformations happen for every input case, in
586 case they have side effects, and ensure that the replacement
587 active dataset gets all the cases it should. */
588 while ((c = casereader_read (reader)) != NULL)
591 ds->proc_state = PROC_CLOSED;
592 ds->ok = casereader_destroy (ds->source) && ds->ok;
594 dataset_set_source (ds, NULL);
597 /* Must return false if the source casereader, a transformation,
598 or the sink casewriter signaled an error. (If a temporary
599 transformation signals an error, then the return value is
600 false, but the replacement active dataset may still be
603 proc_commit (struct dataset *ds)
605 if (ds->shim != NULL)
606 casereader_shim_slurp (ds->shim);
608 assert (ds->proc_state == PROC_CLOSED);
609 ds->proc_state = PROC_COMMITTED;
611 dataset_changed__ (ds);
613 /* Free memory for lagged cases. */
614 while (!deque_is_empty (&ds->lag))
615 case_unref (ds->lag_cases[deque_pop_back (&ds->lag)]);
616 free (ds->lag_cases);
618 /* Dictionary from before TEMPORARY becomes permanent. */
619 proc_cancel_temporary_transformations (ds);
620 bool ok = proc_cancel_all_transformations (ds) && ds->ok;
622 if (!ds->discard_output)
624 /* Finish compacting. */
625 if (ds->compactor != NULL)
627 case_map_destroy (ds->compactor);
628 ds->compactor = NULL;
630 dict_delete_scratch_vars (ds->dict);
631 dict_compact_values (ds->dict);
634 /* Old data sink becomes new data source. */
635 if (ds->sink != NULL)
636 ds->source = casewriter_make_reader (ds->sink);
641 ds->discard_output = false;
645 caseinit_clear (ds->caseinit);
646 caseinit_mark_as_preinited (ds->caseinit, ds->dict);
648 dict_clear_vectors (ds->dict);
649 ds->permanent_dict = NULL;
653 /* Casereader class for procedure execution. */
654 static const struct casereader_class proc_casereader_class =
656 proc_casereader_read,
657 proc_casereader_destroy,
662 /* Updates last_proc_invocation. */
664 update_last_proc_invocation (struct dataset *ds)
666 ds->last_proc_invocation = time (NULL);
669 /* Returns a pointer to the lagged case from N_BEFORE cases before the
670 current one, or NULL if there haven't been that many cases yet. */
672 lagged_case (const struct dataset *ds, int n_before)
674 assert (n_before >= 1);
675 assert (n_before <= ds->n_lag);
677 if (n_before <= deque_count (&ds->lag))
678 return ds->lag_cases[deque_front (&ds->lag, n_before - 1)];
683 /* Adds TRNS to the current set of transformations. */
685 add_transformation (struct dataset *ds,
686 const struct trns_class *class, void *aux)
688 struct trns_chain *chain = (ds->n_stack > 0 ? &ds->stack[ds->n_stack - 1]
689 : ds->temporary ? &ds->temporary_trns_chain
690 : &ds->permanent_trns_chain);
691 struct transformation t = { .class = class, .aux = aux };
692 trns_chain_append (chain, &t);
693 dataset_transformations_changed__ (ds, true);
696 /* Returns true if the next call to add_transformation() will add
697 a temporary transformation, false if it will add a permanent
700 proc_in_temporary_transformations (const struct dataset *ds)
702 return ds->temporary;
705 /* Marks the start of temporary transformations.
706 Further calls to add_transformation() will add temporary
709 proc_start_temporary_transformations (struct dataset *ds)
711 assert (!ds->n_stack);
712 if (!proc_in_temporary_transformations (ds))
714 add_case_limit_trns (ds);
716 ds->permanent_dict = dict_clone (ds->dict);
717 add_measurement_level_trns (ds, ds->permanent_dict);
719 ds->temporary = true;
720 dataset_transformations_changed__ (ds, true);
724 /* Converts all the temporary transformations, if any, to permanent
725 transformations. Further transformations will be permanent.
727 The FILTER command is implemented as a temporary transformation, so a
728 procedure that uses this function should usually use proc_open_filtering()
729 with FILTER false, instead of plain proc_open().
731 Returns true if anything changed, false otherwise. */
733 proc_make_temporary_transformations_permanent (struct dataset *ds)
735 if (proc_in_temporary_transformations (ds))
737 cancel_measurement_level_trns (&ds->permanent_trns_chain);
738 trns_chain_splice (&ds->permanent_trns_chain, &ds->temporary_trns_chain);
740 ds->temporary = false;
742 dict_unref (ds->permanent_dict);
743 ds->permanent_dict = NULL;
751 /* Cancels all temporary transformations, if any. Further
752 transformations will be permanent.
753 Returns true if anything changed, false otherwise. */
755 proc_cancel_temporary_transformations (struct dataset *ds)
757 if (proc_in_temporary_transformations (ds))
759 trns_chain_clear (&ds->temporary_trns_chain);
761 dict_unref (ds->dict);
762 ds->dict = ds->permanent_dict;
763 ds->permanent_dict = NULL;
765 dataset_transformations_changed__ (ds, ds->permanent_trns_chain.n != 0);
772 /* Cancels all transformations, if any.
773 Returns true if successful, false on I/O error. */
775 proc_cancel_all_transformations (struct dataset *ds)
778 assert (ds->proc_state == PROC_COMMITTED);
779 ok = trns_chain_clear (&ds->permanent_trns_chain);
780 ok = trns_chain_clear (&ds->temporary_trns_chain) && ok;
781 ds->temporary = false;
782 for (size_t i = 0; i < ds->n_stack; i++)
783 ok = trns_chain_uninit (&ds->stack[i]) && ok;
785 dataset_transformations_changed__ (ds, false);
791 proc_push_transformations (struct dataset *ds)
793 if (ds->n_stack >= ds->allocated_stack)
794 ds->stack = x2nrealloc (ds->stack, &ds->allocated_stack,
796 trns_chain_init (&ds->stack[ds->n_stack++]);
800 proc_pop_transformations (struct dataset *ds, struct trns_chain *chain)
802 assert (ds->n_stack > 0);
803 *chain = ds->stack[--ds->n_stack];
806 static enum trns_result
807 store_case_num (void *var_, struct ccase **cc, casenumber case_num)
809 struct variable *var = var_;
811 *cc = case_unshare (*cc);
812 *case_num_rw (*cc, var) = case_num;
814 return TRNS_CONTINUE;
817 /* Add a variable which we can sort by to get back the original order. */
819 add_permanent_ordering_transformation (struct dataset *ds)
821 struct variable *temp_var = dict_create_var_assert (ds->dict, "$ORDER", 0);
822 struct variable *order_var
823 = (proc_in_temporary_transformations (ds)
824 ? dict_clone_var_in_place_assert (ds->permanent_dict, temp_var)
827 static const struct trns_class trns_class = {
829 .execute = store_case_num
831 const struct transformation t = { .class = &trns_class, .aux = order_var };
832 trns_chain_append (&ds->permanent_trns_chain, &t);
837 /* Causes output from the next procedure to be discarded, instead
838 of being preserved for use as input for the next procedure. */
840 proc_discard_output (struct dataset *ds)
842 ds->discard_output = true;
846 /* Checks whether DS has a corrupted active dataset. If so,
847 discards it and returns false. If not, returns true without
850 dataset_end_of_command (struct dataset *ds)
852 if (ds->source != NULL)
854 if (casereader_error (ds->source))
861 const struct taint *taint = casereader_get_taint (ds->source);
862 taint_reset_successor_taint (CONST_CAST (struct taint *, taint));
863 assert (!taint_has_tainted_successor (taint));
869 /* Limits the maximum number of cases processed to
871 static enum trns_result
872 case_limit_trns_proc (void *cases_remaining_,
873 struct ccase **c UNUSED, casenumber case_nr UNUSED)
875 size_t *cases_remaining = cases_remaining_;
876 if (*cases_remaining > 0)
878 (*cases_remaining)--;
879 return TRNS_CONTINUE;
882 return TRNS_DROP_CASE;
885 /* Frees the data associated with a case limit transformation. */
887 case_limit_trns_free (void *cases_remaining_)
889 size_t *cases_remaining = cases_remaining_;
890 free (cases_remaining);
894 /* Adds a transformation that limits the number of cases that may
895 pass through, if DS->DICT has a case limit. */
897 add_case_limit_trns (struct dataset *ds)
899 casenumber case_limit = dict_get_case_limit (ds->dict);
902 casenumber *cases_remaining = xmalloc (sizeof *cases_remaining);
903 *cases_remaining = case_limit;
905 static const struct trns_class trns_class = {
906 .name = "case limit",
907 .execute = case_limit_trns_proc,
908 .destroy = case_limit_trns_free,
910 add_transformation (ds, &trns_class, cases_remaining);
912 dict_set_case_limit (ds->dict, 0);
917 /* FILTER transformation. */
918 static enum trns_result
919 filter_trns_proc (void *filter_var_,
920 struct ccase **c, casenumber case_nr UNUSED)
923 struct variable *filter_var = filter_var_;
924 double f = case_num (*c, filter_var);
925 return (f != 0.0 && !var_is_num_missing (filter_var, f)
926 ? TRNS_CONTINUE : TRNS_DROP_CASE);
929 /* Adds a temporary transformation to filter data according to
930 the variable specified on FILTER, if any. */
932 add_filter_trns (struct dataset *ds)
934 struct variable *filter_var = dict_get_filter (ds->dict);
935 if (filter_var != NULL)
937 proc_start_temporary_transformations (ds);
939 static const struct trns_class trns_class = {
941 .execute = filter_trns_proc,
943 add_transformation (ds, &trns_class, filter_var);
948 dataset_need_lag (struct dataset *ds, int n_before)
950 ds->n_lag = MAX (ds->n_lag, n_before);
953 /* Measurement guesser, for guessing a measurement level from formats and
958 struct hmap_node hmap_node;
964 struct variable *var;
969 mg_var_uninit (struct mg_var *mgv)
971 struct mg_value *mgvalue, *next;
972 HMAP_FOR_EACH_SAFE (mgvalue, next, struct mg_value, hmap_node,
975 hmap_delete (mgv->values, &mgvalue->hmap_node);
978 hmap_destroy (mgv->values);
983 mg_var_interpret (const struct mg_var *mgv)
985 size_t n = hmap_count (mgv->values);
988 /* All missing (or no data). */
989 return MEASURE_NOMINAL;
992 const struct mg_value *mgvalue;
993 HMAP_FOR_EACH (mgvalue, struct mg_value, hmap_node,
995 if (mgvalue->value < 10)
996 return MEASURE_NOMINAL;
997 return MEASURE_SCALE;
1001 mg_var_add_value (struct mg_var *mgv, double value)
1003 if (var_is_num_missing (mgv->var, value))
1004 return MEASURE_UNKNOWN;
1005 else if (value < 0 || value != floor (value))
1006 return MEASURE_SCALE;
1008 size_t hash = hash_double (value, 0);
1009 struct mg_value *mgvalue;
1010 HMAP_FOR_EACH_WITH_HASH (mgvalue, struct mg_value, hmap_node,
1012 if (mgvalue->value == value)
1013 return MEASURE_UNKNOWN;
1015 mgvalue = xmalloc (sizeof *mgvalue);
1016 mgvalue->value = value;
1017 hmap_insert (mgv->values, &mgvalue->hmap_node, hash);
1018 if (hmap_count (mgv->values) >= settings_get_scalemin ())
1019 return MEASURE_SCALE;
1021 return MEASURE_UNKNOWN;
1024 struct measure_guesser
1026 struct mg_var *vars;
1030 static struct measure_guesser *
1031 measure_guesser_create__ (struct dictionary *dict)
1033 struct mg_var *mgvs = NULL;
1035 size_t allocated_mgvs = 0;
1037 for (size_t i = 0; i < dict_get_n_vars (dict); i++)
1039 struct variable *var = dict_get_var (dict, i);
1040 if (var_get_measure (var) != MEASURE_UNKNOWN)
1043 const struct fmt_spec *f = var_get_print_format (var);
1044 enum measure m = var_default_measure_for_format (f->type);
1045 if (m != MEASURE_UNKNOWN)
1047 var_set_measure (var, m);
1051 if (n_mgvs >= allocated_mgvs)
1052 mgvs = x2nrealloc (mgvs, &allocated_mgvs, sizeof *mgvs);
1054 struct mg_var *mgv = &mgvs[n_mgvs++];
1055 *mgv = (struct mg_var) {
1057 .values = xmalloc (sizeof *mgv->values),
1059 hmap_init (mgv->values);
1064 struct measure_guesser *mg = xmalloc (sizeof *mg);
1065 *mg = (struct measure_guesser) {
1072 /* Scans through DS's dictionary for variables that have an unknown measurement
1073 level. For those, if the measurement level can be guessed based on the
1074 variable's type and format, sets a default. If that's enough, returns NULL.
1075 If any remain whose levels are unknown and can't be guessed that way,
1076 creates and returns a structure that the caller should pass to
1077 measure_guesser_add_case() or measure_guesser_run() for guessing a
1078 measurement level based on the data. */
1079 struct measure_guesser *
1080 measure_guesser_create (struct dataset *ds)
1082 return measure_guesser_create__ (dataset_dict (ds));
1085 /* Adds data from case C to MG. */
1087 measure_guesser_add_case (struct measure_guesser *mg, const struct ccase *c)
1089 for (size_t i = 0; i < mg->n_vars; )
1091 struct mg_var *mgv = &mg->vars[i];
1092 double value = case_num (c, mgv->var);
1093 enum measure m = mg_var_add_value (mgv, value);
1094 if (m != MEASURE_UNKNOWN)
1096 var_set_measure (mgv->var, m);
1098 mg_var_uninit (mgv);
1099 *mgv = mg->vars[--mg->n_vars];
1108 measure_guesser_destroy (struct measure_guesser *mg)
1113 for (size_t i = 0; i < mg->n_vars; i++)
1115 struct mg_var *mgv = &mg->vars[i];
1116 var_set_measure (mgv->var, mg_var_interpret (mgv));
1117 mg_var_uninit (mgv);
1123 /* Adds final measurement levels based on MG, after all the cases have been
1126 measure_guesser_commit (struct measure_guesser *mg)
1128 for (size_t i = 0; i < mg->n_vars; i++)
1130 struct mg_var *mgv = &mg->vars[i];
1131 var_set_measure (mgv->var, mg_var_interpret (mgv));
1135 /* Passes the cases in READER through MG and uses the data in the cases to set
1136 measurement levels for the variables where they were still unknown. */
1138 measure_guesser_run (struct measure_guesser *mg,
1139 const struct casereader *reader)
1141 struct casereader *r = casereader_clone (reader);
1142 while (mg->n_vars > 0)
1144 struct ccase *c = casereader_read (r);
1147 measure_guesser_add_case (mg, c);
1150 casereader_destroy (r);
1152 measure_guesser_commit (mg);
1155 /* A transformation for guessing measurement levels. */
1157 static enum trns_result
1158 mg_trns_proc (void *mg_, struct ccase **c, casenumber case_nr UNUSED)
1160 struct measure_guesser *mg = mg_;
1161 measure_guesser_add_case (mg, *c);
1162 return TRNS_CONTINUE;
1166 mg_trns_free (void *mg_)
1168 struct measure_guesser *mg = mg_;
1169 measure_guesser_commit (mg);
1170 measure_guesser_destroy (mg);
1174 static const struct trns_class mg_trns_class = {
1175 .name = "add measurement level",
1176 .execute = mg_trns_proc,
1177 .destroy = mg_trns_free,
1181 add_measurement_level_trns (struct dataset *ds, struct dictionary *dict)
1183 struct measure_guesser *mg = measure_guesser_create__ (dict);
1185 add_transformation (ds, &mg_trns_class, mg);
1189 cancel_measurement_level_trns (struct trns_chain *chain)
1194 struct transformation *trns = &chain->xforms[chain->n - 1];
1195 if (trns->class != &mg_trns_class)
1198 struct measure_guesser *mg = trns->aux;
1199 measure_guesser_destroy (mg);
1204 dataset_changed__ (struct dataset *ds)
1206 if (ds->callbacks != NULL && ds->callbacks->changed != NULL)
1207 ds->callbacks->changed (ds->cb_data);
1211 dataset_transformations_changed__ (struct dataset *ds, bool non_empty)
1213 if (ds->callbacks != NULL && ds->callbacks->transformations_changed != NULL)
1214 ds->callbacks->transformations_changed (non_empty, ds->cb_data);
1217 /* Private interface for use by session code. */
1220 dataset_set_session__ (struct dataset *ds, struct session *session)
1222 ds->session = session;