Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

saving and loading functional hierarchies with csv files in the GUI #435

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions src/gui/org/deidentifier/arx/gui/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
import org.deidentifier.arx.DataType.DataTypeDescription;
import org.deidentifier.arx.RowSet;
import org.deidentifier.arx.aggregates.HierarchyBuilder;
import org.deidentifier.arx.aggregates.HierarchyBuilderDate;
import org.deidentifier.arx.aggregates.HierarchyBuilderIntervalBased;
import org.deidentifier.arx.aggregates.HierarchyBuilderOrderBased;
import org.deidentifier.arx.aggregates.HierarchyBuilderPriorityBased;
import org.deidentifier.arx.aggregates.HierarchyBuilderRedactionBased;
import org.deidentifier.arx.aggregates.StatisticsFrequencyDistribution;
import org.deidentifier.arx.exceptions.RollbackRequiredException;
import org.deidentifier.arx.gui.model.Model;
Expand Down Expand Up @@ -1299,8 +1304,53 @@ public void actionMenuFileExportHierarchy() {
main.showErrorDialog(main.getShell(),
Resources.getMessage("Controller.50"), e); //$NON-NLS-1$
}

// Save functional hierarchy as well
actionExportFunctionalHierarchy(file);
}

/**
* Save functional hierarchy of the selected attribute when saving the hierarchy.
* TODO: Suggestions:
* - Adding this part to the actionMenuFileExportHierarchy
* - or, Create new menu item
* - The saving and loading should be handled in the API
* but is teems complicated due to the design of the wizard.
*/
public void actionExportFunctionalHierarchy(String file) {
String attrib = model.getSelectedAttribute();
String filePath = file.replace("csv", "ahs"); //$NON-NLS-1$ //$NON-NLS-1$
final String ERROR_HEADER = Resources.getMessage("HierarchyWizard.11"); //$NON-NLS-1$
final String ERROR_TEXT = Resources.getMessage("HierarchyWizard.12"); //$NON-NLS-1$

// Save
try {
// Select
HierarchyBuilder<?> builder = null;
if (model.getInputConfig().getHierarchyBuilder(attrib) instanceof HierarchyBuilderDate){
builder = (HierarchyBuilder<?>) model.getInputConfig().getHierarchyBuilder(attrib);
} else if (model.getInputConfig().getHierarchyBuilder(attrib) instanceof HierarchyBuilderOrderBased){
builder = (HierarchyBuilder<?>) model.getInputConfig().getHierarchyBuilder(attrib);
} else if (model.getInputConfig().getHierarchyBuilder(attrib) instanceof HierarchyBuilderIntervalBased){
builder = (HierarchyBuilder<?>) model.getInputConfig().getHierarchyBuilder(attrib);
} else if (model.getInputConfig().getHierarchyBuilder(attrib) instanceof HierarchyBuilderRedactionBased){
builder = (HierarchyBuilder<?>) model.getInputConfig().getHierarchyBuilder(attrib);
} else if (model.getInputConfig().getHierarchyBuilder(attrib) instanceof HierarchyBuilderPriorityBased){
builder = (HierarchyBuilder<?>) model.getInputConfig().getHierarchyBuilder(attrib);
} else {
actionShowInfoDialog(main.getShell(), ERROR_HEADER, ERROR_TEXT);
return;
}

// Save
builder.save(filePath);
} catch (Exception e){
e.printStackTrace();
actionShowInfoDialog(main.getShell(), ERROR_HEADER, ERROR_TEXT+e.getMessage());
return;
}
}

/**
* File->Import data.
*/
Expand Down Expand Up @@ -1403,8 +1453,74 @@ public void actionMenuFileImportHierarchy() {
update(new ModelEvent(this, ModelPart.HIERARCHY, hierarchy));
}
}
// load functional hierarchy if found!
actionImportFunctionalHierarchy(path);

}

/**
* Import functional hierarchy.
* If .ahs file exist, it will be used to get better loss calculation
* TODO: Suggestion: A function should be added to the API and used in HierarchyWizard class and here
* instead of repeating code
*/
public void actionImportFunctionalHierarchy(String filePath) {

// Note: API unfortunately does not support load

final String ERROR_HEADER = Resources.getMessage("HierarchyWizard.5"); //$NON-NLS-1$
final String ERROR_TEXT = Resources.getMessage("HierarchyWizard.6"); //$NON-NLS-1$

// .ahs File path
String file = filePath.replace("csv", "ahs"); //$NON-NLS-1$

// Current attribute
String attrib =model.getSelectedAttribute();

// Load
HierarchyBuilder<?> loaded = null;
try {
loaded = HierarchyBuilder.create(file);
} catch (Exception e){
actionShowInfoDialog(main.getShell(), ERROR_HEADER, ERROR_TEXT+e.getMessage());
return;
}

// Checks
if (loaded == null) return;
DataHandle inputHandle = model.getInputConfig().getInput().getHandle();

//TODO: There should be a function to get all rows of a column or all columns of a row
String[] atrribData = new String[inputHandle.getNumRows()];
for (int i=0; i< inputHandle.getNumRows(); i++){
atrribData[i] = inputHandle.getValue(i,inputHandle.getColumnIndexOf(attrib));
}

if (loaded.getType() == HierarchyBuilder.Type.REDACTION_BASED) {
HierarchyBuilderRedactionBased<?> builder = (HierarchyBuilderRedactionBased<?>)loaded;
builder.prepare(atrribData);
model.getInputConfig().setHierarchyBuilder(attrib, builder);
} else if (loaded.getType() == HierarchyBuilder.Type.DATE_BASED) {
HierarchyBuilderDate builder = (HierarchyBuilderDate)loaded;
builder.prepare(atrribData);
model.getInputConfig().setHierarchyBuilder(attrib, builder);
} else if (loaded.getType() == HierarchyBuilder.Type.INTERVAL_BASED) {
HierarchyBuilderIntervalBased<?> builder = (HierarchyBuilderIntervalBased<?>)loaded;
builder.prepare(atrribData);
model.getInputConfig().setHierarchyBuilder(attrib, builder);
} else if (loaded.getType() == HierarchyBuilder.Type.ORDER_BASED) {
HierarchyBuilderOrderBased<?> builder = (HierarchyBuilderOrderBased<?>)loaded;
builder.prepare(atrribData);
model.getInputConfig().setHierarchyBuilder(attrib, builder);
} else if (loaded.getType() == HierarchyBuilder.Type.PRIORITY_BASED) {
HierarchyBuilderPriorityBased<?> builder = (HierarchyBuilderPriorityBased<?>)loaded;
builder.prepare(atrribData);
model.getInputConfig().setHierarchyBuilder(attrib, builder);
} else {
actionShowInfoDialog(main.getShell(), ERROR_HEADER, ERROR_TEXT);
return;
}
}
/**
* File->New project.
*/
Expand Down