1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Collections;
|
---|
27 | using HeuristicLab.GP.Interfaces;
|
---|
28 | using HeuristicLab.PluginInfrastructure;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.GP {
|
---|
31 | public static class GeneralTreeSerializer {
|
---|
32 | private static IFunctionTreeSerializer serializer;
|
---|
33 |
|
---|
34 | internal static string Export(IFunctionTree functionTree) {
|
---|
35 | if (serializer != null || FindSerializer(functionTree)) {
|
---|
36 | return serializer.Export(functionTree);
|
---|
37 | } else throw new NotSupportedException("Can't serialize function trees. A matching export couldn't be found");
|
---|
38 | }
|
---|
39 |
|
---|
40 | internal static IFunctionTree Import(string p) {
|
---|
41 | if (serializer != null || FindSerializer(p)) {
|
---|
42 | return serializer.Import(p);
|
---|
43 | } else throw new NotSupportedException("Can't serialize function trees. A matching export couldn't be found");
|
---|
44 | }
|
---|
45 |
|
---|
46 | private static bool FindSerializer(IFunctionTree functionTree) {
|
---|
47 | DiscoveryService discoveryService = new DiscoveryService();
|
---|
48 | string exported;
|
---|
49 |
|
---|
50 | foreach (IFunctionTreeSerializer exporter in discoveryService.GetInstances<IFunctionTreeSerializer>()) {
|
---|
51 | if (exporter.TryExport(functionTree, out exported)) {
|
---|
52 | GeneralTreeSerializer.serializer = exporter;
|
---|
53 | return true;
|
---|
54 | }
|
---|
55 | }
|
---|
56 | return false;
|
---|
57 | }
|
---|
58 | private static bool FindSerializer(string functionTree) {
|
---|
59 | DiscoveryService discoveryService = new DiscoveryService();
|
---|
60 | IFunctionTree exported;
|
---|
61 |
|
---|
62 | foreach (IFunctionTreeSerializer importer in discoveryService.GetInstances<IFunctionTreeSerializer>()) {
|
---|
63 | if (importer.TryImport(functionTree, out exported)) {
|
---|
64 | GeneralTreeSerializer.serializer = importer;
|
---|
65 | return true;
|
---|
66 | }
|
---|
67 | }
|
---|
68 | return false;
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|