Search in sources :

Example 1 with TreeNode

use of org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.TreeNode in project legend-pure by finos.

the class RelationalOperationElementProcessor method processJoinTreeNode.

static void processJoinTreeNode(JoinTreeNode joinTreeNode, CoreInstance implementation, Matcher matcher, ProcessorState state, ModelRepository repository, ProcessorSupport processorSupport) {
    String joinName = joinTreeNode._joinName();
    Database database = (Database) ImportStub.withImportStubByPass(joinTreeNode._databaseCoreInstance(), processorSupport);
    if (database == null) {
        throw new PureCompilationException(joinTreeNode.getSourceInformation(), "The system can't figure out which database to use.");
    }
    PostProcessor.processElement(matcher, database, state, processorSupport);
    CoreInstance joinType = joinTreeNode._joinTypeCoreInstance();
    if (joinType != null) {
        StringCoreInstance joinTypeString = (StringCoreInstance) joinType;
        String type = "INNER".equals(joinTypeString.getValue()) ? "INNER" : "LEFT_OUTER";
        Enumeration joinTypeEnumeration = (Enumeration) processorSupport.package_getByUserPath(M2RelationalPaths.JoinType);
        Enum joinTypeEnumInstance = (Enum) org.finos.legend.pure.m3.navigation.enumeration.Enumeration.findEnum(joinTypeEnumeration, type);
        if (joinTypeEnumInstance == null) {
            throw new PureCompilationException(joinTreeNode.getSourceInformation(), "The enum value '" + type + "' can't be found in the enumeration " + PackageableElement.getUserPathForPackageableElement(joinTypeEnumeration, "::"));
        }
        joinTreeNode._joinTypeCoreInstance(joinTypeEnumInstance);
    }
    Join join = findJoin(joinTreeNode, database, joinName, processorSupport);
    joinTreeNode._join(join);
    PropertyMappingsImplementation owner = joinTreeNode._setMappingOwner();
    if (owner == null && implementation != null) {
        joinTreeNode._setMappingOwner((PropertyMappingsImplementation) implementation);
    }
    for (TreeNode child : joinTreeNode._childrenData()) {
        processJoinTreeNode((JoinTreeNode) child, implementation, matcher, state, repository, processorSupport);
    }
}
Also used : Enum(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Enum) Enumeration(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Enumeration) TreeNode(org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.TreeNode) JoinTreeNode(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.join.JoinTreeNode) Database(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.Database) StringCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.StringCoreInstance) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) Join(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.join.Join) RelationalOperationElementWithJoin(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.RelationalOperationElementWithJoin) StringCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.StringCoreInstance) PropertyMappingsImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.PropertyMappingsImplementation) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Example 2 with TreeNode

use of org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.TreeNode in project legend-pure by finos.

the class Copy method execute.

