blob: 99c42bb548807de8bd22ebc3fba8c07b65347b30 [file] [log] [blame]
Constantin Ziesche857c7ab2020-02-25 11:24:51 +01001/*******************************************************************************
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*******************************************************************************/
11using BaSyx.Models.Core.AssetAdministrationShell.Generics;
Constantin Ziesche857c7ab2020-02-25 11:24:51 +010012using BaSyx.Models.Core.Common;
Constantin Ziesche08215502020-09-21 19:08:32 +020013using BaSyx.Utils.ResultHandling;
14using Newtonsoft.Json;
15using System;
16using System.Collections;
17using System.Collections.Generic;
Constantin Ziesche857c7ab2020-02-25 11:24:51 +010018using System.Runtime.Serialization;
19
Constantin Ziesche08215502020-09-21 19:08:32 +020020namespace BaSyx.Models.Core.AssetAdministrationShell.Implementations
Constantin Ziesche857c7ab2020-02-25 11:24:51 +010021{
Constantin Ziesche08215502020-09-21 19:08:32 +020022 [DataContract, JsonObject]
23 public class SubmodelElementCollection : SubmodelElement, ISubmodelElementCollection, IElementContainer<ISubmodelElement>
Constantin Ziesche857c7ab2020-02-25 11:24:51 +010024 {
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 Ziesche08215502020-09-21 19:08:32 +020030 [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 Ziesche95d51092020-03-04 17:58:10 +010038 {
Constantin Ziesche08215502020-09-21 19:08:32 +020039 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
Constantin Ziesche0399d412020-09-24 14:31:15 +020053 public int Count => Value.Count;
54
55 public bool IsReadOnly => Value.IsReadOnly;
56
Constantin Ziesche08215502020-09-21 19:08:32 +020057 [IgnoreDataMember, JsonIgnore]
58 public ISubmodelElement this[string idShort] => Value[idShort];
59 [IgnoreDataMember, JsonIgnore]
60 public ISubmodelElement this[int i] => Value[i];
61
62 public SubmodelElementCollection(string idShort) : base(idShort)
63 {
64 Value = new ElementContainer<ISubmodelElement>(this.Parent, this, null);
65
66 Get = element => { return new ElementValue(Value, new DataType(DataObjectType.AnyType)); };
67 Set = (element, value) => { Value = value?.Value as IElementContainer<ISubmodelElement>; };
68 }
69
70 public IResult<IQueryableElementContainer<ISubmodelElement>> RetrieveAll()
71 {
72 return Value.RetrieveAll();
73 }
74
75 public IResult<IQueryableElementContainer<ISubmodelElement>> RetrieveAll(Predicate<ISubmodelElement> predicate)
76 {
77 return Value.RetrieveAll(predicate);
78 }
79
80 public bool HasChildren()
81 {
82 return Value.HasChildren();
83 }
84
85 public bool HasChild(string idShort)
86 {
87 return Value.HasChild(idShort);
88 }
89
90 public bool HasChildPath(string idShortPath)
91 {
92 return Value.HasChildPath(idShortPath);
93 }
94
95 public void Traverse(Action<ISubmodelElement> action)
96 {
97 Value.Traverse(action);
98 }
99
100 public void Add(ISubmodelElement element)
101 {
102 Value.Add(element);
103 }
104
105 public void AddRange(IEnumerable<ISubmodelElement> elements)
106 {
107 Value.AddRange(elements);
108 }
109
110 IResult<T> ICrudContainer<string, ISubmodelElement>.Create<T>(T element)
111 {
112 return Value.Create(element);
113 }
114
115 public IResult<ISubmodelElement> Retrieve(string id)
116 {
117 return Value.Retrieve(id);
118 }
119
120 IResult<T> ICrudContainer<string, ISubmodelElement>.Retrieve<T>(string id)
121 {
122 return Value.Retrieve<T>(id);
123 }
124
125 IResult<IQueryableElementContainer<T>> ICrudContainer<string, ISubmodelElement>.RetrieveAll<T>()
126 {
127 return Value.RetrieveAll<T>();
128 }
129
130 IResult<IQueryableElementContainer<T>> ICrudContainer<string, ISubmodelElement>.RetrieveAll<T>(Predicate<T> predicate)
131 {
132 return Value.RetrieveAll(predicate);
133 }
134
135 public IResult<ISubmodelElement> CreateOrUpdate(string id, ISubmodelElement element)
136 {
137 return Value.CreateOrUpdate(id, element);
138 }
139
140 public IResult<ISubmodelElement> Update(string id, ISubmodelElement element)
141 {
142 return Value.Update(id, element);
143 }
144
145 public IResult Delete(string id)
146 {
147 return Value.Delete(id);
148 }
149
150 public IEnumerator<ISubmodelElement> GetEnumerator()
151 {
152 return Value.GetEnumerator();
153 }
154
155 IEnumerator IEnumerable.GetEnumerator()
156 {
157 return Value.GetEnumerator();
158 }
159
160 public IElementContainer<ISubmodelElement> GetChild(string idShortPath)
161 {
162 return Value.GetChild(idShortPath);
163 }
164
165 public void Remove(string idShort)
166 {
167 Value.Remove(idShort);
168 }
169
170 public void AppendRootPath(string rootPath)
171 {
172 Value.AppendRootPath(rootPath);
173 }
174
175 public IEnumerable<ISubmodelElement> Flatten()
176 {
177 return Value.Flatten();
178 }
Constantin Ziesche0399d412020-09-24 14:31:15 +0200179
180 public void Clear()
181 {
182 Value.Clear();
183 }
184
185 public bool Contains(ISubmodelElement item)
186 {
187 return Value.Contains(item);
188 }
189
190 public void CopyTo(ISubmodelElement[] array, int arrayIndex)
191 {
192 Value.CopyTo(array, arrayIndex);
193 }
194
195 public bool Remove(ISubmodelElement item)
196 {
197 return Value.Remove(item);
198 }
Constantin Ziesche857c7ab2020-02-25 11:24:51 +0100199 }
200}