|
|
|
Supreme Being
      
Group: Forum Members
Last Login: 2012-01-26 14:59:09
Posts: 242,
Visits: 828
|
|
| Hi, A part of my modelis a bit modified Composite pattern: NativeDatapoint ----|> Datapoint CalculatedDatapoint ----|> Datapoint CalculatedDatapoint *-----* Datapoint. The only difference as you see is m:n association instead of n:1 I need to define a derived attribute int Ranque on Datapoint with the following meaning: it should contain the longest path over the association links from this instance to one of the leafs (instance of the class NativeDatapoint). I accept only OCL-solutions I have to less time now to try it myself. One tip: Ranque for NativeDatapoint is always 0.
/Efim
|
|
|
|
|
Supreme Being
      
Group: Forum Members
Last Login: 2011-07-14 17:05:00
Posts: 290,
Visits: 2 617
|
|
efim (2010-03-24)
Hi,
A part of my modelis a bit modified Composite pattern:
NativeDatapoint ----|>Datapoint CalculatedDatapoint ----|>Datapoint
CalculatedDatapoint *-----* Datapoint.
The only difference as you see is m:n association instead of n:1
I need to define a derived attribute int Ranque on Datapointwith the following meaning: it should contain the longest path over the association linksfrom this instance to one of the leafs (instance of the class NativeDatapoint). I accept only OCL-solutions
I have to less time now to try it myself. One tip:Ranque forNativeDatapoint is always 0.
Add a Ranque derived attribute on DataPoint with a value of -1 (a meaningless default)
Add an entry to the DerivationExpressions list on NativeDataPoint as
Ranque=0
Now add one to CalculatedDataPoint as
Ranque = self.dataPoints.ranque->maxValue + 1
====
Pete
|
|
|
|
|
Supreme Being
      
Group: Forum Members
Last Login: 2012-01-26 14:59:09
Posts: 242,
Visits: 828
|
|
It is ingeniously simple to be truth Thanks a lot, Pete, you are master of Recursion Sciences
/Efim
|
|
|
|
|
Forum Guru
      
Group: Forum Members
Last Login: 2010-08-12 17:32:24
Posts: 51,
Visits: 600
|
|
| Nice answer Pete - much quicker and simpler than I was doing it. I totally misread the question and thought you needed the max path between a CalcDataPoint instance and a specific NativeDataPoint instance - not just any NativeDataPoint.
I thought I'd share my solution anyway if anyone ever needs it. This will find the maximum path length between any two objects (of the same inheritance that share the association) linked by a many to many association. Easily changed to shortest path if required. Call it in ocl with startingObjectInstance.MaxPathLengthTo(endObjectInstance, 'AssociationNameAsString') ie - NativeDataPointInstance.MaxPathLengthTo(CalcDataPointInstance, 'DataPoints') Nick public class LongestPath : OclOperationBase { public IObject startingObject; public IObject endObject; public string assoication; public List<int> pathLengths; public int FindLongestPath () { pathLengths = new List<int>(); foreach (IObject associatedObject in startingObject.Properties[assoication].GetAsCollection()) { TraverseObjects(associatedObject, 1, new List<IObject>()); } pathLengths.Sort(); return pathLengths[pathLengths.Count - 1]; //return pathLengths[0]; //returns shortest rather than longest path } public void TraverseObjects ( IObject obj, int pathLengthToObject, List<IObject> traveresedObjects ) { if (obj == endObject) { pathLengths.Add(pathLengthToObject); } else { foreach (IObject associatedObject in obj.Properties[assoication].GetAsCollection()) { if (!traveresedObjects.Contains(associatedObject)) { traveresedObjects.Add(associatedObject); TraverseObjects(associatedObject, pathLengthToObject + 1, traveresedObjects); } } } } public override void Evaluate ( IOclOperationParameters oclParameters ) { startingObject = oclParameters.Values[0].Element as IObject; endObject = oclParameters.Values[1].Element as IObject; assoication = Support.GetAsString(oclParameters.Values[2]); int result = FindLongestPath(); Support.MakeNewInteger(oclParameters.Result, result); } protected override void Init () { IOclType[] oclParameters = new IOclType[3]; oclParameters[0] = Support.ObjectType; oclParameters[1] = Support.ObjectType; oclParameters[2] = Support.StringType; InternalInit("MaxPathLengthToObject", oclParameters, Support.IntegerType); } }
|
|
|
|