Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
* update
Browse files Browse the repository at this point in the history
* introduce plugin for
   product change notifications
* export AsciiDoc
  • Loading branch information
festo-i40 committed Feb 28, 2024
1 parent 9412e93 commit 12820b0
Show file tree
Hide file tree
Showing 65 changed files with 3,413 additions and 515 deletions.
148 changes: 148 additions & 0 deletions src/AasxCsharpLibrary/AdminShellEnumHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
Copyright (c) 2018-2023 Festo SE & Co. KG <https://www.festo.com/net/de_de/Forms/web/contact_international>
Author: Michael Hoffmeister
This source code is licensed under the Apache License 2.0 (see LICENSE.txt).
This source code may use other Open Source software components (see LICENSE.txt).
*/

using AasxCompatibilityModels;
using Extensions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;

namespace AdminShellNS
{
/// <summary>
/// Some string based flags to attach to the property
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple = true)]
public class ExtensionFlagsAttribute : System.Attribute
{
public string Flags = "";

public ExtensionFlagsAttribute(string flags)
{
Flags = flags;
}
}

/// <summary>
/// This attribute attaches some hint text.
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple = false)]
public class ExtensionHintAttributeAttribute : System.Attribute
{
public string HintText = "";

public ExtensionHintAttributeAttribute(string hintText)
{
if (hintText != null)
HintText = hintText;
}
}

/// <summary>
/// This attribute attaches a max lines number to a property.
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property)]
public class ExtensionMultiLineAttribute : System.Attribute
{
public int? MaxLines = null;

public ExtensionMultiLineAttribute(int maxLines = -1)
{
if (maxLines > 0)
MaxLines = maxLines;
}
}

/// <summary>
/// This attribute attaches a display text.
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property)]
public class EnumMemberDisplayAttribute : System.Attribute
{
public string Text = "";

public EnumMemberDisplayAttribute(string text)
{
if (text != null)
Text = text;
}
}

/// <summary>
/// This class adds some helpers for handling enums.
/// </summary>
public static class AdminShellEnumHelper
{
public class EnumHelperMemberInfo
{
public string MemberValue = "";
public string MemberDisplay = "";
public object MemberInstance;
}

public static IEnumerable<EnumHelperMemberInfo> EnumHelperGetMemberInfo(Type underlyingType)
{
foreach (var enumMemberInfo in underlyingType.GetFields(BindingFlags.Public | BindingFlags.Static))
{
var enumInst = Activator.CreateInstance(underlyingType);

var memVal = enumMemberInfo.GetCustomAttribute<EnumMemberAttribute>()?.Value;
var memDisp = enumMemberInfo.GetCustomAttribute<EnumMemberDisplayAttribute>()?.Text;

if (memVal?.HasContent() == true)
{
var ev = enumMemberInfo.GetValue(enumInst);

yield return new EnumHelperMemberInfo()
{
MemberValue = memVal,
MemberDisplay = (memDisp?.HasContent() == true) ? memDisp : memVal,
MemberInstance = ev
};
}
}
}

public static T GetEnumMemberFromValueString<T>(string valStr, T valElse = default(T)) where T : struct
{
foreach (var em in EnumHelperGetMemberInfo(typeof(T)))
if (em.MemberValue.Equals(valStr?.Trim(), StringComparison.InvariantCultureIgnoreCase))
return (T)em.MemberInstance;
return (T)valElse;
}

/// <summary>
/// Use the EnumMemberDisplay attribute to resolve the text
/// </summary>
public static string GetEnumMemberDisplay(Type enumType, object enumValue)
{
// access
if (enumType == null || enumValue == null)
return "";

// see: https://stackoverflow.com/questions/1799370/getting-attributes-of-enums-value
var memberInfos = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
var enumValueMemberInfo = memberInfos.FirstOrDefault(m =>
m.Name == enumValue.ToString()
&& m.DeclaringType == enumType);
var valueAttributes =
enumValueMemberInfo.GetCustomAttributes(typeof(EnumMemberDisplayAttribute), false);
return ((EnumMemberDisplayAttribute)valueAttributes[0]).Text;
}
}
}
62 changes: 62 additions & 0 deletions src/AasxCsharpLibrary/AdminShellStringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright (c) 2018-2023 Festo SE & Co. KG <https://www.festo.com/net/de_de/Forms/web/contact_international>
Author: Michael Hoffmeister
This source code is licensed under the Apache License 2.0 (see LICENSE.txt).
This source code may use other Open Source software components (see LICENSE.txt).
*/

