Skip to content

Commit

Permalink
support for create/constructor operations #232
Browse files Browse the repository at this point in the history
  • Loading branch information
abstratt committed Aug 24, 2017
1 parent 82ab2f4 commit ab2e745
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.eclipse.uml2.uml.UMLPackage.Literals;
import org.eclipse.uml2.uml.VisibilityKind;

import com.abstratt.kirra.Operation.OperationKind;
import com.abstratt.kirra.Relationship.Style;
import com.abstratt.kirra.TypeRef;
import com.abstratt.kirra.TypeRef.TypeKind;
Expand Down Expand Up @@ -382,11 +383,15 @@ public static boolean isRequired(Parameter parameter, boolean creation) {
}

public static boolean isAction(Operation operation) {
return isPublic(operation) && !operation.isQuery() && VisibilityKind.PUBLIC_LITERAL == operation.getVisibility();
return isPublic(operation) && !operation.isQuery() && !FeatureUtils.isConstructor(operation) && VisibilityKind.PUBLIC_LITERAL == operation.getVisibility();
}

public static boolean isEntityOperation(Operation operation) {
return isAction(operation) || isFinder(operation);
return isAction(operation) || isFinder(operation) || isConstructor(operation);
}

public static boolean isConstructor(Operation operation) {
return isPublic(operation) && FeatureUtils.isConstructor(operation) && operation.getReturnResult() != null && !operation.isStatic() && operation.getReturnResult().getType() == operation.getClass_();
}

public static List<Parameter> getParameters(BehavioralFeature operation) {
Expand Down Expand Up @@ -1171,4 +1176,14 @@ public static boolean isInherited(Operation feature, Classifier candidateOwner)
boolean inherited = feature.getOwner() != candidateOwner && ClassifierUtils.isKindOf(candidateOwner, (Classifier) feature.getOwner());
return inherited;
}

