Skip to content

Instantly share code, notes, and snippets.

@johardi
Last active July 26, 2016 01:27
Show Gist options
  • Save johardi/50c25ffa39c84e235aaf7947997f1914 to your computer and use it in GitHub Desktop.
Save johardi/50c25ffa39c84e235aaf7947997f1914 to your computer and use it in GitHub Desktop.
public class SessionRecorder extends OWLEditorKitHook {
public static String SRID = "org.protege.editor.owl.client.SessionRecorder";
private Logger logger = LoggerFactory.getLogger(HistoryManager.class);
private OWLOntologyManager manager;
private Map<OWLOntologyID, Stack<List<OWLOntologyChange>>> stash = new HashMap<>();
private boolean enabled = true;
private OWLOntologyChangeListener ontologyChangeListener = changes -> {
if (enabled) {
OWLOntologyID ontologyId = getEditorKit().getOWLModelManager().getActiveOntology().getOntologyID();
stash.get(ontologyId).add(changes);
}
};
public static SessionRecorder getInstance(OWLEditorKit editorKit) {
return (SessionRecorder) editorKit.get(SRID);
}
@Override
public void initialise() throws Exception {
getEditorKit().getOWLModelManager().addOntologyChangeListener(ontologyChangeListener);
}
/**
* Call this method to clear the change history.
*/
public void clear() {
OWLOntologyID ontologyId = getEditorKit().getOWLModelManager().getActiveOntology().getOntologyID();
stash.get(ontologyId).clear();
}
/**
* Call this method to stop listening to changes that are being applied to the current ontology.
* For example:
* (1) Open remote project:
* ...
* Client client = clientSession.getActiveClient();
* ServerDocument serverDocument = client.openProject(pid);
* sessionRecoder.stopRecording(); // STOP
* VersionedOWLOntology vont = ((LocalHttpClient) client).buildVersionedOntology(serverDocument, owlManager, pid);
* sessionRecoder.startRecording(); //START
* clientSession.setActiveProject(pid, vont);
*
* (2) Update project
* ...
* List<OWLOntologyChange> remoteChanges = ChangeHistoryUtils.getOntologyChanges(remoteChangeHistory, ontology);
* List<OWLOntologyChange> conflictChanges = getConflicts(localChanges, remoteChanges);
* if (conflictChanges.isEmpty()) {
* sessionRecorder.stopRecording(); // STOP
* performUpdate(remoteChanges);
* incomingChanges = remoteChanges;
* vont.update(remoteChangeHistory);
* sessionRecoder.startRecording(); // START
* }
*/
public void stopRecording() {
enabled = false;
}
/**
* Call this method to start listening to changes that are being applied to the current ontology
*/
public void startRecording() {
enabled = true;
}
public List<OWLOntologyChange> getUncommittedChanges() {
OWLOntologyID ontologyId = getEditorKit().getOWLModelManager().getActiveOntology().getOntologyID();
Stack<List<OWLOntologyChange>> stashStack = stash.get(ontologyId);
// Flatten the stack
List<OWLOntologyChange> toReturn = new ArrayList<>();
for (List<OWLOntologyChange> changes : stashStack) {
toReturn.addAll(changes);
}
return ChangeListMinimizer.collapse(toReturn); // a new util code from Matthew to collapse changes (not yet public)
}
@Override
public void dispose() throws Exception {
getEditorKit().getOWLModelManager().removeOntologyChangeListener(ontologyChangeListener);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment