Esempio n. 1
0
def Con_to_Dep(tree):

    ROOT = Node("ROOT")
    VERB = Node("Not filled", parent=ROOT)

    for subtree in tree[0]:

        if subtree.label() == 'VP':

            VERB_temp = VP_subtree(subtree)
            VERB.name = VERB_temp.children[0].name
            while len(VERB_temp.children[0].children) > 0:
                VERB_temp.children[0].children[0].parent = VERB

        elif subtree.label()[:2] == 'NP' or subtree.label() == 'WHNP':
            NSUBJ = Node("NSUBJ", parent=VERB)
            NSUBJ_value = NP_subtree(subtree)[1]
            NSUBJ_value.parent = NSUBJ

        elif subtree.label() == '.':
            PUNCT = Node("PUNCT", parent=VERB)
            PUNCT_value = Node(subtree.label() + " " + subtree[0])
            PUNCT_value.parent = PUNCT

        elif subtree.label() == 'NNP':
            AGENT = Node("AGENT", parent=ROOT)
            AGENT_value = Node(subtree.label() + " " + subtree[0])
            AGENT_value.parent = AGENT

    return ROOT
Esempio n. 2
0
def codegen_agg(ast_dict):
    node = Node("Value", n_type="Value")
    if (isinstance(ast_dict, str) or isinstance(ast_dict, int)
            or isinstance(ast_dict, float)):
        node.val = ast_dict
        return node
    if isinstance(ast_dict, dict):
        agg_type = list(ast_dict.keys())[0]
        agg_type_node = Node(agg_type, n_type="Agg")

        if isinstance(ast_dict[agg_type], dict):
            sec_agg_type = list(ast_dict[agg_type].keys())[0]
            if sec_agg_type in ["distinct"]:
                distinct_type_node = Node("distinct",
                                          parent=agg_type_node,
                                          n_type="Agg")
                node.val = ast_dict[agg_type]["distinct"]
                node.parent = distinct_type_node
            elif sec_agg_type in ["add", "sub", "div", "mul"]:
                sec_agg_node = Node(sec_agg_type,
                                    parent=agg_type_node,
                                    n_type="Agg")
                val1, val2 = ast_dict[agg_type][sec_agg_type]
                codegen_agg(val1).parent = sec_agg_node
                codegen_agg(val2).parent = sec_agg_node
            else:
                raise Exception
        else:
            node.val = ast_dict[agg_type]
            node.parent = agg_type_node

        return agg_type_node
    else:
        print(ast_dict)
Esempio n. 3
0
    def construct_new_mat(i, j, mat):
        # construct a new distance matrix through combining node i and j

        if i not in node_dic:
            node_dic[i] = Node(i, leaf=True, label=i)
        if j not in node_dic:
            node_dic[j] = Node(j, leaf=True, label=j)

        old_mat = mat
        mat = mat.drop(columns=[i, j], axis=1)
        mat = mat.drop(index=[i, j])
        new_dist = []
        for r in mat.index:
            new_dist.append(0.5 * (old_mat[r][i] + old_mat[r][j]))
        new_name = "(" + i + "," + j + ")"
        mat.loc[:, new_name] = new_dist
        new_dist.append(0)
        mat.loc[new_name, :] = new_dist

        # combining node
        node_dic[new_name] = Node(new_name, leaf=False, label="")
        node_dic[i].parent = node_dic[new_name]
        node_dic[j].parent = node_dic[new_name]

        del node_dic[i]
        del node_dic[j]

        return mat
