/** * @param typedVar variable of interest * @param parentType to be checked * @return true if typing the typeVar with type is compatible with role configuration of this query */ @Override public boolean isTypeRoleCompatible(Var typedVar, Type parentType){ if (parentType == null || Schema.MetaSchema.isMetaLabel(parentType.label())) return true; Set<Type> parentTypes = parentType.subs().collect(Collectors.toSet()); return getAtoms(RelationshipAtom.class) .filter(ra -> ra.getVarNames().contains(typedVar)) .noneMatch(ra -> ra.getRoleVarMap().entries().stream() //get roles this type needs to play .filter(e -> e.getValue().equals(typedVar)) .filter(e -> !Schema.MetaSchema.isMetaLabel(e.getKey().label())) //check if it can play it .anyMatch(e -> e.getKey().players().noneMatch(parentTypes::contains))); }
private ImmutableList<SchemaConcept> inferPossibleTypes(ConceptMap sub){ if (getSchemaConcept() != null) return ImmutableList.of(this.getSchemaConcept()); if (sub.containsVar(getPredicateVariable())) return ImmutableList.of(sub.get(getPredicateVariable()).asType()); //determine compatible types from played roles Set<Type> typesFromRoles = getParentQuery().getAtoms(RelationshipAtom.class) .filter(r -> r.getVarNames().contains(getVarName())) .flatMap(r -> r.getRoleVarMap().entries().stream() .filter(e -> e.getValue().equals(getVarName())) .map(Map.Entry::getKey)) .map(role -> role.players().collect(Collectors.toSet())) .reduce(Sets::intersection) .orElse(Sets.newHashSet()); Set<Type> typesFromTypes = getParentQuery().getAtoms(IsaAtom.class) .filter(at -> at.getVarNames().contains(getVarName())) .filter(at -> at != this) .map(Binary::getSchemaConcept) .filter(Objects::nonNull) .filter(Concept::isType) .map(Concept::asType) .collect(Collectors.toSet()); Set<Type> types = typesFromTypes.isEmpty()? typesFromRoles : typesFromRoles.isEmpty()? typesFromTypes: Sets.intersection(typesFromRoles, typesFromTypes); return !types.isEmpty()? ImmutableList.copyOf(ReasonerUtils.top(types)) : tx().admin().getMetaConcept().subs().collect(toImmutableList()); }