Exemplo n.º 1
0
  private Assignment getConstantList(GdlTerm term) {
    Assignment rval = new Assignment();
    if (term instanceof GdlConstant) {
      rval.add((GdlConstant) term);
      return rval;
    } else if (term instanceof GdlVariable)
      throw new RuntimeException("Called getConstantList on something containing a variable.");

    GdlFunction func = (GdlFunction) term;
    for (GdlTerm t : func.getBody()) rval.addAll(getConstantList(t));

    return rval;
  }
Exemplo n.º 2
0
  void updateDomains() {
    boolean changedSomething = true;
    int itrNum = 0;
    Set<Domain> lastUpdatedDomains = new HashSet<>(domains.values());
    while (changedSomething) {
      GamerLogger.log("StateMachine", "Beginning domain finding iteration: " + itrNum);
      Set<Domain> currUpdatedDomains = new HashSet<>();
      changedSomething = false;
      int rulesConsidered = 0;
      for (Domain d : domains.values()) {

        for (RuleReference ruleRef : d.ruleRefs) {
          boolean containsUpdatedDomain = false;
          for (Condition c : ruleRef.conditions)
            if (lastUpdatedDomains.contains(c.dom)) {
              containsUpdatedDomain = true;
              break;
            }
          if (!containsUpdatedDomain) continue;

          rulesConsidered++;

          Set<Map<GdlVariable, GdlConstant>> instantiations = findSatisfyingInstantiations(ruleRef);
          for (Map<GdlVariable, GdlConstant> instantiation : instantiations) {
            Assignment a = new Assignment();
            for (GdlTerm t : ruleRef.productionTemplate) {
              if (t instanceof GdlConstant) a.add((GdlConstant) t);
              else {
                GdlVariable var = (GdlVariable) t;
                a.add(instantiation.get(var));
              }
            }
            if (!d.assignments.contains(a)) {
              currUpdatedDomains.add(d);
              d.assignments.add(a);
              changedSomething = true;
              d.addAssignmentToIndex(a);
            }
          }
          if (instantiations.size() == 0) { // There might just be no variables in the rule
            Assignment a = new Assignment();
            findSatisfyingInstantiations(ruleRef); // just for debugging
            boolean isVar = false;
            for (GdlTerm t : ruleRef.productionTemplate) {
              if (t instanceof GdlConstant) a.add((GdlConstant) t);
              else {
                // There's a variable and we didn't find an instantiation
                isVar = true;
                break;
              }
            }
            if (!isVar && !d.assignments.contains(a)) {
              currUpdatedDomains.add(d);
              d.assignments.add(a);
              changedSomething = true;
              d.addAssignmentToIndex(a);
            }
          }
        }
      }
      itrNum++;
      lastUpdatedDomains = currUpdatedDomains;
      GamerLogger.log(
          "StateMachine", "\tDone with iteration.  Considered " + rulesConsidered + " rules.");
    }
  }