Example #1
0
 public OclCompilerResult TryCompile(string code, bool hasError) {
     var script = CreateTestEnv();
     script.Contents = code;
     OclCompilerResult result = script.CompileToAst();
     Assert.AreEqual(hasError, result.Errors.HasError);
     return result;
 }
Example #2
0
        private void TranslateScript(XElement schSchema, OCLScript oclScript, TranslationSettings translationSettings,
                                     ref Dictionary <PSMClass, List <PatternInfo> > patterns)
        {
            OclCompilerResult compilerResult = oclScript.CompileToAst();

            compilerResult.CompileExpressionsInMessages();
            if (!compilerResult.Errors.HasError)
            {
                XComment comment = new XComment(string.Format("Below follow constraints from OCL script '{0}'. ", oclScript.Name));
                schSchema.Add(comment);

                foreach (ClassifierConstraintBlock classifierConstraintBlock in compilerResult.Constraints.ClassifierConstraintBlocks)
                {
                    PSMClass contextClass = (PSMClass)classifierConstraintBlock.Context.Tag;
                    patterns.CreateSubCollectionIfNeeded(contextClass);
                    string patternName = NameSuggestor <PatternInfo> .SuggestUniqueName(patterns[contextClass], contextClass.Name, p => p.PatternName, true, false);

                    PatternInfo patternInfo = new PatternInfo {
                        PatternName = patternName
                    };
                    XElement patternElement = schSchema.SchematronPattern(patternInfo.PatternName);
                    patterns[contextClass].Add(patternInfo);

                    bool abstractPattern = !contextClass.GeneralizationsAsGeneral.IsEmpty();

                    if (abstractPattern)
                    {
                        patternElement.AddAttributeWithValue("abstract", "true");
                    }

                    string context = !abstractPattern?contextClass.GetXPathFull(true).ToString() : "$" + classifierConstraintBlock.Self.Name;

                    XElement ruleElement = patternElement.SchematronRule(context);
                    patternInfo.ContextVariableName = classifierConstraintBlock.Self.Name;
                    if (!abstractPattern)
                    {
                        ruleElement.SchematronLet(patternInfo.ContextVariableName, @".");
                    }

                    TranslateInvariantsToXPath(classifierConstraintBlock, ruleElement, (PSMBridge)compilerResult.Bridge, translationSettings);
                }
            }
            else
            {
                XComment comment = new XComment(string.Format("OCL script '{0}' contains errors and thus can not be translated. ", oclScript.Name));
                schSchema.Add(comment);
            }
        }
Example #3
0
        public IList <ClassifierConstraintBlock> FindSuitableConstraints(PIMSchema pimSchema, PSMSchema psmSchema)
        {
            List <ClassifierConstraintBlock> result = new List <ClassifierConstraintBlock>();

            foreach (OCLScript oclScript in pimSchema.OCLScripts)
            {
                OclCompilerResult compilerResult = oclScript.CompileToAst();
                if (!compilerResult.Errors.HasError)
                {
                    ConstraintSuitabilityChecker constraintSuitabilityChecker = new ConstraintSuitabilityChecker
                    {
                        TargetPSMSchema = psmSchema,
                        Bridge          = compilerResult.Bridge
                    };
                    ConstraintConvertor constraintConvertor = new ConstraintConvertor
                    {
                        TargetPSMSchema = psmSchema,
                        Bridge          = compilerResult.Bridge
                    };


                    foreach (ClassifierConstraintBlock classifierConstraint in compilerResult.Constraints.ClassifierConstraintBlocks)
                    {
                        /* constraints from one PIM context can be distributed amont several
                         * PSM contexts (different PSM classes with identical interpretation) */
                        Dictionary <Classifier, ClassifierConstraintBlock> translatedInvariants =
                            new Dictionary <Classifier, ClassifierConstraintBlock>();

                        foreach (InvariantWithMessage pimInvariant in classifierConstraint.Invariants)
                        {
                            constraintSuitabilityChecker.Clear();
                            bool suitable = constraintSuitabilityChecker.CheckConstraintSuitability(
                                classifierConstraint, pimInvariant.Constraint);
                            if (suitable)
                            {
                                Classifier psmContextSuggestion = null;
                                constraintConvertor.Clear();
                                try
                                {
                                    OclExpression psmInvariant = constraintConvertor.TranslateConstraint(classifierConstraint, pimInvariant.Constraint, constraintSuitabilityChecker.VariableClassMappings,
                                                                                                         constraintSuitabilityChecker.PathMappings, constraintSuitabilityChecker.VariableTranslations, out psmContextSuggestion);

                                    if (!translatedInvariants.ContainsKey(psmContextSuggestion))
                                    {
                                        translatedInvariants[psmContextSuggestion] = new ClassifierConstraintBlock(psmContextSuggestion,
                                                                                                                   new List <InvariantWithMessage>(), constraintConvertor.SelfVariableDeclaration);
                                    }
                                    translatedInvariants[psmContextSuggestion].Invariants.Add(new InvariantWithMessage(psmInvariant));
                                }
                                catch
                                {
                                }
                            }
                        }

                        foreach (KeyValuePair <Classifier, ClassifierConstraintBlock> kvp in translatedInvariants)
                        {
                            result.Add(kvp.Value);
                        }
                    }
                }
            }
            return(result);
        }
