Static List of all Objects of Class - uml

I wanted to implement a static List for my Class which contains all Objects of the Class like in the following UML approach.
Is this the right way to write the UML?
There is also a question which uses this UML if you know Kotlin i would be happy if you can help me there too ;) Static List of Objects out Class in Kotlin

This is almost correct. Instead of List<IMyClass> the regular UML notation is IMyClass[*].
The UML 2.5.1 specification, section 9.5.4 gives the following notation syntax for a property:
<property> ::= [<visibility>] [‘/’] <name> [‘:’ <prop-type>] [‘[‘ <multiplicity-range> ‘]’] [‘=’ <default>] [‘{‘<prop-modifier > [‘,’ <prop-modifier >]* ’}’]
In your case, the prop-type is IMyClass and the multiplicity-range is *.

Use the stereotype to specify you use a list, for instance :
or if you prefer :
Note : are you sure you want it public ? this is dangerous
Edit for the remark of #qwerty_so: it does not make sense to stereotype an association with <<list>> where you place a multiplicity which makes it a "list" already. Looks like an over-definition too me
The multiplicity indicates a collection, and there are four types of collection depending on isOrdered and isUnique properties (c.f. formal-17-12-05 §7.5.3.2 Multiplicities, Table 7.1 Collection types for MultiplicityElements, page 34) :
isOrdered | isUnique | Collection Type
-----------+----------+-----------------
false | true | Set
true | true | OrderedSet
false | false | Bag
true | false | Sequence
Furthermore a list is one of the subtypes of Sequence, so the multiplicity "*" is far from indicating a "list" already.

Related

Creating regex restriction on OWL class

I'm trying to create a simple ontology that has two classes: class1 and class2,- and two instances that have simple text data property with the same name (hasName: "string1"^^xsd:string and hasName "string2"^^xsd:string respectivly). I want to classify these instances with reasoner to the respective classes based on regular expression (for example, the restriction for class 1 would be hasName some xsd:string[pattern "string1"], and such, the reasoner should infer that instance1 belongs to class1, but instance2 is not). How can it be done?
Using Openllet(2.6.2-SNAPSHOT) you can do things like that :
final OWLNamedIndividual x1 = OWL.Individual("#I1");
final OWLNamedIndividual x2 = OWL.Individual("#I2");
owl.addAxiom(OWL.equivalentClasses(ClsA, OWL.some(propB, OWL.restrict(XSD.STRING, OWL._factory.getOWLFacetRestriction(OWLFacet.PATTERN, OWL.constant("A.A"))))));
owl.addAxiom(OWL.propertyAssertion(x1, propB, OWL.constant("AAA")));
owl.addAxiom(OWL.propertyAssertion(x2, propB, OWL.constant("BBB")));
owl.addAxiom(OWL.differentFrom(x1, x2));
final OpenlletReasoner r = owl.getReasoner();
assertTrue(r.isEntailed(OWL.classAssertion(x1, ClsA)));
assertFalse(r.isEntailed(OWL.classAssertion(x2, ClsA)));
As you can see the line :
OWL.restrict(XSD.STRING, OWL._factory.getOWLFacetRestriction(OWLFacet.PATTERN, OWL.constant("A.A"))))));
is the one that add the "regexp" to the classification algorithm.
Here the pattern is 'A.A', pattern follow 'java-regexp'enter link description here rules.

Parameterizing Objects in spock

I have a problem with parameterizations of list of object by using spock where block. It seems the ListInput value is not taking from the where clause and always coming null value. I have verified the same feature for string and other primitive types and it is working fine.
Does Spock support parameterizations objects ? If yes what is the issue here .
def "check Param Of List of Objects"()
{
expect:
def a= hasflag(ListInput);
a== flag
where:
ListInput | flag
BOList1 | true
BOList2 | false
}
Here the type of BOList1 is an java ArrayList contains the object
You haven't really provided enough information for a definitive answer but I'll try to help.
The where block isn't exactly just a block of code, it's more like a number of parameters passed to a method. It can do a lot, but sometimes you need to pass your code a little differently.
Of note:
- Void methods aren't allowed (but you can get around this using .with{} )
- An iterative parameter cannot also be a derived parameter (constructed from other parameters)
- If you're referencing class level variables (defined within the class but outside this test) they need to be given the #Shared annotation for your tests to have access.
Given more information about where your lists are coming from will help me give better advice.
Final tip; explicitly typecast your parameters to see if that gives you anymore information
def "check Param Of List of Objects"(ArrayList listInput, boolean flag) {
expect:
flag == hasflag(ListInput);
where:
listInput | flag
BOList1 | true
BOList2 | false
}

