@@ -85,40 +85,41 @@ private List<NodeSpace> recurseVariableComponents(CombinatorialDerivation combin
8585 // order components by sequence constraints
8686 VariableComponent [] sortedVCs = sortVariableComponents (combinatorialDerivation );
8787 for (VariableComponent variableComponent : sortedVCs ) {
88+ if (variableComponent != null ) {
89+ // recurse through variant derivations
90+ Set <CombinatorialDerivation > variantDerivs = variableComponent .getVariantDerivations ();
8891
89- // recurse through variant derivations
90- Set <CombinatorialDerivation > variantDerivs = variableComponent .getVariantDerivations ();
92+ Boolean hasVariants = !variableComponent .getVariants ().isEmpty () || !variableComponent .getVariantCollections ().isEmpty ();
9193
92- Boolean hasVariants = !variableComponent .getVariants ().isEmpty () || !variableComponent .getVariantCollections ().isEmpty ();
93-
94- //handle structure for just repeats
95- if (variantDerivs .size () == 1 && !hasVariants ){
96- for (CombinatorialDerivation cv : variantDerivs ) {
97- inputSpace .add (applyOperator (variableComponent .getOperator (), recurseVariableComponents (cv )));
94+ //handle structure for just repeats
95+ if (variantDerivs .size () == 1 && !hasVariants ){
96+ for (CombinatorialDerivation cv : variantDerivs ) {
97+ inputSpace .add (applyOperator (variableComponent .getOperator (), recurseVariableComponents (cv )));
98+ }
9899 }
99- }
100100
101- //else handle collapsed complex ORs
102- else if (variantDerivs .size () > 0 ){
103- List <NodeSpace > orSpace = new LinkedList <>();
104- NodeSpace outputSpace = new NodeSpace ();
101+ //else handle collapsed complex ORs
102+ else if (variantDerivs .size () > 0 ){
103+ List <NodeSpace > orSpace = new LinkedList <>();
104+ NodeSpace outputSpace = new NodeSpace ();
105105
106- if (hasVariants ){
107- orSpace .add (createNodeSpaceFromVariableComponent (variableComponent , template )); //add variants
108- }
106+ if (hasVariants ){
107+ orSpace .add (createNodeSpaceFromVariableComponent (variableComponent , template )); //add variants
108+ }
109109
110- for (CombinatorialDerivation cv : variantDerivs ) {
111- orSpace .add (applyOperator (OperatorType .ONE , recurseVariableComponents (cv )));
112- }
110+ for (CombinatorialDerivation cv : variantDerivs ) {
111+ orSpace .add (applyOperator (OperatorType .ONE , recurseVariableComponents (cv )));
112+ }
113113
114- OROperator .apply (orSpace , outputSpace ); //"or" all the elements in the list
115- List <NodeSpace > tempSpace = new LinkedList <>();
116- tempSpace .add (outputSpace );
117- inputSpace .add (applyOperator (variableComponent .getOperator (), tempSpace ));
118- }
114+ OROperator .apply (orSpace , outputSpace ); //"or" all the elements in the list
115+ List <NodeSpace > tempSpace = new LinkedList <>();
116+ tempSpace .add (outputSpace );
117+ inputSpace .add (applyOperator (variableComponent .getOperator (), tempSpace ));
118+ }
119119
120- else if (hasVariants ){
121- inputSpace .add (createNodeSpaceFromVariableComponent (variableComponent , template ));
120+ else if (hasVariants ){
121+ inputSpace .add (createNodeSpaceFromVariableComponent (variableComponent , template ));
122+ }
122123 }
123124 }
124125
@@ -128,20 +129,69 @@ else if (hasVariants){
128129 private VariableComponent [] sortVariableComponents (CombinatorialDerivation combinatorialDerivation ){
129130 //make ordered components from sequence constraints
130131 List <Component > orderedComponents = new ArrayList <>();
131- Set <SequenceConstraint > seqConstraints = combinatorialDerivation .getTemplate ().getSequenceConstraints ();
132+
133+ ComponentDefinition template = combinatorialDerivation .getTemplate ();
134+ Set <SequenceConstraint > seqConstraints = template .getSequenceConstraints ();
135+
136+ //check if total ordering
137+ Set <URI > subjectURIs = new HashSet <URI >();
138+ Set <URI > objectURIs = new HashSet <URI >();
139+
140+ Set <URI > firstURI = new HashSet <URI >();
141+ Set <URI > lastURI = new HashSet <URI >();
142+
143+ HashMap <URI ,URI > precedesMap = new HashMap <URI ,URI >();
144+
132145 for (SequenceConstraint constraint : seqConstraints ) {
133- //subject precedes object
134- Component subject = constraint .getSubject ();
135- Component object = constraint .getObject ();
136- int subIndex = orderedComponents .indexOf (subject );
137- int objIndex = orderedComponents .indexOf (object );
138- if (subIndex == -1 && objIndex == -1 ){
139- orderedComponents .add (subject );
140- orderedComponents .add (object );
141- }else if (subIndex > -1 ){
142- orderedComponents .add (subIndex +1 , object );
143- }else if (objIndex > -1 ){
144- orderedComponents .add (objIndex , subject );
146+ if (constraint .getRestriction ().equals (RestrictionType .PRECEDES )) {
147+ subjectURIs .add (constraint .getSubjectURI ());
148+ objectURIs .add (constraint .getObjectURI ());
149+
150+ firstURI .add (constraint .getSubjectURI ());
151+ lastURI .add (constraint .getObjectURI ());
152+
153+ precedesMap .put (constraint .getSubjectURI (), constraint .getObjectURI ());
154+ }
155+ }
156+
157+ firstURI .removeAll (objectURIs );
158+ lastURI .removeAll (subjectURIs );
159+
160+ boolean totalOrdering ;
161+
162+ if (firstURI .size () == 1 && lastURI .size () == 1 ) {
163+ URI currentURI = firstURI .iterator ().next ();
164+
165+ orderedComponents .add (template .getComponent (currentURI ));
166+
167+ while (precedesMap .containsKey (currentURI )) {
168+ currentURI = precedesMap .get (currentURI );
169+
170+ orderedComponents .add (template .getComponent (currentURI ));
171+ }
172+
173+ totalOrdering = currentURI .equals (lastURI .iterator ().next ());
174+ } else {
175+ totalOrdering = false ;
176+ }
177+
178+ if (!totalOrdering ) {
179+ orderedComponents .clear ();
180+
181+ for (SequenceConstraint constraint : seqConstraints ) {
182+ //subject precedes object
183+ Component subject = constraint .getSubject ();
184+ Component object = constraint .getObject ();
185+ int subIndex = orderedComponents .indexOf (subject );
186+ int objIndex = orderedComponents .indexOf (object );
187+ if (subIndex == -1 && objIndex == -1 ){
188+ orderedComponents .add (subject );
189+ orderedComponents .add (object );
190+ }else if (subIndex > -1 ){
191+ orderedComponents .add (subIndex +1 , object );
192+ }else if (objIndex > -1 ){
193+ orderedComponents .add (objIndex , subject );
194+ }
145195 }
146196 }
147197
0 commit comments