Esempio n. 1
0
def create_dirstruct(home_dir, ns):
    """

    :param home_dir:
    :param ns:
    :return:
    """

    ns_path = get_path(namespace=ns)
    dir_struct = "ontologies{}{}".format(_DIRSEP, ns_path)

    cur_dir = home_dir + '{}{}'.format(_DIRSEP, dir_struct)
    pathlib.Path(cur_dir).mkdir(parents=True, exist_ok=True)

    for item in ['classes', 'properties']:
        current_dir = '{}{}{}'.format(cur_dir, _DIRSEP, item)
        pathlib.Path(current_dir).mkdir(parents=True, exist_ok=True)

    filename = "{}{ps}templates{ps}__init__.tpl".format(home_dir, ps=_DIRSEP)
    template = read_template(filename=filename)
    content = template.substitute(namespace=ns)
    filename="{}{}__init__.py".format(cur_dir, _DIRSEP)
    write_file(filename=filename, content=content)

    return cur_dir
Esempio n. 2
0
def create_ns_structure(ns_structure):
    """

    :param ns_structure: list of path names that leading to an ontologies
                         "home directory"
    :return:
    """

    for key, value in ns_structure.items():
        for directory in ['classes', 'properties']:
            current_dir = "{}{}{}".format(value['path'], _DIRSEP, directory)
            pathlib.Path(current_dir).mkdir(parents=True, exist_ok=True)

        write_file(filename="{}{}__init__.py".format(value['path'], _DIRSEP),
                   content="ONTOLOGY_NS = '{}'\nPROJECT_ID = '{}'\n".format(
                       key, value['project_id']))

    return
Esempio n. 3
0
def create_kbo(template_dir, target_dir):
    """

    :param template_dir: directory, where we find the templates
    :param target_dir: directory, from where we derive the files' final
                       destination(s)
    :return:
    """

    for context in ['classes', 'properties']:
        context_templates = "{}/{}".format(template_dir, context)
        context_directory = "{}/{}".format(target_dir, context)
        for (dirpath, dirnames, filenames) in os.walk(context_templates):
            for filename in filenames:
                if filename.startswith('kbo_') and filename.endswith('.tpl'):
                    target_file = "{}/{}.py".format(context_directory,
                                                    filename[4:-4])
                    template = read_template(filename="{}/{}".format(context_templates, filename))
                    content = template.substitute()
                    write_file(filename=target_file, content=content)
    return
Esempio n. 4
0
def create_properties(ontology_home, template_file, graph, mappings, src_filename):
    """

    :param ontology_home:
    :param template_file:
    :param graph:
    :param mappings:
    :param src_filename:
    :return:
    """

    for property_uri in graph_properties(graph=graph):
        known_namespaces = ''
        module_import = ''
        general_comment = ''
        class_name = ''
        sub_property_of = ''
        class_comment = ''
        property_ns = ''
        property_name = ''

        property_ns, property_fragment = ns_fragement(property_uri)
        property_dir = "{}{}{ps}properties".format(ontology_home, mappings[property_ns], ps=_DIRSEP)

        cls_dependencies = property_class_dependencies(graph=graph, uri=property_uri, allowed_ns=known_namespaces)

        # $class_name
        class_name = to_class_name(property_fragment)
        property_name = property_fragment

        # $general_comment
        general_comment = file_comment('property', ns=property_ns, name=class_name, source=src_filename)

        # $class_comment
        class_comment = extract_comment(graph, uri=property_uri)

        # $module_import

        # $sub_class_of
        cls_data = {}
        for dependency in cls_dependencies:
            ns, dep_name = ns_fragement(dependency)
            if ns not in mappings:
                continue
            dep_name = to_class_name(dep_name)
            alias = None
            if dep_name == class_name or dep_name in cls_data:
                alias = "{}_{}".format(to_class_name(generate_alias(mappings[ns])), dep_name)
            if dep_name not in cls_data:
                cls_data[dep_name] = []
            cls_data[dep_name].append((ns, alias))

        modules_to_import = []
        parent_properties = []
        for key, value in cls_data.items():
            for item in value:
                cur_path = generate_import("{}{ps}properties".format(mappings[property_ns], ps=_DIRSEP),
                                           "{}{ps}properties".format(mappings[item[0]], ps=_DIRSEP))
                import_str = "from {}.{} import {}".format(cur_path, to_property_name(key), to_class_name(key))
                if item[1]:
                    parent_properties.append(item[1])
                    modules_to_import.append("{} as {}".format(import_str, to_class_name(item[1])))
                else:
                    parent_properties.append(key)
                    modules_to_import.append(import_str)
        sub_property_of = ",".join(parent_properties)

        # read template and generate file
        module_import = "\n".join(modules_to_import)

        class_template = read_template(template_file)
        content = class_template.substitute(module_import=module_import,
                                            general_comment=general_comment,
                                            class_name=class_name,
                                            sub_property_of=sub_property_of,
                                            class_comment=class_comment,
                                            namespace=property_ns,
                                            name=property_name)
        filename = "{}{ps}{}.py".format(property_dir, property_name, ps=_DIRSEP)
        write_file(filename=filename, content=content)
