Package sizzle.types

Examples of sizzle.types.SizzleFloat


    // this maps the Java types in protocol buffers into Sizzle types
    this.protomap = new HashMap<Class<?>, SizzleType>();
    this.protomap.put(int.class, new SizzleInt());
    this.protomap.put(long.class, new SizzleInt());
    this.protomap.put(float.class, new SizzleFloat());
    this.protomap.put(double.class, new SizzleFloat());
    this.protomap.put(boolean.class, new SizzleBool());
    this.protomap.put(byte[].class, new SizzleBytes());
    this.protomap.put(String.class, new SizzleString());

    // this maps scalar Sizzle scalar types names to their classes
    // TODO: do this via reflection
    this.idmap = new HashMap<String, SizzleType>();
    this.idmap.put("any", new SizzleAny());
    this.idmap.put("none", null);
    this.idmap.put("bool", new SizzleBool());
    this.idmap.put("int", new SizzleInt());
    this.idmap.put("float", new SizzleFloat());
    this.idmap.put("time", new SizzleTime());
    this.idmap.put("fingerprint", new SizzleFingerprint());
    this.idmap.put("string", new SizzleString());
    this.idmap.put("bytes", new SizzleBytes());

    // does the same for arrays
    // for (final String key : new HashSet<String>(this.idmap.keySet())) {
    // final SizzleType value = this.idmap.get(key);
    // if (value instanceof SizzleScalar)
    // this.idmap.put("array of " + key, new SizzleArray((SizzleScalar)
    // value));
    // }

    // variables with a global scope
    this.globals = new HashMap<String, SizzleType>();
    // set the type of the input
    this.globals.put("input", input);
    this.globals.put("true", new SizzleBool());
    this.globals.put("false", new SizzleBool());
    this.globals.put("PI", new SizzleFloat());
    this.globals.put("Inf", new SizzleFloat());
    this.globals.put("inf", new SizzleFloat());
    this.globals.put("NaN", new SizzleFloat());
    this.globals.put("nan", new SizzleFloat());

    // variables with a local scope
    this.locals = new HashMap<String, SizzleType>();
    this.aggregators = new HashMap<String, Class<?>>();
    this.functions = new FunctionTrie();

    // these generic functions require more finagling than can currently be
    // (easily) done with a static method, so they are handled with macros

    this.setFunction("def", new SizzleFunction(new SizzleBool(), new SizzleType[] { new SizzleAny() }, "${0} != null"));
    this.setFunction("len", new SizzleFunction(new SizzleInt(), new SizzleType[] { new SizzleArray(new SizzleScalar()) }, "${0}.length"));
    this.setFunction("len", new SizzleFunction(new SizzleInt(), new SizzleType[] { new SizzleString() }, "${0}.length()"));
    this.setFunction("len", new SizzleFunction(new SizzleInt(), new SizzleType[] { new SizzleBytes() }, "${0}.length"));
    this.setFunction("len", new SizzleFunction(new SizzleInt(), new SizzleType[] { new SizzleMap(new SizzleScalar(), new SizzleScalar()) },
        "${0}.keySet().size()"));
    this.setFunction("haskey", new SizzleFunction(new SizzleBool(), new SizzleType[] { new SizzleMap(new SizzleScalar(), new SizzleScalar()),
        new SizzleScalar() }, "${0}.containsKey(${1})"));
    this.setFunction("keys", new SizzleFunction(new SizzleArray(new SizzleScalar()), new SizzleType[] { new SizzleMap(new SizzleScalar(),
        new SizzleScalar()) }, "${0}.keySet().toArray()"));
    this.setFunction("lookup", new SizzleFunction(new SizzleScalar(), new SizzleType[] { new SizzleMap(new SizzleScalar(), new SizzleScalar()),
        new SizzleScalar(), new SizzleScalar() }, "(${0}.containsKey(${1}) ? ${0}.get(${1}) : ${2})"));

    this.setFunction("regex", new SizzleFunction(new SizzleString(), new SizzleType[] { new SizzleName(new SizzleScalar()), new SizzleInt() },
        "sizzle.functions.SizzleSpecialIntrinsics.regex(\"${0}\", ${1})"));
    this.setFunction("regex", new SizzleFunction(new SizzleString(), new SizzleType[] { new SizzleName(new SizzleScalar()) },
        "sizzle.functions.SizzleSpecialIntrinsics.regex(\"${0}\")"));
    // these fingerprints are identity functions
    this.setFunction("fingerprintof", new SizzleFunction(new SizzleFingerprint(), new SizzleScalar[] { new SizzleInt() }));
    this.setFunction("fingerprintof", new SizzleFunction(new SizzleFingerprint(), new SizzleScalar[] { new SizzleTime() }));

    /* expose all the casting constructors to Sawzall */

    // string to bool
    this.setFunction("bool",
        new SizzleFunction("sizzle.functions.SizzleCasts.stringToBoolean", new SizzleBool(), new SizzleScalar[] { new SizzleString() }));

    // bool to int
    this.setFunction("int", new SizzleFunction("sizzle.functions.SizzleCasts.booleanToLong", new SizzleInt(), new SizzleScalar[] { new SizzleBool() }));
    // float to int
    this.setFunction("int", new SizzleFunction(new SizzleInt(), new SizzleScalar[] { new SizzleFloat() }, "(long)${0}"));
    // time to int
    this.setFunction("int", new SizzleFunction(new SizzleInt(), new SizzleScalar[] { new SizzleTime() }));
    // fingerprint to int
    this.setFunction("int", new SizzleFunction(new SizzleInt(), new SizzleScalar[] { new SizzleFingerprint() }));
    // string to int
    this.setFunction("int", new SizzleFunction("java.lang.Long.decode", new SizzleInt(), new SizzleScalar[] { new SizzleString() }));
    // string to int with param base
    this.setFunction("int", new SizzleFunction(new SizzleInt(), new SizzleScalar[] { new SizzleString(), new SizzleInt() },
        "java.lang.Long.parseLong(${0}, (int)${1})"));
    // bytes to int with param encoding format
    this.setFunction("int", new SizzleFunction("sizzle.functions.SizzleCasts.bytesToLong", new SizzleInt(), new SizzleScalar[] { new SizzleBytes(),
        new SizzleString() }));

    // int to float
    this.setFunction("float", new SizzleFunction(new SizzleFloat(), new SizzleScalar[] { new SizzleInt() }, "(double)${0}"));
    // string to float
    this.setFunction("float", new SizzleFunction("java.lang.Double.parseDouble", new SizzleFloat(), new SizzleScalar[] { new SizzleString() }));

    // int to time
    this.setFunction("time", new SizzleFunction(new SizzleTime(), new SizzleScalar[] { new SizzleInt() }));
    // string to time
    this.setFunction("time", new SizzleFunction("sizzle.functions.SizzleCasts.stringToTime", new SizzleTime(), new SizzleScalar[] { new SizzleString() }));
    // string to time
    this.setFunction("time", new SizzleFunction("sizzle.functions.SizzleCasts.stringToTime", new SizzleTime(), new SizzleScalar[] { new SizzleString(),
        new SizzleString() }));

    // int to fingerprint
    this.setFunction("fingerprint", new SizzleFunction(new SizzleFingerprint(), new SizzleScalar[] { new SizzleInt() }));
    // string to fingerprint
    this.setFunction("fingerprint", new SizzleFunction("java.lang.Long.parseLong", new SizzleInt(), new SizzleScalar[] { new SizzleString() }));
    // string to fingerprint with param base
    this.setFunction("fingerprint", new SizzleFunction("java.lang.Long.parseLong", new SizzleInt(), new SizzleScalar[] { new SizzleString(),
        new SizzleInt() }));
    // bytes to fingerprint
    this.setFunction("fingerprint", new SizzleFunction("sizzle.functions.SizzleCasts.bytesToFingerprint", new SizzleFingerprint(),
        new SizzleScalar[] { new SizzleBytes() }));

    // bool to string
    this.setFunction("string", new SizzleFunction("java.lang.Boolean.toString", new SizzleString(), new SizzleScalar[] { new SizzleBool() }));
    // int to string
    this.setFunction("string", new SizzleFunction("java.lang.Long.toString", new SizzleString(), new SizzleScalar[] { new SizzleInt() }));
    // int to string with parameter base
    this.setFunction("string", new SizzleFunction("sizzle.functions.SizzleCasts.longToString", new SizzleString(), new SizzleScalar[] { new SizzleInt(),
        new SizzleInt() }));
    // float to string
    this.setFunction("string", new SizzleFunction("java.lang.Double.toString", new SizzleString(), new SizzleScalar[] { new SizzleFloat() }));
    // time to string
    this.setFunction("string", new SizzleFunction("sizzle.functions.SizzleCasts.timeToString", new SizzleString(), new SizzleScalar[] { new SizzleTime() }));
    // fingerprint to string
    this.setFunction("string", new SizzleFunction("java.lang.Long.toHexString", new SizzleString(), new SizzleScalar[] { new SizzleFingerprint() }));
    // bytes to string
    this.setFunction("string", new SizzleFunction("new java.lang.String", new SizzleString(), new SizzleScalar[] { new SizzleBytes() }));
    // bytes to string
    this.setFunction("string", new SizzleFunction("new java.lang.String", new SizzleString(), new SizzleScalar[] { new SizzleBytes(), new SizzleString() }));

    // int to bytes with param encoding format
    this.setFunction("bytes", new SizzleFunction("sizzle.functions.SizzleCasts.longToBytes", new SizzleInt(), new SizzleScalar[] { new SizzleInt(),
        new SizzleString() }));
    // fingerprint to bytes
    this.setFunction("bytes", new SizzleFunction("sizzle.functions.SizzleCasts.fingerprintToBytes", new SizzleBytes(),
        new SizzleScalar[] { new SizzleFingerprint() }));
    // string to bytes
    this.setFunction("bytes",
        new SizzleFunction("sizzle.functions.SizzleCasts.stringToBytes", new SizzleBytes(), new SizzleScalar[] { new SizzleString() }));

    /* expose the java.lang.Math class to Sawzall */

    this.setFunction("highbit", new SizzleFunction("java.lang.Long.highestOneBit", new SizzleInt(), new SizzleScalar[] { new SizzleInt() }));

    // abs just needs to be overloaded
    this.setFunction("abs", new SizzleFunction("java.lang.Math.abs", new SizzleFloat(), new SizzleScalar[] { new SizzleInt() }));
    this.setFunction("abs", new SizzleFunction("java.lang.Math.abs", new SizzleFloat(), new SizzleScalar[] { new SizzleFloat() }));

    // abs is also named fabs in Sawzall
    this.setFunction("fabs", new SizzleFunction("java.lang.Math.abs", new SizzleFloat(), new SizzleScalar[] { new SizzleFloat() }));

    // log is named ln in Sawzall
    this.setFunction("ln", new SizzleFunction("java.lang.Math.log", new SizzleFloat(), new SizzleScalar[] { new SizzleFloat() }));

    // expose the rest of the unary functions
    for (final String s : Arrays.asList("log10", "exp", "sqrt", "sin", "cos", "tan", "asin", "acos", "atan", "cosh", "sinh", "tanh", "ceil", "floor",
        "round"))
      this.setFunction(s, new SizzleFunction("java.lang.Math." + s, new SizzleFloat(), new SizzleScalar[] { new SizzleFloat() }));

    // expose the binary functions
    for (final String s : Arrays.asList("pow", "atan2"))
      this.setFunction(s, new SizzleFunction("java.lang.Math." + s, new SizzleFloat(), new SizzleScalar[] { new SizzleFloat(), new SizzleFloat() }));

    for (final String s : Arrays.asList("max", "min"))
      for (final SizzleScalar t : Arrays.asList(new SizzleInt(), new SizzleFloat()))
        this.setFunction(s, new SizzleFunction("java.lang.Math." + s, new SizzleFloat(), new SizzleScalar[] { t, t }));

    this.setFunction("max", new SizzleFunction(new SizzleFloat(), new SizzleScalar[] { new SizzleTime(), new SizzleTime() }, "(${0} > ${1} ? ${0} : ${1})"));
    this.setFunction("min", new SizzleFunction(new SizzleFloat(), new SizzleScalar[] { new SizzleTime(), new SizzleTime() }, "(${0} < ${1} ? ${0} : ${1})"));

    this.setFunction("max", new SizzleFunction(new SizzleFloat(), new SizzleScalar[] { new SizzleString(), new SizzleString() },
        "(${0}.compareTo(${1}) > 0 ? ${0} : ${1})"));
    this.setFunction("min", new SizzleFunction(new SizzleFloat(), new SizzleScalar[] { new SizzleString(), new SizzleString() },
        "(${0}.compareTo(${1}) < 0 ? ${0} : ${1})"));

    // expose whatever is left, assuming we are not aiming for strict
    // compatibility
    if (!this.strictCompatibility) {
      // random takes no argument

      // these three have capitals in the name
      this.setFunction("ieeeremainder", new SizzleFunction("java.lang.Math.IEEEremainder", new SizzleFloat(), new SizzleScalar[] { new SizzleFloat(),
          new SizzleFloat() }));
      this.setFunction("todegrees", new SizzleFunction("java.lang.Math.toDegrees", new SizzleFloat(), new SizzleScalar[] { new SizzleFloat() }));
      this.setFunction("toradians", new SizzleFunction("java.lang.Math.toRadians", new SizzleFloat(), new SizzleScalar[] { new SizzleFloat() }));

      // the unaries
      for (final String s : Arrays.asList("cbrt", "expm1", "log1p", "rint", "signum", "ulp"))
        this.setFunction(s, new SizzleFunction("java.lang.Math." + s, new SizzleFloat(), new SizzleScalar[] { new SizzleFloat() }));

      // and binaries
      this.setFunction("hypot",
          new SizzleFunction("java.lang.Math.hypot", new SizzleFloat(), new SizzleScalar[] { new SizzleFloat(), new SizzleFloat() }));
    }

    // add in the default tables
    // FIXME: support format strings and files
    this.set("stdout", new SizzleTable(new SizzleString()));
