Search in sources :

Example 1 with SimpleCoreInstance

use of org.finos.legend.pure.m4.coreinstance.simple.SimpleCoreInstance in project legend-pure by finos.

the class ToJson method execute.

@Override
public CoreInstance execute(ListIterable<? extends CoreInstance> params, final Stack<MutableMap<String, CoreInstance>> resolvedTypeParameters, final Stack<MutableMap<String, CoreInstance>> resolvedMultiplicityParameters, final VariableContext variableContext, final CoreInstance functionExpressionToUseInStack, final Profiler profiler, final InstantiationContext instantiationContext, final ExecutionSupport executionSupport, Context context, final ProcessorSupport processorSupport) throws PureExecutionException {
    ListIterable<? extends CoreInstance> pureObjects = Instance.getValueForMetaPropertyToManyResolved(params.get(0), M3Properties.values, processorSupport);
    if (pureObjects.getFirst() instanceof SimpleCoreInstance && !((SimpleCoreInstance) pureObjects.getFirst()).getClassifier().getName().equals("Map")) {
        pureObjects = pureObjects.collect(SIMPLE_CORE_INSTANCE_TO_ANY);
    }
    CoreInstance jsonConfig = Instance.getValueForMetaPropertyToOneResolved(params.get(1), M3Properties.values, processorSupport);
    String typeKeyName = PrimitiveUtilities.getStringValue(jsonConfig.getValueForMetaPropertyToOne("typeKeyName"));
    boolean includeType = PrimitiveUtilities.getBooleanValue(jsonConfig.getValueForMetaPropertyToOne("includeType"), false);
    boolean fullyQualifiedTypePath = PrimitiveUtilities.getBooleanValue(jsonConfig.getValueForMetaPropertyToOne("fullyQualifiedTypePath"), false);
    boolean serializeQualifiedProperties = PrimitiveUtilities.getBooleanValue(jsonConfig.getValueForMetaPropertyToOne("serializeQualifiedProperties"), false);
    String dateTimeFormat = PrimitiveUtilities.getStringValue(jsonConfig.getValueForMetaPropertyToOne("dateTimeFormat"), null);
    boolean serializePackageableElementName = PrimitiveUtilities.getBooleanValue(jsonConfig.getValueForMetaPropertyToOne("serializePackageableElementName"), false);
    boolean removePropertiesWithEmptyValues = PrimitiveUtilities.getBooleanValue(jsonConfig.getValueForMetaPropertyToOne("removePropertiesWithEmptyValues"), false);
    boolean serializeMultiplicityAsNumber = PrimitiveUtilities.getBooleanValue(jsonConfig.getValueForMetaPropertyToOne("serializeMultiplicityAsNumber"), false);
    String encryptionKey = PrimitiveUtilities.getStringValue(jsonConfig.getValueForMetaPropertyToOne("encryptionKey"), null);
    String decryptionKey = PrimitiveUtilities.getStringValue(jsonConfig.getValueForMetaPropertyToOne("decryptionKey"), null);
    ListIterable<? extends CoreInstance> encryptionStereotypes = jsonConfig.getValueForMetaPropertyToMany("encryptionStereotypes");
    ListIterable<? extends CoreInstance> decryptionStereotypes = jsonConfig.getValueForMetaPropertyToMany("decryptionStereotypes");
    String json = JsonSerializer.toJson((ListIterable<? extends Any>) pureObjects, processorSupport, new JsonSerializationContext<CoreInstance, CoreInstance>(new JsonSerializationCache(), functionExpressionToUseInStack.getSourceInformation(), processorSupport, new Stack(), typeKeyName, includeType, fullyQualifiedTypePath, serializeQualifiedProperties, dateTimeFormat, serializePackageableElementName, removePropertiesWithEmptyValues, serializeMultiplicityAsNumber, encryptionKey, encryptionStereotypes, decryptionKey, decryptionStereotypes) {

        @Override
        protected Object extractPrimitiveValue(Object potentiallyWrappedPrimitive) {
            Object val = potentiallyWrappedPrimitive instanceof PrimitiveCoreInstance ? ((PrimitiveCoreInstance) potentiallyWrappedPrimitive).getValue() : potentiallyWrappedPrimitive;
            val = potentiallyWrappedPrimitive instanceof FloatCoreInstance && val instanceof BigDecimal ? ((BigDecimal) val).doubleValue() : val;
            return val;
        }

        @Override
        protected Object getValueForProperty(CoreInstance pureObject, Property property, String className) {
            CoreInstance res = ToJson.this.functionExecution.executeProperty(property, true, resolvedTypeParameters, resolvedMultiplicityParameters, getParentOrEmptyVariableContext(variableContext), profiler, Lists.immutable.with(ValueSpecificationBootstrap.wrapValueSpecification(pureObject, true, processorSupport)), functionExpressionToUseInStack, instantiationContext, executionSupport);
            if (res instanceof InstanceValue) {
                RichIterable values = ((InstanceValue) res)._values();
                return (values.getFirst() instanceof SimpleCoreInstance && !((SimpleCoreInstance) values.getFirst()).getClassifier().getName().equals("Map")) ? values.collect(SIMPLE_CORE_INSTANCE_TO_ANY) : values;
            }
            return res;
        }

        @Override
        protected Object evaluateQualifiedProperty(CoreInstance pureObject, QualifiedProperty qualifiedProperty, Type type, Multiplicity multiplicity, String propertyName) {
            Stack empty = new Stack();
            CoreInstance res = ToJson.this.functionExecution.executeFunction(false, qualifiedProperty, Lists.immutable.with(ValueSpecificationBootstrap.wrapValueSpecification(pureObject, true, processorSupport)), empty, empty, getParentOrEmptyVariableContextForLambda(variableContext, qualifiedProperty), functionExpressionToUseInStack, profiler, instantiationContext, executionSupport);
            if (res instanceof InstanceValue) {
                return ((InstanceValue) res)._values();
            }
            return res;
        }

        @Override
        protected CoreInstance getClassifier(CoreInstance pureObject) {
            return pureObject.getClassifier();
        }

        @Override
        protected RichIterable<CoreInstance> getMapKeyValues(CoreInstance pureObject) {
            ListIterable<CoreInstance> newPureObjects = Lists.immutable.with(ValueSpecificationBootstrap.wrapValueSpecification(pureObject, true, processorSupport));
            return KeyValues.executeMap(repository, newPureObjects, processorSupport);
        }
    }, functionExpressionToUseInStack.getSourceInformation());
    return ValueSpecificationBootstrap.newStringLiteral(this.repository, json, processorSupport);
}
Also used : PrimitiveCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance) FloatCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.FloatCoreInstance) SimpleCoreInstance(org.finos.legend.pure.m4.coreinstance.simple.SimpleCoreInstance) InstanceValue(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.InstanceValue) BigDecimal(java.math.BigDecimal) Stack(java.util.Stack) RichIterable(org.eclipse.collections.api.RichIterable) ListIterable(org.eclipse.collections.api.list.ListIterable) Type(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type) Multiplicity(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) FloatCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.FloatCoreInstance) SimpleCoreInstance(org.finos.legend.pure.m4.coreinstance.simple.SimpleCoreInstance) PrimitiveCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance) JsonSerializationCache(org.finos.legend.pure.runtime.java.extension.external.json.shared.JsonSerializationCache) QualifiedProperty(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty) Property(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property) QualifiedProperty(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty)

