updateVariablesProvider function
void
updateVariablesProvider( - WidgetRef ref
)
Implementation
void updateVariablesProvider(WidgetRef ref) {
// Reset the rolesProvider and typesProvider.
// ref.read(rolesProvider.notifier).state = {};
// ref.read(typesProvider.notifier).state = {};
// Get the most recent vars information from glimpse and update the
// information in roles provider and types provider.
String stdout = ref.watch(stdoutProvider);
List<VariableInfo> vars = extractVariables(stdout);
// 20241213 gjw Remove this for now. It was used for determining IGNORE roles
// but due to an issue with that (see other comments) it is no longer being
// done.
// List<String> highVars = extractLargeFactors(stdout);
// When a new row is added after transformation, initialise its role and
// update the role of the old variable.
for (var column in vars) {
// Update roles.
if (!ref.read(rolesProvider.notifier).state.containsKey(column.name)) {
// We first check if the variable name has one of the prefixes that
// indicatre it is a transformed variable in Rattle. Note this is somewhat
// dangerous assumption that the loaded dataset does not contain variables
// with the same prefixes. But that's how it has been implemented
// here. (gjw 20250318)
if (isTransformedVar(column.name)) {
// Update the old variable's role.
String target = getTarget(ref);
// If the target variable is the same as the original variable, then
// set the role to target, otherwise set it to input.
ref.read(rolesProvider.notifier).state[column.name] =
target == getOriginal(column.name) ? Role.target : Role.input;
// Update the new variable's role.
ref.read(rolesProvider.notifier).state[getOriginal(column.name)] =
Role.ignoreAfterTransformed;
} else {
// Keep this debugPrint(). If we add a new transformation (as we did for
// TFC) and the developer has not added it to the known prefixes above
// then they need to be informed to do so. A NULL exception is generated
// otherwise. (gjw 20250108)
debugPrint(
'** ERROR: Unidentified transformed variable: ${column.name}.\n'
'** ERROR: Please add its prefix to the list in '
'utils/update_roles_provider.dart',
);
// It seems we have an issue here because after delting an ignored
// variable we end up here withthis error and then an Exception because
// we have not actually deleted the variable from the ds. Try adding
// this variable to have a role of ignoreAfterTransformed. But for a
// variable like max_temp later on the max_gets stripped and then we get
// a NULL exception for the variable temp! (gjw 20250318)
// ref.read(rolesProvider.notifier).state[getOriginal(column.name)] =
// Role.ignoreAfterTransformed;
}
}
// Update the types.
if (!ref.read(typesProvider.notifier).state.containsKey(column.name)) {
ref.read(typesProvider.notifier).state[column.name] =
isNumeric(column.type) ? Type.numeric : Type.categoric;
}
// 20241213 gjw Remove this heuristic for now. It is causing the IGNORE of
// country in the PROTEIN dataset to be retained even if I change it to
// INPUT/RISK/IDENT. Not if I change it to TARGET as per the test below. It
// should actually be IDENT or TARGET.
// 20241213 gjw This is repeated code from dataset/display.dart - we should
// avoid repeated code as it is harder to maintain as I have just
// demonstrated - I had to find this in two places to fix :-)
// for (var highVar in highVars) {
// if (ref.read(rolesProvider.notifier).state[highVar] != Role.target) {
// ref.read(rolesProvider.notifier).state[highVar] = Role.ignore;
// }
// }
}
}