Beispiel #1
0
def validate_arg(instruction,
                 arg,
                 validator,
                 modifier=lambda x: x,
                 required=True,
                 wrap_exception=True):
    if not arg in instruction:
        if required:
            raise VentureException('missing_argument',
                                   'Sivm instruction "{}" is missing '
                                   'the "{}" argument'.format(
                                       instruction['instruction'], arg),
                                   argument=arg)
        return None
    val = instruction[arg]
    try:
        val = validator(val)
    except VentureException as e:
        if e.exception == 'parse' and wrap_exception:
            raise VentureException('invalid_argument',
                                   'Invalid argument {} = {}. {}'.format(
                                       arg, val, str(e)),
                                   argument=arg)
        raise
    return modifier(val)
Beispiel #2
0
 def getConstrainableNode(self, node):
     candidate = self.getOutermostNonReferenceNode(node)
     if isConstantNode(candidate):
         raise VentureException("evaluation",
                                "Cannot constrain a constant value.",
                                address=node.address)
     if not self.pspAt(candidate).isRandom():
         raise VentureException("evaluation",
                                "Cannot constrain a deterministic value.",
                                address=node.address)
     return candidate
Beispiel #3
0
 def f(self,*args):
     try:
         data = json.dumps(args)
         headers = {'content-type':'application/json'}
         r = requests.post(self.base_url + name, data=data, headers=headers)
         if r.status_code == 200:
             return r.json()
         else:
             raise VentureException.from_json_object(r.json())
     except Exception as e:
         raise VentureException('fatal',str(e))
Beispiel #4
0
 def f():
     try:
         args = self._get_json()
         ret_value = obj_function(*args)
         return self._json_response(ret_value,200)
     except VentureException as e:
         print e
         return self._json_response(e.to_json_object(),500)
     except Exception as e:
         ve = VentureException('fatal',str(e))
         print ve
         return self._json_response(ve.to_json_object(),500)
Beispiel #5
0
def validate_instruction(instruction, implemented_instructions):
    try:
        instruction_type = instruction['instruction']
    except:
        raise VentureException(
            'malformed_instruction',
            'Sivm instruction is missing the "instruction" key.')
    if instruction_type not in implemented_instructions:
        raise VentureException(
            'unrecognized_instruction',
            'The "{}" instruction is not supported.'.format(instruction_type))
    return instruction
Beispiel #6
0
def evalFamily(trace, address, exp, env, scaffold,
               shouldRestore, omegaDB, gradients):
  if e.isVariable(exp):
    try:
      sourceNode = env.findSymbol(exp)
    except VentureError as err:
      import sys
      info = sys.exc_info()
      raise VentureException("evaluation", err.message, address=address), \
        None, info[2]
    weight = regen(trace, sourceNode, scaffold,
                   shouldRestore, omegaDB, gradients)
    return (weight, trace.createLookupNode(address, sourceNode))
  elif e.isSelfEvaluating(exp):
    return (0, trace.createConstantNode(address, exp))
  elif e.isQuotation(exp):
    return (0, trace.createConstantNode(address, e.textOfQuotation(exp)))
  else:
    weight = 0
    nodes = []
    for index, subexp in enumerate(exp):
      new_address = addr.extend(address, index)
      w, n = evalFamily(trace, new_address, subexp, env, scaffold,
                        shouldRestore, omegaDB, gradients)
      weight += w
      nodes.append(n)

    (requestNode, outputNode) = \
      trace.createApplicationNodes(address, nodes[0], nodes[1:], env)
    try:
      weight += apply(trace, requestNode, outputNode, scaffold,
                      shouldRestore, omegaDB, gradients)
    except VentureNestedRiplMethodError as err:
      # This is a hack to allow errors raised by inference SP actions
      # that are ripl actions to blame the address of the maker of the
      # action rather than the current address, which is the
      # application of that action (which is where the mistake is
      # detected).
      import sys
      info = sys.exc_info()
      raise VentureException("evaluation", err.message, address=err.addr,
                             cause=err), None, info[2]
    except VentureException:
      raise # Avoid rewrapping with the below
    except Exception as err:
      import sys
      info = sys.exc_info()
      raise VentureException("evaluation", err.message, address=address,
                             cause=err), None, info[2]
    return ensure_python_float(weight), outputNode
