Package org.ggp.base.util.propnet.architecture.components

Examples of org.ggp.base.util.propnet.architecture.components.Proposition


          child.addInput(falseComponent);
        }
        output.removeAllOutputs();

        if(!isEssentialProposition(output)) {
          Proposition prop = (Proposition) output;
          //Remove the proposition entirely
          falseComponent.removeOutput(output);
          output.removeInput(falseComponent);
          //Update its location to the trueComponent in our map
          if(components != null) {
              components.put(prop.getName(), falseComponent);
              negations.put(prop.getName(), trueComponent);
          } else {
              pn.removeComponent(output);
          }
        }
      } else if(output instanceof And) {
View Full Code Here


  private static boolean isLegalOrGoalProposition(Component comp) {
    if (!(comp instanceof Proposition)) {
      return false;
    }

    Proposition prop = (Proposition) comp;
    GdlSentence name = prop.getName();
    return name.getName() == GdlPool.LEGAL || name.getName() == GdlPool.GOAL;
  }
View Full Code Here

          child.addInput(trueComponent);
        }
        output.removeAllOutputs();

        if(!isEssentialProposition(output)) {
          Proposition prop = (Proposition) output;
          //Remove the proposition entirely
          trueComponent.removeOutput(output);
          output.removeInput(trueComponent);
          //Update its location to the trueComponent in our map
          if(components != null) {
              components.put(prop.getName(), trueComponent);
              negations.put(prop.getName(), falseComponent);
          } else {
              pn.removeComponent(output);
          }
        }
      } else if(output instanceof Or) {
View Full Code Here

      return false;

    //We're looking for things that would be outputs of "true" or "false",
    //but we would still want to keep as propositions to be read by the
    //state machine
    Proposition prop = (Proposition) component;
    GdlConstant name = prop.getName().getName();

    return name.equals(LEGAL) /*|| name.equals(NEXT)*/ || name.equals(GOAL)
        || name.equals(INIT) || name.equals(TERMINAL);
  }
