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

Improve the ID range policy feature #1179

Merged
merged 4 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ public OWLOntology reload(OWLOntology ont) throws OWLOntologyCreationException {
// Rebuild the cache in case the imports closure has changed
rebuildActiveOntologiesCache();
refreshRenderer();
idRangesPolocyManager.reload();
} finally {
setClean(ont);
fireEvent(EventType.ONTOLOGY_RELOADED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public class EntityCreationPreferences {
private static final String AUTO_ID_END = "AUTO_ID_END";
private static final String SAVE_AUTO_ID_START = "SAVE_AUTO_ID_START";

private static final String POLICY_RANGE_NAME = "POLICY_RANGE_NAME";
private static final String POLICY_AUTO_ID_START = "AUTO_ID_START_FOR_POLICY_";
private static final String IGNORE_RANGE_POLICY = "IGNORE_RANGE_POLICIES";

private static final String LABEL_DESCRIPTOR = "LABEL_DESCRIPTOR";
private static final String DEFAULT_LABEL_DESCRIPTOR_CLASS = "org.protege.editor.owl.model.entity.MatchRendererLabelDescriptor";

Expand Down Expand Up @@ -186,6 +190,49 @@ public static void setSaveAutoIDStart(boolean saveAutoIDStart) {
prefs.putBoolean(SAVE_AUTO_ID_START, saveAutoIDStart);
}

public static String getPolicyRangeName() {
Preferences prefs = getPrefs();
return prefs.getString(POLICY_RANGE_NAME, "");
}

public static void setPolicyRangeName(String name) {
Preferences prefs = getPrefs();
prefs.putString(POLICY_RANGE_NAME, name);
}

public static int getPolicyAutoIDStart() {
int autoIdStart = -1;
Preferences prefs = getPrefs();
String policyName = prefs.getString(POLICY_RANGE_NAME, null);
if (policyName != null) {
// Get saved ID specific to current policy
autoIdStart = prefs.getInt(POLICY_AUTO_ID_START + policyName, -1);
}
if (autoIdStart == -1) {
// No policy or no saved ID for policy, use global setting
autoIdStart = prefs.getInt(AUTO_ID_START, 0);
}
return autoIdStart;
}

public static void setPolicyAutoIDStart(int start) {
Preferences prefs = getPrefs();
String policyName = prefs.getString(POLICY_RANGE_NAME, null);
// Save under policy-specific key if a policy is active, otherwise as global setting
String keyName = policyName != null ? POLICY_AUTO_ID_START + policyName : AUTO_ID_START;
prefs.putInt(keyName, start);
}

public static boolean getIgnoreRangePolicy() {
Preferences prefs = getPrefs();
return prefs.getBoolean(IGNORE_RANGE_POLICY, false);
}

public static void setIgnoreRangePolicy(boolean noRangePolicies) {
Preferences prefs = getPrefs();
prefs.putBoolean(IGNORE_RANGE_POLICY, noRangePolicies);
}

public static boolean isFragmentAutoGenerated() {
Preferences prefs = getPrefs();
return prefs.getBoolean(USE_AUTO_ID_FOR_FRAGMENT, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public class IterativeAutoIDGenerator extends AbstractIDGenerator implements Rev


public IterativeAutoIDGenerator() {
id = EntityCreationPreferences.getAutoIDStart();
id = EntityCreationPreferences.getPolicyAutoIDStart();
previousStartId = id;
}


protected long getRawID(Class<? extends OWLEntity> type) throws AutoIDException{
//check if start id prefs have changed meanwhile
if (previousStartId != EntityCreationPreferences.getAutoIDStart()) {
id = EntityCreationPreferences.getAutoIDStart();
if (previousStartId != EntityCreationPreferences.getPolicyAutoIDStart()) {
id = EntityCreationPreferences.getPolicyAutoIDStart();
previousStartId = id;
checkpoints.removeAllElements();
checkpoints.push(id);
Expand All @@ -50,7 +50,7 @@ protected long getRawID(Class<? extends OWLEntity> type) throws AutoIDException{
}
if (EntityCreationPreferences.getSaveAutoIDStart()) {
previousStartId = id + 1;
EntityCreationPreferences.setAutoIDStart((int) (previousStartId));
EntityCreationPreferences.setPolicyAutoIDStart((int) (previousStartId));
}
return id++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public ImmutableList<IdRangesPolicy> getIdRangesPolicies() {
}

public void reload() {
EntityCreationPreferences.setPolicyRangeName(null);
if (EntityCreationPreferences.getIgnoreRangePolicy()) {
return;
}
idRangesPolicies = ImmutableList.of();
OWLOntology activeOntology = modelManager.getActiveOntology();
URI activeOntologyLocation = modelManager.getOntologyPhysicalURI(activeOntology);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private void updateRangeForUser(@Nonnull String userName) {
userIdRange.ifPresent(this::updateNumericRange);
if(!userIdRange.isPresent()) {
logger.info("[IdRanges] Could not find a matching user name");
noRangeForUserNameHandler.handleNoRangeForUserName(userName, policy);
noRangeForUserNameHandler.handleNoRangeForUserName(userName, policy, this::updateNumericRange);
}
}

Expand All @@ -115,6 +115,9 @@ private void updateNumericRange(IdRange rng) {
int end = rng.getUpperBound();
logger.info("[IdRanges] Setting id end to {}", end);
EntityCreationPreferences.setAutoIDEnd(end);
String name = String.format("%s_%d", policy.getIdPolicyFor(), start);
logger.info("[IdRanges] Setting policy name to {}", name);
EntityCreationPreferences.setPolicyRangeName(name);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.protege.editor.owl.model.idrange;

import java.util.function.Consumer;

import javax.annotation.Nonnull;

/**
Expand All @@ -10,5 +12,6 @@
public interface NoRangeForUserNameHandler {

void handleNoRangeForUserName(@Nonnull String userName,
@Nonnull IdRangesPolicy policy);
@Nonnull IdRangesPolicy policy,
@Nonnull Consumer<? super UserIdRange> action);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import javax.swing.*;
import java.awt.*;
import java.util.Arrays;
import java.util.function.Consumer;
import java.util.stream.StreamSupport;

/**
Expand All @@ -24,7 +25,8 @@ public class NoRangeForUserNameHandlerUi implements NoRangeForUserNameHandler {

@Override
public void handleNoRangeForUserName(@Nonnull String userName,
@Nonnull IdRangesPolicy policy) {
@Nonnull IdRangesPolicy policy,
@Nonnull Consumer<? super UserIdRange> action) {
Frame owner = Arrays.stream(Frame.getFrames()).findFirst().orElse(null);
NoRangeFoundForUserNamePanel userNamePanel = new NoRangeFoundForUserNamePanel();
userNamePanel.setUserName(userName);
Expand All @@ -41,11 +43,6 @@ public void handleNoRangeForUserName(@Nonnull String userName,
if(ret != 0) {
return;
}
userNamePanel.getSelectedRange().ifPresent(this::updateIdRange);
}

private void updateIdRange(@Nonnull UserIdRange rng) {
EntityCreationPreferences.setAutoIDStart(rng.getIdRange().getLowerBound());
EntityCreationPreferences.setAutoIDEnd(rng.getIdRange().getUpperBound());
userNamePanel.getSelectedRange().ifPresent(action);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class NewEntitiesPreferencesPanel extends OWLPreferencesPanel implements

// Auto-generated ID panel
private JCheckBox saveIterativeIds;
private JCheckBox ignorePolicy;
private JLabel digitCountLabel;
private JLabel endLabel;
private JLabel prefixLabel;
Expand Down Expand Up @@ -419,6 +420,16 @@ private JPanel createAutoGeneratedIDPanel() {
saveIterativeIds.setEnabled(iterativeButton.isSelected());
centerPanel.add(saveIterativeIds, c);

// Ignore auto ID range policies
c.gridx = 1;
c.gridy = 6;
c.gridwidth = 2;
c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 1.0;
ignorePolicy = new JCheckBox("Ignore ID range policies");
ignorePolicy.setSelected(EntityCreationPreferences.getIgnoreRangePolicy());
centerPanel.add(ignorePolicy, c);

// Dummy label for spacing purposes
c.gridx = 2;
c.gridy = 0;
Expand Down Expand Up @@ -452,6 +463,7 @@ private void enableAutoGeneratedIDPanel(boolean b) {
suffixLabel.setEnabled(b);
uniqueIdButton.setEnabled(b);
ideIdButton.setEnabled(b);
ignorePolicy.setEnabled(b);

enableNumericIterativeOptions((iterativeButton.isSelected()) && (iterativeButton.isEnabled()));
}
Expand Down Expand Up @@ -542,6 +554,7 @@ else if (colonButton.isSelected()){
EntityCreationPreferences.setPrefix(autoIDPrefix.getText());
EntityCreationPreferences.setSuffix(autoIDSuffix.getText());
EntityCreationPreferences.setSaveAutoIDStart(saveIterativeIds.isSelected());
EntityCreationPreferences.setIgnoreRangePolicy(ignorePolicy.isSelected());
}

private void handleActionEndWithID() {
Expand Down