Esempio n. 4
0
def test_recursion_detection():
    """Recursion detection."""
    root = Node("root")
    s0 = Node("sub0", parent=root)
    Node("sub0B", parent=s0)
    s0a = Node("sub0A", parent=s0)

    # try recursion
    assert root.parent is None
    try:
        root.parent = root
    except LoopError as exc:
        eq_(str(exc), "Cannot set parent. Node('/root') cannot be parent of itself.")
        assert root.parent is None
    else:
        assert False

    assert root.parent is None
    try:
        root.parent = s0a
    except LoopError as exc:
        eq_(str(exc), ("Cannot set parent. Node('/root') is parent of Node('/root/sub0/sub0A')."))
        assert root.parent is None
    else:
        assert False

    assert s0.parent is root
    try:
        s0.parent = s0a
    except LoopError as exc:
        eq_(str(exc), ("Cannot set parent. Node('/root/sub0') is parent of Node('/root/sub0/sub0A')."))
        assert s0.parent is root
    else:
        assert False
Esempio n. 5
0
    def duplicate_tree(self, original_node, leaf):

        new_root = Node(original_node.name)
        ancestors = [ancestor.name[-3:] for ancestor in leaf.ancestors]
        ancestors.extend([ancestor.name[-3:] for ancestor in original_node.ancestors])
        ancestors.append(leaf.name[-3:])
        ancestors.append(original_node.root.name[-3:])
        ancestors.append(original_node.name[-3:])

        # iterate immediate children only
        all_nodes = PreOrderIter(original_node, maxlevel=2)  

        for node in all_nodes: 
     
            if 'ALIAS' in node.name:            
                node_id = node.name[-3:] 
                if node_id not in ancestors:
                    if node_id in self.project.nodes:
                        new_node = Node(node.name)
                        new_node.parent = new_root
                    else:
                        new_node = Node('! (Missing Node) >'+node_id)
                        new_node.parent = new_root            
                else:
                    new_node = Node('! RECURSION (from tree duplication) : '+ self.project.nodes[node_id].title + ' >'+node_id)
                    new_node.parent = new_root  
                continue

            if node.parent == original_node:
                """ Recursively apply this function to children's children """
                new_node = self.duplicate_tree(node, leaf)
                new_node.parent = new_root

        return new_root
Esempio n. 6
0
 def build_tree(self, node, dataframe):
     if (node.name == 'root'):
         entropy_dic = {}
         for column in self.data_trained:
             if column != 'class':
                 entropy = self.__get_entropy(column, self.data_trained)
                 entropy_dic[column] = entropy
         min_entropy = min(entropy_dic, key=entropy_dic.get)
         for x in np.unique(self.data_trained[min_entropy]):
             child = Node(min_entropy, value=x)
             child.parent = node
         grouped = dataframe.groupby(min_entropy)
         for child_node in LevelOrderIter(
                 node, filter_=lambda n: n.name != node.name, maxlevel=2):
             dataframe_for_child = grouped.get_group(child_node.value)
             self.build_tree(child_node, dataframe_for_child)
     else:
         entropy_dic = {}
         for column in dataframe:
             if column != 'class':
                 entropy = self.__get_entropy(column, dataframe)
                 entropy_dic[column] = entropy
         min_entropy = min(entropy_dic, key=entropy_dic.get)
         if (entropy_dic[min_entropy] != 0):
             for x in np.unique(self.data_trained[min_entropy]):
                 child = Node(min_entropy, value=x)
                 child.parent = node
             grouped = dataframe.groupby(min_entropy)
             for child_node in LevelOrderIter(
                     node, filter_=lambda n: n.name != node.name,
                     maxlevel=2):
                 dataframe_for_child = grouped.get_group(child_node.value)
                 self.build_tree(child_node, dataframe_for_child)
         else:
             entropy_zero = True
             for key in entropy_dic:
                 entropy_zero = entropy_zero and (entropy_dic[key] == 0)
             if entropy_zero == True:
                 class_values = dataframe['class']
                 child = Node('class', value=class_values.iloc[0])
                 child.parent = node
             else:
                 entropy_dic = {
                     k: v
                     for k, v in entropy_dic.items() if k != 0
                 }
                 min_entropy = min(entropy_dic, key=entropy_dic.get)
                 for x in np.unique(self.data_trained[min_entropy]):
                     child = Node(min_entropy, value=x)
                     child.parent = node
                 grouped = dataframe.groupby(min_entropy)
                 for child_node in LevelOrderIter(
                         node,
                         filter_=lambda n: n.name != node.name,
                         maxlevel=2):
                     dataframe_for_child = grouped.get_group(
                         child_node.value)
                     self.build_tree(child_node, dataframe_for_child)
