bce9c95684f71f8ea79488721b60f4821fd76ac1
[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.springframework.beans.factory.annotation.Autowired;
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.stream.Collectors;
27
28 public abstract class KnowledgeModule {
29     @Autowired
30     private IntentService intentService;
31     //Parse, decompose, orchestrate the original intent
32    public  abstract IntentGoalBean intentCognition(Intent intent);
33     // in distribution, ask permission from imf
34     public abstract boolean recieveCreateIntent();
35     public abstract boolean recieveUpdateIntent();
36     public abstract boolean recieveDeleteIntent();
37
38     public List<String> intentResolution(Intent intent) {
39         //db contain original intent
40         List<Intent> sameNameIntentList = intentService.getIntentByName(intent.getIntentName());
41         List<String> intentIdList = new ArrayList<>();
42         if (CollectionUtils.isNotEmpty(sameNameIntentList)) {
43             //remove context.condition  ownerName = formatIntentInputManagementFunction
44             List<Intent> filterIntentList = filterIntent(sameNameIntentList);
45             List<Expectation> expectationList = intent.getIntentExpectations();
46             for (Intent dbIntent : filterIntentList) {
47                 String intentId = dbIntent.getIntentId();
48                 int count = 0;
49                 for (Expectation expectation : expectationList) {//original expectations
50                     //Determine if there is the same ObjectType
51                     List<Expectation> sameObjTypeList = dbIntent.getIntentExpectations().stream()
52                             .filter(x -> x.getExpectationObject().getObjectType().equals(expectation.getExpectationObject().getObjectType()))
53                             .collect(Collectors.toList());
54                     if (CollectionUtils.isNotEmpty(sameObjTypeList)) {
55                         //Determine the targetName of the Expectation which hava same ObjectType
56                         List<String> targetNameList = expectation.getExpectationTargets()
57                                 .stream().map(ExpectationTarget::getTargetName).collect(Collectors.toList());
58                         for (Expectation dbExpectation : sameObjTypeList) {
59                             List<String> dbTargetNameList = dbExpectation.getExpectationTargets()
60                                     .stream().map(ExpectationTarget::getTargetName).collect(Collectors.toList());
61                             //todo name compare need ai
62                             if (dbTargetNameList.containsAll(targetNameList)) {
63                                 count++;
64                                 break;
65                             }
66                         }
67                     }
68                     if (count == expectationList.size()) {
69                         intentIdList.add(intentId);
70                         break;
71                     }
72                 }
73             }
74         }
75         return intentIdList;
76     }
77
78     public List<Intent> filterIntent(List<Intent> list) {
79         //// condition   ownerName = foramtIntentInput
80         List<Intent> fiterList = new ArrayList<>();
81         for (Intent intent : list) {
82             List<Context> ownerInfo = intent.getIntentContexts().stream().filter(x ->
83                     StringUtils.equalsIgnoreCase(x.getContextName(), "ownerInfo")).collect(Collectors.toList());
84             if (CollectionUtils.isNotEmpty(ownerInfo)) {
85                 for (Context context : ownerInfo) {
86                     List<Condition> contextConditions = context.getContextConditions();
87                     boolean equals = false;
88                     for (Condition condition : contextConditions) {
89                         String conditionstr = "ownerName = formatIntentInputManagementFunction";
90                         String concatStr = condition.getConditionName() + condition.getOperator() + condition.getConditionValue();
91                         if (StringUtils.equalsIgnoreCase(concatStr.trim(), conditionstr.trim())) {
92                             fiterList.add(intent);
93                             equals = true;
94                             break;
95                         }
96                     }
97                     if (equals==true) {
98                         break;
99                     }
100                 }
101             }
102         }
103         list.removeAll(fiterList);
104         return list;
105     }
106 }