Skip to content

Commit

Permalink
fix: JSReactionEncoder
Browse files Browse the repository at this point in the history
  • Loading branch information
tpoisseau committed Oct 16, 2024
1 parent aed7bdc commit 2477a7c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 95 deletions.
24 changes: 13 additions & 11 deletions lib/canvas_editor/init/canvas_editor_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ function initCanvasEditorElement(CanvasEditor, Molecule, ReactionEncoder) {
*/
setReaction(reaction) {
this.fragment = reaction.isFragment();
this.idcode = ReactionEncoder.encode(
reaction,
true,
ReactionEncoder.INCLUDE_MAPPING |
this.idcode = ReactionEncoder.encode(reaction, {
keepAbsoluteCoordinates: true,
mode:
ReactionEncoder.INCLUDE_MAPPING |
ReactionEncoder.INCLUDE_COORDS |
ReactionEncoder.RETAIN_REACTANT_AND_PRODUCT_ORDER,
);
});

this.#editor.setReaction(reaction);
}
Expand Down Expand Up @@ -182,7 +182,9 @@ function initCanvasEditorElement(CanvasEditor, Molecule, ReactionEncoder) {
* @this {CanvasEditorElement}
*/
#initReaction() {
const reaction = ReactionEncoder.decode(this.idcode, true);
const reaction = ReactionEncoder.decode(this.idcode, {
ensureCoordinates: true,
});
reaction.setFragment(this.fragment);

this.#editor.setReaction(reaction);
Expand Down Expand Up @@ -226,13 +228,13 @@ function initCanvasEditorElement(CanvasEditor, Molecule, ReactionEncoder) {
}
case CanvasEditorElement.MODE.REACTION: {
const reaction = this.getReaction();
this.idcode = ReactionEncoder.encode(
reaction,
true,
ReactionEncoder.INCLUDE_MAPPING |
this.idcode = ReactionEncoder.encode(reaction, {
keepAbsoluteCoordinates: true,
mode:
ReactionEncoder.INCLUDE_MAPPING |
ReactionEncoder.INCLUDE_COORDS |
ReactionEncoder.RETAIN_REACTANT_AND_PRODUCT_ORDER,
);
});
this.fragment = reaction.isFragment();
break;
}
Expand Down
92 changes: 27 additions & 65 deletions src/com/actelion/research/gwt/core/JSReactionEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,39 @@

@JsType(name = "ReactionEncoder")
public class JSReactionEncoder {
public static final int INCLUDE_MAPPING = ReactionEncoder.INCLUDE_MAPPING;
public static final int INCLUDE_COORDS = ReactionEncoder.INCLUDE_COORDS;
public static final int INCLUDE_DRAWING_OBJECTS = ReactionEncoder.INCLUDE_DRAWING_OBJECTS;
public static final int INCLUDE_CATALYSTS = ReactionEncoder.INCLUDE_CATALYSTS;
public static final int RETAIN_REACTANT_AND_PRODUCT_ORDER = ReactionEncoder.RETAIN_REACTANT_AND_PRODUCT_ORDER;

public static final int INCLUDE_ALL = ReactionEncoder.INCLUDE_ALL;
public static final int INCLUDE_RXN_CODE_ONLY = ReactionEncoder.INCLUDE_RXN_CODE_ONLY;
public static final int INCLUDE_DEFAULT = ReactionEncoder.INCLUDE_DEFAULT;

public static final String MOLECULE_DELIMITER = ReactionEncoder.MOLECULE_DELIMITER_STRING;
public static final String OBJECT_DELIMITER = ReactionEncoder.OBJECT_DELIMITER_STRING;
public static final String PRODUCT_IDENTIFIER = String.valueOf(ReactionEncoder.PRODUCT_IDENTIFIER);
public static final String CATALYST_DELIMITER = String.valueOf(ReactionEncoder.CATALYST_DELIMITER);

public static native String encode(JSReaction reaction, JavaScriptObject options) /*-{
options ??= {keepAbsoluteCoordinates: false, mode: ReactionEncoder.COMBINED_MODES.INCLUDE_DEFAULT};
options.keepAbsoluteCoordinates ??= false;
if (!options) options = {keepAbsoluteCoordinates: false, mode: ReactionEncoder.COMBINED_MODES.INCLUDE_DEFAULT};
options.keepAbsoluteCoordinates = Boolean(options.keepAbsoluteCoordinates);
if ('sortByIDCode' in options) {
options.sortByIDCode ??= false;
options.sortByIDCode = Boolean(options.sortByIDCode);
return @com.actelion.research.gwt.core.generic.JSReactionEncoder::encode(
Lcom.actelion.research.gwt.minimal.JSReaction;ZZ
)(reaction, options.keepAbsoluteCoordinates, options.sortByIDCode)
return @com.actelion.research.gwt.core.JSReactionEncoder::encodeSort(Lcom/actelion/research/gwt/minimal/JSReaction;ZZ)(reaction, options.keepAbsoluteCoordinates, options.sortByIDCode)
}
options.mode ??= ReactionEncoder.COMBINED_MODES.INCLUDE_DEFAULT;
if (typeof options.mode !== 'number') options.mode = @com.actelion.research.gwt.core.JSReactionEncoder::INCLUDE_DEFAULT;
options.mode = Math.trunc(options.mode);
return @com.actelion.research.gwt.core.generic.JSReactionEncoder::encode(
Lcom.actelion.research.gwt.minimal.JSReaction;ZI
)(reaction, options.keepAbsoluteCoordinates, options.mode);
return @com.actelion.research.gwt.core.JSReactionEncoder::encodeMode(Lcom/actelion/research/gwt/minimal/JSReaction;ZI)(reaction, options.keepAbsoluteCoordinates, options.mode);
}-*/;

private static String encode(JSReaction reaction, boolean keepAbsoluteCoordinates, boolean sortByIDCode) {
private static String encodeSort(JSReaction reaction, boolean keepAbsoluteCoordinates, boolean sortByIDCode) {
return String.join(
ReactionEncoder.OBJECT_DELIMITER_STRING,
OBJECT_DELIMITER,
ReactionEncoder.encode(
reaction.getReaction(),
keepAbsoluteCoordinates,
Expand All @@ -41,7 +49,7 @@ private static String encode(JSReaction reaction, boolean keepAbsoluteCoordinate
);
}

private static String encode(JSReaction reaction, boolean keepAbsoluteCoordinates, int mode) {
private static String encodeMode(JSReaction reaction, boolean keepAbsoluteCoordinates, int mode) {
return ReactionEncoder.encode(
reaction.getReaction(),
keepAbsoluteCoordinates,
Expand All @@ -50,76 +58,30 @@ private static String encode(JSReaction reaction, boolean keepAbsoluteCoordinate
}

public static native JSReaction decode(String reaction, JavaScriptObject options) /*-{
options ??= {mode: ReactionEncoder.COMBINED_MODES.INCLUDE_DEFAULT};
if (!options) options = {mode: ReactionEncoder.COMBINED_MODES.INCLUDE_DEFAULT};
if ('ensureCoordinates' in options) {
options.ensureCoordinates ??= false;
options.ensureCoordinates = Boolean(options.ensureCoordinates);
return decode(String s, boolean ensureCoordinates);
return @com.actelion.research.gwt.core.JSReactionEncoder::decodeCoords(Ljava/lang/String;Z)(reaction, options.ensureCoordinates);
}
options.mode ??= ReactionEncoder.COMBINED_MODES.INCLUDE_DEFAULT;
if (typeof options.mode !== 'number') options.mode = @com.actelion.research.gwt.core.JSReactionEncoder::INCLUDE_DEFAULT;
options.mode = Math.trunc(options.mode);
return decode(String s, int includeOptions, Reaction rxn)
return @com.actelion.research.gwt.core.JSReactionEncoder::decodeMode(Ljava/lang/String;I)(reaction, options.mode);
}-*/;

private static JSReaction decode(String strReaction, boolean ensureCoordinates) {
private static JSReaction decodeCoords(String strReaction, boolean ensureCoordinates) {
Reaction reaction = ReactionEncoder.decode(strReaction, ensureCoordinates, null);
if (reaction == null) return null;

return new JSReaction(reaction);
}

private static JSReaction decode(String strReaction, int mode) {
private static JSReaction decodeMode(String strReaction, int mode) {
Reaction reaction = ReactionEncoder.decode(strReaction, mode, null);
if (reaction == null) return null;

return new JSReaction(reaction);
}

// private static class Modes extends JavaScriptObject {
// public final int MAPPING = 0b0001;
// public final int COORDS = 0b0010;
// public final int DRAWING_OBJECTS = 0b0100;
// public final int CATALYSTS = 0b1000;
//
// protected Modes() {}
//
// public native void init() /*-{
// Object.assign(this, {
// MAPPING: 0b0001,
// COORDS: 0b0010,
// DRAWING_OBJECTS: 0b0100,
// CATALYSTS: 0b1000,
// });
// }-*/;
// }
//
// private static class CombinedModes extends JavaScriptObject {
// public final int ALL = 0b1111;
// public final int RXN_CODE_ONLY = 0b0000;
// public final int DEFAULT = 0b0011; // MAPPING | COORDS
//
// protected CombinedModes() {}
//
// public native void init() /*-{
// Object.assign(this, {
// ALL: 0b1111,
// RXN_CODE_ONLY: 0b0000,
// DEFAULT: 0b0011,
// });
// }-*/;
// }

// static final Modes MODES;
// static final CombinedModes COMBINED_MODES;
//
// static {
// MODES = (Modes) JavaScriptObject.createObject().cast();
// MODES.init();
//
// COMBINED_MODES = (CombinedModes) JavaScriptObject.createObject().cast();
// COMBINED_MODES.init();
// }

}
44 changes: 25 additions & 19 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3111,11 +3111,11 @@ export interface ReactionEncoderEncodeOptionsSort
/**
* Bound to Java ReactionEncoder `String encode(Reaction reaction, boolean keepAbsoluteCoordinates, int mode)`
*/
export interface ReactionEncoderEncodeOptionsMode {
export interface ReactionEncoderEncodeOptionsMode
extends ReactionEncoderEncodeOptionsBase {
/**
* @default ReactionEncoder.COMBINED_MODES.INCLUDE_DEFAULT
* @see ReactionEncoder.MODES
* @see ReactionEncoder.COMBINED_MODES
* @default ReactionEncoder.INCLUDE_DEFAULT
* @see ReactionEncoder.INCLUDE_DEFAULT
*/
mode?: number;
}
Expand All @@ -3133,9 +3133,8 @@ export interface ReactionEncoderDecodeOptionsCoordinates {

export interface ReactionEncoderDecodeOptionsMode {
/**
* @default ReactionEncoder.COMBINED_MODES.INCLUDE_DEFAULT
* @see ReactionEncoder.MODES
* @see ReactionEncoder.COMBINED_MODES
* @default ReactionEncoder.INCLUDE_DEFAULT
* @see ReactionEncoder.INCLUDE_DEFAULT
*/
mode?: number;
}
Expand All @@ -3146,18 +3145,25 @@ export type ReactionEncoderDecodeOptions =

// eslint-disable-next-line @typescript-eslint/no-extraneous-class
export declare class ReactionEncoder {
static readonly MODES = {
MAPPING: 0b0001,
COORDS: 0b0010,
DRAWING_OBJECTS: 0b0100,
CATALYSTS: 0b1000,
} as const;

static readonly COMBINED_MODES = {
ALL: 0b1111,
RXN_CODE_ONLY: 0b0000,
DEFAULT: ReactionEncoder.MODES.MAPPING | ReactionEncoder.MODES.COORDS,
} as const;
static readonly INCLUDE_MAPPING = 0b00001;
static readonly INCLUDE_COORDS = 0b00010;
static readonly INCLUDE_DRAWING_OBJECTS = 0b00100;
static readonly INCLUDE_CATALYSTS = 0b01000;
static readonly RETAIN_REACTANT_AND_PRODUCT_ORDER = 0b10000;

static readonly INCLUDE_ALL =
ReactionEncoder.INCLUDE_MAPPING |
ReactionEncoder.INCLUDE_COORDS |
ReactionEncoder.INCLUDE_DRAWING_OBJECTS |
ReactionEncoder.INCLUDE_CATALYSTS;
static readonly INCLUDE_RXN_CODE_ONLY = 0b00000;
static readonly INCLUDE_DEFAULT =
ReactionEncoder.INCLUDE_MAPPING | ReactionEncoder.INCLUDE_COORDS;

static readonly MOLECULE_DELIMITER: string;
static readonly OBJECT_DELIMITER: string;
static readonly PRODUCT_IDENTIFIER: string;
static readonly CATALYST_DELIMITER: string;

/**
* @param reaction
Expand Down

0 comments on commit 2477a7c

Please sign in to comment.