コード例 #1
0
 def get_project_information(project_name):
     """
     :param project_name: the name of the current project.
     :return: the query for general information about the current project.
     """
     pypher_object = Pypher()
     pypher_object.Match.node('u')
     pypher_object.WHERE(__.u.__name__ == project_name)
     pypher_object.RETURN('u')
     return GenerateQuery.reformat_query(pypher_object)
コード例 #2
0
    def get_largest_compilation_unit_query(bundle_name=None, order="mthd"):
        """
        :param bundle_name: in entire project or in one specific bundle. Default is in all bundles.
        :param order: largest by lines of code (loc) or number of methods (mthd). Default is "mthd".
        :return: the query for the largest compilation unit.
        'large' in this context means lines of code or number of methods.
        It also can be the largest CU in a bundle or in general.

        """
        pypher_object = Pypher()
        if order == "loc":
            pypher_object.Match.node('bundle', labels='bundles')\
                .relationship('pkg', labels="Pkg_fragment").node('k')\
                .relationship('kl', labels='compiled_By').node('cmp')
            if bundle_name:
                pypher_object.WHERE(__.bundle.__name__ == bundle_name)
            pypher_object.RETURN('cmp')
            pypher_object.OrderBy(__.cmp.__Loc__)
        return GenerateQuery.reformat_query(pypher_object)
