use of org.finos.legend.pure.m3.coreinstance.meta.pure.functions.lang.KeyExpression in project legend-pure by finos.
the class FunctionExpressionProcessor method addTraceForKeyExpressions.
private void addTraceForKeyExpressions(FunctionExpression functionExpression, ProcessorSupport processorSupport) {
ListIterable<? extends ValueSpecification> params = functionExpression._parametersValues().toList();
if (2 < params.size()) {
int z = 0;
for (CoreInstance keyValue : ImportStub.withImportStubByPasses(((InstanceValue) params.get(2))._valuesCoreInstance().toList(), processorSupport)) {
if (keyValue instanceof KeyExpression) {
KeyValueValueSpecificationContext usageContext = (KeyValueValueSpecificationContext) processorSupport.newAnonymousCoreInstance(null, M3Paths.KeyValueValueSpecificationContext);
usageContext._offset(z);
usageContext._functionExpression(functionExpression);
ValueSpecification expression = ((KeyExpression) keyValue)._expression();
if (null != expression._usageContext()) {
expression._usageContextRemove();
}
expression._usageContext(usageContext);
}
z++;
}
}
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.functions.lang.KeyExpression in project legend-pure by finos.
the class InstanceValueUnbind method run.
@Override
public void run(InstanceValue instanceValue, MatcherState state, Matcher matcher, ModelRepository modelRepository, Context context) throws PureCompilationException {
ProcessorSupport processorSupport = state.getProcessorSupport();
for (CoreInstance value : instanceValue._valuesCoreInstance()) {
if (!(value instanceof Property)) {
((UnbindState) state).freeProcessedAndValidated(value);
}
Shared.cleanUpReferenceUsage(value, instanceValue, processorSupport);
// TODO replace call to Instance.instanceOf once we resolve issue with Path not being available in this module - circular dependency
if (value instanceof LambdaFunction || value instanceof SimpleFunctionExpression || Instance.instanceOf(value, M3Paths.Path, processorSupport) || value instanceof RootRouteNode) {
matcher.fullMatch(value, state);
} else if (Instance.instanceOf(value, M3Paths.RootGraphFetchTree, processorSupport)) {
matcher.fullMatch(value, state);
} else if (value instanceof KeyExpression) {
((UnbindState) state).freeProcessedAndValidated(modelRepository.newBooleanCoreInstance(((KeyExpression) value)._add()));
((UnbindState) state).freeProcessedAndValidated(((KeyExpression) value)._key());
matcher.fullMatch(((KeyExpression) value)._expression(), state);
} else {
Shared.cleanImportStub(value, processorSupport);
}
if (value instanceof ValueSpecification) {
matcher.fullMatch(value, state);
((ValueSpecification) value)._usageContextRemove();
}
}
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.functions.lang.KeyExpression in project legend-pure by finos.
the class LambdaFunctionProcessor method processValue.
private static void processValue(CoreInstance valueSpecification, Consumer<String> declaredVarConsumer, Consumer<String> otherVarConsumer, ProcessorSupport processorSupport) {
if (valueSpecification instanceof FunctionExpression) {
FunctionExpression functionExpression = (FunctionExpression) valueSpecification;
if ("letFunction".equals(functionExpression._functionName())) {
InstanceValue firstParamValue = (InstanceValue) ListHelper.wrapListIterable(functionExpression._parametersValues()).get(0);
String letVarName = PrimitiveUtilities.getStringValue(ListHelper.wrapListIterable(firstParamValue._valuesCoreInstance()).get(0));
declaredVarConsumer.accept(letVarName);
}
functionExpression._parametersValues().forEach(p -> processValue(p, declaredVarConsumer, otherVarConsumer, processorSupport));
} else if (valueSpecification instanceof InstanceValue) {
((InstanceValue) valueSpecification)._valuesCoreInstance().forEach(v -> processValue(v, declaredVarConsumer, otherVarConsumer, processorSupport));
} else if (valueSpecification instanceof KeyExpression) {
processValue(((KeyExpression) valueSpecification)._expression(), declaredVarConsumer, otherVarConsumer, processorSupport);
} else if (valueSpecification instanceof LambdaFunction) {
processLambda((LambdaFunction<?>) valueSpecification, processorSupport).forEach(otherVarConsumer);
} else if (valueSpecification instanceof VariableExpression) {
otherVarConsumer.accept(((VariableExpression) valueSpecification)._name());
}
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.functions.lang.KeyExpression in project legend-pure by finos.
the class Reactivator method reactivateWithoutJavaCompilationImpl.
private static Object reactivateWithoutJavaCompilationImpl(final ValueSpecification valueSpecification, final PureMap lambdaOpenVariablesMap, final ExecutionSupport es, final boolean atRoot, Bridge bridge) {
if (valueSpecification instanceof RoutedValueSpecification) {
return reactivateWithoutJavaCompilationImpl(((RoutedValueSpecification) valueSpecification)._value(), lambdaOpenVariablesMap, es, atRoot, bridge);
} else if (valueSpecification instanceof InstanceValue) {
InstanceValue iv = (InstanceValue) valueSpecification;
RichIterable<?> result = iv._values().flatCollect(new org.eclipse.collections.api.block.function.Function<Object, RichIterable<Object>>() {
@Override
public RichIterable<Object> valueOf(Object value) {
Object result;
if (value instanceof ValueSpecification) {
result = reactivateWithoutJavaCompilationImpl((ValueSpecification) value, lambdaOpenVariablesMap, es, false, bridge);
} else if ((!lambdaOpenVariablesMap.getMap().isEmpty()) && value instanceof KeyExpression) {
KeyExpression ke = (KeyExpression) value;
CompiledProcessorSupport processorSupport = ((CompiledExecutionSupport) es).getProcessorSupport();
Object key = reactivateWithoutJavaCompilationImpl(ke._key(), lambdaOpenVariablesMap, es, false, bridge);
InstanceValue rKey = (InstanceValue) ((CompiledExecutionSupport) es).getProcessorSupport().newCoreInstance("key", M3Paths.InstanceValue, null);
rKey._values(key instanceof RichIterable ? (RichIterable) key : Lists.immutable.with(key));
Object expression = reactivateWithoutJavaCompilationImpl(ke._expression(), lambdaOpenVariablesMap, es, false, bridge);
InstanceValue rExpression = (InstanceValue) ((CompiledExecutionSupport) es).getProcessorSupport().newCoreInstance("key", M3Paths.InstanceValue, null);
rExpression._values(expression instanceof RichIterable ? (RichIterable) expression : Lists.immutable.with(expression));
KeyExpression rKe = ke.copy();
rKe._key(rKey);
rKe._expression(rExpression);
result = rKe;
} else if (value instanceof LambdaFunction) {
LambdaFunction lambdaFunction = (LambdaFunction) value;
UnifiedMap openVariables = UnifiedMap.newMap();
for (Object entry : lambdaOpenVariablesMap.getMap().keyValuesView()) {
Pair<String, org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.List> pair = (Pair<String, org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.List>) entry;
openVariables.put(pair.getOne(), pair.getTwo()._values());
}
for (Object entry : Pure.getOpenVariables(lambdaFunction, bridge).getMap().keyValuesView()) {
Pair<String, org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.List> pair = (Pair<String, org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.List>) entry;
openVariables.put(pair.getOne(), pair.getTwo()._values());
}
result = bridge.lambdaBuilder().value().lambdaFunction(lambdaFunction).pureFunction(DynamicPureLambdaFunctionImpl.createPureLambdaFunction(lambdaFunction, openVariables, bridge));
} else {
result = value;
}
return CompiledSupport.toPureCollection(result);
}
});
return (result != null) && (result.size() == 1) ? result.getFirst() : result;
} else if (valueSpecification instanceof SimpleFunctionExpression) {
SimpleFunctionExpression sfe = (SimpleFunctionExpression) valueSpecification;
Function func = (Function) sfe._func();
if (!canReactivateWithoutJavaCompilationImpl(func, es, atRoot, lambdaOpenVariablesMap, bridge)) {
throw new PureDynamicReactivateException(valueSpecification.getSourceInformation(), "Can not reactivate function, unexpected:" + func._name());
}
RichIterable<?> paramValues = sfe._parametersValues().collect(new org.eclipse.collections.api.block.function.Function<Object, Object>() {
@Override
public Object valueOf(Object value) {
if (value instanceof ValueSpecification) {
Object newValue = reactivateWithoutJavaCompilationImpl((ValueSpecification) value, lambdaOpenVariablesMap, es, false, bridge);
if (newValue instanceof RichIterable) {
return newValue;
} else {
return Lists.fixedSize.of(newValue);
}
} else {
return value;
}
}
});
MutableList<Object> vars = Lists.mutable.withAll(paramValues);
if (sfe._func()._name().equals("new_Class_1__String_1__KeyExpression_MANY__T_1_")) {
// Have to get the first param from the generic type
vars.set(0, Lists.fixedSize.of(sfe._genericType()._rawType()));
} else if (sfe._func()._name().equals("cast_Any_m__T_1__T_m_")) {
// Have to get the second param from the generic type
vars.set(1, Lists.fixedSize.of(sfe._genericType()));
}
return Pure._evaluateToMany(es, bridge, func, vars);
} else if (valueSpecification instanceof VariableExpression) {
String varName = ((VariableExpression) valueSpecification)._name();
if (!lambdaOpenVariablesMap.getMap().containsKey(varName)) {
throw new PureDynamicReactivateException("Attempt to use out of scope variable: " + varName);
}
Object result = lambdaOpenVariablesMap.getMap().get(varName);
return ((org.finos.legend.pure.m3.coreinstance.meta.pure.functions.collection.List<Object>) result)._values();
} else {
throw new PureDynamicReactivateException(valueSpecification.getSourceInformation(), "Unexpected type to dynamically reactivate: " + valueSpecification.getClass().getName());
}
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.functions.lang.KeyExpression in project legend-pure by finos.
the class CopyValidator method validateProperties.
private static void validateProperties(Matcher matcher, ValidatorState state, FunctionExpression expression, GenericType genericType, ProcessorSupport processorSupport) {
Type classifier = (Type) ImportStub.withImportStubByPass(genericType._rawTypeCoreInstance(), processorSupport);
ListIterable<? extends ValueSpecification> parametersValues = expression._parametersValues().toList();
if (parametersValues.size() > 2) {
if (parametersValues.get(2) instanceof InstanceValue) {
for (CoreInstance keyValue : ((InstanceValue) parametersValues.get(2))._valuesCoreInstance()) {
if (keyValue instanceof KeyExpression) {
// Validate key is an actual property
GenericType previousGenericType = genericType;
GenericType currentGenericType = null;
Type currentClassifier = classifier;
AbstractProperty property = null;
RichIterable<? extends CoreInstance> keys = ((KeyExpression) keyValue)._key()._valuesCoreInstance();
for (CoreInstance key : keys) {
property = (AbstractProperty) processorSupport.class_findPropertyUsingGeneralization(currentClassifier, key.getName());
if (property == null) {
throw new PureCompilationException(((KeyExpression) keyValue)._key().getSourceInformation(), "The property '" + key.getName() + "' can't be found in the type '" + classifier.getName() + "' or in its hierarchy.");
}
if (currentGenericType != null) {
previousGenericType = currentGenericType;
}
currentGenericType = property._genericType();
currentClassifier = (Type) ImportStub.withImportStubByPass(currentGenericType._rawTypeCoreInstance(), processorSupport);
}
NewValidator.validatePropertyValue(matcher, state, expression, (KeyExpression) keyValue, previousGenericType, property, processorSupport);
}
}
}
}
}
Aggregations