View Full Code Here


  @Test
  public void testFunctionTrieMultiParameter() {
    final FunctionTrie functionTrie = new FunctionTrie();

    final SizzleFunction sizzleFunction = new SizzleFunction(new SizzleBool(), new SizzleType[] { new SizzleInt(), new SizzleFloat(), new SizzleString() });

    functionTrie.addFunction("function", sizzleFunction);
    functionTrie.addFunction("function", new SizzleFunction(new SizzleBool(), new SizzleType[] { new SizzleString(), new SizzleFloat(), new SizzleInt() }));

    Assert.assertEquals("did not return correct function", sizzleFunction,
        functionTrie.getFunction("function", new SizzleType[] { new SizzleInt(), new SizzleFloat(), new SizzleString() }));
  }
View Full Code Here

    final SymbolTable st = new SymbolTable();

    SizzleParser.ReInit(new StringReader(source));
    TestTypeCheckingVisitor.typeChecker.visit(SizzleParser.Start(), st);

    final List<SizzleType> members = new ArrayList<SizzleType>(Arrays.asList(new SizzleInt(), new SizzleFloat()));

    Assert.assertEquals("out is not an unindexed, unweighted table of int and float", new SizzleTable(new SizzleTuple(members)), st.get("out"));

    Assert.assertEquals("line is not a string", new SizzleString(), st.get("line"));
    Assert.assertEquals("tuple is not an array of strings", new SizzleArray(new SizzleString()), st.get("tuple"));