Beispiel #7
0
def eval(address, exp, env, rng):
    # The exact parallel to venture.lite.regen.eval would be to return a
    # Node, but since the address will always be the input address,
    # might as well just return the value.
    if e.isVariable(exp):
        try:
            value = env.findSymbol(exp).value
        except VentureError as err:
            import sys
            info = sys.exc_info()
            raise VentureException("evaluation", err.message,
                                   address=address), None, info[2]
        return value
    elif e.isSelfEvaluating(exp):
        return node.normalize(exp)
    elif e.isQuotation(exp):
        return node.normalize(e.textOfQuotation(exp))
    else:
        nodes = []
        for index, subexp in enumerate(exp):
            addr2 = addr.extend(address, index)
            v = eval(addr2, subexp, env, rng)
            nodes.append(node.Node(addr2, v))

        try:
            val = apply(address, nodes, env, rng)
        except VentureNestedRiplMethodError as err:
            # This is a hack to allow errors raised by inference SP actions
            # that are ripl actions to blame the address of the maker of the
            # action rather than the current address, which is the
            # application of that action (which is where the mistake is
            # detected).
            import sys
            info = sys.exc_info()
            raise VentureException("evaluation",
                                   err.message,
                                   address=err.addr,
                                   cause=err), None, info[2]
        except VentureException:
            raise  # Avoid rewrapping with the below
        except Exception as err:
            import sys
            info = sys.exc_info()
            raise VentureException("evaluation",
                                   err.message,
                                   address=address,
                                   cause=err), None, info[2]
        return val
Beispiel #8
0
def parse_instruction(string, languages=None):
    ls = parse_instructions(string, languages)
    if len(ls) != 1:
        raise VentureException('text_parse',
            "Expected a single instruction.  String:\n'%s'\nParse:\n%s"
            % (string, ls))
    return ls[0]
Beispiel #9
0
 def set_mode(self, mode):
     if mode in self.parsers:
         self.mode = mode
     else:
         raise VentureException(
             'invalid_mode',
             "Mode {} is not implemented by this RIPL".format(mode))
Beispiel #10
0
def parse_instruction(string, languages=None):
    ls = parse_instructions(string, languages)
    if len(ls) != 1:
        msg = 'Expected %s to parse as a single instruction, got %s' % (string,
                                                                        ls)
        raise VentureException('parse', msg)
    return ls[0]
Beispiel #11
0
 def report_raw(self, directiveId):
     if directiveId not in self.directives:
         raise VentureException(
             "invalid_argument",
             "Cannot report raw value of a non-existent directive id",
             argument=directiveId)
     return self.trace.extractRaw(directiveId)
Beispiel #12
0
 def bindInGlobalEnv(self, sym, id):
     try:
         self.globalEnv.addBinding(sym, self.families[id])
     except VentureError as e:
         raise VentureException("invalid_argument",
                                message=e.message,
                                argument="symbol")
Beispiel #13
0
 def registerConstrainedChoice(self, node):
     if node in self.ccs:
         raise VentureException(
             "evaluation",
             "Cannot constrain the same random choice twice.",
             address=node.address)
     self.ccs.add(node)
     self.unregisterRandomChoice(node)
Beispiel #14
0
def validate_value(ob):
    try:
        validate_symbol(ob['type'])  #validate the type
    except Exception as e:
        raise VentureException('parse',
                               'Invalid literal value. {}'.format(e.message),
                               expression_index=[])
    return ob
Beispiel #15
0
 def p_literal_json(self, type, open, value, close):
     t = type.value
     start, end = type.loc
     if t == 'boolean':
         raise VentureException('parse',
                                ('JSON not allowed for %s' % (t, )),
                                text_index=[start, end])
     return ast.locmerge(type, close, {'type': t, 'value': value})
Beispiel #16
0
def expand(exp):
    if v.is_basic_val_of_type("array", exp):
        exp = _canonicalize(exp)
    for macro in macros:
        if macro.applies(exp):
            return macro.expand(exp)
    raise VentureException('parse',
                           "Unrecognizable expression " + str(exp),
                           expression_index=[])
Beispiel #17
0
def LetRecExpand(exp):
    if len(exp) != 3:
        raise VentureException('parse',
                               '"letrec" statement requires 2 arguments',
                               expression_index=[])
    if not isinstance(exp[1], list):
        raise VentureException('parse',
                               '"letrec" first argument must be a list',
                               expression_index=[1])

    n = len(exp[1])
    syms = ['__sym%d__' % i for i in range(n)]
    vals = ['__val%d__' % i for i in range(n)]

    pattern = ['letrec', map(list, zip(syms, vals)), 'body']

    template = ['fix'] + [['quote'] + [syms]] + [['quote'] + [vals]]
    template = ['eval', ['quote', 'body'], template]
    return SyntaxRule(pattern, template).expand(exp)
