def main(cls, args): """ generated source for method main """ if not args[0].endsWith(".kif") or len(args): print "Please enter the path of a .kif file as an argument." return filename = args[0] theGame = Game.createEphemeralGame(Game.preprocessRulesheet(FileUtils.readFileAsString(File(filename)))) if theGame.getRules() == None or theGame.getRules().size() == 0: System.err.println("Problem reading the file " + filename + " or parsing the GDL.") return try: StaticValidator().checkValidity(theGame) except ValidatorException as e: System.err.println("GDL validation error: " + e.__str__()) return transformedDescription = VariableConstrainer.replaceFunctionValuedVariables(theGame.getRules()) newFilename = filename.substring(0, filename.lastIndexOf(".kif")) + "_VARCONST.kif" try: for gdl in transformedDescription: out.write(gdl.__str__()) out.newLine() out.close() except IOException as e: System.err.println("There was an error writing the translated GDL file " + newFilename + ".") e.printStackTrace()
def __init__(self, description): """ generated source for method __init__ """ description = GdlCleaner.run(description) description = DeORer.run(description) description = VariableConstrainer.replaceFunctionValuedVariables(description) # First we use a sentence model to get the relevant sentence forms model = SentenceDomainModelFactory.createWithCartesianDomains(description) self.formsControlledByFlow = HashSet() self.formsControlledByFlow.addAll(model.getIndependentSentenceForms()) self.formsControlledByFlow.removeAll(model.getConstantSentenceForms()) self.constantForms = model.getConstantSentenceForms() self.constantChecker = ConstantCheckerFactory.createWithForwardChaining(model) # Figure out which of these sentences are true at each stage solveTurns(model)
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