Пример #1
0
        static void Main(string[] args)
        {
            // Define a component with custom name. This name will be used to access the component on any entity.
            ComponentPrototype p = new ComponentPrototype("component");

            // Add a set of attributes. The names are used to access the attributes on instatiated components
            p.AddAttribute <float>("numeric");
            p.AddAttribute <string>("string");
            p.AddAttribute <bool>("boolean");

            // Once the protoype and its set of attributes is defined, register it to the global Component Registry
            ComponentRegistry.Instance.Register(p);

            // We are now all set to use our defined component on entities. For this, first, we create a new empty entity.
            Entity e = new Entity();

            // Components are automatically instatiated on first access. So we can just set the values on our entity as follow:
            e["component"]["numeric"].Set(3.14);
            e["component"]["string"].Set("Hello World");
            e["component"]["boolean"].Set(true);

            // Last, we expose our entity on an HTTP datapoint as Linked Data object. The ECA2LD lib will take care of building the correct
            // RDF graph, and creating and wiring datapoints for the linked component and attribute instances.
            var eDP = new EntityDatapoint(e, "http://localhost:12345/entities/e/");

            // Our entity is now ready and set to be added to the world. The attributes could have been set as above afterwards as well.
            // Then events would have informed other parts of the program that our entity was changed.
            CEC.Instance.Add(e);

            // This concludes the example. In the future, support to add datapoints on the Entity Collection should be implemented. This
            // would automatize the process of creating datapoints for each entity manually.
            Console.ReadKey();
        }
Пример #2
0
        public static void Register()
        {
            ComponentPrototype geometry = new ComponentPrototype("geometry");

            geometry.AddAttribute <byte[]>("binaryMesh");
            geometry.AddAttribute <string>("filetype");
            ComponentRegistry.Instance.Register(geometry);
        }
Пример #3
0
        public static void Register()
        {
            ComponentPrototype robot = new ComponentPrototype("robot");

            robot.AddAttribute <Entity>("arm");
            robot.AddAttribute <Entity>("platform");
            ComponentRegistry.Instance.Register(robot);
        }
Пример #4
0
        public static void RegisterComponents()
        {
            ComponentPrototype spatial = new ComponentPrototype("spatial");

            spatial.AddAttribute <position>("position");
            spatial.AddAttribute <orientation>("orientation");
            spatial.AddAttribute <string>("id");
            ComponentRegistry.Instance.Register(spatial);
        }
 private ComponentChipDefinition(
     string chipName,
     ComponentPrototype componentPrototype,
     Dictionary <ISignal, ISignal>[] portNameMappings,
     Dictionary <ISignal, SignalName> constAssignMappings,
     Dictionary <string, object> chipAttribute)
 {
     this.chipName            = chipName;
     this.componentPrototype  = componentPrototype;
     this.portNameMappings    = portNameMappings;
     this.constAssignMappings = constAssignMappings;
     this.chipAttribute       = chipAttribute;
 }
