예제 #1
0
def handleKey(row, key, mode, hnd):
    if mode == MODE_ARRAY:
        for k in input_format[key]:
            if k in row and row[k] != '':
                hnd(row[k])
        return
    ignore_missing = mode == MODE_DEFAULT
    if key in input_format:
        k = input_format[key]
        if util.is_array(k):
            found = False
            for key in k:
                if key in row and row[key] != '':
                    hnd(row[key])
                    found = True
                    break
            if not found and ignore_missing:
                hnd('')
        else:
            if k in row and row[k] != '':
                hnd(row[k])
            elif ignore_missing:
                hnd('')
    elif ignore_missing:
        hnd('')
예제 #2
0
def handleKey(row, key, mode, hnd):
    if mode == MODE_ARRAY:
        for k in input_format[key]:
            if k in row and row[k] != '':
                hnd(row[k])
        return
    ignore_missing = mode == MODE_DEFAULT
    if key in input_format:
        k = input_format[key]
        if util.is_array(k):
            found = False
            for key in k:
                if key in row and row[key] != '':
                    hnd(row[key])
                    found = True
                    break
            if not found and ignore_missing:
                hnd('')
        else:
            if k in row and row[k] != '':
                hnd(row[k])
            elif ignore_missing:
                hnd('')
    elif ignore_missing:
        hnd('')
예제 #3
0
 def __contains__(self, x):
     if util.is_map(x):
         return all((self.__contains__(v) for v in x.values()))
     if util.is_array(x):
         return all((self.__contains__(v) for v in x))
     if util.is_string(x):
         k = util.safe_decode(x)
     else:
         k = unicode(x)
     return k in self._elements
예제 #4
0
def gen_log_sk(sk_dir, pgr):
  buf = cStringIO.StringIO()
  buf.write("package log;\n")
  buf.write(_const)

  buf.write("""
    // distinct hash values for runtime objects
    int obj_cnt = 0;
    int nonce () {
      return obj_cnt++;
    }
  """)

  # factory of Object
  buf.write("""
    // factory of Object
    Object alloc(int ty) {{
      Object {0} = new Object(hash=nonce(), __cid=ty);
      return {0};
    }}
  """.format(C.SK.self))

  global _ty;
  _clss = []
  for ty in _ty.keys():
    if util.is_collection(ty): continue
    if util.is_array(ty): continue
    cls = class_lookup(ty)
    if not cls: continue # to avoid None definition
    # inner class may appear twice: w/ and w/o outer class name
    if cls not in _clss: _clss.append(cls)

  buf.write("\n// distinct class IDs\n")
  for cls in _clss:
    buf.write("int {cls!r} () {{ return {cls.id}; }}\n".format(**locals()))

  buf.write("\n// distinct method IDs\n")
  for cls in pgr.classes:
    mtds = collect_decls(cls, "mtds")
    if not mtds: continue

    for mtd in mtds:
      mname = sanitize_mname(unicode(repr(mtd)))
      buf.write("""
        int {mname}_ent () {{ return  {mtd.id}; }}
        int {mname}_ext () {{ return -{mtd.id}; }}
      """.format(**locals()))

  with open(os.path.join(sk_dir, "log.sk"), 'w') as f:
    f.write(buf.getvalue())
    logging.info("encoding " + f.name)
  buf.close()
예제 #5
0
 def __delitem__(self, key):
     if util.is_map(key):
         return self.__delitem__(key.values())
     if util.is_array(key):
         for k in key:
             self.__delitem__(k)
         return
     if util.is_string(key):
         k = util.safe_decode(key)
     else:
         k = unicode(key)
     if k in self._elements:
         del self._elements[k]
     return
예제 #6
0
 def __setitem__(self, key, value):
     if util.is_map(key):
         return self.__setitem__(key.values(), value)
     if util.is_array(key):
         for k in key:
             self.__setitem__(k, value)
         return
     if value is None:
         self.__delitem__(key)
         return
     if util.is_string(key):
         k = util.safe_decode(key)
     else:
         k = unicode(key)
     self._elements[k] = util.rec_decode(value)
     return
예제 #7
0
def generate_dto_tostring(camel_case_dto_name, func):
    tostring_fields = ""
    for t in zip(func['types'], func['args']):

        field_name = util.underscore_to_camelcase(t[1])
        # for retval don't generate dto field in Reply
        if util.is_retval_field(field_name):
            continue

        # handle array types
        if util.is_array(util.jni_2_java_type_mapping[t[0]]):
            tostring_fields += tostring_array_field_template.substitute(field_name=field_name)
        else:
            tostring_fields += tostring_field_template.substitute(field_name=field_name)

    return tostring_template.substitute(cls_name=camel_case_dto_name,
                                        fields_tostring=tostring_fields[:-8])
