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

protelis.lang.utils.pt Maven / Gradle / Ivy

There is a newer version: 17.3.45
Show newest version
module protelis:lang:utils

/**
 * @param a bool, first condition
 * @param b bool, second condition
 * @return  bool, true if both the conditions are true
 */
public def and(a, b) {
    a && b
}

/**
 * Line-of-sight detector.
 *
 * @param a T, value
 * @return  bool, true if there is at least a neighbor respect to x, false otherwise
 */
public def canSee(a) {
    anyHood(nbr(a))
}

/**
 * @param x      num, value to be denormalized
 * @param oldmin num, oldmin
 * @param oldmax num, oldmax
 * @param newmin num, newmin
 * @param newmax num, newmax
 * @return       num, denormalized value
 */
public def denormalize(x, oldmin, oldmax, newmin, newmax) {
    x * (newmax - newmin) / (oldmax - oldmin) + (newmin - oldmin)
}

/**
 * @param a num, first field
 * @param b num, second field
 * @return  num, difference between two fields
 */
public def diff(a, b) {
    a - b
}

/**
 * @param a T, first field
 * @param b T, second field
 * @return  bool, true if a is equal to b, false otherwise
 */
public def equal(a, b) {
    a == b
}

/**
 * @param a T, first field
 * @param b T, second field
 * @return  bool, true if a is greater than b, false otherwise
 */
public def greaterThan(a, b) {
    a > b
}

/**
 * @param a T, first field
 * @param b T, second field
 * @return  bool, true if a is greater than or equal to b, false otherwise
 */
public def greaterEqual(a, b) {
    a >= b
}

/**
 * *-hood wrapper.
 *
 * @param local        T, local value
 * @param aggregator   (T, T) -> T, how to reduce information
 * @param default      T, default value in case the field returned by fieldBuilder is empty
 * @param fieldBuilder () -> T, return which field should be reduced
 * @return             T, reduced value
 */
public def hoodW(local, aggregator, default, fieldBuilder) {
    aggregator.apply(local,
        hood(
            (a, b) -> { aggregator.apply(a, b) },
            default,
            fieldBuilder.apply()
        )
    )
}

/**
 * Stateful *-hood wrapper.
 *
 * @param local        T, local value
 * @param aggregator   (T, T) -> T, how to reduce information
 * @param default      T, default value in case the field returned by fieldBuilder is empty
 * @param fieldBuilder (T) -> T, return which field should be reduced
 * @return             T, reduced value
 */
public def hoodWstateful(local, aggregator, default, fieldBuilder) {
    rep (v <- local) {
        aggregator.apply(local,
            hood(
                (a, b) -> { aggregator.apply(a, b) },
                default,
                fieldBuilder.apply(v)
            )
        )
    }
}

/**
 * @param a T, value to be returned
 * @return  T, the same value
 */
public def identity(a) {
    a
}

/**
 * Verify if a device is on the edge of a spatial region.
 *
 * @param condition bool, how to discriminate a region
 * @return          bool, true if the device is on the edge, false otherwise
 */
 public def isEdge(condition) {
     !(allHood(nbr(condition)) || allHood(!nbr(condition)))
 }

/**
 * @param a T, first field
 * @param b T, second field
 * @return  bool, true if a is less than b, false otherwise
 */
public def lessThan(a, b) {
    a < b
}

/**
 * @param a T, first field
 * @param b T, second field
 * @return  bool, true if a is less than or equal to b, false otherwise
 */
public def lessEqual(a, b) {
    a <= b
}

/**
 * @param a T, first field
 * @param b T, second field
 * @return  T, maximum between the two fields
 */
public def max(a, b) {
    mux (a > b) {
        a
    } else {
        b
    }
}

/**
 * @param a num, first field
 * @param b num, second field
 * @return  num, mean of the two fields
 */
public def mean(local, field) {
    (local + field) / 2
}

/**
 * @param a num, first field
 * @param b num, second field
 * @return  num, minimum between the two fields
 */
public def min(a, b) {
    mux (a < b) {
        a
    } else {
        b
    }
}

/**
 * @return node's UID
 */
public def getUID() {
    self.getDeviceUID().toString()
}

/**
 * Norm of a numeric tuple.
 *
 * @param tuple [num], tuple
 * @param p     num, pow
 * @return      num, norm of a numeric tuple
 */
public def norm(tuple, p) {
    pow(
        tuple
            .map(self, (a) -> { pow(a, p) })
            .reduce(self, 0, (a, b) -> { a + b }),
        1/p
    )
}

/**
 * @param a bool, first condition
 * @return  bool, negated condition
 */
public def not(a) {
    !a
}

/**
 * @param a T, first field
 * @param b T, second field
 * @return  bool, true if a is not equal to b, false otherwise
 */
public def notEqual(a, b) {
    a != b
}

/**
 * @param a bool, first condition
 * @param b bool, second condition
 * @return  bool, true if one of the conditions is true
 */
public def or(a, b) {
    a || b
}

/**
 * @param x   num, value
 * @param min num, lower bound
 * @param max num, upper bound
 * @return    num, scale x with respect to the range
 */
public def range(x, min, max) {
    x * (max - min) + min
}

/**
 * @param a num, summand
 * @param b num, summand
 * @return  num, sum of two fields
 */
public def sum(a, b) {
    a + b
}

/**
 * @param a [T], tuple
 * @param b [T], tuple
 * @return  [T], concatenation of two tuples
 */
public def union(a, b) {
    a.union(b)
}

/**
 * @return num, 0
 */
public def zero() {
    0
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy