E. PROPOSTES DE RESOLUCIÓ
4. Proposicions no de llei i altres proposicions
ThecalculateOffset(weavingModel)operation, displayed in Algorithm 5.1, operat- ing on theweavingModelrecalculates the offsets of allTextElementsand is called after each change to the underlying data model and each change to the text displayed in the model editor, because these two operations change the offsets of model elements represented in the projected text. The input to the algo- rithm is the weavingModelon which the offsets of all TextElements have to be recalculated. First, the algorithm retrieves a treeIteratorfor the weavingModel
(line 1). ThetreeIteratortraverses the containment tree of theweavingModelfol- lowing a depth first approach. CallinghasNext(treeIterator)on the tree iterator checks if there are more elements to traverse and the next(treeIterator) oper- ation retrieves the next element in the containment tree of the weavingModel
from the treeIterator. Then the global currentOffsetcounter is set to its initial value, 0, in line 2. The main work of the algorithm is done by lines 3 to 9.
5.4. Textual Weaving Model
Data: weavingModel
Result: Recalculate all offsets after a change 1 treeIterator ← createTreeIterator(weavingModel); 2 currentOffset ← 0; 3 while hasNext(treeIterator) do 4 current ← next(treeIterator); 5 if isTextElement(current) then 6 setOffset(current, currentOffset); 7 setLength(current);
8 currentOffset ← currentOffset + length(current)
9 end
10 end
Algorithm 5.1: ThecalculateOffset(weavingModel)operation.
In this part of the algorithm, the current model element is checked to see if it is of type TextElement (line 5). If this is the case the setOffset(current, cur- rentOffset) operation (line 6) sets the value of the current TextElement to the globally stored currentOffset, the length attribute of the current TextElement is recalculated (line 7) and the value of thecurrent TextElement’s lengthattribute is added to thecurrentOffset(line 8).
The findTextElement(weavingModel, offset, searchStrategy) operation is speci- fied in Algorithm 5.2. The algorithm retrieves a TextElement from a given
weavingModelfor a givenoffset applying a givensearchStrategy. The algorithm first searches allTextElementsfor the offset which is closest to the cursoroffset
(line 1 - line 9). Then the algorithm performs a depth first search on the
weavingModel’scontainment tree for theTextElementswhich have the minimum distance to the cursor offset (line 11 - line 21).
Each model element (current) is checked to determine if it is aWeavingLinkor aTextElementusing theisTextElement(current)(line 13) operation. If thecurrent
model element is aTextElement, it is determined whether theoffsetof the edited text is: 1. at the beginning or within theTextElement(line 14) or 2. at the end of theTextElement(line 18). In both cases theTextElementis added to theresult. If the edited offset is placed between two text elements in the edited file, both conditions are executed and thus more than one result is potentially retrieved
Data: weavingModel, offset, searchStrategy Result: Finds aTextElementfor a given offset.
1 offsets ← ∅, results ← ∅;
2 iterator ← createTreeIterator(weavingModel); 3 while hasNext(iterator) do
4 current ← next(iterator);
5 if ¬isT extEelement(current) then continue; 6 if offset - offset(current) ≥ 0 then
7 offsets ← offsets ∪ (offset - offset(current));
8 end
9 minDistance ← min(offsets);
10 iterator ← createTreeIterator(weavingModel); 11 while hasNext(iterator) do
12 current ← next(iterator);
13 if ¬isT extEelement(current) then continue;
// in model element or at beginning
14 if offset - offset(current) = minDistance then 15 results ← results ∪ current;
16 break;
17 end
// the end of a model element, when between two
18 if minDistance = 0 then
19 if offset - (offset(current)+length(current)) = minDistance then
results ← results ∪ current;
20 end
21 end
22 if size(results > 1) then
23 if strategy = ModelElementPreferred then getModelElement(results); 24 if strategy = TraitPreferred then getTrait(results);
25 else
26 first(results);
27 end
Algorithm 5.2: The findTextElement(weavingModel, offset, searchStrategy) op- eration.
5.4. Textual Weaving Model
from the weaving model.
To resolve this ambiguity the algorithm is configured with different strate- gies when executed. The algorithm can either return the first model element (line 23), the first trait (line 24) or the first element no matter what the type of the weaved target (line 26) from theresultsset is. In case a search strategy is configured to prefer a trait or model element and more than one trait or model element is in theresultslist the element residing at the first position of the list is returned.
Data: weavingModel, offset, file, add, length
Result: Change the model trait in the abstract syntax.
1 textElement ← findTextElement(weavingModel, offset, TraitPreferred);
2 weavingLink ← getWeavingLink(textElement); 3 if add then
4 textElement, setText(substring(file, offset, length(textElement)) +
length)
5 else
6 setText(textElement, substring(file, offset, length(textElement)) -
length)
7 end
8 recalculateOffset(weavingModel);
9 setAbstractSyntaxValue(weavingLink, textElement);
Algorithm 5.3: TheeditTrait(weavingModel, offset, file, add, length)operation.
TheeditTrait(weavingModel, offset, file, add, length)operation as described in Algorithm 5.3 is responsible for transferring changes from the projection in the text editor to the abstract syntax model. The algorithm first searches for the
TextElementat the current offset (line 1) using thefindTextElement(weavingModel, offset, TraitPreferred)operation and retrieves its correspondingWeavingLinkus- ing the getWeavingLink(textElement) operation (line 2). Then the text of the
textElement is set to the new value depending whether a character is being added (line 4) or removed (line 6). Finally, the offsets for the changed weaving model are recalculated (line 8) using therecalculateOffset(weavingModel) oper- ation and the value is written into the abstract syntax representation of the edited model (line 9) using thesetAbstractSyntaxValue(weavingLink, textElement)
operation.
The add and remove model element operations on the abstract syntax model representation work in a similar way to the edit operation described in Algorithm 5.3. The difference is that the remove operation deletes the Weav- ingLink containing the TextElement and the add operation adds a new Weav- ingLinkand its correspondingTextElementsto the model. Afterward, the offsets in theWeavingModelhave to be recalculated. In the predefined modeling mode a modeler can use the textual model editor to add or remove all linguistic model elements (i.e. clabject, model, attribute), whereas in the user-defined mode the modeler can only add or delete clabjects.
Operations for synchronizing changes from the abstract syntax model to the text editor also work in a similar way to Algorithm 5.3. The main difference is that theweavingLinkis not retrieved using anoffsetbut using the edited model element (i.e. trait for theeditTrait()operation) to which theweavingLinkpoints. Then, the corresponding TextElementand the text editor are updated instead of the abstract syntax as described in Algorithm 5.3. Since the algorithms transporting information from the abstract syntax to the text editor are very similar to the algorithms transporting information in the other direction they are not explained in more detail here.