Example #1
0
def _gen_parameter_solutions(substitution, env, parameter_idNodes):
    # TODO: don't copy. avoid rpython ListChangeUnallowed exception
    parameter_idNodes = parameter_idNodes[:]
    if isinstance(substitution, APreconditionSubstitution):
        predicate = substitution.children[0]
        # TODO: RYPTHON AssertionError domain_generator_1
        for dic in compute_constrained_domains(predicate, env, parameter_idNodes):
            yield dic
    # TODO:(#ISSUE 13) maybe more guesses elif...
    else: # no guess possible, try all values (third-arg None cause an enum of all values)
        for dic in gen_all_values(env, parameter_idNodes):
            yield dic  
Example #2
0
def compute_constrained_domains(predicate, env, varList):
    assert isinstance(predicate, Predicate)
    assert isinstance(varList, list)
    for idNode in varList:
        assert isinstance(idNode, AIdentifierExpression)
    # check which kind of strategy: 3 cases

    # case 1: Using the PyB constraint solver
    # check if a solution-set (variable domain) is computable without a external contraint solver
    if USE_PYB_CONSTRAINT_SOLVER:
        try:
            generator = _compute_using_pyB_solver(predicate, env, varList)
            for d in generator:
                yield d
            raise StopIteration()
        except SpecialCaseEnumerationFailedException:
            # print "\033[1m\033[91mDEBUG\033[00m: PyB constraint solver failed. Case not implemented"
            raise StopIteration()

    # case 2: Using a external constraint solver
    # TODO: Handle OverflowError, print Error message and go on
    try:
        if USE_RPYTHON_CODE:  # Using external constraint solver not supported by RPython yet
            raise ImportError()
        if PRINT_WARNINGS:
            print "\033[1m\033[91mWARNING\033[00m: External constraint solver called. Caused by: %s" % pretty_print(
                predicate
            )
        iterator = compute_using_external_solver(predicate, env, varList)
        # constraint solving succeed.
        for d in iterator:
            yield d
        raise StopIteration()
    # TODO: Remove this case when case 1 used brute force enum on default for unconstraint vars
    # case 3: Using brute force enumeration
    except (ConstraintNotImplementedException, ImportError):
        if PRINT_WARNINGS:
            print "\033[1m\033[91mWARNING\033[00m: Brute force enumeration caused by: %s! enumerating: %s" % (
                pretty_print(predicate),
                [v.idName for v in varList],
            )
        # TODO: maybe case 2 was able to constrain the domain of some variables but not all
        # this computation is thrown away in this step. This makes no senese. Fix it!
        # constraint solving failed, enumerate all values (may cause a pyB fail or a timeout)
        generator = gen_all_values(env, varList)
        for d in generator:
            yield d
        raise StopIteration()