View Full Code Here

    SizzleParser.ReInit(new StringReader(source));
    TestTypeCheckingVisitor.typeChecker.visit(SizzleParser.Start(), st);

    Assert.assertEquals("my_bool is not an alias for bool", new SizzleName(new SizzleBool()), st.getType("my_bool"));
    final ArrayList<SizzleType> members = new ArrayList<SizzleType>(Arrays.asList(new SizzleFloat(), new SizzleFloat()));
    Assert.assertEquals("Coordinates is not is not an alias for a tuple of x: float, y: float", new SizzleName(new SizzleTuple(members)),
        st.getType("Coordinates"));
    Assert.assertEquals("CityMap is not an alias for a mapping from string to tuple of x: float, y: float", new SizzleName(new SizzleMap(
        new SizzleString(), new SizzleName(new SizzleTuple(members)))), st.getType("CityMap"));
View Full Code Here

  }

  /** {@inheritDoc} */
  @Override
  public SizzleType visit(final FloatingPointLiteral n, final SymbolTable argu) {
    return new SizzleFloat();
  }
View Full Code Here

    SizzleParser.ReInit(new StringReader(source));
    TestTypeCheckingVisitor.typeChecker.visit(SizzleParser.Start(), st);

    Assert.assertEquals("count is not an unweighted, unindexed table of ints", new SizzleTable(new SizzleInt()), st.get("count"));
    Assert.assertEquals("total is not an unweighted, unindexed stable of floats", new SizzleTable(new SizzleFloat()), st.get("total"));
    Assert.assertEquals("sum_of_squares is not an unweighted, unindexed table of floats", new SizzleTable(new SizzleFloat()), st.get("sum_of_squares"));
    Assert.assertEquals("x is not a float", new SizzleFloat(), st.get("x"));
  }
