02d467a229d11b1cf10cf3557c581a3d6ef1167a
[usecase-ui/intent-analysis.git] /
1 /*
2  * Copyright (C) 2022 CMCC, Inc. and others. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.usecaseui.intentanalysis.intentBaseService.intentModule;
17
18 import org.apache.commons.collections.CollectionUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.onap.usecaseui.intentanalysis.bean.models.*;
21 import org.onap.usecaseui.intentanalysis.service.IntentService;
22 import org.onap.usecaseui.intentanalysis.service.impl.IntentServiceImpl;
23 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.stereotype.Component;
25 import org.springframework.stereotype.Service;
26
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.stream.Collectors;
30
31 public abstract class KnowledgeModule {
32     @Autowired
33     private IntentService intentService;
34     //Parse, decompose, orchestrate the original intent
35    public  abstract IntentGoalBean intentCognition(Intent intent);
36     // in distribution, ask permission from imf
37     public abstract boolean recieveCreateIntent();
38     public abstract boolean recieveUpdateIntent();
39     public abstract boolean recieveDeleteIntent();
40
41     public List<String> intentResolution(Intent intent) {
42         //db contain original intent
43         List<Intent> sameNameIntentList = intentService.getIntentByName(intent.getIntentName());
44         List<String> intentIdList = new ArrayList<>();
45         if (CollectionUtils.isNotEmpty(sameNameIntentList)) {
46             //remove context.condition  ownerName = formatIntentInputManagementFunction
47             List<Intent> filterIntentList = filterIntent(sameNameIntentList);
48             List<Expectation> expectationList = intent.getIntentExpectations();
49             for (Intent dbIntent : filterIntentList) {
50                 String intentId = dbIntent.getIntentId();
51                 int count = 0;
52                 for (Expectation expectation : expectationList) {//original expectations
53                     //Determine if there is the same ObjectType
54                     List<Expectation> sameObjTypeList = dbIntent.getIntentExpectations().stream()
55                             .filter(x -> x.getExpectationObject().getObjectType().equals(expectation.getExpectationObject().getObjectType()))
56                             .collect(Collectors.toList());
57                     if (CollectionUtils.isNotEmpty(sameObjTypeList)) {
58                         //Determine the targetName of the Expectation which hava same ObjectType
59                         List<String> targetNameList = expectation.getExpectationTargets()
60                                 .stream().map(ExpectationTarget::getTargetName).collect(Collectors.toList());
61                         for (Expectation dbExpectation : sameObjTypeList) {
62                             List<String> dbTargetNameList = dbExpectation.getExpectationTargets()
63                                     .stream().map(ExpectationTarget::getTargetName).collect(Collectors.toList());
64                             //todo name compare need ai
65                             if (dbTargetNameList.containsAll(targetNameList)) {
66                                 count++;
67                                 break;
68                             }
69                         }
70                     }
71                     if (count == expectationList.size()) {
72                         intentIdList.add(intentId);
73                         break;
74                     }
75                 }
76             }
77         }
78         return intentIdList;
79     }
80
81     public List<Intent> filterIntent(List<Intent> list) {
82         //// condition   ownerName = foramtIntentInput
83         List<Intent> fiterList = new ArrayList<>();
84         for (Intent intent : list) {
85             List<Context> ownerInfo = intent.getIntentContexts().stream().filter(x ->
86                     StringUtils.equalsIgnoreCase(x.getContextName(), "ownerInfo")).collect(Collectors.toList());
87             if (CollectionUtils.isNotEmpty(ownerInfo)) {
88                 for (Context context : ownerInfo) {
89                     List<Condition> contextConditions = context.getContextConditions();
90                     boolean equals = false;
91                     for (Condition condition : contextConditions) {
92                         String conditionstr = "ownerName = formatIntentInputManagementFunction";
93                         String concatStr = condition.getConditionName() + condition.getOperator() + condition.getConditionValue();
94                         if (StringUtils.equalsIgnoreCase(concatStr.trim(), conditionstr.trim())) {
95                             fiterList.add(intent);
96                             equals = true;
97                             break;
98                         }
99                     }
100                     if (equals==true) {
101                         break;
102                     }
103                 }
104             }
105         }
106         list.removeAll(fiterList);
107         return list;
108     }
109 }