|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.flink.agents.api.context; |
| 19 | + |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +/** |
| 24 | + * A representation of an object in the short-term memory. It is responsible for accessing and |
| 25 | + * manipulating (direct or indirect) fields within the memory structure. A direct field is a field |
| 26 | + * which stores primitive data directly, while an indirect filed is a field which represents a |
| 27 | + * nested object.Fields can be accessed using an absolute or relative path. |
| 28 | + */ |
| 29 | +public interface MemoryObject { |
| 30 | + /** |
| 31 | + * Returns a MemoryObject that represents the given path. |
| 32 | + * |
| 33 | + * @param path relative path from the current object to the target field |
| 34 | + * @return a MemoryObject instance pointing to the field. If the field is a primitive value |
| 35 | + * type, the value of the returned MemoryObject can be exposed via {@link #getValue()}.If |
| 36 | + * the field is a nested object, the subfields of the returned MemoryObject can be exposed |
| 37 | + * via {@link #getFields()}. |
| 38 | + * @throws Exception if the field does not exist |
| 39 | + */ |
| 40 | + MemoryObject get(String path) throws Exception; |
| 41 | + |
| 42 | + /** |
| 43 | + * Sets the value of a direct field in the current object; any missing intermediate objects will |
| 44 | + * be created automatically. |
| 45 | + * |
| 46 | + * @param path relative path from the current object to the target field |
| 47 | + * @param value new value of the field |
| 48 | + * @throws Exception if trying to overwrite a nested object with a primitive value or set a |
| 49 | + * MemoryObject directly |
| 50 | + */ |
| 51 | + void set(String path, Object value) throws Exception; |
| 52 | + |
| 53 | + /** |
| 54 | + * Creates a new MemoryObject as an indirect field in the current object. |
| 55 | + * |
| 56 | + * @param path relative path from the current object to the target field |
| 57 | + * @param overwrite whether to overwrite existing field if it's not a nested object |
| 58 | + * @return the created object |
| 59 | + * @throws Exception if field exists but is not a nested object and overwrite is false |
| 60 | + */ |
| 61 | + MemoryObject newObject(String path, boolean overwrite) throws Exception; |
| 62 | + |
| 63 | + /** |
| 64 | + * Checks whether a (direct or indirect) field exists in the current object. |
| 65 | + * |
| 66 | + * @param path relative path from the current object to the target field |
| 67 | + * @return true if the field exists, false otherwise |
| 68 | + */ |
| 69 | + boolean isExist(String path); |
| 70 | + |
| 71 | + /** |
| 72 | + * Gets names of all the top-level fields of the current object. |
| 73 | + * |
| 74 | + * @return list of top-level field names |
| 75 | + * @throws Exception state-backend failure |
| 76 | + */ |
| 77 | + List<String> getFieldNames() throws Exception; |
| 78 | + |
| 79 | + /** |
| 80 | + * Gets all the top-level fields of the current object. |
| 81 | + * |
| 82 | + * @return map of top-level fields |
| 83 | + * @throws Exception state-backend failure |
| 84 | + */ |
| 85 | + Map<String, Object> getFields() throws Exception; |
| 86 | + |
| 87 | + /** |
| 88 | + * Gets the primitive value stored at the current path. |
| 89 | + * |
| 90 | + * @return the primitive value if this MemoryObject stores primitive value directly, or null if |
| 91 | + * it represents a nested object |
| 92 | + * @throws Exception state-backend failure |
| 93 | + */ |
| 94 | + Object getValue() throws Exception; |
| 95 | + |
| 96 | + /** |
| 97 | + * Checks whether the current object is a nested object. |
| 98 | + * |
| 99 | + * @return true if this MemoryObject is a nested object, false if it stores primitive value |
| 100 | + * directly |
| 101 | + * @throws Exception state-backend failure |
| 102 | + */ |
| 103 | + boolean isNestedObject() throws Exception; |
| 104 | +} |
0 commit comments