Пример #1
0
 def removeUnreachableBasesAndInputs(cls, pn, basesTrueByInit):
     """ generated source for method removeUnreachableBasesAndInputs """
     reachability = Maps.newHashMap()
     numTrueInputs = HashMultiset.create()
     numFalseInputs = HashMultiset.create()
     toAdd = Stack()
     legalsToInputs = Maps.newHashMap()
     for legalProp in Iterables.concat(pn.getLegalPropositions().values()):
         if inputProp != None:
             legalsToInputs.put(legalProp, inputProp)
     for c in pn.getComponents():
         ConcurrencyUtils.checkForInterruption()
         if isinstance(c, (Constant, )):
             if c.getValue():
                 toAdd.add(Pair.of(c, cls.Type.TRUE))
             else:
                 toAdd.add(Pair.of(c, cls.Type.FALSE))
     for p in pn.getInputPropositions().values():
         toAdd.add(Pair.of(p, cls.Type.FALSE))
     for baseProp in pn.getBasePropositions().values():
         if basesTrueByInit.contains(baseProp):
             toAdd.add(Pair.of(baseProp, cls.Type.TRUE))
         else:
             toAdd.add(Pair.of(baseProp, cls.Type.FALSE))
     initProposition = pn.getInitProposition()
     toAdd.add(Pair.of(initProposition, cls.Type.BOTH))
     while not toAdd.isEmpty():
         ConcurrencyUtils.checkForInterruption()
         if oldType == None:
             oldType = cls.Type.NEITHER
         if isinstance(curComp, (Proposition, )):
             typeToAdd = newInputType
         elif isinstance(curComp, (Transition, )):
             typeToAdd = newInputType
         elif isinstance(curComp, (Constant, )):
             typeToAdd = newInputType
         elif isinstance(curComp, (Not, )):
             typeToAdd = newInputType.opposite()
         elif isinstance(curComp, (And, )):
             if newInputType.hasTrue:
                 numTrueInputs.add(curComp)
                 if numTrueInputs.count(curComp) == curComp.getInputs().size():
                     typeToAdd = cls.Type.TRUE
             if newInputType.hasFalse:
                 typeToAdd = typeToAdd.with_(cls.Type.FALSE)
         elif isinstance(curComp, (Or, )):
             if newInputType.hasFalse:
                 numFalseInputs.add(curComp)
                 if numFalseInputs.count(curComp) == curComp.getInputs().size():
                     typeToAdd = cls.Type.FALSE
             if newInputType.hasTrue:
                 typeToAdd = typeToAdd.with_(cls.Type.TRUE)
         else:
             raise RuntimeException("Unhandled component type " + curComp.__class__)
         if oldType.includes(typeToAdd):
             continue 
         reachability.put(curComp, typeToAdd.with_(oldType))
         typeToAdd = typeToAdd.minus(oldType)
         if typeToAdd == cls.Type.NEITHER:
             raise RuntimeException("Something's messed up here")
         for output in curComp.getOutputs():
             toAdd.add(Pair.of(output, typeToAdd))
         if legalsToInputs.containsKey(curComp):
             if inputProp == None:
                 raise IllegalStateException()
             toAdd.add(Pair.of(inputProp, typeToAdd))
     trueConst = Constant(True)
     falseConst = Constant(False)
     pn.addComponent(trueConst)
     pn.addComponent(falseConst)
     for entry in reachability.entrySet():
         if type_ == cls.Type.TRUE or type_ == cls.Type.FALSE:
             if isinstance(c, (Constant, )):
                 continue 
             for input in c.getInputs():
                 input.removeOutput(c)
             c.removeAllInputs()
             if type_ == cls.Type.TRUE ^ (isinstance(c, (Not, ))):
                 c.addInput(trueConst)
                 trueConst.addOutput(c)
             else:
                 c.addInput(falseConst)
                 falseConst.addOutput(c)
     cls.optimizeAwayTrueAndFalse(pn, trueConst, falseConst)