Beispiel #18
0
def propagateConstraint(trace, node, value):
  if isLookupNode(node): trace.setValueAt(node, value)
  elif isRequestNode(node):
    if not isinstance(trace.pspAt(node), NullRequestPSP):
      raise VentureException("evaluation", "Cannot make requests " \
        "downstream of a node that gets constrained during regen",
        address=node.address)
  else:
    # TODO there may be more cases to ban here.
    # e.g. certain kinds of deterministic coupling through mutation.
    assert isOutputNode(node)
    if trace.pspAt(node).isRandom():
      raise VentureException("evaluation", "Cannot make random choices " \
        "downstream of a node that gets constrained during regen",
        address=node.address)
    # TODO Is it necessary to unincorporate and incorporate here?  If
    # not, why not?
    trace.setValueAt(node, trace.pspAt(node).simulate(trace.argsAt(node)))
  for child in trace.childrenAt(node): propagateConstraint(trace, child, value)
Beispiel #19
0
 def freeze(self, directiveId):
     if directiveId not in self.directives:
         raise VentureException(
             "invalid_argument",
             "Cannot freeze a non-existent directive id.  Valid options are %s"
             % self.directives.keys(),
             argument="directive_id",
             directive_id=directiveId)
     self.trace.freeze(directiveId)
     self._record_directive_frozen(directiveId)
Beispiel #20
0
 def bindInGlobalEnv(self, sym, id):
     if sym in self.env.frame:
         # No problems with overwrites in the untraced setting
         del self.env.frame[sym]
     try:
         self.env.addBinding(sym, node.Node(id, self.results[id]))
     except VentureError as e:
         raise VentureException("invalid_argument",
                                message=e.message,
                                argument="symbol")
Beispiel #21
0
def LetExpand(exp):
    if len(exp) != 3:
        raise VentureException('parse',
                               '"let" statement requires 2 arguments',
                               expression_index=[])
    if not isinstance(exp[1], list):
        raise VentureException('parse',
                               '"let" first argument must be a list',
                               expression_index=[1])

    n = len(exp[1])
    syms = ['__sym%d__' % i for i in range(n)]
    vals = ['__val%d__' % i for i in range(n)]

    pattern = ['let', map(list, zip(syms, vals)), 'body']

    template = 'body'
    for i in reversed(range(n)):
        template = [['lambda', [syms[i]], template], vals[i]]
    return SyntaxRule(pattern, template).expand(exp)
Beispiel #22
0
 def _forgetting_label(self, label):
     if label not in self._label_to_did:
         raise VentureException('invalid_argument',
                                'Label %r does not exist.' % (label, ),
                                argument='label')
     did = self._label_to_did[label]
     assert label == self._did_to_label[did]
     yield self._label_to_did[label]
     assert did == self._label_to_did[label]
     assert did not in self._did_to_label
     del self._label_to_did[label]
def verify(pattern, exp, context):
    """Verifies that the given expression matches the pattern in form."""
    if isinstance(pattern, list):
        if not isinstance(exp, list):
            raise VentureException(
                'parse',
                'Invalid expression in %s -- expected list!' % (context, ),
                expression_index=[])
        if len(exp) != len(pattern):
            raise VentureException(
                'parse',
                'Invalid expression in %s -- expected length %d' %
                (context, len(pattern)),
                expression_index=[])
        for index, (p, e) in enumerate(zip(pattern, exp)):
            try:
                verify(p, e, context)
            except VentureException as err:
                err.data['expression_index'].insert(0, index)
                raise
Beispiel #24
0
 def p_literal_json(self, t, v, c):
     t0 = t.value
     start, end = t.loc
     assert t0[-1] == '<'
     t0 = t0[:-1]
     if t0 == 'boolean':
         # XXX Accumulate parse error.
         raise VentureException('text_parse',
                                ('JSON not allowed for %s' % (t0, )),
                                text_index=[start, end])
     return ast.locmerge(t, c, {'type': t0, 'value': v})
Beispiel #25
0
def validate_symbol(s):
    if not is_valid_symbol(s):
        raise VentureException(
            'parse',
            'Invalid symbol. May only contain letters, digits, and underscores. May not begin with digit.',
            expression_index=[])
    # TODO Figure out where in the lower levels the symbol-as-string
    # representation is expected and change to dict-symbols.
    if isinstance(s, basestring):
        return s.encode('ascii')
    else:
        return s["value"].encode('ascii')
