Package org.ggp.base.util.gdl.model

Examples of org.ggp.base.util.gdl.model.SentenceForm


    tuplesBySource = Lists.newArrayListWithCapacity(sourceConjuncts.size());//new ArrayList<List<List<GdlConstant>>>(sourceConjuncts.size());
    varsChosenBySource = Lists.newArrayListWithCapacity(sourceConjuncts.size());//new ArrayList<List<Integer>>(sourceConjuncts.size());
    putDontCheckBySource = Lists.newArrayListWithCapacity(sourceConjuncts.size());//new ArrayList<List<Boolean>>(sourceConjuncts.size());
    for(int j = 0; j < sourceConjuncts.size(); j++) {
      GdlSentence sourceConjunct = sourceConjuncts.get(j);
      SentenceForm form = SimpleSentenceForm.create(sourceConjunct);
      //flatten into a tuple
      List<GdlTerm> conjunctTuple = GdlUtils.getTupleFromSentence(sourceConjunct);
      //Go through the vars/constants in the tuple
      List<Integer> constraintSlots = new ArrayList<Integer>();
      List<GdlConstant> constraintValues = new ArrayList<GdlConstant>();
      List<Integer> varsChosen = new ArrayList<Integer>();
      List<Boolean> putDontCheck = new ArrayList<Boolean>();
      for(int i = 0; i < conjunctTuple.size(); i++) {
        GdlTerm term = conjunctTuple.get(i);
        if(term instanceof GdlConstant) {
          constraintSlots.add(i);
          constraintValues.add((GdlConstant) term);
          //TODO: What if tuple size ends up being 0?
          //Need to keep that in mind
        } else if(term instanceof GdlVariable) {
          int varIndex = varsToAssign.indexOf(term);
          varsChosen.add(varIndex);
          if(sourceDefiningSlot.get(varIndex) == -1) {
            //We define it
            sourceDefiningSlot.set(varIndex, j);
            putDontCheck.add(true);
          } else {
            //It's an overlap; we just check for consistency
            putDontCheck.add(false);
          }
        } else {
          throw new RuntimeException("Function returned in tuple");
        }
      }
      varsChosenBySource.add(ImmutableList.copyOf(varsChosen));
      putDontCheckBySource.add(ImmutableList.copyOf(putDontCheck));

      //Now we put the tuples together
      //We use constraintSlots and constraintValues to check that the
      //tuples have compatible values
      Collection<GdlSentence> sentences = completedSentenceFormValues.get(form);
      List<ImmutableList<GdlConstant>> tuples = Lists.newArrayList();
      byTuple: for(GdlSentence sentence : sentences) {
        //Check that it doesn't conflict with our headAssignment
        if (!headAssignment.isEmpty()) {
          Map<GdlVariable, GdlConstant> tupleAssignment = GdlUtils.getAssignmentMakingLeftIntoRight(sourceConjunct, sentence);
          for (GdlVariable var : headAssignment.keySet()) {
            if (tupleAssignment.containsKey(var)
                && tupleAssignment.get(var) != headAssignment.get(var)) {
              continue byTuple;
            }
          }
        }
        List<GdlConstant> longTuple = GdlUtils.getTupleFromGroundSentence(sentence);
        List<GdlConstant> shortTuple = new ArrayList<GdlConstant>(varsChosen.size());
        for(int c = 0; c < constraintSlots.size(); c++) {
          int slot = constraintSlots.get(c);
          GdlConstant value = constraintValues.get(c);
          if(!longTuple.get(slot).equals(value))
            continue byTuple;
        }
        int c = 0;
        for(int s = 0; s < longTuple.size(); s++) {
          //constraintSlots is sorted in ascending order
          if(c < constraintSlots.size()
              && constraintSlots.get(c) == s)
            c++;
          else
            shortTuple.add(longTuple.get(s));
        }
        //The tuple fits the source conjunct
        tuples.add(ImmutableList.copyOf(shortTuple));
      }
      //sortTuples(tuples); //Needed? Useful? Not sure. Probably not?
      tuplesBySource.add(ImmutableList.copyOf(tuples));
    }


    //We now want to see which we can give assignment functions to
    valuesToCompute = new ArrayList<AssignmentFunction>(varsToAssign.size());
    for(@SuppressWarnings("unused") GdlVariable var : varsToAssign) {
      valuesToCompute.add(null);
    }
    indicesToChangeWhenNull = new ArrayList<Integer>(varsToAssign.size());
    for(int i = 0; i < varsToAssign.size(); i++) {
      //Change itself, why not?
      //Actually, instead let's try -1, to catch bugs better
      indicesToChangeWhenNull.add(-1);
    }
    //Now we have our functions already selected by the ordering
    //bestOrdering.functionalConjunctIndices;

    //Make AssignmentFunctions out of the ordering
    List<GdlSentence> functionalConjuncts = bestOrdering.getFunctionalConjuncts();
