protected void InitExtensions(MoSync.Core core, MoSync.Runtime runtime) { try { MoSync.ExtensionsLoader.Load(); } catch (Exception e) { MoSync.Util.CriticalError("Couldn't load extension: " + e.ToString()); } MoSync.ExtensionModule extMod = runtime.GetModule <MoSync.ExtensionModule>(); System.Reflection.Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (System.Reflection.Assembly a in assemblies) { try { foreach (Type t in a.GetTypes()) { IExtensionModule extensionGroupInstance = null; if (t.GetInterface("MoSync.IExtensionModule", false) != null) { extensionGroupInstance = Activator.CreateInstance(t) as IExtensionModule; extMod.AddModule(extensionGroupInstance); extensionGroupInstance.Init(core, runtime); } } } catch { } } }
public void WithModulesDoesNotDependOnSequence() { // arrange var language = Language.Default; ISemanticNetwork semanticNetwork = new SemanticNetwork(language); var modules = new IExtensionModule[] { new TestModule("4", new string[] { "2", "3" }), new TestModule("5", new string[] { "1", "4" }), new TestModule("0"), new TestModule("2", new string[] { "1" }), new TestModule("3", new string[] { "1" }), new TestModule("1", new string[] { "0" }), }; // act semanticNetwork = semanticNetwork.WithModules(modules); var appliedModules = semanticNetwork.Modules.Keys; var expectedModules = modules.Select(m => m.Name).ToList(); // assert Assert.AreEqual(expectedModules.Count, appliedModules.Count); Assert.IsFalse(expectedModules.Except(appliedModules).Any()); }
public void Init(Syscalls syscalls, Core core, Runtime runtime) { syscalls.maExtensionFunctionInvoke = delegate(int extensionId, int a, int b, int c) { int _module = extensionId >> 8; if (_module >= 0 && _module < mModules.Count) { IExtensionModule module = mModules[_module]; int function = extensionId & 0xff; return(module.Invoke(core, function, a, b, c)); } return(MoSync.Constants.MA_EXTENSION_FUNCTION_UNAVAILABLE); }; }
protected void InitExtensions(Core core, Runtime runtime) { foreach (Type t in Assembly.GetCallingAssembly().GetTypes()) { IExtensionModule extensionGroupInstance = null; if (t.GetInterface("MoSync.IExtensionModule", false) != null) { extensionGroupInstance = Activator.CreateInstance(t) as IExtensionModule; mModules.Add(extensionGroupInstance.GetName(), extensionGroupInstance); } } foreach (KeyValuePair <String, IExtensionModule> module in mModules) { module.Value.Init(core, runtime); } }
public void WithModulesFailsIfCannotResolveDependencies() { // arrange var language = Language.Default; ISemanticNetwork semanticNetwork = new SemanticNetwork(language); var modules = new IExtensionModule[] { new TestModule("1"), new TestModule("2", new string[] { "3" }), }; // act & assert var error = Assert.Throws <ModuleException>(() => semanticNetwork.WithModules(modules)); foreach (var module in modules) { Assert.AreEqual("2", error.ModuleName); } }
protected void InitExtensions(Core core, Runtime runtime) { //Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); //foreach(Assembly a in assemblies) //foreach (Type t in a.GetTypes()) foreach (Type t in Assembly.GetExecutingAssembly().GetTypes()) { IExtensionModule extensionGroupInstance = null; if (t.GetInterface("MoSync.IExtensionModule", false) != null) { extensionGroupInstance = Activator.CreateInstance(t) as IExtensionModule; mModules.Add(extensionGroupInstance); } } foreach (IExtensionModule module in mModules) { module.Init(core, runtime); } }
public void Init(Syscalls syscalls, Core core, Runtime runtime) { InitExtensions(core, runtime); syscalls.maInvokeExtension = delegate(int extensionId, int a, int b, int c) { int ptr = core.GetDataMemory().ReadInt32(extensionId + 0); String module = core.GetDataMemory().ReadStringAtAddress(ptr); int index = core.GetDataMemory().ReadInt32(extensionId + 4); IExtensionModule extension = GetModule(module); if (extension == null) { return(MoSync.Constants.IOCTL_UNAVAILABLE); } else { return(extension.Invoke(index, a, b, c)); } }; }
public void WithModulesFailsOnCircularDependencies() { // arrange var language = Language.Default; ISemanticNetwork semanticNetwork = new SemanticNetwork(language); var modules = new IExtensionModule[] { new TestModule("1", new string[] { "2" }), new TestModule("2", new string[] { "3" }), new TestModule("3", new string[] { "1" }), }; // act & assert var error = Assert.Throws <ModuleException>(() => semanticNetwork.WithModules(modules)); foreach (var module in modules) { Assert.IsTrue(error.ModuleName.Contains(module.Name)); } }
public void Init(Ioctls ioctls, Core core, Runtime runtime) { InitExtensions(core, runtime); ioctls.maExtensionModuleLoad = delegate(int _name, int _hash) { int handle = MoSync.Constants.MA_EXTENSION_MODULE_UNAVAILABLE; String name = core.GetDataMemory().ReadStringAtAddress(_name); IExtensionModule module = null; if ((module = GetModule(name, out handle)) != null) { if (module.GetHash() != (uint)_hash) { MoSync.Util.CriticalError("Invalid extension hash!"); } return(handle); } return(handle); }; ioctls.maExtensionFunctionLoad = delegate(int _module, int _index) { int handle = MoSync.Constants.MA_EXTENSION_FUNCTION_UNAVAILABLE; if (_module >= 0 && _module < mModules.Count) { // maybe have method count as a generated part of the extension return((_module << 8) | (_index & 0xff)); } return(handle); }; }
public void AddModule(IExtensionModule mod) { mModules.Add(mod); }
protected void UseExtension(IExtensionModule extensionModule) { extensionModules.Add(extensionModule); }
public void Add(IExtensionModule mod) { moduleFuncs.Add(() => mod); }
static void Main(string[] args) { #region Preparation section Console.WriteLine("This sample demonstrates simple work with concepts and statements."); Console.WriteLine("It works with biological classification of Bears."); Console.WriteLine("(More details abour Bears can be found here: https://en.wikipedia.org/wiki/Ursus_(mammal))"); Console.WriteLine(); // We need Language in order to create Semantic Network - so, let's use default one. ILanguage language = Language.Default; Console.WriteLine($"Selected language: ({language.Culture}) {language.Name}"); // Initialize modules metadata var modules = new IExtensionModule[] { new BooleanModule(), new ClassificationModule(), new SetModule(), //new MathematicsModule(), //new ProcessesModule(), }; foreach (var module in modules) { module.RegisterMetadata(); } Console.WriteLine("Modules are initialized..."); // Semantic Network is our starting point. ISemanticNetwork semanticNetwork = new SemanticNetwork(language) .WithModules(modules); Console.WriteLine("Semantic network is created..."); // Some concepts are predefined. All of them can be found using Inventor.Semantics.SystemConcepts.GetAll() method. Console.WriteLine($"Semantic network contains {semanticNetwork.Concepts.Count} concepts."); Console.WriteLine(); #endregion #region Define concepts // Need to define our own concepts to work with ... IConcept animal = "Kingdom: Animalia".CreateConcept("Animal"); IConcept chordate = "Phylum: Chordata".CreateConcept("Chordate"); IConcept mammal = "Class: Mammalia".CreateConcept("Mammal"); IConcept carnivor = "Order: Carnivora".CreateConcept("Carnivor"); // Just skip Ursidae, Ursinae and Ursini for short. IConcept bear = "Genus: Ursus".CreateConcept("Bear"); IConcept americanBlackBear = "Ursus americanus".CreateConcept("American black bear"); IConcept brownBear = "Ursus arctos".CreateConcept("Brown bear"); IConcept polarBear = "Ursus maritimus".CreateConcept("Polar bear"); IConcept asianBlackBear = "Ursus thibetanuss".CreateConcept("Asian black bear"); // Pay attention to attributes of concepts below. IConcept hairColor = "Hair color".CreateConcept("Hair color").WithAttribute(IsSignAttribute.Value); IConcept black = "Color: Black".CreateConcept("Black").WithAttribute(IsValueAttribute.Value); IConcept brown = "Color: Brown".CreateConcept("Brown").WithAttribute(IsValueAttribute.Value); IConcept white = "Color: White".CreateConcept("White").WithAttribute(IsValueAttribute.Value); // ... and add them to our semantic network. semanticNetwork.Concepts.Add(animal); semanticNetwork.Concepts.Add(chordate); semanticNetwork.Concepts.Add(mammal); semanticNetwork.Concepts.Add(carnivor); semanticNetwork.Concepts.Add(bear); semanticNetwork.Concepts.Add(americanBlackBear); semanticNetwork.Concepts.Add(brownBear); semanticNetwork.Concepts.Add(polarBear); semanticNetwork.Concepts.Add(asianBlackBear); semanticNetwork.Concepts.Add(hairColor); semanticNetwork.Concepts.Add(black); semanticNetwork.Concepts.Add(brown); semanticNetwork.Concepts.Add(white); // Actually, this sample is so easy, that we could skip adding concepts to semantic network... #endregion #region Define statements // The most important part of preparation work is definition of statements. semanticNetwork.DeclareThat(chordate).IsDescendantOf(animal); // Or we can use DeclareThat(animal).IsAncestorOf(chordate) instead. semanticNetwork.DeclareThat(mammal).IsDescendantOf(chordate); semanticNetwork.DeclareThat(carnivor).IsDescendantOf(mammal); semanticNetwork.DeclareThat(bear).IsDescendantOf(carnivor); semanticNetwork.DeclareThat(americanBlackBear).IsDescendantOf(bear); semanticNetwork.DeclareThat(brownBear).IsDescendantOf(bear); semanticNetwork.DeclareThat(polarBear).IsDescendantOf(bear); semanticNetwork.DeclareThat(asianBlackBear).IsDescendantOf(bear); // 3 statements below are also redundant for this simple case. semanticNetwork.DeclareThat(black).IsDescendantOf(hairColor); semanticNetwork.DeclareThat(brown).IsDescendantOf(hairColor); semanticNetwork.DeclareThat(white).IsDescendantOf(hairColor); semanticNetwork.DeclareThat(bear).HasSign(hairColor); // Or we can use semanticNetwork.DeclareThat(hairColor).IsSignOf(bear) instead. semanticNetwork.DeclareThat(americanBlackBear).HasSignValue(hairColor, black); // Or we can use semanticNetwork.DeclareThat(black).IsSignValue(americanBlackBear, hairColor) instead. semanticNetwork.DeclareThat(brownBear).HasSignValue(hairColor, brown); semanticNetwork.DeclareThat(polarBear).HasSignValue(hairColor, white); semanticNetwork.DeclareThat(asianBlackBear).HasSignValue(hairColor, black); #endregion #region Ask questions // Now we can work with our Semantic Network and ask questions. writeAnswer( "Question 1: Is Bear an animal?", semanticNetwork.Ask().IfIs(bear, animal)); // There are no explicit knowledge about this. writeAnswer( "Question 2: What kind of bears are there?", semanticNetwork.Ask().WhichDescendantsHas(bear)); writeAnswer( "Question 3: What is the difference between Polar and Brown bears?", semanticNetwork.Ask().WhatIsTheDifference(polarBear, brownBear)); writeAnswer( "Question 4: What in common American and Asian black bears have?", semanticNetwork.Ask().WhatInCommon(americanBlackBear, asianBlackBear)); #endregion Console.WriteLine(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }
public static ICollection <String> GetMissingDependencies(this ISemanticNetwork semanticNetwork, IExtensionModule module) { return(module.Dependencies.Except(semanticNetwork.Modules.Keys).ToList()); }