use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.ModelElement in project legend-pure by finos.
the class ProfileUnloaderWalk method run.
@Override
public void run(Profile profile, MatcherState state, Matcher matcher, ModelRepository modelRepository, Context context) throws PureCompilationException {
WalkerState walkerState = (WalkerState) state;
walkerState.addInstance(profile);
for (Tag tag : profile._p_tags()) {
for (AnnotatedElement modelElement : tag._modelElements()) {
matcher.fullMatch(modelElement, state);
}
}
for (Stereotype stereotype : profile._p_stereotypes()) {
for (AnnotatedElement modelElement : stereotype._modelElements()) {
matcher.fullMatch(modelElement, state);
}
}
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.ModelElement in project legend-pure by finos.
the class RelationalInstanceSetImplementationProcessor method process.
@Override
public void process(RootRelationalInstanceSetImplementation implementation, ProcessorState state, Matcher matcher, final ModelRepository repository, final Context context, final ProcessorSupport processorSupport) {
// No need to cross reference... ClassMapping is responsible for that
ModelElement cls = (ModelElement) ImportStub.withImportStubByPass(implementation._classCoreInstance(), processorSupport);
GenericType instanceGenericType = cls._classifierGenericType() == null ? (GenericType) Type.wrapGenericType(processorSupport.getClassifier(cls), processorSupport) : cls._classifierGenericType();
CoreInstance propertyReturnGenericType = org.finos.legend.pure.m3.navigation.generictype.GenericType.resolvePropertyReturnType((implementation._classifierGenericType() == null ? (GenericType) Type.wrapGenericType(processorSupport.getClassifier(implementation), processorSupport) : implementation._classifierGenericType()), implementation.getKeyByName(M3Properties._class), processorSupport);
if (!org.finos.legend.pure.m3.navigation.generictype.GenericType.subTypeOf(instanceGenericType, propertyReturnGenericType, processorSupport)) {
throw new PureCompilationException(implementation.getSourceInformation(), "Trying to map an unsupported type in Relational: Type Error: '" + org.finos.legend.pure.m3.navigation.generictype.GenericType.print(instanceGenericType, processorSupport) + "' not a subtype of '" + org.finos.legend.pure.m3.navigation.generictype.GenericType.print(propertyReturnGenericType, processorSupport) + "'");
}
RichIterable<? extends PropertyMapping> propertyMappings = implementation._propertyMappings();
MutableSet<TableAlias> tableAliases = RelationalPropertyMappingProcessor.processRelationalPropertyMappings(propertyMappings, implementation, implementation, implementation._id(), matcher, state, repository, processorSupport);
if (implementation._id().equals(implementation._superSetImplementationId())) {
throw new PureCompilationException(implementation.getSourceInformation(), "Extend mapping id cannot reference self \'" + implementation._id() + "\'");
}
TableAlias mainTableAlias;
TableAlias userDefinedMainTable = implementation._mainTableAlias();
if (userDefinedMainTable == null) {
MutableSet<RelationalOperationElement> tables = tableAliases.collect(TABLE_ALIAS_TO_RELATIONAL_OPERATION_ELEMENT_FN);
MutableSet<Database> databases = tableAliases.collectWith(TABLE_ALIAS_TO_DATABASE_FN, processorSupport);
if (implementation._superSetImplementationId() != null) {
RootRelationalInstanceSetImplementation superImplementation = getSuperMapping(implementation, processorSupport);
PostProcessor.processElement(matcher, superImplementation, state, processorSupport);
collectTableAndDatabaseFromSuperImplementation(superImplementation, implementation, tables, databases, processorSupport);
}
if (tables.size() != 1) {
throw new PureCompilationException(implementation.getSourceInformation(), "Can't find the main table for class '" + cls._name() + "'. Please specify a main table using the ~mainTable directive.");
}
if (databases.size() != 1) {
throw new PureCompilationException(implementation.getSourceInformation(), "Can't find the main table for class '" + cls._name() + "'. Inconsistent database definitions for the mapping");
}
mainTableAlias = (TableAlias) processorSupport.newAnonymousCoreInstance(null, M2RelationalPaths.TableAlias);
mainTableAlias._name(repository.newStringCoreInstance_cached("").getName());
mainTableAlias._relationalElement(tables.toList().getFirst());
mainTableAlias._databaseCoreInstance(databases.toList().getFirst());
implementation._mainTableAlias(mainTableAlias);
} else {
if (implementation._superSetImplementationId() == null) {
mainTableAlias = userDefinedMainTable;
Database database = (Database) ImportStub.withImportStubByPass(mainTableAlias._databaseCoreInstance(), processorSupport);
NamedRelation table = (NamedRelation) DatabaseProcessor.findTableForAlias(database, mainTableAlias, processorSupport);
mainTableAlias._relationalElement(table);
mainTableAlias._setMappingOwner(implementation);
} else {
throw new PureCompilationException(implementation.getSourceInformation(), "Cannot specify main table explicitly for extended mapping [" + implementation._id() + "]");
}
}
RelationalOperationElement mainTable = mainTableAlias._relationalElement();
for (JoinTreeNode joinTreeNode : RelationalPropertyMappingProcessor.collectJoinTreeNodes(propertyMappings)) {
RelationalOperationElementProcessor.processAliasForJoinTreeNode(joinTreeNode, mainTable, processorSupport);
}
RelationalMappingSpecificationProcessing.processFilterMapping(implementation, implementation, mainTable, matcher, state, repository, processorSupport);
GroupByMapping groupByMapping = RelationalMappingSpecificationProcessing.processGroupByMapping(implementation, implementation, state, matcher, repository, processorSupport).getOne();
Boolean distinct = implementation._distinct();
if (groupByMapping != null) {
implementation._primaryKey(groupByMapping._columns());
} else if (distinct) {
RichIterable<RelationalOperationElement> pks = propertyMappings.flatCollect(PROPERTY_MAPPING_TO_RELATIONAL_OPERATION_ELEMENT_FN);
RichIterable<RelationalOperationElement> pksWithDistinctColumns = pks.groupBy(TAC_TO_COLUMN).keyMultiValuePairsView().collect(Functions.<RichIterable<RelationalOperationElement>>secondOfPair()).collect(new Function<RichIterable<RelationalOperationElement>, RelationalOperationElement>() {
@Override
public RelationalOperationElement valueOf(RichIterable<RelationalOperationElement> roes) {
return roes.getFirst();
}
});
implementation._primaryKey(pksWithDistinctColumns.select(RELATIONAL_OPERATION_ELEMENT_PK_PREDICATE));
} else if (implementation._primaryKey().isEmpty()) {
Relation relation = (Relation) mainTableAlias._relationalElement();
final TableAlias finalMainTable = mainTableAlias;
DatabaseProcessor.processTable(relation, matcher, state, processorSupport);
RichIterable<? extends Column> columns = FastList.newList();
if (relation instanceof Table) {
columns = ((Table) relation)._primaryKey();
} else if (relation instanceof View) {
columns = ((View) relation)._primaryKey();
}
RichIterable<TableAliasColumn> primaryKey = columns.collect(new Function<Column, TableAliasColumn>() {
@Override
public TableAliasColumn valueOf(Column column) {
TableAliasColumn tableAliasColumn = (TableAliasColumn) repository.newEphemeralAnonymousCoreInstance(null, processorSupport.package_getByUserPath(M2RelationalPaths.TableAliasColumn));
tableAliasColumn._column(column);
tableAliasColumn._alias(finalMainTable);
return tableAliasColumn;
}
});
implementation._primaryKey(primaryKey);
} else // else if (Instance.getValueForMetaPropertyToManyResolved(implementation, M2RelationalProperties.primaryKey, context, processorSupport).isEmpty())
// {
// // throw new PureCompilationException(implementation.getSourceInformation(), "Please provide a primaryKey");
// }
{
processUserDefinedPrimaryKey(implementation, implementation, matcher, state, repository, processorSupport);
}
implementation._stores(implementation._propertyMappings().collect(STORE).toSet().with(implementation._mainTableAlias()._database()).without(null));
MilestoningPropertyMappingProcessor.processMilestoningPropertyMapping(implementation, implementation, processorSupport);
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.ModelElement in project legend-pure by finos.
the class RelationalPropertyMappingProcessor method processRelationalPropertyMapping.
static MutableSet<TableAlias> processRelationalPropertyMapping(PropertyMapping propertyMapping, Matcher matcher, ProcessorState state, ModelRepository repository, String depth, ProcessorSupport processorSupport, PropertyMappingsImplementation implementation, PropertyMappingsImplementation topLevel) {
if (MilestoningFunctions.isAutoGeneratedMilestoningNamedDateProperty(propertyMapping._property(), processorSupport)) {
throw new PureCompilationException(propertyMapping.getSourceInformation(), "Class : [" + PackageableElement.getUserPathForPackageableElement(((SetImplementation) implementation)._class()) + "] has temporal specification. Hence mapping of property : [milestoning] is reserved and should not be explicit in the mapping");
}
MutableSet<TableAlias> tableAliases = Sets.mutable.empty();
String newDepth = depth + "_" + ((ModelElement) propertyMapping._propertyCoreInstance())._name();
CoreInstance sourceSetImplementationId = repository.newStringCoreInstance_cached(depth);
if (implementation instanceof EmbeddedRelationalInstanceSetImplementation) {
propertyMapping._sourceSetImplementationId(sourceSetImplementationId.getName());
}
if (propertyMapping instanceof EmbeddedRelationalInstanceSetImplementation) {
EmbeddedRelationalInstanceSetImplementation embeddedRelationalInstanceSetImplementation = (EmbeddedRelationalInstanceSetImplementation) propertyMapping;
if (!(implementation instanceof EmbeddedRelationalInstanceSetImplementation)) {
propertyMapping._sourceSetImplementationId(implementation._id());
}
String newId;
if (embeddedRelationalInstanceSetImplementation._id() == null) {
CoreInstance new_Id = repository.newStringCoreInstance_cached(newDepth);
embeddedRelationalInstanceSetImplementation._id(new_Id.getName());
embeddedRelationalInstanceSetImplementation._targetSetImplementationId(new_Id.getName());
newId = newDepth;
} else {
newId = embeddedRelationalInstanceSetImplementation._id();
}
RootRelationalInstanceSetImplementation owner = embeddedRelationalInstanceSetImplementation._setMappingOwner();
if (owner == null) {
embeddedRelationalInstanceSetImplementation._setMappingOwner((RootRelationalInstanceSetImplementation) topLevel);
}
Property property = (Property) ImportStub.withImportStubByPass(embeddedRelationalInstanceSetImplementation._propertyCoreInstance(), processorSupport);
CoreInstance targetClass = property._classifierGenericType() == null || property._classifierGenericType()._typeArguments() == null || property._classifierGenericType()._typeArguments().size() < 2 || property._classifierGenericType()._typeArguments().toList().get(1) == null ? null : ImportStub.withImportStubByPass(property._classifierGenericType()._typeArguments().toList().get(1)._rawTypeCoreInstance(), processorSupport);
embeddedRelationalInstanceSetImplementation._classCoreInstance(targetClass);
RichIterable<? extends PropertyMapping> embeddedPropertyMappings = embeddedRelationalInstanceSetImplementation._propertyMappings();
tableAliases.addAll(processRelationalPropertyMappings(embeddedPropertyMappings, embeddedRelationalInstanceSetImplementation, (RootRelationalInstanceSetImplementation) topLevel, newId, matcher, state, repository, processorSupport));
RelationalInstanceSetImplementationProcessor.processUserDefinedPrimaryKey(embeddedRelationalInstanceSetImplementation, topLevel, matcher, state, repository, processorSupport);
if (propertyMapping instanceof OtherwiseEmbeddedRelationalInstanceSetImplementation) {
OtherwiseEmbeddedRelationalInstanceSetImplementation otherwiseEmbeddedRelationalInstanceSetImplementation = (OtherwiseEmbeddedRelationalInstanceSetImplementation) propertyMapping;
PropertyMapping otherwiseMapping = otherwiseEmbeddedRelationalInstanceSetImplementation._otherwisePropertyMapping();
otherwiseMapping._sourceSetImplementationId(sourceSetImplementationId.getName());
// relationalOperationElement
RelationalOperationElement impl = ((RelationalPropertyMapping) otherwiseMapping)._relationalOperationElement();
RelationalOperationElementProcessor.processColumnExpr(impl, topLevel, topLevel, tableAliases, matcher, state, repository, processorSupport);
}
} else {
// Transformer
if (propertyMapping instanceof RelationalPropertyMapping) {
GrammarInfoStub transformerStub = (GrammarInfoStub) ((RelationalPropertyMapping) propertyMapping)._transformerCoreInstance();
EnumerationMappingProcessor.processsEnumerationTransformer(transformerStub, propertyMapping, processorSupport);
// relationalOperationElement
RelationalOperationElement impl = ((RelationalPropertyMapping) propertyMapping)._relationalOperationElement();
RelationalOperationElementProcessor.processColumnExpr(impl, topLevel, topLevel, tableAliases, matcher, state, repository, processorSupport);
propertyMapping._store(impl instanceof RelationalOperationElementWithJoin ? ((RelationalOperationElementWithJoin) impl)._joinTreeNode()._database() : null);
}
}
return tableAliases;
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.ModelElement in project legend-pure by finos.
the class ElementWithConstraintsValidator method validateConstraints.
static void validateConstraints(ModelElement instance, RichIterable<? extends Constraint> constraints, MatcherState state) throws PureCompilationException {
ValidatorState validatorState = (ValidatorState) state;
ProcessorSupport processorSupport = validatorState.getProcessorSupport();
MutableList<String> ruleNames = FastList.newList();
if (constraints.notEmpty()) {
CoreInstance booleanType = processorSupport.package_getByUserPath(M3Paths.Boolean);
CoreInstance stringType = processorSupport.package_getByUserPath(M3Paths.String);
for (Constraint constraint : constraints) {
String ruleName = constraint._name();
if (ruleNames.contains(ruleName)) {
String instanceName = instance._name();
throw new PureCompilationException(constraint.getSourceInformation(), "Constraints for " + instanceName + " must be unique, [" + ruleName + "] is duplicated");
}
ruleNames.add(ruleName);
ValueSpecification definition = constraint._functionDefinition()._expressionSequence().getFirst();
Type type = (Type) ImportStub.withImportStubByPass(definition._genericType()._rawTypeCoreInstance(), processorSupport);
if (type != booleanType || !Multiplicity.isToOne(definition._multiplicity(), true)) {
throw new PureCompilationException(constraint._functionDefinition().getSourceInformation(), "A constraint must be of type Boolean and multiplicity one");
}
if (processorSupport.instance_instanceOf(instance, M3Paths.ElementWithConstraints)) {
if (constraint._messageFunction() != null) {
ValueSpecification message = constraint._messageFunction()._expressionSequence().getFirst();
Type messageType = (Type) ImportStub.withImportStubByPass(message._genericType()._rawTypeCoreInstance(), processorSupport);
if (messageType != stringType || !Multiplicity.isToOne(message._multiplicity(), true)) {
throw new PureCompilationException(constraint._messageFunction().getSourceInformation(), "A constraint message must be of type String and multiplicity one");
}
}
}
}
}
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.ModelElement in project legend-pure by finos.
the class PathUnbind method run.
@Override
public void run(Path modelElement, MatcherState state, Matcher matcher, ModelRepository modelRepository, Context context) throws PureCompilationException {
ProcessorSupport processorSupport = state.getProcessorSupport();
GenericType start = modelElement._start();
Shared.cleanUpGenericType(start, (UnbindState) state, processorSupport);
for (PathElement pathElement : (RichIterable<PathElement>) modelElement._path()) {
if (pathElement instanceof PropertyPathElement) {
PropertyStub property = (PropertyStub) ((PropertyPathElement) pathElement)._propertyCoreInstance();
CoreInstance resolved = property._resolvedPropertyCoreInstance();
if (resolved != null) {
ReferenceUsage.removeReferenceUsagesForUser(resolved, modelElement, state.getProcessorSupport());
}
Shared.cleanPropertyStub(property, processorSupport);
property._ownerRemove();
RichIterable<? extends ValueSpecification> parameters = ((PropertyPathElement) pathElement)._parameters();
for (ValueSpecification parameter : parameters) {
if (parameter instanceof InstanceValue) {
RichIterable<? extends CoreInstance> values = ((InstanceValue) parameter)._valuesCoreInstance();
for (CoreInstance value : values) {
Shared.cleanEnumStub(value, processorSupport);
}
parameter._multiplicityRemove();
parameter._genericTypeRemove();
}
}
}
}
modelElement._classifierGenericTypeRemove();
}
Aggregations