void processGdl(Gdl gdl, GdlConstant parent) {
   if (gdl instanceof GdlRelation) {
     GdlRelation relation = (GdlRelation) gdl;
     String name = relation.getName().toString();
     if (!name.equals("base")) {
       for (Gdl gdl2 : relation.getBody()) processGdl(gdl2, relation.getName());
     }
   } else if (gdl instanceof GdlRule) {
     GdlRule rule = (GdlRule) gdl;
     for (Gdl gdl2 : rule.getBody()) processGdl(gdl2, null);
   } else if (gdl instanceof GdlConstant) {
     universe.add((GdlConstant) gdl);
   } else if (gdl instanceof GdlFunction) {
     GdlFunction func = (GdlFunction) gdl;
     for (Gdl gdl2 : func.getBody()) processGdl(gdl2, func.getName());
   } else if (gdl instanceof GdlDistinct) {
     GdlDistinct distinct = (GdlDistinct) gdl;
     processGdl(distinct.getArg1(), null);
     processGdl(distinct.getArg2(), null);
   } else if (gdl instanceof GdlNot) {
     GdlNot not = (GdlNot) gdl;
     processGdl(not.getBody(), null);
   } else if (gdl instanceof GdlOr) {
     GdlOr or = (GdlOr) gdl;
     for (int i = 0; i < or.arity(); i++) processGdl(or.get(i), null);
   } else if (gdl instanceof GdlProposition) {
     // IGNORE
   } else if (gdl instanceof GdlVariable) {
     // IGNORE
   }
 }
示例#2
0
 private List<Gdl> expandFirstOr(Gdl gdl) {
   List<Gdl> rval;
   List<Gdl> expandedChild;
   if (gdl instanceof GdlDistinct) {
     // Can safely be ignored, won't contain 'or'
     rval = new ArrayList<>();
     rval.add(gdl);
     return rval;
   } else if (gdl instanceof GdlNot) {
     GdlNot not = (GdlNot) gdl;
     expandedChild = expandFirstOr(not.getBody());
     rval = new ArrayList<>();
     for (Gdl g : expandedChild) {
       if (!(g instanceof GdlLiteral)) throw new RuntimeException("Not must have literal child.");
       GdlLiteral lit = (GdlLiteral) g;
       rval.add(GdlPool.getNot(lit));
     }
     return rval;
   } else if (gdl instanceof GdlOr) {
     GdlOr or = (GdlOr) gdl;
     rval = new ArrayList<>();
     for (int i = 0; i < or.arity(); i++) {
       rval.add(or.get(i));
     }
     return rval;
   } else if (gdl instanceof GdlProposition) {
     // Can safely be ignored, won't contain 'or'
     rval = new ArrayList<>();
     rval.add(gdl);
     return rval;
   } else if (gdl instanceof GdlRelation) {
     // Can safely be ignored, won't contain 'or'
     rval = new ArrayList<>();
     rval.add(gdl);
     return rval;
   } else if (gdl instanceof GdlRule) {
     throw new RuntimeException(
         "This should be used to remove 'or's from the body of a rule, and rules can't be nested");
   } else if (gdl instanceof GdlConstant) {
     // Can safely be ignored, won't contain 'or'
     rval = new ArrayList<>();
     rval.add(gdl);
     return rval;
   } else if (gdl instanceof GdlFunction) {
     // Can safely be ignored, won't contain 'or'
     rval = new ArrayList<>();
     rval.add(gdl);
     return rval;
   } else if (gdl instanceof GdlVariable) {
     // Can safely be ignored, won't contain 'or'
     rval = new ArrayList<>();
     rval.add(gdl);
     return rval;
   } else {
     throw new RuntimeException(
         "Uh oh, gdl hierarchy must have been extended without updating this code.");
   }
 }
示例#3
0
  private static GdlOr substituteOr(GdlOr or, Substitution theta) {
    if (or.isGround()) {
      return or;
    }
    List<GdlLiteral> disjuncts = new ArrayList<>();
    for (int i = 0; i < or.arity(); i++) {
      disjuncts.add(substituteLiteral(or.get(i), theta));
    }

    return GdlPool.getOr(disjuncts);
  }
