private Set<GdlRelation> mergeBaseRelations(Set<GdlRelation> rels) { HashMap<GdlConstant, List<Set<GdlConstant>>> merges = new HashMap<GdlConstant, List<Set<GdlConstant>>>(); for (GdlRelation rel : rels) { GdlConstant name = (GdlConstant) rel.get(0); if (!merges.containsKey(name)) merges.put(name, new ArrayList<Set<GdlConstant>>()); List<Set<GdlConstant>> merge = merges.get(name); addRelToMerge(rel, merge); } Set<GdlRelation> rval = new HashSet<GdlRelation>(); GdlConstant valConst = GdlPool.getConstant("val"); for (GdlConstant c : merges.keySet()) { List<Set<GdlConstant>> merge = merges.get(c); List<GdlTerm> body = new ArrayList<GdlTerm>(); body.add(c); for (Set<GdlConstant> mergeSet : merge) { List<GdlTerm> ms2 = new ArrayList<GdlTerm>(mergeSet); Collections.sort(ms2, new SortTerms()); body.add(GdlPool.getFunction(valConst, ms2)); } GdlRelation toAdd = GdlPool.getRelation(baseConstant, body); rval.add(toAdd); } return rval; }
/** * Compute all of the roles in a game, in the correct order. * * <p>Order matters, because a joint move is defined as an ordered list of moves, in which the * order determines which player took which of the moves. This function will give an ordered list * in which the roles have that correct order. */ public static List<Role> computeRoles(List<? extends Gdl> description) { List<Role> roles = new ArrayList<Role>(); for (Gdl gdl : description) { if (gdl instanceof GdlRelation) { GdlRelation relation = (GdlRelation) gdl; if (relation.getName().getValue().equals("role")) { roles.add(new Role((GdlConstant) relation.get(0))); } } } return roles; }
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."); }
private static GdlRelation substituteRelation(GdlRelation relation, Substitution theta) { if (relation.isGround()) { return relation; } GdlConstant name = substituteConstant(relation.getName(), theta); List<GdlTerm> body = new ArrayList<>(); for (int i = 0; i < relation.arity(); i++) { body.add(substituteTerm(relation.get(i), theta)); } return GdlPool.getRelation(name, body); }
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; }
private void findAndInstantiateBaseProps(Gdl gdl) { if (gdl instanceof GdlRelation) { GdlRelation relation = (GdlRelation) gdl; String name = relation.getName().toString(); if (name.equals("init")) { if (relation.arity() != 1) throw new RuntimeException("Can't init more than one thing as far as I know."); GdlTerm template = relation.get(0); if (template instanceof GdlConstant) { List<GdlTerm> body = new ArrayList<GdlTerm>(); body.add(template); GdlRelation toAdd = GdlPool.getRelation(baseConstant, body); baseRelations.add(toAdd); System.err.println("Weird init of constant"); } else if (template instanceof GdlVariable) { System.err.println("Weird init of constant"); List<GdlTerm> body = new ArrayList<GdlTerm>(); body.add(universalDom); GdlRelation toAdd = GdlPool.getRelation(baseConstant, body); baseRelations.add(toAdd); System.err.println("Weird init of variable"); } else if (template instanceof GdlFunction) { GdlFunction func = (GdlFunction) template; instantiateBaseProps(func.toSentence()); } } } else if (gdl instanceof GdlRule) { GdlRule rule = (GdlRule) gdl; String name = rule.getHead().getName().toString(); if (name.equals("next")) { GdlSentence head = rule.getHead(); if (head.arity() != 1) throw new RuntimeException("Can't next more than one thing as far as I know."); if (head.get(0) instanceof GdlVariable) { // weird case where you have rule like (next ?q) Location l = new Location(); l.idx = 0; l.name = head.getName(); Domain dom = domains.get(l); for (GdlConstant c : dom.values) { List<GdlTerm> body = new ArrayList<GdlTerm>(); body.add(c); baseRelations.add(GdlPool.getRelation(baseConstant, body)); } } else instantiateBasePropsWithRHS(head.get(0).toSentence(), rule.getBody()); } } }
private void addRelToMerge(GdlRelation rel, List<Set<GdlConstant>> merge) { for (int i = 1; i < rel.arity(); i++) { GdlTerm t = rel.get(i); if (!(t instanceof GdlFunction)) throw new RuntimeException("Incorrectly constructed base props"); if (merge.size() < i) merge.add(new HashSet<GdlConstant>()); GdlFunction f = (GdlFunction) t; Set<GdlConstant> dom = merge.get(i - 1); for (GdlTerm t2 : f.getBody()) { if (!(t2 instanceof GdlConstant)) throw new RuntimeException( "Incorrectly constructed base props: something other than a constant"); dom.add((GdlConstant) t2); } } }
@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); } }