예제 #8
0
def generate_dto_equals(camel_case_dto_name, func):
    equals_fields = ""
    for t in zip(func['types'], func['args']):
        field_name = util.underscore_to_camelcase(t[1])
        # for retval don't generate dto field in Reply
        if util.is_retval_field(field_name):
            continue

        # handle array types
        if util.is_array(util.jni_2_java_type_mapping[t[0]]):
            equals_fields += equals_array_field_template.substitute(field_name=field_name)
        else:
            equals_fields += equals_field_template.substitute(field_name=field_name)

    if equals_fields != "":
        equals_fields = equals_other_template.substitute(cls_name=camel_case_dto_name) + equals_fields

    return equals_template.substitute(comparisons=equals_fields)
예제 #9
0
 def __getitem__(self, key):
     if util.is_map(key):
         return util.cons_map(
             ((k, self.__getitem__(v)) for k, v in key.items()),
             key.__class__
         )
     if isinstance(key, collections.Set):
         return {k: self.__getitem__(k) for k in util.rec_decode(key)}
     if util.is_array(key):
         return util.cons_array(
             (self.__getitem__(k) for k in key),
             key.__class__
         )
     if util.is_string(key):
         k = util.safe_decode(key)
     else:
         k = unicode(key)
     return self._get(k)
예제 #10
0
def generate_dto_hash(func):
    hash_fields = ""

    # Special handling for hashCode in case just a single array field is present. Cannot use Objects.equals since the
    # array is mistaken for a varargs parameter. Instead use Arrays.hashCode in such case.
    if len(func['args']) == 1:
        single_type = func['types'][0]
        single_type_name = func['args'][0]
        if util.is_array(util.jni_2_java_type_mapping[single_type]):
            return hash_single_array_type_template.substitute(fields=util.underscore_to_camelcase(single_type_name))

    for t in zip(func['types'], func['args']):
        field_name = util.underscore_to_camelcase(t[1])
        # for retval don't generate dto field in Reply
        if util.is_retval_field(field_name):
            continue

        hash_fields += field_name + ", "

    return hash_template.substitute(fields=hash_fields[:-2])
예제 #11
0
def gen_type_sk(sk_dir, bases):
  buf = cStringIO.StringIO()
  buf.write("package type;\n")
  buf.write(_const)

  cols, decls = util.partition(lambda c: util.is_collection(c.name), bases)
  decls = filter(lambda c: not util.is_array(c.name), decls)
  itfs, clss = util.partition(op.attrgetter("is_itf"), decls)
  logging.debug("# interface(s): {}".format(len(itfs)))
  logging.debug("# class(es): {}".format(len(clss)))
  # convert interfaces first, then usual classes
  buf.write('\n'.join(util.ffilter(map(to_struct, itfs))))
  buf.write('\n'.join(util.ffilter(map(to_struct, clss))))

  # convert collections at last
  logging.debug("# collection(s): {}".format(len(cols)))
  buf.write('\n'.join(map(col_to_struct, cols)))

  # argument number of methods
  arg_num = map(lambda mtd: len(mtd.params), methods())
  buf.write("""
    #define _{0} {{ {1} }}
    int {0}(int id) {{
      return _{0}[id];
    }}
  """.format(C.typ.argNum, ", ".join(map(str, arg_num))))

  # argument types of methods
  def get_args_typ(mtd):
    def get_arg_typ(param): return str(class_lookup(param[0]).id)
    return '{' + ", ".join(map(get_arg_typ, mtd.params)) + '}'
  args_typ = map(get_args_typ, methods())
  buf.write("""
    #define _{0} {{ {1} }}
    int {0}(int id, int idx) {{
      return _{0}[id][idx];
    }}
  """.format(C.typ.argType, ", ".join(args_typ)))

  # return type of methods
  def get_ret_typ(mtd):
    cls = class_lookup(mtd.typ)
    if cls: return cls.id
    else: return -1
  ret_typ = map(get_ret_typ, methods())
  buf.write("""
    #define _{0} {{ {1} }}
    int {0}(int id) {{
      return _{0}[id];
    }}
  """.format(C.typ.retType, ", ".join(map(str, ret_typ))))

  # belonging class of methods
  belongs = map(lambda mtd: mtd.clazz.id, methods())
  buf.write("""
    #define _{0} {{ {1} }}
    int {0}(int id) {{
      return _{0}[id];
    }}
  """.format(C.typ.belongsTo, ", ".join(map(str, belongs))))

  subcls = \
      map(lambda cls_i: '{' + ", ".join( \
          map(lambda cls_j: str(cls_i <= cls_j).lower(), classes()) \
      ) + '}', classes())
  buf.write("""
    #define _{0} {{ {1} }}
    bit {0}(int i, int j) {{
      return _{0}[i][j];
    }}
  """.format(C.typ.subcls, ", ".join(subcls)))

  ## sub type relations
  #subcls = []
  #for cls_i in classes():
  #  row = []
  #  for cls_j in classes():
  #    row.append(int(cls_i <= cls_j))
  #  subcls.append(row)

  ## sub type relations in yale format 
  #_, IA, JA = util.yale_format(subcls)
  #li, lj = len(IA), len(JA)
  #si = ", ".join(map(str, IA))
  #sj = ", ".join(map(str, JA))
  #buf.write("""
  #  #define _iA {{ {si} }}
  #  #define _jA {{ {sj} }}
  #  int iA(int i) {{
  #    return _iA[i];
  #  }}
  #  int jA(int j) {{
  #    return _jA[j];
  #  }}
  #  bit subcls(int i, int j) {{
  #    int col_i = iA(i);
  #    int col_j = iA(i+1);
  #    for (int col = col_i; col < col_j; col++) {{
  #      if (j == jA(col)) return true;
  #    }}
  #    return false;
  #  }}
  #""".format(**locals()))

  with open(os.path.join(sk_dir, "type.sk"), 'w') as f:
    f.write(buf.getvalue())
    logging.info("encoding " + f.name)
  buf.close()
