Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.CEDMA.Operators/SelectProperties.cs @ 6486

Last change on this file since 6486 was 549, checked in by gkronber, 16 years ago

improved SelectProperties operator to query only one specific predicate. #200 (Simple indexer for results and matching search-frontend for solution-quality)

File size: 4.9 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using System.Threading;
28using HeuristicLab.CEDMA.DB.Interfaces;
29using System.ServiceModel;
30using HeuristicLab.CEDMA.Core;
31using HeuristicLab.Operators;
32using System.IO;
33using System.Xml;
34
35namespace HeuristicLab.CEDMA.Operators {
36  public class SelectProperties : OperatorBase {
37    private static readonly string cedmaNamespace = "http://www.heuristiclab.com/cedma/";
38
39    public override string Description {
40      get { return "TASK."; }
41    }
42
43    public SelectProperties()
44      : base() {
45      AddVariableInfo(new VariableInfo("CedmaServerUri", "Uri of the CEDMA server", typeof(StringData), VariableKind.In));
46      AddVariableInfo(new VariableInfo("SubjectGuid", "", typeof(StringData), VariableKind.In));
47      AddVariableInfo(new VariableInfo("Predicate", "", typeof(StringData), VariableKind.In));
48      AddVariableInfo(new VariableInfo("Property", "", typeof(IItem), VariableKind.New));
49    }
50
51    public override IOperation Apply(IScope scope) {
52      string serverUrl = GetVariableValue<StringData>("CedmaServerUri", scope, true).Data;
53      StringData subjectGuid = GetVariableValue<StringData>("SubjectGuid", scope, true);
54      StringData predicate = GetVariableValue<StringData>("Predicate", scope, true);
55
56      NetTcpBinding binding = new NetTcpBinding();
57      binding.MaxReceivedMessageSize = 10000000; // 10Mbytes
58      binding.ReaderQuotas.MaxStringContentLength = 10000000; // also 10M chars
59      binding.ReaderQuotas.MaxArrayLength = 10000000; // also 10M elements;
60      binding.Security.Mode = SecurityMode.None;
61      using(ChannelFactory<IStore> factory = new ChannelFactory<IStore>(binding)) {
62        IStore store = factory.CreateChannel(new EndpointAddress(serverUrl));
63        Statement template = new Statement(
64          new Entity(cedmaNamespace+subjectGuid.Data),
65          new Entity(cedmaNamespace+predicate.Data),
66          new Entity(null));
67        IList<Statement> result = store.Select(template);
68
69        if(result.Count == 1) {
70          Statement s = result[0];
71          Variable var = new Variable();
72          var.Name = scope.TranslateName("Property");
73          scope.AddVariable(var);
74          if(s.Property is Literal) {
75            var.Value = TranslateLiteral((Literal)s.Property);
76          } else if(s.Property is SerializedLiteral) {
77            var.Value = TranslateLiteral((SerializedLiteral)s.Property);
78          } else {
79            var.Value = new StringData(((Entity)s.Property).Uri.Replace(cedmaNamespace, ""));
80          }
81        } else {
82          ItemList<IItem> resultValues = new ItemList<IItem>();
83          scope.AddVariable(new Variable(scope.TranslateName("Property"), resultValues));
84          foreach(Statement s in result) {
85            if(s.Property is Literal) {
86              resultValues.Add(TranslateLiteral((Literal)s.Property));
87            } else if(s.Property is SerializedLiteral) {
88              resultValues.Add(TranslateLiteral((SerializedLiteral)s.Property));
89            } else {
90              resultValues.Add(new StringData(((Entity)s.Property).Uri.Replace(cedmaNamespace, "")));
91            }
92          }
93        }
94      }
95      return null;
96    }
97
98    private IItem TranslateLiteral(SerializedLiteral serializedLiteral) {
99      XmlDocument doc = new XmlDocument();
100      doc.LoadXml(serializedLiteral.RawData);     
101      return (IItem)PersistenceManager.Restore(doc.ChildNodes[1], new Dictionary<Guid, IStorable>());
102    }
103
104    private IItem TranslateLiteral(Literal literal) {
105      if(literal.Value is bool) {
106        return new BoolData((bool)literal.Value);
107      } else if(literal.Value is double) {
108        return new DoubleData((double)literal.Value);
109      } else if(literal.Value is int) {
110        return new IntData((int)literal.Value);
111      } else if(literal.Value is string) {
112        return new StringData((string)literal.Value);
113      } else {
114        return new StringData(literal.Value.ToString());
115      }
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.