Example 2 with SimpleCoreInstance

use of org.finos.legend.pure.m4.coreinstance.simple.SimpleCoreInstance in project legend-pure by finos.

the class M4GraphBuilder method visitInstanceBlock.

private CoreInstance visitInstanceBlock(M4AntlrParser.InstanceContext ctx) {
    String name = ctx.name().VALID_STRING().getText();
    CoreInstance classifierOwner = this.repository.getOrCreateTopLevel(name, null);
    if (ctx.classifierOwner() != null) {
        for (M4AntlrParser.ClassifierOwnerContext coc : ctx.classifierOwner()) {
            classifierOwner = ((SimpleCoreInstance) classifierOwner).getOrCreateUnknownTypeNode(coc.key().VALID_STRING().getText(), coc.keyInArray() != null ? coc.keyInArray().VALID_STRING().getText() : null, this.repository);
        }
    }
    return classifierOwner;
}
Also used : SimpleCoreInstance(org.finos.legend.pure.m4.coreinstance.simple.SimpleCoreInstance) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance)

Example 3 with SimpleCoreInstance

use of org.finos.legend.pure.m4.coreinstance.simple.SimpleCoreInstance in project legend-pure by finos.

the class SimpleCoreInstance method printProperty.

private void printProperty(SafeAppendable appendable, String propertyName, ListIterable<CoreInstance> values, String tab, MutableStack<CoreInstance> stack, boolean full, boolean addDebug, int max) {
    appendable.append(tab).append("    ");
    if (propertyName == null) {
        appendable.append("null:");
    } else {
        CoreInstance property = getKeyByName(propertyName);
        CoreInstance propertyClassifier = property.getClassifier();
        printNodeName(appendable, property, full, addDebug);
        appendable.append('(');
        if (propertyClassifier == null) {
            appendable.append("null");
        } else {
            printNodeName(appendable, propertyClassifier, full, addDebug);
        }
        appendable.append("):");
    }
    SetIterable<CoreInstance> excluded = this.repository.getExclusionSet();
    for (CoreInstance value : values) {
        appendable.append('\n');
        if (value == null) {
            appendable.append(tab);
            appendable.append("        NULL");
        } else {
            // TODO remove reference to "package" property, which is an M3 thing
            boolean excludeFlag = ((excluded != null) && (excluded.contains(value.getValueForMetaPropertyToOne("package")) || excluded.contains(value))) || (this.repository.getTopLevel(value.getName()) != null);
            if (full ? stack.contains(value) : (excludeFlag || (stack.size() > max) || stack.contains(value))) {
                CoreInstance valueClassifier = value.getClassifier();
                appendable.append(tab).append("        ");
                printNodeName(appendable, value, full, addDebug);
                appendable.append(" instance ");
                if (valueClassifier == null) {
                    appendable.append("null");
                } else {
                    printNodeName(appendable, valueClassifier, full, addDebug);
                    if (full) {
                        appendable.append('\n').append(tab).append("            [...]");
                    } else if (!excludeFlag && (stack.size() > max) && value.getKeys().notEmpty()) {
                        appendable.append('\n').append(tab).append("            [... >").append(max).append(']');
                    }
                }
            } else {
                String newTab = tab + "        ";
                if (value instanceof SimpleCoreInstance) {
                    ((SimpleCoreInstance) value).print(appendable, newTab, stack, full, addDebug, max);
                } else if (full) {
                    value.printFull(appendable, newTab);
                } else if (addDebug) {
                    value.print(appendable, newTab, max);
                } else {
                    value.printWithoutDebug(appendable, newTab, max);
                }
            }
        }
    }
}
Also used : CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) AbstractCoreInstance(org.finos.legend.pure.m4.coreinstance.AbstractCoreInstance)

