locked
Searching through child activities RRS feed

  • Question

  • Hello Everyone,

         I am trying to search through an activity and find a specific child activity by name then insert another activity into the one I just found.  This is what I have so far:
    EditingContext ec = new
     EditingContext(); 
    ModelTreeManager _modelTreeManager = new
     ModelTreeManager(ec); 
    _modelTreeManager.Load(new
     TestingActivity2());
    _currentModelItemRoot = _modelTreeManager.Root; 
    WriteLine activity = new
     WriteLine(); 
    string
     activityName = "If1"
     ; 
    _currentModelItemRoot.Properties["Activities"
    ].Collection.Where(x => x.Name == activityName).Single().Properties["Activities"
    ].Collection.Add(activity);
    
    <Activity mc:Ignorable="sap" x:Class="EntityBuilding.Test.TestResources.TestingActivity2" mva:VisualBasic.Settings="Assembly references and imported namespaces serialized as XML namespaces" xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="clr-namespace:Microsoft.VisualBasic;assembly=System" xmlns:mva="clr-namespace:Microsoft.VisualBasic.Activities;assembly=System.Activities" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:s1="clr-namespace:System;assembly=System" xmlns:s2="clr-namespace:System;assembly=System.Xml" xmlns:s3="clr-namespace:System;assembly=System.Core" xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities" xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation" xmlns:scg="clr-namespace:System.Collections.Generic;assembly=System" xmlns:scg1="clr-namespace:System.Collections.Generic;assembly=System.ServiceModel" xmlns:scg2="clr-namespace:System.Collections.Generic;assembly=System.Core" xmlns:scg3="clr-namespace:System.Collections.Generic;assembly=mscorlib" xmlns:sd="clr-namespace:System.Data;assembly=System.Data" xmlns:sd1="clr-namespace:System.Data;assembly=System.Data.DataSetExtensions" xmlns:sl="clr-namespace:System.Linq;assembly=System.Core" xmlns:st="clr-namespace:System.Text;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <Sequence sad:XamlDebuggerXmlReader.FileName="C:\Users\Amadeus1\documents\visual studio 10\Projects\EntityBuilder\EntityBuilding.Test\TestResources\TestingActivity2.xaml" sap:VirtualizedContainerService.HintSize="486,332">
        <sap:WorkflowViewStateService.ViewState>
          <scg3:Dictionary x:TypeArguments="x:String, x:Object">
            <x:Boolean x:Key="IsExpanded">True</x:Boolean>
          </scg3:Dictionary>
        </sap:WorkflowViewStateService.ViewState>
        <If Condition="True" DisplayName="If1" sap:VirtualizedContainerService.HintSize="464,208" />
      </Sequence>
    </Activity>


    I guess the real problem is that I can't figure out a way to search a workflow and find all the activities that match a certain criteria then modify them.  Hopefully someone will be able to point out what I am doing wrong here.  Also, the preferable solution would be one that could find the If1 activity even if it were nested in ten activities deep.

    Thanks in advance,

    Bob
    Saturday, October 31, 2009 5:10 AM

Answers

  • Bob,

    If you are using the ModelItem tree, or if you are simply programming against the OM, you have basically the same problem which is one of traversing an object graph, with the added complexity that the nodes have customizable link points.  How do I know to look in Then/Else on If, but Branches in parallel. 

    You can basically modify a graph traversal (or tree if you make a few assumptions) algorithm to find all the links that are either properties of type Activity (or subtype), or collections, or ... .  Even then there are holes, you need to handle ActivityAction, for instance.  And then on top of that you need an extensiblity mechanism, so if someone provides a ruleset activity, they can tell you how to find things inside of it.

    There are a number of edge cases that come up with a general purpose search solution for an arbitary workflow (you'll note we don't have a find feature in vs2010, and this is part of the reason).  The best way (after going at a few different prototypes of this) is to try to scope the problem down to the set of things that you want to get working, and start there.  Then, i set up a bunch of unit tests to make sure that my queries are returning what I expect them to (and usually, they aren't on the first try).

    I think the problem above is that your where clause operates on model items, and modelItem.Name may not be the display name property.

    I think you would want your where to be

    .Where(x => x.Properties["DisplayName"].ComputedValue.ToString() == activityName)


    let me know if that helps,

    matt
    Program Manager -- wf designer -- http://blogs.msdn.com/mwinkle
    Saturday, October 31, 2009 5:57 AM

All replies

  • Bob,

    If you are using the ModelItem tree, or if you are simply programming against the OM, you have basically the same problem which is one of traversing an object graph, with the added complexity that the nodes have customizable link points.  How do I know to look in Then/Else on If, but Branches in parallel. 

    You can basically modify a graph traversal (or tree if you make a few assumptions) algorithm to find all the links that are either properties of type Activity (or subtype), or collections, or ... .  Even then there are holes, you need to handle ActivityAction, for instance.  And then on top of that you need an extensiblity mechanism, so if someone provides a ruleset activity, they can tell you how to find things inside of it.

    There are a number of edge cases that come up with a general purpose search solution for an arbitary workflow (you'll note we don't have a find feature in vs2010, and this is part of the reason).  The best way (after going at a few different prototypes of this) is to try to scope the problem down to the set of things that you want to get working, and start there.  Then, i set up a bunch of unit tests to make sure that my queries are returning what I expect them to (and usually, they aren't on the first try).

    I think the problem above is that your where clause operates on model items, and modelItem.Name may not be the display name property.

    I think you would want your where to be

    .Where(x => x.Properties["DisplayName"].ComputedValue.ToString() == activityName)


    let me know if that helps,

    matt
    Program Manager -- wf designer -- http://blogs.msdn.com/mwinkle
    Saturday, October 31, 2009 5:57 AM
  • Matt,

         Now I get it.  This is going to be an interesting challenge to say the least.  Thanks again for your help!

    Bob
    Saturday, October 31, 2009 6:51 AM