using AasxCompatibilityModels;
using Extensions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;

namespace AdminShellNS
{
public static class AdminShellStringExtensions
{
public static bool HasContent(this string str)
{
return str != null && str.Trim() != "";
}

public static string SubstringMax(this string str, int pos, int len)
{
if (!str.HasContent() || str.Length <= pos)
return "";
return str.Substring(pos, Math.Min(len, str.Length));
}

public static void SetIfNoContent(ref string s, string input)
{
if (!input.HasContent())
return;
if (!s.HasContent())
s = input;
}

public static string AddWithDelimiter(this string str, string content, string delimter = "")
{
var res = str;
if (res == null)
return null;

if (res.HasContent())
res += "" + delimter;

res += content;

return res;
}

}
}
32 changes: 17 additions & 15 deletions src/AasxCsharpLibrary/Extensions/ExtendEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,17 @@ public static AasCore.Aas3_0.IEnvironment ConvertFromV10(this AasCore.Aas3_0.IEn
}
foreach (var sourceAas in sourceEnvironement.AdministrationShells)
{
var newAssetInformation = new AssetInformation(AssetKind.Instance);
var newAas = new AssetAdministrationShell(
id: sourceAas.identification.id, newAssetInformation);
environment.AssetAdministrationShells.Add(newAas);

var sourceAsset = sourceEnvironement?.FindAsset(sourceAas.assetRef);
if (sourceAsset != null)
{
var newAssetInformation = new AssetInformation(AssetKind.Instance);
newAssetInformation = newAssetInformation.ConvertFromV10(sourceAsset);

var newAas = new AssetAdministrationShell(id: sourceAas.identification.id, newAssetInformation);
newAas = newAas.ConvertFromV10(sourceAas);

environment.AssetAdministrationShells.Add(newAas);
newAas.AssetInformation = newAssetInformation;
}

}
}

Expand Down Expand Up @@ -766,17 +765,20 @@ public static IReferable FindReferableByReference(
{
var submodelElement = submodelElems.Where(
sme => sme.IdShort.Equals(keyList[keyIndex].Value,
StringComparison.OrdinalIgnoreCase)).First();
StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

//This is required element
if (keyIndex + 1 >= keyList.Count)
if (submodelElement != null)
{
return submodelElement;
}
//This is required element
if (keyIndex + 1 >= keyList.Count)
{
return submodelElement;
}

//Recurse again
if (submodelElement?.EnumeratesChildren() == true)
return environment.FindReferableByReference(reference, ++keyIndex, submodelElement.EnumerateChildren());
//Recurse again
if (submodelElement?.EnumeratesChildren() == true)
return environment.FindReferableByReference(reference, ++keyIndex, submodelElement.EnumerateChildren());
}
}