@Override
public CoreInstance execute(ListIterable<? extends CoreInstance> params, Stack<MutableMap<String, CoreInstance>> resolvedTypeParameters, Stack<MutableMap<String, CoreInstance>> resolvedMultiplicityParameters, VariableContext variableContext, final CoreInstance functionExpressionToUseInStack, Profiler profiler, final InstantiationContext instantiationContext, final ExecutionSupport executionSupport, final Context context, final ProcessorSupport processorSupport) throws PureExecutionException {
    final CoreInstance instance = Instance.getValueForMetaPropertyToOneResolved(params.get(0), M3Properties.values, processorSupport);
    CoreInstance classifierGenericType = Instance.extractGenericTypeFromInstance(instance, processorSupport);
    final CoreInstance sourceClassifier = Instance.getValueForMetaPropertyToOneResolved(classifierGenericType, M3Properties.rawType, processorSupport);
    instantiationContext.push(sourceClassifier);
    // TODO should we start a repository transaction here?
    final CoreInstance newInstance = this.repository.newEphemeralAnonymousCoreInstance(null, sourceClassifier);
    ListIterable<? extends CoreInstance> keyValues = (params.size() > 2) ? Instance.getValueForMetaPropertyToManyResolved(params.get(2), M3Properties.values, processorSupport) : Lists.immutable.<CoreInstance>with();
    MutableSet<CoreInstance> addedValues = UnifiedSet.newSet();
    MutableMap<CoreInstance, MutableMap> propertyTree = Maps.mutable.empty();
    // Add the modified properties
    VariableContext evaluationVariableContext = getParentOrEmptyVariableContext(variableContext);
    for (CoreInstance keyValue : keyValues) {
        CoreInstance classifier = sourceClassifier;
        // Find Property and newInstanceCurrent
        ListIterable<? extends CoreInstance> keys = Instance.getValueForMetaPropertyToManyResolved(Instance.getValueForMetaPropertyToOneResolved(keyValue, M3Properties.key, processorSupport), M3Properties.values, processorSupport);
        String finalPropertyName = keys.getLast().getName();
        CoreInstance property = null;
        CoreInstance newInstanceCurrent = newInstance;
        CoreInstance instanceCurrent = instance;
        int size = keys.size() - 1;
        MutableMap<CoreInstance, MutableMap> treeNode = propertyTree;
        for (CoreInstance key : keys) {
            property = processorSupport.class_findPropertyUsingGeneralization(classifier, key.getName());
            if (property == null) {
                throw new PureExecutionException(Instance.getValueForMetaPropertyToOneResolved(keyValue, M3Properties.key, processorSupport).getSourceInformation(), "The property '" + key.getName() + "' can't be found in the type '" + classifier.getName() + "' or in its hierarchy.");
            }
            treeNode = treeNode.getIfAbsentPut(property, Maps.mutable.empty());
            classifier = Instance.getValueForMetaPropertyToOneResolved(property, M3Properties.genericType, M3Properties.rawType, processorSupport);
            if (size > 0) {
                if (Multiplicity.isToOne(Instance.getValueForMetaPropertyToOneResolved(property, M3Properties.multiplicity, processorSupport), false)) {
                    instanceCurrent = Instance.getValueForMetaPropertyToOneResolved(instanceCurrent, property, processorSupport);
                    CoreInstance res = Instance.getValueForMetaPropertyToOneResolved(newInstanceCurrent, property, processorSupport);
                    if (res == null) {
                        res = this.repository.newEphemeralAnonymousCoreInstance(null, classifier);
                        Instance.addValueToProperty(newInstanceCurrent, key.getName(), res, processorSupport);
                    }
                    newInstanceCurrent = res;
                } else {
                    throw new RuntimeException("Not supported yet!");
                }
            }
            size--;
        }
        // Maybe add the existing ones
        CoreInstance addValue = Instance.getValueForMetaPropertyToOneResolved(keyValue, M3Properties.add, processorSupport);
        boolean add = (addValue != null) && PrimitiveUtilities.getBooleanValue(addValue);
        if (add) {
            ListIterable<? extends CoreInstance> existingOnes = Instance.getValueForMetaPropertyToManyResolved(instanceCurrent, property, processorSupport);
            for (CoreInstance existing : existingOnes) {
                Instance.addValueToProperty(newInstanceCurrent, finalPropertyName, existing, processorSupport);
                addedValues.add(existing);
            }
        }
        // Add the requested ones
        CoreInstance propertyGenericType = GenericType.resolvePropertyReturnType(Instance.extractGenericTypeFromInstance(instanceCurrent, processorSupport), property, processorSupport);
        CoreInstance expression = Instance.getValueForMetaPropertyToOneResolved(keyValue, M3Properties.expression, processorSupport);
        Executor executor = FunctionExecutionInterpreted.findValueSpecificationExecutor(expression, functionExpressionToUseInStack, processorSupport, this.functionExecution);
        CoreInstance instanceValResult = executor.execute(expression, resolvedTypeParameters, resolvedMultiplicityParameters, functionExpressionToUseInStack, evaluationVariableContext, profiler, instantiationContext, executionSupport, this.functionExecution, processorSupport);
        ListIterable<? extends CoreInstance> values = Instance.getValueForMetaPropertyToManyResolved(instanceValResult, M3Properties.values, processorSupport);
        New.validateRangeUsingMultiplicity(instance, keyValue, property, values, processorSupport);
        if (values.isEmpty()) {
            if (newInstanceCurrent.isValueDefinedForKey(finalPropertyName)) {
                if (!add) {
                    Instance.removeProperty(newInstanceCurrent, finalPropertyName, processorSupport);
                    Instance.addPropertyWithEmptyList(newInstanceCurrent, finalPropertyName, processorSupport);
                }
            }
        } else {
            New.validateTypeFromGenericType(propertyGenericType, Instance.getValueForMetaPropertyToOneResolved(instanceValResult, M3Properties.genericType, processorSupport), expression, processorSupport);
            for (CoreInstance value : values) {
                Instance.addValueToProperty(newInstanceCurrent, finalPropertyName, value, processorSupport);
                addedValues.add(value);
            }
        }
    }
    this.copy(instance, newInstance, sourceClassifier, addedValues, functionExpressionToUseInStack.getSourceInformation(), processorSupport, instantiationContext, propertyTree);
    if (addedValues.isEmpty()) {
        newInstance.setSourceInformation(instance.getSourceInformation());
    } else {
        newInstance.setSourceInformation(functionExpressionToUseInStack.getSourceInformation());
    }
    CoreInstance value = ValueSpecificationBootstrap.wrapValueSpecification(newInstance, ValueSpecification.isExecutable(params.get(0), processorSupport), processorSupport);
    instantiationContext.popAndExecuteProcedures(value);
    if (instantiationContext.isEmpty()) {
        instantiationContext.runValidations();
        instantiationContext.reset();
    }
    return DefaultConstraintHandler.handleConstraints(sourceClassifier, value, functionExpressionToUseInStack.getSourceInformation(), this.functionExecution, resolvedTypeParameters, resolvedMultiplicityParameters, variableContext, functionExpressionToUseInStack, profiler, instantiationContext, executionSupport);
}
Also used : Executor(org.finos.legend.pure.runtime.java.interpreted.Executor) PureExecutionException(org.finos.legend.pure.m3.exception.PureExecutionException) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) MutableMap(org.eclipse.collections.api.map.MutableMap) VariableContext(org.finos.legend.pure.runtime.java.interpreted.VariableContext)

