Does MathWorks Simulink prohibit multiple inheritance for system objects? - matlab

The Problem:
I am trying to use some System Objects inside a 'User-defined Function' block in Simulink.
When I try to run the block, I get an error saying the following -
Diamond-shape inheritance is not supported in code generation. Class 'X' inherits from base class 'Y' via two or more paths.
Background: Class X inherits from classes A, B, C - all of which inherit from class Y. There is no conflict between any property/method/class definition between any of these classes. All of them implement their own unique methods, and there is no overloading of a common method/property.
Class X works in MATLAB - I have been able to successfully work with instances of class X.
What I'm not sure of is whether Simulink supports multiple inheritance / diamond shape inheritance, when it is guaranteed that there are no conflicts.
So I would be very interested in finding out if I can make use of class X in Simulink without having to refactor other classes to remove the multiple inheritance.
I have looked at the following MATLAB Central answer, and blog post trying to understand whether multiple inheritance poses any issues.
https://www.mathworks.com/matlabcentral/answers/93189-how-does-matlab-solve-the-diamond-problem-in-multiple-inheritance
https://blogs.mathworks.com/developer/2015/06/08/diamond-of-death/
But I couldn't find anything related to Simulink or Simulink Codegen

Related

What does "class" mean in Modelica?

I do not understand the meaning of class in Modelica context.
From the Modelica Tutorial, version 1.4 on https://modelica.org/publications.html:
In Modelica, the basic structuring element is a class. There are seven restricted classes with specific names, such as model...". Anyone have a simpler explanation? I am very new to Modelica.
If you open the Modelica library in a tool like Dymola or OpenModelica, everything you see in the package or library browser are classes.
As soon as you use one of these classes, e.g. with drag and drop in the diagram layer, you create a new component of this class type.
The instantiated component is not a copy of the class, but a reference to it. Therefore, if you update the class definition, you also update the behavior of all components that are instances of this class.
There are several kinds of classes available. The most general class is actually called class, but it’s not used very often.
It has no restrictions, so it can contain everything that is possible with Modelica: equations, algorithms, public and protected components, etc.
There are more specific class types, which restrict the usage. This helps to make correct use of a class. A function or a record for example cannot be simulated.
The most important restricted class types are:
package: used to group other classes
model: typically used for components with physical connectors and for examples which are simulated
block: used for components with causal connectors (only inputs and outputs, so everything you have in Modelica.Blocks)
function: used for function calls, comparable to other programming languages
record: often used to contain data sets for other components (which allows to quickly change a complete data set)
connector: used to define the interface variables which are needed to connect different components of a domain (e.g. v and i in the electric domain)
type: typically used to define physical quantities like mass, length or time with their unit (e.g. all units in the package Modelica.Units)
More information can be found in chapter 4.6 of the Modelica specification: Specialized Classes
This is just a collection of links to prove that there is an ongoing discussion on class within the Modelica Association:
class still a valid Modelica class type to use?
model no longer identical to class
What is the usage recommendation for class?
Restricted class for parameter record with initial equation

Swift structs: Protocol (w/asoc. types) v/s Composition

I’m refactoring a Linear Algebra library. It includes a Vector, Matrix and Tensor structs. So far so good, but since the three of them are conceptually Tensors, they share a lot of properties and underlying behavior. Therefore, I’m trying a refactor to be able to share more code and tests between them.
But I’m fairly new to Swift, and I’m not sure if I should do it with Protocols:
TensorProtocol with Associated Type (“T”).
Extension over TensorProtocol which implements all the common behavior as the “default” for the protocol.
Tensor, Matrix and Vector which conform to TensorProtocol.
The advantage of this approach is that common behavior is shared in one place, and can be overriden for customization.
The disadvantages are:
TensorProtocol doesn’t let me specify internal logic. I can only have public variables if I want to make the protocol public.
I can’t aggregate the structs for Testing, I think? Because Protocols with AT can’t be used as variable types.
Because of this I thought of a second option:
TensorSkeleton struct, with all the behavior of a Tensor inside of a struct.
TensorProtocol, which only defines the API of a Tensor.
Tensor, Matrix and Vector, all of which contain a TensorSkeleton and use it to conform to the TensorProtocol.
This approach allows me to:
Reuse all of the Tensor logic for the three structs.
TensorSkeleton can have a finer design than a protocol. Since it’s a struct, it can have internal and private fields and functions.
Testing the TensorSkeleton is (at least for me) easier than it would be to test a Protocol. And by testing it, I’m giving guarantees over all of the structs that use it.
But it has two disadvantages:
It still doesn’t allow me to aggregate for Testing (same as the first approach), and...
This model clashes with me. I come from C#, where this would’ve been solved with abstract classes and class inheritance (Matrix : Tensor, and such). Is my concern justified? I have never used a model where inherited traits were passed down with composition instead of class inheritance.
I know Linear Algebra objects are much better suited as structs, but I still have doubts about this design.
Thank you very much, and I’m sorry for the wall of text. Have a great day.

Object-languages misdescribed by UML?