//Nothing in this environment
Expand Down
23 changes: 15 additions & 8 deletions src/AasxCsharpLibrary/Extensions/ExtendQualifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,24 +130,31 @@ public static string ToStringExtended(this IQualifier q,

#region QualifierCollection

public static IQualifier FindQualifierOfType(this List<IQualifier> qualifiers, string qualifierType)
public static IQualifier FindQualifierOfType(this IEnumerable<IQualifier> qualifiers, string qualifierType)
{
if (qualifierType == null)
{
if (qualifiers == null || qualifierType == null)
return null;
}

foreach (var qualifier in qualifiers)
{
if (qualifier != null && qualifierType.Equals(qualifier.Type))
{
return qualifier;
}
}

return null;
}

public static IEnumerable<IQualifier> FindQualifierOfAnyType(
this IEnumerable<IQualifier> qualifiers, string[] qualifierTypes)
{
if (qualifierTypes == null || qualifierTypes.Length < 1)
yield break;
foreach (var qualifierType in qualifierTypes)
{
var res = FindQualifierOfType(qualifiers, qualifierType);
if (res != null)
yield return res;
}
}

// ReSharper disable MethodOverloadWithOptionalParameter .. this seems to work, anyhow
// ReSharper disable RedundantArgumentDefaultValue
public static string ToStringExtended(this List<IQualifier> qualifiers,
Expand Down
27 changes: 22 additions & 5 deletions src/AasxIntegrationBase/AasxSearchUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This source code is licensed under the Apache License 2.0 (see LICENSE.txt).
This source code may use other Open Source software components (see LICENSE.txt).
*/

using AdminShellNS;
using Extensions;
using System;
using System.Collections;
Expand Down Expand Up @@ -263,7 +264,9 @@ public static IEnumerable<EmulateAttribute> EnumerateEmulateAttributes(Aas.IClas
/// </summary>
public static void CheckSearchable(
SearchResults results, SearchOptions options, string qualifiedNameHead, object businessObject,
MemberInfo mi, object memberValue, object containingObject, Func<int> getMemberHash,
MemberInfo mi, object memberValue, object containingObject,
Func<int> getMemberHash,
object foundObjectToDisplay,
Action<bool, int> progress = null, LogInstance log = null)
{
// try get a speaking name
Expand Down Expand Up @@ -294,7 +297,6 @@ public static void CheckSearchable(

var isMLP = (businessObject is Aas.MultiLanguageProperty);


if (!options.SearchCollection && isColl)
return;

Expand Down Expand Up @@ -340,7 +342,7 @@ public static void CheckSearchable(
sri.metaModelName = metaModelName;
sri.businessObject = businessObject;
sri.foundText = foundText;
sri.foundObject = memberValue;
sri.foundObject = foundObjectToDisplay;
sri.containingObject = containingObject;
if (getMemberHash != null)
sri.foundHash = getMemberHash();
Expand Down Expand Up @@ -666,6 +668,11 @@ public static void EnumerateSearchableNew(
// report a "false" progress
progress?.Invoke(false, 0);

if (businessObject is Aas.Blob)
{
;
}

// look at fields, first
var fields = objType.GetFields();
foreach (var fi in fields)
Expand All @@ -684,7 +691,7 @@ public static void EnumerateSearchableNew(
// field is a single entity .. check it
CheckSearchable(
results, options, qualifiedName, businessObject, fi, fieldValue, obj,
() => { return fieldValue.GetHashCode(); }, progress, log);
() => { return fieldValue.GetHashCode(); }, fieldValue, progress, log);
}
}

Expand All @@ -705,13 +712,23 @@ public static void EnumerateSearchableNew(
if (valueElems != null)
{
// property is a collection
// some special case: Blob having a text inside
if (propValue is byte[] pvbytes
&& businessObject is Aas.IBlob blb
&& AdminShellUtil.CheckForTextContentType(blb.ContentType))
{
string strData = System.Text.Encoding.UTF8.GetString(pvbytes);
CheckSearchable(
results, options, qualifiedName, businessObject, pi, strData, obj,
() => { return propValue.GetHashCode(); }, propValue, progress, log);
}
}
else
{
// field is a single entity .. check it
CheckSearchable(
results, options, qualifiedName, businessObject, pi, propValue, obj,
() => { return propValue.GetHashCode(); }, progress, log);
() => { return propValue.GetHashCode(); }, propValue, progress, log);
}
}

Expand Down
Loading

0 comments on commit 12820b0

Please sign in to comment.