Пример #1
0
 private static GdlNot substituteNot(GdlNot not, Substitution theta) {
   if (not.isGround()) {
     return not;
   }
   GdlLiteral body = substituteLiteral(not.getBody(), theta);
   return GdlPool.getNot(body);
 }
Пример #2
0
 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
   }
 }
Пример #3
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.");
   }
 }
Пример #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());
   }
 }
Пример #6
0
 private static GdlNot getNotDistinctLiteral(GdlRule rule) {
   for (GdlLiteral literal : rule.getBody()) {
     if (literal instanceof GdlNot) {
       GdlNot not = (GdlNot) literal;
       if (not.getBody() instanceof GdlDistinct) {
         // For now, we can only deal with this if not both are functions.
         // That means we have to skip that case at this point.
         GdlDistinct distinct = (GdlDistinct) not.getBody();
         if (!(distinct.getArg1() instanceof GdlFunction)
             || !(distinct.getArg2() instanceof GdlFunction)) return not;
       }
     }
   }
   return null;
 }
Пример #7
0
 // Returns null if the rule is useless.
 private static GdlRule removeNotDistinctLiteral(GdlRule rule, GdlNot notDistinctLiteral) {
   // Figure out the substitution we want...
   // If we have two constants: Either remove one or
   // maybe get rid of the ___?
   // One is a variable: Replace the variable with the other thing
   // throughout the rule
   GdlDistinct distinct = (GdlDistinct) notDistinctLiteral.getBody();
   GdlTerm arg1 = distinct.getArg1();
   GdlTerm arg2 = distinct.getArg2();
   if (arg1 == arg2) {
     // Just remove that literal
     List<GdlLiteral> newBody = new ArrayList<GdlLiteral>();
     newBody.addAll(rule.getBody());
     newBody.remove(notDistinctLiteral);
     return GdlPool.getRule(rule.getHead(), newBody);
   }
   if (arg1 instanceof GdlVariable) {
     // What we return will still have the not-distinct literal,
     // but it will get replaced in the next pass.
     // (Even if we have two variables, they will be equal next time through.)
     return CommonTransforms.replaceVariable(rule, (GdlVariable) arg1, arg2);
   }
   if (arg2 instanceof GdlVariable) {
     return CommonTransforms.replaceVariable(rule, (GdlVariable) arg2, arg1);
   }
   if (arg1 instanceof GdlConstant || arg2 instanceof GdlConstant) {
     // We have two non-equal constants, or a constant and a function.
     // The rule should have no effect.
     return null;
   }
   // We have two functions. Complicated! (Have to replace them with unified version.)
   // We pass on this case for now.
   // TODO: Implement correctly.
   throw new UnsupportedOperationException(
       "We can't currently handle (not (distinct <function> <function>)).");
 }