public Map<String, List<Type>> getTypeMapForDocumentType(
     String typeName, DocumentModel currentDoc) {
   Type type = getType(typeName);
   if (type != null) {
     Map<String, List<Type>> docTypesMap = new HashMap<String, List<Type>>();
     Map<String, SubType> allowedSubTypes = type.getAllowedSubTypes();
     allowedSubTypes = filterSubTypesFromConfiguration(allowedSubTypes, currentDoc);
     for (Map.Entry<String, SubType> entry : allowedSubTypes.entrySet()) {
       if (canCreate(entry.getValue())) {
         Type subType = getType(entry.getKey());
         if (subType != null) {
           String key = subType.getCategory();
           if (key == null) {
             key = DEFAULT_CATEGORY;
           }
           if (!docTypesMap.containsKey(key)) {
             docTypesMap.put(key, new ArrayList<Type>());
           }
           docTypesMap.get(key).add(subType);
         }
       }
     }
     return docTypesMap;
   }
   return new HashMap<String, List<Type>>();
 }
Example #2
0
File: Test.java Project: TOSPIO/GF
  public static void main(String[] args) throws IOException {
    PGF gr = null;
    try {
      gr =
          PGF.readPGF(
              "/home/krasimir/www.grammaticalframework.org/examples/phrasebook/Phrasebook.pgf");
    } catch (FileNotFoundException e) {
      e.printStackTrace();
      return;
    } catch (PGFError e) {
      e.printStackTrace();
      return;
    }

    Type typ = gr.getFunctionType("Bulgarian");
    System.out.println(typ.getCategory());
    System.out.println(gr.getAbstractName());
    for (Map.Entry<String, Concr> entry : gr.getLanguages().entrySet()) {
      System.out.println(
          entry.getKey() + " " + entry.getValue() + " " + entry.getValue().getName());
    }

    Concr eng = gr.getLanguages().get("SimpleEng");
    try {
      for (ExprProb ep : eng.parse(gr.getStartCat(), "persons who work with Malmö")) {
        System.out.println("[" + ep.getProb() + "] " + ep.getExpr());
      }
    } catch (ParseError e) {
      System.out.println("Parsing failed at token \"" + e.getToken() + "\"");
    }
  }
Example #3
0
  @Override
  public Type getCommonSupertype(Type t) {

    if (t == this) return this;
    switch (t.getCategory()) {
      case BASIC:
        return extendedBaseType.getCommonSupertype(t);
      case EXTENSION:
        Extension e = (Extension) t;
        if (e.extendedBaseType != extendedBaseType)
          return e.extendedBaseType.getCommonSupertype(extendedBaseType);
        return getCommonInternal(t, ((Extension) t).hierarchyLevel);
    }
    throw new Error("Common supertype error");
  }
Example #4
0
  public Extension(TypeExtension def, Type extendedType) {
    super(def.getName());
    isDistinct = def.getDisjoint();
    switch (extendedType.getCategory()) {
      case BASIC:
        extendedBaseType = (BasicType) extendedType;
        hierarchyLevel = 0;
        break;
      case EXTENSION:
        Extension e = ((Extension) extendedType);
        extendedBaseType = e.extendedBaseType;
        hierarchyLevel = e.hierarchyLevel + 1;
        if (isDistinct)
          throw new CompilationError(
              def,
              "The base type of a disjoint type extension must be a native type, but it is a "
                  + extendedType.getDescription());
        break;
      default:
        throw new CompilationError(
            def,
            "The type "
                + extendedType.getFullName()
                + " cannot be extended. Only basic types and extensions can be extended.");
    }
    definition = def;
    this.extendedType = extendedType;
    this.isKey = def.getIsKey();

    if (extendedType.isKeyType()) {
      throw new CompilationError(def, "Cannot create an extension based on a key type");
    }
    if (isKey) {
      if (extendedBaseType != BasicType.INT && extendedBaseType != BasicType.STRING) {
        throw new CompilationError(
            def, "Only int and string are allowed to be used as base types for key types.");
      }
    }
  }