Esempio n. 7
0
def ast_to_ra(ast_dict, args=None):
    if ast_dict.get("op"):
        res1 = ast_to_ra(ast_dict["op"]["query1"], args)

        res2 = ast_to_ra(ast_dict["op"]["query2"], args)
        c = Node(ast_dict["op"].get("type"), n_type="Op")
        parent1 = Node("Subquery", parent=c, n_type="Table")
        parent2 = Node("Subquery", parent=c, n_type="Table")
        res1.parent = parent1
        res2.parent = parent2
        return c

    root = Node("Project", n_type="Table")
    select_node = codegen_select(ast_dict["select"], args)
    select_node.parent = root
    node = root

    tables, on_list = codegen_from(ast_dict["from"], args)
    where_list = ast_dict.get("where")
    having_list = ast_dict.get("having")
    condition = codegen_where(where_list, on_list, having_list, args)
    if condition:
        node = Node("Selection", parent=node, n_type="Table")
        condition.parent = node

    tables.parent = node
    node = tables
    if ast_dict.get("groupby"):
        curr = Node("Groupby", n_type="Table")
        codegen_select(ast_dict["groupby"], args).parent = curr
        root.parent = curr
        root = root.parent
    if ast_dict.get("orderby"):
        if isinstance(ast_dict["orderby"],
                      dict) and ast_dict["orderby"].get("sort"):
            sort = "Orderby_" + ast_dict["orderby"]["sort"]
        else:
            sort = "Orderby_asc"
        curr = Node(sort, n_type="Table")
        codegen_select(ast_dict["orderby"], args).parent = curr
        root.parent = curr
        root = root.parent

    if ast_dict.get("limit"):
        curr = Node("Limit", n_type="Table")
        val = ast_dict["limit"]
        if isinstance(val, dict):
            val = val["literal"]
        Node("Value", val=val, n_type="Value").parent = curr
        root.parent = curr
        root = root.parent
    return root
Esempio n. 8
0
 def syntaxField_dec(self, subtree, arbol):
     subtree = Node('Field_dec')
     if self.istype(self.tokens[0]):
         self.popToken('field_dec', subtree)
         while True:
             if self.getType(self.tokens[0]) == "ID":
                 self.popToken('field_dec', subtree)
                 if self.isexpected(self.tokens[0], "Delimiter", "["):
                     self.popToken('field_dec', subtree)
                     if self.getType(
                             self.tokens[0]) == "decimal" or self.getType(
                                 self.tokens[0]) == "hexadecimal":
                         self.popToken('field_dec', subtree)
                         if self.isexpected(self.tokens[0], "Delimiter",
                                            "]"):
                             self.popToken('field_dec', subtree)
                         else:
                             self.printExpectedToken("['Delimiter', ']']")
                     else:
                         self.printExpectedToken("<int_literal>")
                 elif self.isexpected(self.tokens[0], "Delimiter", ","):
                     self.popToken('field_dec', subtree)
                     continue
                 elif self.isexpected(self.tokens[0], "Delimiter", ";"):
                     self.popToken('field_dec', subtree)
                     break
                 else:
                     self.printExpectedToken("['Delimiter', ';']")
             else:
                 self.printExpectedToken("['ID', '*']")
     else:
         self.printExpectedToken("<type>")
     subtree.parent = arbol
