Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2020 Robert Bosch GmbH |
| 3 | * Author: Constantin Ziesche (constantin.ziesche@bosch.com) |
| 4 | * |
| 5 | * This program and the accompanying materials are made available under the |
| 6 | * terms of the Eclipse Public License 2.0 which is available at |
| 7 | * http://www.eclipse.org/legal/epl-2.0 |
| 8 | * |
| 9 | * SPDX-License-Identifier: EPL-2.0 |
| 10 | *******************************************************************************/ |
| 11 | using BaSyx.Models.Core.AssetAdministrationShell.Generics; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 12 | using BaSyx.Models.Core.Common; |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame^] | 13 | using BaSyx.Utils.ResultHandling; |
| 14 | using Newtonsoft.Json; |
| 15 | using System; |
| 16 | using System.Collections; |
| 17 | using System.Collections.Generic; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 18 | using System.Runtime.Serialization; |
| 19 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame^] | 20 | namespace BaSyx.Models.Core.AssetAdministrationShell.Implementations |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 21 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame^] | 22 | [DataContract, JsonObject] |
| 23 | public class SubmodelElementCollection : SubmodelElement, ISubmodelElementCollection, IElementContainer<ISubmodelElement> |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 24 | { |
| 25 | public override ModelType ModelType => ModelType.SubmodelElementCollection; |
| 26 | public IElementContainer<ISubmodelElement> Value { get; set; } |
| 27 | public bool AllowDuplicates { get; set; } = false; |
| 28 | public bool Ordered { get; set; } = false; |
| 29 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame^] | 30 | [IgnoreDataMember, JsonIgnore] |
| 31 | public IEnumerable<IElementContainer<ISubmodelElement>> Children => Value.Children; |
| 32 | [IgnoreDataMember, JsonIgnore] |
| 33 | public IEnumerable<ISubmodelElement> Values => Value.Values; |
| 34 | [IgnoreDataMember, JsonIgnore] |
| 35 | ISubmodelElement IElementContainer<ISubmodelElement>.Value { get => this; set { } } |
| 36 | [IgnoreDataMember, JsonIgnore] |
| 37 | public string Path |
Constantin Ziesche | 95d5109 | 2020-03-04 17:58:10 +0100 | [diff] [blame] | 38 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame^] | 39 | get |
| 40 | { |
| 41 | if (string.IsNullOrEmpty(Value.Path)) |
| 42 | return IdShort; |
| 43 | else |
| 44 | return Value.Path; |
| 45 | } |
| 46 | set { Value.Path = value; } |
| 47 | } |
| 48 | [IgnoreDataMember, JsonIgnore] |
| 49 | public bool IsRoot => Value.IsRoot; |
| 50 | [IgnoreDataMember, JsonIgnore] |
| 51 | public IElementContainer<ISubmodelElement> ParentContainer { get => this; set { } } |
| 52 | |
| 53 | [IgnoreDataMember, JsonIgnore] |
| 54 | public ISubmodelElement this[string idShort] => Value[idShort]; |
| 55 | [IgnoreDataMember, JsonIgnore] |
| 56 | public ISubmodelElement this[int i] => Value[i]; |
| 57 | |
| 58 | public SubmodelElementCollection(string idShort) : base(idShort) |
| 59 | { |
| 60 | Value = new ElementContainer<ISubmodelElement>(this.Parent, this, null); |
| 61 | |
| 62 | Get = element => { return new ElementValue(Value, new DataType(DataObjectType.AnyType)); }; |
| 63 | Set = (element, value) => { Value = value?.Value as IElementContainer<ISubmodelElement>; }; |
| 64 | } |
| 65 | |
| 66 | public IResult<IQueryableElementContainer<ISubmodelElement>> RetrieveAll() |
| 67 | { |
| 68 | return Value.RetrieveAll(); |
| 69 | } |
| 70 | |
| 71 | public IResult<IQueryableElementContainer<ISubmodelElement>> RetrieveAll(Predicate<ISubmodelElement> predicate) |
| 72 | { |
| 73 | return Value.RetrieveAll(predicate); |
| 74 | } |
| 75 | |
| 76 | public bool HasChildren() |
| 77 | { |
| 78 | return Value.HasChildren(); |
| 79 | } |
| 80 | |
| 81 | public bool HasChild(string idShort) |
| 82 | { |
| 83 | return Value.HasChild(idShort); |
| 84 | } |
| 85 | |
| 86 | public bool HasChildPath(string idShortPath) |
| 87 | { |
| 88 | return Value.HasChildPath(idShortPath); |
| 89 | } |
| 90 | |
| 91 | public void Traverse(Action<ISubmodelElement> action) |
| 92 | { |
| 93 | Value.Traverse(action); |
| 94 | } |
| 95 | |
| 96 | public void Add(ISubmodelElement element) |
| 97 | { |
| 98 | Value.Add(element); |
| 99 | } |
| 100 | |
| 101 | public void AddRange(IEnumerable<ISubmodelElement> elements) |
| 102 | { |
| 103 | Value.AddRange(elements); |
| 104 | } |
| 105 | |
| 106 | IResult<T> ICrudContainer<string, ISubmodelElement>.Create<T>(T element) |
| 107 | { |
| 108 | return Value.Create(element); |
| 109 | } |
| 110 | |
| 111 | public IResult<ISubmodelElement> Retrieve(string id) |
| 112 | { |
| 113 | return Value.Retrieve(id); |
| 114 | } |
| 115 | |
| 116 | IResult<T> ICrudContainer<string, ISubmodelElement>.Retrieve<T>(string id) |
| 117 | { |
| 118 | return Value.Retrieve<T>(id); |
| 119 | } |
| 120 | |
| 121 | IResult<IQueryableElementContainer<T>> ICrudContainer<string, ISubmodelElement>.RetrieveAll<T>() |
| 122 | { |
| 123 | return Value.RetrieveAll<T>(); |
| 124 | } |
| 125 | |
| 126 | IResult<IQueryableElementContainer<T>> ICrudContainer<string, ISubmodelElement>.RetrieveAll<T>(Predicate<T> predicate) |
| 127 | { |
| 128 | return Value.RetrieveAll(predicate); |
| 129 | } |
| 130 | |
| 131 | public IResult<ISubmodelElement> CreateOrUpdate(string id, ISubmodelElement element) |
| 132 | { |
| 133 | return Value.CreateOrUpdate(id, element); |
| 134 | } |
| 135 | |
| 136 | public IResult<ISubmodelElement> Update(string id, ISubmodelElement element) |
| 137 | { |
| 138 | return Value.Update(id, element); |
| 139 | } |
| 140 | |
| 141 | public IResult Delete(string id) |
| 142 | { |
| 143 | return Value.Delete(id); |
| 144 | } |
| 145 | |
| 146 | public IEnumerator<ISubmodelElement> GetEnumerator() |
| 147 | { |
| 148 | return Value.GetEnumerator(); |
| 149 | } |
| 150 | |
| 151 | IEnumerator IEnumerable.GetEnumerator() |
| 152 | { |
| 153 | return Value.GetEnumerator(); |
| 154 | } |
| 155 | |
| 156 | public IElementContainer<ISubmodelElement> GetChild(string idShortPath) |
| 157 | { |
| 158 | return Value.GetChild(idShortPath); |
| 159 | } |
| 160 | |
| 161 | public void Remove(string idShort) |
| 162 | { |
| 163 | Value.Remove(idShort); |
| 164 | } |
| 165 | |
| 166 | public void AppendRootPath(string rootPath) |
| 167 | { |
| 168 | Value.AppendRootPath(rootPath); |
| 169 | } |
| 170 | |
| 171 | public IEnumerable<ISubmodelElement> Flatten() |
| 172 | { |
| 173 | return Value.Flatten(); |
| 174 | } |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 175 | } |
| 176 | } |