|
27 | 27 | import grakn.core.concept.Label; |
28 | 28 | import grakn.core.concept.answer.ConceptMap; |
29 | 29 | import grakn.core.concept.thing.Attribute; |
| 30 | +import grakn.core.concept.thing.Relation; |
| 31 | +import grakn.core.concept.type.Role; |
30 | 32 | import grakn.core.concept.type.Rule; |
31 | 33 | import grakn.core.concept.type.SchemaConcept; |
32 | 34 | import grakn.core.concept.type.Type; |
@@ -327,16 +329,23 @@ public Stream<Predicate> getInnerPredicates(){ |
327 | 329 | return Stream.concat(super.getInnerPredicates(), getMultiPredicate().stream()); |
328 | 330 | } |
329 | 331 |
|
330 | | - private void attachAttribute(Concept owner, Attribute attribute){ |
| 332 | + private Relation attachAttribute(Concept owner, Attribute attribute){ |
331 | 333 | //check if link exists |
332 | 334 | if (owner.asThing().attributes(attribute.type()).noneMatch(a -> a.equals(attribute))) { |
333 | 335 | if (owner.isEntity()) { |
334 | | - EntityImpl.from(owner.asEntity()).attributeInferred(attribute); |
| 336 | + return EntityImpl.from(owner.asEntity()).attributeInferred(attribute); |
335 | 337 | } else if (owner.isRelation()) { |
336 | | - RelationImpl.from(owner.asRelation()).attributeInferred(attribute); |
| 338 | + return RelationImpl.from(owner.asRelation()).attributeInferred(attribute); |
337 | 339 | } else if (owner.isAttribute()) { |
338 | | - AttributeImpl.from(owner.asAttribute()).attributeInferred(attribute); |
| 340 | + return AttributeImpl.from(owner.asAttribute()).attributeInferred(attribute); |
339 | 341 | } |
| 342 | + return null; |
| 343 | + } else { |
| 344 | + Role ownerRole = tx().getRole(Schema.ImplicitType.HAS_OWNER.getLabel(attribute.type().label()).getValue()); |
| 345 | + Role valueRole = tx().getRole(Schema.ImplicitType.HAS_VALUE.getLabel(attribute.type().label()).getValue()); |
| 346 | + return owner.asThing().relations(ownerRole) |
| 347 | + .filter(relation -> relation.rolePlayersMap().get(valueRole).contains(attribute)) |
| 348 | + .findFirst().orElse(null); |
340 | 349 | } |
341 | 350 | } |
342 | 351 |
|
@@ -375,10 +384,16 @@ public Stream<ConceptMap> materialise(){ |
375 | 384 | } |
376 | 385 |
|
377 | 386 | if (attribute != null) { |
378 | | - attachAttribute(owner, attribute); |
379 | | - return Stream.of(ConceptUtils.mergeAnswers(substitution, new ConceptMap(ImmutableMap.of(resourceVariable, attribute)))); |
| 387 | + Relation relation = attachAttribute(owner, attribute); |
| 388 | + if (relation != null) { |
| 389 | + ConceptMap answer = new ConceptMap(ImmutableMap.of(resourceVariable, attribute)); |
| 390 | + if (getRelationVariable().isReturned()){ |
| 391 | + answer = ConceptUtils.mergeAnswers(answer, new ConceptMap(ImmutableMap.of(getRelationVariable(), relation))); |
| 392 | + } |
| 393 | + return Stream.of(ConceptUtils.mergeAnswers(substitution, answer)); |
| 394 | + } |
380 | 395 | } |
381 | | - return Stream.of(new ConceptMap()); |
| 396 | + return Stream.empty(); |
382 | 397 | } |
383 | 398 |
|
384 | 399 | /** |
|
0 commit comments