Esempio n. 9
0
def assemble_folder_tree(items: list, key: str, node_type) -> Node:
    '''
    Assemble a folder tree, return a root node.

    Use `key` argument as a one-side parent <- child relationship
    between folder nodes.

    Using the behavior that the parent node keeps a hard reference
    to the children therefore removing a local ref won't cripple
    the tree and returning just the root node is fine.
    '''
    nodes = []
    parents = {}
    root_node = None

    for item in items:
        assert 'parent' not in item, item
        node = Node(name=item['id'],
                    node_type=node_type,
                    **{k: v
                       for k, v in item.items()})
        nodes.append(node)
        parents[item['id']] = node

    for node in nodes:
        if node.parent_folder_id is None:
            root_node = node
            continue
        node.parent = parents[getattr(node, key)]

    return root_node
Esempio n. 10
0
def generate_subtree(cur_node):
    if cur_node.type.endswith('|'):
        cur_node.value = globals()[cur_node.type[0:-1]]()
    elif cur_node.type.endswith('*'):
        while True:
            cur_node.value = random.choice(
                list(globals()[cur_node.type[0:-1]].items()))[0]

            if cur_node.depth < 2 or cur_node.value not in FLOW:
                break  # prevent control flow nodes from forming too deep in the tree
    else:
        cur_node.value = cur_node.type

    if cur_node.value not in COMBINED:
        return  # terminal or unknown node

    for child_node_type in COMBINED[
            cur_node.
            value]:  # loop through all child node types this node is supposed to have
        num_children = 1

        if child_node_type.endswith('**'):
            num_children = random.randint(
                2, 3) if cur_node.type == 'CONDITIONS' else random.randint(
                    1, 2)  # more conditions than actions
            child_node_type = child_node_type[0:-1]

        for i in range(num_children):
            child_node = Node(name=str(random.random()), type=child_node_type)
            child_node.parent = cur_node
            generate_subtree(child_node)
Esempio n. 11
0
 def check(self, parent_node):
     """Checks if this rule can be applied next by trying all its paths."""
     org_cursor = Global.cursor
     if org_cursor in self.mem:
         Global.cursor = self.mem[org_cursor][1]
         if self.mem[org_cursor][0]:
             self.mem[org_cursor][2].parent = parent_node
         return self.mem[org_cursor][0]
     
     for choice in self.RHS:
         success = True
         cur_node = Node('%s_%s' % (self.name, Global.cursor))
         for code_obj in choice:
             if not (code_obj == 'NULL' or Global.code_objs[code_obj].check(cur_node)):
                 success = False
                 break
         if success:
             cur_node.parent = parent_node
             # Memoize that it succeeded and the save the
             # new cursor and the tree node
             self.mem[org_cursor] = [True, Global.cursor, cur_node]
             return True
         Global.cursor = org_cursor
     
     # Memoize that it failed and the cursor didn't change
     self.mem[org_cursor] = [False, org_cursor]
     return False
Esempio n. 12
0
def wordProblem(w,v):
    flag = 0
    dictList = selectingWords(w)
    dictIter = iter(dictList)
    tree = BKtree(dictIter, distance=editDistanceFast)

    c = numberOfWordsFromDictionary(w)

    words = tree.find(item = w, threshold = 1)

    w = Node(w)
    for el in words:
        el = Node(el, parent = w)

    for ww in words:
        if len(words) >= c:
            return w, 'and', v, 'are not equivalent words'
        newWords = tree.find(item = ww, threshold = 1)
        ww = Node(ww)
        for el in newWords:
            if flag == 0:
                el = Node(el, parent = ww)
                ww.parent = w
                if el.name == v and flag == 0:
                    return (el)
                    flag = 1
        if flag == 1:
            break
        for wo in newWords:
            if wo not in words:
                words.append(wo)
Esempio n. 13
0
 def syntaxBinOp(self, herencia=None):
     new_tree = Node('bin_op')
     if self.isBinOp(self.tokens[0]):
         self.popToken('bin_op', new_tree)
     else:
         self.printExpectedToken("<bin_op>")
     new_tree.parent = herencia
