Skip to content

Commit

Permalink
ST6RI-796 Move effectiveName and effectiveShortName caching to
Browse files Browse the repository at this point in the history
FeatureAdapter
  • Loading branch information
TheKorpos committed Oct 14, 2024
1 parent d20a563 commit effff7b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 73 deletions.
20 changes: 20 additions & 0 deletions org.omg.sysml/src/org/omg/sysml/adapter/FeatureAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ public boolean redefinesAnyOf(Collection<Feature> features, Set<Feature> visited
// Caching

EList<Type> types = null;
String storedEffectiveName = null;
String storedEffectiveShortName = null;

public void storeEffectiveName(String effectiveName) {
storedEffectiveName = effectiveName;
}

public String getEffectiveName() {
return storedEffectiveName;
}

public void storeEffectiveShortName(String effectiveShortName) {
storedEffectiveShortName = effectiveShortName;
}

public String getEffectiveShortName() {
return storedEffectiveShortName;
}

public EList<Type> getTypes() {
return types;
Expand All @@ -124,6 +142,8 @@ public void clearCaches() {
super.clearCaches();
types = null;
redefinedFeatures.clear();
storedEffectiveName = null;
storedEffectiveShortName = null;
}

// Implicit Elements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,58 +22,23 @@
package org.omg.sysml.delegate.invocation;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EOperation;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.util.BasicInvocationDelegate;
import org.omg.sysml.lang.sysml.Feature;
import org.omg.sysml.lang.sysml.impl.FeatureImpl;
import org.omg.sysml.util.FeatureUtil;

public class Feature_effectiveName_InvocationDelegate extends BasicInvocationDelegate {

public Feature_effectiveName_InvocationDelegate(EOperation operation) {
super(operation);
}

private final Map<Feature, String> nameCache = new HashMap<>();

@Override
public Object dynamicInvoke(InternalEObject target, EList<?> arguments) throws InvocationTargetException {
Feature self = (Feature) target;

return computeEffectiveName(self, new HashSet<>());
}

private boolean isNameDefinite(Feature self) {
return self.getDeclaredName() != null || self.getDeclaredShortName() != null;
}


public String computeEffectiveName(Feature self, Set<Feature> visited) {
if (isNameDefinite(self)) {
return self.getDeclaredName();
}

String effectiveName = nameCache.get(self);

if (effectiveName != null) {
return effectiveName;
}

visited.add(self);
FeatureImpl namingFeature = (FeatureImpl) self.namingFeature();

if (namingFeature != null && !visited.contains(namingFeature)) {
effectiveName = computeEffectiveName(namingFeature, visited);
nameCache.put(self, effectiveName);
}

return effectiveName;
return FeatureUtil.computeEffectiveName(self);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,57 +22,23 @@
package org.omg.sysml.delegate.invocation;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EOperation;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.util.BasicInvocationDelegate;
import org.omg.sysml.lang.sysml.Feature;
import org.omg.sysml.lang.sysml.impl.FeatureImpl;
import org.omg.sysml.util.FeatureUtil;

public class Feature_effectiveShortName_InvocationDelegate extends BasicInvocationDelegate {

public Feature_effectiveShortName_InvocationDelegate(EOperation operation) {
super(operation);
}

private final Map<Feature, String> nameCache = new HashMap<>();

@Override
public Object dynamicInvoke(InternalEObject target, EList<?> arguments) throws InvocationTargetException {
Feature self = (Feature) target;

return computeEffectiveShortName(self, new HashSet<>());
}

private boolean isNameSet(Feature self) {
return self.getDeclaredName() != null || self.getDeclaredShortName() != null;
return FeatureUtil.computeEffectiveShortName(self);
}

public String computeEffectiveShortName(Feature self, Set<Feature> visited) {
if (isNameSet(self)) {
return self.getDeclaredShortName();
}

String effectiveShortName = nameCache.get(self);

if (effectiveShortName != null) {
return effectiveShortName;
}

visited.add(self);
FeatureImpl namingFeature = (FeatureImpl) self.namingFeature();

if (namingFeature != null && !visited.contains(namingFeature)) {
effectiveShortName = computeEffectiveShortName(namingFeature, visited);
nameCache.put(self, effectiveShortName);
}

return effectiveShortName;
}

}
59 changes: 59 additions & 0 deletions org.omg.sysml/src/org/omg/sysml/util/FeatureUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.omg.sysml.lang.sysml.SysMLPackage;
import org.omg.sysml.lang.sysml.Type;
import org.omg.sysml.lang.sysml.TypeFeaturing;
import org.omg.sysml.lang.sysml.impl.FeatureImpl;

public class FeatureUtil {

Expand Down Expand Up @@ -439,4 +440,62 @@ public static Multiplicity getMultiplicityOf(Type type, Set<Type> visited) {
}
return multiplicity;
}

//Naming

public static String computeEffectiveName(Feature self) {
return computeEffectiveName(self, new HashSet<>());
}

private static String computeEffectiveName(Feature self, Set<Feature> visited) {
if (isNameSet(self)) {
return self.getDeclaredName();
}

String effectiveName = getFeatureAdapter(self).getEffectiveName();

if (effectiveName != null) {
return effectiveName;
}

visited.add(self);
FeatureImpl namingFeature = (FeatureImpl) self.namingFeature();

if (namingFeature != null && !visited.contains(namingFeature)) {
effectiveName = computeEffectiveName(namingFeature, visited);
getFeatureAdapter(self).storeEffectiveName(effectiveName);
}

return effectiveName;
}

public static String computeEffectiveShortName(Feature self) {
return computeEffectiveShortName(self, new HashSet<>());
}

private static String computeEffectiveShortName(Feature self, Set<Feature> visited) {
if (isNameSet(self)) {
return self.getDeclaredShortName();
}

String effectiveShortName = getFeatureAdapter(self).getEffectiveShortName();

if (effectiveShortName != null) {
return effectiveShortName;
}

visited.add(self);
FeatureImpl namingFeature = (FeatureImpl) self.namingFeature();

if (namingFeature != null && !visited.contains(namingFeature)) {
effectiveShortName = computeEffectiveShortName(namingFeature, visited);
getFeatureAdapter(self).storeEffectiveShortName(effectiveShortName);
}

return effectiveShortName;
}

private static boolean isNameSet(Feature self) {
return self.getDeclaredName() != null || self.getDeclaredShortName() != null;
}
}

0 comments on commit effff7b

Please sign in to comment.