示例#1
0
        /// <summary>
        /// Generates a attribute definition
        /// </summary>
        /// <param name="aAttribute">The attribute that is going to be transfered</param>
        /// <returns>A attribute predefinition</returns>
        private PropertyPredefinition GenerateProperty(KeyValuePair <AttributeDefinition, string> aAttribute)
        {
            PropertyPredefinition result = new PropertyPredefinition(aAttribute.Key.AttributeName,
                                                                     aAttribute.Value);

            switch (aAttribute.Key.AttributeType.Type)
            {
            case SonesGQLGrammar.TERMINAL_SET:
                result.SetMultiplicityToSet();
                break;

            case SonesGQLGrammar.TERMINAL_LIST:
                result.SetMultiplicityToList();
                break;
            }

            if (aAttribute.Key.DefaultValue != null)
            {
                result.SetDefaultValue(aAttribute.Key.DefaultValue.ToString());
            }

            if (aAttribute.Key.AttributeType.TypeCharacteristics.IsMandatory)
            {
                result.SetAsMandatory();
            }

            return(result);
        }
示例#2
0
        public PropertyPredefinition ToPropertyPredefinition()
        {
            var property = new PropertyPredefinition(this.AttributeName, this.AttributeType); //Todo insert attribute type

            property.SetAttributeType(this.AttributeType != null ? this.AttributeType : null);
            property.SetComment(this.Comment != null ? this.Comment : null);

            if (this.IsIndexed)
            {
                property.SetAsIndexed();
            }
            if (this.IsMandatory)
            {
                property.SetAsMandatory();
            }
            if (this.IsUnique)
            {
                property.SetAsUnique();
            }
            if (this.DefaultValue != null)
            {
                property.SetDefaultValue(this.DefaultValue);
            }

            if (this.Multiplicity.Equals(ServicePropertyMultiplicity.List))
            {
                property.SetMultiplicityToList();
            }
            if (this.Multiplicity.Equals(ServicePropertyMultiplicity.Set))
            {
                property.SetMultiplicityToSet();
            }

            return(property);
        }
示例#3
0
 internal ServicePropertyPredefinition(PropertyPredefinition myPropertyPredefinition) : base(myPropertyPredefinition)
 {
     this.IsIndexed    = myPropertyPredefinition.IsIndexed;
     this.IsMandatory  = myPropertyPredefinition.IsMandatory;
     this.IsUnique     = myPropertyPredefinition.IsUnique;
     this.DefaultValue = myPropertyPredefinition.DefaultValue;
     this.Multiplicity = ConvertHelper.ToServicePropertyMultiplicity(myPropertyPredefinition.Multiplicity);
 }
示例#4
0
        /// <summary>
        /// Adds a property to the vertex type definition
        /// </summary>
        /// <param name="myPropertyDefinition">The property definition that is going to be added</param>
        /// <returns>The reference of the current object. (fluent interface).</returns>
        public RequestAlterVertexType AddProperty(PropertyPredefinition myPropertyDefinition)
        {
            if (myPropertyDefinition != null)
            {
                _toBeAddedAttributes = (_toBeAddedAttributes) ?? new List <AAttributePredefinition>();
                _toBeAddedAttributes.Add(myPropertyDefinition);
                AddPropertyCount++;
            }

            return(this);
        }
示例#5
0
        /// <summary>
        /// Checks if a given property definition has a valid type.
        /// </summary>
        /// <param name="myVertexTypeDefinition">The vertex type predefinition that defines the property.</param>
        /// <param name="myProperty">The property to be checked.</param>
        protected void CheckPropertyType(ATypePredefinition myTypePredefinition,
                                         PropertyPredefinition myProperty)
        {
            if (String.IsNullOrWhiteSpace(myProperty.AttributeType))
            {
                throw new EmptyPropertyTypeException(myTypePredefinition, myProperty.AttributeName);
            }

            if (!_baseTypeManager.IsBaseType(myProperty.AttributeType))
            {
                throw new UnknownPropertyTypeException(myTypePredefinition, myProperty.AttributeType);
            }
        }