Example 3 with TreeNode

use of org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.TreeNode in project legend-pure by finos.

the class RelationalOperationElementProcessor method processAliasForJoinTreeNode.

static void processAliasForJoinTreeNode(JoinTreeNode joinTreeNode, RelationalOperationElement startTable, ProcessorSupport processorSupport) {
    TableAlias alias = findAliasForJoinTreeNode(joinTreeNode, startTable, processorSupport);
    TableAlias aliasCopy = org.finos.legend.pure.m2.relational.TableAlias.copyTableAlias(alias, null, processorSupport);
    PropertyMappingsImplementation setMappingOwner = joinTreeNode._setMappingOwner();
    if (setMappingOwner != null) {
        aliasCopy._setMappingOwner(setMappingOwner);
    }
    joinTreeNode._alias(aliasCopy);
    RichIterable<? extends TreeNode> children = joinTreeNode._childrenData();
    if (children.notEmpty()) {
        RelationalOperationElement newStartTable = alias._relationalElement();
        for (TreeNode child : children) {
            processAliasForJoinTreeNode((JoinTreeNode) child, newStartTable, processorSupport);
        }
    }
}
Also used : RelationalOperationElement(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.RelationalOperationElement) TreeNode(org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.TreeNode) JoinTreeNode(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.join.JoinTreeNode) TableAlias(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.TableAlias) PropertyMappingsImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.PropertyMappingsImplementation)

Example 4 with TreeNode

use of org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.TreeNode in project legend-pure by finos.

the class Pure method replaceTreeNodeCopy.

public static void replaceTreeNodeCopy(TreeNode instance, TreeNode result, TreeNode targetNode, TreeNode subTree) {
    result._childrenData(FastList.newList());
    for (TreeNode child : instance._childrenData()) {
        if (child == targetNode) {
            result._childrenDataAdd(subTree);
        } else {
            TreeNode newCopy = child.copy();
            replaceTreeNodeCopy(child, newCopy, targetNode, subTree);
            result._childrenDataAdd(newCopy);
        }
    }
}
Also used : TreeNode(org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.TreeNode)

Example 5 with TreeNode

use of org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.TreeNode in project legend-engine by finos.

the class HelperRelationalBuilder method getAllJoins.

private static ListIterable<Join> getAllJoins(JoinTreeNode joinTreeNode) {
    Join join = joinTreeNode._join();
    RichIterable<? extends TreeNode> children = joinTreeNode._childrenData();
    RichIterable<Join> childrenJoins = children.flatCollect(TREE_NODE_TO_JOIN_FUNCTION);
    return FastList.<Join>newList(childrenJoins.size() + 1).with(join).withAll(childrenJoins);
}
Also used : Join(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.join.Join) RelationalOperationElementWithJoin(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.RelationalOperationElementWithJoin)

Aggregations

TreeNode (org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.TreeNode)7 JoinTreeNode (org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.join.JoinTreeNode)7 Join (org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.join.Join)4 RelationalOperationElement (org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.RelationalOperationElement)3 RelationalOperationElementWithJoin (org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.RelationalOperationElementWithJoin)3 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)3 PropertyMappingsImplementation (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.PropertyMappingsImplementation)2 RouteNodePropertyStub (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.treepath.RouteNodePropertyStub)2 Enum (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Enum)2 GenericType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType)2 Database (org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.Database)2 StringCoreInstance (org.finos.legend.pure.m4.coreinstance.primitive.StringCoreInstance)2 PureCompilationException (org.finos.legend.pure.m4.exception.PureCompilationException)2 MutableMap (org.eclipse.collections.api.map.MutableMap)1 PropertyStub (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.PropertyStub)1 ExistingPropertyRouteNode (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.treepath.ExistingPropertyRouteNode)1 NewPropertyRouteNode (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.treepath.NewPropertyRouteNode)1 PropertyRouteNode (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.treepath.PropertyRouteNode)1 Enumeration (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Enumeration)1 ValueSpecification (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification)1