示例#4
0
  private Gdl getInstantiationAux(Gdl gdl, Map<GdlVariable, GdlConstant> varInstantiation) {
    if (gdl instanceof GdlRelation) {
      GdlRelation relation = (GdlRelation) gdl;

      List<GdlTerm> body = new ArrayList<>();
      for (int i = 0; i < relation.arity(); i++) {
        body.add((GdlTerm) getInstantiationAux(relation.get(i), varInstantiation));
      }
      return GdlPool.getRelation(relation.getName(), body);
    } else if (gdl instanceof GdlRule) {
      GdlRule rule = (GdlRule) gdl;
      GdlSentence head = (GdlSentence) getInstantiationAux(rule.getHead(), varInstantiation);

      List<GdlLiteral> body = new ArrayList<>();
      for (int i = 0; i < rule.arity(); i++) {
        body.add((GdlLiteral) getInstantiationAux(rule.get(i), varInstantiation));
      }
      return GdlPool.getRule(head, body);
    } else if (gdl instanceof GdlDistinct) {
      GdlDistinct distinct = (GdlDistinct) gdl;
      GdlTerm arg1 = (GdlTerm) getInstantiationAux(distinct.getArg1(), varInstantiation);
      GdlTerm arg2 = (GdlTerm) getInstantiationAux(distinct.getArg2(), varInstantiation);
      return GdlPool.getDistinct(arg1, arg2);
    } else if (gdl instanceof GdlNot) {
      GdlNot not = (GdlNot) gdl;
      GdlLiteral body = (GdlLiteral) getInstantiationAux(not.getBody(), varInstantiation);
      return GdlPool.getNot(body);
    } else if (gdl instanceof GdlOr) {
      GdlOr or = (GdlOr) gdl;
      List<GdlLiteral> body = new ArrayList<>();
      for (int i = 0; i < or.arity(); i++) {
        body.add((GdlLiteral) getInstantiationAux(or.get(i), varInstantiation));
      }
      return GdlPool.getOr(body);
    } else if (gdl instanceof GdlProposition) {
      return gdl;
    } else if (gdl instanceof GdlConstant) {
      return gdl;
    } else if (gdl instanceof GdlFunction) {
      GdlFunction func = (GdlFunction) gdl;
      List<GdlTerm> body = new ArrayList<>();
      for (int i = 0; i < func.arity(); i++) {
        body.add((GdlTerm) getInstantiationAux(func.get(i), varInstantiation));
      }
      return GdlPool.getFunction(func.getName(), body);
    } else if (gdl instanceof GdlVariable) {
      GdlVariable variable = (GdlVariable) gdl;
      return varInstantiation.get(variable);
    } else
      throw new RuntimeException(
          "Someone went and extended the GDL hierarchy without updating this code.");
  }
示例#5
0
 private static void addSentencesInLiteral(GdlLiteral literal, Collection<GdlSentence> sentences) {
   if (literal instanceof GdlSentence) {
     sentences.add((GdlSentence) literal);
   } else if (literal instanceof GdlNot) {
     GdlNot not = (GdlNot) literal;
     addSentencesInLiteral(not.getBody(), sentences);
   } else if (literal instanceof GdlOr) {
     GdlOr or = (GdlOr) literal;
     for (int i = 0; i < or.arity(); i++) addSentencesInLiteral(or.get(i), sentences);
   } else if (!(literal instanceof GdlDistinct)) {
     throw new RuntimeException(
         "Unexpected GdlLiteral type encountered: " + literal.getClass().getSimpleName());
   }
 }
  private Set<Domain> findAllInstancesOf(GdlVariable var, Gdl gdl, Location loc) {
    if (!domains.containsKey(loc)) domains.put(loc, new Domain(loc));

    Set<Domain> rval = new HashSet<Domain>();

    if (gdl instanceof GdlRelation) {
      GdlRelation relation = (GdlRelation) gdl;
      for (int i = 0; i < relation.arity(); i++) {
        Location parent = new Location();
        parent.name = relation.getName();
        parent.idx = i;
        rval.addAll(findAllInstancesOf(var, relation.get(i), parent));
      }
    } else if (gdl instanceof GdlDistinct) {
      //          GdlDistinct distinct = (GdlDistinct)gdl;
      // Negative context, ignore it for now
    } else if (gdl instanceof GdlNot) {
      //          GdlNot not = (GdlNot)gdl;
      // Negative context, ignore it for now
    } else if (gdl instanceof GdlOr) // TODO: check that this is right, I think it may not be
    {
      GdlOr or = (GdlOr) gdl;
      for (int i = 0; i < or.arity(); i++) rval.addAll(findAllInstancesOf(var, or.get(i), null));
    } else if (gdl instanceof GdlProposition) {
      //          GdlProposition prop = (GdlProposition)gdl;
      // I think these can safely be ignored, they have no body
    } else if (gdl instanceof GdlConstant) {
      //          GdlConstant constant = (GdlConstant)gdl;
      // Just a constant
    } else if (gdl instanceof GdlFunction) {
      GdlFunction func = (GdlFunction) gdl;
      for (int i = 0; i < func.arity(); i++) {
        Location parent = new Location();
        parent.name = func.getName();
        parent.idx = i;
        rval.addAll(findAllInstancesOf(var, func.get(i), parent));
      }
    } else if (gdl instanceof GdlVariable) { // This is the interesting one
      GdlVariable variable = (GdlVariable) gdl;
      if (variable == var) { // Found what we're looking for (base case of recursion)
        if (loc == null) throw new RuntimeException("Parent missing for a variable.");
        rval.add(domains.get(loc));
      }
    } else if (gdl instanceof GdlRule) {
      throw new RuntimeException("Shouldn't nest rules.");
    }

    return rval;
  }
示例#7
0
 private static GdlLiteral cleanParentheses(GdlLiteral literal) {
   if (literal instanceof GdlSentence) {
     return cleanParentheses((GdlSentence) literal);
   } else if (literal instanceof GdlDistinct) {
     GdlDistinct distinct = (GdlDistinct) literal;
     GdlTerm term1 = cleanParentheses(distinct.getArg1());
     GdlTerm term2 = cleanParentheses(distinct.getArg2());
     return GdlPool.getDistinct(term1, term2);
   } else if (literal instanceof GdlNot) {
     GdlLiteral body = ((GdlNot) literal).getBody();
     return GdlPool.getNot(cleanParentheses(body));
   } else if (literal instanceof GdlOr) {
     GdlOr or = (GdlOr) literal;
     List<GdlLiteral> disjuncts = new ArrayList<GdlLiteral>();
     for (int i = 0; i < or.arity(); i++) disjuncts.add(cleanParentheses(or.get(i)));
     return GdlPool.getOr(disjuncts);
   }
   throw new RuntimeException("Unexpected literal type in GdlCleaner");
 }