Beispiel #26
0
def apply(address, nodes, env, rng):
    spr = nodes[0].value
    if not isinstance(spr, VentureSPRecord):
        raise VentureException("evaluation",
                               "Cannot apply a non-procedure",
                               address=address)
    req_args = RequestArgs(address, nodes[1:], env, rng.randint(1, 2**31 - 1))
    requests = applyPSP(spr.sp.requestPSP, req_args)
    req_nodes = [evalRequest(req_args, spr, r, rng) for r in requests.esrs]
    assert not requests.lsrs, "The untraced evaluator does not yet support LSRs."
    return applyPSP(
        spr.sp.outputPSP,
        OutputArgs(address, nodes[1:], env, rng.randint(1, 2**31 - 1),
                   req_nodes, requests))
Beispiel #27
0
 def _putting_label(self, label, did):
     if label in self._label_to_did:
         raise VentureException(
             'invalid_argument',
             'Label %r is already assigned to a different directive.' %
             (label, ),
             argument='label')
     assert did not in self._did_to_label, \
         'did %r already has label %r, not %r' % \
         (did, self._did_to_label[did], label)
     yield
     assert label not in self._label_to_did, \
       'Label %r mysteriously appeared in model!' % (label,)
     assert did not in self._did_to_label
     self._label_to_did[label] = did
     self._did_to_label[did] = label
Beispiel #28
0
 def forget(self, directiveId):
     if directiveId not in self.directives:
         raise VentureException(
             "invalid_argument",
             "Cannot forget a non-existent directive id.  Valid options are %s"
             % self.directives.keys(),
             argument="directive_id",
             directive_id=directiveId)
     weight = 0
     directive = self.directives[directiveId]
     if directive[0] == "observe":
         weight += self.trace.unobserve(directiveId)
     self.trace.uneval(directiveId)
     # TODO This may cause a problem in the presence of variable
     # shadowing.  Really, it should remove the binding from the frame
     # into which the directive being forgotten had placed it, rather
     # than the bottom-most frame in which it occurs, as this does.
     if directive[0] == "define": self.trace.unbindInGlobalEnv(directive[1])
     del self.directives[directiveId]
     return weight
Beispiel #29
0
class Semantics(object):
    def __init__(self):
        self.answer = None

    def accept(self):
        assert self.answer is not None
    def parse_failed(self):
        assert self.answer is None
        raise VentureException('text_parse', 'Syntax error!')
    def syntax_error(self, (number, located)):
        # XXX Should not raise here -- should accumulate errors and
        # report them all at the end.
        #
        # XXX Should adapt lemonade to support passing a message, and
        # make the generated parser say which tokens (and, ideally,
        # nonterminals) it was expecting instead.
        text = located.value
        (start, end) = located.loc
        raise VentureException('text_parse',
            ('Syntax error at %s (token %d)' % (repr(text), number)),
            text_index=[start, end])
Beispiel #30
0
def evalRequests(trace, node, scaffold, shouldRestore, omegaDB, gradients):
  assert isRequestNode(node)
  weight = 0
  request = trace.valueAt(node)

  # first evaluate exposed simulation requests (ESRs)
  for esr in request.esrs:
    if not trace.containsSPFamilyAt(node, esr.id):
      if shouldRestore and omegaDB.hasESRParent(trace.spAt(node), esr.id):
        esrParent = omegaDB.getESRParent(trace.spAt(node), esr.id)
        weight += restore(trace, esrParent, scaffold, omegaDB, gradients)
      else:
        address = addr.request(node.address, esr.addr)
        (w, esrParent) = evalFamily(trace, address, esr.exp, esr.env, scaffold,
                                    shouldRestore, omegaDB, gradients)
        weight += w
      if trace.containsSPFamilyAt(node, esr.id):
        # evalFamily already registered a family with this id for the
        # operator being applied here, which means a recursive call to
        # the operator issued a request for the same id.  Currently,
        # the only way for that it happen is for a recursive memmed
        # function to call itself with the same arguments.
        raise VentureException("evaluation", "Recursive mem argument " \
          "loop detected.", address=node.address)
      trace.registerFamilyAt(node, esr.id, esrParent)

    esrParent = trace.spFamilyAt(node, esr.id)
    trace.addESREdge(esrParent, node.outputNode)

  # next evaluate latent simulation requests (LSRs)
  for lsr in request.lsrs:
    if omegaDB.hasLatentDB(trace.spAt(node)):
      latentDB = omegaDB.getLatentDB(trace.spAt(node))
    else:
      latentDB = None
    weight += trace.spAt(node).simulateLatents(trace.argsAt(node), lsr,
                                               shouldRestore, latentDB)

  return ensure_python_float(weight)