Exemplo n.º 1
0
  private List<GdlRule> getAllInstantiations() {
    List<GdlRule> rval = new ArrayList<>();

    for (Gdl gdl : description) {
      if (gdl instanceof GdlRelation) {
        GdlRelation relation = (GdlRelation) gdl;
        String name = relation.getName().toString();
        if (name.equals("base")) continue;

        rval.add(GdlPool.getRule(relation));
      }
    }

    for (Domain d : domains.values()) {
      for (RuleReference r : d.ruleRefs) {
        Set<Map<GdlVariable, GdlConstant>> varInstantiations = findSatisfyingInstantiations(r);

        for (Map<GdlVariable, GdlConstant> varInstantiation : varInstantiations) {
          if (varInstantiation.containsValue(null))
            throw new RuntimeException("Shouldn't instantiate anything to null.");
          rval.add(getInstantiation(r.originalRule, varInstantiation));
          if (rval.get(rval.size() - 1).toString().contains("null"))
            throw new RuntimeException(
                "Shouldn't instantiate anything to null: " + rval.get(rval.size() - 1).toString());
        }
      }
    }

    for (RuleReference ruleRef : extraRefs) {
      List<Condition> newConditions = new ArrayList<>();
      for (Condition c : ruleRef.conditions) {
        if (c.dom == null) c.updateDom();

        if (c.dom != null) newConditions.add(c);
      }
      if (newConditions.size() != ruleRef.conditions.size()) // Remove reference to constant terms
      ruleRef.conditions = newConditions;
    }
    for (RuleReference r : extraRefs) {
      Set<Map<GdlVariable, GdlConstant>> varInstantiations = findSatisfyingInstantiations(r);

      for (Map<GdlVariable, GdlConstant> varInstantiation : varInstantiations) {
        if (varInstantiation.containsValue(null))
          throw new RuntimeException("Shouldn't instantiate anything to null.");
        rval.add(getInstantiation(r.originalRule, varInstantiation));

        if (rval.get(rval.size() - 1).toString().contains("null"))
          throw new RuntimeException("Shouldn't instantiate anything to null.");
      }

      if (varInstantiations.size() == 0)
        rval.add(getInstantiation(r.originalRule, new HashMap<GdlVariable, GdlConstant>()));
    }

    return rval;
  }
Exemplo n.º 2
0
  void initializeDomains(Gdl gdl) {
    if (gdl instanceof GdlRelation) {
      GdlRelation relation = (GdlRelation) gdl;
      String name = relation.getName().toString();
      if (!name.equals("base")) {
        GdlTerm term = relation.toTerm();
        GdlTerm generified = findGenericForm(term);
        Assignment instantiation = getConstantList(term);
        if (!domains.containsKey(generified)) domains.put(generified, new Domain(generified, term));
        Domain dom = domains.get(generified);
        dom.assignments.add(instantiation);
      }
    } else if (gdl instanceof GdlRule) {
      GdlRule rule = (GdlRule) gdl;
      GdlSentence head = rule.getHead();
      if (head instanceof GdlRelation) {
        GdlRelation rel = (GdlRelation) head;
        GdlTerm term = rel.toTerm();

        GdlTerm generified = findGenericForm(term);
        if (!domains.containsKey(generified)) domains.put(generified, new Domain(generified, term));
        Domain dom = domains.get(generified);

        List<GdlTerm> productionTemplate = getConstantAndVariableList(term);

        List<List<GdlLiteral>> newRHSs = deOr(rule.getBody());
        for (List<GdlLiteral> RHS : newRHSs) {
          RuleReference ruleRef = new RuleReference(GdlPool.getRule(head, RHS));
          ruleRef.productionTemplate = productionTemplate;
          for (GdlLiteral lit : RHS) {
            if (lit instanceof GdlSentence) {
              GdlTerm t = ((GdlSentence) lit).toTerm();
              Condition cond = new Condition(t);
              ruleRef.conditions.add(cond);
            }
          }
          dom.ruleRefs.add(ruleRef);
        }

      } else {
        List<List<GdlLiteral>> newRHSs = deOr(rule.getBody());
        for (List<GdlLiteral> RHS : newRHSs) {
          RuleReference ruleRef = new RuleReference(GdlPool.getRule(head, RHS));
          for (GdlLiteral lit : RHS) {
            if (lit instanceof GdlSentence) {
              GdlTerm t = ((GdlSentence) lit).toTerm();
              Condition cond = new Condition(t);
              ruleRef.conditions.add(cond);
            }
          }
          extraRefs.add(ruleRef);
        }
      }
    }
  }
Exemplo n.º 3
0
    public void buildIndices() {
      for (Assignment assignment : assignments) {
        addAssignmentToIndex(assignment);
      }
      for (RuleReference ruleRef : ruleRefs) {
        List<Condition> newConditions = new ArrayList<>();
        for (Condition c : ruleRef.conditions) {
          if (c.dom == null) c.updateDom();

          if (c.dom != null) newConditions.add(c);
        }
        if (newConditions.size() != ruleRef.conditions.size()) // Remove reference to constant terms
        ruleRef.conditions = newConditions;
      }
    }