Example #4
0
        public DetectedChangeInstancesSet DetectChanges(PSMSchema schema1, PSMSchema schema2)
        {
            Version oldVersion = schema1.Version;
            Version newVersion = schema2.Version;

            Debug.Assert(oldVersion != newVersion);

            DetectedChangeInstancesSet changeInstancesSet = new DetectedChangeInstancesSet();

            changeInstancesSet.OldVersion = oldVersion;
            changeInstancesSet.NewVersion = newVersion;

            #region attributes

            testConstructs(schema1.PSMAttributes, oldVersion, newVersion, changeInstancesSet, EChangePredicateScope.PSMAttribute);
            testConstructs(schema2.PSMAttributes, oldVersion, newVersion, changeInstancesSet, EChangePredicateScope.PSMAttribute);

            #endregion

            #region associations

            testConstructs(schema1.PSMAssociations, oldVersion, newVersion, changeInstancesSet, EChangePredicateScope.PSMAssociation);
            testConstructs(schema2.PSMAssociations, oldVersion, newVersion, changeInstancesSet, EChangePredicateScope.PSMAssociation);

            #endregion

            #region classes

            testConstructs(schema1.PSMClasses, oldVersion, newVersion, changeInstancesSet, EChangePredicateScope.PSMClass);
            testConstructs(schema2.PSMClasses, oldVersion, newVersion, changeInstancesSet, EChangePredicateScope.PSMClass);

            #endregion

            #region contentModels

            testConstructs(schema1.PSMContentModels, oldVersion, newVersion, changeInstancesSet, EChangePredicateScope.PSMContentModel);
            testConstructs(schema2.PSMContentModels, oldVersion, newVersion, changeInstancesSet, EChangePredicateScope.PSMContentModel);

            #endregion

            foreach (OCLScript oclScript in schema2.OCLScripts)
            {
                if (oclScript.Type == OCLScript.EOclScriptType.Evolution)
                {
                    try
                    {
                        OclCompilerResult r = oclScript.CompileToAst();
                        changeInstancesSet.OclCompilerResult = r;
                        foreach (PropertyInitializationBlock propertyInitializationBlock in r.PropertyInitializations.PropertyInitializationBlocks)
                        {
                            foreach (PropertyInitialization propertyInitialization in propertyInitializationBlock.PropertyInitializations)
                            {
                                propertyInitialization.InitializationExpression.ConstraintContext = propertyInitializationBlock;
                                if (propertyInitialization.Property is PSMBridgeAttribute)
                                {
                                    PSMAttribute p = ((PSMBridgeAttribute)propertyInitialization.Property).SourceAttribute;
                                    changeInstancesSet.AttributeInitializations[p] = propertyInitialization.InitializationExpression;
                                }

                                if (propertyInitialization.Property is PSMBridgeAssociation)
                                {
                                    PSMAssociation p = ((PSMBridgeAssociation)propertyInitialization.Property).SourceAsscociation;
                                    changeInstancesSet.AssociationInitializations[p] = propertyInitialization.InitializationExpression;
                                }
                            }
                        }
                    }
                    catch
                    {
                    }
                }
            }

            ClassifyNodes(changeInstancesSet, schema2);

            return(changeInstancesSet);
        }