|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +# |
| 18 | +from torch.utils.data import Dataset |
| 19 | + |
| 20 | +from ainode.core.ingress.iotdb import IoTDBTableModelDataset, IoTDBTreeModelDataset |
| 21 | +from ainode.core.util.decorator import singleton |
| 22 | + |
| 23 | + |
| 24 | +class BasicDatabaseDataset(Dataset): |
| 25 | + def __init__(self, ip: str, port: int): |
| 26 | + self.ip = ip |
| 27 | + self.port = port |
| 28 | + |
| 29 | + |
| 30 | +class BasicDatabaseForecastDataset(BasicDatabaseDataset): |
| 31 | + def __init__(self, ip: str, port: int, input_len: int, output_len: int): |
| 32 | + super().__init__(ip, port) |
| 33 | + self.input_len = input_len |
| 34 | + self.output_len = output_len |
| 35 | + |
| 36 | + |
| 37 | +def register_dataset(key: str, dataset: Dataset): |
| 38 | + DatasetFactory().register(key, dataset) |
| 39 | + |
| 40 | + |
| 41 | +@singleton |
| 42 | +class DatasetFactory(object): |
| 43 | + |
| 44 | + def __init__(self): |
| 45 | + self.dataset_list = { |
| 46 | + "iotdb.table": IoTDBTableModelDataset, |
| 47 | + "iotdb.tree": IoTDBTreeModelDataset, |
| 48 | + } |
| 49 | + |
| 50 | + def register(self, key: str, dataset: Dataset): |
| 51 | + if key not in self.dataset_list: |
| 52 | + self.dataset_list[key] = dataset |
| 53 | + else: |
| 54 | + raise KeyError(f"Dataset {key} already exists") |
| 55 | + |
| 56 | + def deregister(self, key: str): |
| 57 | + del self.dataset_list[key] |
| 58 | + |
| 59 | + def get_dataset(self, key: str): |
| 60 | + if key not in self.dataset_list.keys(): |
| 61 | + raise KeyError(f"Dataset {key} does not exist") |
| 62 | + return self.dataset_list[key] |
0 commit comments