Esempio n. 14
0
def main():
    input_file_path = sys.argv[1]

    if not os.path.isfile(input_file_path):
        print(
            int("File path {} does not exist. Exiting...".format(
                input_file_path)))
        sys.exit()

    input_data = open(input_file_path, 'r').read().splitlines()

    node_list = {}

    for data in input_data:
        space_object_data = data.split(")")
        mass_object = Node(space_object_data[0])
        orbit_object = Node(space_object_data[1])

        if mass_object.name in node_list:
            mass_object = node_list[mass_object.name]
        else:
            node_list[mass_object.name] = mass_object

        if orbit_object.name in node_list:
            orbit_object = node_list[orbit_object.name]
        else:
            node_list[orbit_object.name] = orbit_object

        orbit_object.parent = mass_object

    w = Walker()
    walk_path = w.walk(node_list["YOU"].parent, node_list["SAN"].parent)
    print(len(flatten(walk_path)) - 1)
Esempio n. 15
0
 def syntaxCallout_arg(self, herencia=None):
     new_tree = Node('callout_arg')        
     if self.isStringLiteral(self.tokens[0]):
         self.popToken('callout_arg', new_tree)
     else:
         self.syntaxExpr(new_tree)
     new_tree.parent = herencia
Esempio n. 16
0
def main():
    input_file_path = sys.argv[1]

    if not os.path.isfile(input_file_path):
        print(
            int("File path {} does not exist. Exiting...".format(
                input_file_path)))
        sys.exit()

    input_data = open(input_file_path, 'r').read().splitlines()

    node_list = {}

    for data in input_data:
        space_object_data = data.split(")")
        mass_object = Node(space_object_data[0])
        orbit_object = Node(space_object_data[1])

        if mass_object.name in node_list:
            mass_object = node_list[mass_object.name]
        else:
            node_list[mass_object.name] = mass_object

        if orbit_object.name in node_list:
            orbit_object = node_list[orbit_object.name]
        else:
            node_list[orbit_object.name] = orbit_object

        orbit_object.parent = mass_object

    print(sum([node.depth for node_name, node in node_list.items()]))
Esempio n. 17
0
 def to_tree(self):
     nodes = []
     for index, item in enumerate(self):
         node = Node(name=item)
         nodes.append(node)
         if index > 0:
             parent_index = self.parent(index)
             node.parent = nodes[parent_index]
     return RenderTree(nodes[0], style=ContStyle())
Esempio n. 18
0
 def add_nodes(nodes, parent, child):
     """
     Set parent nodes with corresponding child nodes
     """
     if parent not in nodes:
         nodes[parent] = Node(parent)
     if child not in nodes:
         nodes[child] = Node(child)
         nodes[child].parent = nodes[parent]
def organizeParentPanels(panels, m):
    print('todo: organizing parent panels')
    root = Node('root')
    print('created root node')
    for key in panels:
        n = Node(key)
        print(n)
        parent = panels[key]['parent']
        print(f'node:{key}, parent:={parent}')
        print(f'{type(parent)}, {parent}')
        if not parent:
            print('not parent check')
            n.parent = root
        else:
            pn = find_by_attr(root, parent)
            n.parent = pn
    m['nodes'] = root
    pass
