private static List <string> transform(string value, TransformationRule transformationRule) { List <string> tmp = new List <string>(); if (!string.IsNullOrEmpty(transformationRule.RegEx)) { Regex r = new Regex(transformationRule.RegEx, RegexOptions.IgnoreCase); // Match the regular expression pattern against a text string. MatchCollection matchCollection = r.Matches(value); foreach (var match in matchCollection.Cast <Match>()) { if (match.Success) { foreach (var groupElement in match.Groups) { tmp.Add(groupElement.ToString().Trim()); } } } } else { tmp.Add(value); } return(tmp); }
public void Conclude_containsNumberIsOneCondition_DontCareConclusionWhenGivenATwo() { TransformationRule<int> rule = new TransformationRule<int>(); rule.AddPremise(i => i == 1, new Conclusion("Not used")); Assert.IsInstanceOf<DontCareConclusion>(rule.Conclude(2)); }
/// <summary> /// Map a node from xml to system key /// </summary> /// <param name="simpleNodeName">name or xpath</param> /// <param name="simpleType"></param> /// <param name="complexNodeName">name or xpath</param> /// <param name="complexType"></param> /// <param name="key"></param> /// <param name="root"></param> /// <param name="metadataRef"></param> /// <param name="mappingManager"></param> private void createToKeyMapping( string simpleNodeName, LinkElementType simpleType, string complexNodeName, LinkElementType complexType, Key key, Mapping root, XDocument metadataRef, MappingManager mappingManager, TransformationRule transformationRule = null) { if (transformationRule == null) { transformationRule = new TransformationRule(); } LinkElement le = createLinkELementIfNotExist(mappingManager, Convert.ToInt64(key), key.ToString(), LinkElementType.Key, LinkElementComplexity.Simple); if (simpleNodeName.Equals(complexNodeName)) { List <XElement> elements = getXElements(simpleNodeName, metadataRef); foreach (XElement xElement in elements) { string sId = xElement.Attribute("id").Value; string name = xElement.Attribute("name").Value; LinkElement tmp = createLinkELementIfNotExist(mappingManager, Convert.ToInt64(sId), name, simpleType, LinkElementComplexity.Simple); Mapping tmpMapping = MappingHelper.CreateIfNotExistMapping(tmp, le, 1, new TransformationRule(), root, mappingManager); MappingHelper.CreateIfNotExistMapping(tmp, le, 2, transformationRule, tmpMapping, mappingManager); } } else { IEnumerable <XElement> complexElements = getXElements(complexNodeName, metadataRef); foreach (var complex in complexElements) { string sIdComplex = complex.Attribute("id").Value; string nameComplex = complex.Attribute("name").Value; LinkElement tmpComplexElement = createLinkELementIfNotExist(mappingManager, Convert.ToInt64(sIdComplex), nameComplex, complexType, LinkElementComplexity.Complex); Mapping complexMapping = MappingHelper.CreateIfNotExistMapping(tmpComplexElement, le, 1, new TransformationRule(), root, mappingManager); IEnumerable <XElement> simpleElements = XmlUtility.GetAllChildren(complex).Where(s => s.Name.LocalName.Equals(simpleNodeName)); foreach (XElement xElement in simpleElements) { string sId = xElement.Attribute("id").Value; string name = xElement.Attribute("name").Value; LinkElement tmp = createLinkELementIfNotExist(mappingManager, Convert.ToInt64(sId), name, simpleType, LinkElementComplexity.Simple); MappingHelper.CreateIfNotExistMapping(tmp, le, 2, transformationRule, complexMapping, mappingManager); } } } }
public Mapping CreateMapping( long source_elementId, LinkElementType source_type, LinkElementComplexity source_complexity, string source_name, string source_xpath, long target_elementId, LinkElementType target_type, LinkElementComplexity target_complexity, string target_name, string target_xpath, bool source_isSequence = false, bool target_isSequence = false, TransformationRule rule = null, long parentMappingId = 0 ) { LinkElement source = CreateLinkElement( source_elementId, source_type, source_complexity, source_name, source_xpath, source_isSequence ); LinkElement target = CreateLinkElement( target_elementId, target_type, target_complexity, target_name, target_xpath, target_isSequence ); Mapping mapping = new Mapping(); mapping.Source = source; mapping.Target = target; mapping.TransformationRule = rule; if (parentMappingId > 0) { mapping.Parent = this.GetUnitOfWork().GetReadOnlyRepository <Mapping>().Get(parentMappingId); } using (IUnitOfWork uow = this.GetUnitOfWork()) { IRepository <Mapping> repo = uow.GetRepository <Mapping>(); repo.Put(mapping); uow.Commit(); } return(mapping); }
public void Conclude_containsNumberIsOneCondition_CorrectConclusionWhenGivenOne() { TransformationRule<int> rule = new TransformationRule<int>(); rule.AddPremise(i => i == 1, new Conclusion("correct")); string expected = "correct"; string actual = rule.Conclude(1).ToString(); Assert.AreEqual(expected, actual); }
public void Conclude_aLowPriorityAndAHighPrioryWhereHighPriorityAndLowPriorityHaveNoOpinion_ConcludeDontCare() { TransformationRule<int> lowpriority = new TransformationRule<int>(); TransformationRule<int> highpriority = new TransformationRule<int>(); RuleHierarchy<string, int> hierarch = new RuleHierarchy<string, int>(); hierarch.AddLayer("low priority", lowpriority); hierarch.AddLayer("high priority", highpriority); Assert.IsInstanceOf<DontCareConclusion>(hierarch.Conclude(1)); }
public void Conclude_aLowPriorityAndAHighPrioryWhereHighPriorityHaveNoOpinion_ConcludeBasedOnTheLowPrioritiesRules() { TransformationRule<int> lowpriority = new TransformationRule<int>(); TransformationRule<int> highpriority = new TransformationRule<int>(); RuleHierarchy<string, int> hierarch = new RuleHierarchy<string, int>(); hierarch.AddLayer("low priority", lowpriority); hierarch.AddLayer("high priority", highpriority); lowpriority.AddPremise(i => i == 1, new Conclusion("not correct")); string expected = "not correct"; string actual = hierarch.Conclude(1).ToString(); Assert.AreEqual(expected, actual); }
public TransformationRule CreateTransformationRule(string regex, string mask) { var transformationRule = new TransformationRule() { RegEx = regex, Mask = mask }; using (IUnitOfWork uow = this.GetUnitOfWork()) { IRepository <TransformationRule> repo = uow.GetRepository <TransformationRule>(); repo.Put(transformationRule); uow.Commit(); } return(transformationRule); }
private void createFromPartyTypeMapping( string simpleNodeName, LinkElementType simpleType, string complexNodeName, LinkElementType complexType, PartyCustomAttribute partyCustomAttr, PartyType partyType, Mapping root, XDocument metadataRef, MappingManager mappingManager, TransformationRule transformationRule = null) { if (transformationRule == null) { transformationRule = new TransformationRule(); } LinkElement le = createLinkELementIfNotExist(mappingManager, Convert.ToInt64(partyType.Id), partyType.Title, LinkElementType.PartyType, LinkElementComplexity.Complex); XElement complex = getXElements(complexNodeName, metadataRef).FirstOrDefault(); string sIdComplex = complex.Attribute("id").Value; string nameComplex = complex.Attribute("name").Value; LinkElement tmpComplexElement = createLinkELementIfNotExist(mappingManager, Convert.ToInt64(sIdComplex), nameComplex, complexType, LinkElementComplexity.Complex); Mapping complexMapping = MappingHelper.CreateIfNotExistMapping(le, tmpComplexElement, 1, new TransformationRule(), root, mappingManager); IEnumerable <XElement> simpleElements = XmlUtility.GetAllChildren(complex).Where(s => s.Name.LocalName.Equals(simpleNodeName)); LinkElement simpleLe = createLinkELementIfNotExist(mappingManager, Convert.ToInt64(partyCustomAttr.Id), partyCustomAttr.Name, LinkElementType.PartyCustomType, LinkElementComplexity.Simple); foreach (XElement xElement in simpleElements) { string sId = xElement.Attribute("id").Value; string name = xElement.Attribute("name").Value; LinkElement tmp = createLinkELementIfNotExist(mappingManager, Convert.ToInt64(sId), name, simpleType, LinkElementComplexity.Simple); MappingHelper.CreateIfNotExistMapping(simpleLe, tmp, 2, transformationRule, complexMapping, mappingManager); } }
public void Conclude_containsTwoCondradictionaryConclusions_RaiseException() { TransformationRule<int> rule = new TransformationRule<int>(); rule.AddPremise(i => i == 1, new Conclusion("Correct")); rule.AddPremise(i => i == 1, new Conclusion("Not Correct")); bool exceptionthrown = false; Exception exception = null; try { rule.Conclude(1); } catch (MultiConclusionException e) { exceptionthrown = true; exception = e; } Assert.IsTrue(exceptionthrown); Assert.IsInstanceOf<MultiConclusionException>(exception); }
public static bool UpdateSimpleMappings(long sourceId, long targetId, List <SimpleMappingModel> newListOfSimpleMappings, Mapping parent, MappingManager mappingManager) { List <Mapping> mappingsInDatabase = mappingManager.MappingRepo.Get() .Where(m => m.Parent != null && m.Parent.Id.Equals(parent.Id)).ToList(); List <long> deleteMappings = new List <long>(); //delete all mappings that are not in the list foreach (var mapping in mappingsInDatabase) { bool exist = false; foreach (var newMapping in newListOfSimpleMappings) { if (mapping.Source.ElementId.Equals(newMapping.Source.ElementId) && mapping.Target.ElementId.Equals(newMapping.Target.ElementId)) { exist = true; } } if (!exist) { deleteMappings.Add(mapping.Id); } } foreach (var id in deleteMappings) { DeleteMapping(id, mappingManager, false); } //Create simple mappings //all mappings with the same source or target should List <LinkElementModel> createdLinkELementModels = new List <LinkElementModel>(); foreach (var sm in newListOfSimpleMappings) { LinkElement simpleMappingSource = null; LinkElement simpleMappingTarget = null; //if its not a new parent mapping or its in the list, please select existing simpleMappingSource = MappingHelper.CreateIfNotExistLinkElement(sm.Source, sourceId, mappingManager); //if its not a new parent mapping or its in the list, please select existing ////if (ExistLinkElementModel(sm.Target, createdLinkELementModels)) simpleMappingTarget = MappingHelper.CreateIfNotExistLinkElement(sm.Target, targetId, mappingManager); //if (sm.TransformationRule. != null) // simpleMappingTarget = mappingManager.UpdateLinkElement(simpleMappingTarget.Id); TransformationRule transformationRule = new TransformationRule(sm.TransformationRule.Id, sm.TransformationRule.RegEx, sm.TransformationRule.Mask); Mapping simplemapping = MappingHelper.CreateIfNotExistMapping(simpleMappingSource, simpleMappingTarget, 2, null, parent, mappingManager); if (transformationRule != null) { transformationRule = mappingManager.UpdateTransformationRule(transformationRule.Id, transformationRule.RegEx, transformationRule.Mask); simplemapping.TransformationRule = transformationRule; mappingManager.UpdateMapping(simplemapping); } sm.Id = simplemapping.Id; } return(false); }
public static Mapping CreateIfNotExistMapping(LinkElement source, LinkElement target, long level, TransformationRule rule, Mapping parent, MappingManager mappingManager) { object tmp = new object(); IEnumerable <Mapping> mappings = tmp.GetUnitOfWork().GetReadOnlyRepository <Mapping>().Get(); Mapping mapping = null; if (parent != null) { mapping = mappings.FirstOrDefault( m => m.Parent != null && m.Parent.Id.Equals(parent.Id) && m.Source.Id.Equals(source.Id) && m.Source.Type.Equals(source.Type) && m.Target.Id.Equals(target.Id) && m.Target.Type.Equals(target.Type) && m.Level.Equals(level)); } else { mapping = mappings.FirstOrDefault( m => m.Parent == null && m.Source.Id.Equals(source.Id) && m.Source.Type.Equals(source.Type) && m.Target.Id.Equals(target.Id) && m.Target.Type.Equals(target.Type) && m.Level.Equals(level)); } if (mapping == null) { if (rule != null && rule.Id == 0 && rule.RegEx != null) { rule = mappingManager.CreateTransformationRule(rule.RegEx, rule.Mask); } mapping = mappingManager.CreateMapping(source, target, level, rule, parent); } else { if (rule != null) { rule = mappingManager.UpdateTransformationRule(rule.Id, rule.RegEx, rule.Mask); mapping.TransformationRule = rule; mappingManager.UpdateMapping(mapping); } } return(mapping); }
public Mapping CreateMapping(LinkElement source, LinkElement target, long level, TransformationRule rule, Mapping parent) { Mapping mapping = new Mapping(); mapping.Source = source; mapping.Target = target; mapping.TransformationRule = rule; mapping.Level = level; mapping.Parent = parent; Debug.WriteLine("------------------------------------"); if (source != null) { Debug.WriteLine(source.Id); } else { Debug.WriteLine("null"); } if (target != null) { Debug.WriteLine(target.Id); } else { Debug.WriteLine("null"); } if (rule != null) { Debug.WriteLine(rule.Id); } else { Debug.WriteLine("null"); } if (parent != null) { Debug.WriteLine(parent.Id); } else { Debug.WriteLine("null"); } using (IUnitOfWork uow = this.GetUnitOfWork()) { IRepository <Mapping> repo = uow.GetRepository <Mapping>(); if (repo != null) { Debug.WriteLine("repo not null"); } else { Debug.WriteLine("null"); } repo.Put(mapping); uow.Commit(); } return(mapping); }