use of org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method unitInstanceParser.
/**
* Parse the instantiation of an instance of UNIT.
*/
private InstanceValueInstance unitInstanceParser(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.UnitInstanceContext ctx, ImportGroup importId) {
org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.UnitInstanceLiteralContext uctx = ctx.unitInstanceLiteral();
GenericTypeInstance genericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
CoreInstance typeImportStub = ImportStubInstance.createPersistent(this.repository, this.sourceInformation.getPureSourceInformation(ctx.unitName().identifier().getStart()), this.getUnitNameWithMeasure(ctx.unitName()), importId);
genericTypeInstance._rawTypeCoreInstance(typeImportStub);
InstanceValueInstance iv = InstanceValueInstance.createPersistent(this.repository, genericTypeInstance, this.getPureOne());
PrimitiveCoreInstance<?> result;
if (uctx.MINUS() != null) {
if (uctx.INTEGER() != null) {
result = this.repository.newIntegerCoreInstance("-" + uctx.INTEGER().getText());
} else if (uctx.DECIMAL() != null) {
result = this.repository.newDecimalCoreInstance("-" + uctx.DECIMAL().getText());
} else {
result = this.repository.newFloatCoreInstance("-" + uctx.FLOAT().getText());
}
} else {
if (uctx.INTEGER() != null) {
result = this.repository.newIntegerCoreInstance("+" + uctx.INTEGER().getText());
} else if (uctx.DECIMAL() != null) {
result = this.repository.newDecimalCoreInstance("+" + uctx.DECIMAL().getText());
} else {
result = this.repository.newFloatCoreInstance("+" + uctx.FLOAT().getText());
}
}
iv._genericType(genericTypeInstance);
iv._multiplicity(this.getPureOne());
iv._values(Lists.mutable.with(result.getValue()));
InstanceValueInstance wrapperIv = InstanceValueInstance.createPersistent(this.repository, genericTypeInstance, this.getPureOne());
wrapperIv._genericType(genericTypeInstance);
wrapperIv._multiplicity(this.getPureOne());
wrapperIv._values(Lists.mutable.with(iv));
return wrapperIv;
}
use of org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance 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);
}
use of org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance in project legend-pure by finos.
the class CompiledProcessorSupport method getClassifier.
@Override
public CoreInstance getClassifier(CoreInstance instance) {
if (instance instanceof ValCoreInstance) {
return this.metadataAccessor.getPrimitiveType(((ValCoreInstance) instance).getType());
}
// todo: clean this up, seem to have interpreted style core instances in compiled
if (instance instanceof PrimitiveCoreInstance || instance instanceof BaseCoreInstance) {
return instance.getClassifier();
}
if (instance instanceof Any) {
Any any = (Any) instance;
GenericType genericType = any._classifierGenericType();
if (genericType != null) {
org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type type = genericType._rawType();
if (type != null) {
return type;
}
}
return CompiledSupport.getType(any, this.metadataAccessor);
}
throw new PureExecutionException("ERROR unhandled type for value: " + instance + " (instance of " + instance.getClass() + ")");
}
use of org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance in project legend-pure by finos.
the class JsonUnitSerialization method apply.
@Override
public Object apply(T pureObject, ConversionContext context) {
CoreInstance processedPureObject = pureObject;
Object value;
String typeString;
ProcessorSupport processorSupport = context.getProcessorSupport();
if (null == pureObject.getValueForMetaPropertyToOne("genericType")) {
processedPureObject = ValueSpecificationBootstrap.wrapValueSpecification(pureObject, false, context.getProcessorSupport());
value = Long.valueOf(processedPureObject.getValueForMetaPropertyToOne("values").getValueForMetaPropertyToOne("values").getName());
try {
typeString = ((String) pureObject.getClass().getMethod("getFullSystemPath").invoke(pureObject)).substring(6);
} catch (Exception e) {
throw new PureExecutionException("Cannot find full path name for the designated unit type.");
}
} else {
CoreInstance numericInArray = processedPureObject.getValueForMetaPropertyToOne("values");
value = numericInArray instanceof PrimitiveCoreInstance ? ((PrimitiveCoreInstance) numericInArray).getValue() : ((PrimitiveCoreInstance) numericInArray.getValueForMetaPropertyToOne("values")).getValue();
typeString = PackageableElement.getUserPathForPackageableElement(Instance.getValueForMetaPropertyToOneResolved(processedPureObject, M3Properties.genericType, M3Properties.rawType, processorSupport));
}
Map<String, Object> unitMap = UnifiedMap.newMap();
unitMap.put("unitId", typeString);
unitMap.put("exponentValue", 1);
List<Object> unitList = FastList.newList();
unitList.add(unitMap);
Map<String, Object> resultMap = UnifiedMap.newMap();
resultMap.put(this.unitKeyName, unitList);
resultMap.put(this.valueKeyName, value);
return resultMap;
}
use of org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance in project legend-pure by finos.
the class EqualityUtilities method equal.
/**
* Return whether two instances are equal according to
* the Pure function "equal".
*
* @param left left instance
* @param right right instance
* @param processorSupport processor support
* @return whether left and right are equal in Pure
*/
public static // NOSONAR signature is clear enough
boolean equal(// NOSONAR signature is clear enough
CoreInstance left, // NOSONAR signature is clear enough
CoreInstance right, // NOSONAR signature is clear enough
RichIterable<? extends CoreInstance> external, // NOSONAR signature is clear enough
ProcessorSupport processorSupport) {
if (left == right) {
return true;
}
if ((left instanceof PrimitiveCoreInstance<?>) && (right instanceof PrimitiveCoreInstance<?>)) {
return left.getClassifier().getName().equals(right.getClassifier().getName()) && ((PrimitiveCoreInstance<?>) left).getValue().equals(((PrimitiveCoreInstance<?>) right).getValue());
}
CoreInstance type = left.getClassifier();
if (type != right.getClassifier()) {
return false;
}
if (Type.isPrimitiveType(type, processorSupport)) {
return left.getName().equals(right.getName());
}
if (Instance.instanceOf(left, M3Paths.InstanceValue, processorSupport)) {
return equal(ValueSpecification.getValues(left, processorSupport), ValueSpecification.getValues(right, processorSupport), processorSupport);
}
ListIterable<CoreInstance> keys = _Class.getEqualityKeyProperties(type, processorSupport);
if (external.isEmpty() && keys.isEmpty()) {
return false;
}
for (CoreInstance key : external.notEmpty() ? external : keys) {
if (!equal(Instance.getValueForMetaPropertyToManyResolved(left, key, processorSupport), Instance.getValueForMetaPropertyToManyResolved(right, key, processorSupport), processorSupport)) {
return false;
}
}
return true;
}
Aggregations