Esempio n. 20
0
    def feature_types(self, print_tree=True):
        '''
        Returns a summary of the feature types represented in 
        the RefLoci database
        
        Parameters
        ----------
        print_tree : bool (default: True)
            If True, prints the result before returning. 

        Returns
        -------
        An anytree Node object containing the root node.

        '''
        raise NotImplementedError('This method is BUGGY')
        from anytree import Node, RenderTree
        cur = self._db.cursor()
        primary_ftypes = [x[0] for x in cur.execute('''
            SELECT DISTINCT feature_type 
            FROM primary_loci p 
                JOIN loci l ON p.LID = l.LID;
        ''').fetchall()]
        ndict = dict()
        root = Node(self.name)
        for n in primary_ftypes:
            ndict[n] = Node(n,parent=root)
        ftypes = cur.execute('''
            SELECT DISTINCT p.feature_type,c.feature_type 
            FROM relationships r 
                JOIN loci p ON r.parent = p.LID 
                JOIN loci c ON r.child = c.LID;
        ''').fetchall()
        # Create Nodes
        for p,c in ftypes:
            ndict[c] = Node(c)
        for p,c in ftypes:
            if p in ndict:
                ndict[c].parent = ndict[p]
            else:
                ndict[c].parent = root
        if print_tree is True:
            print(RenderTree(root))
        return root
Esempio n. 21
0
def add_node(dyad, parent_node):
    """Make a cost-scored node from a target pair dyad.  Use memoized cost lookups for speed"""
    matches = [x for x in parent_node.children if x.name == dyad]
    if not any(matches):
        n = Node(dyad)
        n.cost = get_cost(dyad)
        n.parent = parent_node
    else:
        n = matches[0]
    return n
 def append_child_nodes(self, dnode, node):
     ''' recursive method to append child for anytree decision tree '''
     for child in dnode.children:
         new_node = Node(child.data)
         new_node.parent = node
         if (child.children != []):
             self.append_child_nodes(child, new_node)
         else:
             # print("Leaf Node")
             return
Esempio n. 23
0
def parse_tree(input_list):
    """Process string and return the anytree object"""
    for pair in input_list.copy():
        left, _, right = pair.rstrip().partition(")")
        globals()[left] = Node(left)
        globals()[right] = Node(right)
    for pair in input_list.copy():
        left, _, right = pair.rstrip().partition(")")
        globals()[right].parent = globals()[left]
    return globals()
Esempio n. 24
0
 def process_content(self, content):
     matches = re.findall(self.RELATIONSHIP_PATTERN, content)
     for relationship, name in matches:
         node, created = self.find_or_create(name)
         if relationship == 'extends':
             self.current_node.parent = node
             continue
         # include (partials) or from .. import (macros)
         if not created:
             node = Node(name)
         node.parent = self.current_node
Esempio n. 25
0
def construct_tree(orbits):
    com = Node("COM")
    planets = {"COM": com}
    for orbit in orbits:
        if not orbit[0] in planets:
            planets[orbit[0]] = Node(orbit[0])
        if not orbit[1] in planets:
            planets[orbit[1]] = Node(orbit[1])
        planets[orbit[1]].parent = planets[orbit[0]]

    return (com, planets)
Esempio n. 26
0
def testTreeCreation():
    root = "Amendment"
    children = ["SPRITE","BILLIT"]
    testTree = tree.createTree(root,children)
    print(RenderTree(testTree))

    mpan = Node("MPAN Amendment")
    mpan.parent = testTree.descendants



    #testTree2 = tree.createTree(children[0],["MPAN amendment"])
    print(RenderTree(mpan))
Esempio n. 27
0
 def syntaxBlock(self, herencia=None):    
     new_tree = Node('block')    
     if self.isexpected(self.tokens[0], "Delimiter", "{"):
         self.popToken('block', new_tree)            
         self.syntaxVar_decl(new_tree)
         self.syntaxStatement(new_tree)
         if self.isexpected(self.tokens[0], "Delimiter", "}"):
             self.popToken('block', new_tree)
         else:
             self.printExpectedToken("['Delimiter','}']")
     else:
         self.printExpectedToken("['Delimiter', '{']")
     new_tree.parent = herencia
Esempio n. 28
0
def treeFromListOfLists(domList):
    '''Crear y devolver un arbol de anytree, desde una lista de listas'''
    tree = Node(ROOT)
    for item in domList:
        currTree = tree
        for subitem in item[::-1]:
            if subitem not in list(map(lambda x: x.name, currTree.children)):
                child = Node(subitem)
                child.parent = currTree
            else:
                child = findChildren(currTree, subitem)
            currTree = child
    return tree
