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

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


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


                        {
                            if(t instanceof GdlConstant)
                                a.add((GdlConstant)t);
                            else
                            {
                                GdlVariable var = (GdlVariable)t;
                                a.add(instantiation.get(var));
                            }
                        }
                        if(!d.assignments.contains(a))
                        {
View Full Code Here

        {
            GdlTerm t = cond.template.get(i);
            GdlConstant c = null;
            if(t instanceof GdlVariable)
            {
                GdlVariable v = (GdlVariable)t;
                if(instantiation.containsKey(v))
                    c = instantiation.get(v);
            }
            else if(t instanceof GdlConstant)
                c = (GdlConstant)t;

            if(c != null)
            {
                if(assignments == null)
                {
                    assignments = new Assignments();
                    if(dom.indices.size() > i//if this doesn't hold it is because there are no assignments and the indices haven't been set up yet
                    {
                        Index index = dom.indices.get(i);
                        if(index.containsKey(c))  //Might be no assignment which satisfies this condition
                            assignments.addAll(index.get(c));
                    }
                }
                else
                {
                    if(dom.indices.size()>i)
                    {
                        Index index = dom.indices.get(i);
                        if(index.containsKey(c))  //Might be no assignment which satisfies this condition
                            assignments.retainAll(index.get(c));
                    }
                    else  //This is when we've tried to find an assignment for a form that doesn't have any assignments yet.  Pretend it returned an empty set
                        assignments.clear();
                }
            }
        }
        if(assignments == null) //case where there are no constants to be consistent with
        {
            assignments = dom.assignments;
        }

        for(Assignment a : assignments)
        {
            Map<GdlVariable, GdlConstant> newInstantiation = new HashMap<GdlVariable, GdlConstant>(instantiation);
            for(int i=0; i<a.size(); i++)
            {
                GdlTerm t = cond.template.get(i);
                if(t instanceof GdlVariable)
                {
                    GdlVariable var = (GdlVariable)t;
                    if(!instantiation.containsKey(var))
                        newInstantiation.put(var, a.get(i));
                }
            }
            rval.addAll(findSatisfyingInstantiations(conditions, idx+1, newInstantiation));
View Full Code Here

            }
          }
          //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
            //TODO: Try this with a finer grain
            //i.e., see if i needs a var from a function that is after
            //it, not one that might be before it
            List<GdlVariable> varsInSentence = GdlUtils.getVariables(functionalSentence);
            if (Collections.disjoint(varsInSentence, varsProducedByFunctions)) {
              continue;
            }
          }

          //What is the best variable to grab from this form, if there are any?
          GdlVariable bestVariable = getBestVariable(functionalSentence, functionInfo);
          if (bestVariable == null) {
            continue;
          }
          IterationOrderCandidate newCandidate =
            new IterationOrderCandidate(this, functionalSentence, i, bestVariable);
View Full Code Here

      }
      //TODO: Should we just generate the candidate vars with a call to getProducibleVars?
      Set<GdlVariable> producibleVars = functionInfo.getProducibleVars(functionalSentence);
      candidateVars.retainAll(producibleVars);
      //Now we look at the domains, trying to find the largest
      GdlVariable bestVar = null;
      int bestDomainSize = 0;
      for(GdlVariable var : candidateVars) {
        int domainSize = varDomainSizes.get(var);
        if(domainSize > bestDomainSize) {
          bestVar = var;
View Full Code Here

    }
    for (int i = 0; i < body.size(); i++) {
      GdlTerm term = body.get(i);
      TermModel termModel = model.get(i);
      if (term instanceof GdlVariable) {
        GdlVariable var = (GdlVariable) term;
        if (varsToModelsMap.containsKey(var)) {
          varsToModelsMap.get(var).mergeIn(termModel);
        }
      } else if (term instanceof GdlFunction) {
        GdlFunction function = (GdlFunction) term;
View Full Code Here

          //and which are suitable as functional outputs.

          //1) Which vars are in this conjunct?
          List<GdlVariable> varsInSentence = GdlUtils.getVariables(functionalConjunct);
          //2) Of these vars, which is "rightmost"?
          GdlVariable rightmostVar = getRightmostVar(varsInSentence);
          //3) Is it only used once in the relation?
          if(Collections.frequency(varsInSentence, rightmostVar) != 1)
            continue; //Can't use it
          //4) Which slot is it used in in the relation?
          //5) Build an AssignmentFunction if appropriate.
          //   This should be able to translate from values of
          //   the other variables to the value of the wanted
          //   variable.
          AssignmentFunction function = AssignmentFunction.create((GdlRelation)functionalConjunct, functionInfo, rightmostVar, varsToAssign, headAssignment);
          //We don't guarantee that this works until we check
          if(!function.functional())
            continue;
          int index = varsToAssign.indexOf(rightmostVar);
          valuesToCompute.set(index, function);
          Set<GdlVariable> remainingVarsInSentence = new HashSet<GdlVariable>(varsInSentence);
          remainingVarsInSentence.remove(rightmostVar);
          GdlVariable nextRightmostVar = getRightmostVar(remainingVarsInSentence);
          indicesToChangeWhenNull.set(index, varsToAssign.indexOf(nextRightmostVar));
        }
      }
    }
View Full Code Here

//    System.out.println("valuesToCompute: " + valuesToCompute);
//    System.out.println("sourceDefiningSlot: " + sourceDefiningSlot);
  }

  private GdlVariable getRightmostVar(Collection<GdlVariable> vars) {
    GdlVariable rightmostVar = null;
    for(GdlVariable var : varsToAssign)
      if(vars.contains(var))
        rightmostVar = var;
    return rightmostVar;
  }
View Full Code Here

      if(distinct.getArg1() instanceof GdlVariable)
        varsInDistinct.add((GdlVariable) distinct.getArg1());
      if(distinct.getArg2() instanceof GdlVariable)
        varsInDistinct.add((GdlVariable) distinct.getArg2());

      GdlVariable varToChange = null;
      if(varsInDistinct.size() == 1) {
        varToChange = varsInDistinct.get(0);
      } else if(varsInDistinct.size() == 2) {
        varToChange = getRightmostVar(varsInDistinct);
      }
View Full Code Here

      if(headTerm instanceof GdlConstant) {
        if(!refTerm.equals(headTerm))
          //The rule can't produce this sentence
          return false;
      } else if(headTerm instanceof GdlVariable) {
        GdlVariable var = (GdlVariable) headTerm;
        GdlConstant curValue = assignment.get(var);
        if(curValue != null && !curValue.equals(refTerm)) {
          //inconsistent assignment (e.g. head is (rel ?x ?x), sentence is (rel 1 2))
          return false;
        }
View Full Code Here

TOP

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

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.