Example 4 with SimpleCoreInstance

use of org.finos.legend.pure.m4.coreinstance.simple.SimpleCoreInstance in project legend-pure by finos.

the class M4GraphBuilder method visitNameSpaceBlock.

private NameSpace visitNameSpaceBlock(M4AntlrParser.NameSpaceContext ctx) {
    String key = null;
    CoreInstance classifierOwner = this.repository.getOrCreateTopLevel(ctx.name().VALID_STRING().getText(), null);
    if (ctx.classifierOwner() != null) {
        for (M4AntlrParser.ClassifierOwnerContext coc : ctx.classifierOwner()) {
            key = coc.key().VALID_STRING().getText();
            if (coc.keyInArray() != null) {
                classifierOwner = ((SimpleCoreInstance) classifierOwner).getOrCreateUnknownTypeNode(key, coc.keyInArray().VALID_STRING().getText(), this.repository);
            }
        }
    }
    return new NameSpace(classifierOwner, key);
}
Also used : SimpleCoreInstance(org.finos.legend.pure.m4.coreinstance.simple.SimpleCoreInstance) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance)

Example 5 with SimpleCoreInstance

use of org.finos.legend.pure.m4.coreinstance.simple.SimpleCoreInstance in project legend-pure by finos.

the class SimpleCoreInstance method validate.

