Hello, I have a problem. I want to build a library where I sometimes need elements that should only be included once. The following example illustrates this: I want the user to be able to attach to “mqttcred” only, not to two. Is this possible, or am I misunderstanding something? Secondly, I have a question about the “broker” attribute. How can I ensure that the user redefines it and does not subset it? Because they could easily subset ‘broker’, and then I would have multiple ‘broker’ instances in ‘mqtt cred’, which makes no sense. Maybe it works with assert constraints but jupyter doesn’t evaluate those.
library package example{
private import ScalarValues::*;
private import Metaobjects::SemanticMetadata;
part def MQTT_CREDS{
attribute broker:ScalarValues::String;
attribute url:ScalarValues::String;
}
part def Communication{
part mqtt_creds:MQTT_CREDS;
}
metadata def <mqqtcred> MData :> SemanticMetadata {
:>> baseType default Communication::mqtt_creds meta SysML::PartUsage;
:>> annotatedElement:SysML::PartUsage;
}
metadata def <comm> CommData :> SemanticMetadata {
:>> baseType default Communication meta SysML::PartDefinition;
:>> annotatedElement:SysML::PartDefinition;
}
#comm part def {
#mqqtcred part example{
:>>broker = "testbroker";
:>>url = "testurl";
}
}
// but the user still can place multiple mqqtcreds
#comm part def {
#mqqtcred part example{
:>>broker = "testbroker";
:>>url = "testurl";
}
#mqqtcred part example2{
:>>broker = "testbroker";
:>>url = "testurl";
}
}
}
I want the user to be able to attach to “mqttcred” only, not to two. Is this possible, or am I misunderstanding something?
Could you clarify what you mean by “attach to” here? The use of #indicates a metadata annotation, meaning it communicates something about the model element itself (here the various part usages called example and example2). Do you mean that you want to ensure that at most one model element in the model is annotated by the metadata definition mqqtcred? If so, then I am not aware of a natural way of doing this using purely SysMLv2. Using a Python script and Syside Automator you could write a custom script that counts the number of model elements annotated by mqqcred, communicating an error or warning in case the count is greater than 1.
To give a better response I would have to better understand the context of what you’re trying to achieve.
Secondly, I have a question about the “broker” attribute. How can I ensure that the user redefines it and does not subset it? Because they could easily subset ‘broker’, and then I would have multiple ‘broker’ instances in ‘mqtt cred’, which makes no sense. Maybe it works with assert constraints but jupyter doesn’t evaluate those.
Again, there is no direct way to express this without a custom script. I suspect that what you actually want to do, though, is to add a multiplicity to the broker attribute
attribute broker: ScalarValues::String[1];
This indicates that any MQTT_CREDS will have exactly one broker value.
Note that it is still syntactically valid to subset broker multiple times, but this would imply that each subsetted attribute is either empty, or equal to all other non-empty broker.
Yes, you’re right. By ‘attach’, I mean applying the user-defined keyword to an element. But you’ve already answered my question, and it aligns with the information I found online. In SysML it is not possible. However, I think it may be possible with constraints. I saw in another forum that you can create constraints in the meta definition and use the Kerml language to write constraints.l. My idea now is to create a constraint which is false when the mqttcred is applied to more than one element.