public static OperationKind getOperationKind(org.eclipse.uml2.uml.Operation operation) {
if (isFinder(operation))
return OperationKind.Finder;
if (isAction(operation))
return OperationKind.Action;
if (isConstructor(operation))
return OperationKind.Construtor;
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ public Entity getEntity(Class umlClass) {
public Operation getEntityOperation(org.eclipse.uml2.uml.Operation umlOperation) {
if (!KirraHelper.isEntityOperation(umlOperation))
throw new IllegalArgumentException();
if (!KirraHelper.isAction(umlOperation) && !KirraHelper.isFinder(umlOperation))
if (!KirraHelper.isAction(umlOperation) && !KirraHelper.isFinder(umlOperation) && !KirraHelper.isConstructor(umlOperation))
throw new IllegalArgumentException();

Operation entityOperation = basicGetOperation(umlOperation);
entityOperation.setKind(KirraHelper.isFinder(umlOperation) ? Operation.OperationKind.Finder : Operation.OperationKind.Action);
entityOperation.setKind(KirraHelper.getOperationKind(umlOperation));
entityOperation.setInstanceOperation(entityOperation.getKind() == OperationKind.Action && !umlOperation.isStatic());
return entityOperation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.function.Function;
Expand Down Expand Up @@ -67,6 +68,7 @@
import com.abstratt.kirra.Tuple;
import com.abstratt.kirra.TupleType;
import com.abstratt.kirra.TypeRef;
import com.abstratt.kirra.InstanceManagement.DataProfile;
import com.abstratt.kirra.InstanceManagement.PageRequest;
import com.abstratt.kirra.mdd.core.KirraHelper;
import com.abstratt.kirra.mdd.core.KirraMDDConstants;
Expand Down Expand Up @@ -173,6 +175,14 @@ public Instance createInstance(Instance kirraInstance) {
try {
RuntimeObject asRuntimeObject = convertToRuntimeObject(kirraInstance);
asRuntimeObject.save();

// execute default constructor if found
// TODO-RC this is not the right place for it - it should be handled by the runtime itself,
// or by the model compiler
Classifier modelClassifier = asRuntimeObject.getRuntimeClass().getModelClassifier();
Optional<? extends Operation> defaultConstructor = FeatureUtils.getBehavioralFeatures(modelClassifier).stream().filter(it -> it instanceof Operation).map(it -> (Operation) it).filter(it -> KirraHelper.isConstructor((Operation) it) && FeatureUtils.getInputParameters(it.getOwnedParameters()).isEmpty()).findFirst();
defaultConstructor.ifPresent(it -> getRuntime().runOperation(asRuntimeObject, it));

return (Instance) convertFromRuntimeObject(asRuntimeObject, DataProfile.Full);
} catch (NodeStoreException e) {
throw new KirraException("Could not create '" + kirraInstance.getTypeRef() + "': " + e.getMessage(), e, Kind.VALIDATION);
Expand Down Expand Up @@ -493,17 +503,17 @@ public Instance getInstance(String namespace, String name, String externalId, Da
RuntimeObject found = findRuntimeObject(namespace, name, externalId);
return (Instance) (found == null ? null : convertFromRuntimeObject(found, dataProfile));
}

@Override
public List<Instance> getInstances(String namespace, String name, DataProfile dataProfile) {
return getInstances(namespace, name, dataProfile, true);
public List<Instance> getInstances(String namespace, String name, DataProfile dataProfile, boolean includeSubclasses) {
return getInstances(namespace, name, new PageRequest(null, null, dataProfile, includeSubclasses));
}


@Override
public List<Instance> getInstances(String namespace, String name, PageRequest pageRequest) {
Class umlClass = (Class) getModelElement(namespace, name, Literals.CLASS);
return filterValidInstances(getRuntime().getAllInstances(umlClass, pageRequest.getIncludeSubtypes()), pageRequest.getDataProfile());
List<RuntimeObject> allInstances = getRuntime().getAllInstances(umlClass, pageRequest.getIncludeSubtypes());
return filterValidInstances(allInstances, pageRequest.getDataProfile());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ public void testOperations() throws CoreException {
model += "import datatypes;\n";
model += "class MyClass1\n";
model += "attribute singleAttribute : Integer;\n";
model += "constructor \\create1(someValue : Integer);\n";
model += "operation action1();\n";
model += "operation action2(par1 : Integer, par2 : Boolean) : String;\n";
model += "operation action3() : MyClass1[*];\n";
Expand All @@ -670,7 +671,7 @@ public void testOperations() throws CoreException {

Entity entity = kirra.getEntity("mypackage", "MyClass1");
List<Operation> operations = entity.getOperations();
TestCase.assertEquals(5, operations.size());
TestCase.assertEquals(6, operations.size());

sortNamedElements(operations);

Expand Down Expand Up @@ -707,11 +708,18 @@ public void testOperations() throws CoreException {
TestCase.assertFalse(operations.get(3).isInstanceOperation());
TestCase.assertEquals(0, operations.get(3).getParameters().size());

TestCase.assertEquals("query1", operations.get(4).getName());
TestCase.assertEquals("create1", operations.get(4).getName());
TestCase.assertEquals("MyClass1", operations.get(4).getTypeRef().getTypeName());
TestCase.assertEquals(Operation.OperationKind.Finder, operations.get(4).getKind());
TestCase.assertEquals(Operation.OperationKind.Construtor, operations.get(4).getKind());
TestCase.assertFalse(operations.get(4).isInstanceOperation());
TestCase.assertEquals(2, operations.get(4).getParameters().size());
TestCase.assertEquals(1, operations.get(4).getParameters().size());
TestCase.assertEquals("mypackage.MyClass1", operations.get(4).getTypeRef().getFullName());

TestCase.assertEquals("query1", operations.get(5).getName());
TestCase.assertEquals("MyClass1", operations.get(5).getTypeRef().getTypeName());
TestCase.assertEquals(Operation.OperationKind.Finder, operations.get(5).getKind());
TestCase.assertFalse(operations.get(5).isInstanceOperation());
TestCase.assertEquals(2, operations.get(5).getParameters().size());
}

public void testSchema() throws CoreException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,6 @@ public BasicType getValue(Property property) {
if (isAssociationEnd(property))
return isPersistable ? traverse(property)
: (BasicType) node.getProperties(true).get(nodeProperty(property));
boolean legal = this.isTuple() || this.getRuntimeClass().getModelClassifier() instanceof Signal
|| !property.isMultivalued();
Assert.isLegal(legal, property.getName());
return getSlotValue(property, node.getProperties());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ provider.registerServiceProvider("orion.edit.highlighter",
},
{
name: 'keyword.control.untitled',
match: '\\b(abstract|access|actor|aggregation|alias|all|allow|and|any|apply|association|as|attribute|begin|broadcast|by|call|catch|class|component|composition|connector|create|datatype|delete|deny|dependency|derived|destroy|do|else|elseif|end|entry|enumeration|exit|extends|extent|external|false|final|finally|function|id|if|implements|import|in|initial|inout|interface|invariant|is|link|literal|load|model|navigable|new|none|nonunique|not|null|on|operation|opposite|or|ordered|out|package|port|postcondition|precondition|private|primitive|profile|property|protected|provided|public|query|raise|raises|read|readonly|reception|reference|repeat|required|return|role|self|send|signal|specializes|state|statemachine|static|stereotype|subsets|terminate|then|to|transition|true|try|type|unique|unlink|unordered|until|update|var|when|where|while)\\b'
match: '\\b(abstract|access|actor|aggregation|alias|all|allow|and|any|apply|association|as|attribute|begin|broadcast|by|call|catch|class|component|composition|connector|constructor|create|datatype|delete|deny|dependency|derived|destroy|do|else|elseif|end|entry|enumeration|exit|extends|extent|external|false|final|finally|function|id|if|implements|import|in|initial|inout|interface|invariant|is|link|literal|load|model|navigable|new|none|nonunique|not|null|on|operation|opposite|or|ordered|out|package|port|postcondition|precondition|private|primitive|profile|property|protected|provided|public|query|raise|raises|read|readonly|reception|reference|repeat|required|return|role|self|send|signal|specializes|state|statemachine|static|stereotype|subsets|terminate|then|to|transition|true|try|type|unique|unlink|unordered|until|update|var|when|where|while)\\b'
},
{
"match": "([a-zA-Z_][a-zA-Z0-9_]*)",
Expand Down

0 comments on commit ab2e745

Please sign in to comment.