cb43954fde676a64d3dfee431a833cb8aa7346cd
[occt.git] / src / Expr / Expr_BinaryExpression.cxx
1 // Created on: 1991-04-12
2 // Created by: Arnaud BOUZY
3 // Copyright (c) 1991-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <Expr_BinaryExpression.ixx>
18 #include <Expr_NamedUnknown.hxx>
19 #include <Expr_InvalidOperand.hxx>
20 #include <Standard_OutOfRange.hxx>
21
22
23 void Expr_BinaryExpression::SetFirstOperand (const Handle(Expr_GeneralExpression)& exp)
24 {
25   Handle(Expr_BinaryExpression) me;
26   me = this;
27   if (exp == me) {
28     Expr_InvalidOperand::Raise();
29   }
30   if (exp->Contains(me)) {
31     Expr_InvalidOperand::Raise();
32   }
33   myFirstOperand = exp;
34 }
35
36 void Expr_BinaryExpression::SetSecondOperand (const Handle(Expr_GeneralExpression)& exp)
37 {
38   Handle(Expr_BinaryExpression) me;
39   me = this;
40   if (exp == me) {
41     Expr_InvalidOperand::Raise();
42   }
43   if (exp->Contains(me)) {
44     Expr_InvalidOperand::Raise();
45   }
46   mySecondOperand = exp;
47 }
48
49 void Expr_BinaryExpression::CreateFirstOperand (const Handle(Expr_GeneralExpression)& exp)
50 {
51   myFirstOperand = exp;
52 }
53
54 void Expr_BinaryExpression::CreateSecondOperand (const Handle(Expr_GeneralExpression)& exp)
55 {
56   mySecondOperand = exp;
57 }
58
59 Standard_Integer Expr_BinaryExpression::NbSubExpressions () const
60 {
61   return 2;
62 }
63
64 const Handle(Expr_GeneralExpression)& Expr_BinaryExpression::SubExpression (const Standard_Integer I) const
65 {
66   if (I == 1) {
67     return myFirstOperand;
68   }
69   else {
70     if (I == 2) {
71       return mySecondOperand;
72     }
73     else {
74       Standard_OutOfRange::Raise();
75     }
76   }
77  return *(  ( Handle_Expr_GeneralExpression* )NULL  );
78 }
79
80 Standard_Boolean Expr_BinaryExpression::ContainsUnknowns () const
81 {
82   if (myFirstOperand->IsKind(STANDARD_TYPE(Expr_NamedUnknown))) {
83     return Standard_True;
84   }
85   if (mySecondOperand->IsKind(STANDARD_TYPE(Expr_NamedUnknown))) {
86     return Standard_True;
87   }
88   if (myFirstOperand->ContainsUnknowns()) {
89     return Standard_True;
90   }
91   if (mySecondOperand->ContainsUnknowns()) {
92     return Standard_True;
93   }
94   return Standard_False;
95 }
96
97 Standard_Boolean Expr_BinaryExpression::Contains (const Handle(Expr_GeneralExpression)& exp) const
98 {
99   if (myFirstOperand == exp) {
100     return Standard_True;
101   }
102   if (mySecondOperand == exp) {
103     return Standard_True;
104   }
105   if (myFirstOperand->Contains(exp)) {
106     return Standard_True;
107   }
108   if (mySecondOperand->Contains(exp)) {
109     return Standard_True;
110   }
111   return Standard_False;
112 }
113
114 void Expr_BinaryExpression::Replace (const Handle(Expr_NamedUnknown)& var, const Handle(Expr_GeneralExpression)& with)
115 {
116   if (myFirstOperand == var) {
117     SetFirstOperand(with);
118   }
119   else {
120     if (myFirstOperand->Contains(var)) {
121       myFirstOperand->Replace(var,with);
122     }
123   }
124   if (mySecondOperand == var) {
125     SetSecondOperand(with);
126   }
127   else {
128     if (mySecondOperand->Contains(var)) {
129       mySecondOperand->Replace(var,with);
130     }
131   }
132 }
133
134
135 Handle(Expr_GeneralExpression) Expr_BinaryExpression::Simplified() const
136 {
137   Handle(Expr_BinaryExpression) cop = Handle(Expr_BinaryExpression)::DownCast(Copy());
138   Handle(Expr_GeneralExpression) op1 = cop->FirstOperand();
139   Handle(Expr_GeneralExpression) op2 = cop->SecondOperand();
140   cop->SetFirstOperand(op1->Simplified());
141   cop->SetSecondOperand(op2->Simplified());
142   return cop->ShallowSimplified();
143 }