Пример #1
0
def describe_part(g, parturi):
    """
    Emit a Python dictionary with a description of a single part,
    used by the second-stage compiler.
    """
    def literal(v):
        v = v.toPython()
        if isinstance(v, str):
            return v
        return "%.16f" % v

    part = {"uri": parturi.toPython()}
    try:
        _, _, template = get_one(g, (parturi, RBMC["kappaTemplate"], None))
    except Exception as e:
        logging.error("%s could not find template" % (parturi, ))
        raise e

    part["template"] = template.toPython()
    for _, _, replace in g.triples((parturi, RBMC["replace"], None)):
        try:
            _, _, token = get_one(g, (replace, RBMC["string"], None))
        except Exception as e:
            logging.error("%s could not find replacement token" % (parturi, ))
            raise e

        try:
            _, _, value = get_one(g, (replace, RBMC["value"], None))
        except Exception as e:
            logging.error("%s could not find value for %s" % (parturi, token))
            raise

        if isinstance(value, Literal):
            part[token.toPython()] = literal(value)
            continue
        if exists(g, (value, SKOS["prefLabel"], None)):
            _, _, label = get_one(g, (value, SKOS["prefLabel"], None))
            part[token.toPython()] = label.toPython()
            continue

        ## if we are here, we are concerned with the states of operators
        data = {}
        replace = value
        _, _, value = get_one(g, (replace, RBMC["value"], None))
        data["value"] = literal(value)
        if exists(g, (replace, RBMC["upstream"], None)):
            _, _, oplist = get_one(g, (replace, RBMC["upstream"], None))
            data["upstream"] = list(reversed(describe_operators(g, oplist)))
        if exists(g, (replace, RBMC["downstream"], None)):
            _, _, oplist = get_one(g, (replace, RBMC["downstream"], None))
            data["downstream"] = describe_operators(g, oplist)

        data["upstream_size"] = len(data.get("upstream", []))
        data["downstream_size"] = len(data.get("downstream", []))
        data["context_size"] = data["upstream_size"] + data[
            "downstream_size"] + 1

        part.setdefault(token.toPython(), []).append(data)

    return add_defaults(g, part)
Пример #2
0
def describe_operators(g, oplist):
    operators = []
    oplist = Collection(g, oplist)
    for desc in oplist:
        state = {}
        if exists(g, (desc, RDF["type"], RBMO["BoundState"])):
            state["bound"] = True
        elif exists(g, (desc, RDF["type"], RBMO["UnboundState"])):
            state["bound"] = False
        if exists(g, (desc, RBMO["stateOf"], None)):
            _, _, operator = get_one(g, (desc, RBMO["stateOf"], None))
            _, _, label = get_one(g, (operator, SKOS["prefLabel"], None))
            state["site"] = label.toPython()
        operators.append(state)
    return operators
Пример #3
0
def mutate_model(model, circuit, database):
    #    logging.info("mutate: circuit is %s", circuit)
    nmodel = Graph()
    nmodel += model

    i = choice(range(len(circuit)))
    #    logging.info("mutate: replacing part %d: %s", i, circuit[i])

    try:
        part = replace_part(nmodel, circuit, i, database)
    except Exception as e:
        dump_graph(model)
        raise e
    ncircuit = circuit.copy()
    ncircuit[i] = part

    ## fixup the "next" linkage for RBS
    if exists(nmodel, (part, GCC["next"], None)):
        _, _, next = get_one(nmodel, (part, GCC["next"], None))
        _, _, label = get_one(nmodel, (circuit[i + 1], GCC["part"], None))
        nmodel.remove((part, GCC["next"], next))
        nmodel.add((part, GCC["next"], label))

#    logging.info("mutate: repacement part is %s", circuit[i])
    return (nmodel, ncircuit)
Пример #4
0
def gen_model(model, database):
    circuit = extract_circuit(model, database)
    proto = extract_proto(model)
    ## the "next" linkage for RBS needs to be fixed up in the model.
    for i in range(len(circuit)):
        part = circuit[i]
        if exists(model, (part, GCC["next"], None)):
            _, _, next = get_one(model, (part, GCC["next"], None))
            _, _, label = get_one(model, (circuit[i + 1], GCC["part"], None))
            model.remove((part, GCC["next"], next))
            model.add((part, GCC["next"], label))
    return (model, circuit)
Пример #5
0
def add_defaults(g, part):
    for _, _, kind in g.triples((URIRef(part["uri"]), RDF["type"], None)):
        for _, _, token in g.triples((kind, RBMC["tokens"], None)):
            try:
                _, _, label = get_one(g, (token, SKOS["prefLabel"], None))
            except Exception as e:
                logging.error("could not find label for %s" % token)
                raise
            if label.toPython() in part:
                continue
            if exists(g, (token, RBMC["default"], None)):
                _, _, default = get_one(g, (token, RBMC["default"], None))
                part[label.toPython()] = default.toPython()
    return part