All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.coweb.oe.ContextVector Maven / Gradle / Ivy

There is a newer version: 1.0
Show newest version
package org.coweb.oe;

import java.util.HashMap;
import java.util.Map;
import java.util.Arrays;

public class ContextVector {
	
	private int[] sites;
		
	public ContextVector(Map args) throws OperationEngineException {
		if(args.containsKey("count")) {
			this.sites = new int[((Integer)args.get("count")).intValue()];
		}
		else if(args.containsKey("contextVector")) {
			this.sites = ((ContextVector)args.get("contextVector")).copySites();
		}
		else if(args.containsKey("sites")) {
			int[] s = (int[])args.get("sites");
			this.sites = Arrays.copyOf(s, s.length);
		}
		else if(args.containsKey("state")) {
			this.sites = (int[])args.get("state");
		}
		else {
			throw new OperationEngineException("uninitialized context vector");
		}
	}
	
	/**
     * Converts the contents of this context vector sites array to a string.
     *
     * @returns {String} All integers in the vector (for debug)
     */
	@Override
	public String toString() {
		StringBuffer b = new StringBuffer();
		b.append(Arrays.toString(this.sites));
		
		return b.toString();
	}
	
	/**
     * Serializes this context vector.
     *
     * @returns {Number[]} Array of integer sequence numbers
     */
	public int[] getState() {
		return this.sites;
	}
	
	
	/**
     * Makes an independent copy of this context vector.
	 * @throws OperationEngineException 
     *
     * @returns {ContextVector} Copy of this context vector
     */
	public ContextVector copy() throws OperationEngineException {
		
		HashMap args = new HashMap();
		args.put("contextVector", this);
		
		return new ContextVector(args);
	}
	
	/**
     * Makes an independent copy of the array in this context vector.
     *
     * @return {Number[]} Copy of this context vector's sites array
     */
	public int[] copySites() {
		return Arrays.copyOf(this.sites, this.sites.length);
	}
	
	/**
     * Computes the difference in sequence numbers at each site between this
     * context vector and the one provided.
     *
     * @param {ContextVector} cv Other context vector object
     * @returns {ContextDifference} Represents the difference between this
     * vector and the one passed
     */
	public ContextDifference subtract(ContextVector cv) {
		ContextDifference cd = new ContextDifference();
        for(int i=0; i < this.sites.length; i++) {
            int a = this.getSeqForSite(i);
            int b = cv.getSeqForSite(i);
            if(a-b > 0) {
                cd.addRange(i, b+1, a+1);
            }
        }
        return cd;
	}
	
	/**
     * Finds the oldest sequence number in the difference in sequence numbers
     * for each site between this context and the one provided.
     *
     * @param {ContextVector} cv Other context vector object
     * @returns {ContextDifference} Represents the oldest difference for each
     * site between this vector and the one passed
     */
	public ContextDifference oldestDifference(ContextVector cv) {
		ContextDifference cd = new ContextDifference();
        for(int i=0; i < this.sites.length; i++) {
            int a = this.getSeqForSite(i);
            int b = cv.getSeqForSite(i);
            if(a-b > 0) {
                cd.addSiteSeq(i, b+1);
            }
        }
        return cd;
	}
	
	/**
     * Increases the size of the context vector to the given size. Initializes
     * new entries with zeros.
     *
     * @param {Number} count Desired integer size of the vector
     */
	public void growTo(int count) {
		
		System.out.println("growTo before new count = " + count + " old array = " + this.toString());
		int[] newSites = new int[count];
		System.arraycopy(this.sites, 0, newSites, 0, this.sites.length);
		for(int i=this.sites.length; i
            


© 2015 - 2024 Weber Informatics LLC | Privacy Policy