void processGdl(Gdl gdl, GdlConstant parent) { if (gdl instanceof GdlRelation) { GdlRelation relation = (GdlRelation) gdl; String name = relation.getName().toString(); if (!name.equals("base")) { for (Gdl gdl2 : relation.getBody()) processGdl(gdl2, relation.getName()); } } else if (gdl instanceof GdlRule) { GdlRule rule = (GdlRule) gdl; for (Gdl gdl2 : rule.getBody()) processGdl(gdl2, null); } else if (gdl instanceof GdlConstant) { universe.add((GdlConstant) gdl); } else if (gdl instanceof GdlFunction) { GdlFunction func = (GdlFunction) gdl; for (Gdl gdl2 : func.getBody()) processGdl(gdl2, func.getName()); } else if (gdl instanceof GdlDistinct) { GdlDistinct distinct = (GdlDistinct) gdl; processGdl(distinct.getArg1(), null); processGdl(distinct.getArg2(), null); } else if (gdl instanceof GdlNot) { GdlNot not = (GdlNot) gdl; processGdl(not.getBody(), null); } else if (gdl instanceof GdlOr) { GdlOr or = (GdlOr) gdl; for (int i = 0; i < or.arity(); i++) processGdl(or.get(i), null); } else if (gdl instanceof GdlProposition) { // IGNORE } else if (gdl instanceof GdlVariable) { // IGNORE } }
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; }
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; }
void addDomain(GdlRelation relation) { int i = 0; for (GdlTerm term : relation.getBody()) { Location loc = new Location(); loc.idx = i; loc.name = relation.getName(); addDomain(term, loc); i++; } }
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); } } } }
/** * 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); }
public List<Gdl> getAugmentedDescription() { List<Gdl> rval = new ArrayList<Gdl>(); for (Gdl gdl : description) { boolean notBase = true; if (gdl instanceof GdlRelation) { GdlRelation rel = (GdlRelation) gdl; if (rel.getName().toString().equals("base")) notBase = false; } if (notBase) rval.add(gdl); } rval.addAll(getAnnotations()); return rval; }
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); } }
void processDomain(Gdl gdl) { if (gdl instanceof GdlRelation) { GdlRelation relation = (GdlRelation) gdl; String name = relation.getName().toString(); if (!name.equals("base")) { addDomain(relation); } } else if (gdl instanceof GdlRule) { GdlRule rule = (GdlRule) gdl; GdlSentence head = rule.getHead(); if (head instanceof GdlRelation) { GdlRelation rel = (GdlRelation) head; int i = 0; for (GdlTerm term : rel.getBody()) { addDomain2(term, rel.getName(), i, rule.getBody()); i++; } } else if (head instanceof GdlProposition) { // GdlProposition prop = (GdlProposition)head; // addDomain2(prop.toTerm(), prop.getName(), 0, rule.getBody()); } else throw new RuntimeException("Don't know how to deal with this."); } }
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; }