Esempio n. 5
0
def create_classes(ontology_home, template_file, graph, mappings, src_filename):
    """

    :param ontology_home:
    :param template_file:
    :param graph:
    :param mappings:
    :param filename: name of the (turtle) source file
    :return:
    """

    for class_uri in graph_classes(graph=graph):
        known_namespaces = ""
        module_import = ""
        general_comment = ""
        class_name = ""
        sub_class_of = ""
        class_comment = ""
        argument = ""
        argument_comment = ""
        namespace = ""
        class_properties = ""

        class_ns, class_fragment = ns_fragement(class_uri)
        class_dir = "{}{}{ps}classes".format(ontology_home,
                                             mappings[class_ns],
                                             ps=_DIRSEP)

        cls_dependencies = class_dependencies(graph=graph,
                                              uri=class_uri,
                                              allowed_ns=known_namespaces)
        prop_dependencies = property_dependencies(graph=graph,
                                                  uri=class_uri)

        # $class_name
        class_name = to_class_name(class_fragment)

        # $general_comment
        general_comment = file_comment('class',
                                       ns=class_ns,
                                       name=class_name,
                                       source=src_filename)

        # $class_comment
        class_comment = extract_comment(graph, uri=class_uri)
        if class_comment:
            class_comment = "    {}".format(class_comment)
        i = 1
        while len(class_comment) > i * 80:
            idx = class_comment.rfind(' ', (i - 1) * 80, i * 80)
            class_comment = class_comment[:idx] + "\n    " + class_comment[idx + 1:]
            i += 1
        class_labels = extract_label(graph, uri=class_uri)
        if class_labels:
            labels = []
            for key in sorted(class_labels):
                labels.append('{} ({})'.format(class_labels[key], key))
            plural = "s" if len(labels) > 1 else ""
            class_comment = "{}\n\n    Label{}: {}".format(class_comment, plural, " / ".join(labels))

        # $module_import

        # $sub_class_of
        cls_data = {}
        for dependency in cls_dependencies:
            ns, dep_name = ns_fragement(dependency)
            if ns not in mappings:
                continue
            dep_name = to_class_name(dep_name)
            alias = None
            if dep_name == class_name or dep_name in cls_data:
                alias = "{}_{}".format(generate_alias(mappings[ns]), dep_name)
            if dep_name not in cls_data:
                cls_data[dep_name] = []
            cls_data[dep_name].append((ns, alias))

        tmp = []
        modules_to_import = []
        for key, value in cls_data.items():
            for item in value:
                cur_path = generate_import("{}{ps}classes".format(mappings[class_ns], ps=_DIRSEP),
                                           "{}{ps}classes".format(mappings[item[0]], ps=_DIRSEP))
                import_str = "from {}.{} import {}".format(cur_path, key, to_class_name(key))
                if item[1]:
                    tmp.append(item[1])
                    modules_to_import.append("{} as {}".format(import_str, item[1]))
                else:
                    tmp.append(key)
                    modules_to_import.append(import_str)
        sub_class_of = ", ".join(tmp) if tmp else ''

        prop_data = {}
        for dependency in prop_dependencies:
            ns, dep_name = ns_fragement(dependency)
            dep_name = to_property_name(dep_name)
            alias = None
            if dep_name == class_name or dep_name in prop_data:
                alias = "{}_{}".format(to_class_name(generate_alias(mappings[ns])), dep_name)
            if dep_name not in prop_data:
                prop_data[dep_name] = []
            prop_data[dep_name].append((ns, alias))

        arg = []
        arg_comment = []
        cls_properties = []
        for key, value in prop_data.items():
            for item in value:
                cur_path = generate_import("{}{ps}classes".format(mappings[class_ns], ps=_DIRSEP),
                                           "{}{ps}properties".format(mappings[item[0]], ps=_DIRSEP))
                import_str = "from {}.{} import {}".format(cur_path, key, to_class_name(key))
                arg.append(key)
                arg_comment.append("{}:param {}:".format(" "*8, key))
                cls_properties.append("{}self.{} = {}({})".format(" "*8, key, to_class_name(key), key))
                if item[1]:
                    arg_comment.append(item[1])
                    modules_to_import.append("{} as {}\n".format(import_str, item[1]))
                else:
                    tmp.append(key)
                    modules_to_import.append(import_str)

        if arg:
            arg_init = ["{}=None".format(item) for item in arg]
            argument = " {},".format(", ".join(arg_init))
            argument_comment = "\n{}".format("\n".join(arg_comment))
            class_properties = "\n{}\n".format("\n".join(cls_properties))

        # read template and generate file
        module_import = "\n".join(modules_to_import)

        class_template = read_template(template_file)
        content = class_template.substitute(module_import=module_import,
                                            general_comment=general_comment,
                                            class_name=class_name,
                                            sub_class_of=sub_class_of,
                                            class_comment=class_comment,
                                            argument=argument,
                                            argument_comment=argument_comment,
                                            namespace=class_ns,
                                            class_properties=class_properties)
        filename = "{}{ps}{}.py".format(class_dir, class_name, ps=_DIRSEP)
        write_file(filename=filename, content=content)
