private void BuildXml(XElement rootElement, string startClassName, string startClassIdentifier) { ClassTemplateMap classTemplateMap = String.IsNullOrEmpty(startClassName) ? _graphMap.classTemplateMaps.First() : _graphMap.GetClassTemplateMapByName(startClassName); if (classTemplateMap != null) { ClassMap classMap = classTemplateMap.classMap; List <TemplateMap> templateMaps = classTemplateMap.templateMaps; if (classMap != null) { for (int dataObjectIndex = 0; dataObjectIndex < _dataObjects.Count; dataObjectIndex++) { bool hasRelatedProperty; List <string> classIdentifiers = GetClassIdentifiers(classMap, dataObjectIndex, out hasRelatedProperty); ProcessOutboundClass(dataObjectIndex, startClassName, startClassIdentifier, true, classIdentifiers, hasRelatedProperty, rootElement, classTemplateMap.classMap, classTemplateMap.templateMaps); } } } else { throw new Exception("Class [" + startClassName + "] not found."); } }
private void ProcessInboundClass(int dataObjectIndex, ClassMap classMap, List <string> classInstances) { for (int classInstanceIndex = 0; classInstanceIndex < classInstances.Count; classInstanceIndex++) { string classInstance = classInstances[classInstanceIndex]; string identifierValue = classInstance.Substring(classInstance.LastIndexOf("/") + 1); ProcessInboundClassIdentifiers(dataObjectIndex, classMap, classInstanceIndex, identifierValue); } ClassTemplateMap classTemplateMap = _graphMap.GetClassTemplateMap(classMap.id, classMap.index); if (classTemplateMap != null) { List <TemplateMap> templateMaps = classTemplateMap.templateMaps; if (templateMaps != null && templateMaps.Count > 0) { ProcessInboundTemplates(dataObjectIndex, classInstances, templateMaps); } } }
// build RDF that's rooted at className with classIdentifier private XElement BuildRdfXml(string startClassName, string startClassIdentifier) { ClassTemplateMap classTemplateMap = _graphMap.GetClassTemplateMapByName(startClassName); if (classTemplateMap != null) { ClassMap classMap = classTemplateMap.classMap; List <TemplateMap> templateMaps = classTemplateMap.templateMaps; if (classMap != null) { bool hasRelatedProperty; List <string> classIdentifiers = GetClassIdentifiers(classMap, 0, out hasRelatedProperty); ProcessOutboundClass(0, startClassName, startClassIdentifier, true, classIdentifiers, hasRelatedProperty, classTemplateMap.classMap, classTemplateMap.templateMaps); } } return(_rdfXml); }
private XElement BuildRdfXml() { ClassTemplateMap classTemplateMap = _graphMap.classTemplateMaps.First(); if (classTemplateMap != null) { ClassMap classMap = classTemplateMap.classMap; List <TemplateMap> templateMaps = classTemplateMap.templateMaps; if (classMap != null) { for (int dataObjectIndex = 0; dataObjectIndex < _dataObjects.Count; dataObjectIndex++) { bool hasRelatedProperty; List <string> classIdentifiers = GetClassIdentifiers(classMap, dataObjectIndex, out hasRelatedProperty); ProcessOutboundClass(dataObjectIndex, String.Empty, String.Empty, true, classIdentifiers, hasRelatedProperty, classTemplateMap.classMap, classTemplateMap.templateMaps); } } } return(_rdfXml); }
public string ProjectProperty(string[] propertyNameParts, ref Values values) { string dataPropertyName = String.Empty; string className = propertyNameParts[0]; string templateName = propertyNameParts[1]; string roleName = propertyNameParts[2]; string classPath = String.Empty; if (className.Contains("$")) { string[] temp = className.Split('$'); className = temp[0]; classPath = temp[1]; } ClassTemplateMap classTemplateMap = _graphMap.classTemplateMaps.Find( cm => cm.classMap.name.Replace(" ", "").ToLower() == className.Replace(" ", "").ToLower()); if (classTemplateMap == null) { throw new Exception("Classmap [" + className + "] not found."); } List <TemplateMap> templateMaps = classTemplateMap.templateMaps; TemplateMap templateMap = templateMaps.Find(tm => tm.name.ToLower() == templateName.ToLower()); RoleMap roleMap = templateMap.roleMaps.Find(rm => rm.name.ToLower() == roleName.ToLower()); switch (roleMap.type) { case RoleType.DataProperty: dataPropertyName = roleMap.propertyName; _valueListName = null; break; case RoleType.ObjectProperty: dataPropertyName = roleMap.propertyName; _valueListName = roleMap.valueListName; break; case RoleType.Property: //if last part... dataPropertyName = roleMap.propertyName; if (String.IsNullOrEmpty(roleMap.valueListName)) { _valueListName = null; } else { _valueListName = roleMap.valueListName; for (int i = 0; i < values.Count; i++) { string value = values[i]; ValueListMap valueListMap = _mapping.valueListMaps.Find(x => x.name.ToLower() == roleMap.valueListName.ToLower()); if (valueListMap != null && valueListMap.valueMaps != null) { /// ValueMap valueMap = valueListMap.valueMaps.Find(x => x.uri == value); ValueMap valueMap = valueListMap.valueMaps.Find(x => x.label == value); if (valueMap != null) { value = valueMap.internalValue; } else { value = valueListMap.valueMaps[0].internalValue; } } values[i] = value; } } break; case RoleType.FixedValue: case RoleType.Possessor: case RoleType.Reference: throw new Exception("Role " + roleName + " can not be projected to property."); } return(dataPropertyName); }
private void CreateTemplateElement(int dataObjectIndex, string startClassName, string startClassIdentifier, string baseUri, string classIdentifier, int classIdentifierIndex, TemplateMap templateMap, bool classIdentifierHasRelatedProperty) { string classInstance = baseUri + classIdentifier; IDataObject dataObject = _dataObjects[dataObjectIndex]; string templateId = templateMap.id.Replace(TPL_PREFIX, TPL_NS.NamespaceName); List <RoleMap> propertyRoles = new List <RoleMap>(); XElement baseTemplateElement = new XElement(OWL_THING); StringBuilder baseValues = new StringBuilder(templateMap.id); List <RoleMap> classRoles = new List <RoleMap>(); baseTemplateElement.Add(new XElement(RDF_TYPE, new XAttribute(RDF_RESOURCE, templateId))); foreach (RoleMap roleMap in templateMap.roleMaps) { string roleId = roleMap.id.Substring(roleMap.id.IndexOf(":") + 1); XElement roleElement = new XElement(TPL_NS + roleId); switch (roleMap.type) { case RoleType.Possessor: roleElement.Add(new XAttribute(RDF_RESOURCE, classInstance)); baseTemplateElement.Add(roleElement); baseValues.Append(classIdentifier); break; case RoleType.FixedValue: string dataType = roleMap.dataType.Replace(XSD_PREFIX, XSD_NS.NamespaceName); roleElement.Add(new XAttribute(RDF_DATATYPE, dataType)); roleElement.Add(new XText(roleMap.value)); baseTemplateElement.Add(roleElement); baseValues.Append(roleMap.value); break; case RoleType.Reference: if (roleMap.classMap != null) { classRoles.Add(roleMap); } else { string value = GetReferenceRoleValue(roleMap); roleElement.Add(new XAttribute(RDF_RESOURCE, value)); baseTemplateElement.Add(roleElement); baseValues.Append(roleMap.value); } break; case RoleType.Property: case RoleType.DataProperty: case RoleType.ObjectProperty: if (String.IsNullOrEmpty(roleMap.propertyName)) { throw new Exception("No data property mapped to role [" + startClassName + "." + templateMap.name + "." + roleMap.name + "]"); } propertyRoles.Add(roleMap); break; } } if (propertyRoles.Count > 0) // property template { bool isTemplateValid = true; // template is not valid when value list uri is empty List <List <XElement> > multiPropertyElements = new List <List <XElement> >(); // create property elements foreach (RoleMap propertyRole in propertyRoles) { List <XElement> propertyElements = new List <XElement>(); multiPropertyElements.Add(propertyElements); string[] propertyParts = propertyRole.propertyName.Split('.'); string propertyName = propertyParts[propertyParts.Length - 1]; int lastDotPos = propertyRole.propertyName.LastIndexOf('.'); string objectPath = propertyRole.propertyName.Substring(0, lastDotPos); if (propertyParts.Length == 2) // direct property { string propertyValue = Convert.ToString(dataObject.GetPropertyValue(propertyName)); XElement propertyElement = CreatePropertyElement(propertyRole, propertyValue); if (propertyElement == null) { isTemplateValid = false; break; } propertyElements.Add(propertyElement); } else // related property { string key = objectPath + "." + dataObjectIndex; List <IDataObject> relatedObjects = null; if (!_relatedObjectsCache.TryGetValue(key, out relatedObjects)) { relatedObjects = GetRelatedObjects(propertyRole.propertyName, dataObject); _relatedObjectsCache.Add(key, relatedObjects); } if (classIdentifierHasRelatedProperty) // reference class identifier has related property { IDataObject relatedObject = relatedObjects[classIdentifierIndex]; string propertyValue = Convert.ToString(relatedObject.GetPropertyValue(propertyName)); XElement propertyElement = CreatePropertyElement(propertyRole, propertyValue); if (propertyElement == null) { isTemplateValid = false; break; } propertyElements.Add(propertyElement); } else // related property is property map { foreach (IDataObject relatedObject in relatedObjects) { string propertyValue = Convert.ToString(relatedObject.GetPropertyValue(propertyName)); XElement propertyElement = CreatePropertyElement(propertyRole, propertyValue); if (propertyElement == null) { isTemplateValid = false; break; } propertyElements.Add(propertyElement); } if (!isTemplateValid) { break; } } } } if (isTemplateValid) { // add property elements to template element(s) if (multiPropertyElements.Count > 0 && multiPropertyElements[0].Count > 0) { // enforce dotNetRDF to store/retrieve templates in order as expressed in RDF string hashPrefixFormat = Regex.Replace(multiPropertyElements[0].Count.ToString(), "\\d", "0") + "0"; for (int i = 0; i < multiPropertyElements[0].Count; i++) { XElement templateElement = new XElement(baseTemplateElement); _rdfXml.Add(templateElement); StringBuilder templateValue = new StringBuilder(baseValues.ToString()); for (int j = 0; j < multiPropertyElements.Count; j++) { XElement propertyElement = multiPropertyElements[j][i]; templateElement.Add(propertyElement); if (!String.IsNullOrEmpty(propertyElement.Value)) { templateValue.Append(propertyElement.Value); } else { templateValue.Append(propertyElement.Attribute(RDF_RESOURCE).Value); } } string hashCode = Utility.MD5Hash(templateValue.ToString()); hashCode = i.ToString(hashPrefixFormat) + hashCode.Substring(hashPrefixFormat.Length); templateElement.Add(new XAttribute(RDF_ABOUT, hashCode)); } } } } else if (classRoles.Count > 0) // relationship template with known class role { bool isTemplateValid = false; // template is valid when there is at least one class referernce identifier that is not null Dictionary <RoleMap, List <string> > relatedClassRoles = new Dictionary <RoleMap, List <string> >(); foreach (RoleMap classRole in classRoles) { bool refClassHasRelatedProperty; List <string> refClassIdentifiers = GetClassIdentifiers(classRole.classMap, dataObjectIndex, out refClassHasRelatedProperty); if (refClassHasRelatedProperty) { relatedClassRoles[classRole] = refClassIdentifiers; } else { string refClassIdentifier = refClassIdentifiers.First(); if (!String.IsNullOrEmpty(refClassIdentifier)) { isTemplateValid = true; baseValues.Append(refClassIdentifier); string roleId = classRole.id.Substring(classRole.id.IndexOf(":") + 1); XElement roleElement = new XElement(TPL_NS + roleId); roleElement.Add(new XAttribute(RDF_RESOURCE, _graphBaseUri + Utility.TitleCase(classRole.classMap.name) + "/" + refClassIdentifier)); baseTemplateElement.Add(roleElement); } ClassTemplateMap relatedClassTemplateMap = _graphMap.GetClassTemplateMap(classRole.classMap.id, classRole.classMap.index); if (relatedClassTemplateMap != null && relatedClassTemplateMap.classMap != null) { ProcessOutboundClass(dataObjectIndex, startClassName, startClassIdentifier, false, refClassIdentifiers, refClassHasRelatedProperty, relatedClassTemplateMap.classMap, relatedClassTemplateMap.templateMaps); } } } if (relatedClassRoles.Count > 0) { string refClassBaseValues = baseValues.ToString(); // enforce dotNetRDF to store/retrieve templates in order as expressed in RDF string hashPrefixFormat = Regex.Replace(relatedClassRoles.Count.ToString(), "\\d", "0") + "0"; foreach (var pair in relatedClassRoles) { RoleMap classRole = pair.Key; List <string> refClassIdentifiers = pair.Value; string roleId = classRole.id.Substring(classRole.id.IndexOf(":") + 1); string baseRelatedClassUri = _graphBaseUri + Utility.TitleCase(classRole.classMap.name) + "/"; for (int i = 0; i < refClassIdentifiers.Count; i++) { string refClassIdentifier = refClassIdentifiers[i]; if (!String.IsNullOrEmpty(refClassIdentifier)) { XElement refBaseTemplateElement = new XElement(baseTemplateElement); string hashCode = Utility.MD5Hash(refClassBaseValues + refClassIdentifier); hashCode = i.ToString(hashPrefixFormat) + hashCode.Substring(hashPrefixFormat.Length); refBaseTemplateElement.Add(new XAttribute(RDF_ABOUT, hashCode)); XElement roleElement = new XElement(TPL_NS + roleId); roleElement.Add(new XAttribute(RDF_RESOURCE, baseRelatedClassUri + refClassIdentifier)); refBaseTemplateElement.Add(roleElement); _rdfXml.Add(refBaseTemplateElement); } } ClassTemplateMap relatedClassTemplateMap = _graphMap.GetClassTemplateMap(classRole.classMap.id, classRole.classMap.index); if (relatedClassTemplateMap != null && relatedClassTemplateMap.classMap != null) { ProcessOutboundClass(dataObjectIndex, startClassName, startClassIdentifier, false, refClassIdentifiers, true, relatedClassTemplateMap.classMap, relatedClassTemplateMap.templateMaps); } } } else if (isTemplateValid) { string hashCode = Utility.MD5Hash(baseValues.ToString()); baseTemplateElement.Add(new XAttribute(RDF_ABOUT, hashCode)); _rdfXml.Add(baseTemplateElement); } } else // relationship template with no class role (e.g. primary classification template) { string hashCode = Utility.MD5Hash(baseValues.ToString()); baseTemplateElement.Add(new XAttribute(RDF_ABOUT, hashCode)); _rdfXml.Add(baseTemplateElement); } }
private void CreateTemplateElement(int dataObjectIndex, string startClassName, string startClassIdentifier, int classIdentifierIndex, XElement individualElement, TemplateMap templateMap, bool classIdentifierHasRelatedProperty) { IDataObject dataObject = _dataObjects[dataObjectIndex]; List <RoleMap> classRoles = new List <RoleMap>(); List <RoleMap> propertyRoles = new List <RoleMap>(); XElement baseTemplateElement = new XElement(_appNamespace + templateMap.name); baseTemplateElement.Add(new XAttribute(RDL_URI_ATTR, templateMap.id)); foreach (RoleMap roleMap in templateMap.roleMaps) { XElement roleElement = new XElement(_appNamespace + roleMap.name); switch (roleMap.type) { case RoleType.Possessor: baseTemplateElement.Add(new XAttribute(POSSESSOR_ATTR, roleMap.id)); break; case RoleType.Reference: if (roleMap.classMap != null) { classRoles.Add(roleMap); } else { roleElement.Add(new XAttribute(RDL_URI_ATTR, roleMap.id)); roleElement.Add(new XAttribute(REF_ATTR, roleMap.value)); baseTemplateElement.Add(roleElement); } break; case RoleType.FixedValue: roleElement.Add(new XAttribute(RDL_URI_ATTR, roleMap.id)); roleElement.Add(new XText(roleMap.value)); baseTemplateElement.Add(roleElement); break; case RoleType.Property: case RoleType.DataProperty: case RoleType.ObjectProperty: if (String.IsNullOrEmpty(roleMap.propertyName)) { throw new Exception("No data property mapped to role [" + startClassName + "." + templateMap.name + "." + roleMap.name + "]"); } propertyRoles.Add(roleMap); break; } } if (propertyRoles.Count > 0) // property template { bool isTemplateValid = true; // template is not valid when value list uri is empty List <List <XElement> > multiPropertyElements = new List <List <XElement> >(); // create property elements foreach (RoleMap propertyRole in propertyRoles) { List <XElement> propertyElements = new List <XElement>(); multiPropertyElements.Add(propertyElements); string[] propertyParts = propertyRole.propertyName.Split('.'); string propertyName = propertyParts[propertyParts.Length - 1]; int lastDotPos = propertyRole.propertyName.LastIndexOf('.'); string objectPath = propertyRole.propertyName.Substring(0, lastDotPos); if (propertyParts.Length == 2) // direct property { string propertyValue = Convert.ToString(dataObject.GetPropertyValue(propertyName)); XElement propertyElement = CreatePropertyElement(propertyRole, propertyValue); if (propertyElement == null) { isTemplateValid = false; break; } propertyElements.Add(propertyElement); } else // related property { string key = objectPath + "." + dataObjectIndex; List <IDataObject> relatedObjects = null; if (!_relatedObjectsCache.TryGetValue(key, out relatedObjects)) { relatedObjects = GetRelatedObjects(propertyRole.propertyName, dataObject); _relatedObjectsCache.Add(key, relatedObjects); } if (classIdentifierHasRelatedProperty) // reference class identifier has related property { IDataObject relatedObject = relatedObjects[classIdentifierIndex]; string propertyValue = Convert.ToString(relatedObject.GetPropertyValue(propertyName)); XElement propertyElement = CreatePropertyElement(propertyRole, propertyValue); if (propertyElement == null) { isTemplateValid = false; break; } propertyElements.Add(propertyElement); } else // related property is property map { foreach (IDataObject relatedObject in relatedObjects) { string propertyValue = Convert.ToString(relatedObject.GetPropertyValue(propertyName)); XElement propertyElement = CreatePropertyElement(propertyRole, propertyValue); if (propertyElement == null) { isTemplateValid = false; break; } propertyElements.Add(propertyElement); } if (!isTemplateValid) { break; } } } } if (isTemplateValid) { // add property elements to template element(s) if (multiPropertyElements.Count > 0 && multiPropertyElements[0].Count > 0) { for (int i = 0; i < multiPropertyElements[0].Count; i++) { XElement templateElement = new XElement(baseTemplateElement); individualElement.Add(templateElement); for (int j = 0; j < multiPropertyElements.Count; j++) { XElement propertyElement = multiPropertyElements[j][i]; templateElement.Add(propertyElement); } } } } } else if (classRoles.Count > 0) // reference template with known class role { bool isTemplateValid = false; // at least one class role identifier is not null or empty foreach (RoleMap classRole in classRoles) { XElement roleElement = new XElement(_appNamespace + classRole.name); ClassTemplateMap relatedClassTemplateMap = _graphMap.GetClassTemplateMap(classRole.classMap.id, classRole.classMap.index); bool refClassHasRelatedProperty; List <string> refClassIdentifiers = GetClassIdentifiers(classRole.classMap, dataObjectIndex, out refClassHasRelatedProperty); if (refClassIdentifiers.Count > 0 && !String.IsNullOrEmpty(refClassIdentifiers.First())) { isTemplateValid = true; roleElement.Add(new XAttribute(RDL_URI_ATTR, classRole.id)); baseTemplateElement.Add(roleElement); if (relatedClassTemplateMap != null && relatedClassTemplateMap.classMap != null) { ProcessOutboundClass(dataObjectIndex, startClassName, startClassIdentifier, false, refClassIdentifiers, refClassHasRelatedProperty, roleElement, relatedClassTemplateMap.classMap, relatedClassTemplateMap.templateMaps); } else { roleElement.Add(new XAttribute(REF_ATTR, "#" + refClassIdentifiers.First())); } } } if (isTemplateValid) { individualElement.Add(baseTemplateElement); } } else // reference template with no class role (e.g. primary classification template) { individualElement.Add(baseTemplateElement); } }