//    System.out.println("functionalConjuncts: " + functionalConjuncts);
    for(int i = 0; i < functionalConjuncts.size(); i++) {
      GdlSentence functionalConjunct = functionalConjuncts.get(i);
      if(functionalConjunct != null) {
        //These are the only ones that could be constant functions
        SentenceForm conjForm = SimpleSentenceForm.create(functionalConjunct);
        FunctionInfo functionInfo = null;

        if(functionInfoMap != null)
          functionInfo = functionInfoMap.get(conjForm);
        if(functionInfo != null) {
View Full Code Here


    Map<GdlVariable, Integer> varDomainSizes = getVarDomainSizes(varDomains/*rule, model*/);

    List<Integer> sourceConjunctSizes = new ArrayList<Integer>();
    for(GdlLiteral conjunct : rule.getBody()) {
      if(conjunct instanceof GdlRelation) {
        SentenceForm form = SimpleSentenceForm.create((GdlRelation)conjunct);
        if(completedSentenceFormSizes != null
            && completedSentenceFormSizes.containsKey(form)) {
          int size = completedSentenceFormSizes.get(form);
          //New: Don't add if it will be useless as a source
          //For now, we take a strict definition of that
          //Compare its size with the product of the domains
          //of the variables it defines
          //In the future, we could require a certain ratio
          //to decide that this is worthwhile
          GdlRelation relation = (GdlRelation) conjunct;
          int maxSize = 1;
          Set<GdlVariable> vars = new HashSet<GdlVariable>(GdlUtils.getVariables(relation));
          for(GdlVariable var : vars) {
            int domainSize = varDomainSizes.get(var);
            maxSize *= domainSize;
          }
          if(size >= maxSize)
            continue;
          sourceConjunctCandidates.add(relation);
          sourceConjunctSizes.add(size);
        }
      }
    }

    List<GdlSentence> functionalSentences = new ArrayList<GdlSentence>();
    List<FunctionInfo> functionalSentencesInfo = new ArrayList<FunctionInfo>();
    for(GdlLiteral conjunct : rule.getBody()) {
      if(conjunct instanceof GdlSentence) {
        SentenceForm form = SimpleSentenceForm.create((GdlSentence) conjunct);
        if(functionInfoMap != null && functionInfoMap.containsKey(form)) {
          functionalSentences.add((GdlSentence) conjunct);
          functionalSentencesInfo.add(functionInfoMap.get(form));
        }
      }
View Full Code Here

    return newRulesHeuristic < curRuleHeuristic;
  }

  private static SentenceDomainModel augmentModelWithNewForm(
      final SentenceDomainModel oldModel, List<GdlRule> newRules) {
    final SentenceForm newForm = SimpleSentenceForm.create(newRules.get(0).getHead());
    final SentenceFormDomain newFormDomain = getNewFormDomain(newRules.get(0), oldModel, newForm);
    return new SentenceDomainModel() {
      @Override
      public SentenceFormDomain getDomain(SentenceForm form) {
        if (form.equals(newForm)) {
View Full Code Here

TOP

Related Classes of org.ggp.base.util.gdl.model.SentenceForm

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.