Skip to content

Commit 0a51f17

Browse files
Work on flatten type
1 parent bf3e40c commit 0a51f17

14 files changed

+143
-22
lines changed

include/fields/direction.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define DIRECTION_H
1212

1313
#include <vector>
14+
#include <string>
1415

1516
class Obj;
1617
class Field;
@@ -50,13 +51,15 @@ class Direction {
5051
*/
5152
virtual int getDirectionId() const = 0;
5253

54+
virtual std::string getName() const = 0;
55+
56+
5357
/**
5458
* @brief Gets the inverse direction
5559
*
5660
* @return The opposite direction
5761
*/
5862
virtual Direction* invert() const = 0;
59-
6063
/**
6164
* @brief Gets the link definition for a field
6265
*
@@ -99,6 +102,7 @@ class Direction {
99102
class Input : public Direction {
100103
public:
101104
int getDirectionId() const override;
105+
std::string getName() const override;
102106
Direction* invert() const override;
103107
std::vector<FieldLinkDefinition*> getFieldLinkDefinitions(FieldDefinition* fd) const override;
104108
FlattenedType* getFlattenedType(Type* type) const override;
@@ -117,6 +121,7 @@ class Input : public Direction {
117121
class Output : public Direction {
118122
public:
119123
int getDirectionId() const override;
124+
std::string getName() const override;
120125
Direction* invert() const override;
121126
std::vector<FieldLinkDefinition*> getFieldLinkDefinitions(FieldDefinition* fd) const override;
122127
FlattenedType* getFlattenedType(Type* type) const override;

include/fields/field_link_definition.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ class FieldLinkDefinition {
132132
*
133133
* @return The argument position
134134
*/
135-
int getArgument() const;
135+
std::optional<int> getArgument() const;
136+
137+
std::string getArgumentAsString() const;
138+
136139

137140
/**
138141
* @brief Gets the input field for a given object

include/fields/flattened_type.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class FlattenedType {
5757
int getFieldIndex(FieldDefinition* fd);
5858
int getNumberOfFields() const;
5959
Type* getType() const;
60+
Direction* getDirection() const;
6061

6162
FieldDefinition*** getFieldsReverse();
6263
FieldDefinition* getFieldDefinitionIdByIndex(short idx);

include/fields/obj.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class Obj : public QueueProvider {
2121
virtual RelatedObjectIterable* followManyRelation(Relation* rel) const = 0;
2222
virtual Obj* followSingleRelation(const Relation* rel) = 0;
2323
bool isInstanceOf(Type* t);
24-
Field* getFieldOutput(FieldDefinition* fd);
24+
Field* getFieldInput(FieldDefinition* fd) const;
25+
Field* getFieldOutput(FieldDefinition* fd) const;
2526
Field* getOrCreateFieldInput(FieldDefinition* fd);
2627
Obj& setFieldValue(FieldDefinition* fd, double v);
2728
double getFieldValue(FieldDefinition* fd);
@@ -30,6 +31,7 @@ class Obj : public QueueProvider {
3031

3132
std::string toKeyString();
3233
std::string toString() const;
34+
std::string getFieldAsString(FieldDefinition* fd) const;
3335
};
3436

3537
#endif // OBJ_H

python-tests/subtraction-test.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,31 @@ def testSubtraction(self, linking_pos, test_name):
5151
oa = typeA.instantiate()
5252
ob = typeB.instantiate()
5353

54+
print("linking_pos:", linking_pos)
55+
5456
if linking_pos == 0:
5557
aika.TestObj.linkObjects(oa, ob)
5658
ob.initFields()
5759

5860
oa.setFieldValue(a, 50.0)
61+
print("oa.getFieldValue(a):", oa.getFieldValue(a))
5962

6063
if linking_pos == 1:
6164
aika.TestObj.linkObjects(oa, ob)
6265
ob.initFields()
6366

6467
oa.setFieldValue(b, 20.0)
68+
print("oa.getFieldValue(b):", oa.getFieldValue(b))
6569

6670
if linking_pos == 2:
6771
aika.TestObj.linkObjects(oa, ob)
6872
ob.initFields()
6973

70-
self.assertEqual(30.0, ob.getFieldOutput(c).getValue())
74+
print("ob.getFieldValue(c):", ob.getFieldValue(c))
75+
76+
print("ob.getFieldAsString(c):", ob.getFieldAsString(c))
77+
78+
self.assertEqual(30.0, ob.getFieldValue(b))
7179

7280
if __name__ == '__main__':
7381
unittest.main()

src/fields/direction.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ int Input::getDirectionId() const {
2424
return 0; // Return 0 for the "input" direction
2525
}
2626

27+
std::string Input::getName() const {
28+
return "input"; // Return the name of the input direction
29+
}
30+
2731
Direction* Input::invert() const {
2832
return Direction::OUTPUT; // Return the OUTPUT direction
2933
}
@@ -48,6 +52,10 @@ int Output::getDirectionId() const {
4852
return 1; // Return 1 for the "output" direction
4953
}
5054

55+
std::string Output::getName() const {
56+
return "output"; // Return the name of the output direction
57+
}
58+
5159
Direction* Output::invert() const {
5260
return Direction::INPUT; // Return the INPUT direction as the inverse
5361
}

src/fields/field.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,7 @@ void Field::receiveUpdate(double u) {
217217
* @return String representation in the format "name: value"
218218
*/
219219
std::string Field::toString() const {
220-
std::stringstream ss;
221-
ss << getName() << ": " << getValueString();
222-
return ss.str();
220+
return getName() + ": " + getValueString();
223221
}
224222

225223
/**
@@ -228,5 +226,5 @@ std::string Field::toString() const {
228226
* @return Value string representation
229227
*/
230228
std::string Field::getValueString() const {
231-
return toString();
229+
return std::to_string(value);
232230
}

src/fields/field_definition.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ FieldDefinition::FieldDefinition(Type* objectType, const std::string& name, int
3333
objectType->setFieldDefinition(this);
3434

3535
if (numArgs > 0) {
36-
inputs.reserve(numArgs);
36+
inputs.resize(numArgs);
3737
}
3838
}
3939

@@ -148,6 +148,11 @@ FieldDefinition* FieldDefinition::resolveInheritedFieldDefinition(const std::set
148148
* @param field The field to initialize
149149
*/
150150
void FieldDefinition::initializeField(Field* field) {
151+
std::cout << "Initializing field " << std::endl;
152+
std::cout << "Field " << field << std::endl;
153+
std::cout << "Field object type " << field->getObject()->getType() << std::endl;
154+
std::cout << "Field object type flattened type input side " << field->getObject()->getType()->getFlattenedTypeInputSide() << std::endl;
155+
151156
field->getObject()->getType()->getFlattenedTypeInputSide()->followLinks(field);
152157
}
153158

@@ -159,10 +164,20 @@ void FieldDefinition::initializeField(Field* field) {
159164
* @param fl The field link to add
160165
*/
161166
void FieldDefinition::addInput(FieldLinkDefinition* fl) {
162-
if(fl->getArgument() >= 0)
163-
inputs[fl->getArgument()] = fl;
164-
else
165-
inputs.push_back(fl);
167+
std::cout << "Adding input " << fl->toString() << std::endl;
168+
std::cout << " Argument:" << fl->getArgumentAsString() << std::endl;
169+
170+
if (fl->getArgument().has_value()) {
171+
int arg = fl->getArgument().value();
172+
if (arg >= inputs.size()) {
173+
inputs.resize(arg + 1);
174+
}
175+
inputs[arg] = fl;
176+
} else {
177+
inputs.push_back(fl);
178+
}
179+
180+
std::cout << " Number of inputs:" << getInputs().size() << std::endl;
166181
}
167182

168183
/**

src/fields/field_link_definition.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,22 @@ Direction* FieldLinkDefinition::getDirection() const {
112112
*
113113
* @return The argument position
114114
*/
115-
int FieldLinkDefinition::getArgument() const {
116-
return argument.value_or(0);
115+
std::optional<int> FieldLinkDefinition::getArgument() const {
116+
return argument;
117+
}
118+
119+
/**
120+
* @brief Gets the argument as a string
121+
*
122+
* Returns "no argument" if no argument position is set.
123+
*
124+
* @return The argument position as a string
125+
*/
126+
std::string FieldLinkDefinition::getArgumentAsString() const {
127+
if (argument) {
128+
return std::to_string(argument.value());
129+
}
130+
return "no argument";
117131
}
118132

119133
/**
@@ -122,8 +136,7 @@ int FieldLinkDefinition::getArgument() const {
122136
* @return String representation
123137
*/
124138
std::string FieldLinkDefinition::toString() const {
125-
// TODO: Implement proper string representation
126-
return "";
139+
return "FieldLinkDefinition(" + originFD->getName() + " -> " + relatedFD->getName() + ")";
127140
}
128141

129142
/**

src/fields/flattened_type.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ void FlattenedType::flatten() {
125125
mapping = new FlattenedTypeRelation**[type->getRelations().size()];
126126

127127
for (const auto& rel : type->getRelations()) {
128+
std::cout << "FlattenedType::flatten rel " << rel->getRelationLabel() << std::endl;
129+
128130
std::vector<FlattenedTypeRelation*> resultsPerRelation(type->getTypeRegistry()->getTypes().size());
129131
for (const auto& relatedType : type->getTypeRegistry()->getTypes()) {
130132
resultsPerRelation[relatedType->getId()] = flattenPerType(rel, relatedType);
@@ -155,6 +157,8 @@ void FlattenedType::flatten() {
155157
FlattenedTypeRelation* FlattenedType::flattenPerType(Relation* relation, Type* relatedType) {
156158
std::vector<FieldLinkDefinition*> fieldLinks;
157159

160+
std::cout << "FlattenedType::flattenPerType begin " << relation->getRelationLabel() << " " << relatedType->getName() << std::endl;
161+
158162
for (int i = 0; i < numberOfFields; i++) {
159163
FieldDefinition** fdArray = fieldsReverse[i];
160164

@@ -172,6 +176,8 @@ FlattenedTypeRelation* FlattenedType::flattenPerType(Relation* relation, Type* r
172176
}
173177
}
174178
}
179+
180+
std::cout << "FlattenedType::flattenPerType end fieldLinks " << fieldLinks.size() << std::endl;
175181
}
176182

177183
return fieldLinks.empty() ?
@@ -198,6 +204,7 @@ void FlattenedType::followLinks(Field* field) {
198204
auto it = iterable->iterator();
199205
while (it->hasNext()) {
200206
auto relatedObj = it->next();
207+
201208
followLinks(ftr[relatedObj->getType()->getId()], relatedObj, field);
202209
}
203210
delete it;
@@ -257,6 +264,17 @@ Type* FlattenedType::getType() const {
257264
return type;
258265
}
259266

267+
/**
268+
* @brief Gets the direction
269+
*
270+
* This function gets the direction in the flattened type.
271+
*
272+
* @return The direction in the flattened type
273+
*/
274+
Direction* FlattenedType::getDirection() const {
275+
return direction;
276+
}
277+
260278
/**
261279
* @brief Gets the fields reverse
262280
*

0 commit comments

Comments
 (0)