Пример #2
0
 def create_0(cls, description, verbose):
     """ generated source for method create_0 """
     print "Building propnet..."
     startTime = System.currentTimeMillis()
     description = GdlCleaner.run(description)
     description = DeORer.run(description)
     description = VariableConstrainer.replaceFunctionValuedVariables(description)
     description = Relationizer.run(description)
     description = CondensationIsolator.run(description)
     if verbose:
         for gdl in description:
             print gdl
     # We want to start with a rule graph and follow the rule graph.
     # Start by finding general information about the game
     model = SentenceDomainModelFactory.createWithCartesianDomains(description)
     # Restrict domains to values that could actually come up in rules.
     # See chinesecheckers4's "count" relation for an example of why this
     # could be useful.
     model = SentenceDomainModelOptimizer.restrictDomainsToUsefulValues(model)
     if verbose:
         print "Setting constants..."
     constantChecker = ConstantCheckerFactory.createWithForwardChaining(model)
     if verbose:
         print "Done setting constants"
     sentenceFormNames = SentenceForms.getNames(model.getSentenceForms())
     usingBase = sentenceFormNames.contains("base")
     usingInput = sentenceFormNames.contains("input")
     # For now, we're going to build this to work on those with a
     # particular restriction on the dependency graph:
     # Recursive loops may only contain one sentence form.
     # This describes most games, but not all legal games.
     dependencyGraph = model.getDependencyGraph()
     if verbose:
         print "Computing topological ordering... ",
         System.out.flush()
     ConcurrencyUtils.checkForInterruption()
     topologicalOrdering = getTopologicalOrdering(model.getSentenceForms(), dependencyGraph, usingBase, usingInput)
     if verbose:
         print "done"
     roles = Role.computeRoles(description)
     components = HashMap()
     negations = HashMap()
     trueComponent = Constant(True)
     falseComponent = Constant(False)
     functionInfoMap = HashMap()
     completedSentenceFormValues = HashMap()
     for form in topologicalOrdering:
         ConcurrencyUtils.checkForInterruption()
         if verbose:
             print "Adding sentence form " + form,
             System.out.flush()
         if constantChecker.isConstantForm(form):
             if verbose:
                 print " (constant)"
             # Only add it if it's important
             if form.__name__ == cls.LEGAL or form.__name__ == cls.GOAL or form.__name__ == cls.INIT:
                 # Add it
                 for trueSentence in constantChecker.getTrueSentences(form):
                     trueProp.addInput(trueComponent)
                     trueComponent.addOutput(trueProp)
                     components.put(trueSentence, trueComponent)
             if verbose:
                 print "Checking whether " + form + " is a functional constant..."
             addConstantsToFunctionInfo(form, constantChecker, functionInfoMap)
             addFormToCompletedValues(form, completedSentenceFormValues, constantChecker)
             continue 
         if verbose:
             print 
         # TODO: Adjust "recursive forms" appropriately
         # Add a temporary sentence form thingy? ...
         addSentenceForm(form, model, components, negations, trueComponent, falseComponent, usingBase, usingInput, Collections.singleton(form), temporaryComponents, temporaryNegations, functionInfoMap, constantChecker, completedSentenceFormValues)
         # TODO: Pass these over groups of multiple sentence forms
         if verbose and not temporaryComponents.isEmpty():
             print "Processing temporary components..."
         processTemporaryComponents(temporaryComponents, temporaryNegations, components, negations, trueComponent, falseComponent)
         addFormToCompletedValues(form, completedSentenceFormValues, components)
         # if(verbose)
         # TODO: Add this, but with the correct total number of components (not just Propositions)
         # print "  "+completedSentenceFormValues.get(form).size() + " components added";
     # Connect "next" to "true"
     if verbose:
         print "Adding transitions..."
     addTransitions(components)
     # Set up "init" proposition
     if verbose:
         print "Setting up 'init' proposition..."
     setUpInit(components, trueComponent, falseComponent)
     # Now we can safely...
     removeUselessBasePropositions(components, negations, trueComponent, falseComponent)
     if verbose:
         print "Creating component set..."
     componentSet = HashSet(components.values())
     # Try saving some memory here...
     components = None
     negations = None
     completeComponentSet(componentSet)
     ConcurrencyUtils.checkForInterruption()
     if verbose:
         print "Initializing propnet object..."
     # Make it look the same as the PropNetFactory results, until we decide
     # how we want it to look
     normalizePropositions(componentSet)
     propnet = PropNet(roles, componentSet)
     if verbose:
         print "Done setting up propnet; took " + (System.currentTimeMillis() - startTime) + "ms, has " + len(componentSet) + " components and " + propnet.getNumLinks() + " links"
         print "Propnet has " + propnet.getNumAnds() + " ands; " + propnet.getNumOrs() + " ors; " + propnet.getNumNots() + " nots"
     # print propnet;
     return propnet