Generate field from association

I want to generate a field from association.
The problem that I have is that I'm using:
[for (p: Property | aClass.getAssociations().ownedEnd -> select(p: Property | p.type <> aClass)) separator('\n')]
[p.visibility/] [p.type.name/] [p.type.name.toLowerFirst()/];
[/for]
[for (p: Property | aClass.getAssociations().ownedEnd -> select(p: Property | p.type = p.getOtherEnd().type)) separator('\n')]
[p.visibility/] [p.type.name/] [p.type.name.toLowerFirst()/];
[/for]
So that checks for all associations and if the association type isn't the same ass class type it creates a field of that type.
And if the ends are the same type it creates a field of the same type as the class in which it is contained.
So basically if there is association A - B and A - A
For B create A, for A create B and A.
But there is a problem with second loop. It creates two fields instead of one, which is pretty logical. And here comes the question. How I can fix this? I'm not sure if I'm able to declare some kind of variable here and check %2. I can't just take one value, because there could be multiple associations.
Perhaps I did all the thing wrong? Maybe there is a way to iterate over the ends that would save me all those checks and two for loops?
[for (a: Association | aClass.getAssociations())]
[if a.ownedEnd.type = a.ownedEnd.getOtherEnd().type]
[a.visibility/] [a.endType.name/] [a.endType.name.toLowerFirst()/];
[else]
[for (p: Property | a.ownedEnd -> select (type <> aClass))]
[p.visibility/] [p.type.name/] [p.type.name.toLowerFirst()/];
[/for]
[/if]
[/for]
That seems to do the trick

OCL verify value in parameter

Class Game
Method: addPlayer(param Player)
I would like to create an invariant for my method addPlayer so it verifies that parameter Player exists.
Example:
context Game::addPlayer(pl:Player)
inv pl->exists( p : Player | p.playerID = pl.playerID )
Not sure if syntax is valid
I´m not going to get into the discussion if the constraint itself makes sense. Just some comments to help you understand OCL in this case.
Invariants are created on classes. They don´t make sense in an operation context.
Probably what you want is an Operation precondition.
"exists" is an operator (existential quantifier) to work on a collection. The point is that you are implicitly creating a collection with the parameter p1 (by doing pl->exists(...), so your constraint will always be true.
A probably better constraint would be the following
Using the allInstances() operation
context Game::addPlayer(pl:Player)
pre : Player.allInstances()->exists(p : Player | p.playerID = pl.playerID )

Groovy type conversion

In Groovy you can do surprising type conversions using either the as operator or the asType method. Examples include
Short s = new Integer(6) as Short
List collection = new HashSet().asType(List)
I'm surprised that I can convert from an Integer to a Short and from a Set to a List, because there is no "is a" relationship between these types, although they do share a common ancestor.
For example, the following code is equivalent to the Integer/Short example in terms of the
relationship between the types involved in the conversion
class Parent {}
class Child1 extends Parent {}
class Child2 extends Parent {}
def c = new Child1() as Child2
But of course this example fails. What exactly are the type conversion rules behind the as operator and the asType method?
I believe the default asType behaviour can be found in: org.codehaus.groovy.runtime.DefaultGroovyMethods.java
org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.java.
Starting from DefaultGroovyMethods it is quite easy to follow the behavior of asType for a specific object type and requested type combination.
According to what Ruben has already pointed out the end result of:
Set collection = new HashSet().asType(List)
is
Set collection = new ArrayList( new HashSet() )
The asType method recognizes you are wanting a List and being the fact HashSet is a Collection, it just uses ArrayList's constructor which takes a Collection.
As for the numbers one, it converts the Integer into a Number, then calls the shortValue method.
I didn't realize there was so much logic in converting references/values like this, my sincere gratitude to Ruben for pointing out the source, I'll be making quite a few blog posts over this topic.

Resources