View Full Code Here

    final SymbolTable st = new SymbolTable();

    SizzleParser.ReInit(new StringReader(source));
    TestTypeCheckingVisitor.typeChecker.visit(SizzleParser.Start(), st);

    final List<SizzleType> members = new ArrayList<SizzleType>(Arrays.asList(new SizzleInt(), new SizzleFloat(), new SizzleFloat()));

    Assert.assertEquals("s is not an unweighted, unindexed table of tuple of int, float and float", new SizzleTable(new SizzleTuple(members)), st.get("s"));

    Assert.assertEquals("x is not a float", new SizzleFloat(), st.get("x"));
  }
View Full Code Here

    SizzleParser.ReInit(new StringReader(source));
    TestTypeCheckingVisitor.typeChecker.visit(SizzleParser.Start(), st);

    Assert.assertEquals("max_pagerank_url is not table of strings indexed by string weighted by int",
        new SizzleTable(new SizzleString(), Arrays.asList(new SizzleScalar[] { new SizzleString() }), new SizzleFloat()), st.get("max_pagerank_url"));
    final List<SizzleType> members = new ArrayList<SizzleType>(Arrays.asList(new SizzleString(), new SizzleInt()));
    Assert.assertEquals("doc is not a Document", new SizzleTuple(members), st.get("doc"));
  }
