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); } } }
private static List<Gdl> runOnce(List<Gdl> description) { List<Gdl> newDescription = new ArrayList<Gdl>(); // First: Clean up all rules with zero-element bodies for (Gdl gdl : description) { if (gdl instanceof GdlRule) { GdlRule rule = (GdlRule) gdl; if (rule.getBody().size() == 0) { newDescription.add(rule.getHead()); } else { newDescription.add(gdl); } } else { newDescription.add(gdl); } } // TODO: Add (role ?player) where appropriate, i.e. in rules for // "legal" or "input" where the first argument is an undefined // variable // Get rid of "extra parentheses", i.e. zero-arity functions description = newDescription; newDescription = new ArrayList<Gdl>(); for (Gdl gdl : description) { if (gdl instanceof GdlRelation) { newDescription.add(cleanParentheses((GdlRelation) gdl)); } else if (gdl instanceof GdlRule) { newDescription.add(cleanParentheses((GdlRule) gdl)); } else { newDescription.add(gdl); } } // TODO: Get rid of GdlPropositions in the description // Get rid of (not (distinct _ _)) literals in rules // TODO: Expand to functions description = newDescription; newDescription = new ArrayList<Gdl>(); for (Gdl gdl : description) { if (gdl instanceof GdlRule) { GdlRule cleaned = removeNotDistinctLiterals((GdlRule) gdl); if (cleaned != null) newDescription.add(cleaned); } else { newDescription.add(gdl); } } // Get rid of the old style of "base" sentences (with arity more than 1, not in rules) // See e.g. current version of Qyshinsu on the Dresden server description = newDescription; newDescription = new ArrayList<Gdl>(); boolean removeBaseSentences = false; for (Gdl gdl : description) { if (gdl instanceof GdlRelation) { GdlRelation relation = (GdlRelation) gdl; if (relation.getName() == BASE && relation.arity() != 1) { removeBaseSentences = true; break; } } } // Note that in this case, we have to remove ALL of them or we might // misinterpret this as being the new kind of "base" relation for (Gdl gdl : description) { if (gdl instanceof GdlRelation) { GdlRelation relation = (GdlRelation) gdl; if (removeBaseSentences && relation.getName() == BASE) { // Leave out the relation } else { newDescription.add(gdl); } } else { newDescription.add(gdl); } } return newDescription; }