Skip to content

Commit 7a1de31

Browse files
committed
* a little scripting for
testing
1 parent 2ad36b0 commit 7a1de31

File tree

2 files changed

+124
-16
lines changed

2 files changed

+124
-16
lines changed

src/AasxPackageLogic/AasxScript.cs

+59
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public interface IAasxScriptRemoteInterface
3333
{
3434
Task<int> Tool(object[] args);
3535
Aas.IReferable Select(object[] args);
36+
Aas.IReferable[] SelectAll(object[] args);
3637
Task<bool> Location(object[] args);
3738
}
3839

@@ -299,6 +300,63 @@ public override object Invoke(IScriptContext context, object[] args)
299300
}
300301
}
301302

303+
public class Script_SelectAll : ScriptInvokableBase
304+
{
305+
public Script_SelectAll(AasxScript script) : base(script)
306+
{
307+
script?.AddHelpInfo("SelectAll",
308+
"Returns all Referables of a certain kind fron the active environment.",
309+
args: new AasxMenuListOfArgDefs()
310+
.Add("<ref. type>", "String indicating Referable type, such as AAS, SM, CD. ")
311+
.Add("returns:", "Enumeration of Referables of certain kind."));
312+
}
313+
314+
public override object Invoke(IScriptContext context, object[] args)
315+
{
316+
// access
317+
if (_script == null)
318+
return -1;
319+
320+
if (args == null || args.Length < 1 || !(args[0] is string))
321+
{
322+
_script.ScriptLog?.Error("Script: SelectAll: Referable type missing");
323+
return -1;
324+
}
325+
326+
// debug
327+
if (_script._logLevel >= 2)
328+
Console.WriteLine($"Execute SelectAll " + string.Join(",", args));
329+
330+
// which application
331+
if (Application.Current == null)
332+
{
333+
Log.Singleton.Error("For script execution, Application.Current for Blazor is not available.");
334+
}
335+
336+
// invoke action
337+
// https://stackoverflow.com/questions/39438441/
338+
Aas.IReferable[] x = null;
339+
if (Application.Current != null)
340+
{
341+
// WPF case
342+
x = Application.Current?.Dispatcher.Invoke(() =>
343+
{
344+
return _script.Remote?.SelectAll(args);
345+
});
346+
if (x != null)
347+
Log.Singleton.Silent("" + x.Count());
348+
}
349+
else
350+
{
351+
// Blazor?? case
352+
x = _script.Remote?.SelectAll(args);
353+
}
354+
355+
// done
356+
return x;
357+
}
358+
}
359+
302360
public class Script_Location : ScriptInvokableBase
303361
{
304362
public Script_Location(AasxScript script) : base(script)
@@ -482,6 +540,7 @@ public void StartEnginBackground(
482540
s.Context.Scope.SetItem("FileReadAll", new Script_FileReadAll(this));
483541
s.Context.Scope.SetItem("FileExists", new Script_FileExists(this));
484542
s.Context.Scope.SetItem("Select", new Script_Select(this));
543+
s.Context.Scope.SetItem("SelectAll", new Script_SelectAll(this));
485544
s.Context.Scope.SetItem("Location", new Script_Location(this));
486545
s.Context.Scope.SetItem("System", new Script_System(this));
487546
if (_logLevel >= 2)

src/AasxPackageLogic/MainWindowScripting.cs

+65-16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This source code may use other Open Source software components (see LICENSE.txt)
1515
using AnyUi;
1616
using Extensions;
1717
using System;
18+
using System.Collections.Generic;
1819
using System.Linq;
1920
using System.Threading.Tasks;
2021
using Aas = AasCore.Aas3_0;
@@ -299,41 +300,48 @@ public enum ScriptSelectAdressMode { None = 0, First, Next, Prev, idShort, seman
299300
return null;
300301
}
301302

302-
Aas.IReferable IAasxScriptRemoteInterface.Select(object[] args)
303+
protected ScriptSelectRefType GetScriptRefType(string refTypeName)
303304
{
304-
// access
305-
if (args == null || args.Length < 1
306-
|| !(args[0] is string refTypeName))
307-
{
308-
Log.Singleton.Error("Script: Select: Referable type missing!");
309-
return null;
310-
}
311-
312-
// check if Referable Type is ok
313-
ScriptSelectRefType refType = ScriptSelectRefType.None;
305+
ScriptSelectRefType res = ScriptSelectRefType.None;
314306

315307
switch (refTypeName.Trim().ToLower())
316308
{
317309
case "this":
318-
refType = ScriptSelectRefType.This;
310+
res = ScriptSelectRefType.This;
319311
break;
320312
case "aas":
321313
case "assetadministrationshell":
322-
refType = ScriptSelectRefType.AAS;
314+
res = ScriptSelectRefType.AAS;
323315
break;
324316
case "sm":
325317
case "submodel":
326-
refType = ScriptSelectRefType.SM;
318+
res = ScriptSelectRefType.SM;
327319
break;
328320
case "sme":
329321
case "submodelelement":
330-
refType = ScriptSelectRefType.SME;
322+
res = ScriptSelectRefType.SME;
331323
break;
332324
case "cd":
333325
case "conceptdescription":
334-
refType = ScriptSelectRefType.CD;
326+
res = ScriptSelectRefType.CD;
335327
break;
336328
}
329+
330+
return res;
331+
}
332+
333+
Aas.IReferable IAasxScriptRemoteInterface.Select(object[] args)
334+
{
335+
// access
336+
if (args == null || args.Length < 1
337+
|| !(args[0] is string refTypeName))
338+
{
339+
Log.Singleton.Error("Script: Select: Referable type missing!");
340+
return null;
341+
}
342+
343+
// check if Referable Type is ok
344+
var refType = GetScriptRefType(refTypeName);
337345

338346
if (refType == ScriptSelectRefType.None)
339347
{
@@ -381,6 +389,47 @@ Aas.IReferable IAasxScriptRemoteInterface.Select(object[] args)
381389
return null;
382390
}
383391

392+
Aas.IReferable[] IAasxScriptRemoteInterface.SelectAll(object[] args)
393+
{
394+
// access
395+
if (args == null || args.Length < 1
396+
|| !(args[0] is string refTypeName))
397+
{
398+
Log.Singleton.Error("Script: SelectAll: Referable type missing!");
399+
return new IReferable[0];
400+
}
401+
402+
// check if Referable Type is ok
403+
var refType = GetScriptRefType(refTypeName);
404+
405+
if (!(refType == ScriptSelectRefType.AAS
406+
|| refType == ScriptSelectRefType.SM
407+
|| refType == ScriptSelectRefType.CD))
408+
{
409+
Log.Singleton.Error("Script: SelectAll: Referable type invalid (no AAS, SM, CD)!");
410+
return new IReferable[0];
411+
}
412+
413+
// something to select
414+
var pm = PackageCentral?.Main?.AasEnv;
415+
if (pm == null)
416+
{
417+
Log.Singleton.Error("Script: SelectAll: No main package AAS environment available!");
418+
return new IReferable[0];
419+
}
420+
421+
// ok
422+
if (refType == ScriptSelectRefType.AAS)
423+
return pm.AllAssetAdministrationShells().ToArray();
424+
425+
if (refType == ScriptSelectRefType.SM)
426+
return pm.AllSubmodels().ToArray();
427+
428+
if (refType == ScriptSelectRefType.CD)
429+
return pm.AllConceptDescriptions().ToArray();
430+
431+
return new IReferable[0];
432+
}
384433

385434
public async Task<int> Tool(object[] args)
386435
{

0 commit comments

Comments
 (0)