Package org.ggp.base.util.gdl.grammar

Examples of org.ggp.base.util.gdl.grammar.GdlSentence


        //and the set of outstanding vars they depend on...
        Map<GdlVariable, Set<Integer>> functionsProducingVars = new HashMap<GdlVariable, Set<Integer>>();
        //We start by adding to the varOrdering the vars not produced by functions
        //First, we have to find them
        for(int i = 0; i < functionalSentencesInfo.size(); i++) {
          GdlSentence functionalSentence = functionalSentences.get(i);
          FunctionInfo functionInfo = functionalSentencesInfo.get(i);
          Set<GdlVariable> producibleVars = functionInfo.getProducibleVars(functionalSentence);
          for(GdlVariable producibleVar : producibleVars) {
            if(!functionsProducingVars.containsKey(producibleVar))
              functionsProducingVars.put(producibleVar, new HashSet<Integer>());
            functionsProducingVars.get(producibleVar).add(i);
          }
        }
        //Non-producible vars get iterated over before we start
        //deciding which functions to add
        for(GdlVariable var : varsToAssign) {
          if(!varOrdering.contains(var)) {
            if(!functionsProducingVars.containsKey(var)) {
              //Add var to the ordering
              varOrdering.add(var);
              functionalConjunctIndices.add(-1);
              varSources.add(-1);
            }
          }
        }


        //Map is from potential set of dependencies to function indices
        Map<Set<GdlVariable>, Set<Integer>> functionsHavingDependencies = new HashMap<Set<GdlVariable>, Set<Integer>>();
        //Create this map...
        for(int i = 0; i < functionalSentencesInfo.size(); i++) {
          GdlSentence functionalSentence = functionalSentences.get(i);
          FunctionInfo functionInfo = functionalSentencesInfo.get(i);
          Set<GdlVariable> producibleVars = functionInfo.getProducibleVars(functionalSentence);
          Set<GdlVariable> allVars = new HashSet<GdlVariable>(GdlUtils.getVariables(functionalSentence));
          //Variables already in varOrdering don't go in dependents list
          producibleVars.removeAll(varOrdering);
          allVars.removeAll(varOrdering);
          for(GdlVariable producibleVar : producibleVars) {
            Set<GdlVariable> dependencies = new HashSet<GdlVariable>();
            dependencies.addAll(allVars);
            dependencies.remove(producibleVar);
            if(!functionsHavingDependencies.containsKey(dependencies))
              functionsHavingDependencies.put(dependencies, new HashSet<Integer>());
            functionsHavingDependencies.get(dependencies).add(i);
          }
        }
        //Now, we can keep creating functions to generate the remaining variables
        while(varOrdering.size() < varsToAssign.size()) {
          if(functionsHavingDependencies.isEmpty())
            throw new RuntimeException("We should not run out of functions we could use");
          //Find the smallest set of dependencies
          Set<GdlVariable> dependencySetToUse = null;
          if(functionsHavingDependencies.containsKey(Collections.emptySet())) {
            dependencySetToUse = Collections.emptySet();
          } else {
            int smallestSize = Integer.MAX_VALUE;
            for(Set<GdlVariable> dependencySet : functionsHavingDependencies.keySet()) {
              if(dependencySet.size() < smallestSize) {
                smallestSize = dependencySet.size();
                dependencySetToUse = dependencySet;
              }
            }
          }
          //See if any of the functions are applicable
          Set<Integer> functions = functionsHavingDependencies.get(dependencySetToUse);
          int functionToUse = -1;
          GdlVariable varProduced = null;
          for (int function : functions) {
            GdlSentence functionalSentence = functionalSentences.get(function);
            FunctionInfo functionInfo = functionalSentencesInfo.get(function);
            Set<GdlVariable> producibleVars = functionInfo.getProducibleVars(functionalSentence);
            producibleVars.removeAll(dependencySetToUse);
            producibleVars.removeAll(varOrdering);
            if(!producibleVars.isEmpty()) {
              functionToUse = function;
              varProduced = producibleVars.iterator().next();
              break;
            }
          }

          if(functionToUse == -1) {
            //None of these functions were actually useful now?
            //Dump the dependency set
            functionsHavingDependencies.remove(dependencySetToUse);
          } else {
            //Apply the function
            //1) Add the remaining dependencies as iterated variables
            for(GdlVariable var : dependencySetToUse) {
              varOrdering.add(var);
              functionalConjunctIndices.add(-1);
              varSources.add(-1);
            }
            //2) Add the function's produced variable (varProduced)
            varOrdering.add(varProduced);
            functionalConjunctIndices.add(functionToUse);
            varSources.add(-1);
            //3) Remove all vars added this way from all dependency sets
            Set<GdlVariable> addedVars = new HashSet<GdlVariable>();
            addedVars.addAll(dependencySetToUse);
            addedVars.add(varProduced);
            //Tricky, because we have to merge sets
            //Easier to use a new map
            Map<Set<GdlVariable>, Set<Integer>> newFunctionsHavingDependencies = new HashMap<Set<GdlVariable>, Set<Integer>>();
            for(Entry<Set<GdlVariable>, Set<Integer>> entry : functionsHavingDependencies.entrySet()) {
              Set<GdlVariable> newKey = new HashSet<GdlVariable>(entry.getKey());
              newKey.removeAll(addedVars);
              if(!newFunctionsHavingDependencies.containsKey(newKey))
                newFunctionsHavingDependencies.put(newKey, new HashSet<Integer>());
              newFunctionsHavingDependencies.get(newKey).addAll(entry.getValue());
            }
            functionsHavingDependencies = newFunctionsHavingDependencies;
            //4) Remove this function from the lists?
            for(Set<Integer> functionSet : functionsHavingDependencies.values())
              functionSet.remove(functionToUse);
          }

        }

        //Now we need to actually return the ordering in a list
        //Here's the quick way to do that...
        //(since we've added all the new stuff to ourself already)
        return Collections.singletonList(new IterationOrderCandidate(this));

      } else {

        //Let's try a new technique for restricting the space of possibilities...
        //We already have an ordering on the functions
        //Let's try to constrain things to that order
        //Namely, if i<j and constant form j is already used as a function,
        //we cannot use constant form i UNLESS constant form j supplies
        //as its variable something used by constant form i.
        //We might also try requiring that c.f. i NOT provide a variable
        //used by c.f. j, though there may be multiple possibilities as
        //to what it could provide.
        int lastFunctionUsedIndex = -1;
        if (!functionalConjunctIndices.isEmpty()) {
          lastFunctionUsedIndex = Collections.max(functionalConjunctIndices);
        }
        Set<GdlVariable> varsProducedByFunctions = new HashSet<GdlVariable>();
        for (int i = 0; i < functionalConjunctIndices.size(); i++) {
          if (functionalConjunctIndices.get(i) != -1) {
            varsProducedByFunctions.add(varOrdering.get(i));
          }
        }
        for (int i = 0; i < functionalSentencesInfo.size(); i++) {
          GdlSentence functionalSentence = functionalSentences.get(i);
          FunctionInfo functionInfo = functionalSentencesInfo.get(i);

          if (i < lastFunctionUsedIndex) {
            //We need to figure out whether i could use any of the
            //vars we're producing with functions
View Full Code Here


            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<GdlLiteral>();
            for(int i=0; i<rule.arity(); i++)
            {
                body.add((GdlLiteral)getInstantiationAux(rule.get(i), varInstantiation));
View Full Code Here

            }
        }
        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();
View Full Code Here

        GdlOr or = (GdlOr) qPrime;
        isConstant = askOr(or, goals, context, theta, cache, renamer, askOne, results, alreadyAsking);
      }
      else
      {
        GdlSentence sentence = (GdlSentence) qPrime;
        isConstant = askSentence(sentence, goals, context, theta, cache, renamer, askOne, results, alreadyAsking);
      }

      goals.addFirst(literal);
      return isConstant;
View Full Code Here

  }

  // Returns true iff the result is constant across all possible states of the game.
  private boolean askSentence(GdlSentence sentence, LinkedList<GdlLiteral> goals, KnowledgeBase context, Substitution theta, ProverCache cache, VariableRenamer renamer, boolean askOne, Set<Substitution> results, Set<GdlSentence> alreadyAsking)
  {
    GdlSentence varRenamedSentence = new VariableRenamer().rename(sentence);
    if (!fixedAnswerCache.contains(varRenamedSentence) && !cache.contains(varRenamedSentence))
    {
      //Prevent infinite loops on certain recursive queries.
      if(alreadyAsking.contains(sentence)) {
        return false;
View Full Code Here


    //Go through all the collections it could appear in
    if(c instanceof Proposition) {
      Proposition p = (Proposition) c;
      GdlSentence name = p.getName();
      if(basePropositions.containsKey(name)) {
        basePropositions.remove(name);
      } else if(inputPropositions.containsKey(name)) {
        inputPropositions.remove(name);
        //The map goes both ways...
View Full Code Here

      GdlConstant name = nameAndArity.getName();
      List<TermModel> bodyModels = sentenceEntry.getValue();
      // We'll end up taking the Cartesian product of the different
      // types of terms we have available
      if (nameAndArity.getArity() == 0) {
        GdlSentence sentence = GdlPool.getProposition(name);
        SimpleSentenceForm form = SimpleSentenceForm.create(sentence);
        results.put(form, CartesianSentenceFormDomain.create(form, ImmutableList.<Set<GdlConstant>>of()));
      } else {
        List<Set<GdlTerm>> sampleTerms = toSampleTerms(bodyModels);
        for (List<GdlTerm> terms : Sets.cartesianProduct(sampleTerms)) {
View Full Code Here

      return output;
    }
    else
    {
      GdlSentence sentence = (GdlSentence) literal;

      Proposition proposition = getProposition(sentence);
      components.add(proposition);

      return proposition;
View Full Code Here

      GdlConstant name = nameAndArity.getName();
      List<TermModel> bodyModels = sentenceEntry.getValue();
      // We'll end up taking the Cartesian product of the different
      // types of terms we have available
      if (nameAndArity.getArity() == 0) {
        GdlSentence sentence = GdlPool.getProposition(name);
        results.add(SimpleSentenceForm.create(sentence));
      } else {
        List<Set<GdlTerm>> sampleTerms = toSampleTerms(bodyModels);
        for (List<GdlTerm> terms : Sets.cartesianProduct(sampleTerms)) {
          GdlSentence sentence = GdlPool.getRelation(name, terms);
          results.add(SimpleSentenceForm.create(sentence));
        }
      }
    }
    return results;
View Full Code Here

    // the head of the rule as presented or due to a variable connected
    // to the positive literals in the rule. (In the latter case, it
    // should be in the intersection of the models of all such positive
    // literals.) For each slot in the body, we want to set up everything
    // that will be injected into it.
    GdlSentence headSentence = rule.getHead();

    // We need to get the possible contents of variables beforehand, to
    // deal with the case of variables being inside functions.
    Map<GdlVariable, TermModel> varsToModelsMap = getVarsToModelsMap(rule);
View Full Code Here

TOP

Related Classes of org.ggp.base.util.gdl.grammar.GdlSentence

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.