Esempio n. 29
0
def reconstruct(andNodes, orNodes, ex1):
    global andDict
    global orDict

    tree = Node("THEN")

    for k in ex1:
        if k in andDict:
            if len(andDict) == 1 or k not in andNodes:
                andDict[k].parent = tree
            else:
                for i in range(0, andNodes.size - 1, 2):
                    node = Node("AND")
                    if (andNodes[i] in andDict):
                        andDict[andNodes[i]].parent = node
                    elif (andNodes[i] in orDict):
                        orDict[andNodes[i]].parent = node
                    if (andNodes[i + 1] in andDict):
                        andDict[andNodes[i + 1]].parent = node
                    elif (andNodes[i + 1] in orDict):
                        orDict[andNodes[i + 1]].parent = node
                    node.parent = tree
        elif k in orDict:
            if len(orDict) == 1 or k not in orNodes:
                orDict[k].parent = tree
            else:
                for i in range(0, orNodes.size - 1, 2):
                    node = Node("OR", parent=tree)
                    if (orNodes[i] in andDict):
                        andDict[orNodes[i]].parent = node
                    elif (orNodes[i] in orDict):
                        orDict[orNodes[i]].parent = node
                    if (orNodes[i + 1] in andDict):
                        andDict[orNodes[i + 1]].parent = node
                    elif (orNodes[i + 1] in orDict):
                        orDict[orNodes[i + 1]].parent = node
                node.parent = tree
    return tree
Esempio n. 30
0
 def syntaxLocation(self, herencia=None):
     new_tree = Node('location')
     if self.isID(self.tokens[0]):
         self.popToken('location', new_tree)
         if self.isexpected(self.tokens[0], "Delimiter", "["):
             self.popToken('location', new_tree)
             self.syntaxExpr(new_tree)
             if self.isexpected(self.tokens[0], "Delimiter", "]"):
                 self.popToken('location', new_tree)
             else:
                 self.printExpectedToken("['Delimiter',']']")
     else:
         self.printExpectedToken("<ID>")
     new_tree.parent = herencia
 def dependency_tree(self, repo: str) -> Node:
     """Query and generate a tree from the response"""
     data = self._shallow_dependencies(repo)
     if data.get("errors"):
         print(data.get("errors"))
         sys.exit(1)
     package_node = Node(repo)
     manifests = data["data"]["repository"]["dependencyGraphManifests"]
     for manifest in manifests["nodes"]:
         manifest_node = Node(manifest["blobPath"].split("/")[-1])
         manifest_node.parent = package_node
         for dependency in manifest["dependencies"]["nodes"]:
             try:
                 dependency_id = dependency["repository"]["nameWithOwner"]
             # Sometimes dependency repositories don't exist.
             except TypeError:
                 dependency_id = dependency["packageName"]
             dependency_node = Node("{} {}".format(
                 dependency_id, dependency["requirements"]))
             dependency_node.parent = manifest_node
             if dependency["hasDependencies"]:
                 Node("⋯ ").parent = dependency_node
     return package_node
