cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

Method to get Assigned Expression

GrahamV
7-Bedrock

Method to get Assigned Expression

Hello all,

 

I'm currently writing a custom JSP to summarise changes resulting from a Change Notice, I've managed to pull all the required data into the report except for Assigned Usage Expression (e.g. 'COLOUR= "GREEN"), which I'm struggling to get, does anyone know of an API method to retrieve this?

 

I've tried:

  • WTPartUsageLink.getExpressionData() - Returns null,  seems to just return the ExpressionData field from the WTPartUsageLink table,  which is completely empty, guess it's a redundant field 
  • ExpressionHelper.getDisplayExpression(Expressionable,  WTContainerRef) - Returns "{Display : "" , invalid: false}", not sure why it's taking a WTContainerRef argument as this shouldn't make a difference to the returned expression, so maybe it's not doing what the method name implies.

CS384380 states that there's no supported API to achieve this, however that's not to say there isn't one available. Worst case, I could always build the expression through QuerySpec, but it feels like reinventing the wheel, if anyone knows of a method for getting the expression then it would be greatly appreciated.

 

Thanks,

 

Graham

1 ACCEPTED SOLUTION

Accepted Solutions

Hi @GrahamV 

It is not so easy to find the correct API

OptionHelper.service.getAllChoices(child, optionSet);

here you can see the API how to get options from your result optionset with wtpart. Also try to use the Usage link as a input

 

OptionSet optionSet = result1.getOptionSet();
WTList[] choices = OptionHelper.service.getAllChoices(child, optionSet);

 

HelesicPetr_0-1705474928404.png

Here is my working code

You can create reference from WTPart or UsageLink and combinate it.

 

ObjectReference ref1 = ObjectReference.newObjectReference(wtpLink); WTPartUsageLink
ObjectReference ref2 = ObjectReference.newObjectReference(subpart); WTPart
OptionSetDelegateResult result1 = OptionHelper.service.getAssignedOptionSet(ref1, ref2);
OptionSet optionSet = result1.getOptionSet();
WTList[] choices = OptionHelper.service.getAllChoices(wtpLink, optionSet); // result for specific usageLink

 

PetrH

 

View solution in original post

6 REPLIES 6

Hi @GrahamV 

Where is the expression assigned? WTpart, UsageLink? difference place ? 

 

Here are apis that can be used

//with WTReference 
OptionHelper.service.getAssignedOptionSet(WTReference var0,WTReference var1);
//with colletion
OptionHelper.service.getAssignedOptionSets(WTColletion var1);

PetrH

Hi Petr, 

 

I'm trying to retrieve the Assign Usage Exapression (i.e. the expression associated to WTPartUsageLink), though I suspect the same method will also work for other Expressionable objects like WTParts.

 

The methods you listed appear to get the Assigned Option Set rather than the Assigned Usage Expression, so I don't think that's what I'm after, it may come in useful though.

 

Thanks,

 

Graham

Hi @GrahamV 

What Assigned Usage expression are you talking about?

The Assigned Usage Expressions are from the OptionsSets and the API retrieve it as you can see it in the Uses tab in the Structure.

HelesicPetr_0-1705390726912.png

HelesicPetr_1-1705391015535.png

If you use WTReference where you put usage link you will get what is in that usage link assigned. 

Or you can put first reference as a parent and second reference as a child WTPart and you would get again the Assigned Usage Expressions

PetrH

Hi Petr, 

 

Yes your image shows exactly what I'm trying to extract, however I can't see how the OptionHelper methods can retrieve this, as it returns an Assigned Option Set not an Assigned Usage Expression, I don't believe you can navigate between these objects (or rather you can't navigate from an Option Set to an assigned Choice, since expressions are really just a set of assigned choices).

 

I tried your suggestions anyway...

//Using WTReference method
WTPart parent = usageLink.getUsedBy();
WTPartMaster child = usageLink.getUses();
ObjectReference ref1 = ObjectReference.newObjectReference (parent);
ObjectReference ref2 = ObjectReference.newObjectReference (child);
OptionSetDelegateResult result1 = OptionHelper.service.getAssignedOptionSet(ref1,ref2);
OptionSet optionSet = result1.getOptionSet();
String optionSetName = optionSet.getName();

This returned an Option Set, I couldn't see any methods to get to an expression or choice.

 

//Using WTCollection method
WTArrayList collection = new WTArrayList();
collection.add(usageLink);
OptionSetDelegateResult result2 = OptionHelper.service.getAssignedOptionSets(collection);
OptionSet optionSet2 = result1.getOptionSet();
String optionSetName2 = optionSet.getName();