Пример #6
0
        public override object Evaluate(ParseTreeNode node)
        {
            string entityName  = node.ChildNodes[1].Token.Text;
            var    portSignals = new SignalTable();

            var portClauseNode = node.ChildNodes[4].ChildNodes[0];

            foreach (var declarationNode in portClauseNode.ChildNodes[2].ChildNodes)
            {
                if (declarationNode.ChildNodes[0].Term.Name == "object_declaration")
                {
                    var signals = (List <ISignal>)EvaluateGeneral(declarationNode.ChildNodes[0]);
                    foreach (var signal in signals)
                    {
                        portSignals[signal.name] = signal;
                    }
                }
            }

            var entity = new ComponentPrototype(entityName, portSignals);

            this.declaredObjects.componentDeclarations[entityName] = entity;
            return(entity);
        }
        public static ComponentChipDefinition ImportFromFile(string fileName)
        {
            var portNameMappings   = new List <Dictionary <ISignal, ISignal> >();
            var constAssignMapping = new Dictionary <ISignal, SignalName>();

            var objects = (new Parser.MyParser()).Parse(fileName);

            if (objects.components.Count == 0 ||
                objects.componentDeclarations.Count == 1 ||
                objects.logicGates.Count > 0)
            {
                return(null);
            }

            string chipName = objects.entityPrototype.name;

            // チップの入出力信号
            var portSet = new HashSet <ISignal>(objects.signalTable.Values.Where(
                                                    x => (x.mode == SignalMode.IN || x.mode == SignalMode.OUT || x.mode == SignalMode.INOUT) &&
                                                    x.GetType() == typeof(StdLogic)));

            // ポートの対応関係を作成
            ComponentPrototype componentPrototype = objects.components[0].prototype;

            foreach (var component in objects.components)
            {
                if (component.prototype != componentPrototype)
                {
                    throw new ChipDefinitionException(fileName,
                                                      string.Format(@"Component chip ""{0}"" has another component ""{1}""",
                                                                    componentPrototype.name, component.prototype.name));
                }

                var portNameMap = new Dictionary <ISignal, ISignal>();

                foreach (var componentSignal in component.portMap.Keys)
                {
                    if (componentSignal.mode == SignalMode.IN ||
                        componentSignal.mode == SignalMode.OUT ||
                        componentSignal.mode == SignalMode.INOUT)
                    {
                        if (!portSet.Contains(component.portMap[componentSignal]))
                        {
                            throw new ChipDefinitionException(fileName,
                                                              string.Format("The port signal of the component must be connected directly to the port of the component"));
                        }

                        portSet.Remove(component.portMap[componentSignal]);
                        portNameMap.Add(componentSignal, component.portMap[componentSignal]);
                    }
                }

                portNameMappings.Add(portNameMap);
            }

            // チップの入力でコンポネントの入出力以外に接続するもの
            foreach (var port in objects.signalTable.Values)
            {
                if ((port.mode == SignalMode.IN || port.mode == SignalMode.INOUT) &&
                    portSet.Contains(port) && /* まだ接続されていない */
                    port.attribute.ContainsKey("const_assign"))
                {
                    var constValue = port.attribute["const_assign"];
                    if (constValue is string)
                    {
                        constAssignMapping[port] = SignalName.Parse((string)constValue);
                    }
                }
            }

            return(new ComponentChipDefinition(chipName, componentPrototype, portNameMappings.ToArray(), constAssignMapping, objects.entityAttribute));
        }