Esempio n. 32
0
def create_ontology_graph():
    # Construct ISA trees from triples

    graph = rdflib.Graph()
    graph.parse(os.path.join(ontology_dir, 'inferred_vrd'))

    ontology_labels_nodes = {}
    ontology_labels_equivalent_tmp = Set([])
    ontology_labels_equivalent = Set([])

    for s, p, o in graph.triples((None, URIRef("http://www.w3.org/2002/07/owl#equivalentProperty"), None)):
        # print s, " -> ", p, " -> ", o
        if "http://" in s and "http://" in o:

            subj_label = str(s.split("#")[1])
            obj_label = str(o.split("#")[1])
            ontology_labels_equivalent.add(subj_label)
            ontology_labels_equivalent.add(obj_label)

            if ontology_labels_nodes:
                new_node = True
                for node_label in ontology_labels_nodes.keys():

                    if subj_label in node_label.split(","):
                        ontology_labels_equivalent_tmp.remove(node_label)
                        ontology_labels_nodes[node_label].name = ontology_labels_nodes[node_label].name + "," + obj_label
                        ontology_labels_equivalent_tmp.add(ontology_labels_nodes[node_label].name)
                        ontology_labels_nodes[ontology_labels_nodes[node_label].name] = ontology_labels_nodes[
                            node_label]
                        del ontology_labels_nodes[node_label]
                        new_node = False

                    elif obj_label in node_label.split(","):
                        ontology_labels_equivalent_tmp.remove(node_label)
                        ontology_labels_nodes[node_label].name = ontology_labels_nodes[node_label].name + "," + subj_label
                        ontology_labels_equivalent_tmp.add(ontology_labels_nodes[node_label].name)
                        ontology_labels_nodes[ontology_labels_nodes[node_label].name] = ontology_labels_nodes[node_label]
                        del ontology_labels_nodes[node_label]
                        new_node = False
                if new_node:
                    ontology_labels_nodes[subj_label + "," + obj_label] = Node(subj_label + "," + obj_label)
                    ontology_labels_equivalent_tmp.add(subj_label + "," + obj_label)
            else:
                ontology_labels_nodes[subj_label + "," + obj_label] = Node(subj_label + "," + obj_label)
                ontology_labels_equivalent_tmp.add(subj_label + "," + obj_label)

    for s, p, o in graph.triples((None, URIRef("http://www.w3.org/2000/01/rdf-schema#subPropertyOf"), None)):
        #print s, " -> ", p, " -> ", o

        if "http://" in s and "http://" in o:

            subj_label = str(s.split("#")[1])
            obj_label = str(o.split("#")[1])

            subj_node_name = ""
            obj_node_name = ""
            for node_label in ontology_labels_equivalent_tmp:
                if subj_label in node_label.split(","):
                    subj_node_name = node_label
                    continue
                if obj_label in node_label.split(","):
                    obj_node_name = node_label
                    continue
            if subj_node_name and obj_node_name:
                ontology_labels_nodes[subj_node_name].parent = ontology_labels_nodes[obj_node_name]

            if subj_label not in ontology_labels_equivalent and obj_label not in ontology_labels_equivalent:

                if subj_label not in ontology_labels_nodes:
                    ontology_labels_nodes[subj_label] = Node(subj_label)
                if obj_label not in ontology_labels_nodes:
                    ontology_labels_nodes[obj_label] = Node(obj_label)

                ontology_labels_nodes[subj_label].parent = ontology_labels_nodes[obj_label]

            if subj_label in ontology_labels_equivalent and obj_label not in ontology_labels_equivalent:
                if obj_label not in ontology_labels_nodes:
                    ontology_labels_nodes[obj_label] = Node(obj_label)

                # retrieve subj node
                for node_label in ontology_labels_nodes.keys():
                    if subj_label in node_label.split(","):
                        ontology_labels_nodes[node_label].parent = ontology_labels_nodes[obj_label]

            if subj_label not in ontology_labels_equivalent and obj_label in ontology_labels_equivalent:
                if subj_label not in ontology_labels_nodes:
                    ontology_labels_nodes[subj_label] = Node(subj_label)

                # retrieve obj node
                for node_label in ontology_labels_nodes.keys():
                    if obj_label in node_label.split(","):
                        ontology_labels_nodes[subj_label].parent = ontology_labels_nodes[node_label]

    tree_list = []
    for node_label in ontology_labels_nodes:
        if ontology_labels_nodes[node_label].is_root:
            tree_list.append(ontology_labels_nodes[node_label])
    return tree_list, ontology_labels_equivalent_tmp