This one returned null, not sure if I did something wrong or maybe WTPartUsageLinks just don't get assigned Option Sets (quite possible, as the Option Set assigned to the parent WTPart is what dictates the available options).

 

Open to any suggestions, or if you have any example code that pulls Assigned Usage Expressions that would be most helpful.

 

Thanks,

 

Graham

Hi @GrahamV 

It is not so easy to find the correct API

OptionHelper.service.getAllChoices(child, optionSet);

here you can see the API how to get options from your result optionset with wtpart. Also try to use the Usage link as a input

 

OptionSet optionSet = result1.getOptionSet();
WTList[] choices = OptionHelper.service.getAllChoices(child, optionSet);

 

HelesicPetr_0-1705474928404.png

Here is my working code

You can create reference from WTPart or UsageLink and combinate it.

 

ObjectReference ref1 = ObjectReference.newObjectReference(wtpLink); WTPartUsageLink
ObjectReference ref2 = ObjectReference.newObjectReference(subpart); WTPart
OptionSetDelegateResult result1 = OptionHelper.service.getAssignedOptionSet(ref1, ref2);
OptionSet optionSet = result1.getOptionSet();
WTList[] choices = OptionHelper.service.getAllChoices(wtpLink, optionSet); // result for specific usageLink

 

PetrH

 

Hi Petr, 

 

Got round to trying your suggestion, this succefully retrieved the choices, many thanks!

 

I had to do some more work to merge the WTLists into an expression string, couldn't find any OOTB methods to do this but I need something custom anyway (in my use case I need to be able to merge two expressions together), here's what I came up with...

 

	public static String getExpression(ExpressionAssignable expressionAssignable) {
		
		try {
			
			//Get the Option Set
			WTPartUsageLink usageLink = (WTPartUsageLink) expressionAssignable;
			WTPart parent = usageLink.getUsedBy();
			WTPartMaster child = usageLink.getUses();
			ObjectReference parentRef = ObjectReference.newObjectReference (parent);
			ObjectReference childRef = ObjectReference.newObjectReference (child);
			OptionSetDelegateResult result = OptionHelper.service.getAssignedOptionSet(parentRef,childRef);
			OptionSet optionSet = result.getOptionSet();
			
			//Get the Choices
			WTList[] allChoices = OptionHelper.service.getAllChoices(usageLink, optionSet);
			WTList includedChoices = allChoices[0]; //Included Choices
			WTList excludedChoices = allChoices[1]; //Excluded Choices
			
			//Generate the expression string
			ArrayList<String> expressionArray = new ArrayList<>();
			expressionArray.addAll(getExpressionArray(includedChoices, " = "));
			expressionArray.addAll(getExpressionArray(excludedChoices, " ≠ "));
			Collections.sort(expressionArray);
			String expression = String.join("; ", expressionArray);
			
			return expression;
		
		} catch (Exception e) {
			return "Error";
		}
	
	}
	
	private static ArrayList<String> getExpressionArray(WTList choices, String includeExclude) throws WTException {
		
		//Add all the choices to a map to allow grouping by option and = / ≠
		Map<String, ArrayList<String>> map = new TreeMap<String, ArrayList<String>>();
		for (int i = 0; i < choices.size(); i++) {
			
			Object object = choices.get(i);
			if (object instanceof ObjectReference) {
				ObjectReference objectReference = (ObjectReference) object;
				Object choiceObject = objectReference.getObject();
				
				if (choiceObject instanceof Choice) {
					Choice choice = (Choice) choiceObject;
					ChoiceMaster choiceMaster = (ChoiceMaster) choice.getMaster();
					String choiceMasterName = choiceMaster.getName();
					OptionMaster optionMaster = (OptionMaster) OptionHelper.service.getOptionForChoice(choice).getMaster();
					String optionMasterName = optionMaster.getName();
					
					ArrayList<String> group = map.get(optionMasterName + includeExclude);
					if (group == null) {
						group = new ArrayList<String>();
						map.put(optionMasterName + includeExclude, group);
			        }
					group.add(choiceMasterName);
					Collections.sort(group);
				}
			}

	    }
		
		//Generate an arrayList of option expression strings
		ArrayList<String> expressionArrayList = new ArrayList<String>();
		for (Map.Entry<String, ArrayList<String>> entry : map.entrySet()) {
			ArrayList<String> choicesArrayList = entry.getValue();
			String optionExpression = entry.getKey() + String.join(", ", choicesArrayList);
			expressionArrayList.add(optionExpression);
		}
		
	    return expressionArrayList;
	
	}

 

This seems to work well 🙂

Top Tags