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

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


            }
            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));
            }
            return GdlPool.getRule(head, body);
        }
        else if(gdl instanceof GdlDistinct)
        {
View Full Code Here


                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)
                    {
View Full Code Here

      boolean isConstant = !isTrueOrDoesSentence(sentence);

      Set<Substitution> sentenceResults = new HashSet<Substitution>();
      for (GdlRule rule : candidates)
      {
        GdlRule r = renamer.rename(rule);
        Substitution thetaPrime = Unifier.unify(r.getHead(), sentence);

        if (thetaPrime != null)
        {
          LinkedList<GdlLiteral> sentenceGoals = new LinkedList<GdlLiteral>();
          for (int i = 0; i < r.arity(); i++)
          {
            sentenceGoals.add(r.get(i));
          }

          isConstant &= ask(sentenceGoals, context, theta.compose(thetaPrime), cache, renamer, false, sentenceResults, alreadyAsking);
        }
      }
View Full Code Here

  public KnowledgeBase(Set<? extends Gdl> description)
  {
    contents = new HashMap<GdlConstant, List<GdlRule>>();
    for (Gdl gdl : description)
    {
      GdlRule rule = (gdl instanceof GdlRule) ? (GdlRule) gdl : GdlPool.getRule((GdlSentence) gdl);
      GdlConstant key = rule.getHead().getName();

      if (!contents.containsKey(key))
      {
        contents.put(key, new ArrayList<GdlRule>());
      }
View Full Code Here

    oldRules = DeORer.run(oldRules);

    List<Gdl> newRules = Lists.newArrayListWithCapacity(oldRules.size());
    for (Gdl gdl : oldRules) {
      if (gdl instanceof GdlRule) {
        GdlRule rule = (GdlRule) gdl;
        newRules.add(reorderRule(rule));
      } else {
        newRules.add(gdl);
      }
    }
View Full Code Here

        List<Gdl> newDesc = new ArrayList<Gdl>();
        for(Gdl gdl : description)
        {
            if(gdl instanceof GdlRule)
            {
                GdlRule rule = (GdlRule)gdl;
                List<List<GdlLiteral>> newBodies = deOr(rule.getBody());
                for(List<GdlLiteral> body : newBodies)
                {
                    newDesc.add(GdlPool.getRule(rule.getHead(), body));
                }
            }
            else
                newDesc.add(gdl);
        }
View Full Code Here

    for(SentenceForm nextForm : model.getSentenceForms()) {
      if(nextForm.getName().equals(NEXT)) {
        //See if there is exactly one update rule, and it is the persistence rule
        Set<GdlRule> rules = model.getRules(nextForm);
        if(rules.size() == 1) {
          GdlRule rule = rules.iterator().next();
          //Persistence rule: Exactly one literal, the "true" form of the sentence
          if(rule.arity() == 1) {
            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
            List<GdlLiteral> body = rule.getBody();
            List<GdlLiteral> newBody = replaceRelationInBody(body, trueForm);
            if(!body.equals(newBody)) {
              GdlRule newRule = GdlPool.getRule(head, newBody);
              newDescription.set(i, newRule);
            }
          }
        }
      }
View Full Code Here

    ConcurrencyUtils.checkForInterruption();

    List<Gdl> curDescription = Lists.newArrayList(description);
    while(!rulesToAdd.isEmpty()) {
      GdlRule curRule = rulesToAdd.remove();
      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

      condenserHead = GdlPool.getProposition(condenserName);
    } else {
      condenserHead = GdlPool.getRelation(condenserName, orderedVars);
    }
    List<GdlLiteral> condenserBody = new ArrayList<GdlLiteral>(condensationSet);
    GdlRule condenserRule = GdlPool.getRule(condenserHead, condenserBody);
    //TODO: Look for existing rules matching the new one

    List<GdlLiteral> remainingLiterals = new ArrayList<GdlLiteral>();
    for(GdlLiteral literal : rule.getBody())
      if(!condensationSet.contains(literal))
        remainingLiterals.add(literal);

    remainingLiterals.add(condenserHead);
    GdlRule modifiedRule = GdlPool.getRule(rule.getHead(), remainingLiterals);

    List<GdlRule> newRules = new ArrayList<GdlRule>(2);
    newRules.add(condenserRule);
    newRules.add(modifiedRule);
    return newRules;
View Full Code Here

    //with getting through them in a reasonable amount of time, so we do want to
    //count them. TODO: Not sure if they should be counted in L, though...
    long curRuleHeuristic = assignments * literals;
    //And if we split them up...
    List<GdlRule> newRules = applyCondensation(minSet, rule, sentenceNameSource);
    GdlRule r1 = newRules.get(0), r2 = newRules.get(1);

    //Augment the model
    SentenceDomainModel newModel = augmentModelWithNewForm(model, newRules);

    long a1 = AssignmentsImpl.getNumAssignmentsEstimate(r1,
        SentenceDomainModels.getVarDomains(r1, newModel, VarDomainOpts.INCLUDE_HEAD), checker);
    long a2 = AssignmentsImpl.getNumAssignmentsEstimate(r2,
        SentenceDomainModels.getVarDomains(r2, newModel, VarDomainOpts.INCLUDE_HEAD), checker);
    int l1 = r1.arity(); if(l1 > 1) l1++;
    int l2 = r2.arity(); if(l2 > 1) l2++;

    //Whether we split or not depends on what the two heuristics say
    long newRulesHeuristic = a1 * l1 + a2 * l2;
    return newRulesHeuristic < curRuleHeuristic;
View Full Code Here

TOP

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

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.