summaryrefslogtreecommitdiffstats
path: root/src/core/expression/RBAForAllOperator.cpp
blob: eca8ccad537fbff5ce9402be504b48a1eb09be1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
 * Copyright (c) 2019 DENSO CORPORATION.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * ForAll operator class
 */

#include "RBAForAllOperator.hpp"

#include "RBAAllocatable.hpp"
#include "RBAAllocatableSet.hpp"
#include "RBAContent.hpp"
#include "RBAContentSet.hpp"
#include "RBAExpressionVisitor.hpp"
#include "RBALambdaExpression.hpp"
#include "RBALogManager.hpp"
#include "RBAExpressionType.hpp"
#include "RBAModelElementType.hpp"
#include "RBAConstraintInfo.hpp"

namespace rba
{

void
RBAForAllOperator::accept(RBAExpressionVisitor& visitor)
{
  visitor.visit(*this);
}

RBAModelElementType
RBAForAllOperator::getModelElementType() const
{
  return RBAModelElementType::ForAllOperator;
}

bool
RBAForAllOperator::executeCore(RBAConstraintInfo* info,
				   RBAArbitratorImpl * arb) const
{
  const RBAExpression* const setObj {getLhsOperand()};
  RBALambdaExpression* const lambda {getLambda()};

  // Add itself to the constraint hierarchy for coverage
  LOG_addHierarchy(LOG_getSymbol());

  // get Rule object and loop
  RBAConstraintInfo* const leftInfo {info->getChild(0U)};

  std::vector<const RBARuleObject*> objs;
  const RBARuleObject* const ruleObj {setObj->getReferenceObject(leftInfo, arb)};
  const RBARuleObject* objset {nullptr};
  if (ruleObj != nullptr) {
    objset = ruleObj->getRawObject();
  }

  if(leftInfo->isExceptionBeforeArbitrate() || (objset == nullptr)) {
    // Remove itself from the constraint hierarchy for coverage
    LOG_removeHierarchy();

    info->setExceptionBeforeArbitrate(true);
    return false;
  }
  if (dynamic_cast<const RBAAllocatableSet*>(objset) != nullptr) {
    objs.clear();
    for(const RBAAllocatable* alloc
	  : dynamic_cast<const RBAAllocatableSet*>(objset)->getLeafAllocatable()) {
      objs.push_back(alloc);
    }
  }
  else {
    objs.clear();
    for(const auto& cont : dynamic_cast<const RBAContentSet*>(objset)->getLeafContent() ){
      objs.push_back(cont);
    }
  }

  bool isTrue {true};
  std::uint32_t i {0U};
  for(const RBARuleObject* const obj : objs) {
    // Add count to the constraint hierarchy for coverage
    LOG_addHierarchy("#" + std::to_string(i));

    RBAConstraintInfo* const childInfo {info->getChild(i)};
    lambda->setRuleObj(obj);

    const bool res {lambda->execute(childInfo, arb)};

    // Remove count from the constraint hierarchy for coverage
    LOG_removeHierarchy();

    if(childInfo->isExceptionBeforeArbitrate()) {
      info->setExceptionBeforeArbitrate(true);
    } else {
      isTrue = (isTrue && res);
    }
    i++;
  }

  if (isTrue == false) {
    LOG_arbitrateConstraintLogicLogLine(
        "      [" + LOG_getExpressionText() + "] false");
    LOG_coverageConstraintExpressionLog(LOG_getCoverageExpressionText(),
                                        RBAExecuteResult::FALSE);
    // Remove count from the constraint hierarchy for coverage
    LOG_removeHierarchy();
    info->setExceptionBeforeArbitrate(false);
    return false;
  } else if (info->isExceptionBeforeArbitrate()) {
    LOG_arbitrateConstraintLogicLogLine(
        "      [" + LOG_getExpressionText() + "] before arbitrate skip");
    LOG_coverageConstraintExpressionLog(LOG_getCoverageExpressionText(),
                                        RBAExecuteResult::SKIP);
    // Remove count from the constraint hierarchy for coverage
    LOG_removeHierarchy();
    return false;
  } else {
    LOG_arbitrateConstraintLogicLogLine(
        "      [" + LOG_getExpressionText() + "] true");
    LOG_coverageConstraintExpressionLog(LOG_getCoverageExpressionText(),
                                        RBAExecuteResult::TRUE);
    // Remove count from the constraint hierarchy for coverage
    LOG_removeHierarchy();
    return true;
  }
}

#ifdef RBA_USE_LOG
const std::string
RBAForAllOperator::getSymbol() const
{
  return "For All";
}

const std::string
RBAForAllOperator::getExpressionText() const
{
  RBAExpression* exprLhs = getLhsOperand();
  return getSymbol() + " "
    + exprLhs->getExpressionText() + " "
    + getLambda()->getExpressionText();
}

const std::string
RBAForAllOperator::getCoverageExpressionText() const
{
  getLambda()->clearRuleObj();
  RBAExpression* exprLhs = getLhsOperand();
  return getSymbol() + " "
    + exprLhs->getCoverageExpressionText() + " "
    + getLambda()->getCoverageExpressionText();
}

RBAExpressionType
RBAForAllOperator::getUnderlyingType() const
{
  return RBAExpressionType::BOOLEAN;
}
#endif

}