Exemplo n.º 1
0
  @Override
  public int getGoal(MachineState state, Role role) throws GoalDefinitionException {
    Set<GdlSentence> results =
        prover.askAll(ProverQueryBuilder.getGoalQuery(role), ProverQueryBuilder.getContext(state));

    if (results.size() != 1) {
      GamerLogger.logError(
          "StateMachine",
          "Got goal results of size: " + results.size() + " when expecting size one.");
      throw new GoalDefinitionException(state, role);
    }

    try {
      GdlRelation relation = (GdlRelation) results.iterator().next();
      GdlConstant constant = (GdlConstant) relation.get(1);

      return Integer.parseInt(constant.toString());
    } catch (Exception e) {
      throw new GoalDefinitionException(state, role);
    }
  }
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.");
    }
  }