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

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


            GdlLiteral literal = rule.get(0);
            if(literal instanceof GdlRelation) {
              //Check that it really is the true form
              SentenceForm trueForm = nextForm.withName(GdlPool.getConstant("true"));
              if(trueForm.matches((GdlRelation) literal)) {
                GdlSentence head = rule.getHead();
                GdlSentence body = (GdlSentence) literal;
                //Check that the tuples are the same, and that they
                //consist of distinct variables
                List<GdlTerm> headTuple = GdlUtils.getTupleFromSentence(head);
                List<GdlTerm> bodyTuple = GdlUtils.getTupleFromSentence(body);
                if(headTuple.equals(bodyTuple)) {
                  //Distinct variables?
                  Set<GdlTerm> vars = new HashSet<GdlTerm>(headTuple);
                  if(vars.size() == headTuple.size()) {
                    nextFormsToReplace.add(nextForm);
                  }
                }
              }
            }
          }
        }
      }
    }

    List<Gdl> newDescription = new ArrayList<Gdl>(description);
    //Now, replace the next forms
    for(SentenceForm nextForm : nextFormsToReplace) {
      SentenceForm initForm = nextForm.withName(GdlPool.getConstant("init"));
      SentenceForm trueForm = nextForm.withName(GdlPool.getConstant("true"));

      //Go through the rules and relations, making replacements as needed
      for(int i = 0; i < newDescription.size(); i++) {
        Gdl gdl = newDescription.get(i);
        if(gdl instanceof GdlRelation) {
          //Replace initForm
          GdlRelation relation = (GdlRelation) gdl;
          if(initForm.matches(relation)) {
            GdlSentence newSentence = relation.get(0).toSentence();
            newDescription.set(i, newSentence);
          }
        } else if(gdl instanceof GdlRule) {
          GdlRule rule = (GdlRule) gdl;
          //Remove persistence rule (i.e. rule with next form as head)
          GdlSentence head = rule.getHead();
          if(nextForm.matches(head)) {
            newDescription.remove(i);
            i--;
          } else {
            //Replace true in bodies of rules with relation-only form
View Full Code Here


  }

  private static GdlLiteral replaceRelationInLiteral(GdlLiteral literal,
      SentenceForm trueForm) {
    if(literal instanceof GdlSentence) {
      GdlSentence sentence = (GdlSentence) literal;
      if(trueForm.matches(sentence)) {
        //Replace with the sentence contained in the true sentence...
        return sentence.get(0).toSentence();
      } else {
        return literal;
      }
    } else if(literal instanceof GdlNot) {
      GdlNot not = (GdlNot) literal;
View Full Code Here

    List<GdlSentence> sourceConjuncts = bestOrdering.getSourceConjuncts();
    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;
View Full Code Here

      if(isRecursive(curRule)) {
        //Don't mess with it!
        newDescription.add(curRule);
        continue;
      }
      GdlSentence curRuleHead = curRule.getHead();
      if(SentenceModelUtils.inSentenceFormGroup(curRuleHead, constantForms)) {
        newDescription.add(curRule);
        continue;
      }
      Set<GdlLiteral> condensationSet = getCondensationSet(curRule, model, constantChecker, sentenceNameSource);
View Full Code Here

    //Now we're ready to split it apart
    //Let's make the new rule
    List<GdlTerm> orderedVars = new ArrayList<GdlTerm>(varsToKeep);
    GdlConstant condenserName = sentenceNameSource.getNameWithPrefix(rule.getHead().getName());
    //Make the rule head
    GdlSentence condenserHead;
    if(orderedVars.isEmpty()) {
      condenserHead = GdlPool.getProposition(condenserName);
    } else {
      condenserHead = GdlPool.getRelation(condenserName, orderedVars);
    }
View Full Code Here

    }

    public boolean noAnnotations() {
        for ( Gdl gdl : description ) {
            if ( ! (gdl instanceof GdlSentence) ) continue;
            GdlSentence sentence = (GdlSentence) gdl;

            if (sentence.getName().getValue().equals("base"))
                return false;
        }

        return true;
    }
View Full Code Here

            if ( constant.getValue().equals("does") )
            {
                for ( GdlRule rule : getInstantiations(GdlPool.getConstant("legal")) )
                {
                    GdlSentence head = GdlPool.getRelation(GdlPool.getConstant("does"), rule.getHead().getBody());
                    GdlRule equivalentDoesRule = GdlPool.getRule(head);
                    instantiations.get(constant).add(equivalentDoesRule);
                }
            }
            else
View Full Code Here

        List<GdlRule> trues = new ArrayList<GdlRule>();
        for ( Gdl gdl : description )
        {
            if ( gdl instanceof GdlSentence )
            {
                GdlSentence sentence = (GdlSentence) gdl;
                if ( sentence.getName().getValue().equals("base") )
                {
                    if ( sentence.arity() == 1 )
                    {
                        GdlConstant constant = (GdlConstant) sentence.get(0);
                        trues.add(GdlPool.getRule(GdlPool.getRelation(GdlPool.getConstant("true"), new GdlTerm[] { constant })));
                    }
                    else
                    {
                        List<GdlRule> results = new ArrayList<GdlRule>();
View Full Code Here

        else
        {
            GdlLiteral literal = template.getBody().get(index);
            if ( literal instanceof GdlSentence )
            {
                GdlSentence sentence = (GdlSentence) Substituter.substitute(literal, theta);
                for ( GdlRule instantiation : getInstantiations(sentence.getName()) )
                {
                    Substitution thetaPrime = Unifier.unify(sentence, instantiation.getHead());
                    if ( thetaPrime!=null )
                    {
                        Substitution thetaCopy = theta.copy();
View Full Code Here

    {
      return rule;
    }
    else
    {
      GdlSentence head = renameSentence(rule.getHead(), renamings);

      List<GdlLiteral> body = new ArrayList<GdlLiteral>();
      for (int i = 0; i < rule.arity(); i++)
      {
        body.add(renameLiteral(rule.get(i), renamings));
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.