Export to GitHub

synclib - BeanbagHaskellJavaBridge.wiki


Introduction

When you compile a Beanbag program, the program generates the synchronizer as an executable file. To integrate the synchronizer into applications, the applications need to generate input for the executable file and parse output, which is not convenient.

The Java bridge library provide a Java interface for invoking the synchronizer. To use the library, the programmer just create some Java objects to represent the values and updates, and invoke `Sync.synchronize' to get the synchronized result.

Prerequisite

The following library must be in your classpath: * javaBridge.jar: The main file of the Java bridge library. Source included. * antlr-runtime-3.1.3.jar: The ANTLR runtime

Walkthrough

Suppose we are going to invoke the following Beanbag program in Java. def main(a, b, c) := a = b

We first save this program as "basic.beanbag" and then compile it using "`compile basic.beanbag basic.exe'" to get a synchronizer in Java.

To invoke the synchronizer, we need first create a SyncWrapper instance for the executable file. import ac.jp.u_tokyo.Beanbag.*; ... // pass the executable file path to the constructor SyncWrapper sync = new SyncWrapper("basic.exe");

Suppose the input values are {a=1, b=1}, the input updates are {b->!2}. We may consider the values as a big dictionary mapping variable names to integers and the updates as a dictionary update mapping variable names to updates on these variables. The dictionary value can be created using HashMap provided by JDK while the dictionary update can be created using DictModFactory provided by the library. Map<String, Object> values = new HashMap<String, Object>(); values.put("a", 1); values.put("b", 1); DictModFactory<String, Object> factory = new DictModFactory<String, Object>(); factory.addPut("b", new PrimMod<Object>(2)); DictMod<String, Object> updates = factory.create();

After we have created the input values and the input updates, we can pass them to sync.synchronize and get the result. DictMod<String, Object> result = sync.synchronize(values, updates); System.out.println(result.toString()); //should print "{a->!2, b->!2}"

The Update Classes

The classes representing updates are the same as those classes in Beanbag 0.2. All implement the interface Mod, which is listed below. ``` public interface Mod {

/**
 * apply the update to a value
 * @param the value to be updated
 * @return the updated value
 */
T apply(T value);

/**
 * Test if two updates conflict. The expression a.isConflict(b) is the same as a.merge(b).equals(b.merge(a)).
 * @param The update to be tested against this.
 * @return Whether two updates conflict.
 */
boolean isConflict(Mod<T> m);

/**
 * Merge the two updates as if this is applied first while latter is applied later.
 * @param the object to be merged to this object as a latter update
 * @return the merged result
 */
Mod<T> merge(Mod<T> latter);

/**
 * Subtract another object from this one.
 */
Mod<T> subtract(Mod<T> subtrahend);

/**
 * get the sub update that actually change {@code value}.
 */
Mod<T> getEffectiveMod(T value);

/**
 * get the update that undo this update after this update is applied to {@code value}.
 */
public abstract Mod<T> getInverse(T value);

/**
 * Modify an object in place and return the object if the object is mutable, otherwise is same as apply(T).  
 * @param the object to be modified
 * @return the modified object
 */
public abstract T inPlaceApply(T value);

} ```

The PrimMod class represents an update on primitive values while DictMod represents an update on dictionaries. The following examples show how to create instances of these classes.

Update: !23

Java Code: import ac.jp.u_tokyo.SyncLib.*; new PrimMod<Integer>(23);

Update: {1->!23, 2->!null}

Java Code: import ac.jp.u_tokyo.SyncLib.*; import ac.jp.u_tokyo.SyncLib.dictionaries.*; DictModFactory f = new DictModFactory(); f.addPut(1, new PrimMod<Integer>(23)); f.addRemove(2); f.create();

Update: {"a"->{1->!23, 2->!null}}

Java Code: //..following the above code DictModFactory g = new DictModFactory(); g.addPut("a", f.create()); g.create();