Esempio n. 6
0
def create_properties(template_file, graph, mappings, src_filename):
    """

    :param template_file:
    :param graph:
    :param mappings:
    :param src_filename:
    :return:
    """

    for property_uri in graph_properties(graph=graph):
        if str(property_uri).endswith('Value'):
            if URIRef(str(property_uri)[:-5]) in graph_properties(graph=graph):
                continue

        known_namespaces = ''
        property_internal_vars = ''

        property_ns, property_fragment = ns_fragement(property_uri)
        property_dir = "{}{ps}properties".format(mappings[property_ns]['path'],
                                                 ps=_DIRSEP)

        cls_dependencies = property_class_dependencies(
            graph=graph, uri=property_uri, allowed_ns=known_namespaces)

        # $class_name
        class_name = to_class_name(property_fragment)
        property_name = property_fragment

        # $general_comment
        general_comment = generate_file_comment('property',
                                                namespace=property_ns,
                                                name=class_name,
                                                source=src_filename)

        # $class_comment
        class_comment = extract_comment(graph, uri=property_uri)

        # $sub_class_of
        cls_data = {}
        for dependency in cls_dependencies:
            ns, dep_name = ns_fragement(dependency)
            if ns not in mappings:
                continue
            dep_name = to_class_name(dep_name)
            alias = None
            if dep_name == class_name or dep_name in cls_data:
                alias = "{}_{}".format(mappings[ns]['name'], dep_name)
            if dep_name not in cls_data:
                cls_data[dep_name] = []
            cls_data[dep_name].append((ns, alias))

        if URIRef("{}Value".format(
                str(property_uri))) in graph_properties(graph=graph):
            coordinates = prop_coordinates(graph=graph, uri=property_uri)
            try:
                obj_class_constraint = str(
                    coordinates['objectClassConstraint'].pop())
                property_internal_vars = '        self._objectClassConstraint = "{}"'.format(
                    obj_class_constraint)
            except Exception as e:
                print(e)

        modules_to_import = []
        parent_properties = []
        for key, value in cls_data.items():
            for item in value:
                cur_path = generate_import(
                    "{}{ps}properties".format(
                        mappings[property_ns]['rel_path'], ps=_DIRSEP),
                    "{}{ps}properties".format(mappings[item[0]]['rel_path'],
                                              ps=_DIRSEP))
                import_str = "from {}.{} import {}".format(
                    cur_path, to_property_name(key), to_class_name(key))
                if item[1]:
                    parent_properties.append(item[1])
                    modules_to_import.append("{} as {}".format(
                        import_str, item[1]))
                else:
                    parent_properties.append(key)
                    modules_to_import.append(import_str)
        sub_property_of = ",".join(parent_properties)

        # read template and generate file
        module_import = "\n".join(modules_to_import)

        class_template = read_template(template_file)
        content = class_template.substitute(
            module_import=module_import,
            general_comment=general_comment,
            class_name=class_name,
            sub_property_of=sub_property_of,
            class_comment=class_comment,
            namespace=property_ns,
            name=property_name,
            property_internal_vars=property_internal_vars)
        filename = "{}{ps}{}.py".format(property_dir,
                                        to_property_name(property_name),
                                        ps=_DIRSEP)
        write_file(filename=filename, content=content)