I've read that UML assumes by default that :
a class can inherit several others
an object is an instance of only one class
an object of a given class cannot change to another class
This leads me to the question : as there are 3 hypothesis, there are 2^3 possible combinations. Could you give me languages which would be examples of each of them ?
I mean for me Java is "false-true-true" and C++ is "true-true-true". What about the 6 others ? Or did I misinterpret the assumptions ?
Let's look at the UML 2.5 standard of the OMG, to have a definitive answer:
1.Class inheritance
The UML 2.5 standard clearly defines that a class can have none or several superclasses and, that conversely, a class can be superclass of none or several classes (see section 11.4.2 and 11.8.3.6).
So UML definitively allows multiple inheritance (as in C++ or Python). But you may as well restrict yourself and use only single inheritance and several interface implementations, like in Java and C#. You'd use a realization relationship to show the "inheritance" from an abstract interface (the inheritance arrow is then dotted).
2. Objects and classes
9.8.1: InstanceSpecifications represent instances of Classifiers in a modeled
system. They are often used to model example configurations of
instances.
FYI: the terms used in the standard are a little more general, but an object is an instance, and a class a classifier. This definition is then further refined in the semantcs in chapter 9.8.3 :
The InstanceSpecification may represent: • Classification of the
instance by one or more Classifiers, any of which may be abstract.
So UML allows objects to be an instantiation of several classes. I don't know languages that allow this, but if you do don't hesitate to comment ;-).
3. Changing class of object
I must admit that I can't answer this answer 100%. I don't think so, because, becoming an instance of another class would mean to re-insantiate a class, so it's not corresponding anymore to the definition of an instantiation.
Furthermore (see 9.8.3):
An InstanceSpecification may represent an instance at a point in time
(a snapshot). Changes to the instance may be modeled using multiple
InstanceSpecification, one for each snapshot.
This is somewhat ambiguous: a given object in a given diagram can't change classes. However, you can represent several times the object in different diagrams (snapshot) to show a change.
Conclusions
So your assumption 1 is true, 2 is false, and 3 true or false depending if you're reasoning at diagram or model level.

Access methods with other classes in MATLAB

I am teaching myself how to use object oriented programming with MATLAB and have a question about access.
I have three current classes that need to interact with each other. One class, which I'll call main, is what the user will interface with (or the gui if I build one). Main stores all of the pertinent data that the use may want and has a few methods to perform some preprocessing and to construct the main object. Main also calls the constructors for the two other classes.
Another class, I'll call it instruction, loads information about the steps to process the data (this is a recursive process) and some other information.
The final class, which I'll call core, performs the core operations of the process.
Heres what my question is. Within main I have some "helper" methods that are used in the preprocessing. I want the access to these helper methods to be private so that the user cannot see or use them. Some of these helper functions also need to be used by the processes in core. My question is how do I grant access to the helper functions in main so that only main and core can access them? I have tried to understand the information provided here: http://www.mathworks.com/help/matlab/matlab_oop/selective-access-to-class-methods.html, but when I try something like:
classdef main < handle
%this is the main class
properties
core %the core object
instruction %the instruction object
end %properties
methods (Access = {?core,?main})
... %some code
end %methods
end %class
Matlab gives me this error:
Illegal value for attribute 'Access'.
Parameter must be a string.
Any help would be greatly appreciated!
Andrew
Btw, I realize that having three different classes is unnecessary here but as I stated I am just learning object oriented programming and when I started this project I thought it would be a good idea to have multiple classes because the total project will be over 5000 lines of code.
Is it possible you're using a relatively old version of MATLAB?
The ability to give access to class methods to specific classes was implemented in release 2012a.
If you're using a version of MATLAB older than that, you can only set method access attributes to public, protected or private.
Note that the documentation page you link to always refers to the current version of MATLAB (2014a at the moment). You can access old versions of the documentation via the website as well by logging into your MathWorks account.
If you have methods of main that need to be accessed by core, that could be a sign that you've designed your class relationships poorly. If core is a property of main, then perhaps it could be a method of core, and main could call it via core.
Selective access is needed quite rarely. The main reason for not using it, is that it breaks the encapsulation principle of OOP. I would consider changing your design, before implementing it.
For more information, check out the friend keyword in C++. Most likely you will find a lot of information on it, and why not to use it.

What does it mean in UML that instance could realize more than 1 classifier?

Does any programming language provide such a thing?
Where could this be used?
For example:
note that somethingStrange is not a class, its an instance (its underlined) and this is an object diagram
Spec (section 7.3.22) says:
An instance specification is depicted using the same notation as its classifier, but in place of the classifier name appears an underlined concatenation of the instance name (if any), a colon (‘:’) and the classifier name or names.
The convention for showing multiple classifiers is to separate their names by commas.
So im stuck with "multiple classifiers".
Any language with extensional rather than intensional typing will allow such constructs.
For example, in RDF two sources could make claims about a web resource which are completely conflicting, or in a 'duck type' language an object could have all the characteristics of two otherwise unrelated types.
Extensional languages classify objects by their properties - if it has prongs it's a fork, if it's got a handle and a bowl it's a spoon, if it has both prongs and a bowl it is both a fork and a spoon.
The difference between such languages and class oriented intensional languages such as C++/Java/C# to which UML is more commonly applied, is that you don't need a spork class to define things which are both spoons and forks - whether things belong to a classifier is defined by whether they meet the requirements of the classifier.
That's multiple inheritance if you're referring to classes (except that you should use solid edges for generalization), nothing wrong with that ;)
Note that an interface is also a classifier, so also the text of your question needs a bit of refinement -- nothing wrong with generalizing more than one interface, after all.
It's is a Dependency.
Dependency is a weaker form of relationship which indicates that one class depends on another because it uses it at some point of time. One class depends on another if the latter is a parameter variable or local variable of a method of the former. This is different from an association, where an attribute of the former is an instance of the latter.
In other words your somethingStance class will use both Cat and Panzer
The below it is just an example of how it might look like
Public class SomethingStrange{
public Cat CatDependency{get;set;}
public Panzer PanzerDependency{get;set;}
}
UML does allow an object to be instance of several different classes (even if they are unrelated) at the same time. The fact that this is not the normal convention and not supported by programming languages is a different issue. UML tries to be as broad as possible even if specific technologies only can implement a subset of it.