Beispiel #1
0
def findLowerNeighbours(node, ctx):
    candidates = set()
    for m in ctx.M:
        if m in node.M:
            continue
        x1 = node.M.copy()
        x1.add(m)
        x1 = core.derivM(ctx, x1)
        y1 = core.derivG(ctx, x1)
        if (core.Node(x1, y1) not in candidates):
            candidates.add(core.Node(x1, y1))
    return core.maxGen(candidates)
Beispiel #2
0
 def _from_proto(cls, proto: proto.Message):
     return cls(proto.nonce, [
         core.Node(addr=neighbor.ip,
                   port=neighbor.port,
                   nodeid=core.ID.from_bytes(neighbor.nodeid))
         for neighbor in proto.findNodeResponse.neighbors
     ])
Beispiel #3
0
def add_attribute():
  try:
    current_concept = request.json["old_concept"]
    current_lattice = request.json["old_lattice"]
    attrM = request.json["attrM"]
    attrMI = request.json["attrMI"]

    a = core.Context(current_concept["G"],
        current_concept["M"],
        current_concept["I"])
    b = core.Lattice(
      list(map(lambda x: core.Node(set(x["G"]), set(x["M"])), current_lattice["C"])), 
      set(map(lambda x: (int(x[0]), int(x[1])), current_lattice["E"]))
    )
    start = datetime.now()
    (newCtx, L) = addAttribute.addAttr(a, b, attrM, attrMI)
    end = datetime.now()

    draw_start = datetime.now()
    draw.draw(L, OUT_FILE_NAME)
    draw_end = datetime.now()

    resp = dict({
      'latticeImg': encodedFile(OUT_FILE_NAME),
      'lattice': b.toJson(),
      'concept': a.toJson(),
      'drawTime': (draw_end - draw_start).total_seconds(),
      'addTime': (end - start).total_seconds(),
      'json': json.dumps(dict(G = newCtx.G, M = newCtx.M, I = newCtx.I)) 
    })
    return jsonify(resp)
  except:
    return "Wrong input", 400
Beispiel #4
0
    def __init__(self, addr: str, port: int, constants: core.Constants = None):
        self.constants = constants if constants is not None else core.Constants(
        )
        self.addr = addr
        self.port = port
        self.nodeid = core.ID()

        self.node = core.Node(addr=addr, port=port, nodeid=self.nodeid)
        self.server = protocol.Server(self.nodeid, self.constants)
Beispiel #5
0
    async def listen(self, addr, port):
        loop = asyncio.get_running_loop()
        local_addr = (addr, port)

        self.node = core.Node(addr=addr, port=port, nodeid=self.nodeid)

        endpoint = loop.create_datagram_endpoint(
            lambda: Protocol(self.table, self.node, self.received_rpc),
            local_addr=local_addr)
        self.transport, self.protocol = await endpoint
Beispiel #6
0
def addAttr(ctx, lattice, attrM, attrMI):
    if (len(attrMI) != len(ctx.G)):
        raise Exception("there should be same number of objects parameters")

    newCtx = copy.deepcopy(ctx)
    updateCtx(newCtx, attrM, attrMI)
    k = core.derivG(newCtx, core.derivM(newCtx, set({attrM})))
    kk = core.derivM(newCtx, set({attrM}))

    founded = False
    for n in lattice.C:
        if n.G == kk:
            founded = True
            break
    if founded:
        for n in range(len(lattice.C)):
            if kk.intersection(lattice.C[n].G) == lattice.C[n].G:
                lattice.updateCbyIdx(
                    n, core.Node(lattice.C[n].G, lattice.C[n].M.union(k),
                                 True))
        updateCtx(ctx, attrM, attrMI)
        return (ctx, lattice)

    oldL = copy.deepcopy(lattice)
    rootNode = core.Node(core.derivM(ctx, set(ctx.M)), set(ctx.M))

    if kk.intersection(rootNode.G) != rootNode.G:
        n = core.Node(set(), k, True)
        lattice.C.append(n)
        lattice.C = sorted(lattice.C, key=lambda node: node.level)
        lattice.addEdgeIdxs((lattice.C.index(n), lattice.C.index(rootNode)))
    n = core.Node(kk, k, True)
    lattice.C.append(n)
    lattice.C = sorted(lattice.C, key=lambda node: node.level)
    linkConcept(lattice.C.index(n), set({lattice.C.index(rootNode)}), lattice)

    for nIdx in range(len(oldL.C)):
        node = oldL.C[nIdx]
        if node.G.intersection(kk) == node.G:
            M = copy.copy(node.M)
            M = M.union(k)
            lattice.updateCbyIdx(lattice.C.index(node),
                                 core.Node(node.G, M, True))
        inters = node.G.intersection(kk)
        if not (len(inters) == 0 or inters == kk or inters == node.G
                or intersCheck(lattice.C.index(node), inters, lattice)):
            M = copy.copy(node.M)
            M = M.union(k)
            nn = core.Node(inters, M, True)
            lattice.C.append(nn)
            lattice.C = sorted(lattice.C, key=lambda node: node.level)
            linkConcept(lattice.C.index(nn),
                        set({lattice.C.index(n),
                             lattice.C.index(node)}), lattice)

    lattice.C = sorted(lattice.C, key=lambda node: node.level)
    updateCtx(ctx, attrM, attrMI)
    return (ctx, lattice)
Beispiel #7
0
def nextNeighbours(ctx):
    root = core.Node(set(ctx.G), core.derivG(ctx, ctx.G))
    C = [root]
    E = set()
    currentLevel = set({root})
    while len(currentLevel) != 0:
        nextLevel = set()
        for node in currentLevel:
            lowerNeighbours = findLowerNeighbours(node, ctx)
            for lowerNode in lowerNeighbours:
                if lowerNode not in C:
                    C.append(lowerNode)
                    nextLevel.add(lowerNode)
                E.add((lowerNode.__hash__(),
                       node.__hash__()))  # add edge loweNode -> node to E
        currentLevel = nextLevel
    C = sorted(C, key=lambda node: node.level)
    return core.Lattice(C, E)
Beispiel #8
0
 def _parse_node(sender: proto.Node) -> core.Node:
     return core.Node(addr=sender.ip,
                      port=sender.port,
                      nodeid=core.ID.from_bytes(sender.nodeid))