View Full Code Here

    TestTypeCheckingVisitor.typeChecker.visit(SizzleParser.Start(), st);

    Assert.assertEquals("queries_per_degree is not an unweighted table of ints indexed by ints",
        new SizzleTable(new SizzleInt(), Arrays.asList(new SizzleScalar[] { new SizzleInt(), new SizzleInt() }), null), st.get("queries_per_degree"));

    final List<SizzleType> lmembers = new ArrayList<SizzleType>(Arrays.asList(new SizzleFloat(), new SizzleFloat()));

    Assert.assertEquals("loc is not a Location", new SizzleTuple(lmembers), st.get("loc"));

    final List<SizzleType> qlpmembers = new ArrayList<SizzleType>(Arrays.asList(new SizzleString(), new SizzleInt()));
View Full Code Here

    // assertEquals("t is not an time", new SizzleTime(), st.get("t"));
    // assertEquals("m is not an int", new SizzleInt(), st.get("m"));
    Assert.assertEquals("RESOLUTION is not an int", new SizzleInt(), st.get("RESOLUTION"));

    final List<SizzleType> lmembers = new ArrayList<SizzleType>(Arrays.asList(new SizzleFloat(), new SizzleFloat()));

    Assert.assertEquals("loc is not a Location", new SizzleTuple(lmembers), st.get("loc"));

    final List<SizzleType> qlpmembers = new ArrayList<SizzleType>(Arrays.asList(new SizzleString(), new SizzleInt()));
View Full Code Here

TOP

Related Classes of sizzle.types.SizzleFloat

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.