View Full Code Here

  //TODO: Replace with version using constantChecker only
  //TODO: This can give problematic results if interpreted in
  //the standard way (see test_case_3d)
  private static void setUpInit(Map<GdlSentence, Component> components,
      Constant trueComponent, Constant falseComponent) {
    Proposition initProposition = new Proposition(GdlPool.getProposition(INIT_CAPS));
    for(Entry<GdlSentence, Component> entry : components.entrySet()) {
      //Is this something that will be true?
      if(entry.getValue() == trueComponent) {
        if(entry.getKey().getName().equals(INIT)) {
          //Find the corresponding true sentence
          GdlSentence trueSentence = GdlPool.getRelation(TRUE, entry.getKey().getBody());
          //System.out.println("True sentence from init: " + trueSentence);
          Component trueSentenceComponent = components.get(trueSentence);
          if(trueSentenceComponent.getInputs().isEmpty()) {
            //Case where there is no transition input
            //Add the transition input, connect to init, continue loop
            Transition transition = new Transition();
            //init goes into transition
            transition.addInput(initProposition);
            initProposition.addOutput(transition);
            //transition goes into component
            trueSentenceComponent.addInput(transition);
            transition.addOutput(trueSentenceComponent);
          } else {
            //The transition already exists
View Full Code Here

    for(GdlSentence alwaysTrueSentence : alwaysTrueSentences) {
      //We add the sentence as a constant
      if(alwaysTrueSentence.getName().equals(LEGAL)
          || alwaysTrueSentence.getName().equals(NEXT)
          || alwaysTrueSentence.getName().equals(GOAL)) {
        Proposition prop = new Proposition(alwaysTrueSentence);
        //Attach to true
        trueComponent.addOutput(prop);
        prop.addInput(trueComponent);
        //Still want the same components;
        //we just don't want this to be anonymized
      }
      //Assign as true
      components.put(alwaysTrueSentence, trueComponent);
      negations.put(alwaysTrueSentence, falseComponent);
      continue;
    }

    //For does/true, make nodes based on input/base, if available
    if(usingInput && form.getName().equals(DOES)) {
      //Add only those propositions for which there is a corresponding INPUT
      SentenceForm inputForm = form.withName(INPUT);
      for (GdlSentence inputSentence : constantChecker.getTrueSentences(inputForm)) {
        GdlSentence doesSentence = GdlPool.getRelation(DOES, inputSentence.getBody());
        Proposition prop = new Proposition(doesSentence);
        components.put(doesSentence, prop);
      }
      return;
    }
    if(usingBase && form.getName().equals(TRUE)) {
      SentenceForm baseForm = form.withName(BASE);
      for (GdlSentence baseSentence : constantChecker.getTrueSentences(baseForm)) {
        GdlSentence trueSentence = GdlPool.getRelation(TRUE, baseSentence.getBody());
        Proposition prop = new Proposition(trueSentence);
        components.put(trueSentence, prop);
      }
      return;
    }

    Map<GdlSentence, Set<Component>> inputsToOr = new HashMap<GdlSentence, Set<Component>>();
    for(GdlRule rule : rules) {
      Assignments assignments = AssignmentsFactory.getAssignmentsForRule(rule, model, functionInfoMap, completedSentenceFormValues);

      //Calculate vars in live (non-constant, non-distinct) conjuncts
      Set<GdlVariable> varsInLiveConjuncts = getVarsInLiveConjuncts(rule, constantChecker.getConstantSentenceForms());
      varsInLiveConjuncts.addAll(GdlUtils.getVariables(rule.getHead()));
      Set<GdlVariable> varsInRule = new HashSet<GdlVariable>(GdlUtils.getVariables(rule));
      boolean preventDuplicatesFromConstants =
        (varsInRule.size() > varsInLiveConjuncts.size());

      //Do we just pass those to the Assignments class in that case?
      for(AssignmentIterator asnItr = assignments.getIterator(); asnItr.hasNext(); ) {
        Map<GdlVariable, GdlConstant> assignment = asnItr.next();
        if(assignment == null) continue; //Not sure if this will ever happen

        ConcurrencyUtils.checkForInterruption();

        GdlSentence sentence = CommonTransforms.replaceVariables(rule.getHead(), assignment);

        //Now we go through the conjuncts as before, but we wait to hook them up.
        List<Component> componentsToConnect = new ArrayList<Component>(rule.arity());
        for(GdlLiteral literal : rule.getBody()) {
          if(literal instanceof GdlSentence) {
            //Get the sentence post-substitutions
            GdlSentence transformed = CommonTransforms.replaceVariables((GdlSentence) literal, assignment);

            //Check for constant-ness
            SentenceForm conjunctForm = model.getSentenceForm(transformed);
            if(constantChecker.isConstantForm(conjunctForm)) {
              if(!constantChecker.isTrueConstant(transformed)) {
                List<GdlVariable> varsToChange = getVarsInConjunct(literal);
                asnItr.changeOneInNext(varsToChange, assignment);
                componentsToConnect.add(null);
              }
              continue;
            }

            Component conj = components.get(transformed);
            //If conj is null and this is a sentence form we're still handling,
            //hook up to a temporary sentence form
            if(conj == null) {
              conj = temporaryComponents.get(transformed);
            }
            if(conj == null && SentenceModelUtils.inSentenceFormGroup(transformed, recursionForms)) {
              //Set up a temporary component
              Proposition tempProp = new Proposition(transformed);
              temporaryComponents.put(transformed, tempProp);
              conj = tempProp;
            }
            //Let's say this is false; we want to backtrack and change the right variable
            if(conj == null || isThisConstant(conj, falseComponent)) {
              List<GdlVariable> varsInConjunct = getVarsInConjunct(literal);
              asnItr.changeOneInNext(varsInConjunct, assignment);
              //These last steps just speed up the process
              //telling the factory to ignore this rule
              componentsToConnect.add(null);
              continue; //look at all the other restrictions we'll face
            }

            componentsToConnect.add(conj);
          } else if(literal instanceof GdlNot) {
            //Add a "not" if necessary
            //Look up the negation
            GdlSentence internal = (GdlSentence) ((GdlNot) literal).getBody();
            GdlSentence transformed = CommonTransforms.replaceVariables(internal, assignment);

            //Add constant-checking here...
            SentenceForm conjunctForm = model.getSentenceForm(transformed);
            if(constantChecker.isConstantForm(conjunctForm)) {
              if(constantChecker.isTrueConstant(transformed)) {
                List<GdlVariable> varsToChange = getVarsInConjunct(literal);
                asnItr.changeOneInNext(varsToChange, assignment);
                componentsToConnect.add(null);
              }
              continue;
            }

            Component conj = negations.get(transformed);
            if(isThisConstant(conj, falseComponent)) {
              //We need to change one of the variables inside
              List<GdlVariable> varsInConjunct = getVarsInConjunct(internal);
              asnItr.changeOneInNext(varsInConjunct, assignment);
              //ignore this rule
              componentsToConnect.add(null);
              continue;
            }
            if(conj == null) {
              conj = temporaryNegations.get(transformed);
            }
            //Check for the recursive case:
            if(conj == null && SentenceModelUtils.inSentenceFormGroup(transformed, recursionForms)) {
              Component positive = components.get(transformed);
              if(positive == null) {
                positive = temporaryComponents.get(transformed);
              }
              if(positive == null) {
                //Make the temporary proposition
                Proposition tempProp = new Proposition(transformed);
                temporaryComponents.put(transformed, tempProp);
                positive = tempProp;
              }
              //Positive is now set and in temporaryComponents
              //Evidently, wasn't in temporaryNegations
              //So we add the "not" gate and set it in temporaryNegations
              Not not = new Not();
              //Add positive as input
              not.addInput(positive);
              positive.addOutput(not);
              temporaryNegations.put(transformed, not);
              conj = not;
            }
            if(conj == null) {
              Component positive = components.get(transformed);
              //No, because then that will be attached to "negations", which could be bad

              if(positive == null) {
                //So the positive can't possibly be true (unless we have recurstion)
                //and so this would be positive always
                //We want to just skip this conjunct, so we continue to the next

                continue; //to the next conjunct
              }

              //Check if we're sharing a component with another sentence with a negation
              //(i.e. look for "nots" in our outputs and use those instead)
              Not existingNotOutput = getNotOutput(positive);
              if(existingNotOutput != null) {
                componentsToConnect.add(existingNotOutput);
                negations.put(transformed, existingNotOutput);
                continue; //to the next conjunct
              }

              Not not = new Not();
              not.addInput(positive);
              positive.addOutput(not);
              negations.put(transformed, not);
              conj = not;
            }
            componentsToConnect.add(conj);
          } else if(literal instanceof GdlDistinct) {
            //Already handled; ignore
          } else {
            throw new RuntimeException("Unwanted GdlLiteral type");
          }
        }
        if(!componentsToConnect.contains(null)) {
          //Connect all the components
          Proposition andComponent = new Proposition(TEMP);

          andify(componentsToConnect, andComponent, trueComponent);
          if(!isThisConstant(andComponent, falseComponent)) {
            if(!inputsToOr.containsKey(sentence))
              inputsToOr.put(sentence, new HashSet<Component>());
            inputsToOr.get(sentence).add(andComponent);
            //We'll want to make sure at least one of the non-constant
            //components is changing
            if(preventDuplicatesFromConstants) {
              asnItr.changeOneInNext(varsInLiveConjuncts, assignment);
            }
          }
        }
      }
    }

    //At the end, we hook up the conjuncts
    for(Entry<GdlSentence, Set<Component>> entry : inputsToOr.entrySet()) {
      ConcurrencyUtils.checkForInterruption();

      GdlSentence sentence = entry.getKey();
      Set<Component> inputs = entry.getValue();
      Set<Component> realInputs = new HashSet<Component>();
      for(Component input : inputs) {
        if(input instanceof Constant || input.getInputs().size() == 0) {
          realInputs.add(input);
        } else {
          realInputs.add(input.getSingleInput());
          input.getSingleInput().removeOutput(input);
          input.removeAllInputs();
        }
      }

      Proposition prop = new Proposition(sentence);
      orify(realInputs, prop, falseComponent);
      components.put(sentence, prop);
    }

    //True/does sentences will have none of these rules, but
    //still need to exist/"float"
    //We'll do this if we haven't used base/input as a basis
    if(form.getName().equals(TRUE)
        || form.getName().equals(DOES)) {
      for(GdlSentence sentence : model.getDomain(form)) {
        ConcurrencyUtils.checkForInterruption();

        Proposition prop = new Proposition(sentence);
        components.put(sentence, prop);
      }
    }

  }
View Full Code Here

    Stack<Pair<Component, Type>> toAdd = new Stack<Pair<Component, Type>>();

    //It's easier here if we get just the one-way version of the map
    Map<Proposition, Proposition> legalsToInputs = Maps.newHashMap();
    for (Proposition legalProp : Iterables.concat(pn.getLegalPropositions().values())) {
      Proposition inputProp = pn.getLegalInputMap().get(legalProp);
      if (inputProp != null) {
        legalsToInputs.put(legalProp, inputProp);
      }
    }

    //All constants have their values
    for (Component c : pn.getComponents()) {
      ConcurrencyUtils.checkForInterruption();
      if (c instanceof Constant) {
        if (c.getValue()) {
          toAdd.add(Pair.of(c, Type.TRUE));
        } else {
          toAdd.add(Pair.of(c, Type.FALSE));
        }
      }
    }

    //Every input can be false (we assume that no player will have just one move allowed all game)
        for(Proposition p : pn.getInputPropositions().values()) {
          toAdd.add(Pair.of((Component) p, Type.FALSE));
        }
      //Every base with "init" can be true, every base without "init" can be false
      for(Proposition baseProp : pn.getBasePropositions().values()) {
            if (basesTrueByInit.contains(baseProp)) {
              toAdd.add(Pair.of((Component) baseProp, Type.TRUE));
            } else {
              toAdd.add(Pair.of((Component) baseProp, Type.FALSE));
            }
      }
      //Keep INIT, for those who use it
      Proposition initProposition = pn.getInitProposition();
      toAdd.add(Pair.of((Component) initProposition, Type.BOTH));

      while (!toAdd.isEmpty()) {
      ConcurrencyUtils.checkForInterruption();
        Pair<Component, Type> curEntry = toAdd.pop();
        Component curComp = curEntry.left;
        Type newInputType = curEntry.right;
        Type oldType = reachability.get(curComp);
        if (oldType == null) {
          oldType = Type.NEITHER;
        }

        //We want to send only the new addition to our children,
        //for consistency in our parent-true and parent-false
        //counts.
        //Make sure we don't double-apply a type.

        Type typeToAdd = Type.NEITHER; // Any new values that we discover we can have this iteration.
        if (curComp instanceof Proposition) {
          typeToAdd = newInputType;
        } else if (curComp instanceof Transition) {
          typeToAdd = newInputType;
        } else if (curComp instanceof Constant) {
          typeToAdd = newInputType;
        } else if (curComp instanceof Not) {
          typeToAdd = newInputType.opposite();
        } else if (curComp instanceof And) {
          if (newInputType.hasTrue) {
            numTrueInputs.add(curComp);
            if (numTrueInputs.count(curComp) == curComp.getInputs().size()) {
              typeToAdd = Type.TRUE;
            }
          }
          if (newInputType.hasFalse) {
            typeToAdd = typeToAdd.with(Type.FALSE);
          }
        } else if (curComp instanceof Or) {
          if (newInputType.hasFalse) {
            numFalseInputs.add(curComp);
            if (numFalseInputs.count(curComp) == curComp.getInputs().size()) {
              typeToAdd = Type.FALSE;
            }
          }
          if (newInputType.hasTrue) {
            typeToAdd = typeToAdd.with(Type.TRUE);
          }
        } else {
          throw new RuntimeException("Unhandled component type " + curComp.getClass());
        }

        if (oldType.includes(typeToAdd)) {
          //We don't know anything new about curComp
          continue;
        }
        reachability.put(curComp, typeToAdd.with(oldType));
        typeToAdd = typeToAdd.minus(oldType);
        if (typeToAdd == Type.NEITHER) {
          throw new RuntimeException("Something's messed up here");
        }

        //Add all our children to the stack
        for (Component output : curComp.getOutputs()) {
          toAdd.add(Pair.of(output, typeToAdd));
        }
      if (legalsToInputs.containsKey(curComp)) {
        Proposition inputProp = legalsToInputs.get(curComp);
        if (inputProp == null) {
          throw new IllegalStateException();
        }
        toAdd.add(Pair.of((Component) inputProp, typeToAdd));
      }
View Full Code Here

TOP

Related Classes of org.ggp.base.util.propnet.architecture.components.Proposition

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.