예제 #12
0
def gen_type_sk(sk_dir, bases):
  buf = cStringIO.StringIO()
  buf.write("package type;\n")
  buf.write(_const)

  buf.write(trans_lib())
  buf.write('\n')

  cols, decls = util.partition(lambda c: util.is_collection(c.name), bases)
  decls = filter(lambda c: not util.is_array(c.name), decls)
  itfs, clss = util.partition(op.attrgetter("is_itf"), decls)
  logging.debug("# interface(s): {}".format(len(itfs)))
  logging.debug("# class(es): {}".format(len(clss)))
  # convert interfaces first, then usual classes
  buf.write('\n'.join(util.ffilter(map(to_struct, itfs))))
  buf.write('\n'.join(util.ffilter(map(to_struct, clss))))

  # convert collections at last
  logging.debug("# collection(s): {}".format(len(cols)))
  buf.write('\n'.join(map(col_to_struct, cols)))

  # argument number of methods
  arg_num = map(lambda mtd: len(mtd.params), methods())
  buf.write("""
    #define _{0} {{ {1} }}
    int {0}(int id) {{
      return _{0}[id];
    }}
  """.format(C.typ.argNum, ", ".join(map(str, arg_num))))

  # argument types of methods
  def get_args_typ(mtd):
    def get_arg_typ(param): return str(class_lookup(param[0]).id)
    return '{' + ", ".join(map(get_arg_typ, mtd.params)) + '}'
  args_typ = map(get_args_typ, methods())
  buf.write("""
    #define _{0} {{ {1} }}
    int {0}(int id, int idx) {{
      return _{0}[id][idx];
    }}
  """.format(C.typ.argType, ", ".join(args_typ)))

  # return type of methods
  def get_ret_typ(mtd):
    cls = class_lookup(mtd.typ)
    if cls: return cls.id
    else: return -1
  ret_typ = map(get_ret_typ, methods())
  buf.write("""
    #define _{0} {{ {1} }}
    int {0}(int id) {{
      return _{0}[id];
    }}
  """.format(C.typ.retType, ", ".join(map(str, ret_typ))))

  # belonging class of methods
  belongs = map(lambda mtd: mtd.clazz.id, methods())
  buf.write("""
    #define _{0} {{ {1} }}
    int {0}(int id) {{
      return _{0}[id];
    }}
  """.format(C.typ.belongsTo, ", ".join(map(str, belongs))))

  subcls = \
      map(lambda cls_i: '{' + ", ".join( \
          map(lambda cls_j: str(cls_i <= cls_j).lower(), classes()) \
      ) + '}', classes())
  buf.write("""
    #define _{0} {{ {1} }}
    bit {0}(int i, int j) {{
      return _{0}[i][j];
    }}
  """.format(C.typ.subcls, ", ".join(subcls)))

  ## sub type relations
  #subcls = []
  #for cls_i in classes():
  #  row = []
  #  for cls_j in classes():
  #    row.append(int(cls_i <= cls_j))
  #  subcls.append(row)

  ## sub type relations in yale format 
  #_, IA, JA = util.yale_format(subcls)
  #li, lj = len(IA), len(JA)
  #si = ", ".join(map(str, IA))
  #sj = ", ".join(map(str, JA))
  #buf.write("""
  #  #define _iA {{ {si} }}
  #  #define _jA {{ {sj} }}
  #  int iA(int i) {{
  #    return _iA[i];
  #  }}
  #  int jA(int j) {{
  #    return _jA[j];
  #  }}
  #  bit subcls(int i, int j) {{
  #    int col_i = iA(i);
  #    int col_j = iA(i+1);
  #    for (int col = col_i; col < col_j; col++) {{
  #      if (j == jA(col)) return true;
  #    }}
  #    return false;
  #  }}
  #""".format(**locals()))

  with open(os.path.join(sk_dir, "type.sk"), 'w') as f:
    f.write(buf.getvalue())
    logging.info("encoding " + f.name)
  buf.close()
