# Secure Partitioning of Cloud Applications, with Cost Look-Ahead
## 6 Partitioning with a Look Ahead on Migration Costs
### Probabilistic SKnife
The last predicates we present are used to calculate the labellings with at most K differences from the starting one, listed in Listing 10. ```
68 labelling(K,L):-
69 dataCharList(DCs),
70 labelling(DCs,K,L,_). 71 labelling([DC|DCs],K,[(DC,Lb,P)|Labelling],Diff):-
72 labelling(DCs,K,Labelling,Diff),
73 tagChange(DC,Lb,P),
74 tag(DC,Lb),
75 labelling([DC|DCs],K,[(DC,Lb,P)|Labelling],NewDiff):-
76 labelling(DCs,K,Labelling,Diff),
77 tagChange(DC,Lb,P),
78\+tag(DC,Lb),
79 NewDiffsDiff+1,
80 NewDiffs+K. 81 labelling(K|_,[],0). ```  
**Listing 10**. The labelling/2 and labelling/4 predicates. The top-level predicate is labellingK/2. By specifying the parameter K, the resulting labelling L is a list of elements (DC,Lb,P) representing the name of the data or characteristic (DC), its label (Lb) and the probability (P) of having that label. This predicate retrieves the list of all data and characteristic DCs (line 69) and then calls the sub-predicate with it (line 70). The predicate labelling/4 recursively creates a labelling starting from an empty list and initialising the number of differences from the starting one at 0 (line 81). Then, it either includes a pair of data/characteristics and labels of the starting labelling (first clause of line 71) or include a new pair, incrementing the number of differences (second clause of line 75). ProbSKnife is usable through a Python script that queries the Prolog code and, by parsing the results, builds the probabilistic model and calculates the future cost of the partitionings. The usage of this script is as follows  
```
python3 main.pyAPPID[-dOLMIT][-kK][-f][-l][-h]APPIDidentifieroftapplicationDILMITanintegerKaninteger-fshowsfullresultsintables-lshowspartitioninglabels-tshowsthetimestampofoperations-hshowsthehelp
```  
The main arguments of the program are the application identifier used in the Prolog definition (APPID), the desired maximum limit of domains (OLMIT) and the maximum number of label changes from the starting one (K). The code of the Python program is listed in Listing 11, omitting the logging and timestamp calculations. The first Prolog query retrieves the starting labelling and finds the maximum values for K and DLimit (line 83). Then, the labellingK/2 predicate is queried to retrieve all the labellings with K different labels from the starting one. For each of them, the probabilistic model (the random variables \(Y_{i}\) and \(Z_{se}^{k}\)) is calculated (line 84). To find all the possible partitionings that satisfy the starting labelling, the sknife/4 predicate is queried (line 85). For each partitioning \(\mathsf{p}\) (line 86) all the labellings are scanned (line 11). All the partitionings with at most \(DLimit\) domains and that satisfy each labelling are calculated by the sknife/4 predicate and the cost from \(\mathsf{p}\) is calculated by the cost/3 predicate (line 93). If a labelling makes the application non-safely partitionable (line 94), the probability of reaching this labelling is accumulated to the total probability of impossible migration (line 96). Otherwise, all the results--partitioning, costs, labellings and probabilities--are recorded (lines 99-102). ```
82(appId,D,K,tables,timestamp)=checkArgs()
83(StartLabelling,D,K)=checkApp(appId,K,D)
84(labellings,Already)=queryLabellings(appId,StartLabelling,K)
85 partitions=queryAllPartitionings(appId,StartLabelling,D)
86 for\(\mathsf{p}\)inpartitionings:
87 labs=[]
88 parts=[]
89 costs=[]
90 probs=[]
91 impossible=0.0
92 forlabelling,probin labellings:
93 pcs=queryCost(appId,p,labelling,D)
94 if(pcsisNone):
95#probabilitythatlabellingisnotsatisfiable
96 impossible=prob
97 continue
98 for(part,cost)inpcs:
99 labs.append(Labelling)
100 parts.append(part)
101 costs.append(cost)
102 probs.append(prob)
103(sumProb,expCost)=buildResults(labs,parts,costs,probs)
```  
**Listing 11**. The Python program of ProbSKnife. Finally, the future cost of \(\mathsf{p}\) is calculated (line 103) by grouping the results by labelling, taking the minimum cost and multiplying it by the labelling probability.