private void validate(MutableSet<CoreInstance> doneList, MutableStack<SimpleCoreInstance> stack) throws PureCompilationException {
    while (!stack.isEmpty()) {
        SimpleCoreInstance element = stack.pop();
        doneList.add(element);
        if (element.getClassifier() == null) {
            SourceInformation foundSourceInformation = element.sourceInformation;
            if (stack.size() > 1) {
                SourceInformation cursorSourceInformation = element.sourceInformation;
                int cursor = stack.size() - 1;
                while (cursorSourceInformation == null && cursor >= 0) {
                    cursorSourceInformation = stack.peekAt(cursor--).sourceInformation;
                }
                foundSourceInformation = stack.peekAt(cursor + 1).sourceInformation;
            }
            throw new PureCompilationException(foundSourceInformation, element.getName() + " has not been defined!");
        }
        try {
            element.getState().getKeys().forEach(keyName -> {
                ImmutableList<String> realKey = element.getState().getRealKeyByName(keyName);
                if (realKey == null) {
                    throw new RuntimeException("No real key can be found for '" + keyName + "' in\n" + element.getName() + " (" + element + ")");
                }
                CoreInstance key = element.getKeyByName(keyName);
                if (key.getClassifier() == null) {
                    throw new RuntimeException("'" + key.getName() + "' used in '" + element.name + "' has not been defined!\n" + element.print("   "));
                }
                ListIterable<CoreInstance> values = element.getState().getValues(keyName);
                if (values != null) {
                    values.forEach(childElement -> {
                        if ((!doneList.contains(childElement) || (childElement.getClassifier() == null)) && (childElement instanceof SimpleCoreInstance)) {
                            stack.push((SimpleCoreInstance) childElement);
                        }
                    });
                }
            });
        } catch (Exception e) {
            PureException pe = PureException.findPureException(e);
            if (pe != null) {
                throw pe;
            }
            throw e;
        }
    }
}
Also used : PureException(org.finos.legend.pure.m4.exception.PureException) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) AbstractCoreInstance(org.finos.legend.pure.m4.coreinstance.AbstractCoreInstance) SourceInformation(org.finos.legend.pure.m4.coreinstance.SourceInformation) IDConflictException(org.finos.legend.pure.m4.coreinstance.indexing.IDConflictException) PureException(org.finos.legend.pure.m4.exception.PureException) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Aggregations

CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)6 SimpleCoreInstance (org.finos.legend.pure.m4.coreinstance.simple.SimpleCoreInstance)4 AbstractCoreInstance (org.finos.legend.pure.m4.coreinstance.AbstractCoreInstance)2 FloatCoreInstance (org.finos.legend.pure.m4.coreinstance.primitive.FloatCoreInstance)2 PrimitiveCoreInstance (org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance)2 BigDecimal (java.math.BigDecimal)1 Stack (java.util.Stack)1 RichIterable (org.eclipse.collections.api.RichIterable)1 ListIterable (org.eclipse.collections.api.list.ListIterable)1 Property (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property)1 QualifiedProperty (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty)1 Multiplicity (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity)1 Type (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type)1 InstanceValue (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.InstanceValue)1 SourceInformation (org.finos.legend.pure.m4.coreinstance.SourceInformation)1 IDConflictException (org.finos.legend.pure.m4.coreinstance.indexing.IDConflictException)1 BinaryCoreInstance (org.finos.legend.pure.m4.coreinstance.primitive.BinaryCoreInstance)1 BooleanCoreInstance (org.finos.legend.pure.m4.coreinstance.primitive.BooleanCoreInstance)1 DateCoreInstance (org.finos.legend.pure.m4.coreinstance.primitive.DateCoreInstance)1 DecimalCoreInstance (org.finos.legend.pure.m4.coreinstance.primitive.DecimalCoreInstance)1