コード例 #3
0
class GenerateQuery:
    def __init__(self, tracker):

        self.extracted_intents = dict()
        self.extracted_values = dict()
        self.pypher_object = Pypher()
        self.tracker = tracker

    def get_simple_query(self):
        extracted_intent = self.tracker.latest_message['intent']['name']

        if len(self.tracker.latest_message['entities']) > 0:
            extracted_value = self.tracker.latest_message['entities'][0][
                'value']
            extracted_entities = {
                self.tracker.latest_message['entities'][index]['entity']:
                self.tracker.latest_message['entities'][index]['value']
                for index in range(len(
                    self.tracker.latest_message['entities']))
            }
        else:
            extracted_entities = self.tracker.slots
        if len(extracted_entities) <= 0:
            return []

        if extracted_intent == 'showNodeInformation':
            if extracted_entities['node'] is None:
                return []
            self.pypher_object.Match.node('u').where.u.__name__.CONTAINS(
                Param('per_param', extracted_entities['node']))
            self.pypher_object.RETURN('u')

        elif extracted_intent == 'showAllNodes':
            self.pypher_object.Match.node(
                'u',
                labels=self.get_key_with_none_empty_value(extracted_entities))
            self.pypher_object.RETURN('u')

        elif extracted_intent == 'countAllNodes':
            self.pypher_object.Match.node(
                'u',
                labels=self.get_key_with_none_empty_value(extracted_entities))
            self.pypher_object.RETURN(__.count('u'))

        elif extracted_intent == 'showLargestCompilationUnit':

            if self.tracker.get_slot('Methods') is not None:

                self.pypher_object.Match.node('bundle', labels='bundles').relationship \
                    ('pkg', labels="Pkg_fragment").node('k').relationship \
                    ('kl', labels='compiled_By').node().relationship \
                    ('cp', labels="compiledUnits_topLevelType").node('n').relationship \
                    ('rl', 'Methods_Contains').node('mthd')

                self.pypher_object.RETURN('bundle.name', 'n.name',
                                          __.count('mthd'))
                self.pypher_object.OrderBy(__.count('mthd'))

            else:
                self.pypher_object.Match.node('bundle', labels='bundles').relationship \
                    ('pkg', labels="Pkg_fragment").node('k').relationship \
                    ('kl', labels='compiled_By').node('cmp')

                if self.tracker.get_slot('bundles') is not None:
                    self.pypher_object.WHERE(
                        __.bundle.__name__ == self.tracker.get_slot('bundles'))

                self.pypher_object.RETURN('bundle.name', 'cmp.name', 'cmp.Loc')
                self.pypher_object.OrderBy(__.cmp.__Loc__)

            self.pypher_object.Desc()
            self.pypher_object.Limit(1)

        elif extracted_intent == 'showDetailInfoBundles':

            bundle_name = None
            key_value = None
            # iterate through all entities
            for key, value in self.tracker.slots.items():

                # key_value is assigned according to relation names
                if key == 'bundles':
                    bundle_name = value
                elif key == 'imports' or key == 'Exports':
                    key_value = value
                elif key == 'packages':
                    key_value = 'uses_pkgs'
                elif key == 'components':
                    key_value = 'uses_components'
                elif key == 'compilationUnit':
                    key_value = 'compiled_By'
                elif key == 'Methods':
                    key_value = 'Methods_Contains'

            # this is relation name
            if key_value == 'compiled_By':

                self.pypher_object.Match.node('u', labels='bundles').relationship \
                    ('f', labels="Pkg_fragment").node('n').relationship \
                    ('c', labels="compiled_By").node("m")

            elif key_value == 'Methods_Contains':
                self.pypher_object.Match.node('u', labels='bundles').relationship \
                    ('pkg', labels="Pkg_fragment").node('k').relationship \
                    ('kl', labels='compiled_By').node('n').relationship \
                    ('r', labels='compiledUnits_topLevelType').node('nl').relationship \
                    ('rl', labels='Methods_Contains').node('m')

            else:
                self.pypher_object.Match.node('u', labels='bundles').relationship \
                    ('r', labels=key_value).node('m')

            self.pypher_object.WHERE(__.u.__name__ == bundle_name)

            # this can be changed according to req. if we need all info or just names of packages
            # query = str(self.pypherObject.RETURN('u.name', 'm.name'))
            self.pypher_object.RETURN('u.name', 'm.name')

        elif self.extracted_intents == 'showProjectInformation':
            self.pypher_object.Match.node('u')
            self.pypher_object.WHERE(
                __.u.__name__ == self.tracker.get_slot('bundles'))
            self.pypher_object.RETURN('u')

        else:
            if extracted_value is not None:
                self.pypher_object.Match.node(
                    'u', labels=extracted_entities).WHERE.u.property(
                        'name') == extracted_value
                self.pypher_object.RETURN('u')

        query = str(self.pypher_object)
        params = self.pypher_object.bound_params
        return [query, params, extracted_intent]

    @staticmethod
    def get_key_with_none_empty_value(entities_dict):
        for key, value in entities_dict.items():
            print('key: ' + key)
            if value:
                print('value: ' + value)
                if key != "project":
                    return key
        return {}

    def convert_text_to_query(self):
        error = None
        [query, params, extracted_intent] = self.get_simple_query()

        if query is None or params is None or extracted_intent is None:
            error = "no Query written regarding this intention or intent prediction is not valid"

        return [query, params, self.tracker.latest_message['intent'], error]
