Skip to content

Commit

Permalink
Merge pull request #525 from Systems-Modeling/ST6RI-727
Browse files Browse the repository at this point in the history
ST6RI-727 Implicit redefinitions should be added even if there are owned redefinitions
  • Loading branch information
seidewitz authored Jan 8, 2024
2 parents 922c9b6 + 7d2a9fc commit e3f6d52
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* SysML 2 Pilot Implementation
* Copyright (c) 2022, 2023 Model Driven Solutions, Inc.
* Copyright (c) 2022-2024 Model Driven Solutions, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -87,18 +87,21 @@ protected InvocationExpression instantiateInvocation(InvocationExpression expres
// Add implicit generalization.
ElementUtil.transform(instantiation);

// Add parameters corresponding to parameters in original expression.
// Evaluate value Expressions for parameters on original instantiation.
for (Feature parameter: TypeUtil.getOwnedParametersOf(expression)) {
Feature newParameter = SysMLFactory.eINSTANCE.createFeature();
newParameter.setDirection(parameter.getDirection());
for (Feature redefinedFeature: FeatureUtil.getRedefinedFeaturesWithComputedOf(parameter, null)) {
Redefinition newRedefinition = SysMLFactory.eINSTANCE.createRedefinition();
newRedefinition.setRedefinedFeature(redefinedFeature);
newParameter.getOwnedRelationship().add(newRedefinition);
}

Expression valueExpression = FeatureUtil.getValueExpressionFor(parameter);
if (valueExpression != null) {
// Add a new parameter to hold the result of the Expression evaluation.
Feature newParameter = SysMLFactory.eINSTANCE.createFeature();
TypeUtil.addOwnedFeatureTo(instantiation, newParameter);

newParameter.setDirection(parameter.getDirection());
for (Feature redefinedFeature: FeatureUtil.getRedefinedFeaturesWithComputedOf(parameter, null)) {
Redefinition newRedefinition = SysMLFactory.eINSTANCE.createRedefinition();
newRedefinition.setRedefinedFeature(redefinedFeature);
newParameter.getOwnedRelationship().add(newRedefinition);
}

// Evaluate the value expression for the original parameter with the given target,
// NOT including the bindings in the original invocation expression.
EList<Element> values = evaluate(valueExpression, target);
Expand All @@ -113,9 +116,8 @@ protected InvocationExpression instantiateInvocation(InvocationExpression expres
}
}
}

TypeUtil.addOwnedFeatureTo(instantiation, newParameter);
}

return instantiation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,25 @@ public void testIndexOperator() throws Exception {
assertElement("LiteralInteger 234", instance.eval("arr2#(2,3,4)", "IndexOperatorTest"));
}

// Test named-argument invocation.
public final String invocationTest =
"package InvocationTest {\n"
+ " calc def Test {"
+ " in x;"
+ " in y;"
+ " x"
+ " }"
+ "}";

@Test
public void testInvocationEvaluation() throws Exception {
SysMLInteractive instance = getSysMLInteractiveInstance();
process(instance, invocationTest);
// assertElement("LiteralInteger 1", instance.eval("Test(1, 2)", "InvocationTest"));
// assertElement("LiteralInteger 1", instance.eval("Test(x = 1, y = 2)", "InvocationTest"));
assertElement("LiteralInteger 1", instance.eval("Test(y = 2, x = 1)", "InvocationTest"));
}

