Exemple #1
0
    def __insert_helper(self, stc, insertion):
        # print(f'__insert_helper({stc}, {insertion})')
        self.db[stc][1] = insertion.hash
        self.db[stc][0].push(insertion.value)
        if insertion.stc == stc:
            # done base case, no need further
            return

        if insertion.stc.tc > stc.tc:
            diff_level, diff_value = insertion.stc.tc - stc.tc
            # print(f'diff_level: {diff_level}, diff_value: {diff_value}')
            ntc = stc.tc.copy()
            setattr(ntc, diff_level, diff_value)
            ntc.depth += 1
            nstc = STC(SC(stc.sc.path), ntc)
            # print(f'nstc: {nstc}')
            if nstc not in self.db:
                # Create the new stc
                self.db[nstc] = [Statistics(), '', set(), set()]
                # Insert new stc to old stc's child
                self.db[stc][3].add(nstc)
            if self.db[nstc][1] != insertion.hash:
                self.__insert_helper(nstc, insertion)

        if insertion.stc.sc > stc.sc:
            diff_char = insertion.stc.sc - stc.sc
            nstc = STC(SC(stc.sc.path + diff_char), stc.tc.copy())
            if nstc not in self.db:
                self.db[nstc] = [Statistics(), '', set(), set()]
                self.db[stc][2].add(nstc)
            if self.db[nstc][1] != insertion.hash:
                self.__insert_helper(nstc, insertion)
Exemple #2
0
    def retrieve(self, query):
        stc, feature = self.lexer.parse_query(query)
        print(f"stc: {stc}, feature: {feature}")
        if feature is not None and str(stc) == '':
            # Only provided feature, sum this feature's root
            s = Statistics()
            c = collections, Counter({})
            for stg in self.db[feature]:
                temps, tempc = stg.retrieve_root_sum()
                if temps is not None:
                    s += temps
                if tempc is not None:
                    c += tempc
            return s, c

        if feature is None and str(stc) == '':
            # Nothing provided, sum everything
            s = Statistics()
            c = collections.Counter({})
            for feature in self.features:
                for stg in self.db[feature]:
                    temps, tempc = stg.retrieve_root_sum()
                    if temps is not None:
                        s += temps
                    if tempc is not None:
                        c += tempc
            return s, c

        if stc is None:
            return None, None

        # if no feature is specified, yet a valid stc, do featural summation
        if feature is None:
            s = Statistics()
            c = collections.Counter({})
            for feature in self.features:
                for stg in self.db[feature]:
                    temps, tempc = stg.retrieve(stc)
                    if temps is not None:
                        s += temps
                    if tempc is not None:
                        c += tempc
            return s if len(s) > 0 else None, c if len(s) > 0 else None
        else:
            if feature not in self.db:
                return None, None
            s = Statistics()
            c = collections.Counter({})

            for stg in self.db[feature]:
                temps, tempc = stg.retrieve(stc)
                if temps is not None:
                    s += temps
                if tempc is not None:
                    c += tempc
            return s if len(s) > 0 else None, c if len(s) > 0 else None
Exemple #3
0
    def __retrieve_helper(self, curr_stc, retrieve_stc):
        print(f'__retrieve_helper({curr_stc}, {retrieve_stc})')
        if retrieve_stc == curr_stc:
            return self.db[curr_stc][0]

        if retrieve_stc.tc > curr_stc.tc:
            # CHECK if next step is wild card
            diff_level, diff_value = retrieve_stc.tc - curr_stc.tc
            print(f'diff_level: {diff_level}, diff_value: {diff_value}')
            if diff_value is None:
                # Wild card, sum all temporal child
                s = Statistics()
                for stc in self.db[curr_stc][3]:
                    print('checkkk')
                    temps = self.__retrieve_helper(stc, retrieve_stc)
                    if temps is not None:
                        s += temps
                return s if len(s) > 0 else None
            # Go one level deeper:
            ntc = curr_stc.tc.copy()
            setattr(ntc, diff_level, diff_value)
            ntc.depth += 1
            nstc = STC(SC(curr_stc.sc.path), ntc)
            if nstc not in self.db:
                # Doesn't have further node, no data
                return None
            return self.__retrieve_helper(nstc, retrieve_stc)
        else:
            # TC done, go sc direction
            diff_char = retrieve_stc.sc - curr_stc.sc
            nstc = STC(SC(curr_stc.sc.path + diff_char), curr_stc.tc.copy())
            if nstc not in self.db:
                # Doesn't have further node, no data
                return None
            return self.__retrieve_helper(nstc, retrieve_stc)
Exemple #4
0
 def __insert(self, insertion):
     # print(f'insert({insertion})')
     # Start from temporal_path
     self.lock.acquire()
     if len(insertion.stc.tc) > 0:
         # Create new temporal top level node if missing
         rootstc = STC(SC(), TC({'year': insertion.stc.tc.year}))
         if rootstc not in self.db:
             self.temporal_root.add(rootstc)
             self.db[rootstc] = [Statistics(), '', set(), set()]
         self.__insert_helper(rootstc, insertion)
     if len(insertion.stc.sc) > 0:
         rootstc = STC(SC(insertion.stc.sc.path[0]), TC())
         if rootstc not in self.db:
             self.spatial_root.add(rootstc)
             self.db[rootstc] = [Statistics(), '', set(), set()]
         self.__insert_helper(rootstc, insertion)
     self.lock.release()
Exemple #5
0
    def retrieve_root_sum(self):
        s = Statistics()
        m = {}
        self.lock.acquire()
        for stc in self.spatial_root:
            s += self.db[stc][0]
            m[stc] = len(self.db[stc][0])

        for stc in self.temporal_root:
            m[stc] = len(self.db[stc][0])
        self.lock.release()
        return s, collections.Counter(m)
Exemple #6
0
    def retrieve(self, stc):

        self.lock.acquire()
        if str(stc) == '':
            return self.retrieve_root_sum()

        self.prune()
        if stc in self.db:
            v = self.db[stc][0]
            self.lock.release()
            return v, self.lower_distr(stc)

        # if there is any Nones in insertion's tc
        if None in [
                getattr(stc.tc, l) for l in TC.levels if hasattr(stc.tc, l)
        ]:
            #HAS WILD CARD(S)
            if stc.tc.year == None:
                # WILD CARD @ year, rec call and sum all temporal root
                stats = Statistics()
                for trstc in self.temporal_root:
                    temps = self.__retrieve_helper(trstc, stc)
                    if temps is not None:
                        stats += temps
                self.lock.release()
                return stats if len(stats) > 0 else None, None
            else:
                y = stc.tc.year
                rootstc = STC(SC(), TC({'year': y}))
                if rootstc not in self.db:
                    self.lock.release()
                    return None, None
                v = self.__retrieve_helper(rootstc, stc)
                self.lock.release()
                return v, None

        self.lock.release()
        return None, None
Exemple #7
0
 def ssum(s):
     # note: sum does not take keyword arguments
     return sum(s, Statistics())