コード例 #4
0
    def save_relationship(self, entity, ensure_unique=False):
        """this method handles creating and saving relationships.
        It will hanle quite a few situations. 
        Given the structure:

        (start)-[rel]->[end]

        We can have:
        * A new start node
        * An existing start node
        * A new rel
        * An existing rel
        * A new end node
        * An existing end node

        Each start, rel, and end could have uniqueness assigned to it
        * start/end could have unique properties
        * rel could have unique relationships

        A Chyper query should be generated that looks something like this,
        depending on the settings for each of the nodes:

        MERGE (n_0:Node {`key`: val})
        ON CREATE SET n_0.key = val, n_0.key2 = val2
        ON MATCH SET n_0.key = val, n_0.key2 = val2
        CREATE (n_0)-[r_0:RelLabel, {`key`: someVal}]->(n_1:Node {`key`: val})
        RETURN n_0, n_1, r_0
        """
        start = entity.start
        start_properties = {}
        end = entity.end
        end_properties = {}
        props = self._properties(entity)

        if start is None or end is None:
            raise Exception('The relationship must have a start and end node')

        if not isinstance(start, Node):
            start = Node(id=start)

        if not isinstance(end, Node):
            end = Node(id=end)

        VM.set_query_var(start)
        VM.set_query_var(end)
        VM.set_query_var(entity)

        rel = Pypher()

        if start not in self.matched_entities:
            if start.id is not None:
                self._update_properties(start)
                self.matches.append(self._node_by_id(start))
            else:
                start_properties = self._properties(start)

            self.matched_entities.append(start)
            self.returns.append(start.query_variable)

        if end not in self.matched_entities:
            if end.id is not None:
                self._update_properties(end)
                self.matches.append(self._node_by_id(end))
            else:
                end_properties = self._properties(end)

            self.matched_entities.append(end)
            self.returns.append(end.query_variable)

        if entity.id is None:
            if start.id is not None:
                rel = rel.node(start.query_variable)
            else:
                start_query = Query(start, self.params)
                start_query.build_save_pypher()

                if len(start_query.creates):
                    rel.append(*start_query.creates)
                elif len(start_query.merges):
                    has_matches = len(self.matches) > 0
                    start_merge = Pypher()
                    start_merge.MERGE(*start_query.merges)

                    if start_query.on_create_sets:
                        start_merge.OnCreateSet(*start_query.on_create_sets)

                    if start_query.on_match_sets:
                        start_merge.OnMatchSet(*start_query.on_match_sets)

                    self.before_matches.append(start_merge)
                    rel.node(start.query_variable)

            rel.rel(entity.query_variable,
                    labels=entity.labels,
                    direction='out',
                    **props)

            if end.id is not None:
                rel.node(end.query_variable)
            else:
                end_query = Query(end, self.params)
                end_query.build_save_pypher()

                if len(end_query.creates):
                    rel.append(*end_query.creates)
                elif len(end_query.merges):
                    end_merge = Pypher().MERGE(*end_query.merges)

                    if end_query.on_create_sets:
                        end_merge.OnCreateSet(*end_query.on_create_sets)

                    if end_query.on_match_sets:
                        end_merge.OnMatchSet(*end_query.on_match_sets)

                    self.before_matches.append(end_merge)
                    rel.node(end.query_variable)

            if ensure_unique:
                self.merges.append(rel)
            else:
                self.creates.append(rel)
        else:
            _id = VM.get_next(entity, 'id')
            _id = Param(_id, entity.id)

            if start.id is not None:
                rel = rel.node(start.query_variable)
            else:
                start_query = Query(start, self.params)
                start_query.build_save_pypher()

                if len(start_query.creates):
                    rel.append(*start_query.creates)
                elif len(start_query.merges):
                    start_merge = Pypher().MERGE(*start_query.merges)

                    if start_query.on_create_sets:
                        start_merge.OnCreateSet(*start_query.on_create_sets)

                    if start_query.on_match_sets:
                        start_merge.OnMatchSet(*start_query.on_match_sets)

                    if len(self.matches):
                        self.matches[-1].append(start_merge)
                    else:
                        self.matches.append(start_merge)

                    rel.node(start.query_variable)

            rel.rel(entity.query_variable,
                    labels=entity.labels,
                    direction='out')

            if end.id is not None:
                rel.node(end.query_variable)
            else:
                end_query = Query(end, self.params)
                end_query.build_save_pypher()

                if len(end_query.creates):
                    rel.append(*end_query.creates)
                elif len(end_query.merges):
                    end_merge = Pypher().MERGE(*end_query.merges)

                    if end_query.on_create_sets:
                        end_merge.OnCreateSet(*end_query.on_create_sets)

                    if end_query.on_match_sets:
                        end_merge.OnMatchSet(*end_query.on_match_sets)

                    if len(self.matches):
                        self.matches[-1].append(end_merge)
                    else:
                        self.matches.append(end_merge)

                    rel.node(end.query_variable)

            rel.WHERE(__.ID(entity.query_variable) == _id)
            self._update_properties(entity)
            self.matches.append(rel)

        self.returns.append(entity.query_variable)

        return self