@Test
public void testArithmeticEvaluation() throws Exception {
SysMLInteractive instance = getSysMLInteractiveInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ standard library package TradeStudies {
* selectedAlternative have the minimum ObjectiveFunction value of all the
* given alternatives.
*/

subject :>> selectedAlternative;
in ref :>> alternatives;
in calc :>>fn;

out attribute :>> best = alternatives->minimize {
doc
Expand All @@ -103,6 +107,10 @@ standard library package TradeStudies {
* given alternatives.
*/

subject :>> selectedAlternative;
in ref :>> alternatives;
in calc :>>fn;

out attribute :>> best = alternatives->maximize {
doc
/*
Expand Down Expand Up @@ -154,6 +162,7 @@ standard library package TradeStudies {
* defined one.
*/

subject :>> selectedAlternative;
in ref :>> alternatives = studyAlternatives;
in calc :>> fn = evaluationFunction;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,18 @@ package ConnectionTest {

connection bus : C connect (d1, d2, d3, d4);

connection : C {
end :>> end1 ::> d1;
end end2 ::> d2;
end end3 ::> d3;
}

connection {
part q;
end end1 ::> d1 :> q;
end end2 ::> d2;
}

flow def F {
end f_p : P;
end f_q;
Expand Down
8 changes: 5 additions & 3 deletions org.omg.sysml/src/org/omg/sysml/adapter/FeatureAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,14 @@ public void forceComputeRedefinitions() {
}

public boolean isComputeRedefinitions() {
EList<Redefinition> ownedRedefinitions = getTarget().getOwnedRedefinition();
return isAddImplicitGeneralTypes && isComputeRedefinitions && ownedRedefinitions.isEmpty();
Feature target = getTarget();
return isAddImplicitGeneralTypes && isComputeRedefinitions &&
(!FeatureUtil.isParameter(target) ||
target.getOwnedRedefinition().isEmpty());
}

/**
* If this Feature has no Redefinitions, compute relevant Redefinitions, as appropriate.
* Compute relevant implicit Redefinitions, as appropriate.
*/
public void addComputedRedefinitions(Element skip) {
if (isComputeRedefinitions()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************************************
* SysML 2 Pilot Implementation
* Copyright (c) 2021-2022 Model Driven Solutions, Inc.
* Copyright (c) 2021-2024 Model Driven Solutions, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand All @@ -22,7 +22,6 @@
package org.omg.sysml.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.omg.sysml.lang.sysml.Expression;
import org.omg.sysml.lang.sysml.Feature;
Expand Down Expand Up @@ -100,20 +99,6 @@ public static Feature getInputForFeature(List<Feature> inputs, Feature feature,
return input;
}

// Computed Redefinition

@Override
public List<Feature> getRelevantFeatures() {
Expression target = getTarget();
Type type = getExpressionType();
int m = type == null ? 0 :
(int)TypeUtil.getAllParametersOf(target, null).stream().
filter(FeatureUtil::isInputParameter).count();
List<Feature> features = target.getOwnedFeature();
int n = features.size();
return m >= n ? Collections.emptyList() : features.subList(m, n);
}

// Transformation

protected void addResultTyping() {
Expand Down
21 changes: 15 additions & 6 deletions org.omg.sysml/src/org/omg/sysml/adapter/TypeAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.omg.sysml.lang.sysml.MetadataFeature;
import org.omg.sysml.lang.sysml.Redefinition;
import org.omg.sysml.expressions.util.EvaluationUtil;
import org.omg.sysml.lang.sysml.BindingConnector;
import org.omg.sysml.lang.sysml.Element;
Expand Down Expand Up @@ -202,21 +203,29 @@ public void addImplicitMemberBindingConnector(BindingConnector connector) {

public void removeUnnecessaryImplicitGeneralTypes() {
Type target = getTarget();
List<Type> generals = target.getOwnedSpecialization().stream().
List<Specialization> specializations = target.getOwnedSpecialization().stream().
filter(spec->spec.getSpecific() == target && spec.getGeneral() != target).
collect(Collectors.toList());
List<Type> generals = specializations.stream().
map(Specialization::getGeneral).
collect(Collectors.toList());
List<Type> redefinedFeatures = specializations.stream().
filter(Redefinition.class::isInstance).
map(Specialization::getGeneral).
collect(Collectors.toList());
List<Type> implicitGenerals = new ArrayList<>();
implicitGeneralTypes.values().forEach(implicitGenerals::addAll);
for (Object eClass: implicitGeneralTypes.keySet().toArray()) {
if (eClass != SysMLPackage.eINSTANCE.getRedefinition()) {
List<Type> implicitEClassGenerals = implicitGeneralTypes.get(eClass);
List<Type> implicitEClassGenerals = implicitGeneralTypes.get(eClass);
if (eClass == SysMLPackage.eINSTANCE.getRedefinition()) {
implicitEClassGenerals.removeAll(redefinedFeatures);
} else {
implicitEClassGenerals.removeIf(gen->
generals.stream().anyMatch(type->conforms(type, gen)) ||
implicitGenerals.stream().anyMatch(type->type != gen && conforms(type, gen)));
if (implicitEClassGenerals.isEmpty()) {
implicitGeneralTypes.remove(eClass);
}
}
if (implicitEClassGenerals.isEmpty()) {
implicitGeneralTypes.remove(eClass);
}
}

Expand Down
25 changes: 1 addition & 24 deletions org.omg.sysml/src/org/omg/sysml/adapter/UsageAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@

package org.omg.sysml.adapter;

import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

import org.omg.sysml.lang.sysml.ActionDefinition;
import org.omg.sysml.lang.sysml.ActionUsage;
import org.omg.sysml.lang.sysml.Definition;
import org.omg.sysml.lang.sysml.Element;
import org.omg.sysml.lang.sysml.Feature;
import org.omg.sysml.lang.sysml.FeatureMembership;
import org.omg.sysml.lang.sysml.FeatureValue;
Expand Down Expand Up @@ -94,7 +91,7 @@ public boolean isEntryExitAction() {
}
}

// Implicit Generalization
// Implicit Generalization

protected void addSubsetting(String subsettedFeatureName) {
Feature feature = (Feature)getLibraryType(subsettedFeatureName);
Expand Down Expand Up @@ -122,26 +119,6 @@ protected String getDefaultSupertype() {
return super.getDefaultSupertype();
}

// Computed Redefinitions

/**
* A subject Parameter always redefines a subject Parameter.
*/
@Override
public List<? extends Feature> getParameterRelevantFeatures(Type type, Element skip) {
if (UsageUtil.isSubjectParameter(getTarget())) {
Feature typeSubject = TypeUtil.getSubjectParameterOf(type);
return typeSubject == null? Collections.emptyList():
Collections.singletonList(typeSubject);
}
return super.getParameterRelevantFeatures(type, skip);
}

@Override
public boolean isIgnoredParameter() {
return super.isIgnoredParameter() || UsageUtil.isSubjectParameter(getTarget());
}

// Transformation

protected void addDefaultMultiplicity() {
Expand Down
1 change: 1 addition & 0 deletions org.omg.sysml/src/org/omg/sysml/util/TypeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ public static Usage basicGetSubjectParameterOf(Type type) {
// Objective requirements

public static RequirementUsage getObjectiveRequirementOf(Type type) {
ElementUtil.transform(type);
return type instanceof CaseDefinition? ((CaseDefinition)type).getObjectiveRequirement():
type instanceof CaseUsage? ((CaseUsage)type).getObjectiveRequirement():
null;
Expand Down
9 changes: 9 additions & 0 deletions sysml.library/Domain Libraries/Analysis/TradeStudies.sysml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ standard library package TradeStudies {
* selectedAlternative have the minimum ObjectiveFunction value of all the
* given alternatives.
*/

subject :>> selectedAlternative;
in ref :>> alternatives;
in calc :>>fn;

out attribute :>> best = alternatives->minimize {
doc
Expand All @@ -103,6 +107,10 @@ standard library package TradeStudies {
* given alternatives.
*/

subject :>> selectedAlternative;
in ref :>> alternatives;
in calc :>>fn;

out attribute :>> best = alternatives->maximize {
doc
/*
Expand Down Expand Up @@ -154,6 +162,7 @@ standard library package TradeStudies {
* defined one.
*/

subject :>> selectedAlternative;
in ref :>> alternatives = studyAlternatives;
in calc :>> fn = evaluationFunction;
}
Expand Down
8 changes: 8 additions & 0 deletions sysml/src/examples/Analysis Examples/Dynamics.sysml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ package Dynamics {
// Analysis actions

action dyn1 : StraightLineVehicleDynamics {
in attribute dt : TimeValue;
in attribute whlpwr : PowerValue;
in attribute Cd : Real;
in attribute Cf: Real;
in attribute tm : MassValue;
in attribute v_in : SpeedValue;
in attribute x_in : LengthValue;

attribute tp : PowerValue = Power(whlpwr, Cd, Cf, tm, v_in);

out attribute :>> a_out : AccelerationValue = Acceleration(dt, tm, tp);
Expand Down
6 changes: 6 additions & 0 deletions sysml/src/examples/Simple Tests/ConnectionTest.sysml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ package ConnectionTest {

connection bus : C connect (d1, d2, d3, d4);

connection : C {
end :>> end1 ::> d1;
end end2 ::> d2;
end end3 ::> d3;
}

connection {
part q;
end end1 ::> d1 :> q;
Expand Down

0 comments on commit e3f6d52

Please sign in to comment.