public override Exceptional<IObject> Aggregate(IEnumerable<DBObjectStream> myDBObjects, TypeAttribute myTypeAttribute, DBContext myDBContext, params Functions.ParameterValue[] myParameters) { var foundFirstMax = false; var aggregateResult = myTypeAttribute.GetADBBaseObjectType(myDBContext.DBTypeManager); foreach (var dbo in myDBObjects) { var attrResult = dbo.GetAttribute(myTypeAttribute, myTypeAttribute.GetDBType(myDBContext.DBTypeManager), myDBContext); if (attrResult.Failed()) { return attrResult; } var attr = attrResult.Value; if (attr != null && attr is ADBBaseObject && aggregateResult.IsValidValue((attr as ADBBaseObject).Value)) { if (foundFirstMax == false) { aggregateResult.Value = (attr as ADBBaseObject).Value; foundFirstMax = true; } else { if (aggregateResult.CompareTo((attr as ADBBaseObject).Value) < 0) { aggregateResult.Value = (attr as ADBBaseObject).Value; } } } else { return new Exceptional<IObject>(new Error_AggregateIsNotValidOnThisAttribute(myTypeAttribute.Name)); } } return new Exceptional<IObject>(aggregateResult); }
public override Exceptional<FuncParameter> Aggregate(IEnumerable<DBObjectStream> myDBObjects, TypeAttribute myTypeAttribute, DBContext myDBContext, params Functions.ParameterValue[] myParameters) { var aggregateResult = new DBDouble(0d); var total = 0UL; foreach (var dbo in myDBObjects) { var attr = dbo.GetAttribute(myTypeAttribute, myTypeAttribute.GetDBType(myDBContext.DBTypeManager), myDBContext); if (attr.Failed()) { return new Exceptional<FuncParameter>(attr); } if (attr.Value != null && attr.Value is ADBBaseObject && aggregateResult.IsValidValue((attr.Value as ADBBaseObject).Value)) { aggregateResult.Add((attr.Value as ADBBaseObject)); total++; } else { return new Exceptional<FuncParameter>(new Error_AggregateIsNotValidOnThisAttribute(myTypeAttribute.Name)); } } aggregateResult.Div(new DBUInt64(total)); return new Exceptional<FuncParameter>(new FuncParameter(aggregateResult)); }
public override Exceptional<IObject> Aggregate(IEnumerable<DBObjectStream> myDBObjects, TypeAttribute myTypeAttribute, DBContext myDBContext, params Functions.ParameterValue[] myParameters) { var aggregateResult = myTypeAttribute.GetADBBaseObjectType(myDBContext.DBTypeManager); foreach (var dbo in myDBObjects) { var attrResult = dbo.GetAttribute(myTypeAttribute, myTypeAttribute.GetDBType(myDBContext.DBTypeManager), myDBContext); if (attrResult.Failed()) { return attrResult; } var attr = attrResult.Value; if (attr != null && attr is ADBBaseObject && aggregateResult.IsValidValue((attr as ADBBaseObject).Value)) { aggregateResult.Add((attr as ADBBaseObject)); } else if (attr != null) // if null, this value will be skipped { return new Exceptional<IObject>(new Error_AggregateIsNotValidOnThisAttribute(myTypeAttribute.Name)); } } return new Exceptional<IObject>(aggregateResult); }
private IEnumerable<Exceptional<DBObjectStream>> GetReferenceObjects(DBObjectStream myStartingDBObject, TypeAttribute interestingAttributeEdge, GraphDBType myStartingDBObjectType, DBTypeManager myDBTypeManager) { if (interestingAttributeEdge.GetDBType(myDBTypeManager).IsUserDefined || interestingAttributeEdge.IsBackwardEdge) { switch (interestingAttributeEdge.KindOfType) { case KindsOfType.SingleReference: yield return LoadDBObjectStream(interestingAttributeEdge.GetDBType(myDBTypeManager), ((ASingleReferenceEdgeType)myStartingDBObject.GetAttribute(interestingAttributeEdge.UUID)).GetUUID()); break; case KindsOfType.SetOfReferences: if (interestingAttributeEdge.IsBackwardEdge) { //get backwardEdge var beStream = LoadDBBackwardEdgeStream(myStartingDBObjectType, myStartingDBObject.ObjectUUID); if (beStream.Failed()) { throw new GraphDBException(new Error_ExpressionGraphInternal(null, String.Format("Error while trying to get BackwardEdge of the DBObject: \"{0}\"", myStartingDBObject.ToString()))); } if (beStream.Value.ContainsBackwardEdge(interestingAttributeEdge.BackwardEdgeDefinition)) { foreach (var aBackwardEdgeObject in LoadListOfDBObjectStreams(interestingAttributeEdge.BackwardEdgeDefinition.TypeUUID, beStream.Value.GetBackwardEdgeUUIDs(interestingAttributeEdge.BackwardEdgeDefinition))) { yield return aBackwardEdgeObject; } } } else { foreach (var aDBOStream in LoadListOfDBObjectStreams(interestingAttributeEdge.GetDBType(myDBTypeManager), ((ASetOfReferencesEdgeType)myStartingDBObject.GetAttribute(interestingAttributeEdge.UUID)).GetAllReferenceIDs())) { yield return aDBOStream; } } break; case KindsOfType.SetOfNoneReferences: case KindsOfType.ListOfNoneReferences: default: throw new GraphDBException(new Error_ExpressionGraphInternal(new System.Diagnostics.StackTrace(true), String.Format("The attribute \"{0}\" has an invalid KindOfType \"{1}\"!", interestingAttributeEdge.Name, interestingAttributeEdge.KindOfType.ToString()))); } } else { throw new GraphDBException(new Error_ExpressionGraphInternal(new System.Diagnostics.StackTrace(true), String.Format("The attribute \"{0}\" is no reference attribute.", interestingAttributeEdge.Name))); } yield break; }
private ValueDefinition GetCorrectValueDefinition(TypeAttribute typeAttribute, GraphDBType graphDBType, ValueDefinition myValueDefinition, DBContext dbContext, SessionSettings mySessionsInfos) { if (typeAttribute.IsBackwardEdge) { return GetCorrectValueDefinition(dbContext.DBTypeManager.GetTypeByUUID(typeAttribute.BackwardEdgeDefinition.TypeUUID).GetTypeAttributeByUUID(typeAttribute.BackwardEdgeDefinition.AttrUUID), graphDBType, myValueDefinition, dbContext, mySessionsInfos); } else { if (typeAttribute is ASpecialTypeAttribute && typeAttribute.UUID == SpecialTypeAttribute_UUID.AttributeUUID) { //var uuid = SpecialTypeAttribute_UUID.ConvertToUUID(atomValue.Value.Value.ToString(), graphDBType, mySessionsInfos, dbContext.DBTypeManager); //if (uuid.Failed()) //{ // throw new GraphDBException(uuid.Errors); //} //return new AtomValue(new DBReference(uuid.Value)); return new ValueDefinition(new DBReference(ObjectUUID.FromString(myValueDefinition.Value.Value.ToString()))); } else if (!typeAttribute.GetDBType(dbContext.DBTypeManager).IsUserDefined) { return new ValueDefinition(GraphDBTypeMapper.GetADBBaseObjectFromUUID(typeAttribute.DBTypeUUID, myValueDefinition.Value)); } else { throw new GraphDBException(new Error_InvalidAttributeValue(typeAttribute.Name, myValueDefinition.Value)); } } }
/// <summary> /// Extracts the attribute from <paramref name="myDBObject"/>. /// </summary> /// <param name="myType"></param> /// <param name="myTypeAttribute"></param> /// <param name="myDBObject"></param> /// <param name="myLevelKey"></param> /// <returns></returns> private Object GetAttributeValue(GraphDBType myType, TypeAttribute myTypeAttribute, DBObjectStream myDBObject, EdgeList myLevelKey) { if (myTypeAttribute.TypeCharacteristics.IsBackwardEdge) { #region IsBackwardEdge EdgeKey edgeKey = myTypeAttribute.BackwardEdgeDefinition; var contBackwardExcept = myDBObject.ContainsBackwardEdge(edgeKey, _DBContext, _DBContext.DBObjectCache, myType); if (contBackwardExcept.Failed()) throw new GraphDBException(contBackwardExcept.IErrors); if (contBackwardExcept.Value) { var getBackwardExcept = myDBObject.GetBackwardEdges(edgeKey, _DBContext, _DBContext.DBObjectCache, myTypeAttribute.GetDBType(_DBContext.DBTypeManager)); if (getBackwardExcept.Failed()) throw new GraphDBException(getBackwardExcept.IErrors); return getBackwardExcept.Value; } #endregion } else if (myDBObject.HasAttribute(myTypeAttribute.UUID, myType)) { #region ELSE (!IsBackwardEdge) return myDBObject.GetAttribute(myTypeAttribute.UUID, myType, _DBContext); #endregion } return null; }
/// <summary> /// Resolves an attribute /// </summary> /// <param name="attrDefinition">The TypeAttribute</param> /// <param name="attributeValue">The attribute value, either a AListReferenceEdgeType or ASingleEdgeType or a basic object (Int, String, ...)</param> /// <param name="myDepth">The current depth defined by a setting or in the select</param> /// <param name="currentLvl">The current level (for recursive resolve)</param> /// <returns>List<Vertex> for user defined list attributes; Vertex for reference attributes, Object for basic type values </returns> private object ResolveAttributeValue(TypeAttribute attrDefinition, object attributeValue, Int64 myDepth, EdgeList myEdgeList, DBObjectStream mySourceDBObject, String reference, Boolean myUsingGraph) { #region attributeValue is not a reference type just return the value if (!((attributeValue is ASetOfReferencesEdgeType) || (attributeValue is ASingleReferenceEdgeType))) { return attributeValue; } #endregion #region get typeOfAttribute var typeOfAttribute = attrDefinition.GetDBType(_DBContext.DBTypeManager); // For backwardEdges, the baseType is the type of the DBObjects! if (attrDefinition.TypeCharacteristics.IsBackwardEdge) typeOfAttribute = _DBContext.DBTypeManager.GetTypeAttributeByEdge(attrDefinition.BackwardEdgeDefinition).GetRelatedType(_DBContext.DBTypeManager); #endregion #region Get levelKey and UsingGraph if (!(attrDefinition is ASpecialTypeAttribute)) { if (myEdgeList.Level == 0) { myEdgeList = new EdgeList(new EdgeKey(attrDefinition.RelatedGraphDBTypeUUID, attrDefinition.UUID)); } else { myEdgeList += new EdgeKey(attrDefinition.RelatedGraphDBTypeUUID, attrDefinition.UUID); } } // at some deeper level we could get into graph independend results. From this time, we can use the GUID index rather than asking the graph all the time if (myUsingGraph) { myUsingGraph = _ExpressionGraph.IsGraphRelevant(new LevelKey(myEdgeList.Edges, _DBContext.DBTypeManager), mySourceDBObject); } #endregion if (attributeValue is ASetOfReferencesEdgeType) { #region SetReference attribute -> return new Edge IEnumerable<Vertex> resultList = null; var edge = ((ASetOfReferencesEdgeType)attributeValue); if (myUsingGraph) { var dbos = _ExpressionGraph.Select(new LevelKey(myEdgeList.Edges, _DBContext.DBTypeManager), mySourceDBObject, true); resultList = GetVertices(typeOfAttribute, edge, dbos, myDepth, myEdgeList, reference, myUsingGraph); } else { resultList = GetVertices(typeOfAttribute, edge, myDepth, myEdgeList, reference, myUsingGraph); } return new Edge(null, resultList, typeOfAttribute.Name); #endregion } else if (attributeValue is ASingleReferenceEdgeType) { #region Single reference attributeValue = new Edge(null, (attributeValue as ASingleReferenceEdgeType).GetVertex(a => LoadAndResolveVertex(a, typeOfAttribute, myDepth, myEdgeList, reference, myUsingGraph)) , typeOfAttribute.Name); return attributeValue; #endregion } else { throw new GraphDBException(new Error_NotImplemented(new System.Diagnostics.StackTrace(true))); } }
private Edge GetNotResolvedBackwardEdgeReferenceAttributeValue(DBObjectStream myDBObject, TypeAttribute myTypeAttribute, EdgeKey edgeKey, EdgeList currentEdgeList, Boolean myUsingGraph, DBContext _DBContext) { IObject attrValue = null; if (myUsingGraph) { var interestingLevelKey = new LevelKey((currentEdgeList + new EdgeKey(myTypeAttribute.RelatedGraphDBTypeUUID, myTypeAttribute.UUID)).Edges, _DBContext.DBTypeManager); attrValue = new EdgeTypeSetOfReferences(_ExpressionGraph.SelectUUIDs(interestingLevelKey, myDBObject), myTypeAttribute.DBTypeUUID); } else { var attrValueException = myDBObject.GetBackwardEdges(edgeKey, _DBContext, _DBContext.DBObjectCache, myTypeAttribute.GetDBType(_DBContext.DBTypeManager)); if (attrValueException.Failed()) { throw new GraphDBException(attrValueException.IErrors); } attrValue = attrValueException.Value; } if (attrValue == null) { return null; } else if (!(attrValue is IReferenceEdge)) { throw new GraphDBException(new Error_InvalidEdgeType(attrValue.GetType(), typeof(IReferenceEdge))); } var readouts = new List<Vertex>(); var typeName = _DBContext.DBTypeManager.GetTypeByUUID(edgeKey.TypeUUID).Name; foreach (var reference in (attrValue as IReferenceEdge).GetAllReferenceIDs()) { var specialAttributes = new Dictionary<string, object>(); specialAttributes.Add(SpecialTypeAttribute_UUID.AttributeName, reference); specialAttributes.Add(SpecialTypeAttribute_TYPE.AttributeName, typeName); readouts.Add(new Vertex(specialAttributes)); } return new Edge(null, readouts, _DBContext.DBTypeManager.GetTypeAttributeByEdge(edgeKey).GetDBType(_DBContext.DBTypeManager).Name); }
/// <summary> /// Get the attribute value of <paramref name="myTypeAttribute"/> and calls GetNotResolvedReferenceEdgeAttributeValue with the edge /// </summary> /// <param name="myDBObjectStream"></param> /// <param name="myTypeAttribute"></param> /// <param name="myGraphDBType"></param> /// <param name="myCurrentEdgeList"></param> /// <param name="myUsingGraph"></param> /// <param name="myDBContext"></param> /// <returns></returns> private Edge GetNotResolvedReferenceAttributeValue(DBObjectStream myDBObjectStream, TypeAttribute myTypeAttribute, GraphDBType myGraphDBType, EdgeList myCurrentEdgeList, Boolean myUsingGraph, DBContext myDBContext) { IObject attrValue = null; if (myUsingGraph) { var interestingLevelKey = new LevelKey((myCurrentEdgeList + new EdgeKey(myGraphDBType.UUID, myTypeAttribute.UUID)).Edges, myDBContext.DBTypeManager); var interestingUUIDs = _ExpressionGraph.SelectUUIDs(interestingLevelKey, myDBObjectStream); attrValue = ((IReferenceEdge)myDBObjectStream.GetAttribute(myTypeAttribute.UUID, myGraphDBType, myDBContext)).GetNewInstance(interestingUUIDs, myGraphDBType.UUID); } else { attrValue = myDBObjectStream.GetAttribute(myTypeAttribute.UUID, myGraphDBType, myDBContext); } if (attrValue == null) { return null; } var typeName = myTypeAttribute.GetDBType(myDBContext.DBTypeManager).Name; return GetNotResolvedReferenceEdgeAttributeValue(attrValue as IReferenceEdge, myTypeAttribute.GetDBType(myDBContext.DBTypeManager), myDBContext); }
private GraphDBType GetTypeOfAttribute(GraphDBType currentType, TypeAttribute interestingAttribute) { switch (interestingAttribute.KindOfType) { case KindsOfType.SingleReference: return interestingAttribute.GetDBType(_DBContext.DBTypeManager); case KindsOfType.SetOfReferences: if (interestingAttribute.IsBackwardEdge) { return _DBContext.DBTypeManager.GetTypeByUUID(interestingAttribute.BackwardEdgeDefinition.TypeUUID); } else { return interestingAttribute.GetDBType(_DBContext.DBTypeManager); } case KindsOfType.SetOfNoneReferences: case KindsOfType.ListOfNoneReferences: case KindsOfType.SingleNoneReference: return interestingAttribute.GetDBType(_DBContext.DBTypeManager); default: throw new GraphDBException(new Error_NotImplemented(new System.Diagnostics.StackTrace())); } }
private String CreateGraphDDLOfAttributeDefinition(DumpFormats myDumpFormat, TypeAttribute myTypeAttribute, DBContext myDBContext) { if (myTypeAttribute.EdgeType != null) { return String.Concat(myTypeAttribute.EdgeType.GetGDDL(myTypeAttribute.GetDBType(myDBContext.DBTypeManager)), " ", myTypeAttribute.Name); } else { return String.Concat(myTypeAttribute.GetDBType(myDBContext.DBTypeManager).Name, " ", myTypeAttribute.Name); } }
/// <summary> /// Adds a new edge from the current DBObject to the destination defined by <paramref name="myObjectReference"/>. /// Do not forget to call Flush() after doing all changes! /// </summary> /// <param name="myEdgeAttribute">The edge attribute</param> /// <param name="myObjectReference">The destination DBObjectUUID</param> /// <param name="myEdgeParams">Optional parameters like weight, etc.</param> /// <returns></returns> public BulkInsertDBO AddEdge(TypeAttribute myEdgeAttribute, ObjectUUID myObjectReference, params ADBBaseObject[] myEdgeParams) { #region Currently only ListRefernceEdgeType is implemented if (!myEdgeAttribute.GetDBType(_DBContext.DBTypeManager).IsUserDefined) throw new GraphDBException(new Error_InvalidEdgeType(typeof(ASetOfReferencesEdgeType), typeof(ASingleReferenceEdgeType))); #endregion if (!_DBObjectStream.HasAttribute(myEdgeAttribute.UUID, myEdgeAttribute.GetRelatedType(_DBContext.DBTypeManager))) _DBObjectStream.AddAttribute(myEdgeAttribute.UUID, myEdgeAttribute.EdgeType.GetNewInstance()); if (myEdgeAttribute.KindOfType == KindsOfType.SetOfReferences) (_DBObjectStream.GetAttribute(myEdgeAttribute.UUID) as ASetOfReferencesEdgeType).Add(myObjectReference, myEdgeAttribute.DBTypeUUID, myEdgeParams); else (_DBObjectStream.GetAttribute(myEdgeAttribute.UUID) as ASingleReferenceEdgeType).Set(myObjectReference, myEdgeAttribute.DBTypeUUID, myEdgeParams); EdgesCount++; return this; }
/// <summary> /// Get the content of the SetRef as an edge /// </summary> /// <param name="typeAttribute"></param> /// <param name="dbContext"></param> /// <param name="validationType"></param> /// <returns></returns> public Exceptional<ASingleReferenceEdgeType> GetEdge(TypeAttribute typeAttribute, DBContext dbContext, GraphDBType validationType) { var retvalue = typeAttribute.EdgeType.GetNewInstance() as ASingleReferenceEdgeType; if (retvalue == null) { return new Exceptional<ASingleReferenceEdgeType>(new Error_InvalidEdgeType(typeAttribute.EdgeType.GetType(), typeof(ASingleReferenceEdgeType))); } if (_IsREFUUID) { return TupleDefinition.GetAsUUIDSingleEdge(dbContext, typeAttribute); } else { var dbos = GetCorrespondigDBObjects(typeAttribute.GetDBType(dbContext.DBTypeManager), dbContext, validationType); if (dbos.CountIsGreater(1)) { return new Exceptional<ASingleReferenceEdgeType>(new Error_TooManyElementsForEdge(retvalue, dbos.ULongCount()));//"Error while evaluating BinaryExpression on REF/REFERENCE. There are more than one references which is not possible in an REF/REFERENCE insert.")); } var dboToAdd = dbos.FirstOrDefault(); if (dboToAdd == null) { //NLOG: temporarily commented //Logger.Warn("REF/REFERENCE is null because of an expression without any results! Skip adding this attribute."); return new Exceptional<ASingleReferenceEdgeType>(new Error_ReferenceAssignment_EmptyValue(typeAttribute.Name)); } else if (dboToAdd.Failed()) { return new Exceptional<ASingleReferenceEdgeType>(dboToAdd); } else { retvalue.Set(dboToAdd.Value.ObjectUUID, typeAttribute.DBTypeUUID, Parameters); } } return new Exceptional<ASingleReferenceEdgeType>(retvalue); }