Search in sources :

Example 1 with FloatCoreInstance

use of org.finos.legend.pure.m4.coreinstance.primitive.FloatCoreInstance 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 FloatCoreInstance

use of org.finos.legend.pure.m4.coreinstance.primitive.FloatCoreInstance in project legend-pure by finos.

the class RoundWithScale method execute.

@Override
public CoreInstance execute(ListIterable<? extends CoreInstance> params, Stack<MutableMap<String, CoreInstance>> resolvedTypeParameters, Stack<MutableMap<String, CoreInstance>> resolvedMultiplicityParameters, VariableContext variableContext, CoreInstance functionExpressionToUseInStack, Profiler profiler, InstantiationContext instantiationContext, ExecutionSupport executionSupport, Context context, ProcessorSupport processorSupport) throws PureExecutionException {
    CoreInstance number = Instance.getValueForMetaPropertyToOneResolved(params.get(0), M3Properties.values, processorSupport);
    CoreInstance scale = Instance.getValueForMetaPropertyToOneResolved(params.get(1), M3Properties.values, processorSupport);
    BigDecimal decimal = number instanceof DecimalCoreInstance ? ((DecimalCoreInstance) number).getValue() : ((FloatCoreInstance) number).getValue();
    BigDecimal result = decimal.setScale(((IntegerCoreInstance) scale).getValue().intValue(), RoundingMode.HALF_UP);
    return NumericUtilities.toPureNumberValueExpression(result, number instanceof DecimalCoreInstance, this.repository, processorSupport);
}
Also used : DecimalCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.DecimalCoreInstance) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) FloatCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.FloatCoreInstance) IntegerCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.IntegerCoreInstance) IntegerCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.IntegerCoreInstance) DecimalCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.DecimalCoreInstance) BigDecimal(java.math.BigDecimal)

Aggregations

BigDecimal (java.math.BigDecimal)2 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)2 FloatCoreInstance (org.finos.legend.pure.m4.coreinstance.primitive.FloatCoreInstance)2 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 DecimalCoreInstance (org.finos.legend.pure.m4.coreinstance.primitive.DecimalCoreInstance)1 IntegerCoreInstance (org.finos.legend.pure.m4.coreinstance.primitive.IntegerCoreInstance)1 PrimitiveCoreInstance (org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance)1 SimpleCoreInstance (org.finos.legend.pure.m4.coreinstance.simple.SimpleCoreInstance)1 JsonSerializationCache (org.finos.legend.pure.runtime.java.extension.external.json.shared.JsonSerializationCache)1