public override QueryResult Execute(IGraphDB myGraphDB, IGraphQL myGraphQL, GQLPluginManager myPluginManager, String myQuery, SecurityToken mySecurityToken, TransactionToken myTransactionToken) { _query = myQuery; var vertexType = myGraphDB.GetVertexType<IVertexType>( mySecurityToken, myTransactionToken, new RequestGetVertexType(_typeName), (stats, vType) => vType); _WhereExpression.Validate(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, vertexType); var expressionGraph = _WhereExpression.Calculon(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, new CommonUsageGraph(myGraphDB, mySecurityToken, myTransactionToken)); var toBeDeletedVertices = expressionGraph.Select( new LevelKey(vertexType.ID, myGraphDB, mySecurityToken, myTransactionToken), null, true); //TODO: do sth that is better than that: ew RequestDelete(new RequestGetVertices(_typeName, toBeDeletedVertices.Select(_ => _.VertexID))). return myGraphDB.Delete<QueryResult>( mySecurityToken, myTransactionToken, new RequestDelete(new RequestGetVertices(_typeName, toBeDeletedVertices.Select(_ => _.VertexID))).AddAttributes(_toBeDeletedAttributes), CreateQueryResult); }
public TResult GetVertexType <TResult>(sones.Library.Commons.Security.SecurityToken mySecurityToken, Int64 myTransactionToken, RequestGetVertexType myRequestGetVertexType, Converter.GetVertexTypeResultConverter <TResult> myOutputconverter) { return(_iGraphDB.GetVertexType <TResult>(mySecurityToken, myTransactionToken, myRequestGetVertexType, myOutputconverter)); }
public override IQueryResult Execute(IGraphDB myGraphDB, IGraphQL myGraphQL, GQLPluginManager myPluginManager, String myQuery, SecurityToken mySecurityToken, Int64 myTransactionToken) { var sw = Stopwatch.StartNew(); _query = myQuery; //prepare var vertexType = myGraphDB.GetVertexType <IVertexType>( mySecurityToken, myTransactionToken, new RequestGetVertexType(_SourceType), (stats, vtype) => vtype); //validate _Condition.Validate(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, vertexType); //calculate var expressionGraph = _Condition.Calculon(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, new CommonUsageGraph(myGraphDB, mySecurityToken, myTransactionToken), false); //extract var myToBeUpdatedVertices = expressionGraph.Select(new LevelKey(vertexType.ID, myGraphDB, mySecurityToken, myTransactionToken), null, true).ToList(); if (myToBeUpdatedVertices.Count > 0) { //update ProcessUpdate(myToBeUpdatedVertices, myGraphDB, myPluginManager, mySecurityToken, myTransactionToken); } sw.Stop(); return(GenerateResult(sw.Elapsed.TotalMilliseconds)); }
protected EdgeKey GetBackwardEdgeKey(LevelKey myPath, int desiredBELevel, IGraphDB myGraphDB, SecurityToken mySecurityToken, Int64 myTransactionToken) { #region data IVertexType tempType; IAttributeDefinition tempAttr; #endregion tempType = myGraphDB.GetVertexType <IVertexType>( mySecurityToken, myTransactionToken, new RequestGetVertexType(myPath.Edges[desiredBELevel].VertexTypeID), (stats, vertexType) => vertexType); tempAttr = tempType.GetAttributeDefinition(myPath.Edges[desiredBELevel].AttributeID); if (tempAttr.Kind != AttributeType.IncomingEdge) { return(new EdgeKey(tempType.ID, tempAttr.ID)); } else { IIncomingEdgeDefinition incomingEdgeDefinition = tempAttr as IIncomingEdgeDefinition; return(new EdgeKey(incomingEdgeDefinition.RelatedEdgeDefinition.SourceVertexType.ID, incomingEdgeDefinition.RelatedEdgeDefinition.ID)); } }
/// <summary> /// Validates the id chain if it is not already validated and returns all errors and warnings. /// </summary> public void Validate(GQLPluginManager myPluginManager, IGraphDB myGraphDB, SecurityToken mySecurityToken, Int64 myTransactionToken, Boolean allowUndefinedAttributes, params IVertexType[] validationTypes) { if (IsValidated) { return; } var listOfRefs = new Dictionary <String, IVertexType>(); if (validationTypes == null || validationTypes.Count() == 0) { foreach (var type in _References) { var vertexType = myGraphDB.GetVertexType <IVertexType>( mySecurityToken, myTransactionToken, new GraphDB.Request.RequestGetVertexType(type.TypeName), (stats, theVertexType) => theVertexType); listOfRefs.Add(type.Reference, vertexType); } } else { foreach (var type in validationTypes) { listOfRefs.Add(type.Name, type); } } Validate(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, listOfRefs, allowUndefinedAttributes); }
/// <summary> /// Creates the request for the graphdb /// </summary> /// <returns>The created vertex</returns> private RequestInsertVertex CreateRequest(GQLPluginManager myPluginManager, IGraphDB myGraphDB, SecurityToken mySecurityToken, Int64 myTransactionToken) { #region data var result = new RequestInsertVertex(_TypeName); var vertexType = myGraphDB.GetVertexType <IVertexType>( mySecurityToken, myTransactionToken, new RequestGetVertexType(_TypeName), (stats, vtype) => vtype); #endregion if (_AttributeAssignList != null) { foreach (var aAttributeDefinition in _AttributeAssignList) { ProcessAAttributeDefinition(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, vertexType, aAttributeDefinition, ref result); } } return(result); }
private void AddVertexTypeAndAttributesRecursivly(IGraphDB myGraphDB, SecurityToken mySecurityToken, Int64 myTransactionToken, IVertexType type, ref HashSet <IVertexType> types) { if (!type.IsUserDefined) { return; } if (type.HasParentType) { if (!types.Contains(type.ParentVertexType)) { types.Add(type.ParentVertexType); foreach (var attr in (type.GetAttributeDefinitions(false)).Where(attrDef => attrDef.Kind == AttributeType.Property)) { var attrType = myGraphDB.GetVertexType <IVertexType>(mySecurityToken, myTransactionToken, new RequestGetVertexType(attr.ID), (stats, vertex) => vertex); AddVertexTypeAndAttributesRecursivly(myGraphDB, mySecurityToken, myTransactionToken, attrType, ref types); } } } }
public override IQueryResult Execute(IGraphDB myGraphDB, IGraphQL myGraphQL, GQLPluginManager myPluginManager, String myQuery, SecurityToken mySecurityToken, Int64 myTransactionToken) { _query = myQuery; var vertexType = myGraphDB.GetVertexType <IVertexType>( mySecurityToken, myTransactionToken, new RequestGetVertexType(_typeName), (stats, vType) => vType); _WhereExpression.Validate(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, vertexType); var expressionGraph = _WhereExpression.Calculon(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, new CommonUsageGraph(myGraphDB, mySecurityToken, myTransactionToken)); var toBeDeletedVertices = expressionGraph.Select( new LevelKey(vertexType.ID, myGraphDB, mySecurityToken, myTransactionToken), null, true); //TODO: do sth that is better than that: ew RequestDelete(new RequestGetVertices(_typeName, toBeDeletedVertices.Select(_ => _.VertexID))). return(myGraphDB.Delete <IQueryResult>( mySecurityToken, myTransactionToken, new RequestDelete(new RequestGetVertices(_typeName, toBeDeletedVertices.Select(_ => _.VertexID))).AddAttributes(_toBeDeletedAttributes), CreateQueryResult)); }
public LevelKey(IEnumerable <EdgeKey> myEdgeKey, IGraphDB myGraphDB, SecurityToken mySecurityToken, Int64 myTransactionToken) { Edges = new List <EdgeKey>(); foreach (var aEdgeKey in myEdgeKey) { if (aEdgeKey.IsAttributeSet) { var vertexType = myGraphDB.GetVertexType <IVertexType> (mySecurityToken, myTransactionToken, new RequestGetVertexType(aEdgeKey.VertexTypeID), (stats, type) => type); var attribute = vertexType.GetAttributeDefinition(aEdgeKey.AttributeID); if (attribute != null && attribute.Kind != AttributeType.Property) { //so there is an edge Edges.Add(aEdgeKey); Level++; AddHashCodeFromSingleEdge(ref _hashcode, aEdgeKey); } else { if (Level == 0) { var newEdgeKey = new EdgeKey(aEdgeKey.VertexTypeID); Edges.Add(newEdgeKey); AddHashCodeFromSingleEdge(ref _hashcode, newEdgeKey); break; } else { break; } } } else { if (Level == 0) { Edges.Add(aEdgeKey); AddHashCodeFromSingleEdge(ref _hashcode, aEdgeKey); break; } else { break; } } } }
public LevelKey(IEnumerable<EdgeKey> myEdgeKey, IGraphDB myGraphDB, SecurityToken mySecurityToken, TransactionToken myTransactionToken) { Edges = new List<EdgeKey>(); foreach (var aEdgeKey in myEdgeKey) { if (aEdgeKey.IsAttributeSet) { var vertexType = myGraphDB.GetVertexType<IVertexType> (mySecurityToken, myTransactionToken, new RequestGetVertexType(aEdgeKey.VertexTypeID), (stats, type) => type); var attribute = vertexType.GetAttributeDefinition(aEdgeKey.AttributeID); if (attribute != null && attribute.Kind != AttributeType.Property) { //so there is an edge Edges.Add(aEdgeKey); Level++; AddHashCodeFromSingleEdge(ref _hashcode, aEdgeKey); } else { if (Level == 0) { var newEdgeKey = new EdgeKey(aEdgeKey.VertexTypeID); Edges.Add(newEdgeKey); AddHashCodeFromSingleEdge(ref _hashcode, newEdgeKey); break; } else { break; } } } else { if (Level == 0) { Edges.Add(aEdgeKey); AddHashCodeFromSingleEdge(ref _hashcode, aEdgeKey); break; } else { break; } } } }
public List <TypeWithProperty> StringParser(String current_string, IGraphDB myDB, SecurityToken mySecurityToken, Int64 myTransactionToken) { current_string = current_string.Replace(" ", ""); List <TypeWithProperty> list = new List <TypeWithProperty>(); bool endFlag = false; int EndPos = 0; do { EndPos = current_string.IndexOf(','); if (EndPos == -1) { EndPos = current_string.Length; endFlag = true; } var typeVertexString = current_string.Substring(0, current_string.IndexOf('.')); IVertexType typeVertex = null; try { typeVertex = myDB.GetVertexType <IVertexType>( mySecurityToken, myTransactionToken, new sones.GraphDB.Request.RequestGetVertexType(typeVertexString), (statistics, type) => type); } catch { throw new InvalidFunctionParameterException("edgeType", "Object reference not set to an instance of an object.", "null"); } var propertyIDString = current_string.Substring(current_string.IndexOf('.') + 1, EndPos - current_string.IndexOf('.') - 1); var property = typeVertex.GetPropertyDefinition(propertyIDString); if (property == null) { throw new InvalidFunctionParameterException("Property", "Property: " + propertyIDString + " not exist in VertexType:" + typeVertexString, "null"); } TypeWithProperty value = new TypeWithProperty(); value.PropertyDefinition = property; value.Type = typeVertex; if (!list.Contains(value)) { list.Add(value); } if (!endFlag) { current_string = current_string.Substring(EndPos + 1); } }while (endFlag != true); return(list); }
public List<TypeWithProperty> StringParser(String current_string, IGraphDB myDB, SecurityToken mySecurityToken, Int64 myTransactionToken) { current_string = current_string.Replace(" ",""); List<TypeWithProperty> list = new List<TypeWithProperty>(); bool endFlag = false; int EndPos = 0; do { EndPos = current_string.IndexOf(','); if (EndPos == -1) { EndPos = current_string.Length; endFlag = true; } var typeVertexString = current_string.Substring(0, current_string.IndexOf('.')); IVertexType typeVertex = null; try { typeVertex = myDB.GetVertexType<IVertexType>( mySecurityToken, myTransactionToken, new sones.GraphDB.Request.RequestGetVertexType(typeVertexString), (statistics, type) => type); } catch { throw new InvalidFunctionParameterException("edgeType", "Object reference not set to an instance of an object.", "null"); } var propertyIDString = current_string.Substring(current_string.IndexOf('.') + 1, EndPos - current_string.IndexOf('.') - 1); var property = typeVertex.GetPropertyDefinition(propertyIDString); if (property==null) throw new InvalidFunctionParameterException("Property", "Property: " + propertyIDString + " not exist in VertexType:" + typeVertexString, "null"); TypeWithProperty value = new TypeWithProperty(); value.propertyDifinition = property; value.type = typeVertex; if (!list.Contains(value)) list.Add(value); if (!endFlag) current_string = current_string.Substring(EndPos + 1); } while (endFlag != true); return list; }
/// <summary> /// Resolves the TypeReferenceDefinition list to a dictionary of reference (coming from the FROM) and corresponding type /// </summary> private Dictionary <String, IVertexType> ResolveTypes(List <TypeReferenceDefinition> myTypeList, SecurityToken mySecurityToken, Int64 myTransactionToken) { var typeList = new Dictionary <String, IVertexType>(); foreach (var aType in myTypeList) { var t = _graphdb.GetVertexType( mySecurityToken, myTransactionToken, new RequestGetVertexType(aType.TypeName), (stats, type) => type); if (t == null) { throw new VertexTypeDoesNotExistException(aType.TypeName, String.Empty); } typeList.Add(aType.Reference, t); } return(typeList); }
public override IQueryResult Execute(IGraphDB myGraphDB, IGraphQL myGraphQL, GQLPluginManager myPluginManager, String myQuery, SecurityToken mySecurityToken, Int64 myTransactionToken) { var sw = Stopwatch.StartNew(); IQueryResult result = null; _query = myQuery; String myAction = ""; IEnumerable <IVertex> myToBeUpdatedVertices = null; //prepare var vertexType = myGraphDB.GetVertexType <IVertexType>( mySecurityToken, myTransactionToken, new RequestGetVertexType(_Type), (stats, vtype) => vtype); if (_WhereExpression != null) { //validate _WhereExpression.Validate(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, vertexType); //calculate var expressionGraph = _WhereExpression.Calculon(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, new CommonUsageGraph(myGraphDB, mySecurityToken, myTransactionToken), false); //extract myToBeUpdatedVertices = expressionGraph.Select(new LevelKey(vertexType.ID, myGraphDB, mySecurityToken, myTransactionToken), null, true).ToList(); } if (myToBeUpdatedVertices != null && myToBeUpdatedVertices.Count() > 0) { //update result = ProcessUpdate(myToBeUpdatedVertices, myGraphDB, myPluginManager, mySecurityToken, myTransactionToken); myAction = "Updated"; } else { //insert result = ProcessInsert(myGraphDB, myPluginManager, mySecurityToken, myTransactionToken); myAction = "Inserted"; } if (result.Error != null) { throw result.Error; } sw.Stop(); return(GenerateResult(sw.Elapsed.TotalMilliseconds, result, myAction)); }
/// <summary> /// <seealso cref=" ADescribeDefinition"/> /// </summary> public override QueryResult GetResult(GQLPluginManager myPluginManager, IGraphDB myGraphDB, SecurityToken mySecurityToken, TransactionToken myTransactionToken) { var resultingVertices = new List<IVertexView>(); ASonesException error = null; if (!String.IsNullOrEmpty(_TypeName)) { #region Specific type var request = new RequestGetVertexType(_TypeName); var type = myGraphDB.GetVertexType<IVertexType>(mySecurityToken, myTransactionToken, request, (stats, vertexType) => vertexType); if (type != null) { resultingVertices = new List<IVertexView>() { (GenerateOutput(type, 1)) }; } else { error = new VertexTypeDoesNotExistException(_TypeName, ""); } #endregion } else { #region All types foreach (var type in myGraphDB.GetAllVertexTypes<IEnumerable<IVertexType>>(mySecurityToken, myTransactionToken, new RequestGetAllVertexTypes(), (stats, vertexTypes) => vertexTypes)) { resultingVertices.Add(GenerateOutput(type)); } #endregion } if (error != null) return new QueryResult("", SonesGQLConstants.GQL, 0L, ResultType.Failed, resultingVertices, error); else return new QueryResult("", SonesGQLConstants.GQL, 0L, ResultType.Successful, resultingVertices); }
private IEnumerable <IVertexType> GetVertexTypes(IGraphDB myGraphDB, SecurityToken mySecurityToken, Int64 myTransactionToken, IEnumerable <String> myTypes) { #region GetTypeToDump IEnumerable <IVertexType> typesToDump = new List <IVertexType>(); if (myTypes.IsNullOrEmpty()) { typesToDump = myGraphDB.GetAllVertexTypes(mySecurityToken, myTransactionToken, new RequestGetAllVertexTypes(), (stats, vertexTypes) => vertexTypes) .Where(_ => _.IsUserDefined); } else { var typesToDumpHash = new HashSet <IVertexType>(); foreach (var stringType in myTypes) { var type = myGraphDB.GetVertexType(mySecurityToken, myTransactionToken, new RequestGetVertexType(stringType), (stats, vertexType) => vertexType); if (type == null) { throw new TypeDoesNotExistException(stringType, ""); } //typesToDumpHash.UnionWith(myDBContext.DBTypeManager.GetAllParentTypes(type, true, false)); AddVertexTypeAndAttributesRecursivly(myGraphDB, mySecurityToken, myTransactionToken, type, ref typesToDumpHash); } typesToDump = typesToDumpHash.ToList(); } #endregion return(typesToDump); }
/// <summary> /// <seealso cref=" ADescribeDefinition"/> /// </summary> public override IEnumerable <IVertexView> GetResult(GQLPluginManager myPluginManager, IGraphDB myGraphDB, SecurityToken mySecurityToken, Int64 myTransactionToken) { var resultingVertices = new List <IVertexView>(); if (!String.IsNullOrEmpty(_TypeName)) { #region Specific type var request = new RequestGetVertexType(_TypeName); var type = myGraphDB.GetVertexType <IVertexType>(mySecurityToken, myTransactionToken, request, (stats, vertexType) => vertexType); if (type != null) { resultingVertices = new List <IVertexView>() { (GenerateOutput(type, 1)) }; } else { throw new VertexTypeDoesNotExistException(_TypeName, ""); } #endregion } else { #region All types foreach (var type in myGraphDB.GetAllVertexTypes <IEnumerable <IVertexType> >(mySecurityToken, myTransactionToken, new RequestGetAllVertexTypes(), (stats, vertexTypes) => vertexTypes)) { resultingVertices.Add(GenerateOutput(type)); } #endregion } return(resultingVertices); }
/// <summary> /// <seealso cref=" ADescribeDefinition"/> /// </summary> public override IEnumerable<IVertexView> GetResult(GQLPluginManager myPluginManager, IGraphDB myGraphDB, SecurityToken mySecurityToken, Int64 myTransactionToken) { var resultingVertices = new List<IVertexView>(); if (!String.IsNullOrEmpty(_TypeName)) { #region Specific type var request = new RequestGetVertexType(_TypeName); var type = myGraphDB.GetVertexType<IVertexType>(mySecurityToken, myTransactionToken, request, (stats, vertexType) => vertexType); if (type != null) { resultingVertices = new List<IVertexView>() { (GenerateOutput(type, 1)) }; } else { throw new VertexTypeDoesNotExistException(_TypeName, ""); } #endregion } else { #region All types foreach (var type in myGraphDB.GetAllVertexTypes<IEnumerable<IVertexType>>(mySecurityToken, myTransactionToken, new RequestGetAllVertexTypes(), (stats, vertexTypes) => vertexTypes)) { resultingVertices.Add(GenerateOutput(type)); } #endregion } return resultingVertices; }
public override IQueryResult Execute(IGraphDB myGraphDB, IGraphQL myGraphQL, GQLPluginManager myPluginManager, String myQuery, SecurityToken mySecurityToken, Int64 myTransactionToken) { _query = myQuery; //prepare var vertexType = myGraphDB.GetVertexType <IVertexType>( mySecurityToken, myTransactionToken, new RequestGetVertexType(_TypeName), (stats, vtype) => vtype); //validate _whereExpression.Validate(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, vertexType); //calculate var expressionGraph = _whereExpression.Calculon(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, new CommonUsageGraph(myGraphDB, mySecurityToken, myTransactionToken), false); //extract var myToBeUpdatedVertices = expressionGraph.Select(new LevelKey(vertexType.ID, myGraphDB, mySecurityToken, myTransactionToken), null, true).ToList(); switch (myToBeUpdatedVertices.Count) { case 0: //insert throw new ReplaceException("There are no vertices that match the where expression, so it's not possible to execute a Replace statement. Try using InsertOrUpdate."); case 1: //delete ProcessDelete(myToBeUpdatedVertices[0], myGraphDB, myPluginManager, mySecurityToken, myTransactionToken); //insert return(ProcessInsert(myGraphDB, myPluginManager, mySecurityToken, myTransactionToken)); default: //error throw new NotImplementedQLException("It's currenty not implemented to InsertOrReplace more than one vertex"); } }
public override QueryResult Execute(IGraphDB myGraphDB, IGraphQL myGraphQL, GQLPluginManager myPluginManager, String myQuery, SecurityToken mySecurityToken, Int64 myTransactionToken) { _query = myQuery; //prepare var vertexType = myGraphDB.GetVertexType<IVertexType>( mySecurityToken, myTransactionToken, new RequestGetVertexType(_TypeName), (stats, vtype) => vtype); //validate _whereExpression.Validate(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, vertexType); //calculate var expressionGraph = _whereExpression.Calculon(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, new CommonUsageGraph(myGraphDB, mySecurityToken, myTransactionToken), false); //extract var myToBeUpdatedVertices = expressionGraph.Select(new LevelKey(vertexType.ID, myGraphDB, mySecurityToken, myTransactionToken), null, true).ToList(); switch (myToBeUpdatedVertices.Count) { case 0: //insert throw new ReplaceException("There are no vertices that match the where expression, so it's not possible to execute a Replace statement. Try using InsertOrUpdate."); case 1: //delete ProcessDelete(myToBeUpdatedVertices[0], myGraphDB, myPluginManager, mySecurityToken, myTransactionToken); //insert return ProcessInsert(myGraphDB, myPluginManager, mySecurityToken, myTransactionToken); default: //error throw new NotImplementedQLException("It's currenty not implemented to InsertOrReplace more than one vertex"); } }
private void Helper(IGraphDB graph, SecurityToken sec, Int64 trans, List <TypeWithProperty> type_property_object, List <Tuple <long, long> > PATHShort) { List <Tuple <String, String> > output = new List <Tuple <string, string> >(); this._stringPath.Clear(); var StartTypeLong = PATHShort.Find(x => x.Item2 == 0); var StartType = graph.GetVertexType(sec, trans, new sones.GraphDB.Request.RequestGetVertexType(StartTypeLong.Item1), (statistics, type) => type); this._stringPath.Add(Tuple.Create("Starting VertexType for SELECT", StartType.Name)); String str = StartType.Name; foreach (TypeWithProperty value in type_property_object.Where(x => x.Type.ID == StartType.ID)) { this._stringPath.Add(Tuple.Create(StartType.Name + '.' + value.PropertyDefinition.Name, StartType.Name + '.' + value.PropertyDefinition.Name)); } Recurs(str, StartType, type_property_object, PATHShort); }
public override FuncParameter ExecFunc(IAttributeDefinition myAttributeDefinition, Object myCallingObject, IVertex myStartVertex, IGraphDB myGraphDB, SecurityToken mySecurityToken, Int64 myTransactionToken, params FuncParameter[] myParams) { #region initialize data var graph = myGraphDB; // The edge we starting of (e.g. Friends) var typeAttribute = myAttributeDefinition; if (myStartVertex == null) { throw new InvalidFunctionParameterException("StartVertex", "Vertex that represents the start vertex", "null"); } //set the start node var startNode = myStartVertex; if ((myParams[0].Value as IEnumerable <IVertex>) == null) { throw new InvalidFunctionParameterException("TargetVertex", "Set of vertices that represents the target vertices", "null"); } //set the target node var targetNode = (myParams[0].Value as IEnumerable <IVertex>).First(); //set the maximum depth UInt64 maxDepth = Convert.ToUInt64(myParams[1].Value); //set the maximum path length UInt64 maxPathLength = Convert.ToUInt64(myParams[2].Value); //mark if only the shortest path should be searched bool onlyShortestPath = Convert.ToBoolean(myParams[3].Value); //mark if all paths should be searched bool allPaths = Convert.ToBoolean(myParams[4].Value); if (!onlyShortestPath && !allPaths) { allPaths = true; } bool useBidirectionalBFS = false; if (myParams.GetLength(0) == 6) { //mark if the BidirectionalBFS should be used useBidirectionalBFS = Convert.ToBoolean(myParams[5].Value); } var vertexType = myGraphDB.GetVertexType <IVertexType>(mySecurityToken, myTransactionToken, new RequestGetVertexType(startNode.VertexTypeID), (stats, type) => type); if (vertexType == null) { throw new InvalidFunctionParameterException("StartVertexType", "VertexType that represents the start vertex type not found", startNode.VertexTypeID); } #endregion #region check correctness of parameters //check if values are correct if (maxDepth < 1) { throw new InvalidFunctionParameterException("maxDepth", ">= 1", maxDepth.ToString()); } if (maxPathLength < 2) { throw new InvalidFunctionParameterException("maxPathLength", ">= 2", maxPathLength.ToString()); } #endregion #region call graph function HashSet <List <Tuple <long, long> > > paths = null; //call BFS find methods if (useBidirectionalBFS) { paths = new BidirectionalBFS().Find(typeAttribute, vertexType, startNode, targetNode, onlyShortestPath, allPaths, maxDepth, maxPathLength); } else { paths = new BFS().Find(typeAttribute, vertexType, startNode, targetNode, onlyShortestPath, allPaths, maxDepth, maxPathLength); } #endregion #region create output if (paths != null) { #region create outputted views List <List <Tuple <long, long> > .Enumerator> enumerators = new List <List <Tuple <long, long> > .Enumerator>(); foreach (var path in paths) { enumerators.Add(path.GetEnumerator()); } var view = GenerateVertexView(enumerators); #endregion return(new FuncParameter(view)); } else { return(new FuncParameter(new VertexView(null, null))); } #endregion }
/// <summary> /// Algorithm Dijkstra for find all shortest path from start Vertex to all others Vertices /// </summary> /// <param name="myAttributeDefinition"></param> /// <param name="myCallingObject"></param> /// <param name="myStartVertex"></param> /// <param name="myGraphDB"></param> /// <param name="maxDepth"></param> /// <param name="mySecurityToken"></param> /// <param name="myTransactionToken"></param> /// <returns></returns> private Tuple<IVertex, DataDijkstra> FindShortPathToAll(IAttributeDefinition myAttributeDefinition, Object myCallingObject, IVertex myStartVertex, IGraphDB myGraphDB, UInt64 maxDepth, SecurityToken mySecurityToken, Int64 myTransactionToken) { if (myStartVertex == null) throw new InvalidFunctionParameterException("StartVertex", "Vertex that represents the start vertex", "null"); //set the start node var currentVertex = myStartVertex; var myType = myGraphDB.GetVertexType<IVertexType>(mySecurityToken, myTransactionToken, new sones.GraphDB.Request.RequestGetVertexType(myAttributeDefinition.RelatedType.Name), (statistics, type) => type); var myEdgeType = myAttributeDefinition.ID; var hasProperty = myType.GetOutgoingEdgeDefinition(myAttributeDefinition.Name).InnerEdgeType.HasProperty("Weight"); long myPropertyID = 0; if (hasProperty == true) { myPropertyID = myType.GetOutgoingEdgeDefinition(myAttributeDefinition.Name).InnerEdgeType.GetPropertyDefinition("Weight").ID; } var edgeTypeDifinition = myType.GetOutgoingEdgeDefinition(myAttributeDefinition.Name); DataDijkstra lists = new DataDijkstra(); BufferDijkstra buffer = new BufferDijkstra(); buffer.Add(myStartVertex, 0, 0); lists.Add(currentVertex, 0, 0, null,null, currentVertex); double currentVertexDistance = 0; ulong currentVertexDepth = 0; while (buffer.Count != 0) { if (currentVertexDepth < maxDepth) { var hyperEdge = currentVertex.GetOutgoingHyperEdge(myEdgeType); if (hyperEdge != null) { var singleEdge = hyperEdge.GetAllEdges(); for (int iCount = 0; iCount < singleEdge.Count(); iCount++) { var TargetVertex = singleEdge.ElementAt(iCount).GetTargetVertex(); double current_distance; if (hasProperty == true) { current_distance = Math.Abs(singleEdge.ElementAt(iCount).GetProperty<double>(myPropertyID)); } else { current_distance = 1; } var current_singleEdge = singleEdge.ElementAt(iCount); var TargetVertexID = lists.GetElement(TargetVertex.VertexID); if (TargetVertexID == null) { buffer.Add(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1); lists.Add(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1, current_singleEdge, edgeTypeDifinition, currentVertex); } else { if (currentVertexDistance + current_distance < TargetVertexID.Item2) { buffer.Set(TargetVertexID.Item2,TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1); lists.Set(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1, current_singleEdge, edgeTypeDifinition, currentVertex); } else if (currentVertexDistance + current_distance == TargetVertexID.Item2 && currentVertexDepth + 1 < TargetVertexID.Item3) { buffer.Set(TargetVertexID.Item2,TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1); lists.Set(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1, current_singleEdge, edgeTypeDifinition, currentVertex); } } } //delate from Buffer Vertex and any information } } buffer.Remove(currentVertexDistance,currentVertex.VertexID); //Minimum in distance from Buffer if (buffer.Count != 0) { var minVertex = buffer.Min(); currentVertex = minVertex.Item1; currentVertexDistance = minVertex.Item2; currentVertexDepth = minVertex.Item3; } } var result = Tuple.Create(myStartVertex, lists); buffer.Clear(); lists.Clear(); buffer = null; lists = null; return result; }
public override FuncParameter ExecFunc(IAttributeDefinition myAttributeDefinition, Object myCallingObject, IVertex myStartVertex, IGraphDB myGraphDB, SecurityToken mySecurityToken, Int64 myTransactionToken, params FuncParameter[] myParams) { #region initialization if (myStartVertex == null) throw new InvalidFunctionParameterException("StartVertex", "Vertex that represents the start vertex", "null"); //set the start node var currentVertex = myStartVertex; if ((myParams[0].Value as IEnumerable<IVertex>).First() == null) throw new InvalidFunctionParameterException("EndVertex", "Set of vertices that represents the target vertices", "null"); //set the target node var endVertex = (myParams[0].Value as IEnumerable<IVertex>).First(); //set the maximum depth if (myAttributeDefinition == null) throw new InvalidFunctionParameterException("EdgeType ", "Edge type not found", "faild"); UInt64 maxDepth = Convert.ToUInt64(myParams[1].Value); if (maxDepth < 1) throw new InvalidFunctionParameterException("MaxDepth", "Max depth to low", maxDepth.ToString()); var myType = myGraphDB.GetVertexType<IVertexType>( mySecurityToken, myTransactionToken, new sones.GraphDB.Request.RequestGetVertexType(myAttributeDefinition.RelatedType.Name), (statistics, type) => type); var myEdgeType = myAttributeDefinition.ID; var hasProperty = myType.GetOutgoingEdgeDefinition(myAttributeDefinition.Name).InnerEdgeType.HasProperty("Weight"); long myPropertyID = 0; if (hasProperty == true) { myPropertyID = myType.GetOutgoingEdgeDefinition(myAttributeDefinition.Name).InnerEdgeType.GetPropertyDefinition("Weight").ID; } Dictionary<IOutgoingEdgeDefinition, Tuple<bool, long>> edgeTypeID = new Dictionary<IOutgoingEdgeDefinition, Tuple<bool, long>>(); var edgeType = myParams[2].Value.ToString(); if (edgeType == "") { edgeTypeID.Add(myType.GetOutgoingEdgeDefinition(myAttributeDefinition.Name), Tuple.Create(hasProperty, myPropertyID)); } else { if (edgeType.Contains("all edgeType")) { edgeTypeID = this.AllEdgeWithTargetVertex(myType); } else { edgeTypeID = this.StringParser(edgeType, myType); if (!edgeTypeID.ContainsKey(myType.GetOutgoingEdgeDefinition(myAttributeDefinition.Name))) edgeTypeID.Add(myType.GetOutgoingEdgeDefinition(myAttributeDefinition.Name), Tuple.Create(hasProperty, myPropertyID)); } } List<UInt64> depthBuffer = new List<UInt64>(); List<Tuple<ISingleEdge, IOutgoingEdgeDefinition>> edgeBuffer = new List<Tuple<ISingleEdge, IOutgoingEdgeDefinition>>(); List<double> distanceBuffer = new List<double>(); List<IVertex> VertexBuffer = new List<IVertex>(); BufferDijkstra buf = new BufferDijkstra(); DataDijkstra lists = new DataDijkstra(); buf.Add(myStartVertex, 0, 0); lists.Add(currentVertex, 0, 0, null,null, currentVertex); bool endVertexFlag = false; #endregion #region Dijkstra algorithm double currentVertexDistance = 0; ulong currentVertexDepth = 0; double endVertexDistance = 0; ulong endvertexDepth = 0; Stopwatch clock = new Stopwatch(); clock.Start(); while (buf.Count != 0) { if (currentVertexDepth < maxDepth) { List<Tuple<ISingleEdge, IOutgoingEdgeDefinition>> AllsingleEdge = new List<Tuple<ISingleEdge, IOutgoingEdgeDefinition>>(); foreach (IOutgoingEdgeDefinition id in edgeTypeID.Keys) { var hyperEdge = currentVertex.GetOutgoingHyperEdge(id.ID); if (hyperEdge != null) { var singleEdge = hyperEdge.GetAllEdges(); foreach (ISingleEdge value in singleEdge) { AllsingleEdge.Add(Tuple.Create(value,id)); } } } if (AllsingleEdge != null) { for (int iCount = 0; iCount < AllsingleEdge.Count(); iCount++) { var elementSingleEdge = AllsingleEdge.ElementAt(iCount); var TargetVertex = elementSingleEdge.Item1.GetTargetVertex(); double current_distance=0; if (edgeTypeID[elementSingleEdge.Item2].Item1 == true) { current_distance = Math.Abs(AllsingleEdge.ElementAt(iCount).Item1.GetProperty<double>(edgeTypeID[elementSingleEdge.Item2].Item2)); } else { current_distance = 1; } var current_singleEdge = AllsingleEdge.ElementAt(iCount).Item1; var TargetVertexID = lists.GetElement(TargetVertex.VertexID); if (TargetVertexID == null) { if (!endVertexFlag) { buf.Add(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1); lists.Add(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1, current_singleEdge, elementSingleEdge.Item2, currentVertex); } else if (endVertexDistance > currentVertexDistance + current_distance || (endVertexDistance == currentVertexDistance + current_distance && endvertexDepth > currentVertexDepth + 1)) { buf.Add(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1); lists.Add(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1, current_singleEdge, elementSingleEdge.Item2, currentVertex); } } else { if (currentVertexDistance + current_distance < TargetVertexID.Item2) { if (!endVertexFlag) { buf.Set(TargetVertexID.Item2,TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1); lists.Set(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1, current_singleEdge, elementSingleEdge.Item2, currentVertex); } else if (endVertexDistance > currentVertexDistance + current_distance || (endVertexDistance == currentVertexDistance + current_distance && endvertexDepth > currentVertexDepth + 1)) { buf.Set(TargetVertexID.Item2,TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1); lists.Set(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1, current_singleEdge, elementSingleEdge.Item2, currentVertex); } } else if (currentVertexDistance + current_distance == TargetVertexID.Item2 && currentVertexDepth + 1 < TargetVertexID.Item3) { if (!endVertexFlag) { buf.Set(TargetVertexID.Item2,TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1); lists.Set(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1, current_singleEdge, elementSingleEdge.Item2, currentVertex); } else if (endVertexDistance > currentVertexDistance + current_distance || (endVertexDistance == currentVertexDistance + current_distance && endvertexDepth > currentVertexDepth + 1)) { buf.Set(TargetVertexID.Item2,TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1); lists.Set(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1, current_singleEdge, elementSingleEdge.Item2, currentVertex); } } } if (TargetVertex == endVertex) { endVertexFlag = true; var endNode = lists.GetElement(endVertex.VertexID); endVertexDistance = endNode.Item2; endvertexDepth = endNode.Item3; } } } } //delate from Buffer current Vertex or all if (currentVertex == endVertex) { buf.Clear(); } else { buf.Remove(currentVertexDistance,currentVertex.VertexID); } //Minimum distance from Buffer if (buf.Count != 0) { var minVertex = buf.Min(); currentVertex = minVertex.Item1; currentVertexDistance = minVertex.Item2; currentVertexDepth = minVertex.Item3; } } #endregion clock.Stop(); #region create output edgeBuffer.Add(null); currentVertex = endVertex; while (currentVertex != myStartVertex) { var current_tuple = lists.GetElement(currentVertex.VertexID); if (current_tuple == null) throw new InvalidFunctionParameterException("MaxDepth", "Max depth to low or end node is not with start node connected, find't end node", maxDepth); VertexBuffer.Add(currentVertex); distanceBuffer.Add(current_tuple.Item2); depthBuffer.Add(current_tuple.Item3); edgeBuffer.Add(current_tuple.Item4); currentVertex = current_tuple.Item5; } depthBuffer.Add(0); distanceBuffer.Add(0); VertexBuffer.Add(myStartVertex); edgeBuffer.Add(null); var path = CreateVertexView(myPropertyID, VertexBuffer, distanceBuffer, edgeBuffer, depthBuffer); #endregion distanceBuffer = null; VertexBuffer = null; edgeBuffer = null; depthBuffer = null; buf.Clear(); lists.Clear(); buf = null; lists = null; return new FuncParameter(path); }
protected EdgeKey GetBackwardEdgeKey(LevelKey myPath, int desiredBELevel, IGraphDB myGraphDB, SecurityToken mySecurityToken, Int64 myTransactionToken) { #region data IVertexType tempType; IAttributeDefinition tempAttr; #endregion tempType = myGraphDB.GetVertexType<IVertexType>( mySecurityToken, myTransactionToken, new RequestGetVertexType(myPath.Edges[desiredBELevel].VertexTypeID), (stats, vertexType) => vertexType); tempAttr = tempType.GetAttributeDefinition(myPath.Edges[desiredBELevel].AttributeID); if (tempAttr.Kind != AttributeType.IncomingEdge) { return new EdgeKey(tempType.ID, tempAttr.ID); } else { IIncomingEdgeDefinition incomingEdgeDefinition = tempAttr as IIncomingEdgeDefinition; return new EdgeKey(incomingEdgeDefinition.RelatedEdgeDefinition.SourceVertexType.ID, incomingEdgeDefinition.RelatedEdgeDefinition.ID); } }
private void Helper(IGraphDB graph, SecurityToken sec, Int64 trans, List<TypeWithProperty> type_property_object, List<Tuple<long, long>> PATHShort) { List<Tuple<String, String>> output = new List<Tuple<string, string>>(); this._stringPath.Clear(); var StartTypeLong = PATHShort.Find(x => x.Item2 == 0); var StartType = graph.GetVertexType(sec, trans, new sones.GraphDB.Request.RequestGetVertexType(StartTypeLong.Item1), (statistics, type) => type); this._stringPath.Add(Tuple.Create("Starting VertexType for SELECT", StartType.Name)); String str = StartType.Name; foreach (TypeWithProperty value in type_property_object.Where(x => x.Type.ID == StartType.ID)) this._stringPath.Add(Tuple.Create(StartType.Name + '.' + value.PropertyDefinition.Name,StartType.Name + '.' + value.PropertyDefinition.Name)); Recurs(str, StartType, type_property_object, PATHShort); }
public override QueryResult Execute(IGraphDB myGraphDB, IGraphQL myGraphQL, GQLPluginManager myPluginManager, String myQuery, SecurityToken mySecurityToken, TransactionToken myTransactionToken) { var sw = Stopwatch.StartNew(); _query = myQuery; //prepare var vertexTypeSource = myGraphDB.GetVertexType<IVertexType>( mySecurityToken, myTransactionToken, new RequestGetVertexType(_SourceType), (stats, vtype) => vtype); var vertexTypeTarget = myGraphDB.GetVertexType<IVertexType>( mySecurityToken, myTransactionToken, new RequestGetVertexType(_TargetType), (stats, vtype) => vtype); //validate _Condition.Validate(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, vertexTypeSource); //calculate var expressionGraph = _Condition.Calculon(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, new CommonUsageGraph(myGraphDB, mySecurityToken, myTransactionToken), false); //extract var myToBeUpdatedVertices = expressionGraph.Select(new LevelKey(vertexTypeSource.ID, myGraphDB, mySecurityToken, myTransactionToken), null, true).ToList(); if (myToBeUpdatedVertices.Count > 0) { //update ProcessUpdate(myToBeUpdatedVertices, myGraphDB, myPluginManager, mySecurityToken, myTransactionToken); } sw.Stop(); return GenerateResult(sw.Elapsed.TotalMilliseconds); }
/// <summary> /// Algorithm Dijkstra for find all shortest path from start Vertex to all others Vertices /// </summary> /// <param name="myAttributeDefinition"></param> /// <param name="myCallingObject"></param> /// <param name="myStartVertex"></param> /// <param name="myGraphDB"></param> /// <param name="maxDepth"></param> /// <param name="mySecurityToken"></param> /// <param name="myTransactionToken"></param> /// <returns></returns> private Tuple <IVertex, DataDijkstra> FindShortPathToAll(IAttributeDefinition myAttributeDefinition, Object myCallingObject, IVertex myStartVertex, IGraphDB myGraphDB, UInt64 maxDepth, SecurityToken mySecurityToken, Int64 myTransactionToken) { if (myStartVertex == null) { throw new InvalidFunctionParameterException("StartVertex", "Vertex that represents the start vertex", "null"); } //set the start node var currentVertex = myStartVertex; var myType = myGraphDB.GetVertexType <IVertexType>(mySecurityToken, myTransactionToken, new sones.GraphDB.Request.RequestGetVertexType(myAttributeDefinition.RelatedType.Name), (statistics, type) => type); var myEdgeType = myAttributeDefinition.ID; var hasProperty = myType.GetOutgoingEdgeDefinition(myAttributeDefinition.Name).InnerEdgeType.HasProperty("Weight"); long myPropertyID = 0; if (hasProperty == true) { myPropertyID = myType.GetOutgoingEdgeDefinition(myAttributeDefinition.Name).InnerEdgeType.GetPropertyDefinition("Weight").ID; } var edgeTypeDifinition = myType.GetOutgoingEdgeDefinition(myAttributeDefinition.Name); DataDijkstra lists = new DataDijkstra(); BufferDijkstra buffer = new BufferDijkstra(); buffer.Add(myStartVertex, 0, 0); lists.Add(currentVertex, 0, 0, null, null, currentVertex); double currentVertexDistance = 0; ulong currentVertexDepth = 0; while (buffer.Count != 0) { if (currentVertexDepth < maxDepth) { var hyperEdge = currentVertex.GetOutgoingHyperEdge(myEdgeType); if (hyperEdge != null) { var singleEdge = hyperEdge.GetAllEdges(); for (int iCount = 0; iCount < singleEdge.Count(); iCount++) { var TargetVertex = singleEdge.ElementAt(iCount).GetTargetVertex(); double current_distance; if (hasProperty == true) { current_distance = Math.Abs(singleEdge.ElementAt(iCount).GetProperty <double>(myPropertyID)); } else { current_distance = 1; } var current_singleEdge = singleEdge.ElementAt(iCount); var TargetVertexID = lists.GetElement(TargetVertex.VertexID); if (TargetVertexID == null) { buffer.Add(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1); lists.Add(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1, current_singleEdge, edgeTypeDifinition, currentVertex); } else { if (currentVertexDistance + current_distance < TargetVertexID.Item2) { buffer.Set(TargetVertexID.Item2, TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1); lists.Set(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1, current_singleEdge, edgeTypeDifinition, currentVertex); } else if (currentVertexDistance + current_distance == TargetVertexID.Item2 && currentVertexDepth + 1 < TargetVertexID.Item3) { buffer.Set(TargetVertexID.Item2, TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1); lists.Set(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1, current_singleEdge, edgeTypeDifinition, currentVertex); } } } //delate from Buffer Vertex and any information } } buffer.Remove(currentVertexDistance, currentVertex.VertexID); //Minimum in distance from Buffer if (buffer.Count != 0) { var minVertex = buffer.Min(); currentVertex = minVertex.Item1; currentVertexDistance = minVertex.Item2; currentVertexDepth = minVertex.Item3; } } var result = Tuple.Create(myStartVertex, lists); buffer.Clear(); lists.Clear(); buffer = null; lists = null; return(result); }
private void AddVertexTypeAndAttributesRecursivly(IGraphDB myGraphDB, SecurityToken mySecurityToken, Int64 myTransactionToken, IVertexType type, ref HashSet<IVertexType> types) { if (!type.IsUserDefined) return; if (type.HasParentType) { if (!types.Contains(type.ParentVertexType)) { types.Add(type.ParentVertexType); foreach (var attr in (type.GetAttributeDefinitions(false)).Where(attrDef => attrDef.Kind == AttributeType.Property)) { var attrType = myGraphDB.GetVertexType<IVertexType>(mySecurityToken, myTransactionToken, new RequestGetVertexType(attr.ID), (stats, vertex) => vertex); AddVertexTypeAndAttributesRecursivly(myGraphDB, mySecurityToken, myTransactionToken, attrType, ref types); } } } }
public override IQueryResult Execute(IGraphDB myGraphDB, IGraphQL myGraphQL, GQLPluginManager myPluginManager, String myQuery, SecurityToken mySecurityToken, Int64 myTransactionToken) { var sw = Stopwatch.StartNew(); IQueryResult result = null; _query = myQuery; String myAction = ""; IEnumerable<IVertex> myToBeUpdatedVertices = null; //prepare var vertexType = myGraphDB.GetVertexType<IVertexType>( mySecurityToken, myTransactionToken, new RequestGetVertexType(_Type), (stats, vtype) => vtype); if (_WhereExpression != null) { //validate _WhereExpression.Validate(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, vertexType); //calculate var expressionGraph = _WhereExpression.Calculon(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, new CommonUsageGraph(myGraphDB, mySecurityToken, myTransactionToken), false); //extract myToBeUpdatedVertices = expressionGraph.Select(new LevelKey(vertexType.ID, myGraphDB, mySecurityToken, myTransactionToken), null, true).ToList(); } if (myToBeUpdatedVertices != null && myToBeUpdatedVertices.Count() > 0) { //update result = ProcessUpdate(myToBeUpdatedVertices, myGraphDB, myPluginManager, mySecurityToken, myTransactionToken); myAction = "Updated"; } else { //insert result = ProcessInsert(myGraphDB, myPluginManager, mySecurityToken, myTransactionToken); myAction = "Inserted"; } if (result.Error != null) throw result.Error; sw.Stop(); return GenerateResult(sw.Elapsed.TotalMilliseconds, result, myAction); }
private IEnumerable<IVertexType> GetVertexTypes(IGraphDB myGraphDB, SecurityToken mySecurityToken, Int64 myTransactionToken, IEnumerable<String> myTypes) { #region GetTypeToDump IEnumerable<IVertexType> typesToDump = new List<IVertexType>(); if (myTypes.IsNullOrEmpty()) typesToDump = myGraphDB.GetAllVertexTypes(mySecurityToken, myTransactionToken, new RequestGetAllVertexTypes(), (stats, vertexTypes) => vertexTypes) .Where(_ => _.IsUserDefined); else { var typesToDumpHash = new HashSet<IVertexType>(); foreach (var stringType in myTypes) { var type = myGraphDB.GetVertexType(mySecurityToken, myTransactionToken, new RequestGetVertexType(stringType), (stats, vertexType) => vertexType); if (type == null) throw new TypeDoesNotExistException(stringType, ""); //typesToDumpHash.UnionWith(myDBContext.DBTypeManager.GetAllParentTypes(type, true, false)); AddVertexTypeAndAttributesRecursivly(myGraphDB, mySecurityToken, myTransactionToken, type, ref typesToDumpHash); } typesToDump = typesToDumpHash.ToList(); } #endregion return typesToDump; }
public override FuncParameter ExecFunc(IAttributeDefinition myAttributeDefinition, Object myCallingObject, IVertex myStartVertex, IGraphDB myGraphDB, SecurityToken mySecurityToken, Int64 myTransactionToken, params FuncParameter[] myParams) { #region initialize data var graph = myGraphDB; // The edge we starting of (e.g. Friends) var typeAttribute = myAttributeDefinition; if (myStartVertex == null) throw new InvalidFunctionParameterException("StartVertex", "Vertex that represents the start vertex", "null"); //set the start node var startNode = myStartVertex; if ((myParams[0].Value as IEnumerable<IVertex>) == null) throw new InvalidFunctionParameterException("TargetVertex", "Set of vertices that represents the target vertices", "null"); //set the target node var targetNode = (myParams[0].Value as IEnumerable<IVertex>).First(); //set the maximum depth UInt64 maxDepth = Convert.ToUInt64(myParams[1].Value); //set the maximum path length UInt64 maxPathLength = Convert.ToUInt64(myParams[2].Value); //mark if only the shortest path should be searched bool onlyShortestPath = Convert.ToBoolean(myParams[3].Value); //mark if all paths should be searched bool allPaths = Convert.ToBoolean(myParams[4].Value); if (!onlyShortestPath && !allPaths) { allPaths = true; } bool useBidirectionalBFS = false; if(myParams.GetLength(0) == 6) //mark if the BidirectionalBFS should be used useBidirectionalBFS = Convert.ToBoolean(myParams[5].Value); var vertexType = myGraphDB.GetVertexType<IVertexType>(mySecurityToken, myTransactionToken, new RequestGetVertexType(startNode.VertexTypeID), (stats, type) => type); if (vertexType == null) throw new InvalidFunctionParameterException("StartVertexType", "VertexType that represents the start vertex type not found", startNode.VertexTypeID); #endregion #region check correctness of parameters //check if values are correct if (maxDepth < 1) throw new InvalidFunctionParameterException("maxDepth", ">= 1", maxDepth.ToString()); if (maxPathLength < 2) throw new InvalidFunctionParameterException("maxPathLength", ">= 2", maxPathLength.ToString()); #endregion #region call graph function HashSet<List<Tuple<long, long>>> paths = null; //call BFS find methods if (useBidirectionalBFS) paths = new BidirectionalBFS().Find(typeAttribute, vertexType, startNode, targetNode, onlyShortestPath, allPaths, maxDepth, maxPathLength); else paths = new BFS().Find(typeAttribute, vertexType, startNode, targetNode, onlyShortestPath, allPaths, maxDepth, maxPathLength); #endregion #region create output if (paths != null) { #region create outputted views List<List<Tuple<long, long>>.Enumerator> enumerators = new List<List<Tuple<long, long>>.Enumerator>(); foreach (var path in paths) { enumerators.Add(path.GetEnumerator()); } var view = GenerateVertexView(enumerators); #endregion return new FuncParameter(view); } else { return new FuncParameter(new VertexView(null, null)); } #endregion }
public override FuncParameter ExecFunc(IAttributeDefinition myAttributeDefinition, Object myCallingObject, IVertex myStartVertex, IGraphDB myGraphDB, SecurityToken mySecurityToken, Int64 myTransactionToken, params FuncParameter[] myParams) { #region initialization if (myStartVertex == null) { throw new InvalidFunctionParameterException("StartVertex", "Vertex that represents the start vertex", "null"); } //set the start node var currentVertex = myStartVertex; if ((myParams[0].Value as IEnumerable <IVertex>).First() == null) { throw new InvalidFunctionParameterException("EndVertex", "Set of vertices that represents the target vertices", "null"); } //set the target node var endVertex = (myParams[0].Value as IEnumerable <IVertex>).First(); //set the maximum depth if (myAttributeDefinition == null) { throw new InvalidFunctionParameterException("EdgeType ", "Edge type not found", "faild"); } UInt64 maxDepth = Convert.ToUInt64(myParams[1].Value); if (maxDepth < 1) { throw new InvalidFunctionParameterException("MaxDepth", "Max depth to low", maxDepth.ToString()); } var myType = myGraphDB.GetVertexType <IVertexType>( mySecurityToken, myTransactionToken, new sones.GraphDB.Request.RequestGetVertexType(myAttributeDefinition.RelatedType.Name), (statistics, type) => type); var myEdgeType = myAttributeDefinition.ID; var hasProperty = myType.GetOutgoingEdgeDefinition(myAttributeDefinition.Name).InnerEdgeType.HasProperty("Weight"); long myPropertyID = 0; if (hasProperty == true) { myPropertyID = myType.GetOutgoingEdgeDefinition(myAttributeDefinition.Name).InnerEdgeType.GetPropertyDefinition("Weight").ID; } Dictionary <IOutgoingEdgeDefinition, Tuple <bool, long> > edgeTypeID = new Dictionary <IOutgoingEdgeDefinition, Tuple <bool, long> >(); var edgeType = myParams[2].Value.ToString(); if (edgeType == "") { edgeTypeID.Add(myType.GetOutgoingEdgeDefinition(myAttributeDefinition.Name), Tuple.Create(hasProperty, myPropertyID)); } else { if (edgeType.Contains("all edgeType")) { edgeTypeID = this.AllEdgeWithTargetVertex(myType); } else { edgeTypeID = this.StringParser(edgeType, myType); if (!edgeTypeID.ContainsKey(myType.GetOutgoingEdgeDefinition(myAttributeDefinition.Name))) { edgeTypeID.Add(myType.GetOutgoingEdgeDefinition(myAttributeDefinition.Name), Tuple.Create(hasProperty, myPropertyID)); } } } List <UInt64> depthBuffer = new List <UInt64>(); List <Tuple <ISingleEdge, IOutgoingEdgeDefinition> > edgeBuffer = new List <Tuple <ISingleEdge, IOutgoingEdgeDefinition> >(); List <double> distanceBuffer = new List <double>(); List <IVertex> VertexBuffer = new List <IVertex>(); BufferDijkstra buf = new BufferDijkstra(); DataDijkstra lists = new DataDijkstra(); buf.Add(myStartVertex, 0, 0); lists.Add(currentVertex, 0, 0, null, null, currentVertex); bool endVertexFlag = false; #endregion #region Dijkstra algorithm double currentVertexDistance = 0; ulong currentVertexDepth = 0; double endVertexDistance = 0; ulong endvertexDepth = 0; Stopwatch clock = new Stopwatch(); clock.Start(); while (buf.Count != 0) { if (currentVertexDepth < maxDepth) { List <Tuple <ISingleEdge, IOutgoingEdgeDefinition> > AllsingleEdge = new List <Tuple <ISingleEdge, IOutgoingEdgeDefinition> >(); foreach (IOutgoingEdgeDefinition id in edgeTypeID.Keys) { var hyperEdge = currentVertex.GetOutgoingHyperEdge(id.ID); if (hyperEdge != null) { var singleEdge = hyperEdge.GetAllEdges(); foreach (ISingleEdge value in singleEdge) { AllsingleEdge.Add(Tuple.Create(value, id)); } } } if (AllsingleEdge != null) { for (int iCount = 0; iCount < AllsingleEdge.Count(); iCount++) { var elementSingleEdge = AllsingleEdge.ElementAt(iCount); var TargetVertex = elementSingleEdge.Item1.GetTargetVertex(); double current_distance = 0; if (edgeTypeID[elementSingleEdge.Item2].Item1 == true) { current_distance = Math.Abs(AllsingleEdge.ElementAt(iCount).Item1.GetProperty <double>(edgeTypeID[elementSingleEdge.Item2].Item2)); } else { current_distance = 1; } var current_singleEdge = AllsingleEdge.ElementAt(iCount).Item1; var TargetVertexID = lists.GetElement(TargetVertex.VertexID); if (TargetVertexID == null) { if (!endVertexFlag) { buf.Add(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1); lists.Add(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1, current_singleEdge, elementSingleEdge.Item2, currentVertex); } else if (endVertexDistance > currentVertexDistance + current_distance || (endVertexDistance == currentVertexDistance + current_distance && endvertexDepth > currentVertexDepth + 1)) { buf.Add(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1); lists.Add(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1, current_singleEdge, elementSingleEdge.Item2, currentVertex); } } else { if (currentVertexDistance + current_distance < TargetVertexID.Item2) { if (!endVertexFlag) { buf.Set(TargetVertexID.Item2, TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1); lists.Set(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1, current_singleEdge, elementSingleEdge.Item2, currentVertex); } else if (endVertexDistance > currentVertexDistance + current_distance || (endVertexDistance == currentVertexDistance + current_distance && endvertexDepth > currentVertexDepth + 1)) { buf.Set(TargetVertexID.Item2, TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1); lists.Set(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1, current_singleEdge, elementSingleEdge.Item2, currentVertex); } } else if (currentVertexDistance + current_distance == TargetVertexID.Item2 && currentVertexDepth + 1 < TargetVertexID.Item3) { if (!endVertexFlag) { buf.Set(TargetVertexID.Item2, TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1); lists.Set(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1, current_singleEdge, elementSingleEdge.Item2, currentVertex); } else if (endVertexDistance > currentVertexDistance + current_distance || (endVertexDistance == currentVertexDistance + current_distance && endvertexDepth > currentVertexDepth + 1)) { buf.Set(TargetVertexID.Item2, TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1); lists.Set(TargetVertex, current_distance + currentVertexDistance, currentVertexDepth + 1, current_singleEdge, elementSingleEdge.Item2, currentVertex); } } } if (TargetVertex == endVertex) { endVertexFlag = true; var endNode = lists.GetElement(endVertex.VertexID); endVertexDistance = endNode.Item2; endvertexDepth = endNode.Item3; } } } } //delate from Buffer current Vertex or all if (currentVertex == endVertex) { buf.Clear(); } else { buf.Remove(currentVertexDistance, currentVertex.VertexID); } //Minimum distance from Buffer if (buf.Count != 0) { var minVertex = buf.Min(); currentVertex = minVertex.Item1; currentVertexDistance = minVertex.Item2; currentVertexDepth = minVertex.Item3; } } #endregion clock.Stop(); #region create output edgeBuffer.Add(null); currentVertex = endVertex; while (currentVertex != myStartVertex) { var current_tuple = lists.GetElement(currentVertex.VertexID); if (current_tuple == null) { throw new InvalidFunctionParameterException("MaxDepth", "Max depth to low or end node is not with start node connected, find't end node", maxDepth); } VertexBuffer.Add(currentVertex); distanceBuffer.Add(current_tuple.Item2); depthBuffer.Add(current_tuple.Item3); edgeBuffer.Add(current_tuple.Item4); currentVertex = current_tuple.Item5; } depthBuffer.Add(0); distanceBuffer.Add(0); VertexBuffer.Add(myStartVertex); edgeBuffer.Add(null); var path = CreateVertexView(myPropertyID, VertexBuffer, distanceBuffer, edgeBuffer, depthBuffer); #endregion distanceBuffer = null; VertexBuffer = null; edgeBuffer = null; depthBuffer = null; buf.Clear(); lists.Clear(); buf = null; lists = null; return(new FuncParameter(path)); }
/// <summary> /// Creates the request for the graphdb /// </summary> /// <returns>The created vertex</returns> private RequestInsertVertex CreateRequest(GQLPluginManager myPluginManager, IGraphDB myGraphDB, SecurityToken mySecurityToken, Int64 myTransactionToken) { #region data var result = new RequestInsertVertex(_TypeName); var vertexType = myGraphDB.GetVertexType<IVertexType>( mySecurityToken, myTransactionToken, new RequestGetVertexType(_TypeName), (stats, vtype) => vtype); #endregion if (_AttributeAssignList != null) { foreach (var aAttributeDefinition in _AttributeAssignList) { ProcessAAttributeDefinition(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, vertexType, aAttributeDefinition, ref result); } } return result; }
private void AddTypeAndAttributesRecursivly(ref IGraphDB myGraphDB, ref SecurityToken mySecurityToken, ref TransactionToken myTransactionToken, IVertexType type, ref HashSet<IVertexType> types) { if (!type.IsUserDefined) return; if (type.HasParentType) { if (!types.Contains(type.ParentVertexType)) { types.Add(type.ParentVertexType); foreach (var attr in (type.GetAttributeDefinitions(false)).Where(attrDef => attrDef.Kind == AttributeType.Property)) { var attrType = myGraphDB.GetVertexType<IVertexType>(mySecurityToken, myTransactionToken, new RequestGetVertexType(attr.ID), (stats, vertex) => vertex); AddTypeAndAttributesRecursivly(ref myGraphDB, ref mySecurityToken, ref myTransactionToken, attrType, ref types); } } //types.UnionWith(myDBContext.DBTypeManager.GetAllParentTypes(type, true, false)); } }
public override QueryResult Execute(IGraphDB myGraphDB, IGraphQL myGraphQL, GQLPluginManager myPluginManager, String myQuery, SecurityToken mySecurityToken, TransactionToken myTransactionToken) { Stopwatch sw = Stopwatch.StartNew(); _query = myQuery; QueryResult result = null; String myAction = ""; List<IVertex> myToBeUpdatedVertices = new List<IVertex>(); //prepare var vertexType = myGraphDB.GetVertexType<IVertexType>( mySecurityToken, myTransactionToken, new RequestGetVertexType(_type), (stats, vtype) => vtype); if (_whereExpression != null) { //validate _whereExpression.Validate(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, vertexType); //calculate var expressionGraph = _whereExpression.Calculon(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, new CommonUsageGraph(myGraphDB, mySecurityToken, myTransactionToken), false); //extract myToBeUpdatedVertices = expressionGraph.Select(new LevelKey(vertexType.ID, myGraphDB, mySecurityToken, myTransactionToken), null, true).ToList(); } switch (myToBeUpdatedVertices.Count) { case 0: //insert result = ProcessInsert(myGraphDB, myPluginManager, mySecurityToken, myTransactionToken); myAction = "Inserted"; break; case 1: //delete ProcessDelete(myToBeUpdatedVertices[0], myGraphDB, myPluginManager, mySecurityToken, myTransactionToken); //insert result = ProcessInsert(myGraphDB, myPluginManager, mySecurityToken, myTransactionToken); myAction = "Replaced"; break; default: //error throw new NotImplementedQLException("It's currenty not implemented to InsertOrReplace more than one vertex"); } if (result.Error != null) throw result.Error; return GenerateResult(sw.ElapsedMilliseconds, result, myAction); }
private RequestUpdate GenerateUpdateRequest(IGraphDB myGraphDB, GQLPluginManager myPluginManager, SecurityToken mySecurityToken, Int64 myTransactionToken) { IEnumerable <IVertex> toBeupdatedVertices = null; //prepare var vertexType = myGraphDB.GetVertexType <IVertexType>( mySecurityToken, myTransactionToken, new RequestGetVertexType(_TypeName), (stats, vtype) => vtype); RequestUpdate result = null; if (_vertexIDs == null) { if (_WhereExpression != null) { //validate _WhereExpression.Validate(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, vertexType); //calculate var expressionGraph = _WhereExpression.Calculon(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, new CommonUsageGraph(myGraphDB, mySecurityToken, myTransactionToken), false); //extract toBeupdatedVertices = expressionGraph.Select(new LevelKey(vertexType.ID, myGraphDB, mySecurityToken, myTransactionToken), null, true); result = new RequestUpdate( new RequestGetVertices(toBeupdatedVertices .Select(_ => _.VertexTypeID) .FirstOrDefault(), toBeupdatedVertices.Select(_ => _.VertexID), false)); } else { toBeupdatedVertices = myGraphDB.GetVertices <IEnumerable <IVertex> >( mySecurityToken, myTransactionToken, new RequestGetVertices(vertexType.ID), (stats, vertices) => vertices); } } else { toBeupdatedVertices = _vertexIDs; } if (result == null) { result = new RequestUpdate(new RequestGetVertices(vertexType.ID, toBeupdatedVertices .Select(_ => _.VertexID), false)); } ProcessListOfUpdates(vertexType, myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, ref result); return(result); }
private RequestUpdate GenerateUpdateRequest(IGraphDB myGraphDB, GQLPluginManager myPluginManager, SecurityToken mySecurityToken, TransactionToken myTransactionToken) { IEnumerable<IVertex> toBeupdatedVertices = null; //prepare var vertexType = myGraphDB.GetVertexType<IVertexType>( mySecurityToken, myTransactionToken, new RequestGetVertexType(_TypeName), (stats, vtype) => vtype); if (_vertexIDs == null) { if (_WhereExpression != null) { //validate _WhereExpression.Validate(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, vertexType); //calculate var expressionGraph = _WhereExpression.Calculon(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, new CommonUsageGraph(myGraphDB, mySecurityToken, myTransactionToken), false); //extract toBeupdatedVertices = expressionGraph.Select(new LevelKey(vertexType.ID, myGraphDB, mySecurityToken, myTransactionToken), null, true); } else { toBeupdatedVertices = myGraphDB.GetVertices<IEnumerable<IVertex>>( mySecurityToken, myTransactionToken, new RequestGetVertices(vertexType.ID), (stats, vertices) => vertices); } } else { toBeupdatedVertices = _vertexIDs; } var result = new RequestUpdate(new RequestGetVertices(vertexType.ID, toBeupdatedVertices.Select(_ => _.VertexID) , false)); ProcessListOfUpdates(vertexType, myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, ref result); return result; }
public override IQueryResult Execute(IGraphDB myGraphDB, IGraphQL myGraphQL, GQLPluginManager myPluginManager, String myQuery, SecurityToken mySecurityToken, Int64 myTransactionToken) { Stopwatch sw = Stopwatch.StartNew(); _query = myQuery; IQueryResult result = null; String myAction = ""; List <IVertex> myToBeUpdatedVertices = new List <IVertex>(); //prepare var vertexType = myGraphDB.GetVertexType <IVertexType>( mySecurityToken, myTransactionToken, new RequestGetVertexType(_type), (stats, vtype) => vtype); if (_whereExpression != null) { //validate _whereExpression.Validate(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, vertexType); //calculate var expressionGraph = _whereExpression.Calculon(myPluginManager, myGraphDB, mySecurityToken, myTransactionToken, new CommonUsageGraph(myGraphDB, mySecurityToken, myTransactionToken), false); //extract myToBeUpdatedVertices = expressionGraph.Select(new LevelKey(vertexType.ID, myGraphDB, mySecurityToken, myTransactionToken), null, true).ToList(); } switch (myToBeUpdatedVertices.Count) { case 0: //insert result = ProcessInsert(myGraphDB, myPluginManager, mySecurityToken, myTransactionToken); myAction = "Inserted"; break; case 1: //delete ProcessDelete(myToBeUpdatedVertices[0], myGraphDB, myPluginManager, mySecurityToken, myTransactionToken); //insert result = ProcessInsert(myGraphDB, myPluginManager, mySecurityToken, myTransactionToken); myAction = "Replaced"; break; default: //error throw new NotImplementedQLException("It's currenty not implemented to InsertOrReplace more than one vertex"); } if (result.Error != null) { throw result.Error; } return(GenerateResult(sw.ElapsedMilliseconds, result, myAction)); }