1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 /***
19 * <p>Title: WSMO Studio</p>
20 * <p>Description: Semantic Web Service Editor</p>
21 * <p>Copyright: Copyright (c) 2004-2007</p>
22 * <p>Company: Ontotext Lab. / SIRMA </p>
23 */
24
25 package org.semanticgov.ui.editor;
26
27 import java.util.*;
28
29 import org.omwg.ontology.*;
30 import org.semanticgov.ui.Constants;
31 import org.wsmo.common.*;
32 import org.wsmo.common.exception.InvalidModelException;
33 import org.wsmo.factory.WsmoFactory;
34 import org.wsmo.service.*;
35 import org.wsmostudio.runtime.LogManager;
36 import org.wsmostudio.runtime.WSMORuntime;
37 import org.wsmostudio.ui.Utils;
38 import org.wsmostudio.ui.editors.model.ServiceDescriptionModel;
39
40
41 public class PAServiceModel extends ServiceDescriptionModel {
42
43 private Set<Ontology> importedOntosClosure = new HashSet<Ontology>();
44
45 private Ontology instanceOntology;
46 private Instance paInstance;
47 private WsmoFactory factory;
48
49
50 public PAServiceModel(WebService wsmoService) {
51 super(wsmoService);
52 this.factory = WSMORuntime.getRuntime().getWsmoFactory();
53 wsmoService.addOntology(this.factory.getOntology(Constants.GEA_ID));
54 doLoadImportedOntologies(wsmoService.listOntologies());
55 }
56
57 public Ontology getInstanceOntology() {
58
59 if (instanceOntology == null) {
60 ServiceDescription service = getServiceDescription();
61 Set<Object> instOntos = service.listNFPValues(Constants.NFP_INST_ONTOLOGY);
62 if (instOntos.size() > 0) {
63 this.instanceOntology = WSMORuntime.getIOManager().doLoadOntology(
64 (Identifier)instOntos.iterator().next());
65 }
66 for(Ontology iOnto : this.getServiceDescription().listOntologies()) {
67 this.instanceOntology.addOntology(iOnto);
68 }
69 }
70 if (this.instanceOntology == null) {
71 throw new RuntimeException("Instance ontology for service '"
72 + getServiceDescription().getIdentifier().toString()
73 + "' is not found!");
74 }
75 return this.instanceOntology;
76 }
77
78 public String getWSDL() {
79 Set<Object> wsdlServices =
80 getServiceDescription().listNFPValues(Constants.NFP_WSDL_ENDPOINT);
81 if (wsdlServices.size() == 0) {
82 return "";
83 }
84 else {
85 return wsdlServices.iterator().next().toString();
86 }
87 }
88
89 public void setWSDL(String wsdlTargetNamespace) {
90 try {
91 ServiceDescription service = getServiceDescription();
92 service.removeNFP(Constants.NFP_WSDL_ENDPOINT);
93 if (wsdlTargetNamespace != null
94 && wsdlTargetNamespace.trim().length() > 0) {
95 service.addNFPValue(Constants.NFP_WSDL_ENDPOINT,
96 factory.createIRI(wsdlTargetNamespace));
97 }
98 }
99 catch(InvalidModelException ime) {
100
101 }
102 setChanged();
103 }
104
105 public void addOntology(Ontology onto) {
106 getTopEntity().addOntology(onto);
107 doLoadImportedOntologies(getServiceDescription().listOntologies());
108 this.instanceOntology.addOntology(onto);
109 setChanged();
110 }
111
112 public void removeOntology(Ontology onto) {
113 if (onto.getIdentifier().equals(Constants.GEA_ID)) {
114 return;
115 }
116
117 getTopEntity().removeOntology(onto);
118 this.importedOntosClosure.clear();
119 this.instanceOntology.removeOntology(onto);
120 doLoadImportedOntologies(getServiceDescription().listOntologies());
121 setChanged();
122 }
123
124 public void reinitImportedOntologies() {
125 this.importedOntosClosure.clear();
126 doLoadImportedOntologies(getServiceDescription().listOntologies());
127 setChanged();
128 }
129
130 public Set<IRI> listAvailableServiceTypes() {
131 return UIUtils.getInstances(Constants.PUBLIC_SERVICE_TYPE, this.importedOntosClosure);
132 }
133 public Set<IRI> listAvailablePAServices() {
134 return UIUtils.getSubConcepts(Constants.PUBLIC_SERVICE_ID, this.importedOntosClosure);
135 }
136 public Set<IRI> listAvailableLaws() {
137 return UIUtils.getInstances(Constants.LAW, this.importedOntosClosure);
138 }
139 public Set<IRI> listAvailableDomains() {
140 return UIUtils.getInstances(Constants.PUBLIC_SERVICE_DOMAIN, this.importedOntosClosure);
141 }
142 public Set<IRI> listAvailableSubDomains() {
143 return UIUtils.listSubdomainEntries(getDomain(), this.importedOntosClosure);
144 }
145 public Set<IRI> listAvailableEffectTypes() {
146 return UIUtils.getInstances(Constants.PUBLIC_SERVICE_EFFECT_TYPE, this.importedOntosClosure);
147 }
148 public Set<IRI> listAvailableAdminLevels() {
149 return UIUtils.getInstances(Constants.ADMINISTRATION_LEVEL, this.importedOntosClosure);
150 }
151 public Set<IRI> listAvailableELocations() {
152 return UIUtils.getInstances(Constants.ELECTRONIC_LOCATOIN, this.importedOntosClosure);
153 }
154 public Set<IRI> listAvailablePhLocations() {
155 return UIUtils.getInstances(Constants.PHYSICAL_LOCATOIN, this.importedOntosClosure);
156 }
157
158 /***
159 *
160 * @return an IRI of concept PublicService
161 */
162 public IRI getPAInstanceType() {
163 findInitServiceInstance();
164 return getFirstValue(
165 this.paInstance.listConcepts());
166 }
167 /***
168 *
169 * @param typeInstance an IRI of concept PublicService
170 */
171 public void setPAInstanceType(IRI typeConcept) {
172 if (typeConcept == null) {
173 typeConcept = Constants.PUBLIC_SERVICE_ID;
174 }
175 if (secureEquals(typeConcept, getPAInstanceType())) {
176 return;
177 }
178 try {
179 for(Concept type : new HashSet<Concept>(this.paInstance.listConcepts())) {
180 this.paInstance.removeConcept(type);
181 }
182 this.paInstance.addConcept(factory.getConcept(typeConcept));
183 }
184 catch(InvalidModelException ime) {
185 LogManager.logError(ime);
186 }
187 setChanged();
188 }
189
190 /***
191 *
192 * @return an instance IRI of concept PublicServiceType
193 */
194 public IRI getServiceType() {
195 return getAttributeValue(Constants.PA_ATTR_HAS_SERVICE_TYPE);
196 }
197
198 /***
199 *
200 * @param typeInstance an instance IRI of concept PublicServiceType
201 */
202 public void setServiceType(IRI typeInstance) {
203 if (secureEquals(typeInstance, getServiceType())) {
204 return;
205 }
206 setAttributeValue(Constants.PA_ATTR_HAS_SERVICE_TYPE, typeInstance);
207 }
208
209 /***
210 *
211 * @return an instance IRI of concept Law
212 */
213 public IRI getGovernedBy() {
214 return getAttributeValue(Constants.PA_ATTR_GOVERNED_BY);
215 }
216
217 /***
218 *
219 * @param lawInstance an instance IRI of concept Law
220 */
221 public void setGovernedBy(IRI lawInstance) {
222 if (secureEquals(lawInstance, getGovernedBy())) {
223 return;
224 }
225 setAttributeValue(Constants.PA_ATTR_GOVERNED_BY, lawInstance);
226 }
227
228 /***
229 *
230 * @return an instance IRI of concept Public_Service_Domain
231 */
232 public IRI getDomain() {
233 return getAttributeValue(Constants.PA_ATTR_DOMAIN);
234 }
235
236 /***
237 *
238 * @param domainInstance an instance IRI of concept Public_Service_Domain
239 */
240 public void setDomain(IRI domainInstance) {
241 if (secureEquals(domainInstance, getDomain())) {
242 return;
243 }
244 setAttributeValue(Constants.PA_ATTR_DOMAIN, domainInstance);
245 }
246
247 /***
248 *
249 * @return an instance IRI of concept Public_Service_SubDomain
250 */
251 public IRI getSubDomain() {
252 return getAttributeValue(Constants.PA_ATTR_SUB_DOMAIN);
253 }
254
255 /***
256 *
257 * @param subDomainInstance an instance IRI of concept Public_Service_SubDomain
258 */
259 public void setSubDomain(IRI subDomainInstance) {
260 if (secureEquals(subDomainInstance, getSubDomain())) {
261 return;
262 }
263 setAttributeValue(Constants.PA_ATTR_SUB_DOMAIN, subDomainInstance);
264 }
265
266 /***
267 *
268 * @return an instance IRI of concept Public_Service_Effect_Type
269 */
270 public IRI getEffectType() {
271 return getAttributeValue(Constants.PA_ATTR_EFFECT_TYPE);
272 }
273
274 /***
275 *
276 * @param effectTypeInstance an instance IRI of concept Public_Service_Effect_Type
277 */
278 public void setEffectType(IRI effectTypeInstance) {
279 if (secureEquals(effectTypeInstance, getEffectType())) {
280 return;
281 }
282 setAttributeValue(Constants.PA_ATTR_EFFECT_TYPE, effectTypeInstance);
283 }
284
285 /***
286 *
287 * @return an instance IRI of concept Administration_Level
288 */
289 public IRI getAdminLevel() {
290 return getAttributeValue(Constants.PA_ATTR_ADMIN_LEVEL);
291 }
292
293 /***
294 *
295 * @param adminLevelInstance an instance IRI of concept Administration_Level
296 */
297 public void setAdminLevel(IRI adminLevelInstance) {
298 if (secureEquals(adminLevelInstance, getAdminLevel())) {
299 return;
300 }
301 setAttributeValue(Constants.PA_ATTR_ADMIN_LEVEL, adminLevelInstance);
302 }
303
304
305 /***
306 *
307 * @return an instance IRI of concept Electronic_Location
308 */
309 public IRI getElectronicLocation() {
310 findInitServiceInstance();
311 for(Value val : this.paInstance.listAttributeValues(Constants.PA_ATTR_LOCATION)) {
312 if (val instanceof Instance) {
313 Instance inst = (Instance)val;
314 for(Concept concept : inst.listConcepts()) {
315 if (concept.getIdentifier().equals(Constants.ELECTRONIC_LOCATOIN)) {
316 return (IRI)inst.getIdentifier();
317 }
318 }
319 }
320 }
321 return null;
322 }
323
324 /***
325 *
326 * @param eLocatgionInstance an instance IRI of concept Electronic_Location
327 */
328 public void setElectronicLocation(IRI eLocationInstance) {
329 if (secureEquals(eLocationInstance, getElectronicLocation())) {
330 return;
331 }
332 findInitServiceInstance();
333 IRI oldValue = getElectronicLocation();
334 if(oldValue != null) {
335 try {
336 this.paInstance.removeAttributeValue(
337 Constants.PA_ATTR_LOCATION, factory.getInstance(oldValue));
338 }
339 catch(InvalidModelException ime) {
340 LogManager.logError(ime);
341 }
342 }
343 if (eLocationInstance != null) {
344 try {
345 this.paInstance.addAttributeValue(Constants.PA_ATTR_LOCATION,
346 factory.getInstance(eLocationInstance));
347 }
348 catch(InvalidModelException ime) {
349 LogManager.logError(ime);
350 }
351 }
352 setChanged();
353 }
354
355 /***
356 *
357 * @return an instance IRI of concept Physical_Location
358 */
359 public IRI getPhysicalLocation() {
360 findInitServiceInstance();
361 for(Value val : this.paInstance.listAttributeValues(Constants.PA_ATTR_LOCATION)) {
362 if (val instanceof Instance) {
363 Instance inst = (Instance)val;
364 for(Concept concept : inst.listConcepts()) {
365 if (concept.getIdentifier().equals(Constants.PHYSICAL_LOCATOIN)) {
366 return (IRI)inst.getIdentifier();
367 }
368 }
369 }
370 }
371 return null;
372 }
373
374 /***
375 *
376 * @param phLocatgionInstance an instance IRI of concept Electronic_Location
377 */
378 public void setPhysicalLocation(IRI phLocationInstance) {
379 if (secureEquals(phLocationInstance, getPhysicalLocation())) {
380 return;
381 }
382 findInitServiceInstance();
383 IRI oldValue = getPhysicalLocation();
384 if(oldValue != null) {
385 try {
386 this.paInstance.removeAttributeValue(
387 Constants.PA_ATTR_LOCATION, factory.getInstance(oldValue));
388 }
389 catch(InvalidModelException ime) {
390 LogManager.logError(ime);
391 }
392 }
393 if (phLocationInstance != null) {
394 try {
395 this.paInstance.addAttributeValue(Constants.PA_ATTR_LOCATION,
396 factory.getInstance(phLocationInstance));
397 }
398 catch(InvalidModelException ime) {
399 LogManager.logError(ime);
400 }
401 }
402 setChanged();
403 }
404
405 /***
406 *
407 * @return a list of instance IRI of concept Societal_Entity
408 */
409 public Set<IRI> listClientTypes() {
410 return readAttributeValues(Constants.PA_ATTR_CLIENT_TYPE);
411 }
412
413 /***
414 *
415 * @param societalEntityInstance an instance IRI of concept SocietalEntity
416 */
417 public void addClientType(IRI societalEntityInstance) {
418 addNewValue(Constants.PA_ATTR_CLIENT_TYPE, societalEntityInstance, listClientTypes());
419 }
420 /***
421 *
422 * @param societalEntityInstance an instance IRI of concept Societal_Entity
423 */
424 public void removeClientType(IRI societalEntityInstance) {
425 removeValue(Constants.PA_ATTR_CLIENT_TYPE, societalEntityInstance, listClientTypes());
426 }
427
428 /***
429 *
430 * @return a list of instance IRI of concept ServiceProvider
431 */
432 public Set<IRI> listServiceProviders() {
433 return readAttributeValues(Constants.PA_ATTR_IS_PROVIDED_BY);
434 }
435 /***
436 *
437 * @param societalEntityInstance an instance IRI of concept ServiceProvider
438 */
439 public void addServiceProvider(IRI serviceProvider) {
440 addNewValue(Constants.PA_ATTR_IS_PROVIDED_BY, serviceProvider, listServiceProviders());
441 }
442 /***
443 *
444 * @param societalEntityInstance an instance IRI of concept ServiceProvider
445 */
446 public void removeServiceProvider(IRI serviceProvider) {
447 removeValue(Constants.PA_ATTR_IS_PROVIDED_BY, serviceProvider, listServiceProviders());
448 }
449
450 /***
451 *
452 * @return a list of instance IRI of concept ServiceProcess
453 */
454 public Set<IRI> listServiceProcesses() {
455 return readAttributeValues(Constants.PA_ATTR_HAS_PROCESS);
456 }
457 /***
458 *
459 * @param societalEntityInstance an instance IRI of concept ServiceProcess
460 */
461 public void addServiceProcess(IRI serviceProcess) {
462 addNewValue(Constants.PA_ATTR_HAS_PROCESS, serviceProcess, listServiceProcesses());
463 }
464 /***
465 *
466 * @param societalEntityInstance an instance IRI of concept ServiceProcess
467 */
468 public void removeServiceProcess(IRI serviceProcess) {
469 removeValue(Constants.PA_ATTR_HAS_PROCESS, serviceProcess, listServiceProcesses());
470 }
471
472 /***
473 *
474 * @return a list of instance IRI of concept Evidence_Placeholder
475 */
476 public Set<IRI> listInputs() {
477 return readAttributeValues(Constants.PA_ATTR_INPUT);
478 }
479 /***
480 *
481 * @param inputInstance an instance IRI of concept Evidence_Placeholder
482 */
483 public void addInput(IRI inputInstance) {
484 addNewValue(Constants.PA_ATTR_INPUT, inputInstance, listInputs());
485 }
486 /***
487 *
488 * @param inputInstance an instance IRI of concept Evidence_Placeholder
489 */
490 public void removeInput(IRI inputInstance) {
491 removeValue(Constants.PA_ATTR_INPUT, inputInstance, listInputs());
492 }
493
494 /***
495 *
496 * @return a list of instance IRI of concept Evidence_Placeholder
497 */
498 public Set<IRI> listOutputs() {
499 return readAttributeValues(Constants.PA_ATTR_OUTCOME);
500 }
501 /***
502 *
503 * @param outputInstance an instance IRI of concept Evidence_Placeholder
504 */
505 public void addOutput(IRI outputInstance) {
506 addNewValue(Constants.PA_ATTR_OUTCOME, outputInstance, listOutputs());
507 }
508
509 /***
510 *
511 * @param inputInstance an instance IRI of concept Evidence_Placeholder
512 */
513 public void removeOutput(IRI outputInstance) {
514 removeValue(Constants.PA_ATTR_OUTCOME, outputInstance, listOutputs());
515 }
516
517 public Set<Axiom> listPreconditions() {
518 ServiceDescription service = getServiceDescription();
519 if (service == null) {
520 return new HashSet<Axiom>();
521 }
522 Capability cap = service.getCapability();
523 if (cap == null) {
524 return new HashSet<Axiom>();
525 }
526 return cap.listPreConditions();
527 }
528
529 public void addPrecondition(Axiom precond) {
530 ensureHasCapability();
531 try {
532 getServiceDescription().getCapability().addPreCondition(precond);
533 }
534 catch(InvalidModelException ime) {
535 LogManager.logError(ime);
536 }
537 setChanged();
538 }
539 public void removePrecondition(Axiom precond) {
540 ensureHasCapability();
541 try {
542 getServiceDescription().getCapability().removePreCondition(precond);
543 }
544 catch(InvalidModelException ime) {
545 LogManager.logError(ime);
546 }
547 setChanged();
548 }
549
550 public Set<Axiom> listEffects() {
551 ServiceDescription service = getServiceDescription();
552 if (service == null) {
553 return new HashSet<Axiom>();
554 }
555 Capability cap = service.getCapability();
556 if (cap == null) {
557 return new HashSet<Axiom>();
558 }
559 return cap.listEffects();
560 }
561
562 public void addEffect(Axiom effect) {
563 ensureHasCapability();
564 try {
565 getServiceDescription().getCapability().addEffect(effect);
566 }
567 catch(InvalidModelException ime) {
568 LogManager.logError(ime);
569 }
570 setChanged();
571 }
572 public void removeEffect(Axiom effect) {
573 ensureHasCapability();
574 try {
575 getServiceDescription().getCapability().removeEffect(effect);
576 }
577 catch(InvalidModelException ime) {
578 LogManager.logError(ime);
579 }
580 setChanged();
581 }
582
583
584
585 private Set<IRI> readAttributeValues(IRI propID) {
586 findInitServiceInstance();
587 Set<IRI> result = new HashSet<IRI>();
588 Set<Value> rawValues = this.paInstance.listAttributeValues(propID);
589 for(Value value : rawValues) {
590 if (value instanceof Instance
591 && ((Instance)value).getIdentifier() instanceof IRI) {
592 result.add((IRI)((Instance)value).getIdentifier());
593 }
594 }
595 return result;
596 }
597
598 private void addNewValue(IRI propID, IRI newValue, Set<IRI> oldValues) {
599 if (oldValues.contains(newValue)) {
600 return;
601 }
602 try {
603 this.paInstance.addAttributeValue(propID, factory.getInstance(newValue));
604 }
605 catch(InvalidModelException ime) {
606 LogManager.logError(ime);
607 }
608 setChanged();
609 }
610
611 public void removeValue(IRI propID, IRI oldValue, Set<IRI> existingValues) {
612 if (false == existingValues.contains(oldValue)) {
613 return;
614 }
615 try {
616 this.paInstance.removeAttributeValue(propID, factory.getInstance(oldValue));
617 }
618 catch(InvalidModelException ime) {
619 LogManager.logError(ime);
620 }
621 setChanged();
622 }
623
624 private void ensureHasCapability() {
625 ServiceDescription service = getServiceDescription();
626 if (service.getCapability() != null) {
627 return;
628 }
629 WsmoFactory factory = WSMORuntime.getRuntime().getWsmoFactory();
630 IRI serviceID = (IRI)service.getIdentifier();
631 IRI newCapID = factory.createIRI(serviceID.getNamespace()
632 + "Capability_" + serviceID.getLocalName());
633 Capability newCap = factory.createCapability(newCapID);
634 service.setCapability(newCap);
635 }
636
637 private void findInitServiceInstance() {
638 if (paInstance != null) {
639 return;
640 }
641
642 Concept paServiceConcept = this.factory.getConcept(Constants.PUBLIC_SERVICE_ID);
643 for(Instance inst : getInstanceOntology().listInstances()) {
644
645 if(inst.listConcepts().contains(paServiceConcept)){
646 this.paInstance = inst;
647 return;
648 }
649 for(Concept concept : inst.listConcepts()) {
650 if (UIUtils.isSubconceptOf(concept, paServiceConcept)) {
651 this.paInstance = inst;
652 return;
653 }
654 }
655 }
656
657 WsmoFactory fact = WSMORuntime.getRuntime().getWsmoFactory();
658 Concept paConcept = fact.getConcept(Constants.PUBLIC_SERVICE_ID);
659 try {
660 this.paInstance = WSMORuntime.getRuntime().getWsmoFactory().createInstance(
661 fact.createAnonymousID(), paConcept);
662 }
663 catch(InvalidModelException ime) {
664 LogManager.logError(ime);
665 }
666 if (this.paInstance != null) {
667 try {
668 this.instanceOntology.addInstance(this.paInstance);
669 }
670 catch(InvalidModelException ime) {
671 LogManager.logError(ime);
672 }
673 }
674 }
675
676 private void setAttributeValue(IRI attribute, IRI instanceID) {
677 findInitServiceInstance();
678 try {
679 this.paInstance.removeAttributeValues(attribute);
680 if (instanceID != null) {
681 this.paInstance.addAttributeValue(attribute,
682 factory.getInstance(instanceID));
683 }
684 }
685 catch(InvalidModelException ime) {
686 LogManager.logError(ime);
687 }
688 setChanged();
689 }
690
691 private IRI getAttributeValue(IRI attribute) {
692 findInitServiceInstance();
693 return getFirstValue(this.paInstance.listAttributeValues(attribute));
694
695 }
696
697 private IRI getFirstValue(Set<?> values) {
698 for(Iterator<?> it = values.iterator(); it.hasNext(); ) {
699 Object value = it.next();
700 if (value instanceof IRI) {
701 return (IRI)value;
702 }
703 if (value instanceof Entity) {
704 return (IRI)((Entity)value).getIdentifier();
705 }
706 }
707 return null;
708 }
709
710 private boolean secureEquals(Object o1, Object o2) {
711 if (o1 == null) {
712 return o2 == null;
713 }
714 else {
715 return o1.equals(o2);
716 }
717 }
718
719 /***
720 * Forces the loading of imported ontologies if any. The loading is fail-silent, i.e. not loaded, no error
721 * @param onto
722 */
723 private void doLoadImportedOntologies(Set<Ontology> ontos) {
724 if (ontos.size() == 0) {
725 return;
726 }
727 for(Ontology iOnto : ontos) {
728 if (importedOntosClosure.contains(iOnto)) {
729 continue;
730 }
731 else {
732 if (Utils.isAProxy(iOnto)) {
733 WSMORuntime.getIOManager().doLoadOntology(iOnto.getIdentifier(), true);
734 }
735 if (Utils.isAProxy(iOnto) == false) {
736 importedOntosClosure.add(iOnto);
737 doLoadImportedOntologies(iOnto.listOntologies());
738 }
739 }
740 }
741 }
742
743
744 }
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766