Пример #8
0
        public static void Register(JavaScriptContext context)
        {
            JavaScriptValue ComponentPrototype;
            JavaScriptValue ComponentConstructor = Bridge.CreateConstructor(
                typeof(UnityEngine.Component),
                (args) => { throw new System.NotImplementedException(); },
                out ComponentPrototype
                );

            JavaScriptValue
            .GlobalObject
            .GetProperty("UnityEngine")
            .SetProperty("Component", ComponentConstructor);


            // Static Fields


            // Static Property Accessors


            // Static Methods


            // Instance Fields


            // Instance Property Accessors

            Bridge.DefineGetter(
                ComponentPrototype,
                "transform",
                Bridge.WithExternal <UnityEngine.Component>((o, args) => Bridge.CreateExternalWithPrototype(o.transform))
                );


            Bridge.DefineGetter(
                ComponentPrototype,
                "gameObject",
                Bridge.WithExternal <UnityEngine.Component>((o, args) => Bridge.CreateExternalWithPrototype(o.gameObject))
                );


            Bridge.DefineGetterSetter(
                ComponentPrototype,
                "tag",
                Bridge.WithExternal <UnityEngine.Component>((o, args) => JavaScriptValue.FromString(o.tag)),
                Bridge.WithExternal <UnityEngine.Component>((o, args) => { o.tag = args[1].ToString(); })
                );


            // Instance Methods

            ComponentPrototype.SetProperty("GetComponent", Bridge.CreateFunction(Bridge.WithExternal <UnityEngine.Component>((o, args) => {
                string typeName;
                if (args[1].ValueType == JavaScriptValueType.Function)
                {
                    typeName = args[1].GetProperty("name").ToString();
                }
                else if (args[1].ValueType == JavaScriptValueType.String)
                {
                    typeName = args[1].ToString();
                }
                else
                {
                    throw new System.Exception("Argument passed to GetComponent must be a class or string");
                }

                System.Type type          = Bridge.GetType(typeName);
                JavaScriptValue prototype = Bridge.GetPrototype(typeName);

                return(Bridge.CreateExternalWithPrototype(
                           o.GetComponent(type),
                           prototype
                           ));
            })));


            ComponentPrototype.SetProperty(
                "GetComponentInChildren",
                Bridge.CreateFunction(
                    "GetComponentInChildren",
                    Bridge.WithExternal <UnityEngine.Component>((o, args) => {
                string typeName = args[1].GetProperty("name").ToString();

                System.Type type          = Bridge.GetType(typeName);
                JavaScriptValue prototype = Bridge.GetPrototype(typeName);

                if (args.Length == 2)
                {
                    return(Bridge.CreateExternalWithPrototype(
                               o.GetComponentInChildren(type),
                               prototype
                               ));
                }
                else
                {
                    return(Bridge.CreateExternalWithPrototype(
                               o.GetComponentInChildren(type, args[2].ToBoolean()),
                               prototype
                               ));
                }
            })
                    )
                );

            ComponentPrototype.SetProperty(
                "GetComponentInParent",
                Bridge.CreateFunction(
                    "GetComponentInParent",
                    Bridge.WithExternal <UnityEngine.Component>((o, args) => {
                string typeName = args[1].GetProperty("name").ToString();

                System.Type type          = Bridge.GetType(typeName);
                JavaScriptValue prototype = Bridge.GetPrototype(typeName);

                return(Bridge.CreateExternalWithPrototype(
                           o.GetComponentInParent(type),
                           prototype
                           ));
            })
                    )
                );


            ComponentPrototype.SetProperty(
                "GetComponents",
                Bridge.CreateFunction(
                    "GetComponents",
                    Bridge.WithExternal <UnityEngine.Component>((o, args) => {
                string typeName = args[1].GetProperty("name").ToString();

                System.Type type          = Bridge.GetType(typeName);
                JavaScriptValue prototype = Bridge.GetPrototype(typeName);


                JavaScriptValue array = JavaScriptValue.CreateArray(0);

                foreach (var component in o.GetComponents(type))
                {
                    array.GetProperty("push").CallFunction(array, Bridge.CreateExternalWithPrototype(
                                                               component,
                                                               prototype
                                                               ));
                }

                return(array);
            })
                    )
                );


            ComponentPrototype.SetProperty(
                "GetComponentsInChildren",
                Bridge.CreateFunction(
                    "GetComponentsInChildren",
                    Bridge.WithExternal <UnityEngine.Component>((o, args) => {
                string typeName = args[1].GetProperty("name").ToString();

                System.Type type          = Bridge.GetType(typeName);
                JavaScriptValue prototype = Bridge.GetPrototype(typeName);
                bool includeInactive      = args.Length == 3 ? args[2].ToBoolean() : false;

                JavaScriptValue array = JavaScriptValue.CreateArray(0);

                foreach (var component in o.GetComponentsInChildren(type, includeInactive))
                {
                    array.GetProperty("push").CallFunction(array, Bridge.CreateExternalWithPrototype(
                                                               component,
                                                               prototype
                                                               ));
                }

                return(array);
            })
                    )
                );


            ComponentPrototype.SetProperty(
                "GetComponentsInParent",
                Bridge.CreateFunction(
                    "GetComponentsInParent",
                    Bridge.WithExternal <UnityEngine.Component>((o, args) => {
                string typeName = args[1].GetProperty("name").ToString();

                System.Type type          = Bridge.GetType(typeName);
                JavaScriptValue prototype = Bridge.GetPrototype(typeName);
                bool includeInactive      = args.Length == 3 ? args[2].ToBoolean() : false;

                JavaScriptValue array = JavaScriptValue.CreateArray(0);

                foreach (var component in o.GetComponentsInParent(type, includeInactive))
                {
                    array.GetProperty("push").CallFunction(array, Bridge.CreateExternalWithPrototype(
                                                               component,
                                                               prototype
                                                               ));
                }

                return(array);
            })
                    )
                );


            ComponentPrototype.SetProperty(
                "CompareTag",
                Bridge.CreateFunction(
                    "CompareTag",
                    Bridge.WithExternal <UnityEngine.Component>((o, args) => JavaScriptValue.FromBoolean(o.CompareTag(args[1].ToString())))
                    )
                );


            ComponentPrototype.SetProperty(
                "SendMessageUpwards",
                Bridge.CreateFunction(
                    "SendMessageUpwards",
                    Bridge.WithExternal <UnityEngine.Component>((o, args) => o.SendMessageUpwards(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2]), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[3])))
                    )
                );


            ComponentPrototype.SetProperty(
                "SendMessageUpwards",
                Bridge.CreateFunction(
                    "SendMessageUpwards",
                    Bridge.WithExternal <UnityEngine.Component>((o, args) => o.SendMessageUpwards(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2])))
                    )
                );


            ComponentPrototype.SetProperty(
                "SendMessageUpwards",
                Bridge.CreateFunction(
                    "SendMessageUpwards",
                    Bridge.WithExternal <UnityEngine.Component>((o, args) => o.SendMessageUpwards(args[1].ToString()))
                    )
                );


            ComponentPrototype.SetProperty(
                "SendMessageUpwards",
                Bridge.CreateFunction(
                    "SendMessageUpwards",
                    Bridge.WithExternal <UnityEngine.Component>((o, args) => o.SendMessageUpwards(args[1].ToString(), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[2])))
                    )
                );


            ComponentPrototype.SetProperty(
                "SendMessage",
                Bridge.CreateFunction(
                    "SendMessage",
                    Bridge.WithExternal <UnityEngine.Component>((o, args) => o.SendMessage(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2])))
                    )
                );


            ComponentPrototype.SetProperty(
                "SendMessage",
                Bridge.CreateFunction(
                    "SendMessage",
                    Bridge.WithExternal <UnityEngine.Component>((o, args) => o.SendMessage(args[1].ToString()))
                    )
                );


            ComponentPrototype.SetProperty(
                "SendMessage",
                Bridge.CreateFunction(
                    "SendMessage",
                    Bridge.WithExternal <UnityEngine.Component>((o, args) => o.SendMessage(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2]), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[3])))
                    )
                );


            ComponentPrototype.SetProperty(
                "SendMessage",
                Bridge.CreateFunction(
                    "SendMessage",
                    Bridge.WithExternal <UnityEngine.Component>((o, args) => o.SendMessage(args[1].ToString(), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[2])))
                    )
                );


            ComponentPrototype.SetProperty(
                "BroadcastMessage",
                Bridge.CreateFunction(
                    "BroadcastMessage",
                    Bridge.WithExternal <UnityEngine.Component>((o, args) => o.BroadcastMessage(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2]), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[3])))
                    )
                );


            ComponentPrototype.SetProperty(
                "BroadcastMessage",
                Bridge.CreateFunction(
                    "BroadcastMessage",
                    Bridge.WithExternal <UnityEngine.Component>((o, args) => o.BroadcastMessage(args[1].ToString(), Bridge.GetExternal <System.Object>(args[2])))
                    )
                );


            ComponentPrototype.SetProperty(
                "BroadcastMessage",
                Bridge.CreateFunction(
                    "BroadcastMessage",
                    Bridge.WithExternal <UnityEngine.Component>((o, args) => o.BroadcastMessage(args[1].ToString()))
                    )
                );


            ComponentPrototype.SetProperty(
                "BroadcastMessage",
                Bridge.CreateFunction(
                    "BroadcastMessage",
                    Bridge.WithExternal <UnityEngine.Component>((o, args) => o.BroadcastMessage(args[1].ToString(), Bridge.GetExternal <UnityEngine.SendMessageOptions>(args[2])))
                    )
                );
        }