Ejemplo n.º 1
0
  public Json makeTyped(Object anything) {
    boolean isarray = anything.getClass().isArray();
    Class<?> type = isarray ? anything.getClass().getComponentType() : anything.getClass();
    JsonConverter converter = converterMap.get(type.getName());
    String typeName = shortNameMap.getY(type.getName());
    if (typeName == null) typeName = type.getName();
    if (isarray) {
      Json result = Json.array();
      Object[] A = (Object[]) anything;
      for (Object x : A) {
        if (x == null) result.add(Json.nil());
        else result.add(converter != null ? converter.to(x) : make(x));
      }
      return Json.object().set("javaArrayType", typeName).set("array", result);
    } else if (type.isEnum())
      return Json.object().set("java.lang.Enum", type.getName()).set("value", anything.toString());

    for (Class<?> abstractConv : converterFromAbstractMap.keySet())
      if (abstractConv.isAssignableFrom(type))
        return converterFromAbstractMap.get(abstractConv).to(anything);
    Json value = null;
    if (converter != null) value = converter.to(anything);
    else if (Collection.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type))
      value = beanConverter.to(anything);
    else
      try {
        value = f.make(anything);
      } catch (Throwable t) {
        value = beanConverter.to(anything);
      }
    return Json.object().set("javaType", typeName).set("value", value);
  }
Ejemplo n.º 2
0
 public static HGHandle getHandle(String name) {
   HGHandle result = syntacticPredicates.get(name);
   if (result == null) {
     String s = relationNames.getX(name);
     if (s != null) result = syntacticPredicates.get(s);
   }
   return result;
 }
Ejemplo n.º 3
0
 @SuppressWarnings({"unchecked", "rawtypes"})
 public <T> T value(Json x) {
   if (x == null || x.isNull()) return null;
   else if (x.isPrimitive()) return (T) x.getValue();
   else if (x.isArray())
     // what to do here ... this is some sort of collection??
     return (T) x.getValue();
   else if (x.has("java.lang.Enum")) {
     try {
       return (T)
           Enum.valueOf(
               (Class<Enum>) Class.forName(x.at("java.lang.Enum").asString()),
               x.at("value").asString());
     } catch (Exception t) {
       throw new RuntimeException(t);
     }
   } else if (x.has("javaArrayType")) {
     String fullName = shortNameMap.getX(x.at("javaArrayType").asString());
     if (fullName == null) fullName = x.at("javaArrayType").asString();
     try {
       Class<?> cl = Class.forName(fullName);
       JsonConverter converter = converterMap.get(fullName);
       Object A = Array.newInstance(cl, x.at("array").asJsonList().size());
       for (int i = 0; i < x.at("array").asJsonList().size(); i++) {
         Json j = x.at("array").at(i);
         Array.set(A, i, j.isNull() ? null : converter != null ? converter.from(j) : value(j));
       }
       return (T) A;
     } catch (Exception ex) {
       throw new RuntimeException(ex);
     }
   } else if (x.has("javaType")) {
     String fullName = shortNameMap.getX(x.at("javaType").asString());
     if (fullName == null) fullName = x.at("javaType").asString();
     JsonConverter converter = converterMap.get(fullName);
     if (converter != null)
       return (T) converter.from(converter instanceof BeanJsonConverter ? x : x.at("value"));
     else return (T) beanConverter.from(x); // .at("value"));
   } else return (T) x.getValue();
 }
Ejemplo n.º 4
0
 public static String idFriendlyToRelex(String idFriendlyName) {
   String y = relationNames.getY(idFriendlyName);
   return y != null ? y : idFriendlyName;
 }
Ejemplo n.º 5
0
  static {
    relationNames.add("rx_action", "_to-do");
    relationNames.add("rx_adj_modifier", "_amod");
    relationNames.add("rx_adv_modifier", "_advmod");
    relationNames.add("rx_appositive", "_appo");
    relationNames.add("rx_cause", "_%because");
    relationNames.add("rx_ind_obj", "_iobj");
    relationNames.add("rx_is", "_%inheritance");
    relationNames.add("rx_location", "_%atLocation");
    relationNames.add("rx_noun_mod", "_nn");
    relationNames.add("rx_object", "_obj");
    relationNames.add("rx_prep_object", "_pobj");
    relationNames.add("rx_prep_subject", "_psubj");
    relationNames.add("rx_pred_adjective", "_predadj");
    relationNames.add("rx_possesion", "_poss");
    relationNames.add("rx_property", "_to-be");
    relationNames.add("rx_quant_mult", "_%quantity_mult");
    relationNames.add("rx_quantifier", "_%quantity_mod");
    relationNames.add("rx_quantity", "_%quantity");
    relationNames.add("rx_subject", "_subj");
    relationNames.add("rx_that", "_that");
    relationNames.add("rx_time", "_%atTime");
    relationNames.add("rx_comparison", "_more");
    relationNames.add("rx_gender", "gender");
    relationNames.add("rx_number", "noun_number");
    relationNames.add("rx_pos", "POS");
    relationNames.add("rx_quantification", "DEFINITE-FLAG");
    relationNames.add("rx_query", "QUERY-TYPE");
    relationNames.add("rx_tense", "tense");
    relationNames.add("rx_hyp", "hyp");
    relationNames.add("rx_copula", "_%copula");

    // unary relations on words, coming from the value of rx_quantification etc.
    relationNames.add("rx_word", "WORD"); // a lonely word that wasn't included in a linkage
    relationNames.add("rx_noun", "noun");
    relationNames.add("rx_verb", "verb");
    relationNames.add("rx_adjective", "adj");
    relationNames.add("rx_adverb", "adv");
    relationNames.add("rx_definite", "definite");
    relationNames.add("rx_past", "past");
    relationNames.add("rx_present", "present");
    relationNames.add("rx_future", "future");
    relationNames.add("rx_singular", "singular");
    relationNames.add("rx_plural", "plural");
    relationNames.add("rx_punctuation", "punctuation");
    relationNames.add("rx_det", "det");
    relationNames.add("rx_prep", "prep");
    relationNames.add("rx_progressive", "progressive");
    relationNames.add("rx_present_progressive", "present_progressive");
    relationNames.add("rx_infinitive", "infinitive");
    relationNames.add("rx_particle", "particle");
    relationNames.add("rx_uncountable", "uncountable");
    relationNames.add("rx_countable", "countable");
  }
Ejemplo n.º 6
0
 public static String relexToIdFriendly(String relexName) {
   String x = relationNames.getX(relexName);
   return x != null ? x : relexName;
 }