1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| package com.ontotext.wsmo4j.parser.xml; |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| import java.util.*; |
31 |
| |
32 |
| import org.deri.wsmo4j.logicalexpression.*; |
33 |
| import org.omwg.ontology.*; |
34 |
| import org.w3c.dom.*; |
35 |
| import org.wsmo.common.*; |
36 |
| import org.wsmo.common.Entity; |
37 |
| import org.wsmo.common.exception.*; |
38 |
| |
39 |
| import com.ontotext.wsmo4j.serializer.xml.*; |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| class NodeConcept { |
47 |
0
| static Concept deserialize(Node xmlNode, WsmlXmlParser parser) throws InvalidModelException {
|
48 |
0
| if (xmlNode == null || parser == null || xmlNode.getNodeName() != "concept") {
|
49 |
0
| throw new IllegalArgumentException();
|
50 |
| } |
51 |
| |
52 |
0
| IRI iriConcept = parser.getFactory().createIRI(WsmlXmlHelper.getAttrValue(xmlNode, "name"));
|
53 |
0
| Concept concept = parser.getFactory().createConcept(iriConcept);
|
54 |
| |
55 |
0
| NodeList nodes = xmlNode.getChildNodes();
|
56 |
0
| for (int i = 0; i < nodes.getLength(); i++) {
|
57 |
0
| Node node = nodes.item(i);
|
58 |
| |
59 |
0
| if (node.getNodeName() == "superConcept" && node.getNodeType() == Node.ELEMENT_NODE) {
|
60 |
0
| IRI scIri = parser.getFactory().createIRI(WsmlXmlHelper.getElementText(node));
|
61 |
0
| Concept superConcept = parser.getFactory().getConcept(scIri);
|
62 |
0
| concept.addSuperConcept(superConcept);
|
63 |
0
| superConcept.addSubConcept(concept);
|
64 |
| } |
65 |
0
| else if (node.getNodeName() == "nonFunctionalProperties"
|
66 |
| && node.getNodeType() == Node.ELEMENT_NODE) { |
67 |
0
| NodeNFP.deserialize(concept, node, parser);
|
68 |
| } |
69 |
0
| else if (node.getNodeName() == "attribute" && node.getNodeType() == Node.ELEMENT_NODE) {
|
70 |
0
| IRI attrIri = parser.getFactory().createIRI(
|
71 |
| WsmlXmlHelper.getAttrValue(node, "name")); |
72 |
0
| Attribute attr = concept.createAttribute(attrIri);
|
73 |
0
| String attrType = WsmlXmlHelper.getAttrValue(node, "type");
|
74 |
| |
75 |
0
| if (attrType.equals("inferring")) {
|
76 |
0
| attr.setConstraining(false);
|
77 |
| } |
78 |
| else { |
79 |
0
| attr.setConstraining(true);
|
80 |
| } |
81 |
| |
82 |
0
| NodeList attrNodes = node.getChildNodes();
|
83 |
0
| IRI inverseAttrIRI = null;
|
84 |
0
| for (int j = 0; j < attrNodes.getLength(); j++) {
|
85 |
0
| Node attrNode = attrNodes.item(j);
|
86 |
0
| if (attrNode.getNodeName() == "range"
|
87 |
| && attrNode.getNodeType() == Node.ELEMENT_NODE) { |
88 |
0
| ConstantTransformer cf = ConstantTransformer.getInstance();
|
89 |
0
| String type = WsmlXmlHelper.getElementText(attrNode);
|
90 |
0
| if (cf.isDataType(type)) {
|
91 |
0
| attr.addType(parser.getDataFactory().createWsmlDataType(type));
|
92 |
| } |
93 |
| else { |
94 |
0
| attr.addType(parser.getFactory().createConcept(
|
95 |
| parser.getFactory().createIRI(type))); |
96 |
| } |
97 |
| } |
98 |
0
| else if (attrNode.getNodeName() == "symmetric"
|
99 |
| && attrNode.getNodeType() == Node.ELEMENT_NODE) { |
100 |
0
| attr.setSymmetric(true);
|
101 |
| } |
102 |
0
| else if (attrNode.getNodeName() == "transitive"
|
103 |
| && attrNode.getNodeType() == Node.ELEMENT_NODE) { |
104 |
0
| attr.setTransitive(true);
|
105 |
| } |
106 |
0
| else if (attrNode.getNodeName() == "reflexive"
|
107 |
| && attrNode.getNodeType() == Node.ELEMENT_NODE) { |
108 |
0
| attr.setReflexive(true);
|
109 |
| } |
110 |
0
| else if (attrNode.getNodeName() == "inverseOf"
|
111 |
| && attrNode.getNodeType() == Node.ELEMENT_NODE) { |
112 |
0
| inverseAttrIRI = parser.getFactory().createIRI(
|
113 |
| WsmlXmlHelper.getAttrValue(attrNode, "type")); |
114 |
0
| attr.setInverseOf(inverseAttrIRI);
|
115 |
| } |
116 |
0
| else if (attrNode.getNodeName() == "minCardinality"
|
117 |
| && attrNode.getNodeType() == Node.ELEMENT_NODE) { |
118 |
0
| attr.setMinCardinality(Integer.parseInt(WsmlXmlHelper
|
119 |
| .getElementText(attrNode))); |
120 |
| } |
121 |
0
| else if (attrNode.getNodeName() == "maxCardinality"
|
122 |
| && attrNode.getNodeType() == Node.ELEMENT_NODE) { |
123 |
0
| attr.setMaxCardinality(Integer.parseInt(WsmlXmlHelper
|
124 |
| .getElementText(attrNode))); |
125 |
| } |
126 |
0
| else if (attrNode.getNodeName() == "nonFunctionalProperties"
|
127 |
| && attrNode.getNodeType() == Node.ELEMENT_NODE) { |
128 |
0
| NodeNFP.deserialize(attr, attrNode, parser);
|
129 |
| } |
130 |
| } |
131 |
| } |
132 |
| } |
133 |
| |
134 |
0
| return concept;
|
135 |
| } |
136 |
| |
137 |
0
| static Element serialize(Concept concept, WsmlXmlSerializer serializer) {
|
138 |
0
| Element conceptElement = serializer.createElement("concept");
|
139 |
0
| conceptElement.setAttribute("name", concept.getIdentifier().toString());
|
140 |
| |
141 |
0
| if (!concept.listSuperConcepts().isEmpty()) {
|
142 |
0
| Object[] entities = concept.listSuperConcepts().toArray();
|
143 |
0
| for (int i = 0; i < entities.length; i++) {
|
144 |
0
| Element superConcept = serializer.createElement("superConcept");
|
145 |
0
| conceptElement.appendChild(superConcept);
|
146 |
0
| superConcept.appendChild(serializer.createTextNode(((Entity) entities[i])
|
147 |
| .getIdentifier().toString())); |
148 |
| } |
149 |
| } |
150 |
0
| if (!concept.listNFPValues().isEmpty()) {
|
151 |
0
| NodeNFP.serialize(conceptElement, concept, serializer);
|
152 |
| } |
153 |
0
| if (!concept.listAttributes().isEmpty()) {
|
154 |
0
| Object[] attrList = concept.listAttributes().toArray();
|
155 |
0
| for (int i = 0; i < attrList.length; i++) {
|
156 |
0
| Element attrElement = serializer.createElement("attribute");
|
157 |
0
| conceptElement.appendChild(attrElement);
|
158 |
| |
159 |
0
| Attribute attribute = (Attribute) attrList[i];
|
160 |
0
| attrElement.setAttribute("name", attribute.getIdentifier().toString());
|
161 |
0
| attrElement.setAttribute("type", attribute.isConstraining() ? "constraining"
|
162 |
| : "inferring"); |
163 |
| |
164 |
0
| if (!attribute.listTypes().isEmpty()) {
|
165 |
0
| Element range = serializer.createElement("range");
|
166 |
0
| attrElement.appendChild(range);
|
167 |
0
| for (Iterator j = attribute.listTypes().iterator(); j.hasNext();) {
|
168 |
0
| Object o = j.next();
|
169 |
0
| if (o instanceof Concept)
|
170 |
0
| range.appendChild(serializer.createTextNode(((Concept) o)
|
171 |
| .getIdentifier().toString())); |
172 |
0
| else if (o instanceof WsmlDataType) {
|
173 |
0
| range.appendChild(serializer.createTextNode(
|
174 |
| ((WsmlDataType) o).getIRI().toString())); |
175 |
| } |
176 |
| else { |
177 |
0
| throw new RuntimeException("Unknown range for attribute!");
|
178 |
| } |
179 |
0
| break;
|
180 |
| } |
181 |
| } |
182 |
| |
183 |
0
| if (attribute.isSymmetric()) {
|
184 |
0
| attrElement.appendChild(serializer.createElement("symmetric"));
|
185 |
| } |
186 |
0
| if (attribute.isTransitive()) {
|
187 |
0
| attrElement.appendChild(serializer.createElement("transitive"));
|
188 |
| } |
189 |
0
| if (attribute.isReflexive()) {
|
190 |
0
| attrElement.appendChild(serializer.createElement("reflexive"));
|
191 |
| } |
192 |
0
| if (attribute.getInverseOf() != null) {
|
193 |
0
| Element inverseOf = serializer.createElement("inverseOf");
|
194 |
0
| attrElement.appendChild(inverseOf);
|
195 |
0
| inverseOf.setAttribute("type", attribute.getInverseOf().toString());
|
196 |
| } |
197 |
0
| if (attribute.getMinCardinality() > 0) {
|
198 |
0
| Element minCardinality = serializer.createElement("minCardinality");
|
199 |
0
| attrElement.appendChild(minCardinality);
|
200 |
0
| minCardinality.appendChild(serializer.createTextNode(String.valueOf(attribute
|
201 |
| .getMinCardinality()))); |
202 |
| } |
203 |
0
| if (attribute.getMaxCardinality() < Integer.MAX_VALUE) {
|
204 |
0
| Element maxCardinality = serializer.createElement("maxCardinality");
|
205 |
0
| attrElement.appendChild(maxCardinality);
|
206 |
0
| maxCardinality.appendChild(serializer.createTextNode(String.valueOf(attribute
|
207 |
| .getMaxCardinality()))); |
208 |
| } |
209 |
0
| if (!attribute.listNFPValues().isEmpty()) {
|
210 |
0
| NodeNFP.serialize(attrElement, attribute, serializer);
|
211 |
| } |
212 |
| } |
213 |
| } |
214 |
| |
215 |
0
| assert conceptElement != null;
|
216 |
0
| return conceptElement;
|
217 |
| } |
218 |
| } |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
| |
224 |
| |
225 |
| |
226 |
| |
227 |
| |
228 |
| |
229 |
| |
230 |
| |
231 |
| |
232 |
| |
233 |
| |
234 |
| |
235 |
| |
236 |
| |
237 |
| |
238 |
| |
239 |
| |
240 |
| |
241 |
| |
242 |
| |
243 |
| |
244 |
| |
245 |
| |
246 |
| |
247 |
| |
248 |
| |
249 |
| |
250 |
| |