예제 #13
0
def gen_log_sk(sk_dir, tmpl):
  buf = cStringIO.StringIO()
  buf.write("package log;\n")
  buf.write(_const)
  global max_objs
  buf.write("int O = {}; // # of objects\n".format(max_objs + 1))

  buf.write("""
    int log_cnt = 0;
    int[P][N] ev;
    int[O] obj;

    // to enforce the length of logs
    int get_log_cnt() {
      return log_cnt;
    }

    // after writing logs, reset the cursor in order to check logs in order
    void reset_log_cnt() {
      log_cnt = 0;
    }

    // to clean up the logs totally
    void clear_log() {
      reset_log_cnt();
      ev = {};
      obj = {};
    }

    // to write the log from samples
    void write_log (int[P] params) {
      ev[log_cnt++] = params;
    }

    // to check whether control-flow conforms to the samples
    @Native("{ std::cout << \\\"log::check_log::\\\" << params[0] << std::endl; }")
    void check_log (int[P] params) {
      assert params[0] == ev[log_cnt][0]; // check mid
      for (int i = 1; i < P; i++) {
        if (ev[log_cnt][i] != 0) {
          if (obj[ev[log_cnt][i]] == 0) { // not set yet
            obj[ev[log_cnt][i]] = params[i];
          }
          else { // o.w. check obj eq.
            assert obj[ev[log_cnt][i]] == params[i];
          }
        }
      }
      log_cnt++; // advance
    }

    // distinct hash values for runtime objects
    int obj_cnt = 0;
    int nonce () {
      return obj_cnt++;
    }
  """)

  global _inits
  reg_codes = []
  for ty in _inits:
    cls = class_lookup(ty)
    if not cls: continue

    buf.write("""
      int obj_{0}_cnt = 0;
      {1}[O] obj_{0};

      // to register runtime instances of {0}
      void register_{0} ({1} {2}) {{
        if (obj_{0}_cnt < O) {{
          obj_{0}[obj_{0}_cnt++] = {2};
        }}
      }}

      // to access to a certain instance of {0}
      {1} retrieve_{0} (int idx) {{
        if (0 <= idx && idx < obj_{0}_cnt) {{
          return obj_{0}[idx];
        }}
        else {{
          return null;
        }}
      }}
    """.format(ty, trans_ty(ty), ty.lower()))

    reg_code = "if (ty == {0}) register_{1}@log({2});".format(cls.id, repr(cls), C.SK.self)
    reg_codes.append(reg_code)

  # factory of Object
  buf.write("""
    // factory of Object
    Object alloc(int ty) {{
      Object {0} = new Object(hash=nonce(), __cid=ty);
      {1}
      return {0};
    }}
  """.format(C.SK.self, "\nelse ".join(reg_codes)))

  global _ty;
  _clss = []
  for ty in _ty.keys():
    if util.is_collection(ty): continue
    if util.is_array(ty): continue
    cls = class_lookup(ty)
    if not cls: continue # to avoid None definition
    # inner class may appear twice: w/ and w/o outer class name
    if cls not in _clss: _clss.append(cls)

  buf.write("\n// distinct class IDs\n")
  for cls in _clss:
    buf.write("int {cls!r} () {{ return {cls.id}; }}\n".format(**locals()))

  buf.write("\n// distinct method IDs\n")
  for cls in tmpl.classes:
    mtds = collect_decls(cls, "mtds")
    if not mtds: continue

    for mtd in mtds:
      mname = sanitize_mname(unicode(repr(mtd)))
      buf.write("""
        int {mname}_ent () {{ return  {mtd.id}; }}
        int {mname}_ext () {{ return -{mtd.id}; }}
      """.format(**locals()))

  with open(os.path.join(sk_dir, "log.sk"), 'w') as f:
    f.write(buf.getvalue())
    logging.info("encoding " + f.name)
  buf.close()