示例#6
0
        /// <summary>
        /// Reads an attribute definition from the GraphML File and stores
        /// it internal for later usage on vertex / edge reading.
        /// </summary>
        /// <param name='myReader'>
        /// XmlReader
        /// </param>
        private void ReadAttributeDefinition(XmlReader myReader)
        {
            #region data

            var attrId   = myReader.GetAttribute(GraphMLTokens.ID);
            var attrFor  = myReader.GetAttribute(GraphMLTokens.FOR);
            var attrName = myReader.GetAttribute(GraphMLTokens.ATTRIBUTE_NAME);
            var attrType = myReader.GetAttribute(GraphMLTokens.ATTRIBUTE_TYPE).ToLower();

            string attrDefault = null;

            using (var readerAttribute = myReader.ReadSubtree())
            {
                while (readerAttribute.Read())
                {
                    if (readerAttribute.Name == GraphMLTokens.DEFAULT)
                    {
                        attrDefault = readerAttribute.ReadElementContentAsString();
                    }
                }
            }

            // make attribute type DB conform (capitalize first letter)
            attrType = char.ToUpper(attrType[0]) + attrType.Substring(1).ToLower();
            // and store the whole definition
            _AttributeDefinitions.Add(attrId, new Tuple <string, string, string, string>(attrFor, attrName, attrType, attrDefault));
            // get GraphDB internal type
            attrType = GetInternalTypeName(attrType);

            #endregion

            #region alter vertex type with new attribute

            if (attrFor.Equals(GraphMLTokens.VERTEX))
            {
                var requestAlterVertexType = new RequestAlterVertexType(_VertexTypeName);

                var propertyPreDefinition = new PropertyPredefinition(attrName, attrType);

                propertyPreDefinition.SetDefaultValue(attrDefault);

                requestAlterVertexType.AddProperty(propertyPreDefinition);

                _GraphDB.AlterVertexType(_SecurityToken,
                                         _TransactionToken,
                                         requestAlterVertexType,
                                         (stats, vType) => vType);
            }

            #endregion
        }
示例#7
0
        /// <summary>
        /// Creates the vertexType based on the VertexTypeName.
        ///
        /// The vertexType contains one Outgoing Edge Defintion, the edge
        /// is weighted and can't contain any other attributes.
        /// </summary>
        private void CreateVertexType()
        {
            #region create vertex type

            var vertexTypePreDef = new VertexTypePredefinition(_VertexTypeName);
            var outEdgePreDef    = new OutgoingEdgePredefinition(_EdgeTypeName, vertexTypePreDef);

            #region create edge definition

            // weighted multi-edge
            outEdgePreDef.SetEdgeTypeAsWeighted();
            // set inner edge type to weighted
            outEdgePreDef.SetMultiplicityAsMultiEdge("Weighted");

            vertexTypePreDef.AddOutgoingEdge(outEdgePreDef);

            #endregion

            #region create id definition

            var idPreDefinition = new PropertyPredefinition(GraphMLTokens.VERTEX_ID_NAME, GraphMLTokens.VERTEX_ID_TYPE);

            idPreDefinition.SetDefaultValue(GraphMLTokens.VERTEX_ID_DEF_VAL);

            vertexTypePreDef.AddProperty(idPreDefinition);

            #endregion

            #region create vertex type

            var requestCreateVertexType = new RequestCreateVertexType(vertexTypePreDef);

            _GraphDB.CreateVertexType(_SecurityToken,
                                      _TransactionToken,
                                      requestCreateVertexType,
                                      (stats, vType) => vType);

            #endregion

            #endregion
        }
示例#8
0
        /// <summary>
        /// Converts the unknown properties into properties / edges.
        /// </summary>
        /// <param name="myUnknown">The unknown property predefinition.</param>
        /// <returns>The property predefinition.</returns>
        protected static PropertyPredefinition ConvertUnknownToProperty(UnknownAttributePredefinition myUnknown)
        {
            if (myUnknown.EdgeType != null)
            {
                throw new Exception("An edge type is not allowed on a property.");
            }

            var prop = new PropertyPredefinition(myUnknown.AttributeName, myUnknown.AttributeType)
                       .SetDefaultValue(myUnknown.DefaultValue)
                       .SetComment(myUnknown.Comment);

            if (myUnknown.IsUnique)
            {
                prop.SetAsUnique();
            }

            if (myUnknown.IsMandatory)
            {
                prop.SetAsMandatory();
            }

            if (myUnknown.Multiplicity != null)
            {
                switch (myUnknown.Multiplicity)
                {
                case UnknownAttributePredefinition.LISTMultiplicity:
                    prop.SetMultiplicityToList();
                    break;

                case UnknownAttributePredefinition.SETMultiplicity:
                    prop.SetMultiplicityToSet();
                    break;

                default:
                    throw new Exception("Unknown multiplicity for properties.");
                }
            }

            return(prop);
        }
        public PropertyPredefinition ToPropertyPredefinition()
        {
            var property =  new PropertyPredefinition(this.AttributeName,this.AttributeType); //Todo insert attribute type
            property.SetAttributeType(this.AttributeType != null ? this.AttributeType : null);
            property.SetComment(this.Comment != null ? this.Comment : null);

            if (this.IsIndexed)
                property.SetAsIndexed();
            if (this.IsMandatory)
                property.SetAsMandatory();
            if (this.IsUnique)
                property.SetAsUnique();
            if (this.DefaultValue != null)
                property.SetDefaultValue(this.DefaultValue);

            if (this.Multiplicity.Equals(ServicePropertyMultiplicity.List))
                property.SetMultiplicityToList();
            if (this.Multiplicity.Equals(ServicePropertyMultiplicity.Set))
                property.SetMultiplicityToSet();

            return property;
        }
示例#10
0
        /// <summary>
        /// Describes how to define a type with user defined properties and indices and create some instances by using GraphDB requests.
        /// </summary>
        private void GraphDBRequests()
        {
            #region define type "Tag"

            //create a VertexTypePredefinition
            var Tag_VertexTypePredefinition = new VertexTypePredefinition("Tag");

            //create property
            var PropertyName = new PropertyPredefinition("Name", "String")
                               .SetComment("This is a property on type 'Tag' named 'Name' and is of type 'String'");

            //add property
            Tag_VertexTypePredefinition.AddProperty(PropertyName);

            //create outgoing edge to "Website"
            var OutgoingEdgesTaggedWebsites = new OutgoingEdgePredefinition("TaggedWebsites", "Website")
                                              .SetMultiplicityAsMultiEdge()
                                              .SetComment(@"This is an outgoing edge on type 'Tag' wich points to the type 'Website' (the AttributeType) 
                                                                            and is defined as 'MultiEdge', which means that this edge can contain multiple single edges");

            //add outgoing edge
            Tag_VertexTypePredefinition.AddOutgoingEdge(OutgoingEdgesTaggedWebsites);

            #endregion

            #region define type "Website"

            //create a VertexTypePredefinition
            var Website_VertexTypePredefinition = new VertexTypePredefinition("Website");

            //create properties
            PropertyName = new PropertyPredefinition("Name", "String")
                           .SetComment("This is a property on type 'Website' named 'Name' and is of type 'String'");

            var PropertyUrl = new PropertyPredefinition("URL", "String")
                              .SetAsMandatory();

            //add properties
            Website_VertexTypePredefinition.AddProperty(PropertyName);
            Website_VertexTypePredefinition.AddProperty(PropertyUrl);

            #region create an index on type "Website" on property "Name"
            //there are three ways to set an index on property "Name"
            //Beware: Use just one of them!

            //1. create an index definition and specifie the property- and type name
            var MyIndex = new IndexPredefinition("MyIndex").SetIndexType("SonesIndex").AddProperty("Name").SetVertexType("Website");
            //add index
            Website_VertexTypePredefinition.AddIndex((IndexPredefinition)MyIndex);

            //2. on creating the property definition of property "Name" call the SetAsIndexed() method, the GraphDB will create the index
            //PropertyName = new PropertyPredefinition("Name")
            //                           .SetAttributeType("String")
            //                           .SetComment("This is a property on type 'Website' with name 'Name' and is of type 'String'")
            //                           .SetAsIndexed();

            //3. make a create index request, like creating a type
            //BEWARE: This statement must be execute AFTER the type "Website" is created.
            //var MyIndex = GraphDSServer.CreateIndex<IIndexDefinition>(SecToken,
            //                                                          TransToken,
            //                                                          new RequestCreateIndex(
            //                                                          new IndexPredefinition("MyIndex")
            //                                                                   .SetIndexType("SonesIndex")
            //                                                                   .AddProperty("Name")
            //                                                                   .SetVertexType("Website")), (Statistics, Index) => Index);

            #endregion

            //add IncomingEdge "Tags", the related OutgoingEdge is "TaggedWebsites" on type "Tag"
            Website_VertexTypePredefinition.AddIncomingEdge(new IncomingEdgePredefinition("Tags",
                                                                                          "Tag",
                                                                                          "TaggedWebsites"));

            #endregion


            #region create types by sending requests

            //create the types "Tag" and "Website"
            var DBTypes = GraphDSServer.CreateVertexTypes <IEnumerable <IVertexType> >(SecToken,
                                                                                       TransationID,
                                                                                       new RequestCreateVertexTypes(
                                                                                           new List <VertexTypePredefinition> {
                Tag_VertexTypePredefinition,
                Website_VertexTypePredefinition
            }),
                                                                                       (Statistics, VertexTypes) => VertexTypes);

            /*
             * BEWARE: The following two operations won't work because the two types "Tag" and "Website" depending on each other,
             *          because one type has an incoming edge to the other and the other one has an incoming edge,
             *          so they cannot be created separate (by using create type),
             *          they have to be created at the same time (by using create types)
             *
             * //create the type "Website"
             * var Website = GraphDSServer.CreateVertexType<IVertexType>(SecToken,
             *                                                           TransToken,
             *                                                           new RequestCreateVertexType(Website_VertexTypePredefinition),
             *                                                           (Statistics, VertexType) => VertexType);
             *
             * //create the type "Tag"
             * var Tag = GraphDSServer.CreateVertexType<IVertexType>(SecToken,
             *                                                       TransToken,
             *                                                       new RequestCreateVertexType(Tag_VertexTypePredefinition),
             *                                                       (Statistics, VertexType) => VertexType);
             */

            var Tag = DBTypes.Where(type => type.Name == "Tag").FirstOrDefault();

            var Website = DBTypes.Where(type => type.Name == "Website").FirstOrDefault();

            #endregion

            #region insert some Websites by sending requests

            var cnn = GraphDSServer.Insert <IVertex>(SecToken, TransationID, new RequestInsertVertex("Website")
                                                     .AddStructuredProperty("Name", "CNN")
                                                     .AddStructuredProperty("URL", "http://cnn.com/"),
                                                     (Statistics, Result) => Result);

            var xkcd = GraphDSServer.Insert <IVertex>(SecToken, TransationID, new RequestInsertVertex("Website")
                                                      .AddStructuredProperty("Name", "xkcd")
                                                      .AddStructuredProperty("URL", "http://xkcd.com/"),
                                                      (Statistics, Result) => Result);

            var onion = GraphDSServer.Insert <IVertex>(SecToken, TransationID, new RequestInsertVertex("Website")
                                                       .AddStructuredProperty("Name", "onion")
                                                       .AddStructuredProperty("URL", "http://theonion.com/"),
                                                       (Statistics, Result) => Result);

            //adding an unknown property means the property isn't defined before
            var test = GraphDSServer.Insert <IVertex>(SecToken, TransationID, new RequestInsertVertex("Website")
                                                      .AddStructuredProperty("Name", "Test")
                                                      .AddStructuredProperty("URL", "")
                                                      .AddUnknownProperty("Unknown", "unknown property"),
                                                      (Statistics, Result) => Result);

            #endregion

            #region insert some Tags by sending requests

            //insert a "Tag" with an OutgoingEdge to a "Website" include that the GraphDB creates an IncomingEdge on the given Website instances
            //(because we created an IncomingEdge on type "Website") --> as a consequence we never have to set any IncomingEdge
            var good = GraphDSServer.Insert <IVertex>(SecToken, TransationID, new RequestInsertVertex("Tag")
                                                      .AddStructuredProperty("Name", "good")
                                                      .AddEdge(new EdgePredefinition("TaggedWebsites")
                                                               .AddVertexID(Website.ID, cnn.VertexID)
                                                               .AddVertexID(Website.ID, xkcd.VertexID)),
                                                      (Statistics, Result) => Result);

            var funny = GraphDSServer.Insert <IVertex>(SecToken, TransationID, new RequestInsertVertex("Tag")
                                                       .AddStructuredProperty("Name", "funny")
                                                       .AddEdge(new EdgePredefinition("TaggedWebsites")
                                                                .AddVertexID(Website.ID, xkcd.VertexID)
                                                                .AddVertexID(Website.ID, onion.VertexID)),
                                                       (Statistics, Result) => Result);

            #endregion

            #region how to get a type from the DB, properties of the type, instances of a specific type and read out property values

            //how to get a type from the DB
            var TagDBType = GraphDSServer.GetVertexType <IVertexType>(SecToken, TransationID, new RequestGetVertexType(Tag.ID), (Statistics, Type) => Type);

            //read informations from type
            var typeName = TagDBType.Name;
            //are there other types wich extend the type "Tag"
            var hasChildTypes = TagDBType.HasChildTypes;
            //get the definition of the property "Name"
            var propName = TagDBType.GetPropertyDefinition("Name");

            //how to get all instances of a type from the DB
            var TagInstances = GraphDSServer.GetVertices(SecToken, TransationID, new RequestGetVertices(TagDBType.ID), (Statistics, Vertices) => Vertices);

            foreach (var item in TagInstances)
            {
                //to get the value of a property of an instance, you need the property ID
                //(that's why we fetched the type from DB an read out the property definition of property "Name")
                var name = item.GetPropertyAsString(propName.ID);
            }

            #endregion
        }
示例#11
0
        /// <summary>
        /// Adds a property to the type definition
        /// </summary>
        /// <param name="myPropertyDefinition">The property definition that is going to be added</param>
        /// <returns>The reference of the current object. (fluent interface).</returns>
        public RequestAlterEdgeType AddProperty(PropertyPredefinition myPropertyDefinition)
        {
            if (myPropertyDefinition != null)
            {
                _toBeAddedAttributes = (_toBeAddedAttributes) ?? new List<AAttributePredefinition>();
                _toBeAddedAttributes.Add(myPropertyDefinition);
                AddPropertyCount++;
            }

            return this;
        }
示例#12
0
        /// <summary>
        /// Checks if a given property definition has a valid type.
        /// </summary>
        /// <param name="myVertexTypeDefinition">The vertex type predefinition that defines the property.</param>
        /// <param name="myProperty">The property to be checked.</param>
        protected static void CheckPropertyType(VertexTypePredefinition myVertexTypeDefinition, PropertyPredefinition myProperty)
        {
            if (String.IsNullOrWhiteSpace(myProperty.AttributeType))
            {
                throw new EmptyPropertyTypeException(myVertexTypeDefinition, myProperty.AttributeName);
            }

            if (!IsBaseType(myProperty.AttributeType))
            {
                //it is not one of the base types
                //TODO: check if it is a user defined data type
                throw new UnknownPropertyTypeException(myVertexTypeDefinition, myProperty.AttributeType);
            }
        }
示例#13
0
        /// <summary>
        /// Reads an attribute definition from the GraphML File and stores
        /// it internal for later usage on vertex / edge reading.
        /// </summary>
        /// <param name='myReader'>
        /// XmlReader
        /// </param>
        private void ReadAttributeDefinition(XmlReader myReader)
        {
            #region data

            var attrId 		= myReader.GetAttribute(GraphMLTokens.ID);
            var attrFor 	= myReader.GetAttribute(GraphMLTokens.FOR);
            var attrName 	= myReader.GetAttribute(GraphMLTokens.ATTRIBUTE_NAME);
            var attrType 	= myReader.GetAttribute(GraphMLTokens.ATTRIBUTE_TYPE).ToLower();

            string attrDefault = null;

            using (var readerAttribute = myReader.ReadSubtree())
            {
                while (readerAttribute.Read())
                {
                    if (readerAttribute.Name == GraphMLTokens.DEFAULT)
                    {
                        attrDefault = readerAttribute.ReadElementContentAsString();
                    }
                }
            }

            // make attribute type DB conform (capitalize first letter)
            attrType = char.ToUpper(attrType[0]) + attrType.Substring(1).ToLower();
            // and store the whole definition
            _AttributeDefinitions.Add(attrId, new Tuple<string, string, string, string>(attrFor, attrName, attrType, attrDefault));
            // get GraphDB internal type
            attrType = GetInternalTypeName(attrType);

            #endregion

            #region alter vertex type with new attribute

            if(attrFor.Equals(GraphMLTokens.VERTEX))
            {
                var requestAlterVertexType = new RequestAlterVertexType(_VertexTypeName);

                var propertyPreDefinition = new PropertyPredefinition(attrName, attrType);

                propertyPreDefinition.SetDefaultValue(attrDefault);

                requestAlterVertexType.AddProperty(propertyPreDefinition);

                _GraphDB.AlterVertexType(_SecurityToken,
                    _TransactionToken,
                    requestAlterVertexType,
                    (stats, vType) => vType);
            }

            #endregion
        }
示例#14
0
        /// <summary>
        /// Creates the vertexType based on the VertexTypeName.
        /// 
        /// The vertexType contains one Outgoing Edge Defintion, the edge
        /// is weighted and can't contain any other attributes.
        /// </summary>
        private void CreateVertexType()
        {
            #region create vertex type

            var vertexTypePreDef 	= new VertexTypePredefinition(_VertexTypeName);
            var outEdgePreDef 		= new OutgoingEdgePredefinition(_EdgeTypeName, vertexTypePreDef);

            #region create edge definition

            // weighted multi-edge
            outEdgePreDef.SetEdgeTypeAsWeighted();
            // set inner edge type to weighted
            outEdgePreDef.SetMultiplicityAsMultiEdge("Weighted");

            vertexTypePreDef.AddOutgoingEdge(outEdgePreDef);

            #endregion

            #region create id definition

            var idPreDefinition = new PropertyPredefinition(GraphMLTokens.VERTEX_ID_NAME , GraphMLTokens.VERTEX_ID_TYPE);

            idPreDefinition.SetDefaultValue(GraphMLTokens.VERTEX_ID_DEF_VAL);

            vertexTypePreDef.AddProperty(idPreDefinition);

            #endregion

            #region create vertex type

            var requestCreateVertexType = new RequestCreateVertexType(vertexTypePreDef);

            _GraphDB.CreateVertexType(_SecurityToken,
                                     _TransactionToken,
                                     requestCreateVertexType,
                                     (stats, vType) => vType);

            #endregion

            #endregion
        }
示例#15
0
        /// <summary>
        /// Generates a attribute definition
        /// </summary>
        /// <param name="aAttribute">The attribute that is going to be transfered</param>
        /// <returns>A attribute predefinition</returns>
        private PropertyPredefinition GenerateProperty(KeyValuePair<AttributeDefinition, string> aAttribute)
        {
            PropertyPredefinition result = new PropertyPredefinition(aAttribute.Key.AttributeName,
                                                                        aAttribute.Value);

            switch (aAttribute.Key.AttributeType.Type)
            {
                case SonesGQLGrammar.TERMINAL_SET:
                    result.SetMultiplicityToSet();
                    break;

                case SonesGQLGrammar.TERMINAL_LIST:
                    result.SetMultiplicityToList();
                    break;
            }

            if (aAttribute.Key.DefaultValue != null)
                result.SetDefaultValue(aAttribute.Key.DefaultValue.ToString());

            if (aAttribute.Key.AttributeType.TypeCharacteristics.IsMandatory)
                result.SetAsMandatory();

            return result;
        }
示例#16
0
        private void GraphDBRequests()
        {
            Console.WriteLine("performing DB requests...");
            #region define type "Tag"

            //create a VertexTypePredefinition
            var Tag_VertexTypePredefinition = new VertexTypePredefinition("Tag");

            //create property
            var PropertyName = new PropertyPredefinition("Name", "String")
                                           .SetComment("This is a property on type 'Tag' named 'Name' and is of type 'String'");

            //add property
            Tag_VertexTypePredefinition.AddProperty(PropertyName);

            //create outgoing edge to "Website"
            var OutgoingEdgesTaggedWebsites = new OutgoingEdgePredefinition("TaggedWebsites", "Website")
                                                          .SetMultiplicityAsMultiEdge()
                                                          .SetComment(@"This is an outgoing edge on type 'Tag' wich points to the type 'Website' (the AttributeType) 
                                                                            and is defined as 'MultiEdge', which means that this edge can contain multiple single edges");

            //add outgoing edge
            Tag_VertexTypePredefinition.AddOutgoingEdge(OutgoingEdgesTaggedWebsites);

            #endregion

            #region define type "Website"

            //create a VertexTypePredefinition
            var Website_VertexTypePredefinition = new VertexTypePredefinition("Website");

            //create properties
            PropertyName = new PropertyPredefinition("Name", "String")
                                       .SetComment("This is a property on type 'Website' named 'Name' and is of type 'String'");

            var PropertyUrl = new PropertyPredefinition("URL", "String")
                                         .SetAsMandatory();

            //add properties
            Website_VertexTypePredefinition.AddProperty(PropertyName);
            Website_VertexTypePredefinition.AddProperty(PropertyUrl);

            #region create an index on type "Website" on property "Name"
            //there are three ways to set an index on property "Name" 
            //Beware: Use just one of them!

            //1. create an index definition and specifie the property- and type name
            var MyIndex = new IndexPredefinition("MyIndex").SetIndexType("SonesIndex").AddProperty("Name").SetVertexType("Website");
            //add index
            Website_VertexTypePredefinition.AddIndex((IndexPredefinition)MyIndex);

            //2. on creating the property definition of property "Name" call the SetAsIndexed() method, the GraphDB will create the index
            //PropertyName = new PropertyPredefinition("Name")
            //                           .SetAttributeType("String")
            //                           .SetComment("This is a property on type 'Website' with name 'Name' and is of type 'String'")
            //                           .SetAsIndexed();

            //3. make a create index request, like creating a type
            //BEWARE: This statement must be execute AFTER the type "Website" is created.
            //var MyIndex = GraphDSServer.CreateIndex<IIndexDefinition>(SecToken,
            //                                                          TransToken,
            //                                                          new RequestCreateIndex(
            //                                                          new IndexPredefinition("MyIndex")
            //                                                                   .SetIndexType("SonesIndex")
            //                                                                   .AddProperty("Name")
            //                                                                   .SetVertexType("Website")), (Statistics, Index) => Index);

            #endregion

            //add IncomingEdge "Tags", the related OutgoingEdge is "TaggedWebsites" on type "Tag"
            Website_VertexTypePredefinition.AddIncomingEdge(new IncomingEdgePredefinition("Tags",
                                                                                            "Tag",
                                                                                            "TaggedWebsites"));

            #endregion


            #region create types by sending requests

            //create the types "Tag" and "Website"
            var DBTypes = GraphDSClient.CreateVertexTypes<IEnumerable<IVertexType>>(SecToken,
                                                                                    TransToken,
                                                                                    new RequestCreateVertexTypes(
                                                                                        new List<VertexTypePredefinition> { Tag_VertexTypePredefinition, 
                                                                                                                            Website_VertexTypePredefinition }),
                                                                                    (Statistics, VertexTypes) => VertexTypes);

            /* 
             * BEWARE: The following two operations won't work because the two types "Tag" and "Website" depending on each other,
             *          because one type has an incoming edge to the other and the other one has an incoming edge, 
             *          so they cannot be created separate (by using create type),
             *          they have to be created at the same time (by using create types)
             *          
             * //create the type "Website"
             * var Website = GraphDSServer.CreateVertexType<IVertexType>(SecToken, 
             *                                                           TransToken, 
             *                                                           new RequestCreateVertexType(Website_VertexTypePredefinition), 
             *                                                           (Statistics, VertexType) => VertexType);
             * 
             * //create the type "Tag"
             * var Tag = GraphDSServer.CreateVertexType<IVertexType>(SecToken, 
             *                                                       TransToken, 
             *                                                       new RequestCreateVertexType(Tag_VertexTypePredefinition), 
             *                                                       (Statistics, VertexType) => VertexType);
             */

            var Tag = DBTypes.Where(type => type.Name == "Tag").FirstOrDefault();
            if (Tag != null)
                Console.WriteLine("Vertex Type 'Tag' created");
            var Website = DBTypes.Where(type => type.Name == "Website").FirstOrDefault();
            if(Website != null)
                Console.WriteLine("Vertex Type 'Website' created");

            #endregion

            #region insert some Websites by sending requests

            var sones = GraphDSClient.Insert<IVertex>(SecToken, TransToken, new RequestInsertVertex("Website")
                                                                                    .AddStructuredProperty("Name", "Sones")
                                                                                    .AddStructuredProperty("URL", "http://sones.com/"),
                                                                                    (Statistics, Result) => Result);

            var cnn = GraphDSClient.Insert<IVertex>(SecToken, TransToken, new RequestInsertVertex("Website")
                                                                                    .AddStructuredProperty("Name", "CNN")
                                                                                    .AddStructuredProperty("URL", "http://cnn.com/"),
                                                                                    (Statistics, Result) => Result);

            var xkcd = GraphDSClient.Insert<IVertex>(SecToken, TransToken, new RequestInsertVertex("Website")
                                                                                    .AddStructuredProperty("Name", "xkcd")
                                                                                    .AddStructuredProperty("URL", "http://xkcd.com/"),
                                                                                    (Statistics, Result) => Result);

            var onion = GraphDSClient.Insert<IVertex>(SecToken, TransToken, new RequestInsertVertex("Website")
                                                                                    .AddStructuredProperty("Name", "onion")
                                                                                    .AddStructuredProperty("URL", "http://theonion.com/"),
                                                                                    (Statistics, Result) => Result);

            //adding an unknown property means the property isn't defined before
            var test = GraphDSClient.Insert<IVertex>(SecToken, TransToken, new RequestInsertVertex("Website")
                                                                                    .AddStructuredProperty("Name", "Test")
                                                                                    .AddStructuredProperty("URL", "")
                                                                                    .AddUnknownProperty("Unknown", "unknown property"),
                                                                                    (Statistics, Result) => Result);

            if (sones != null)
                Console.WriteLine("Website 'sones' successfully inserted");

            if (cnn != null)
                Console.WriteLine("Website 'cnn' successfully inserted");

            if (xkcd != null)
                Console.WriteLine("Website 'xkcd' successfully inserted");

            if (onion != null)
                Console.WriteLine("Website 'onion' successfully inserted");

            if (test != null)
                Console.WriteLine("Website 'test' successfully inserted");

            #endregion

            #region insert some Tags by sending requests

            //insert a "Tag" with an OutgoingEdge to a "Website" include that the GraphDB creates an IncomingEdge on the given Website instances
            //(because we created an IncomingEdge on type "Website") --> as a consequence we never have to set any IncomingEdge
            var good = GraphDSClient.Insert<IVertex>(SecToken, TransToken, new RequestInsertVertex("Tag")
                                                                                    .AddStructuredProperty("Name", "good")
                                                                                    .AddEdge(new EdgePredefinition("TaggedWebsites")
                                                                                        .AddVertexID(Website.ID, cnn.VertexID)
                                                                                        .AddVertexID(Website.ID, xkcd.VertexID)),
                                                                                    (Statistics, Result) => Result);

            var funny = GraphDSClient.Insert<IVertex>(SecToken, TransToken, new RequestInsertVertex("Tag")
                                                                                    .AddStructuredProperty("Name", "funny")
                                                                                    .AddEdge(new EdgePredefinition("TaggedWebsites")
                                                                                        .AddVertexID(Website.ID, xkcd.VertexID)
                                                                                        .AddVertexID(Website.ID, onion.VertexID)),
                                                                                    (Statistics, Result) => Result);

            if (good != null)
                Console.WriteLine("Tag 'good' successfully inserted");

            if (funny != null)
                Console.WriteLine("Tag 'funny' successfully inserted");

            #endregion

            #region how to get a type from the DB, properties of the type, instances of a specific type and read out property values

            //how to get a type from the DB
            var TagDBType = GraphDSClient.GetVertexType<IVertexType>(SecToken, TransToken, new RequestGetVertexType(Tag.Name), (Statistics, Type) => Type);

            //how to get a type from the DB
            var WebsiteDBType = GraphDSClient.GetVertexType<IVertexType>(SecToken, TransToken, new RequestGetVertexType(Website.Name), (Statistics, Type) => Type);

            //read informations from type
            var typeName = TagDBType.Name;
            //are there other types wich extend the type "Tag"
            var hasChildTypes = TagDBType.HasChildTypes;

            var hasPropName = TagDBType.HasProperty("Name");

            //get the definition of the property "Name"
            var propName = TagDBType.GetPropertyDefinition("Name");

            //how to get all instances of a type from the DB
            var TagInstances = GraphDSClient.GetVertices(SecToken, TransToken, new RequestGetVertices(TagDBType.Name), (Statistics, Vertices) => Vertices);

            foreach (var item in TagInstances)
            {
                //to get the value of a property of an instance, you need the property ID 
                //(that's why we fetched the type from DB an read out the property definition of property "Name")
                var name = item.GetPropertyAsString(propName.ID);
            }

            Console.WriteLine("API operations finished...");

            #endregion
        }
示例#17
0
        private static PropertyPredefinition ConvertUnknownToProperty(UnknownAttributePredefinition unknown)
        {
            if (unknown.EdgeType != null)
                throw new Exception("An edge type is not allowed on a property.");

            var prop = new PropertyPredefinition(unknown.AttributeName)
                           .SetDefaultValue(unknown.DefaultValue)
                           .SetAttributeType(unknown.AttributeType)
                           .SetComment(unknown.Comment);

            if (unknown.IsUnique)
                prop.SetAsUnique();

            if (unknown.IsMandatory)
                prop.SetAsMandatory();

            if (unknown.Multiplicity != null)
                switch (unknown.Multiplicity)
                {
                    case UnknownAttributePredefinition.LISTMultiplicity:
                        prop.SetMultiplicityToList();
                        break;
                    case UnknownAttributePredefinition.SETMultiplicity:
                        prop.SetMultiplicityToSet();
                        break;
                    default:
                        throw new Exception("Unknown multiplicity for properties.");
                }
            return prop;
        }